]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Update frame title when redisplay scrolls selected window
[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 #ifdef GLYPH_DEBUG
624
625 /* True means print traces of redisplay if compiled with
626 GLYPH_DEBUG defined. */
627
628 bool trace_redisplay_p;
629
630 #endif /* GLYPH_DEBUG */
631
632 #ifdef DEBUG_TRACE_MOVE
633 /* True means trace with TRACE_MOVE to stderr. */
634 static bool trace_move;
635
636 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
637 #else
638 #define TRACE_MOVE(x) (void) 0
639 #endif
640
641 /* Buffer being redisplayed -- for redisplay_window_error. */
642
643 static struct buffer *displayed_buffer;
644
645 /* Value returned from text property handlers (see below). */
646
647 enum prop_handled
648 {
649 HANDLED_NORMALLY,
650 HANDLED_RECOMPUTE_PROPS,
651 HANDLED_OVERLAY_STRING_CONSUMED,
652 HANDLED_RETURN
653 };
654
655 /* A description of text properties that redisplay is interested
656 in. */
657
658 struct props
659 {
660 /* The symbol index of the name of the property. */
661 short name;
662
663 /* A unique index for the property. */
664 enum prop_idx idx;
665
666 /* A handler function called to set up iterator IT from the property
667 at IT's current position. Value is used to steer handle_stop. */
668 enum prop_handled (*handler) (struct it *it);
669 };
670
671 static enum prop_handled handle_face_prop (struct it *);
672 static enum prop_handled handle_invisible_prop (struct it *);
673 static enum prop_handled handle_display_prop (struct it *);
674 static enum prop_handled handle_composition_prop (struct it *);
675 static enum prop_handled handle_overlay_change (struct it *);
676 static enum prop_handled handle_fontified_prop (struct it *);
677
678 /* Properties handled by iterators. */
679
680 static struct props it_props[] =
681 {
682 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
683 /* Handle `face' before `display' because some sub-properties of
684 `display' need to know the face. */
685 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
686 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
687 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
688 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
689 {0, 0, NULL}
690 };
691
692 /* Value is the position described by X. If X is a marker, value is
693 the marker_position of X. Otherwise, value is X. */
694
695 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
696
697 /* Enumeration returned by some move_it_.* functions internally. */
698
699 enum move_it_result
700 {
701 /* Not used. Undefined value. */
702 MOVE_UNDEFINED,
703
704 /* Move ended at the requested buffer position or ZV. */
705 MOVE_POS_MATCH_OR_ZV,
706
707 /* Move ended at the requested X pixel position. */
708 MOVE_X_REACHED,
709
710 /* Move within a line ended at the end of a line that must be
711 continued. */
712 MOVE_LINE_CONTINUED,
713
714 /* Move within a line ended at the end of a line that would
715 be displayed truncated. */
716 MOVE_LINE_TRUNCATED,
717
718 /* Move within a line ended at a line end. */
719 MOVE_NEWLINE_OR_CR
720 };
721
722 /* This counter is used to clear the face cache every once in a while
723 in redisplay_internal. It is incremented for each redisplay.
724 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
725 cleared. */
726
727 #define CLEAR_FACE_CACHE_COUNT 500
728 static int clear_face_cache_count;
729
730 /* Similarly for the image cache. */
731
732 #ifdef HAVE_WINDOW_SYSTEM
733 #define CLEAR_IMAGE_CACHE_COUNT 101
734 static int clear_image_cache_count;
735
736 /* Null glyph slice */
737 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
738 #endif
739
740 /* True while redisplay_internal is in progress. */
741
742 bool redisplaying_p;
743
744 /* If a string, XTread_socket generates an event to display that string.
745 (The display is done in read_char.) */
746
747 Lisp_Object help_echo_string;
748 Lisp_Object help_echo_window;
749 Lisp_Object help_echo_object;
750 ptrdiff_t help_echo_pos;
751
752 /* Temporary variable for XTread_socket. */
753
754 Lisp_Object previous_help_echo_string;
755
756 /* Platform-independent portion of hourglass implementation. */
757
758 #ifdef HAVE_WINDOW_SYSTEM
759
760 /* True means an hourglass cursor is currently shown. */
761 static bool hourglass_shown_p;
762
763 /* If non-null, an asynchronous timer that, when it expires, displays
764 an hourglass cursor on all frames. */
765 static struct atimer *hourglass_atimer;
766
767 #endif /* HAVE_WINDOW_SYSTEM */
768
769 /* Default number of seconds to wait before displaying an hourglass
770 cursor. */
771 #define DEFAULT_HOURGLASS_DELAY 1
772
773 #ifdef HAVE_WINDOW_SYSTEM
774
775 /* Default pixel width of `thin-space' display method. */
776 #define THIN_SPACE_WIDTH 1
777
778 #endif /* HAVE_WINDOW_SYSTEM */
779
780 /* Function prototypes. */
781
782 static void setup_for_ellipsis (struct it *, int);
783 static void set_iterator_to_next (struct it *, bool);
784 static void mark_window_display_accurate_1 (struct window *, bool);
785 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
786 static bool cursor_row_p (struct glyph_row *);
787 static int redisplay_mode_lines (Lisp_Object, bool);
788
789 static void handle_line_prefix (struct it *);
790
791 static void handle_stop_backwards (struct it *, ptrdiff_t);
792 static void unwind_with_echo_area_buffer (Lisp_Object);
793 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
794 static bool current_message_1 (ptrdiff_t, Lisp_Object);
795 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
796 static void set_message (Lisp_Object);
797 static bool set_message_1 (ptrdiff_t, Lisp_Object);
798 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
799 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
800 static void unwind_redisplay (void);
801 static void extend_face_to_end_of_line (struct it *);
802 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
803 static void push_it (struct it *, struct text_pos *);
804 static void iterate_out_of_display_property (struct it *);
805 static void pop_it (struct it *);
806 static void redisplay_internal (void);
807 static void echo_area_display (bool);
808 static void redisplay_windows (Lisp_Object);
809 static void redisplay_window (Lisp_Object, bool);
810 static Lisp_Object redisplay_window_error (Lisp_Object);
811 static Lisp_Object redisplay_window_0 (Lisp_Object);
812 static Lisp_Object redisplay_window_1 (Lisp_Object);
813 static bool set_cursor_from_row (struct window *, struct glyph_row *,
814 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
815 int, int);
816 static bool update_menu_bar (struct frame *, bool, bool);
817 static bool try_window_reusing_current_matrix (struct window *);
818 static int try_window_id (struct window *);
819 static bool display_line (struct it *);
820 static int display_mode_lines (struct window *);
821 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
822 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
823 Lisp_Object, bool);
824 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
825 Lisp_Object);
826 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
827 static void display_menu_bar (struct window *);
828 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
829 ptrdiff_t *);
830 static int display_string (const char *, Lisp_Object, Lisp_Object,
831 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
832 static void compute_line_metrics (struct it *);
833 static void run_redisplay_end_trigger_hook (struct it *);
834 static bool get_overlay_strings (struct it *, ptrdiff_t);
835 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
836 static void next_overlay_string (struct it *);
837 static void reseat (struct it *, struct text_pos, bool);
838 static void reseat_1 (struct it *, struct text_pos, bool);
839 static bool next_element_from_display_vector (struct it *);
840 static bool next_element_from_string (struct it *);
841 static bool next_element_from_c_string (struct it *);
842 static bool next_element_from_buffer (struct it *);
843 static bool next_element_from_composition (struct it *);
844 static bool next_element_from_image (struct it *);
845 static bool next_element_from_stretch (struct it *);
846 static void load_overlay_strings (struct it *, ptrdiff_t);
847 static bool get_next_display_element (struct it *);
848 static enum move_it_result
849 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
850 enum move_operation_enum);
851 static void get_visually_first_element (struct it *);
852 static void compute_stop_pos (struct it *);
853 static int face_before_or_after_it_pos (struct it *, bool);
854 static ptrdiff_t next_overlay_change (ptrdiff_t);
855 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
856 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
857 static int handle_single_display_spec (struct it *, Lisp_Object,
858 Lisp_Object, Lisp_Object,
859 struct text_pos *, ptrdiff_t, int, bool);
860 static int underlying_face_id (struct it *);
861
862 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
863 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
864
865 #ifdef HAVE_WINDOW_SYSTEM
866
867 static void update_tool_bar (struct frame *, bool);
868 static void x_draw_bottom_divider (struct window *w);
869 static void notice_overwritten_cursor (struct window *,
870 enum glyph_row_area,
871 int, int, int, int);
872 static int normal_char_height (struct font *, int);
873 static void normal_char_ascent_descent (struct font *, int, int *, int *);
874
875 static void append_stretch_glyph (struct it *, Lisp_Object,
876 int, int, int);
877
878 static Lisp_Object get_it_property (struct it *, Lisp_Object);
879 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
880 struct font *, int, bool);
881
882 #endif /* HAVE_WINDOW_SYSTEM */
883
884 static void produce_special_glyphs (struct it *, enum display_element_type);
885 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
886 static bool coords_in_mouse_face_p (struct window *, int, int);
887
888
889 \f
890 /***********************************************************************
891 Window display dimensions
892 ***********************************************************************/
893
894 /* Return the bottom boundary y-position for text lines in window W.
895 This is the first y position at which a line cannot start.
896 It is relative to the top of the window.
897
898 This is the height of W minus the height of a mode line, if any. */
899
900 int
901 window_text_bottom_y (struct window *w)
902 {
903 int height = WINDOW_PIXEL_HEIGHT (w);
904
905 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
906
907 if (WINDOW_WANTS_MODELINE_P (w))
908 height -= CURRENT_MODE_LINE_HEIGHT (w);
909
910 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
911
912 return height;
913 }
914
915 /* Return the pixel width of display area AREA of window W.
916 ANY_AREA means return the total width of W, not including
917 fringes to the left and right of the window. */
918
919 int
920 window_box_width (struct window *w, enum glyph_row_area area)
921 {
922 int width = w->pixel_width;
923
924 if (!w->pseudo_window_p)
925 {
926 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
927 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
928
929 if (area == TEXT_AREA)
930 width -= (WINDOW_MARGINS_WIDTH (w)
931 + WINDOW_FRINGES_WIDTH (w));
932 else if (area == LEFT_MARGIN_AREA)
933 width = WINDOW_LEFT_MARGIN_WIDTH (w);
934 else if (area == RIGHT_MARGIN_AREA)
935 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
936 }
937
938 /* With wide margins, fringes, etc. we might end up with a negative
939 width, correct that here. */
940 return max (0, width);
941 }
942
943
944 /* Return the pixel height of the display area of window W, not
945 including mode lines of W, if any. */
946
947 int
948 window_box_height (struct window *w)
949 {
950 struct frame *f = XFRAME (w->frame);
951 int height = WINDOW_PIXEL_HEIGHT (w);
952
953 eassert (height >= 0);
954
955 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
956 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
957
958 /* Note: the code below that determines the mode-line/header-line
959 height is essentially the same as that contained in the macro
960 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
961 the appropriate glyph row has its `mode_line_p' flag set,
962 and if it doesn't, uses estimate_mode_line_height instead. */
963
964 if (WINDOW_WANTS_MODELINE_P (w))
965 {
966 struct glyph_row *ml_row
967 = (w->current_matrix && w->current_matrix->rows
968 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
969 : 0);
970 if (ml_row && ml_row->mode_line_p)
971 height -= ml_row->height;
972 else
973 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
974 }
975
976 if (WINDOW_WANTS_HEADER_LINE_P (w))
977 {
978 struct glyph_row *hl_row
979 = (w->current_matrix && w->current_matrix->rows
980 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
981 : 0);
982 if (hl_row && hl_row->mode_line_p)
983 height -= hl_row->height;
984 else
985 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
986 }
987
988 /* With a very small font and a mode-line that's taller than
989 default, we might end up with a negative height. */
990 return max (0, height);
991 }
992
993 /* Return the window-relative coordinate of the left edge of display
994 area AREA of window W. ANY_AREA means return the left edge of the
995 whole window, to the right of the left fringe of W. */
996
997 int
998 window_box_left_offset (struct window *w, enum glyph_row_area area)
999 {
1000 int x;
1001
1002 if (w->pseudo_window_p)
1003 return 0;
1004
1005 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1006
1007 if (area == TEXT_AREA)
1008 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1009 + window_box_width (w, LEFT_MARGIN_AREA));
1010 else if (area == RIGHT_MARGIN_AREA)
1011 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1012 + window_box_width (w, LEFT_MARGIN_AREA)
1013 + window_box_width (w, TEXT_AREA)
1014 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1015 ? 0
1016 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1017 else if (area == LEFT_MARGIN_AREA
1018 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1019 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1020
1021 /* Don't return more than the window's pixel width. */
1022 return min (x, w->pixel_width);
1023 }
1024
1025
1026 /* Return the window-relative coordinate of the right edge of display
1027 area AREA of window W. ANY_AREA means return the right edge of the
1028 whole window, to the left of the right fringe of W. */
1029
1030 static int
1031 window_box_right_offset (struct window *w, enum glyph_row_area area)
1032 {
1033 /* Don't return more than the window's pixel width. */
1034 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1035 w->pixel_width);
1036 }
1037
1038 /* Return the frame-relative coordinate of the left edge of display
1039 area AREA of window W. ANY_AREA means return the left edge of the
1040 whole window, to the right of the left fringe of W. */
1041
1042 int
1043 window_box_left (struct window *w, enum glyph_row_area area)
1044 {
1045 struct frame *f = XFRAME (w->frame);
1046 int x;
1047
1048 if (w->pseudo_window_p)
1049 return FRAME_INTERNAL_BORDER_WIDTH (f);
1050
1051 x = (WINDOW_LEFT_EDGE_X (w)
1052 + window_box_left_offset (w, area));
1053
1054 return x;
1055 }
1056
1057
1058 /* Return the frame-relative coordinate of the right edge of display
1059 area AREA of window W. ANY_AREA means return the right edge of the
1060 whole window, to the left of the right fringe of W. */
1061
1062 int
1063 window_box_right (struct window *w, enum glyph_row_area area)
1064 {
1065 return window_box_left (w, area) + window_box_width (w, area);
1066 }
1067
1068 /* Get the bounding box of the display area AREA of window W, without
1069 mode lines, in frame-relative coordinates. ANY_AREA means the
1070 whole window, not including the left and right fringes of
1071 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1072 coordinates of the upper-left corner of the box. Return in
1073 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1074
1075 void
1076 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1077 int *box_y, int *box_width, int *box_height)
1078 {
1079 if (box_width)
1080 *box_width = window_box_width (w, area);
1081 if (box_height)
1082 *box_height = window_box_height (w);
1083 if (box_x)
1084 *box_x = window_box_left (w, area);
1085 if (box_y)
1086 {
1087 *box_y = WINDOW_TOP_EDGE_Y (w);
1088 if (WINDOW_WANTS_HEADER_LINE_P (w))
1089 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1090 }
1091 }
1092
1093 #ifdef HAVE_WINDOW_SYSTEM
1094
1095 /* Get the bounding box of the display area AREA of window W, without
1096 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1097 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1098 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1099 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1100 box. */
1101
1102 static void
1103 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1104 int *bottom_right_x, int *bottom_right_y)
1105 {
1106 window_box (w, ANY_AREA, top_left_x, top_left_y,
1107 bottom_right_x, bottom_right_y);
1108 *bottom_right_x += *top_left_x;
1109 *bottom_right_y += *top_left_y;
1110 }
1111
1112 #endif /* HAVE_WINDOW_SYSTEM */
1113
1114 /***********************************************************************
1115 Utilities
1116 ***********************************************************************/
1117
1118 /* Return the bottom y-position of the line the iterator IT is in.
1119 This can modify IT's settings. */
1120
1121 int
1122 line_bottom_y (struct it *it)
1123 {
1124 int line_height = it->max_ascent + it->max_descent;
1125 int line_top_y = it->current_y;
1126
1127 if (line_height == 0)
1128 {
1129 if (last_height)
1130 line_height = last_height;
1131 else if (IT_CHARPOS (*it) < ZV)
1132 {
1133 move_it_by_lines (it, 1);
1134 line_height = (it->max_ascent || it->max_descent
1135 ? it->max_ascent + it->max_descent
1136 : last_height);
1137 }
1138 else
1139 {
1140 struct glyph_row *row = it->glyph_row;
1141
1142 /* Use the default character height. */
1143 it->glyph_row = NULL;
1144 it->what = IT_CHARACTER;
1145 it->c = ' ';
1146 it->len = 1;
1147 PRODUCE_GLYPHS (it);
1148 line_height = it->ascent + it->descent;
1149 it->glyph_row = row;
1150 }
1151 }
1152
1153 return line_top_y + line_height;
1154 }
1155
1156 DEFUN ("line-pixel-height", Fline_pixel_height,
1157 Sline_pixel_height, 0, 0, 0,
1158 doc: /* Return height in pixels of text line in the selected window.
1159
1160 Value is the height in pixels of the line at point. */)
1161 (void)
1162 {
1163 struct it it;
1164 struct text_pos pt;
1165 struct window *w = XWINDOW (selected_window);
1166 struct buffer *old_buffer = NULL;
1167 Lisp_Object result;
1168
1169 if (XBUFFER (w->contents) != current_buffer)
1170 {
1171 old_buffer = current_buffer;
1172 set_buffer_internal_1 (XBUFFER (w->contents));
1173 }
1174 SET_TEXT_POS (pt, PT, PT_BYTE);
1175 start_display (&it, w, pt);
1176 it.vpos = it.current_y = 0;
1177 last_height = 0;
1178 result = make_number (line_bottom_y (&it));
1179 if (old_buffer)
1180 set_buffer_internal_1 (old_buffer);
1181
1182 return result;
1183 }
1184
1185 /* Return the default pixel height of text lines in window W. The
1186 value is the canonical height of the W frame's default font, plus
1187 any extra space required by the line-spacing variable or frame
1188 parameter.
1189
1190 Implementation note: this ignores any line-spacing text properties
1191 put on the newline characters. This is because those properties
1192 only affect the _screen_ line ending in the newline (i.e., in a
1193 continued line, only the last screen line will be affected), which
1194 means only a small number of lines in a buffer can ever use this
1195 feature. Since this function is used to compute the default pixel
1196 equivalent of text lines in a window, we can safely ignore those
1197 few lines. For the same reasons, we ignore the line-height
1198 properties. */
1199 int
1200 default_line_pixel_height (struct window *w)
1201 {
1202 struct frame *f = WINDOW_XFRAME (w);
1203 int height = FRAME_LINE_HEIGHT (f);
1204
1205 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1206 {
1207 struct buffer *b = XBUFFER (w->contents);
1208 Lisp_Object val = BVAR (b, extra_line_spacing);
1209
1210 if (NILP (val))
1211 val = BVAR (&buffer_defaults, extra_line_spacing);
1212 if (!NILP (val))
1213 {
1214 if (RANGED_INTEGERP (0, val, INT_MAX))
1215 height += XFASTINT (val);
1216 else if (FLOATP (val))
1217 {
1218 int addon = XFLOAT_DATA (val) * height + 0.5;
1219
1220 if (addon >= 0)
1221 height += addon;
1222 }
1223 }
1224 else
1225 height += f->extra_line_spacing;
1226 }
1227
1228 return height;
1229 }
1230
1231 /* Subroutine of pos_visible_p below. Extracts a display string, if
1232 any, from the display spec given as its argument. */
1233 static Lisp_Object
1234 string_from_display_spec (Lisp_Object spec)
1235 {
1236 if (CONSP (spec))
1237 {
1238 while (CONSP (spec))
1239 {
1240 if (STRINGP (XCAR (spec)))
1241 return XCAR (spec);
1242 spec = XCDR (spec);
1243 }
1244 }
1245 else if (VECTORP (spec))
1246 {
1247 ptrdiff_t i;
1248
1249 for (i = 0; i < ASIZE (spec); i++)
1250 {
1251 if (STRINGP (AREF (spec, i)))
1252 return AREF (spec, i);
1253 }
1254 return Qnil;
1255 }
1256
1257 return spec;
1258 }
1259
1260
1261 /* Limit insanely large values of W->hscroll on frame F to the largest
1262 value that will still prevent first_visible_x and last_visible_x of
1263 'struct it' from overflowing an int. */
1264 static int
1265 window_hscroll_limited (struct window *w, struct frame *f)
1266 {
1267 ptrdiff_t window_hscroll = w->hscroll;
1268 int window_text_width = window_box_width (w, TEXT_AREA);
1269 int colwidth = FRAME_COLUMN_WIDTH (f);
1270
1271 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1272 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1273
1274 return window_hscroll;
1275 }
1276
1277 /* Return true if position CHARPOS is visible in window W.
1278 CHARPOS < 0 means return info about WINDOW_END position.
1279 If visible, set *X and *Y to pixel coordinates of top left corner.
1280 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1281 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1282
1283 bool
1284 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1285 int *rtop, int *rbot, int *rowh, int *vpos)
1286 {
1287 struct it it;
1288 void *itdata = bidi_shelve_cache ();
1289 struct text_pos top;
1290 bool visible_p = false;
1291 struct buffer *old_buffer = NULL;
1292 bool r2l = false;
1293
1294 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1295 return visible_p;
1296
1297 if (XBUFFER (w->contents) != current_buffer)
1298 {
1299 old_buffer = current_buffer;
1300 set_buffer_internal_1 (XBUFFER (w->contents));
1301 }
1302
1303 SET_TEXT_POS_FROM_MARKER (top, w->start);
1304 /* Scrolling a minibuffer window via scroll bar when the echo area
1305 shows long text sometimes resets the minibuffer contents behind
1306 our backs. */
1307 if (CHARPOS (top) > ZV)
1308 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1309
1310 /* Compute exact mode line heights. */
1311 if (WINDOW_WANTS_MODELINE_P (w))
1312 w->mode_line_height
1313 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1314 BVAR (current_buffer, mode_line_format));
1315
1316 if (WINDOW_WANTS_HEADER_LINE_P (w))
1317 w->header_line_height
1318 = display_mode_line (w, HEADER_LINE_FACE_ID,
1319 BVAR (current_buffer, header_line_format));
1320
1321 start_display (&it, w, top);
1322 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1323 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1324
1325 if (charpos >= 0
1326 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1327 && IT_CHARPOS (it) >= charpos)
1328 /* When scanning backwards under bidi iteration, move_it_to
1329 stops at or _before_ CHARPOS, because it stops at or to
1330 the _right_ of the character at CHARPOS. */
1331 || (it.bidi_p && it.bidi_it.scan_dir == -1
1332 && IT_CHARPOS (it) <= charpos)))
1333 {
1334 /* We have reached CHARPOS, or passed it. How the call to
1335 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1336 or covered by a display property, move_it_to stops at the end
1337 of the invisible text, to the right of CHARPOS. (ii) If
1338 CHARPOS is in a display vector, move_it_to stops on its last
1339 glyph. */
1340 int top_x = it.current_x;
1341 int top_y = it.current_y;
1342 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1343 int bottom_y;
1344 struct it save_it;
1345 void *save_it_data = NULL;
1346
1347 /* Calling line_bottom_y may change it.method, it.position, etc. */
1348 SAVE_IT (save_it, it, save_it_data);
1349 last_height = 0;
1350 bottom_y = line_bottom_y (&it);
1351 if (top_y < window_top_y)
1352 visible_p = bottom_y > window_top_y;
1353 else if (top_y < it.last_visible_y)
1354 visible_p = true;
1355 if (bottom_y >= it.last_visible_y
1356 && it.bidi_p && it.bidi_it.scan_dir == -1
1357 && IT_CHARPOS (it) < charpos)
1358 {
1359 /* When the last line of the window is scanned backwards
1360 under bidi iteration, we could be duped into thinking
1361 that we have passed CHARPOS, when in fact move_it_to
1362 simply stopped short of CHARPOS because it reached
1363 last_visible_y. To see if that's what happened, we call
1364 move_it_to again with a slightly larger vertical limit,
1365 and see if it actually moved vertically; if it did, we
1366 didn't really reach CHARPOS, which is beyond window end. */
1367 /* Why 10? because we don't know how many canonical lines
1368 will the height of the next line(s) be. So we guess. */
1369 int ten_more_lines = 10 * default_line_pixel_height (w);
1370
1371 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1372 MOVE_TO_POS | MOVE_TO_Y);
1373 if (it.current_y > top_y)
1374 visible_p = false;
1375
1376 }
1377 RESTORE_IT (&it, &save_it, save_it_data);
1378 if (visible_p)
1379 {
1380 if (it.method == GET_FROM_DISPLAY_VECTOR)
1381 {
1382 /* We stopped on the last glyph of a display vector.
1383 Try and recompute. Hack alert! */
1384 if (charpos < 2 || top.charpos >= charpos)
1385 top_x = it.glyph_row->x;
1386 else
1387 {
1388 struct it it2, it2_prev;
1389 /* The idea is to get to the previous buffer
1390 position, consume the character there, and use
1391 the pixel coordinates we get after that. But if
1392 the previous buffer position is also displayed
1393 from a display vector, we need to consume all of
1394 the glyphs from that display vector. */
1395 start_display (&it2, w, top);
1396 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1397 /* If we didn't get to CHARPOS - 1, there's some
1398 replacing display property at that position, and
1399 we stopped after it. That is exactly the place
1400 whose coordinates we want. */
1401 if (IT_CHARPOS (it2) != charpos - 1)
1402 it2_prev = it2;
1403 else
1404 {
1405 /* Iterate until we get out of the display
1406 vector that displays the character at
1407 CHARPOS - 1. */
1408 do {
1409 get_next_display_element (&it2);
1410 PRODUCE_GLYPHS (&it2);
1411 it2_prev = it2;
1412 set_iterator_to_next (&it2, true);
1413 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1414 && IT_CHARPOS (it2) < charpos);
1415 }
1416 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1417 || it2_prev.current_x > it2_prev.last_visible_x)
1418 top_x = it.glyph_row->x;
1419 else
1420 {
1421 top_x = it2_prev.current_x;
1422 top_y = it2_prev.current_y;
1423 }
1424 }
1425 }
1426 else if (IT_CHARPOS (it) != charpos)
1427 {
1428 Lisp_Object cpos = make_number (charpos);
1429 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1430 Lisp_Object string = string_from_display_spec (spec);
1431 struct text_pos tpos;
1432 bool newline_in_string
1433 = (STRINGP (string)
1434 && memchr (SDATA (string), '\n', SBYTES (string)));
1435
1436 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1437 bool replacing_spec_p
1438 = (!NILP (spec)
1439 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1440 charpos, FRAME_WINDOW_P (it.f)));
1441 /* The tricky code below is needed because there's a
1442 discrepancy between move_it_to and how we set cursor
1443 when PT is at the beginning of a portion of text
1444 covered by a display property or an overlay with a
1445 display property, or the display line ends in a
1446 newline from a display string. move_it_to will stop
1447 _after_ such display strings, whereas
1448 set_cursor_from_row conspires with cursor_row_p to
1449 place the cursor on the first glyph produced from the
1450 display string. */
1451
1452 /* We have overshoot PT because it is covered by a
1453 display property that replaces the text it covers.
1454 If the string includes embedded newlines, we are also
1455 in the wrong display line. Backtrack to the correct
1456 line, where the display property begins. */
1457 if (replacing_spec_p)
1458 {
1459 Lisp_Object startpos, endpos;
1460 EMACS_INT start, end;
1461 struct it it3;
1462
1463 /* Find the first and the last buffer positions
1464 covered by the display string. */
1465 endpos =
1466 Fnext_single_char_property_change (cpos, Qdisplay,
1467 Qnil, Qnil);
1468 startpos =
1469 Fprevious_single_char_property_change (endpos, Qdisplay,
1470 Qnil, Qnil);
1471 start = XFASTINT (startpos);
1472 end = XFASTINT (endpos);
1473 /* Move to the last buffer position before the
1474 display property. */
1475 start_display (&it3, w, top);
1476 if (start > CHARPOS (top))
1477 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1478 /* Move forward one more line if the position before
1479 the display string is a newline or if it is the
1480 rightmost character on a line that is
1481 continued or word-wrapped. */
1482 if (it3.method == GET_FROM_BUFFER
1483 && (it3.c == '\n'
1484 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1485 move_it_by_lines (&it3, 1);
1486 else if (move_it_in_display_line_to (&it3, -1,
1487 it3.current_x
1488 + it3.pixel_width,
1489 MOVE_TO_X)
1490 == MOVE_LINE_CONTINUED)
1491 {
1492 move_it_by_lines (&it3, 1);
1493 /* When we are under word-wrap, the #$@%!
1494 move_it_by_lines moves 2 lines, so we need to
1495 fix that up. */
1496 if (it3.line_wrap == WORD_WRAP)
1497 move_it_by_lines (&it3, -1);
1498 }
1499
1500 /* Record the vertical coordinate of the display
1501 line where we wound up. */
1502 top_y = it3.current_y;
1503 if (it3.bidi_p)
1504 {
1505 /* When characters are reordered for display,
1506 the character displayed to the left of the
1507 display string could be _after_ the display
1508 property in the logical order. Use the
1509 smallest vertical position of these two. */
1510 start_display (&it3, w, top);
1511 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1512 if (it3.current_y < top_y)
1513 top_y = it3.current_y;
1514 }
1515 /* Move from the top of the window to the beginning
1516 of the display line where the display string
1517 begins. */
1518 start_display (&it3, w, top);
1519 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1520 /* If it3_moved stays false after the 'while' loop
1521 below, that means we already were at a newline
1522 before the loop (e.g., the display string begins
1523 with a newline), so we don't need to (and cannot)
1524 inspect the glyphs of it3.glyph_row, because
1525 PRODUCE_GLYPHS will not produce anything for a
1526 newline, and thus it3.glyph_row stays at its
1527 stale content it got at top of the window. */
1528 bool it3_moved = false;
1529 /* Finally, advance the iterator until we hit the
1530 first display element whose character position is
1531 CHARPOS, or until the first newline from the
1532 display string, which signals the end of the
1533 display line. */
1534 while (get_next_display_element (&it3))
1535 {
1536 PRODUCE_GLYPHS (&it3);
1537 if (IT_CHARPOS (it3) == charpos
1538 || ITERATOR_AT_END_OF_LINE_P (&it3))
1539 break;
1540 it3_moved = true;
1541 set_iterator_to_next (&it3, false);
1542 }
1543 top_x = it3.current_x - it3.pixel_width;
1544 /* Normally, we would exit the above loop because we
1545 found the display element whose character
1546 position is CHARPOS. For the contingency that we
1547 didn't, and stopped at the first newline from the
1548 display string, move back over the glyphs
1549 produced from the string, until we find the
1550 rightmost glyph not from the string. */
1551 if (it3_moved
1552 && newline_in_string
1553 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1554 {
1555 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1556 + it3.glyph_row->used[TEXT_AREA];
1557
1558 while (EQ ((g - 1)->object, string))
1559 {
1560 --g;
1561 top_x -= g->pixel_width;
1562 }
1563 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1564 + it3.glyph_row->used[TEXT_AREA]);
1565 }
1566 }
1567 }
1568
1569 *x = top_x;
1570 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1571 *rtop = max (0, window_top_y - top_y);
1572 *rbot = max (0, bottom_y - it.last_visible_y);
1573 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1574 - max (top_y, window_top_y)));
1575 *vpos = it.vpos;
1576 if (it.bidi_it.paragraph_dir == R2L)
1577 r2l = true;
1578 }
1579 }
1580 else
1581 {
1582 /* Either we were asked to provide info about WINDOW_END, or
1583 CHARPOS is in the partially visible glyph row at end of
1584 window. */
1585 struct it it2;
1586 void *it2data = NULL;
1587
1588 SAVE_IT (it2, it, it2data);
1589 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1590 move_it_by_lines (&it, 1);
1591 if (charpos < IT_CHARPOS (it)
1592 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1593 {
1594 visible_p = true;
1595 RESTORE_IT (&it2, &it2, it2data);
1596 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1597 *x = it2.current_x;
1598 *y = it2.current_y + it2.max_ascent - it2.ascent;
1599 *rtop = max (0, -it2.current_y);
1600 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1601 - it.last_visible_y));
1602 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1603 it.last_visible_y)
1604 - max (it2.current_y,
1605 WINDOW_HEADER_LINE_HEIGHT (w))));
1606 *vpos = it2.vpos;
1607 if (it2.bidi_it.paragraph_dir == R2L)
1608 r2l = true;
1609 }
1610 else
1611 bidi_unshelve_cache (it2data, true);
1612 }
1613 bidi_unshelve_cache (itdata, false);
1614
1615 if (old_buffer)
1616 set_buffer_internal_1 (old_buffer);
1617
1618 if (visible_p)
1619 {
1620 if (w->hscroll > 0)
1621 *x -=
1622 window_hscroll_limited (w, WINDOW_XFRAME (w))
1623 * WINDOW_FRAME_COLUMN_WIDTH (w);
1624 /* For lines in an R2L paragraph, we need to mirror the X pixel
1625 coordinate wrt the text area. For the reasons, see the
1626 commentary in buffer_posn_from_coords and the explanation of
1627 the geometry used by the move_it_* functions at the end of
1628 the large commentary near the beginning of this file. */
1629 if (r2l)
1630 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1631 }
1632
1633 #if false
1634 /* Debugging code. */
1635 if (visible_p)
1636 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1637 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1638 else
1639 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1640 #endif
1641
1642 return visible_p;
1643 }
1644
1645
1646 /* Return the next character from STR. Return in *LEN the length of
1647 the character. This is like STRING_CHAR_AND_LENGTH but never
1648 returns an invalid character. If we find one, we return a `?', but
1649 with the length of the invalid character. */
1650
1651 static int
1652 string_char_and_length (const unsigned char *str, int *len)
1653 {
1654 int c;
1655
1656 c = STRING_CHAR_AND_LENGTH (str, *len);
1657 if (!CHAR_VALID_P (c))
1658 /* We may not change the length here because other places in Emacs
1659 don't use this function, i.e. they silently accept invalid
1660 characters. */
1661 c = '?';
1662
1663 return c;
1664 }
1665
1666
1667
1668 /* Given a position POS containing a valid character and byte position
1669 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1670
1671 static struct text_pos
1672 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1673 {
1674 eassert (STRINGP (string) && nchars >= 0);
1675
1676 if (STRING_MULTIBYTE (string))
1677 {
1678 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1679 int len;
1680
1681 while (nchars--)
1682 {
1683 string_char_and_length (p, &len);
1684 p += len;
1685 CHARPOS (pos) += 1;
1686 BYTEPOS (pos) += len;
1687 }
1688 }
1689 else
1690 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1691
1692 return pos;
1693 }
1694
1695
1696 /* Value is the text position, i.e. character and byte position,
1697 for character position CHARPOS in STRING. */
1698
1699 static struct text_pos
1700 string_pos (ptrdiff_t charpos, Lisp_Object string)
1701 {
1702 struct text_pos pos;
1703 eassert (STRINGP (string));
1704 eassert (charpos >= 0);
1705 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1706 return pos;
1707 }
1708
1709
1710 /* Value is a text position, i.e. character and byte position, for
1711 character position CHARPOS in C string S. MULTIBYTE_P
1712 means recognize multibyte characters. */
1713
1714 static struct text_pos
1715 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1716 {
1717 struct text_pos pos;
1718
1719 eassert (s != NULL);
1720 eassert (charpos >= 0);
1721
1722 if (multibyte_p)
1723 {
1724 int len;
1725
1726 SET_TEXT_POS (pos, 0, 0);
1727 while (charpos--)
1728 {
1729 string_char_and_length ((const unsigned char *) s, &len);
1730 s += len;
1731 CHARPOS (pos) += 1;
1732 BYTEPOS (pos) += len;
1733 }
1734 }
1735 else
1736 SET_TEXT_POS (pos, charpos, charpos);
1737
1738 return pos;
1739 }
1740
1741
1742 /* Value is the number of characters in C string S. MULTIBYTE_P
1743 means recognize multibyte characters. */
1744
1745 static ptrdiff_t
1746 number_of_chars (const char *s, bool multibyte_p)
1747 {
1748 ptrdiff_t nchars;
1749
1750 if (multibyte_p)
1751 {
1752 ptrdiff_t rest = strlen (s);
1753 int len;
1754 const unsigned char *p = (const unsigned char *) s;
1755
1756 for (nchars = 0; rest > 0; ++nchars)
1757 {
1758 string_char_and_length (p, &len);
1759 rest -= len, p += len;
1760 }
1761 }
1762 else
1763 nchars = strlen (s);
1764
1765 return nchars;
1766 }
1767
1768
1769 /* Compute byte position NEWPOS->bytepos corresponding to
1770 NEWPOS->charpos. POS is a known position in string STRING.
1771 NEWPOS->charpos must be >= POS.charpos. */
1772
1773 static void
1774 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1775 {
1776 eassert (STRINGP (string));
1777 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1778
1779 if (STRING_MULTIBYTE (string))
1780 *newpos = string_pos_nchars_ahead (pos, string,
1781 CHARPOS (*newpos) - CHARPOS (pos));
1782 else
1783 BYTEPOS (*newpos) = CHARPOS (*newpos);
1784 }
1785
1786 /* EXPORT:
1787 Return an estimation of the pixel height of mode or header lines on
1788 frame F. FACE_ID specifies what line's height to estimate. */
1789
1790 int
1791 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1792 {
1793 #ifdef HAVE_WINDOW_SYSTEM
1794 if (FRAME_WINDOW_P (f))
1795 {
1796 int height = FONT_HEIGHT (FRAME_FONT (f));
1797
1798 /* This function is called so early when Emacs starts that the face
1799 cache and mode line face are not yet initialized. */
1800 if (FRAME_FACE_CACHE (f))
1801 {
1802 struct face *face = FACE_FROM_ID (f, face_id);
1803 if (face)
1804 {
1805 if (face->font)
1806 height = normal_char_height (face->font, -1);
1807 if (face->box_line_width > 0)
1808 height += 2 * face->box_line_width;
1809 }
1810 }
1811
1812 return height;
1813 }
1814 #endif
1815
1816 return 1;
1817 }
1818
1819 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1820 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1821 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1822 not force the value into range. */
1823
1824 void
1825 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1826 NativeRectangle *bounds, bool noclip)
1827 {
1828
1829 #ifdef HAVE_WINDOW_SYSTEM
1830 if (FRAME_WINDOW_P (f))
1831 {
1832 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1833 even for negative values. */
1834 if (pix_x < 0)
1835 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1836 if (pix_y < 0)
1837 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1838
1839 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1840 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1841
1842 if (bounds)
1843 STORE_NATIVE_RECT (*bounds,
1844 FRAME_COL_TO_PIXEL_X (f, pix_x),
1845 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1846 FRAME_COLUMN_WIDTH (f) - 1,
1847 FRAME_LINE_HEIGHT (f) - 1);
1848
1849 /* PXW: Should we clip pixels before converting to columns/lines? */
1850 if (!noclip)
1851 {
1852 if (pix_x < 0)
1853 pix_x = 0;
1854 else if (pix_x > FRAME_TOTAL_COLS (f))
1855 pix_x = FRAME_TOTAL_COLS (f);
1856
1857 if (pix_y < 0)
1858 pix_y = 0;
1859 else if (pix_y > FRAME_TOTAL_LINES (f))
1860 pix_y = FRAME_TOTAL_LINES (f);
1861 }
1862 }
1863 #endif
1864
1865 *x = pix_x;
1866 *y = pix_y;
1867 }
1868
1869
1870 /* Find the glyph under window-relative coordinates X/Y in window W.
1871 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1872 strings. Return in *HPOS and *VPOS the row and column number of
1873 the glyph found. Return in *AREA the glyph area containing X.
1874 Value is a pointer to the glyph found or null if X/Y is not on
1875 text, or we can't tell because W's current matrix is not up to
1876 date. */
1877
1878 static struct glyph *
1879 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1880 int *dx, int *dy, int *area)
1881 {
1882 struct glyph *glyph, *end;
1883 struct glyph_row *row = NULL;
1884 int x0, i;
1885
1886 /* Find row containing Y. Give up if some row is not enabled. */
1887 for (i = 0; i < w->current_matrix->nrows; ++i)
1888 {
1889 row = MATRIX_ROW (w->current_matrix, i);
1890 if (!row->enabled_p)
1891 return NULL;
1892 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1893 break;
1894 }
1895
1896 *vpos = i;
1897 *hpos = 0;
1898
1899 /* Give up if Y is not in the window. */
1900 if (i == w->current_matrix->nrows)
1901 return NULL;
1902
1903 /* Get the glyph area containing X. */
1904 if (w->pseudo_window_p)
1905 {
1906 *area = TEXT_AREA;
1907 x0 = 0;
1908 }
1909 else
1910 {
1911 if (x < window_box_left_offset (w, TEXT_AREA))
1912 {
1913 *area = LEFT_MARGIN_AREA;
1914 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1915 }
1916 else if (x < window_box_right_offset (w, TEXT_AREA))
1917 {
1918 *area = TEXT_AREA;
1919 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1920 }
1921 else
1922 {
1923 *area = RIGHT_MARGIN_AREA;
1924 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1925 }
1926 }
1927
1928 /* Find glyph containing X. */
1929 glyph = row->glyphs[*area];
1930 end = glyph + row->used[*area];
1931 x -= x0;
1932 while (glyph < end && x >= glyph->pixel_width)
1933 {
1934 x -= glyph->pixel_width;
1935 ++glyph;
1936 }
1937
1938 if (glyph == end)
1939 return NULL;
1940
1941 if (dx)
1942 {
1943 *dx = x;
1944 *dy = y - (row->y + row->ascent - glyph->ascent);
1945 }
1946
1947 *hpos = glyph - row->glyphs[*area];
1948 return glyph;
1949 }
1950
1951 /* Convert frame-relative x/y to coordinates relative to window W.
1952 Takes pseudo-windows into account. */
1953
1954 static void
1955 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1956 {
1957 if (w->pseudo_window_p)
1958 {
1959 /* A pseudo-window is always full-width, and starts at the
1960 left edge of the frame, plus a frame border. */
1961 struct frame *f = XFRAME (w->frame);
1962 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1963 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1964 }
1965 else
1966 {
1967 *x -= WINDOW_LEFT_EDGE_X (w);
1968 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1969 }
1970 }
1971
1972 #ifdef HAVE_WINDOW_SYSTEM
1973
1974 /* EXPORT:
1975 Return in RECTS[] at most N clipping rectangles for glyph string S.
1976 Return the number of stored rectangles. */
1977
1978 int
1979 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1980 {
1981 XRectangle r;
1982
1983 if (n <= 0)
1984 return 0;
1985
1986 if (s->row->full_width_p)
1987 {
1988 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1989 r.x = WINDOW_LEFT_EDGE_X (s->w);
1990 if (s->row->mode_line_p)
1991 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
1992 else
1993 r.width = WINDOW_PIXEL_WIDTH (s->w);
1994
1995 /* Unless displaying a mode or menu bar line, which are always
1996 fully visible, clip to the visible part of the row. */
1997 if (s->w->pseudo_window_p)
1998 r.height = s->row->visible_height;
1999 else
2000 r.height = s->height;
2001 }
2002 else
2003 {
2004 /* This is a text line that may be partially visible. */
2005 r.x = window_box_left (s->w, s->area);
2006 r.width = window_box_width (s->w, s->area);
2007 r.height = s->row->visible_height;
2008 }
2009
2010 if (s->clip_head)
2011 if (r.x < s->clip_head->x)
2012 {
2013 if (r.width >= s->clip_head->x - r.x)
2014 r.width -= s->clip_head->x - r.x;
2015 else
2016 r.width = 0;
2017 r.x = s->clip_head->x;
2018 }
2019 if (s->clip_tail)
2020 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2021 {
2022 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2023 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2024 else
2025 r.width = 0;
2026 }
2027
2028 /* If S draws overlapping rows, it's sufficient to use the top and
2029 bottom of the window for clipping because this glyph string
2030 intentionally draws over other lines. */
2031 if (s->for_overlaps)
2032 {
2033 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2034 r.height = window_text_bottom_y (s->w) - r.y;
2035
2036 /* Alas, the above simple strategy does not work for the
2037 environments with anti-aliased text: if the same text is
2038 drawn onto the same place multiple times, it gets thicker.
2039 If the overlap we are processing is for the erased cursor, we
2040 take the intersection with the rectangle of the cursor. */
2041 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2042 {
2043 XRectangle rc, r_save = r;
2044
2045 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2046 rc.y = s->w->phys_cursor.y;
2047 rc.width = s->w->phys_cursor_width;
2048 rc.height = s->w->phys_cursor_height;
2049
2050 x_intersect_rectangles (&r_save, &rc, &r);
2051 }
2052 }
2053 else
2054 {
2055 /* Don't use S->y for clipping because it doesn't take partially
2056 visible lines into account. For example, it can be negative for
2057 partially visible lines at the top of a window. */
2058 if (!s->row->full_width_p
2059 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2060 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2061 else
2062 r.y = max (0, s->row->y);
2063 }
2064
2065 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2066
2067 /* If drawing the cursor, don't let glyph draw outside its
2068 advertised boundaries. Cleartype does this under some circumstances. */
2069 if (s->hl == DRAW_CURSOR)
2070 {
2071 struct glyph *glyph = s->first_glyph;
2072 int height, max_y;
2073
2074 if (s->x > r.x)
2075 {
2076 if (r.width >= s->x - r.x)
2077 r.width -= s->x - r.x;
2078 else /* R2L hscrolled row with cursor outside text area */
2079 r.width = 0;
2080 r.x = s->x;
2081 }
2082 r.width = min (r.width, glyph->pixel_width);
2083
2084 /* If r.y is below window bottom, ensure that we still see a cursor. */
2085 height = min (glyph->ascent + glyph->descent,
2086 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2087 max_y = window_text_bottom_y (s->w) - height;
2088 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2089 if (s->ybase - glyph->ascent > max_y)
2090 {
2091 r.y = max_y;
2092 r.height = height;
2093 }
2094 else
2095 {
2096 /* Don't draw cursor glyph taller than our actual glyph. */
2097 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2098 if (height < r.height)
2099 {
2100 max_y = r.y + r.height;
2101 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2102 r.height = min (max_y - r.y, height);
2103 }
2104 }
2105 }
2106
2107 if (s->row->clip)
2108 {
2109 XRectangle r_save = r;
2110
2111 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2112 r.width = 0;
2113 }
2114
2115 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2116 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2117 {
2118 #ifdef CONVERT_FROM_XRECT
2119 CONVERT_FROM_XRECT (r, *rects);
2120 #else
2121 *rects = r;
2122 #endif
2123 return 1;
2124 }
2125 else
2126 {
2127 /* If we are processing overlapping and allowed to return
2128 multiple clipping rectangles, we exclude the row of the glyph
2129 string from the clipping rectangle. This is to avoid drawing
2130 the same text on the environment with anti-aliasing. */
2131 #ifdef CONVERT_FROM_XRECT
2132 XRectangle rs[2];
2133 #else
2134 XRectangle *rs = rects;
2135 #endif
2136 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2137
2138 if (s->for_overlaps & OVERLAPS_PRED)
2139 {
2140 rs[i] = r;
2141 if (r.y + r.height > row_y)
2142 {
2143 if (r.y < row_y)
2144 rs[i].height = row_y - r.y;
2145 else
2146 rs[i].height = 0;
2147 }
2148 i++;
2149 }
2150 if (s->for_overlaps & OVERLAPS_SUCC)
2151 {
2152 rs[i] = r;
2153 if (r.y < row_y + s->row->visible_height)
2154 {
2155 if (r.y + r.height > row_y + s->row->visible_height)
2156 {
2157 rs[i].y = row_y + s->row->visible_height;
2158 rs[i].height = r.y + r.height - rs[i].y;
2159 }
2160 else
2161 rs[i].height = 0;
2162 }
2163 i++;
2164 }
2165
2166 n = i;
2167 #ifdef CONVERT_FROM_XRECT
2168 for (i = 0; i < n; i++)
2169 CONVERT_FROM_XRECT (rs[i], rects[i]);
2170 #endif
2171 return n;
2172 }
2173 }
2174
2175 /* EXPORT:
2176 Return in *NR the clipping rectangle for glyph string S. */
2177
2178 void
2179 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2180 {
2181 get_glyph_string_clip_rects (s, nr, 1);
2182 }
2183
2184
2185 /* EXPORT:
2186 Return the position and height of the phys cursor in window W.
2187 Set w->phys_cursor_width to width of phys cursor.
2188 */
2189
2190 void
2191 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2192 struct glyph *glyph, int *xp, int *yp, int *heightp)
2193 {
2194 struct frame *f = XFRAME (WINDOW_FRAME (w));
2195 int x, y, wd, h, h0, y0, ascent;
2196
2197 /* Compute the width of the rectangle to draw. If on a stretch
2198 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2199 rectangle as wide as the glyph, but use a canonical character
2200 width instead. */
2201 wd = glyph->pixel_width;
2202
2203 x = w->phys_cursor.x;
2204 if (x < 0)
2205 {
2206 wd += x;
2207 x = 0;
2208 }
2209
2210 if (glyph->type == STRETCH_GLYPH
2211 && !x_stretch_cursor_p)
2212 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2213 w->phys_cursor_width = wd;
2214
2215 /* Don't let the hollow cursor glyph descend below the glyph row's
2216 ascent value, lest the hollow cursor looks funny. */
2217 y = w->phys_cursor.y;
2218 ascent = row->ascent;
2219 if (row->ascent < glyph->ascent)
2220 {
2221 y =- glyph->ascent - row->ascent;
2222 ascent = glyph->ascent;
2223 }
2224
2225 /* If y is below window bottom, ensure that we still see a cursor. */
2226 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2227
2228 h = max (h0, ascent + glyph->descent);
2229 h0 = min (h0, ascent + glyph->descent);
2230
2231 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2232 if (y < y0)
2233 {
2234 h = max (h - (y0 - y) + 1, h0);
2235 y = y0 - 1;
2236 }
2237 else
2238 {
2239 y0 = window_text_bottom_y (w) - h0;
2240 if (y > y0)
2241 {
2242 h += y - y0;
2243 y = y0;
2244 }
2245 }
2246
2247 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2248 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2249 *heightp = h;
2250 }
2251
2252 /*
2253 * Remember which glyph the mouse is over.
2254 */
2255
2256 void
2257 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2258 {
2259 Lisp_Object window;
2260 struct window *w;
2261 struct glyph_row *r, *gr, *end_row;
2262 enum window_part part;
2263 enum glyph_row_area area;
2264 int x, y, width, height;
2265
2266 /* Try to determine frame pixel position and size of the glyph under
2267 frame pixel coordinates X/Y on frame F. */
2268
2269 if (window_resize_pixelwise)
2270 {
2271 width = height = 1;
2272 goto virtual_glyph;
2273 }
2274 else if (!f->glyphs_initialized_p
2275 || (window = window_from_coordinates (f, gx, gy, &part, false),
2276 NILP (window)))
2277 {
2278 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2279 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2280 goto virtual_glyph;
2281 }
2282
2283 w = XWINDOW (window);
2284 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2285 height = WINDOW_FRAME_LINE_HEIGHT (w);
2286
2287 x = window_relative_x_coord (w, part, gx);
2288 y = gy - WINDOW_TOP_EDGE_Y (w);
2289
2290 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2291 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2292
2293 if (w->pseudo_window_p)
2294 {
2295 area = TEXT_AREA;
2296 part = ON_MODE_LINE; /* Don't adjust margin. */
2297 goto text_glyph;
2298 }
2299
2300 switch (part)
2301 {
2302 case ON_LEFT_MARGIN:
2303 area = LEFT_MARGIN_AREA;
2304 goto text_glyph;
2305
2306 case ON_RIGHT_MARGIN:
2307 area = RIGHT_MARGIN_AREA;
2308 goto text_glyph;
2309
2310 case ON_HEADER_LINE:
2311 case ON_MODE_LINE:
2312 gr = (part == ON_HEADER_LINE
2313 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2314 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2315 gy = gr->y;
2316 area = TEXT_AREA;
2317 goto text_glyph_row_found;
2318
2319 case ON_TEXT:
2320 area = TEXT_AREA;
2321
2322 text_glyph:
2323 gr = 0; gy = 0;
2324 for (; r <= end_row && r->enabled_p; ++r)
2325 if (r->y + r->height > y)
2326 {
2327 gr = r; gy = r->y;
2328 break;
2329 }
2330
2331 text_glyph_row_found:
2332 if (gr && gy <= y)
2333 {
2334 struct glyph *g = gr->glyphs[area];
2335 struct glyph *end = g + gr->used[area];
2336
2337 height = gr->height;
2338 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2339 if (gx + g->pixel_width > x)
2340 break;
2341
2342 if (g < end)
2343 {
2344 if (g->type == IMAGE_GLYPH)
2345 {
2346 /* Don't remember when mouse is over image, as
2347 image may have hot-spots. */
2348 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2349 return;
2350 }
2351 width = g->pixel_width;
2352 }
2353 else
2354 {
2355 /* Use nominal char spacing at end of line. */
2356 x -= gx;
2357 gx += (x / width) * width;
2358 }
2359
2360 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2361 {
2362 gx += window_box_left_offset (w, area);
2363 /* Don't expand over the modeline to make sure the vertical
2364 drag cursor is shown early enough. */
2365 height = min (height,
2366 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2367 }
2368 }
2369 else
2370 {
2371 /* Use nominal line height at end of window. */
2372 gx = (x / width) * width;
2373 y -= gy;
2374 gy += (y / height) * height;
2375 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2376 /* See comment above. */
2377 height = min (height,
2378 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2379 }
2380 break;
2381
2382 case ON_LEFT_FRINGE:
2383 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2384 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2385 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2386 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2387 goto row_glyph;
2388
2389 case ON_RIGHT_FRINGE:
2390 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2391 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2392 : window_box_right_offset (w, TEXT_AREA));
2393 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2394 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2395 && !WINDOW_RIGHTMOST_P (w))
2396 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2397 /* Make sure the vertical border can get her own glyph to the
2398 right of the one we build here. */
2399 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2400 else
2401 width = WINDOW_PIXEL_WIDTH (w) - gx;
2402 else
2403 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2404
2405 goto row_glyph;
2406
2407 case ON_VERTICAL_BORDER:
2408 gx = WINDOW_PIXEL_WIDTH (w) - width;
2409 goto row_glyph;
2410
2411 case ON_VERTICAL_SCROLL_BAR:
2412 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2413 ? 0
2414 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2415 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2416 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2417 : 0)));
2418 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2419
2420 row_glyph:
2421 gr = 0, gy = 0;
2422 for (; r <= end_row && r->enabled_p; ++r)
2423 if (r->y + r->height > y)
2424 {
2425 gr = r; gy = r->y;
2426 break;
2427 }
2428
2429 if (gr && gy <= y)
2430 height = gr->height;
2431 else
2432 {
2433 /* Use nominal line height at end of window. */
2434 y -= gy;
2435 gy += (y / height) * height;
2436 }
2437 break;
2438
2439 case ON_RIGHT_DIVIDER:
2440 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2441 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2442 gy = 0;
2443 /* The bottom divider prevails. */
2444 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2445 goto add_edge;
2446
2447 case ON_BOTTOM_DIVIDER:
2448 gx = 0;
2449 width = WINDOW_PIXEL_WIDTH (w);
2450 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2451 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2452 goto add_edge;
2453
2454 default:
2455 ;
2456 virtual_glyph:
2457 /* If there is no glyph under the mouse, then we divide the screen
2458 into a grid of the smallest glyph in the frame, and use that
2459 as our "glyph". */
2460
2461 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2462 round down even for negative values. */
2463 if (gx < 0)
2464 gx -= width - 1;
2465 if (gy < 0)
2466 gy -= height - 1;
2467
2468 gx = (gx / width) * width;
2469 gy = (gy / height) * height;
2470
2471 goto store_rect;
2472 }
2473
2474 add_edge:
2475 gx += WINDOW_LEFT_EDGE_X (w);
2476 gy += WINDOW_TOP_EDGE_Y (w);
2477
2478 store_rect:
2479 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2480
2481 /* Visible feedback for debugging. */
2482 #if false && defined HAVE_X_WINDOWS
2483 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2484 f->output_data.x->normal_gc,
2485 gx, gy, width, height);
2486 #endif
2487 }
2488
2489
2490 #endif /* HAVE_WINDOW_SYSTEM */
2491
2492 static void
2493 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2494 {
2495 eassert (w);
2496 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2497 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2498 w->window_end_vpos
2499 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2500 }
2501
2502 /***********************************************************************
2503 Lisp form evaluation
2504 ***********************************************************************/
2505
2506 /* Error handler for safe_eval and safe_call. */
2507
2508 static Lisp_Object
2509 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2510 {
2511 add_to_log ("Error during redisplay: %S signaled %S",
2512 Flist (nargs, args), arg);
2513 return Qnil;
2514 }
2515
2516 /* Call function FUNC with the rest of NARGS - 1 arguments
2517 following. Return the result, or nil if something went
2518 wrong. Prevent redisplay during the evaluation. */
2519
2520 static Lisp_Object
2521 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2522 {
2523 Lisp_Object val;
2524
2525 if (inhibit_eval_during_redisplay)
2526 val = Qnil;
2527 else
2528 {
2529 ptrdiff_t i;
2530 ptrdiff_t count = SPECPDL_INDEX ();
2531 Lisp_Object *args;
2532 USE_SAFE_ALLOCA;
2533 SAFE_ALLOCA_LISP (args, nargs);
2534
2535 args[0] = func;
2536 for (i = 1; i < nargs; i++)
2537 args[i] = va_arg (ap, Lisp_Object);
2538
2539 specbind (Qinhibit_redisplay, Qt);
2540 if (inhibit_quit)
2541 specbind (Qinhibit_quit, Qt);
2542 /* Use Qt to ensure debugger does not run,
2543 so there is no possibility of wanting to redisplay. */
2544 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2545 safe_eval_handler);
2546 SAFE_FREE ();
2547 val = unbind_to (count, val);
2548 }
2549
2550 return val;
2551 }
2552
2553 Lisp_Object
2554 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2555 {
2556 Lisp_Object retval;
2557 va_list ap;
2558
2559 va_start (ap, func);
2560 retval = safe__call (false, nargs, func, ap);
2561 va_end (ap);
2562 return retval;
2563 }
2564
2565 /* Call function FN with one argument ARG.
2566 Return the result, or nil if something went wrong. */
2567
2568 Lisp_Object
2569 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2570 {
2571 return safe_call (2, fn, arg);
2572 }
2573
2574 static Lisp_Object
2575 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2576 {
2577 Lisp_Object retval;
2578 va_list ap;
2579
2580 va_start (ap, fn);
2581 retval = safe__call (inhibit_quit, 2, fn, ap);
2582 va_end (ap);
2583 return retval;
2584 }
2585
2586 Lisp_Object
2587 safe_eval (Lisp_Object sexpr)
2588 {
2589 return safe__call1 (false, Qeval, sexpr);
2590 }
2591
2592 static Lisp_Object
2593 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2594 {
2595 return safe__call1 (inhibit_quit, Qeval, sexpr);
2596 }
2597
2598 /* Call function FN with two arguments ARG1 and ARG2.
2599 Return the result, or nil if something went wrong. */
2600
2601 Lisp_Object
2602 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2603 {
2604 return safe_call (3, fn, arg1, arg2);
2605 }
2606
2607
2608 \f
2609 /***********************************************************************
2610 Debugging
2611 ***********************************************************************/
2612
2613 /* Define CHECK_IT to perform sanity checks on iterators.
2614 This is for debugging. It is too slow to do unconditionally. */
2615
2616 static void
2617 CHECK_IT (struct it *it)
2618 {
2619 #if false
2620 if (it->method == GET_FROM_STRING)
2621 {
2622 eassert (STRINGP (it->string));
2623 eassert (IT_STRING_CHARPOS (*it) >= 0);
2624 }
2625 else
2626 {
2627 eassert (IT_STRING_CHARPOS (*it) < 0);
2628 if (it->method == GET_FROM_BUFFER)
2629 {
2630 /* Check that character and byte positions agree. */
2631 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2632 }
2633 }
2634
2635 if (it->dpvec)
2636 eassert (it->current.dpvec_index >= 0);
2637 else
2638 eassert (it->current.dpvec_index < 0);
2639 #endif
2640 }
2641
2642
2643 /* Check that the window end of window W is what we expect it
2644 to be---the last row in the current matrix displaying text. */
2645
2646 static void
2647 CHECK_WINDOW_END (struct window *w)
2648 {
2649 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2650 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2651 {
2652 struct glyph_row *row;
2653 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2654 !row->enabled_p
2655 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2656 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2657 }
2658 #endif
2659 }
2660
2661 /***********************************************************************
2662 Iterator initialization
2663 ***********************************************************************/
2664
2665 /* Initialize IT for displaying current_buffer in window W, starting
2666 at character position CHARPOS. CHARPOS < 0 means that no buffer
2667 position is specified which is useful when the iterator is assigned
2668 a position later. BYTEPOS is the byte position corresponding to
2669 CHARPOS.
2670
2671 If ROW is not null, calls to produce_glyphs with IT as parameter
2672 will produce glyphs in that row.
2673
2674 BASE_FACE_ID is the id of a base face to use. It must be one of
2675 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2676 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2677 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2678
2679 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2680 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2681 will be initialized to use the corresponding mode line glyph row of
2682 the desired matrix of W. */
2683
2684 void
2685 init_iterator (struct it *it, struct window *w,
2686 ptrdiff_t charpos, ptrdiff_t bytepos,
2687 struct glyph_row *row, enum face_id base_face_id)
2688 {
2689 enum face_id remapped_base_face_id = base_face_id;
2690
2691 /* Some precondition checks. */
2692 eassert (w != NULL && it != NULL);
2693 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2694 && charpos <= ZV));
2695
2696 /* If face attributes have been changed since the last redisplay,
2697 free realized faces now because they depend on face definitions
2698 that might have changed. Don't free faces while there might be
2699 desired matrices pending which reference these faces. */
2700 if (!inhibit_free_realized_faces)
2701 {
2702 if (face_change)
2703 {
2704 face_change = false;
2705 free_all_realized_faces (Qnil);
2706 }
2707 else if (XFRAME (w->frame)->face_change)
2708 {
2709 XFRAME (w->frame)->face_change = 0;
2710 free_all_realized_faces (w->frame);
2711 }
2712 }
2713
2714 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2715 if (! NILP (Vface_remapping_alist))
2716 remapped_base_face_id
2717 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2718
2719 /* Use one of the mode line rows of W's desired matrix if
2720 appropriate. */
2721 if (row == NULL)
2722 {
2723 if (base_face_id == MODE_LINE_FACE_ID
2724 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2725 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2726 else if (base_face_id == HEADER_LINE_FACE_ID)
2727 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2728 }
2729
2730 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2731 Other parts of redisplay rely on that. */
2732 memclear (it, sizeof *it);
2733 it->current.overlay_string_index = -1;
2734 it->current.dpvec_index = -1;
2735 it->base_face_id = remapped_base_face_id;
2736 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2737 it->paragraph_embedding = L2R;
2738 it->bidi_it.w = w;
2739
2740 /* The window in which we iterate over current_buffer: */
2741 XSETWINDOW (it->window, w);
2742 it->w = w;
2743 it->f = XFRAME (w->frame);
2744
2745 it->cmp_it.id = -1;
2746
2747 /* Extra space between lines (on window systems only). */
2748 if (base_face_id == DEFAULT_FACE_ID
2749 && FRAME_WINDOW_P (it->f))
2750 {
2751 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2752 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2753 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2754 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2755 * FRAME_LINE_HEIGHT (it->f));
2756 else if (it->f->extra_line_spacing > 0)
2757 it->extra_line_spacing = it->f->extra_line_spacing;
2758 }
2759
2760 /* If realized faces have been removed, e.g. because of face
2761 attribute changes of named faces, recompute them. When running
2762 in batch mode, the face cache of the initial frame is null. If
2763 we happen to get called, make a dummy face cache. */
2764 if (FRAME_FACE_CACHE (it->f) == NULL)
2765 init_frame_faces (it->f);
2766 if (FRAME_FACE_CACHE (it->f)->used == 0)
2767 recompute_basic_faces (it->f);
2768
2769 it->override_ascent = -1;
2770
2771 /* Are control characters displayed as `^C'? */
2772 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2773
2774 /* -1 means everything between a CR and the following line end
2775 is invisible. >0 means lines indented more than this value are
2776 invisible. */
2777 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2778 ? (clip_to_bounds
2779 (-1, XINT (BVAR (current_buffer, selective_display)),
2780 PTRDIFF_MAX))
2781 : (!NILP (BVAR (current_buffer, selective_display))
2782 ? -1 : 0));
2783 it->selective_display_ellipsis_p
2784 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2785
2786 /* Display table to use. */
2787 it->dp = window_display_table (w);
2788
2789 /* Are multibyte characters enabled in current_buffer? */
2790 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2791
2792 /* Get the position at which the redisplay_end_trigger hook should
2793 be run, if it is to be run at all. */
2794 if (MARKERP (w->redisplay_end_trigger)
2795 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2796 it->redisplay_end_trigger_charpos
2797 = marker_position (w->redisplay_end_trigger);
2798 else if (INTEGERP (w->redisplay_end_trigger))
2799 it->redisplay_end_trigger_charpos
2800 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2801 PTRDIFF_MAX);
2802
2803 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2804
2805 /* Are lines in the display truncated? */
2806 if (TRUNCATE != 0)
2807 it->line_wrap = TRUNCATE;
2808 if (base_face_id == DEFAULT_FACE_ID
2809 && !it->w->hscroll
2810 && (WINDOW_FULL_WIDTH_P (it->w)
2811 || NILP (Vtruncate_partial_width_windows)
2812 || (INTEGERP (Vtruncate_partial_width_windows)
2813 /* PXW: Shall we do something about this? */
2814 && (XINT (Vtruncate_partial_width_windows)
2815 <= WINDOW_TOTAL_COLS (it->w))))
2816 && NILP (BVAR (current_buffer, truncate_lines)))
2817 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2818 ? WINDOW_WRAP : WORD_WRAP;
2819
2820 /* Get dimensions of truncation and continuation glyphs. These are
2821 displayed as fringe bitmaps under X, but we need them for such
2822 frames when the fringes are turned off. But leave the dimensions
2823 zero for tooltip frames, as these glyphs look ugly there and also
2824 sabotage calculations of tooltip dimensions in x-show-tip. */
2825 #ifdef HAVE_WINDOW_SYSTEM
2826 if (!(FRAME_WINDOW_P (it->f)
2827 && FRAMEP (tip_frame)
2828 && it->f == XFRAME (tip_frame)))
2829 #endif
2830 {
2831 if (it->line_wrap == TRUNCATE)
2832 {
2833 /* We will need the truncation glyph. */
2834 eassert (it->glyph_row == NULL);
2835 produce_special_glyphs (it, IT_TRUNCATION);
2836 it->truncation_pixel_width = it->pixel_width;
2837 }
2838 else
2839 {
2840 /* We will need the continuation glyph. */
2841 eassert (it->glyph_row == NULL);
2842 produce_special_glyphs (it, IT_CONTINUATION);
2843 it->continuation_pixel_width = it->pixel_width;
2844 }
2845 }
2846
2847 /* Reset these values to zero because the produce_special_glyphs
2848 above has changed them. */
2849 it->pixel_width = it->ascent = it->descent = 0;
2850 it->phys_ascent = it->phys_descent = 0;
2851
2852 /* Set this after getting the dimensions of truncation and
2853 continuation glyphs, so that we don't produce glyphs when calling
2854 produce_special_glyphs, above. */
2855 it->glyph_row = row;
2856 it->area = TEXT_AREA;
2857
2858 /* Get the dimensions of the display area. The display area
2859 consists of the visible window area plus a horizontally scrolled
2860 part to the left of the window. All x-values are relative to the
2861 start of this total display area. */
2862 if (base_face_id != DEFAULT_FACE_ID)
2863 {
2864 /* Mode lines, menu bar in terminal frames. */
2865 it->first_visible_x = 0;
2866 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2867 }
2868 else
2869 {
2870 it->first_visible_x
2871 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2872 it->last_visible_x = (it->first_visible_x
2873 + window_box_width (w, TEXT_AREA));
2874
2875 /* If we truncate lines, leave room for the truncation glyph(s) at
2876 the right margin. Otherwise, leave room for the continuation
2877 glyph(s). Done only if the window has no right fringe. */
2878 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2879 {
2880 if (it->line_wrap == TRUNCATE)
2881 it->last_visible_x -= it->truncation_pixel_width;
2882 else
2883 it->last_visible_x -= it->continuation_pixel_width;
2884 }
2885
2886 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2887 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2888 }
2889
2890 /* Leave room for a border glyph. */
2891 if (!FRAME_WINDOW_P (it->f)
2892 && !WINDOW_RIGHTMOST_P (it->w))
2893 it->last_visible_x -= 1;
2894
2895 it->last_visible_y = window_text_bottom_y (w);
2896
2897 /* For mode lines and alike, arrange for the first glyph having a
2898 left box line if the face specifies a box. */
2899 if (base_face_id != DEFAULT_FACE_ID)
2900 {
2901 struct face *face;
2902
2903 it->face_id = remapped_base_face_id;
2904
2905 /* If we have a boxed mode line, make the first character appear
2906 with a left box line. */
2907 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2908 if (face && face->box != FACE_NO_BOX)
2909 it->start_of_box_run_p = true;
2910 }
2911
2912 /* If a buffer position was specified, set the iterator there,
2913 getting overlays and face properties from that position. */
2914 if (charpos >= BUF_BEG (current_buffer))
2915 {
2916 it->stop_charpos = charpos;
2917 it->end_charpos = ZV;
2918 eassert (charpos == BYTE_TO_CHAR (bytepos));
2919 IT_CHARPOS (*it) = charpos;
2920 IT_BYTEPOS (*it) = bytepos;
2921
2922 /* We will rely on `reseat' to set this up properly, via
2923 handle_face_prop. */
2924 it->face_id = it->base_face_id;
2925
2926 it->start = it->current;
2927 /* Do we need to reorder bidirectional text? Not if this is a
2928 unibyte buffer: by definition, none of the single-byte
2929 characters are strong R2L, so no reordering is needed. And
2930 bidi.c doesn't support unibyte buffers anyway. Also, don't
2931 reorder while we are loading loadup.el, since the tables of
2932 character properties needed for reordering are not yet
2933 available. */
2934 it->bidi_p =
2935 NILP (Vpurify_flag)
2936 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2937 && it->multibyte_p;
2938
2939 /* If we are to reorder bidirectional text, init the bidi
2940 iterator. */
2941 if (it->bidi_p)
2942 {
2943 /* Since we don't know at this point whether there will be
2944 any R2L lines in the window, we reserve space for
2945 truncation/continuation glyphs even if only the left
2946 fringe is absent. */
2947 if (base_face_id == DEFAULT_FACE_ID
2948 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2949 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2950 {
2951 if (it->line_wrap == TRUNCATE)
2952 it->last_visible_x -= it->truncation_pixel_width;
2953 else
2954 it->last_visible_x -= it->continuation_pixel_width;
2955 }
2956 /* Note the paragraph direction that this buffer wants to
2957 use. */
2958 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2959 Qleft_to_right))
2960 it->paragraph_embedding = L2R;
2961 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2962 Qright_to_left))
2963 it->paragraph_embedding = R2L;
2964 else
2965 it->paragraph_embedding = NEUTRAL_DIR;
2966 bidi_unshelve_cache (NULL, false);
2967 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2968 &it->bidi_it);
2969 }
2970
2971 /* Compute faces etc. */
2972 reseat (it, it->current.pos, true);
2973 }
2974
2975 CHECK_IT (it);
2976 }
2977
2978
2979 /* Initialize IT for the display of window W with window start POS. */
2980
2981 void
2982 start_display (struct it *it, struct window *w, struct text_pos pos)
2983 {
2984 struct glyph_row *row;
2985 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
2986
2987 row = w->desired_matrix->rows + first_vpos;
2988 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2989 it->first_vpos = first_vpos;
2990
2991 /* Don't reseat to previous visible line start if current start
2992 position is in a string or image. */
2993 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2994 {
2995 int first_y = it->current_y;
2996
2997 /* If window start is not at a line start, skip forward to POS to
2998 get the correct continuation lines width. */
2999 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3000 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3001 if (!start_at_line_beg_p)
3002 {
3003 int new_x;
3004
3005 reseat_at_previous_visible_line_start (it);
3006 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3007
3008 new_x = it->current_x + it->pixel_width;
3009
3010 /* If lines are continued, this line may end in the middle
3011 of a multi-glyph character (e.g. a control character
3012 displayed as \003, or in the middle of an overlay
3013 string). In this case move_it_to above will not have
3014 taken us to the start of the continuation line but to the
3015 end of the continued line. */
3016 if (it->current_x > 0
3017 && it->line_wrap != TRUNCATE /* Lines are continued. */
3018 && (/* And glyph doesn't fit on the line. */
3019 new_x > it->last_visible_x
3020 /* Or it fits exactly and we're on a window
3021 system frame. */
3022 || (new_x == it->last_visible_x
3023 && FRAME_WINDOW_P (it->f)
3024 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3025 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3026 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3027 {
3028 if ((it->current.dpvec_index >= 0
3029 || it->current.overlay_string_index >= 0)
3030 /* If we are on a newline from a display vector or
3031 overlay string, then we are already at the end of
3032 a screen line; no need to go to the next line in
3033 that case, as this line is not really continued.
3034 (If we do go to the next line, C-e will not DTRT.) */
3035 && it->c != '\n')
3036 {
3037 set_iterator_to_next (it, true);
3038 move_it_in_display_line_to (it, -1, -1, 0);
3039 }
3040
3041 it->continuation_lines_width += it->current_x;
3042 }
3043 /* If the character at POS is displayed via a display
3044 vector, move_it_to above stops at the final glyph of
3045 IT->dpvec. To make the caller redisplay that character
3046 again (a.k.a. start at POS), we need to reset the
3047 dpvec_index to the beginning of IT->dpvec. */
3048 else if (it->current.dpvec_index >= 0)
3049 it->current.dpvec_index = 0;
3050
3051 /* We're starting a new display line, not affected by the
3052 height of the continued line, so clear the appropriate
3053 fields in the iterator structure. */
3054 it->max_ascent = it->max_descent = 0;
3055 it->max_phys_ascent = it->max_phys_descent = 0;
3056
3057 it->current_y = first_y;
3058 it->vpos = 0;
3059 it->current_x = it->hpos = 0;
3060 }
3061 }
3062 }
3063
3064
3065 /* Return true if POS is a position in ellipses displayed for invisible
3066 text. W is the window we display, for text property lookup. */
3067
3068 static bool
3069 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3070 {
3071 Lisp_Object prop, window;
3072 bool ellipses_p = false;
3073 ptrdiff_t charpos = CHARPOS (pos->pos);
3074
3075 /* If POS specifies a position in a display vector, this might
3076 be for an ellipsis displayed for invisible text. We won't
3077 get the iterator set up for delivering that ellipsis unless
3078 we make sure that it gets aware of the invisible text. */
3079 if (pos->dpvec_index >= 0
3080 && pos->overlay_string_index < 0
3081 && CHARPOS (pos->string_pos) < 0
3082 && charpos > BEGV
3083 && (XSETWINDOW (window, w),
3084 prop = Fget_char_property (make_number (charpos),
3085 Qinvisible, window),
3086 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3087 {
3088 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3089 window);
3090 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3091 }
3092
3093 return ellipses_p;
3094 }
3095
3096
3097 /* Initialize IT for stepping through current_buffer in window W,
3098 starting at position POS that includes overlay string and display
3099 vector/ control character translation position information. Value
3100 is false if there are overlay strings with newlines at POS. */
3101
3102 static bool
3103 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3104 {
3105 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3106 int i;
3107 bool overlay_strings_with_newlines = false;
3108
3109 /* If POS specifies a position in a display vector, this might
3110 be for an ellipsis displayed for invisible text. We won't
3111 get the iterator set up for delivering that ellipsis unless
3112 we make sure that it gets aware of the invisible text. */
3113 if (in_ellipses_for_invisible_text_p (pos, w))
3114 {
3115 --charpos;
3116 bytepos = 0;
3117 }
3118
3119 /* Keep in mind: the call to reseat in init_iterator skips invisible
3120 text, so we might end up at a position different from POS. This
3121 is only a problem when POS is a row start after a newline and an
3122 overlay starts there with an after-string, and the overlay has an
3123 invisible property. Since we don't skip invisible text in
3124 display_line and elsewhere immediately after consuming the
3125 newline before the row start, such a POS will not be in a string,
3126 but the call to init_iterator below will move us to the
3127 after-string. */
3128 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3129
3130 /* This only scans the current chunk -- it should scan all chunks.
3131 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3132 to 16 in 22.1 to make this a lesser problem. */
3133 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3134 {
3135 const char *s = SSDATA (it->overlay_strings[i]);
3136 const char *e = s + SBYTES (it->overlay_strings[i]);
3137
3138 while (s < e && *s != '\n')
3139 ++s;
3140
3141 if (s < e)
3142 {
3143 overlay_strings_with_newlines = true;
3144 break;
3145 }
3146 }
3147
3148 /* If position is within an overlay string, set up IT to the right
3149 overlay string. */
3150 if (pos->overlay_string_index >= 0)
3151 {
3152 int relative_index;
3153
3154 /* If the first overlay string happens to have a `display'
3155 property for an image, the iterator will be set up for that
3156 image, and we have to undo that setup first before we can
3157 correct the overlay string index. */
3158 if (it->method == GET_FROM_IMAGE)
3159 pop_it (it);
3160
3161 /* We already have the first chunk of overlay strings in
3162 IT->overlay_strings. Load more until the one for
3163 pos->overlay_string_index is in IT->overlay_strings. */
3164 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3165 {
3166 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3167 it->current.overlay_string_index = 0;
3168 while (n--)
3169 {
3170 load_overlay_strings (it, 0);
3171 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3172 }
3173 }
3174
3175 it->current.overlay_string_index = pos->overlay_string_index;
3176 relative_index = (it->current.overlay_string_index
3177 % OVERLAY_STRING_CHUNK_SIZE);
3178 it->string = it->overlay_strings[relative_index];
3179 eassert (STRINGP (it->string));
3180 it->current.string_pos = pos->string_pos;
3181 it->method = GET_FROM_STRING;
3182 it->end_charpos = SCHARS (it->string);
3183 /* Set up the bidi iterator for this overlay string. */
3184 if (it->bidi_p)
3185 {
3186 it->bidi_it.string.lstring = it->string;
3187 it->bidi_it.string.s = NULL;
3188 it->bidi_it.string.schars = SCHARS (it->string);
3189 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3190 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3191 it->bidi_it.string.unibyte = !it->multibyte_p;
3192 it->bidi_it.w = it->w;
3193 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3194 FRAME_WINDOW_P (it->f), &it->bidi_it);
3195
3196 /* Synchronize the state of the bidi iterator with
3197 pos->string_pos. For any string position other than
3198 zero, this will be done automagically when we resume
3199 iteration over the string and get_visually_first_element
3200 is called. But if string_pos is zero, and the string is
3201 to be reordered for display, we need to resync manually,
3202 since it could be that the iteration state recorded in
3203 pos ended at string_pos of 0 moving backwards in string. */
3204 if (CHARPOS (pos->string_pos) == 0)
3205 {
3206 get_visually_first_element (it);
3207 if (IT_STRING_CHARPOS (*it) != 0)
3208 do {
3209 /* Paranoia. */
3210 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3211 bidi_move_to_visually_next (&it->bidi_it);
3212 } while (it->bidi_it.charpos != 0);
3213 }
3214 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3215 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3216 }
3217 }
3218
3219 if (CHARPOS (pos->string_pos) >= 0)
3220 {
3221 /* Recorded position is not in an overlay string, but in another
3222 string. This can only be a string from a `display' property.
3223 IT should already be filled with that string. */
3224 it->current.string_pos = pos->string_pos;
3225 eassert (STRINGP (it->string));
3226 if (it->bidi_p)
3227 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3228 FRAME_WINDOW_P (it->f), &it->bidi_it);
3229 }
3230
3231 /* Restore position in display vector translations, control
3232 character translations or ellipses. */
3233 if (pos->dpvec_index >= 0)
3234 {
3235 if (it->dpvec == NULL)
3236 get_next_display_element (it);
3237 eassert (it->dpvec && it->current.dpvec_index == 0);
3238 it->current.dpvec_index = pos->dpvec_index;
3239 }
3240
3241 CHECK_IT (it);
3242 return !overlay_strings_with_newlines;
3243 }
3244
3245
3246 /* Initialize IT for stepping through current_buffer in window W
3247 starting at ROW->start. */
3248
3249 static void
3250 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3251 {
3252 init_from_display_pos (it, w, &row->start);
3253 it->start = row->start;
3254 it->continuation_lines_width = row->continuation_lines_width;
3255 CHECK_IT (it);
3256 }
3257
3258
3259 /* Initialize IT for stepping through current_buffer in window W
3260 starting in the line following ROW, i.e. starting at ROW->end.
3261 Value is false if there are overlay strings with newlines at ROW's
3262 end position. */
3263
3264 static bool
3265 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3266 {
3267 bool success = false;
3268
3269 if (init_from_display_pos (it, w, &row->end))
3270 {
3271 if (row->continued_p)
3272 it->continuation_lines_width
3273 = row->continuation_lines_width + row->pixel_width;
3274 CHECK_IT (it);
3275 success = true;
3276 }
3277
3278 return success;
3279 }
3280
3281
3282
3283 \f
3284 /***********************************************************************
3285 Text properties
3286 ***********************************************************************/
3287
3288 /* Called when IT reaches IT->stop_charpos. Handle text property and
3289 overlay changes. Set IT->stop_charpos to the next position where
3290 to stop. */
3291
3292 static void
3293 handle_stop (struct it *it)
3294 {
3295 enum prop_handled handled;
3296 bool handle_overlay_change_p;
3297 struct props *p;
3298
3299 it->dpvec = NULL;
3300 it->current.dpvec_index = -1;
3301 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3302 it->ellipsis_p = false;
3303
3304 /* Use face of preceding text for ellipsis (if invisible) */
3305 if (it->selective_display_ellipsis_p)
3306 it->saved_face_id = it->face_id;
3307
3308 /* Here's the description of the semantics of, and the logic behind,
3309 the various HANDLED_* statuses:
3310
3311 HANDLED_NORMALLY means the handler did its job, and the loop
3312 should proceed to calling the next handler in order.
3313
3314 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3315 change in the properties and overlays at current position, so the
3316 loop should be restarted, to re-invoke the handlers that were
3317 already called. This happens when fontification-functions were
3318 called by handle_fontified_prop, and actually fontified
3319 something. Another case where HANDLED_RECOMPUTE_PROPS is
3320 returned is when we discover overlay strings that need to be
3321 displayed right away. The loop below will continue for as long
3322 as the status is HANDLED_RECOMPUTE_PROPS.
3323
3324 HANDLED_RETURN means return immediately to the caller, to
3325 continue iteration without calling any further handlers. This is
3326 used when we need to act on some property right away, for example
3327 when we need to display the ellipsis or a replacing display
3328 property, such as display string or image.
3329
3330 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3331 consumed, and the handler switched to the next overlay string.
3332 This signals the loop below to refrain from looking for more
3333 overlays before all the overlay strings of the current overlay
3334 are processed.
3335
3336 Some of the handlers called by the loop push the iterator state
3337 onto the stack (see 'push_it'), and arrange for the iteration to
3338 continue with another object, such as an image, a display string,
3339 or an overlay string. In most such cases, it->stop_charpos is
3340 set to the first character of the string, so that when the
3341 iteration resumes, this function will immediately be called
3342 again, to examine the properties at the beginning of the string.
3343
3344 When a display or overlay string is exhausted, the iterator state
3345 is popped (see 'pop_it'), and iteration continues with the
3346 previous object. Again, in many such cases this function is
3347 called again to find the next position where properties might
3348 change. */
3349
3350 do
3351 {
3352 handled = HANDLED_NORMALLY;
3353
3354 /* Call text property handlers. */
3355 for (p = it_props; p->handler; ++p)
3356 {
3357 handled = p->handler (it);
3358
3359 if (handled == HANDLED_RECOMPUTE_PROPS)
3360 break;
3361 else if (handled == HANDLED_RETURN)
3362 {
3363 /* We still want to show before and after strings from
3364 overlays even if the actual buffer text is replaced. */
3365 if (!handle_overlay_change_p
3366 || it->sp > 1
3367 /* Don't call get_overlay_strings_1 if we already
3368 have overlay strings loaded, because doing so
3369 will load them again and push the iterator state
3370 onto the stack one more time, which is not
3371 expected by the rest of the code that processes
3372 overlay strings. */
3373 || (it->current.overlay_string_index < 0
3374 && !get_overlay_strings_1 (it, 0, false)))
3375 {
3376 if (it->ellipsis_p)
3377 setup_for_ellipsis (it, 0);
3378 /* When handling a display spec, we might load an
3379 empty string. In that case, discard it here. We
3380 used to discard it in handle_single_display_spec,
3381 but that causes get_overlay_strings_1, above, to
3382 ignore overlay strings that we must check. */
3383 if (STRINGP (it->string) && !SCHARS (it->string))
3384 pop_it (it);
3385 return;
3386 }
3387 else if (STRINGP (it->string) && !SCHARS (it->string))
3388 pop_it (it);
3389 else
3390 {
3391 it->string_from_display_prop_p = false;
3392 it->from_disp_prop_p = false;
3393 handle_overlay_change_p = false;
3394 }
3395 handled = HANDLED_RECOMPUTE_PROPS;
3396 break;
3397 }
3398 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3399 handle_overlay_change_p = false;
3400 }
3401
3402 if (handled != HANDLED_RECOMPUTE_PROPS)
3403 {
3404 /* Don't check for overlay strings below when set to deliver
3405 characters from a display vector. */
3406 if (it->method == GET_FROM_DISPLAY_VECTOR)
3407 handle_overlay_change_p = false;
3408
3409 /* Handle overlay changes.
3410 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3411 if it finds overlays. */
3412 if (handle_overlay_change_p)
3413 handled = handle_overlay_change (it);
3414 }
3415
3416 if (it->ellipsis_p)
3417 {
3418 setup_for_ellipsis (it, 0);
3419 break;
3420 }
3421 }
3422 while (handled == HANDLED_RECOMPUTE_PROPS);
3423
3424 /* Determine where to stop next. */
3425 if (handled == HANDLED_NORMALLY)
3426 compute_stop_pos (it);
3427 }
3428
3429
3430 /* Compute IT->stop_charpos from text property and overlay change
3431 information for IT's current position. */
3432
3433 static void
3434 compute_stop_pos (struct it *it)
3435 {
3436 register INTERVAL iv, next_iv;
3437 Lisp_Object object, limit, position;
3438 ptrdiff_t charpos, bytepos;
3439
3440 if (STRINGP (it->string))
3441 {
3442 /* Strings are usually short, so don't limit the search for
3443 properties. */
3444 it->stop_charpos = it->end_charpos;
3445 object = it->string;
3446 limit = Qnil;
3447 charpos = IT_STRING_CHARPOS (*it);
3448 bytepos = IT_STRING_BYTEPOS (*it);
3449 }
3450 else
3451 {
3452 ptrdiff_t pos;
3453
3454 /* If end_charpos is out of range for some reason, such as a
3455 misbehaving display function, rationalize it (Bug#5984). */
3456 if (it->end_charpos > ZV)
3457 it->end_charpos = ZV;
3458 it->stop_charpos = it->end_charpos;
3459
3460 /* If next overlay change is in front of the current stop pos
3461 (which is IT->end_charpos), stop there. Note: value of
3462 next_overlay_change is point-max if no overlay change
3463 follows. */
3464 charpos = IT_CHARPOS (*it);
3465 bytepos = IT_BYTEPOS (*it);
3466 pos = next_overlay_change (charpos);
3467 if (pos < it->stop_charpos)
3468 it->stop_charpos = pos;
3469
3470 /* Set up variables for computing the stop position from text
3471 property changes. */
3472 XSETBUFFER (object, current_buffer);
3473 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3474 }
3475
3476 /* Get the interval containing IT's position. Value is a null
3477 interval if there isn't such an interval. */
3478 position = make_number (charpos);
3479 iv = validate_interval_range (object, &position, &position, false);
3480 if (iv)
3481 {
3482 Lisp_Object values_here[LAST_PROP_IDX];
3483 struct props *p;
3484
3485 /* Get properties here. */
3486 for (p = it_props; p->handler; ++p)
3487 values_here[p->idx] = textget (iv->plist,
3488 builtin_lisp_symbol (p->name));
3489
3490 /* Look for an interval following iv that has different
3491 properties. */
3492 for (next_iv = next_interval (iv);
3493 (next_iv
3494 && (NILP (limit)
3495 || XFASTINT (limit) > next_iv->position));
3496 next_iv = next_interval (next_iv))
3497 {
3498 for (p = it_props; p->handler; ++p)
3499 {
3500 Lisp_Object new_value = textget (next_iv->plist,
3501 builtin_lisp_symbol (p->name));
3502 if (!EQ (values_here[p->idx], new_value))
3503 break;
3504 }
3505
3506 if (p->handler)
3507 break;
3508 }
3509
3510 if (next_iv)
3511 {
3512 if (INTEGERP (limit)
3513 && next_iv->position >= XFASTINT (limit))
3514 /* No text property change up to limit. */
3515 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3516 else
3517 /* Text properties change in next_iv. */
3518 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3519 }
3520 }
3521
3522 if (it->cmp_it.id < 0)
3523 {
3524 ptrdiff_t stoppos = it->end_charpos;
3525
3526 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3527 stoppos = -1;
3528 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3529 stoppos, it->string);
3530 }
3531
3532 eassert (STRINGP (it->string)
3533 || (it->stop_charpos >= BEGV
3534 && it->stop_charpos >= IT_CHARPOS (*it)));
3535 }
3536
3537
3538 /* Return the position of the next overlay change after POS in
3539 current_buffer. Value is point-max if no overlay change
3540 follows. This is like `next-overlay-change' but doesn't use
3541 xmalloc. */
3542
3543 static ptrdiff_t
3544 next_overlay_change (ptrdiff_t pos)
3545 {
3546 ptrdiff_t i, noverlays;
3547 ptrdiff_t endpos;
3548 Lisp_Object *overlays;
3549 USE_SAFE_ALLOCA;
3550
3551 /* Get all overlays at the given position. */
3552 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3553
3554 /* If any of these overlays ends before endpos,
3555 use its ending point instead. */
3556 for (i = 0; i < noverlays; ++i)
3557 {
3558 Lisp_Object oend;
3559 ptrdiff_t oendpos;
3560
3561 oend = OVERLAY_END (overlays[i]);
3562 oendpos = OVERLAY_POSITION (oend);
3563 endpos = min (endpos, oendpos);
3564 }
3565
3566 SAFE_FREE ();
3567 return endpos;
3568 }
3569
3570 /* How many characters forward to search for a display property or
3571 display string. Searching too far forward makes the bidi display
3572 sluggish, especially in small windows. */
3573 #define MAX_DISP_SCAN 250
3574
3575 /* Return the character position of a display string at or after
3576 position specified by POSITION. If no display string exists at or
3577 after POSITION, return ZV. A display string is either an overlay
3578 with `display' property whose value is a string, or a `display'
3579 text property whose value is a string. STRING is data about the
3580 string to iterate; if STRING->lstring is nil, we are iterating a
3581 buffer. FRAME_WINDOW_P is true when we are displaying a window
3582 on a GUI frame. DISP_PROP is set to zero if we searched
3583 MAX_DISP_SCAN characters forward without finding any display
3584 strings, non-zero otherwise. It is set to 2 if the display string
3585 uses any kind of `(space ...)' spec that will produce a stretch of
3586 white space in the text area. */
3587 ptrdiff_t
3588 compute_display_string_pos (struct text_pos *position,
3589 struct bidi_string_data *string,
3590 struct window *w,
3591 bool frame_window_p, int *disp_prop)
3592 {
3593 /* OBJECT = nil means current buffer. */
3594 Lisp_Object object, object1;
3595 Lisp_Object pos, spec, limpos;
3596 bool string_p = string && (STRINGP (string->lstring) || string->s);
3597 ptrdiff_t eob = string_p ? string->schars : ZV;
3598 ptrdiff_t begb = string_p ? 0 : BEGV;
3599 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3600 ptrdiff_t lim =
3601 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3602 struct text_pos tpos;
3603 int rv = 0;
3604
3605 if (string && STRINGP (string->lstring))
3606 object1 = object = string->lstring;
3607 else if (w && !string_p)
3608 {
3609 XSETWINDOW (object, w);
3610 object1 = Qnil;
3611 }
3612 else
3613 object1 = object = Qnil;
3614
3615 *disp_prop = 1;
3616
3617 if (charpos >= eob
3618 /* We don't support display properties whose values are strings
3619 that have display string properties. */
3620 || string->from_disp_str
3621 /* C strings cannot have display properties. */
3622 || (string->s && !STRINGP (object)))
3623 {
3624 *disp_prop = 0;
3625 return eob;
3626 }
3627
3628 /* If the character at CHARPOS is where the display string begins,
3629 return CHARPOS. */
3630 pos = make_number (charpos);
3631 if (STRINGP (object))
3632 bufpos = string->bufpos;
3633 else
3634 bufpos = charpos;
3635 tpos = *position;
3636 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3637 && (charpos <= begb
3638 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3639 object),
3640 spec))
3641 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3642 frame_window_p)))
3643 {
3644 if (rv == 2)
3645 *disp_prop = 2;
3646 return charpos;
3647 }
3648
3649 /* Look forward for the first character with a `display' property
3650 that will replace the underlying text when displayed. */
3651 limpos = make_number (lim);
3652 do {
3653 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3654 CHARPOS (tpos) = XFASTINT (pos);
3655 if (CHARPOS (tpos) >= lim)
3656 {
3657 *disp_prop = 0;
3658 break;
3659 }
3660 if (STRINGP (object))
3661 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3662 else
3663 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3664 spec = Fget_char_property (pos, Qdisplay, object);
3665 if (!STRINGP (object))
3666 bufpos = CHARPOS (tpos);
3667 } while (NILP (spec)
3668 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3669 bufpos, frame_window_p)));
3670 if (rv == 2)
3671 *disp_prop = 2;
3672
3673 return CHARPOS (tpos);
3674 }
3675
3676 /* Return the character position of the end of the display string that
3677 started at CHARPOS. If there's no display string at CHARPOS,
3678 return -1. A display string is either an overlay with `display'
3679 property whose value is a string or a `display' text property whose
3680 value is a string. */
3681 ptrdiff_t
3682 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3683 {
3684 /* OBJECT = nil means current buffer. */
3685 Lisp_Object object =
3686 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3687 Lisp_Object pos = make_number (charpos);
3688 ptrdiff_t eob =
3689 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3690
3691 if (charpos >= eob || (string->s && !STRINGP (object)))
3692 return eob;
3693
3694 /* It could happen that the display property or overlay was removed
3695 since we found it in compute_display_string_pos above. One way
3696 this can happen is if JIT font-lock was called (through
3697 handle_fontified_prop), and jit-lock-functions remove text
3698 properties or overlays from the portion of buffer that includes
3699 CHARPOS. Muse mode is known to do that, for example. In this
3700 case, we return -1 to the caller, to signal that no display
3701 string is actually present at CHARPOS. See bidi_fetch_char for
3702 how this is handled.
3703
3704 An alternative would be to never look for display properties past
3705 it->stop_charpos. But neither compute_display_string_pos nor
3706 bidi_fetch_char that calls it know or care where the next
3707 stop_charpos is. */
3708 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3709 return -1;
3710
3711 /* Look forward for the first character where the `display' property
3712 changes. */
3713 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3714
3715 return XFASTINT (pos);
3716 }
3717
3718
3719 \f
3720 /***********************************************************************
3721 Fontification
3722 ***********************************************************************/
3723
3724 /* Handle changes in the `fontified' property of the current buffer by
3725 calling hook functions from Qfontification_functions to fontify
3726 regions of text. */
3727
3728 static enum prop_handled
3729 handle_fontified_prop (struct it *it)
3730 {
3731 Lisp_Object prop, pos;
3732 enum prop_handled handled = HANDLED_NORMALLY;
3733
3734 if (!NILP (Vmemory_full))
3735 return handled;
3736
3737 /* Get the value of the `fontified' property at IT's current buffer
3738 position. (The `fontified' property doesn't have a special
3739 meaning in strings.) If the value is nil, call functions from
3740 Qfontification_functions. */
3741 if (!STRINGP (it->string)
3742 && it->s == NULL
3743 && !NILP (Vfontification_functions)
3744 && !NILP (Vrun_hooks)
3745 && (pos = make_number (IT_CHARPOS (*it)),
3746 prop = Fget_char_property (pos, Qfontified, Qnil),
3747 /* Ignore the special cased nil value always present at EOB since
3748 no amount of fontifying will be able to change it. */
3749 NILP (prop) && IT_CHARPOS (*it) < Z))
3750 {
3751 ptrdiff_t count = SPECPDL_INDEX ();
3752 Lisp_Object val;
3753 struct buffer *obuf = current_buffer;
3754 ptrdiff_t begv = BEGV, zv = ZV;
3755 bool old_clip_changed = current_buffer->clip_changed;
3756
3757 val = Vfontification_functions;
3758 specbind (Qfontification_functions, Qnil);
3759
3760 eassert (it->end_charpos == ZV);
3761
3762 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3763 safe_call1 (val, pos);
3764 else
3765 {
3766 Lisp_Object fns, fn;
3767
3768 fns = Qnil;
3769
3770 for (; CONSP (val); val = XCDR (val))
3771 {
3772 fn = XCAR (val);
3773
3774 if (EQ (fn, Qt))
3775 {
3776 /* A value of t indicates this hook has a local
3777 binding; it means to run the global binding too.
3778 In a global value, t should not occur. If it
3779 does, we must ignore it to avoid an endless
3780 loop. */
3781 for (fns = Fdefault_value (Qfontification_functions);
3782 CONSP (fns);
3783 fns = XCDR (fns))
3784 {
3785 fn = XCAR (fns);
3786 if (!EQ (fn, Qt))
3787 safe_call1 (fn, pos);
3788 }
3789 }
3790 else
3791 safe_call1 (fn, pos);
3792 }
3793 }
3794
3795 unbind_to (count, Qnil);
3796
3797 /* Fontification functions routinely call `save-restriction'.
3798 Normally, this tags clip_changed, which can confuse redisplay
3799 (see discussion in Bug#6671). Since we don't perform any
3800 special handling of fontification changes in the case where
3801 `save-restriction' isn't called, there's no point doing so in
3802 this case either. So, if the buffer's restrictions are
3803 actually left unchanged, reset clip_changed. */
3804 if (obuf == current_buffer)
3805 {
3806 if (begv == BEGV && zv == ZV)
3807 current_buffer->clip_changed = old_clip_changed;
3808 }
3809 /* There isn't much we can reasonably do to protect against
3810 misbehaving fontification, but here's a fig leaf. */
3811 else if (BUFFER_LIVE_P (obuf))
3812 set_buffer_internal_1 (obuf);
3813
3814 /* The fontification code may have added/removed text.
3815 It could do even a lot worse, but let's at least protect against
3816 the most obvious case where only the text past `pos' gets changed',
3817 as is/was done in grep.el where some escapes sequences are turned
3818 into face properties (bug#7876). */
3819 it->end_charpos = ZV;
3820
3821 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3822 something. This avoids an endless loop if they failed to
3823 fontify the text for which reason ever. */
3824 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3825 handled = HANDLED_RECOMPUTE_PROPS;
3826 }
3827
3828 return handled;
3829 }
3830
3831
3832 \f
3833 /***********************************************************************
3834 Faces
3835 ***********************************************************************/
3836
3837 /* Set up iterator IT from face properties at its current position.
3838 Called from handle_stop. */
3839
3840 static enum prop_handled
3841 handle_face_prop (struct it *it)
3842 {
3843 int new_face_id;
3844 ptrdiff_t next_stop;
3845
3846 if (!STRINGP (it->string))
3847 {
3848 new_face_id
3849 = face_at_buffer_position (it->w,
3850 IT_CHARPOS (*it),
3851 &next_stop,
3852 (IT_CHARPOS (*it)
3853 + TEXT_PROP_DISTANCE_LIMIT),
3854 false, it->base_face_id);
3855
3856 /* Is this a start of a run of characters with box face?
3857 Caveat: this can be called for a freshly initialized
3858 iterator; face_id is -1 in this case. We know that the new
3859 face will not change until limit, i.e. if the new face has a
3860 box, all characters up to limit will have one. But, as
3861 usual, we don't know whether limit is really the end. */
3862 if (new_face_id != it->face_id)
3863 {
3864 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3865 /* If it->face_id is -1, old_face below will be NULL, see
3866 the definition of FACE_FROM_ID. This will happen if this
3867 is the initial call that gets the face. */
3868 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3869
3870 /* If the value of face_id of the iterator is -1, we have to
3871 look in front of IT's position and see whether there is a
3872 face there that's different from new_face_id. */
3873 if (!old_face && IT_CHARPOS (*it) > BEG)
3874 {
3875 int prev_face_id = face_before_it_pos (it);
3876
3877 old_face = FACE_FROM_ID (it->f, prev_face_id);
3878 }
3879
3880 /* If the new face has a box, but the old face does not,
3881 this is the start of a run of characters with box face,
3882 i.e. this character has a shadow on the left side. */
3883 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3884 && (old_face == NULL || !old_face->box));
3885 it->face_box_p = new_face->box != FACE_NO_BOX;
3886 }
3887 }
3888 else
3889 {
3890 int base_face_id;
3891 ptrdiff_t bufpos;
3892 int i;
3893 Lisp_Object from_overlay
3894 = (it->current.overlay_string_index >= 0
3895 ? it->string_overlays[it->current.overlay_string_index
3896 % OVERLAY_STRING_CHUNK_SIZE]
3897 : Qnil);
3898
3899 /* See if we got to this string directly or indirectly from
3900 an overlay property. That includes the before-string or
3901 after-string of an overlay, strings in display properties
3902 provided by an overlay, their text properties, etc.
3903
3904 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3905 if (! NILP (from_overlay))
3906 for (i = it->sp - 1; i >= 0; i--)
3907 {
3908 if (it->stack[i].current.overlay_string_index >= 0)
3909 from_overlay
3910 = it->string_overlays[it->stack[i].current.overlay_string_index
3911 % OVERLAY_STRING_CHUNK_SIZE];
3912 else if (! NILP (it->stack[i].from_overlay))
3913 from_overlay = it->stack[i].from_overlay;
3914
3915 if (!NILP (from_overlay))
3916 break;
3917 }
3918
3919 if (! NILP (from_overlay))
3920 {
3921 bufpos = IT_CHARPOS (*it);
3922 /* For a string from an overlay, the base face depends
3923 only on text properties and ignores overlays. */
3924 base_face_id
3925 = face_for_overlay_string (it->w,
3926 IT_CHARPOS (*it),
3927 &next_stop,
3928 (IT_CHARPOS (*it)
3929 + TEXT_PROP_DISTANCE_LIMIT),
3930 false,
3931 from_overlay);
3932 }
3933 else
3934 {
3935 bufpos = 0;
3936
3937 /* For strings from a `display' property, use the face at
3938 IT's current buffer position as the base face to merge
3939 with, so that overlay strings appear in the same face as
3940 surrounding text, unless they specify their own faces.
3941 For strings from wrap-prefix and line-prefix properties,
3942 use the default face, possibly remapped via
3943 Vface_remapping_alist. */
3944 /* Note that the fact that we use the face at _buffer_
3945 position means that a 'display' property on an overlay
3946 string will not inherit the face of that overlay string,
3947 but will instead revert to the face of buffer text
3948 covered by the overlay. This is visible, e.g., when the
3949 overlay specifies a box face, but neither the buffer nor
3950 the display string do. This sounds like a design bug,
3951 but Emacs always did that since v21.1, so changing that
3952 might be a big deal. */
3953 base_face_id = it->string_from_prefix_prop_p
3954 ? (!NILP (Vface_remapping_alist)
3955 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3956 : DEFAULT_FACE_ID)
3957 : underlying_face_id (it);
3958 }
3959
3960 new_face_id = face_at_string_position (it->w,
3961 it->string,
3962 IT_STRING_CHARPOS (*it),
3963 bufpos,
3964 &next_stop,
3965 base_face_id, false);
3966
3967 /* Is this a start of a run of characters with box? Caveat:
3968 this can be called for a freshly allocated iterator; face_id
3969 is -1 is this case. We know that the new face will not
3970 change until the next check pos, i.e. if the new face has a
3971 box, all characters up to that position will have a
3972 box. But, as usual, we don't know whether that position
3973 is really the end. */
3974 if (new_face_id != it->face_id)
3975 {
3976 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3977 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3978
3979 /* If new face has a box but old face hasn't, this is the
3980 start of a run of characters with box, i.e. it has a
3981 shadow on the left side. */
3982 it->start_of_box_run_p
3983 = new_face->box && (old_face == NULL || !old_face->box);
3984 it->face_box_p = new_face->box != FACE_NO_BOX;
3985 }
3986 }
3987
3988 it->face_id = new_face_id;
3989 return HANDLED_NORMALLY;
3990 }
3991
3992
3993 /* Return the ID of the face ``underlying'' IT's current position,
3994 which is in a string. If the iterator is associated with a
3995 buffer, return the face at IT's current buffer position.
3996 Otherwise, use the iterator's base_face_id. */
3997
3998 static int
3999 underlying_face_id (struct it *it)
4000 {
4001 int face_id = it->base_face_id, i;
4002
4003 eassert (STRINGP (it->string));
4004
4005 for (i = it->sp - 1; i >= 0; --i)
4006 if (NILP (it->stack[i].string))
4007 face_id = it->stack[i].face_id;
4008
4009 return face_id;
4010 }
4011
4012
4013 /* Compute the face one character before or after the current position
4014 of IT, in the visual order. BEFORE_P means get the face
4015 in front (to the left in L2R paragraphs, to the right in R2L
4016 paragraphs) of IT's screen position. Value is the ID of the face. */
4017
4018 static int
4019 face_before_or_after_it_pos (struct it *it, bool before_p)
4020 {
4021 int face_id, limit;
4022 ptrdiff_t next_check_charpos;
4023 struct it it_copy;
4024 void *it_copy_data = NULL;
4025
4026 eassert (it->s == NULL);
4027
4028 if (STRINGP (it->string))
4029 {
4030 ptrdiff_t bufpos, charpos;
4031 int base_face_id;
4032
4033 /* No face change past the end of the string (for the case
4034 we are padding with spaces). No face change before the
4035 string start. */
4036 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4037 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4038 return it->face_id;
4039
4040 if (!it->bidi_p)
4041 {
4042 /* Set charpos to the position before or after IT's current
4043 position, in the logical order, which in the non-bidi
4044 case is the same as the visual order. */
4045 if (before_p)
4046 charpos = IT_STRING_CHARPOS (*it) - 1;
4047 else if (it->what == IT_COMPOSITION)
4048 /* For composition, we must check the character after the
4049 composition. */
4050 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4051 else
4052 charpos = IT_STRING_CHARPOS (*it) + 1;
4053 }
4054 else
4055 {
4056 if (before_p)
4057 {
4058 /* With bidi iteration, the character before the current
4059 in the visual order cannot be found by simple
4060 iteration, because "reverse" reordering is not
4061 supported. Instead, we need to start from the string
4062 beginning and go all the way to the current string
4063 position, remembering the previous position. */
4064 /* Ignore face changes before the first visible
4065 character on this display line. */
4066 if (it->current_x <= it->first_visible_x)
4067 return it->face_id;
4068 SAVE_IT (it_copy, *it, it_copy_data);
4069 IT_STRING_CHARPOS (it_copy) = 0;
4070 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4071
4072 do
4073 {
4074 charpos = IT_STRING_CHARPOS (it_copy);
4075 if (charpos >= SCHARS (it->string))
4076 break;
4077 bidi_move_to_visually_next (&it_copy.bidi_it);
4078 }
4079 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4080
4081 RESTORE_IT (it, it, it_copy_data);
4082 }
4083 else
4084 {
4085 /* Set charpos to the string position of the character
4086 that comes after IT's current position in the visual
4087 order. */
4088 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4089
4090 it_copy = *it;
4091 while (n--)
4092 bidi_move_to_visually_next (&it_copy.bidi_it);
4093
4094 charpos = it_copy.bidi_it.charpos;
4095 }
4096 }
4097 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4098
4099 if (it->current.overlay_string_index >= 0)
4100 bufpos = IT_CHARPOS (*it);
4101 else
4102 bufpos = 0;
4103
4104 base_face_id = underlying_face_id (it);
4105
4106 /* Get the face for ASCII, or unibyte. */
4107 face_id = face_at_string_position (it->w,
4108 it->string,
4109 charpos,
4110 bufpos,
4111 &next_check_charpos,
4112 base_face_id, false);
4113
4114 /* Correct the face for charsets different from ASCII. Do it
4115 for the multibyte case only. The face returned above is
4116 suitable for unibyte text if IT->string is unibyte. */
4117 if (STRING_MULTIBYTE (it->string))
4118 {
4119 struct text_pos pos1 = string_pos (charpos, it->string);
4120 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4121 int c, len;
4122 struct face *face = FACE_FROM_ID (it->f, face_id);
4123
4124 c = string_char_and_length (p, &len);
4125 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4126 }
4127 }
4128 else
4129 {
4130 struct text_pos pos;
4131
4132 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4133 || (IT_CHARPOS (*it) <= BEGV && before_p))
4134 return it->face_id;
4135
4136 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4137 pos = it->current.pos;
4138
4139 if (!it->bidi_p)
4140 {
4141 if (before_p)
4142 DEC_TEXT_POS (pos, it->multibyte_p);
4143 else
4144 {
4145 if (it->what == IT_COMPOSITION)
4146 {
4147 /* For composition, we must check the position after
4148 the composition. */
4149 pos.charpos += it->cmp_it.nchars;
4150 pos.bytepos += it->len;
4151 }
4152 else
4153 INC_TEXT_POS (pos, it->multibyte_p);
4154 }
4155 }
4156 else
4157 {
4158 if (before_p)
4159 {
4160 int current_x;
4161
4162 /* With bidi iteration, the character before the current
4163 in the visual order cannot be found by simple
4164 iteration, because "reverse" reordering is not
4165 supported. Instead, we need to use the move_it_*
4166 family of functions, and move to the previous
4167 character starting from the beginning of the visual
4168 line. */
4169 /* Ignore face changes before the first visible
4170 character on this display line. */
4171 if (it->current_x <= it->first_visible_x)
4172 return it->face_id;
4173 SAVE_IT (it_copy, *it, it_copy_data);
4174 /* Implementation note: Since move_it_in_display_line
4175 works in the iterator geometry, and thinks the first
4176 character is always the leftmost, even in R2L lines,
4177 we don't need to distinguish between the R2L and L2R
4178 cases here. */
4179 current_x = it_copy.current_x;
4180 move_it_vertically_backward (&it_copy, 0);
4181 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4182 pos = it_copy.current.pos;
4183 RESTORE_IT (it, it, it_copy_data);
4184 }
4185 else
4186 {
4187 /* Set charpos to the buffer position of the character
4188 that comes after IT's current position in the visual
4189 order. */
4190 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4191
4192 it_copy = *it;
4193 while (n--)
4194 bidi_move_to_visually_next (&it_copy.bidi_it);
4195
4196 SET_TEXT_POS (pos,
4197 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4198 }
4199 }
4200 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4201
4202 /* Determine face for CHARSET_ASCII, or unibyte. */
4203 face_id = face_at_buffer_position (it->w,
4204 CHARPOS (pos),
4205 &next_check_charpos,
4206 limit, false, -1);
4207
4208 /* Correct the face for charsets different from ASCII. Do it
4209 for the multibyte case only. The face returned above is
4210 suitable for unibyte text if current_buffer is unibyte. */
4211 if (it->multibyte_p)
4212 {
4213 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4214 struct face *face = FACE_FROM_ID (it->f, face_id);
4215 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4216 }
4217 }
4218
4219 return face_id;
4220 }
4221
4222
4223 \f
4224 /***********************************************************************
4225 Invisible text
4226 ***********************************************************************/
4227
4228 /* Set up iterator IT from invisible properties at its current
4229 position. Called from handle_stop. */
4230
4231 static enum prop_handled
4232 handle_invisible_prop (struct it *it)
4233 {
4234 enum prop_handled handled = HANDLED_NORMALLY;
4235 int invis;
4236 Lisp_Object prop;
4237
4238 if (STRINGP (it->string))
4239 {
4240 Lisp_Object end_charpos, limit;
4241
4242 /* Get the value of the invisible text property at the
4243 current position. Value will be nil if there is no such
4244 property. */
4245 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4246 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4247 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4248
4249 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4250 {
4251 /* Record whether we have to display an ellipsis for the
4252 invisible text. */
4253 bool display_ellipsis_p = (invis == 2);
4254 ptrdiff_t len, endpos;
4255
4256 handled = HANDLED_RECOMPUTE_PROPS;
4257
4258 /* Get the position at which the next visible text can be
4259 found in IT->string, if any. */
4260 endpos = len = SCHARS (it->string);
4261 XSETINT (limit, len);
4262 do
4263 {
4264 end_charpos
4265 = Fnext_single_property_change (end_charpos, Qinvisible,
4266 it->string, limit);
4267 /* Since LIMIT is always an integer, so should be the
4268 value returned by Fnext_single_property_change. */
4269 eassert (INTEGERP (end_charpos));
4270 if (INTEGERP (end_charpos))
4271 {
4272 endpos = XFASTINT (end_charpos);
4273 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4274 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4275 if (invis == 2)
4276 display_ellipsis_p = true;
4277 }
4278 else /* Should never happen; but if it does, exit the loop. */
4279 endpos = len;
4280 }
4281 while (invis != 0 && endpos < len);
4282
4283 if (display_ellipsis_p)
4284 it->ellipsis_p = true;
4285
4286 if (endpos < len)
4287 {
4288 /* Text at END_CHARPOS is visible. Move IT there. */
4289 struct text_pos old;
4290 ptrdiff_t oldpos;
4291
4292 old = it->current.string_pos;
4293 oldpos = CHARPOS (old);
4294 if (it->bidi_p)
4295 {
4296 if (it->bidi_it.first_elt
4297 && it->bidi_it.charpos < SCHARS (it->string))
4298 bidi_paragraph_init (it->paragraph_embedding,
4299 &it->bidi_it, true);
4300 /* Bidi-iterate out of the invisible text. */
4301 do
4302 {
4303 bidi_move_to_visually_next (&it->bidi_it);
4304 }
4305 while (oldpos <= it->bidi_it.charpos
4306 && it->bidi_it.charpos < endpos);
4307
4308 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4309 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4310 if (IT_CHARPOS (*it) >= endpos)
4311 it->prev_stop = endpos;
4312 }
4313 else
4314 {
4315 IT_STRING_CHARPOS (*it) = endpos;
4316 compute_string_pos (&it->current.string_pos, old, it->string);
4317 }
4318 }
4319 else
4320 {
4321 /* The rest of the string is invisible. If this is an
4322 overlay string, proceed with the next overlay string
4323 or whatever comes and return a character from there. */
4324 if (it->current.overlay_string_index >= 0
4325 && !display_ellipsis_p)
4326 {
4327 next_overlay_string (it);
4328 /* Don't check for overlay strings when we just
4329 finished processing them. */
4330 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4331 }
4332 else
4333 {
4334 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4335 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4336 }
4337 }
4338 }
4339 }
4340 else
4341 {
4342 ptrdiff_t newpos, next_stop, start_charpos, tem;
4343 Lisp_Object pos, overlay;
4344
4345 /* First of all, is there invisible text at this position? */
4346 tem = start_charpos = IT_CHARPOS (*it);
4347 pos = make_number (tem);
4348 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4349 &overlay);
4350 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4351
4352 /* If we are on invisible text, skip over it. */
4353 if (invis != 0 && start_charpos < it->end_charpos)
4354 {
4355 /* Record whether we have to display an ellipsis for the
4356 invisible text. */
4357 bool display_ellipsis_p = invis == 2;
4358
4359 handled = HANDLED_RECOMPUTE_PROPS;
4360
4361 /* Loop skipping over invisible text. The loop is left at
4362 ZV or with IT on the first char being visible again. */
4363 do
4364 {
4365 /* Try to skip some invisible text. Return value is the
4366 position reached which can be equal to where we start
4367 if there is nothing invisible there. This skips both
4368 over invisible text properties and overlays with
4369 invisible property. */
4370 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4371
4372 /* If we skipped nothing at all we weren't at invisible
4373 text in the first place. If everything to the end of
4374 the buffer was skipped, end the loop. */
4375 if (newpos == tem || newpos >= ZV)
4376 invis = 0;
4377 else
4378 {
4379 /* We skipped some characters but not necessarily
4380 all there are. Check if we ended up on visible
4381 text. Fget_char_property returns the property of
4382 the char before the given position, i.e. if we
4383 get invis = 0, this means that the char at
4384 newpos is visible. */
4385 pos = make_number (newpos);
4386 prop = Fget_char_property (pos, Qinvisible, it->window);
4387 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4388 }
4389
4390 /* If we ended up on invisible text, proceed to
4391 skip starting with next_stop. */
4392 if (invis != 0)
4393 tem = next_stop;
4394
4395 /* If there are adjacent invisible texts, don't lose the
4396 second one's ellipsis. */
4397 if (invis == 2)
4398 display_ellipsis_p = true;
4399 }
4400 while (invis != 0);
4401
4402 /* The position newpos is now either ZV or on visible text. */
4403 if (it->bidi_p)
4404 {
4405 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4406 bool on_newline
4407 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4408 bool after_newline
4409 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4410
4411 /* If the invisible text ends on a newline or on a
4412 character after a newline, we can avoid the costly,
4413 character by character, bidi iteration to NEWPOS, and
4414 instead simply reseat the iterator there. That's
4415 because all bidi reordering information is tossed at
4416 the newline. This is a big win for modes that hide
4417 complete lines, like Outline, Org, etc. */
4418 if (on_newline || after_newline)
4419 {
4420 struct text_pos tpos;
4421 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4422
4423 SET_TEXT_POS (tpos, newpos, bpos);
4424 reseat_1 (it, tpos, false);
4425 /* If we reseat on a newline/ZV, we need to prep the
4426 bidi iterator for advancing to the next character
4427 after the newline/EOB, keeping the current paragraph
4428 direction (so that PRODUCE_GLYPHS does TRT wrt
4429 prepending/appending glyphs to a glyph row). */
4430 if (on_newline)
4431 {
4432 it->bidi_it.first_elt = false;
4433 it->bidi_it.paragraph_dir = pdir;
4434 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4435 it->bidi_it.nchars = 1;
4436 it->bidi_it.ch_len = 1;
4437 }
4438 }
4439 else /* Must use the slow method. */
4440 {
4441 /* With bidi iteration, the region of invisible text
4442 could start and/or end in the middle of a
4443 non-base embedding level. Therefore, we need to
4444 skip invisible text using the bidi iterator,
4445 starting at IT's current position, until we find
4446 ourselves outside of the invisible text.
4447 Skipping invisible text _after_ bidi iteration
4448 avoids affecting the visual order of the
4449 displayed text when invisible properties are
4450 added or removed. */
4451 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4452 {
4453 /* If we were `reseat'ed to a new paragraph,
4454 determine the paragraph base direction. We
4455 need to do it now because
4456 next_element_from_buffer may not have a
4457 chance to do it, if we are going to skip any
4458 text at the beginning, which resets the
4459 FIRST_ELT flag. */
4460 bidi_paragraph_init (it->paragraph_embedding,
4461 &it->bidi_it, true);
4462 }
4463 do
4464 {
4465 bidi_move_to_visually_next (&it->bidi_it);
4466 }
4467 while (it->stop_charpos <= it->bidi_it.charpos
4468 && it->bidi_it.charpos < newpos);
4469 IT_CHARPOS (*it) = it->bidi_it.charpos;
4470 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4471 /* If we overstepped NEWPOS, record its position in
4472 the iterator, so that we skip invisible text if
4473 later the bidi iteration lands us in the
4474 invisible region again. */
4475 if (IT_CHARPOS (*it) >= newpos)
4476 it->prev_stop = newpos;
4477 }
4478 }
4479 else
4480 {
4481 IT_CHARPOS (*it) = newpos;
4482 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4483 }
4484
4485 if (display_ellipsis_p)
4486 {
4487 /* Make sure that the glyphs of the ellipsis will get
4488 correct `charpos' values. If we would not update
4489 it->position here, the glyphs would belong to the
4490 last visible character _before_ the invisible
4491 text, which confuses `set_cursor_from_row'.
4492
4493 We use the last invisible position instead of the
4494 first because this way the cursor is always drawn on
4495 the first "." of the ellipsis, whenever PT is inside
4496 the invisible text. Otherwise the cursor would be
4497 placed _after_ the ellipsis when the point is after the
4498 first invisible character. */
4499 if (!STRINGP (it->object))
4500 {
4501 it->position.charpos = newpos - 1;
4502 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4503 }
4504 }
4505
4506 /* If there are before-strings at the start of invisible
4507 text, and the text is invisible because of a text
4508 property, arrange to show before-strings because 20.x did
4509 it that way. (If the text is invisible because of an
4510 overlay property instead of a text property, this is
4511 already handled in the overlay code.) */
4512 if (NILP (overlay)
4513 && get_overlay_strings (it, it->stop_charpos))
4514 {
4515 handled = HANDLED_RECOMPUTE_PROPS;
4516 if (it->sp > 0)
4517 {
4518 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4519 /* The call to get_overlay_strings above recomputes
4520 it->stop_charpos, but it only considers changes
4521 in properties and overlays beyond iterator's
4522 current position. This causes us to miss changes
4523 that happen exactly where the invisible property
4524 ended. So we play it safe here and force the
4525 iterator to check for potential stop positions
4526 immediately after the invisible text. Note that
4527 if get_overlay_strings returns true, it
4528 normally also pushed the iterator stack, so we
4529 need to update the stop position in the slot
4530 below the current one. */
4531 it->stack[it->sp - 1].stop_charpos
4532 = CHARPOS (it->stack[it->sp - 1].current.pos);
4533 }
4534 }
4535 else if (display_ellipsis_p)
4536 {
4537 it->ellipsis_p = true;
4538 /* Let the ellipsis display before
4539 considering any properties of the following char.
4540 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4541 handled = HANDLED_RETURN;
4542 }
4543 }
4544 }
4545
4546 return handled;
4547 }
4548
4549
4550 /* Make iterator IT return `...' next.
4551 Replaces LEN characters from buffer. */
4552
4553 static void
4554 setup_for_ellipsis (struct it *it, int len)
4555 {
4556 /* Use the display table definition for `...'. Invalid glyphs
4557 will be handled by the method returning elements from dpvec. */
4558 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4559 {
4560 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4561 it->dpvec = v->contents;
4562 it->dpend = v->contents + v->header.size;
4563 }
4564 else
4565 {
4566 /* Default `...'. */
4567 it->dpvec = default_invis_vector;
4568 it->dpend = default_invis_vector + 3;
4569 }
4570
4571 it->dpvec_char_len = len;
4572 it->current.dpvec_index = 0;
4573 it->dpvec_face_id = -1;
4574
4575 /* Remember the current face id in case glyphs specify faces.
4576 IT's face is restored in set_iterator_to_next.
4577 saved_face_id was set to preceding char's face in handle_stop. */
4578 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4579 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4580
4581 /* If the ellipsis represents buffer text, it means we advanced in
4582 the buffer, so we should no longer ignore overlay strings. */
4583 if (it->method == GET_FROM_BUFFER)
4584 it->ignore_overlay_strings_at_pos_p = false;
4585
4586 it->method = GET_FROM_DISPLAY_VECTOR;
4587 it->ellipsis_p = true;
4588 }
4589
4590
4591 \f
4592 /***********************************************************************
4593 'display' property
4594 ***********************************************************************/
4595
4596 /* Set up iterator IT from `display' property at its current position.
4597 Called from handle_stop.
4598 We return HANDLED_RETURN if some part of the display property
4599 overrides the display of the buffer text itself.
4600 Otherwise we return HANDLED_NORMALLY. */
4601
4602 static enum prop_handled
4603 handle_display_prop (struct it *it)
4604 {
4605 Lisp_Object propval, object, overlay;
4606 struct text_pos *position;
4607 ptrdiff_t bufpos;
4608 /* Nonzero if some property replaces the display of the text itself. */
4609 int display_replaced = 0;
4610
4611 if (STRINGP (it->string))
4612 {
4613 object = it->string;
4614 position = &it->current.string_pos;
4615 bufpos = CHARPOS (it->current.pos);
4616 }
4617 else
4618 {
4619 XSETWINDOW (object, it->w);
4620 position = &it->current.pos;
4621 bufpos = CHARPOS (*position);
4622 }
4623
4624 /* Reset those iterator values set from display property values. */
4625 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4626 it->space_width = Qnil;
4627 it->font_height = Qnil;
4628 it->voffset = 0;
4629
4630 /* We don't support recursive `display' properties, i.e. string
4631 values that have a string `display' property, that have a string
4632 `display' property etc. */
4633 if (!it->string_from_display_prop_p)
4634 it->area = TEXT_AREA;
4635
4636 propval = get_char_property_and_overlay (make_number (position->charpos),
4637 Qdisplay, object, &overlay);
4638 if (NILP (propval))
4639 return HANDLED_NORMALLY;
4640 /* Now OVERLAY is the overlay that gave us this property, or nil
4641 if it was a text property. */
4642
4643 if (!STRINGP (it->string))
4644 object = it->w->contents;
4645
4646 display_replaced = handle_display_spec (it, propval, object, overlay,
4647 position, bufpos,
4648 FRAME_WINDOW_P (it->f));
4649 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4650 }
4651
4652 /* Subroutine of handle_display_prop. Returns non-zero if the display
4653 specification in SPEC is a replacing specification, i.e. it would
4654 replace the text covered by `display' property with something else,
4655 such as an image or a display string. If SPEC includes any kind or
4656 `(space ...) specification, the value is 2; this is used by
4657 compute_display_string_pos, which see.
4658
4659 See handle_single_display_spec for documentation of arguments.
4660 FRAME_WINDOW_P is true if the window being redisplayed is on a
4661 GUI frame; this argument is used only if IT is NULL, see below.
4662
4663 IT can be NULL, if this is called by the bidi reordering code
4664 through compute_display_string_pos, which see. In that case, this
4665 function only examines SPEC, but does not otherwise "handle" it, in
4666 the sense that it doesn't set up members of IT from the display
4667 spec. */
4668 static int
4669 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4670 Lisp_Object overlay, struct text_pos *position,
4671 ptrdiff_t bufpos, bool frame_window_p)
4672 {
4673 int replacing = 0;
4674
4675 if (CONSP (spec)
4676 /* Simple specifications. */
4677 && !EQ (XCAR (spec), Qimage)
4678 && !EQ (XCAR (spec), Qspace)
4679 && !EQ (XCAR (spec), Qwhen)
4680 && !EQ (XCAR (spec), Qslice)
4681 && !EQ (XCAR (spec), Qspace_width)
4682 && !EQ (XCAR (spec), Qheight)
4683 && !EQ (XCAR (spec), Qraise)
4684 /* Marginal area specifications. */
4685 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4686 && !EQ (XCAR (spec), Qleft_fringe)
4687 && !EQ (XCAR (spec), Qright_fringe)
4688 && !NILP (XCAR (spec)))
4689 {
4690 for (; CONSP (spec); spec = XCDR (spec))
4691 {
4692 int rv = handle_single_display_spec (it, XCAR (spec), object,
4693 overlay, position, bufpos,
4694 replacing, frame_window_p);
4695 if (rv != 0)
4696 {
4697 replacing = rv;
4698 /* If some text in a string is replaced, `position' no
4699 longer points to the position of `object'. */
4700 if (!it || STRINGP (object))
4701 break;
4702 }
4703 }
4704 }
4705 else if (VECTORP (spec))
4706 {
4707 ptrdiff_t i;
4708 for (i = 0; i < ASIZE (spec); ++i)
4709 {
4710 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4711 overlay, position, bufpos,
4712 replacing, frame_window_p);
4713 if (rv != 0)
4714 {
4715 replacing = rv;
4716 /* If some text in a string is replaced, `position' no
4717 longer points to the position of `object'. */
4718 if (!it || STRINGP (object))
4719 break;
4720 }
4721 }
4722 }
4723 else
4724 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4725 bufpos, 0, frame_window_p);
4726 return replacing;
4727 }
4728
4729 /* Value is the position of the end of the `display' property starting
4730 at START_POS in OBJECT. */
4731
4732 static struct text_pos
4733 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4734 {
4735 Lisp_Object end;
4736 struct text_pos end_pos;
4737
4738 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4739 Qdisplay, object, Qnil);
4740 CHARPOS (end_pos) = XFASTINT (end);
4741 if (STRINGP (object))
4742 compute_string_pos (&end_pos, start_pos, it->string);
4743 else
4744 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4745
4746 return end_pos;
4747 }
4748
4749
4750 /* Set up IT from a single `display' property specification SPEC. OBJECT
4751 is the object in which the `display' property was found. *POSITION
4752 is the position in OBJECT at which the `display' property was found.
4753 BUFPOS is the buffer position of OBJECT (different from POSITION if
4754 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4755 previously saw a display specification which already replaced text
4756 display with something else, for example an image; we ignore such
4757 properties after the first one has been processed.
4758
4759 OVERLAY is the overlay this `display' property came from,
4760 or nil if it was a text property.
4761
4762 If SPEC is a `space' or `image' specification, and in some other
4763 cases too, set *POSITION to the position where the `display'
4764 property ends.
4765
4766 If IT is NULL, only examine the property specification in SPEC, but
4767 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4768 is intended to be displayed in a window on a GUI frame.
4769
4770 Value is non-zero if something was found which replaces the display
4771 of buffer or string text. */
4772
4773 static int
4774 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4775 Lisp_Object overlay, struct text_pos *position,
4776 ptrdiff_t bufpos, int display_replaced,
4777 bool frame_window_p)
4778 {
4779 Lisp_Object form;
4780 Lisp_Object location, value;
4781 struct text_pos start_pos = *position;
4782
4783 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4784 If the result is non-nil, use VALUE instead of SPEC. */
4785 form = Qt;
4786 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4787 {
4788 spec = XCDR (spec);
4789 if (!CONSP (spec))
4790 return 0;
4791 form = XCAR (spec);
4792 spec = XCDR (spec);
4793 }
4794
4795 if (!NILP (form) && !EQ (form, Qt))
4796 {
4797 ptrdiff_t count = SPECPDL_INDEX ();
4798
4799 /* Bind `object' to the object having the `display' property, a
4800 buffer or string. Bind `position' to the position in the
4801 object where the property was found, and `buffer-position'
4802 to the current position in the buffer. */
4803
4804 if (NILP (object))
4805 XSETBUFFER (object, current_buffer);
4806 specbind (Qobject, object);
4807 specbind (Qposition, make_number (CHARPOS (*position)));
4808 specbind (Qbuffer_position, make_number (bufpos));
4809 form = safe_eval (form);
4810 unbind_to (count, Qnil);
4811 }
4812
4813 if (NILP (form))
4814 return 0;
4815
4816 /* Handle `(height HEIGHT)' specifications. */
4817 if (CONSP (spec)
4818 && EQ (XCAR (spec), Qheight)
4819 && CONSP (XCDR (spec)))
4820 {
4821 if (it)
4822 {
4823 if (!FRAME_WINDOW_P (it->f))
4824 return 0;
4825
4826 it->font_height = XCAR (XCDR (spec));
4827 if (!NILP (it->font_height))
4828 {
4829 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4830 int new_height = -1;
4831
4832 if (CONSP (it->font_height)
4833 && (EQ (XCAR (it->font_height), Qplus)
4834 || EQ (XCAR (it->font_height), Qminus))
4835 && CONSP (XCDR (it->font_height))
4836 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4837 {
4838 /* `(+ N)' or `(- N)' where N is an integer. */
4839 int steps = XINT (XCAR (XCDR (it->font_height)));
4840 if (EQ (XCAR (it->font_height), Qplus))
4841 steps = - steps;
4842 it->face_id = smaller_face (it->f, it->face_id, steps);
4843 }
4844 else if (FUNCTIONP (it->font_height))
4845 {
4846 /* Call function with current height as argument.
4847 Value is the new height. */
4848 Lisp_Object height;
4849 height = safe_call1 (it->font_height,
4850 face->lface[LFACE_HEIGHT_INDEX]);
4851 if (NUMBERP (height))
4852 new_height = XFLOATINT (height);
4853 }
4854 else if (NUMBERP (it->font_height))
4855 {
4856 /* Value is a multiple of the canonical char height. */
4857 struct face *f;
4858
4859 f = FACE_FROM_ID (it->f,
4860 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4861 new_height = (XFLOATINT (it->font_height)
4862 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4863 }
4864 else
4865 {
4866 /* Evaluate IT->font_height with `height' bound to the
4867 current specified height to get the new height. */
4868 ptrdiff_t count = SPECPDL_INDEX ();
4869
4870 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4871 value = safe_eval (it->font_height);
4872 unbind_to (count, Qnil);
4873
4874 if (NUMBERP (value))
4875 new_height = XFLOATINT (value);
4876 }
4877
4878 if (new_height > 0)
4879 it->face_id = face_with_height (it->f, it->face_id, new_height);
4880 }
4881 }
4882
4883 return 0;
4884 }
4885
4886 /* Handle `(space-width WIDTH)'. */
4887 if (CONSP (spec)
4888 && EQ (XCAR (spec), Qspace_width)
4889 && CONSP (XCDR (spec)))
4890 {
4891 if (it)
4892 {
4893 if (!FRAME_WINDOW_P (it->f))
4894 return 0;
4895
4896 value = XCAR (XCDR (spec));
4897 if (NUMBERP (value) && XFLOATINT (value) > 0)
4898 it->space_width = value;
4899 }
4900
4901 return 0;
4902 }
4903
4904 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4905 if (CONSP (spec)
4906 && EQ (XCAR (spec), Qslice))
4907 {
4908 Lisp_Object tem;
4909
4910 if (it)
4911 {
4912 if (!FRAME_WINDOW_P (it->f))
4913 return 0;
4914
4915 if (tem = XCDR (spec), CONSP (tem))
4916 {
4917 it->slice.x = XCAR (tem);
4918 if (tem = XCDR (tem), CONSP (tem))
4919 {
4920 it->slice.y = XCAR (tem);
4921 if (tem = XCDR (tem), CONSP (tem))
4922 {
4923 it->slice.width = XCAR (tem);
4924 if (tem = XCDR (tem), CONSP (tem))
4925 it->slice.height = XCAR (tem);
4926 }
4927 }
4928 }
4929 }
4930
4931 return 0;
4932 }
4933
4934 /* Handle `(raise FACTOR)'. */
4935 if (CONSP (spec)
4936 && EQ (XCAR (spec), Qraise)
4937 && CONSP (XCDR (spec)))
4938 {
4939 if (it)
4940 {
4941 if (!FRAME_WINDOW_P (it->f))
4942 return 0;
4943
4944 #ifdef HAVE_WINDOW_SYSTEM
4945 value = XCAR (XCDR (spec));
4946 if (NUMBERP (value))
4947 {
4948 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4949 it->voffset = - (XFLOATINT (value)
4950 * (normal_char_height (face->font, -1)));
4951 }
4952 #endif /* HAVE_WINDOW_SYSTEM */
4953 }
4954
4955 return 0;
4956 }
4957
4958 /* Don't handle the other kinds of display specifications
4959 inside a string that we got from a `display' property. */
4960 if (it && it->string_from_display_prop_p)
4961 return 0;
4962
4963 /* Characters having this form of property are not displayed, so
4964 we have to find the end of the property. */
4965 if (it)
4966 {
4967 start_pos = *position;
4968 *position = display_prop_end (it, object, start_pos);
4969 /* If the display property comes from an overlay, don't consider
4970 any potential stop_charpos values before the end of that
4971 overlay. Since display_prop_end will happily find another
4972 'display' property coming from some other overlay or text
4973 property on buffer positions before this overlay's end, we
4974 need to ignore them, or else we risk displaying this
4975 overlay's display string/image twice. */
4976 if (!NILP (overlay))
4977 {
4978 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4979
4980 if (ovendpos > CHARPOS (*position))
4981 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
4982 }
4983 }
4984 value = Qnil;
4985
4986 /* Stop the scan at that end position--we assume that all
4987 text properties change there. */
4988 if (it)
4989 it->stop_charpos = position->charpos;
4990
4991 /* Handle `(left-fringe BITMAP [FACE])'
4992 and `(right-fringe BITMAP [FACE])'. */
4993 if (CONSP (spec)
4994 && (EQ (XCAR (spec), Qleft_fringe)
4995 || EQ (XCAR (spec), Qright_fringe))
4996 && CONSP (XCDR (spec)))
4997 {
4998 int fringe_bitmap;
4999
5000 if (it)
5001 {
5002 if (!FRAME_WINDOW_P (it->f))
5003 /* If we return here, POSITION has been advanced
5004 across the text with this property. */
5005 {
5006 /* Synchronize the bidi iterator with POSITION. This is
5007 needed because we are not going to push the iterator
5008 on behalf of this display property, so there will be
5009 no pop_it call to do this synchronization for us. */
5010 if (it->bidi_p)
5011 {
5012 it->position = *position;
5013 iterate_out_of_display_property (it);
5014 *position = it->position;
5015 }
5016 return 1;
5017 }
5018 }
5019 else if (!frame_window_p)
5020 return 1;
5021
5022 #ifdef HAVE_WINDOW_SYSTEM
5023 value = XCAR (XCDR (spec));
5024 if (!SYMBOLP (value)
5025 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
5026 /* If we return here, POSITION has been advanced
5027 across the text with this property. */
5028 {
5029 if (it && it->bidi_p)
5030 {
5031 it->position = *position;
5032 iterate_out_of_display_property (it);
5033 *position = it->position;
5034 }
5035 return 1;
5036 }
5037
5038 if (it)
5039 {
5040 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5041
5042 if (CONSP (XCDR (XCDR (spec))))
5043 {
5044 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5045 int face_id2 = lookup_derived_face (it->f, face_name,
5046 FRINGE_FACE_ID, false);
5047 if (face_id2 >= 0)
5048 face_id = face_id2;
5049 }
5050
5051 /* Save current settings of IT so that we can restore them
5052 when we are finished with the glyph property value. */
5053 push_it (it, position);
5054
5055 it->area = TEXT_AREA;
5056 it->what = IT_IMAGE;
5057 it->image_id = -1; /* no image */
5058 it->position = start_pos;
5059 it->object = NILP (object) ? it->w->contents : object;
5060 it->method = GET_FROM_IMAGE;
5061 it->from_overlay = Qnil;
5062 it->face_id = face_id;
5063 it->from_disp_prop_p = true;
5064
5065 /* Say that we haven't consumed the characters with
5066 `display' property yet. The call to pop_it in
5067 set_iterator_to_next will clean this up. */
5068 *position = start_pos;
5069
5070 if (EQ (XCAR (spec), Qleft_fringe))
5071 {
5072 it->left_user_fringe_bitmap = fringe_bitmap;
5073 it->left_user_fringe_face_id = face_id;
5074 }
5075 else
5076 {
5077 it->right_user_fringe_bitmap = fringe_bitmap;
5078 it->right_user_fringe_face_id = face_id;
5079 }
5080 }
5081 #endif /* HAVE_WINDOW_SYSTEM */
5082 return 1;
5083 }
5084
5085 /* Prepare to handle `((margin left-margin) ...)',
5086 `((margin right-margin) ...)' and `((margin nil) ...)'
5087 prefixes for display specifications. */
5088 location = Qunbound;
5089 if (CONSP (spec) && CONSP (XCAR (spec)))
5090 {
5091 Lisp_Object tem;
5092
5093 value = XCDR (spec);
5094 if (CONSP (value))
5095 value = XCAR (value);
5096
5097 tem = XCAR (spec);
5098 if (EQ (XCAR (tem), Qmargin)
5099 && (tem = XCDR (tem),
5100 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5101 (NILP (tem)
5102 || EQ (tem, Qleft_margin)
5103 || EQ (tem, Qright_margin))))
5104 location = tem;
5105 }
5106
5107 if (EQ (location, Qunbound))
5108 {
5109 location = Qnil;
5110 value = spec;
5111 }
5112
5113 /* After this point, VALUE is the property after any
5114 margin prefix has been stripped. It must be a string,
5115 an image specification, or `(space ...)'.
5116
5117 LOCATION specifies where to display: `left-margin',
5118 `right-margin' or nil. */
5119
5120 bool valid_p = (STRINGP (value)
5121 #ifdef HAVE_WINDOW_SYSTEM
5122 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5123 && valid_image_p (value))
5124 #endif /* not HAVE_WINDOW_SYSTEM */
5125 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5126
5127 if (valid_p && display_replaced == 0)
5128 {
5129 int retval = 1;
5130
5131 if (!it)
5132 {
5133 /* Callers need to know whether the display spec is any kind
5134 of `(space ...)' spec that is about to affect text-area
5135 display. */
5136 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5137 retval = 2;
5138 return retval;
5139 }
5140
5141 /* Save current settings of IT so that we can restore them
5142 when we are finished with the glyph property value. */
5143 push_it (it, position);
5144 it->from_overlay = overlay;
5145 it->from_disp_prop_p = true;
5146
5147 if (NILP (location))
5148 it->area = TEXT_AREA;
5149 else if (EQ (location, Qleft_margin))
5150 it->area = LEFT_MARGIN_AREA;
5151 else
5152 it->area = RIGHT_MARGIN_AREA;
5153
5154 if (STRINGP (value))
5155 {
5156 it->string = value;
5157 it->multibyte_p = STRING_MULTIBYTE (it->string);
5158 it->current.overlay_string_index = -1;
5159 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5160 it->end_charpos = it->string_nchars = SCHARS (it->string);
5161 it->method = GET_FROM_STRING;
5162 it->stop_charpos = 0;
5163 it->prev_stop = 0;
5164 it->base_level_stop = 0;
5165 it->string_from_display_prop_p = true;
5166 /* Say that we haven't consumed the characters with
5167 `display' property yet. The call to pop_it in
5168 set_iterator_to_next will clean this up. */
5169 if (BUFFERP (object))
5170 *position = start_pos;
5171
5172 /* Force paragraph direction to be that of the parent
5173 object. If the parent object's paragraph direction is
5174 not yet determined, default to L2R. */
5175 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5176 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5177 else
5178 it->paragraph_embedding = L2R;
5179
5180 /* Set up the bidi iterator for this display string. */
5181 if (it->bidi_p)
5182 {
5183 it->bidi_it.string.lstring = it->string;
5184 it->bidi_it.string.s = NULL;
5185 it->bidi_it.string.schars = it->end_charpos;
5186 it->bidi_it.string.bufpos = bufpos;
5187 it->bidi_it.string.from_disp_str = true;
5188 it->bidi_it.string.unibyte = !it->multibyte_p;
5189 it->bidi_it.w = it->w;
5190 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5191 }
5192 }
5193 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5194 {
5195 it->method = GET_FROM_STRETCH;
5196 it->object = value;
5197 *position = it->position = start_pos;
5198 retval = 1 + (it->area == TEXT_AREA);
5199 }
5200 #ifdef HAVE_WINDOW_SYSTEM
5201 else
5202 {
5203 it->what = IT_IMAGE;
5204 it->image_id = lookup_image (it->f, value);
5205 it->position = start_pos;
5206 it->object = NILP (object) ? it->w->contents : object;
5207 it->method = GET_FROM_IMAGE;
5208
5209 /* Say that we haven't consumed the characters with
5210 `display' property yet. The call to pop_it in
5211 set_iterator_to_next will clean this up. */
5212 *position = start_pos;
5213 }
5214 #endif /* HAVE_WINDOW_SYSTEM */
5215
5216 return retval;
5217 }
5218
5219 /* Invalid property or property not supported. Restore
5220 POSITION to what it was before. */
5221 *position = start_pos;
5222 return 0;
5223 }
5224
5225 /* Check if PROP is a display property value whose text should be
5226 treated as intangible. OVERLAY is the overlay from which PROP
5227 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5228 specify the buffer position covered by PROP. */
5229
5230 bool
5231 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5232 ptrdiff_t charpos, ptrdiff_t bytepos)
5233 {
5234 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5235 struct text_pos position;
5236
5237 SET_TEXT_POS (position, charpos, bytepos);
5238 return (handle_display_spec (NULL, prop, Qnil, overlay,
5239 &position, charpos, frame_window_p)
5240 != 0);
5241 }
5242
5243
5244 /* Return true if PROP is a display sub-property value containing STRING.
5245
5246 Implementation note: this and the following function are really
5247 special cases of handle_display_spec and
5248 handle_single_display_spec, and should ideally use the same code.
5249 Until they do, these two pairs must be consistent and must be
5250 modified in sync. */
5251
5252 static bool
5253 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5254 {
5255 if (EQ (string, prop))
5256 return true;
5257
5258 /* Skip over `when FORM'. */
5259 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5260 {
5261 prop = XCDR (prop);
5262 if (!CONSP (prop))
5263 return false;
5264 /* Actually, the condition following `when' should be eval'ed,
5265 like handle_single_display_spec does, and we should return
5266 false if it evaluates to nil. However, this function is
5267 called only when the buffer was already displayed and some
5268 glyph in the glyph matrix was found to come from a display
5269 string. Therefore, the condition was already evaluated, and
5270 the result was non-nil, otherwise the display string wouldn't
5271 have been displayed and we would have never been called for
5272 this property. Thus, we can skip the evaluation and assume
5273 its result is non-nil. */
5274 prop = XCDR (prop);
5275 }
5276
5277 if (CONSP (prop))
5278 /* Skip over `margin LOCATION'. */
5279 if (EQ (XCAR (prop), Qmargin))
5280 {
5281 prop = XCDR (prop);
5282 if (!CONSP (prop))
5283 return false;
5284
5285 prop = XCDR (prop);
5286 if (!CONSP (prop))
5287 return false;
5288 }
5289
5290 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5291 }
5292
5293
5294 /* Return true if STRING appears in the `display' property PROP. */
5295
5296 static bool
5297 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5298 {
5299 if (CONSP (prop)
5300 && !EQ (XCAR (prop), Qwhen)
5301 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5302 {
5303 /* A list of sub-properties. */
5304 while (CONSP (prop))
5305 {
5306 if (single_display_spec_string_p (XCAR (prop), string))
5307 return true;
5308 prop = XCDR (prop);
5309 }
5310 }
5311 else if (VECTORP (prop))
5312 {
5313 /* A vector of sub-properties. */
5314 ptrdiff_t i;
5315 for (i = 0; i < ASIZE (prop); ++i)
5316 if (single_display_spec_string_p (AREF (prop, i), string))
5317 return true;
5318 }
5319 else
5320 return single_display_spec_string_p (prop, string);
5321
5322 return false;
5323 }
5324
5325 /* Look for STRING in overlays and text properties in the current
5326 buffer, between character positions FROM and TO (excluding TO).
5327 BACK_P means look back (in this case, TO is supposed to be
5328 less than FROM).
5329 Value is the first character position where STRING was found, or
5330 zero if it wasn't found before hitting TO.
5331
5332 This function may only use code that doesn't eval because it is
5333 called asynchronously from note_mouse_highlight. */
5334
5335 static ptrdiff_t
5336 string_buffer_position_lim (Lisp_Object string,
5337 ptrdiff_t from, ptrdiff_t to, bool back_p)
5338 {
5339 Lisp_Object limit, prop, pos;
5340 bool found = false;
5341
5342 pos = make_number (max (from, BEGV));
5343
5344 if (!back_p) /* looking forward */
5345 {
5346 limit = make_number (min (to, ZV));
5347 while (!found && !EQ (pos, limit))
5348 {
5349 prop = Fget_char_property (pos, Qdisplay, Qnil);
5350 if (!NILP (prop) && display_prop_string_p (prop, string))
5351 found = true;
5352 else
5353 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5354 limit);
5355 }
5356 }
5357 else /* looking back */
5358 {
5359 limit = make_number (max (to, BEGV));
5360 while (!found && !EQ (pos, limit))
5361 {
5362 prop = Fget_char_property (pos, Qdisplay, Qnil);
5363 if (!NILP (prop) && display_prop_string_p (prop, string))
5364 found = true;
5365 else
5366 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5367 limit);
5368 }
5369 }
5370
5371 return found ? XINT (pos) : 0;
5372 }
5373
5374 /* Determine which buffer position in current buffer STRING comes from.
5375 AROUND_CHARPOS is an approximate position where it could come from.
5376 Value is the buffer position or 0 if it couldn't be determined.
5377
5378 This function is necessary because we don't record buffer positions
5379 in glyphs generated from strings (to keep struct glyph small).
5380 This function may only use code that doesn't eval because it is
5381 called asynchronously from note_mouse_highlight. */
5382
5383 static ptrdiff_t
5384 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5385 {
5386 const int MAX_DISTANCE = 1000;
5387 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5388 around_charpos + MAX_DISTANCE,
5389 false);
5390
5391 if (!found)
5392 found = string_buffer_position_lim (string, around_charpos,
5393 around_charpos - MAX_DISTANCE, true);
5394 return found;
5395 }
5396
5397
5398 \f
5399 /***********************************************************************
5400 `composition' property
5401 ***********************************************************************/
5402
5403 /* Set up iterator IT from `composition' property at its current
5404 position. Called from handle_stop. */
5405
5406 static enum prop_handled
5407 handle_composition_prop (struct it *it)
5408 {
5409 Lisp_Object prop, string;
5410 ptrdiff_t pos, pos_byte, start, end;
5411
5412 if (STRINGP (it->string))
5413 {
5414 unsigned char *s;
5415
5416 pos = IT_STRING_CHARPOS (*it);
5417 pos_byte = IT_STRING_BYTEPOS (*it);
5418 string = it->string;
5419 s = SDATA (string) + pos_byte;
5420 it->c = STRING_CHAR (s);
5421 }
5422 else
5423 {
5424 pos = IT_CHARPOS (*it);
5425 pos_byte = IT_BYTEPOS (*it);
5426 string = Qnil;
5427 it->c = FETCH_CHAR (pos_byte);
5428 }
5429
5430 /* If there's a valid composition and point is not inside of the
5431 composition (in the case that the composition is from the current
5432 buffer), draw a glyph composed from the composition components. */
5433 if (find_composition (pos, -1, &start, &end, &prop, string)
5434 && composition_valid_p (start, end, prop)
5435 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5436 {
5437 if (start < pos)
5438 /* As we can't handle this situation (perhaps font-lock added
5439 a new composition), we just return here hoping that next
5440 redisplay will detect this composition much earlier. */
5441 return HANDLED_NORMALLY;
5442 if (start != pos)
5443 {
5444 if (STRINGP (it->string))
5445 pos_byte = string_char_to_byte (it->string, start);
5446 else
5447 pos_byte = CHAR_TO_BYTE (start);
5448 }
5449 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5450 prop, string);
5451
5452 if (it->cmp_it.id >= 0)
5453 {
5454 it->cmp_it.ch = -1;
5455 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5456 it->cmp_it.nglyphs = -1;
5457 }
5458 }
5459
5460 return HANDLED_NORMALLY;
5461 }
5462
5463
5464 \f
5465 /***********************************************************************
5466 Overlay strings
5467 ***********************************************************************/
5468
5469 /* The following structure is used to record overlay strings for
5470 later sorting in load_overlay_strings. */
5471
5472 struct overlay_entry
5473 {
5474 Lisp_Object overlay;
5475 Lisp_Object string;
5476 EMACS_INT priority;
5477 bool after_string_p;
5478 };
5479
5480
5481 /* Set up iterator IT from overlay strings at its current position.
5482 Called from handle_stop. */
5483
5484 static enum prop_handled
5485 handle_overlay_change (struct it *it)
5486 {
5487 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5488 return HANDLED_RECOMPUTE_PROPS;
5489 else
5490 return HANDLED_NORMALLY;
5491 }
5492
5493
5494 /* Set up the next overlay string for delivery by IT, if there is an
5495 overlay string to deliver. Called by set_iterator_to_next when the
5496 end of the current overlay string is reached. If there are more
5497 overlay strings to display, IT->string and
5498 IT->current.overlay_string_index are set appropriately here.
5499 Otherwise IT->string is set to nil. */
5500
5501 static void
5502 next_overlay_string (struct it *it)
5503 {
5504 ++it->current.overlay_string_index;
5505 if (it->current.overlay_string_index == it->n_overlay_strings)
5506 {
5507 /* No more overlay strings. Restore IT's settings to what
5508 they were before overlay strings were processed, and
5509 continue to deliver from current_buffer. */
5510
5511 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5512 pop_it (it);
5513 eassert (it->sp > 0
5514 || (NILP (it->string)
5515 && it->method == GET_FROM_BUFFER
5516 && it->stop_charpos >= BEGV
5517 && it->stop_charpos <= it->end_charpos));
5518 it->current.overlay_string_index = -1;
5519 it->n_overlay_strings = 0;
5520 /* If there's an empty display string on the stack, pop the
5521 stack, to resync the bidi iterator with IT's position. Such
5522 empty strings are pushed onto the stack in
5523 get_overlay_strings_1. */
5524 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5525 pop_it (it);
5526
5527 /* Since we've exhausted overlay strings at this buffer
5528 position, set the flag to ignore overlays until we move to
5529 another position. The flag is reset in
5530 next_element_from_buffer. */
5531 it->ignore_overlay_strings_at_pos_p = true;
5532
5533 /* If we're at the end of the buffer, record that we have
5534 processed the overlay strings there already, so that
5535 next_element_from_buffer doesn't try it again. */
5536 if (NILP (it->string)
5537 && IT_CHARPOS (*it) >= it->end_charpos
5538 && it->overlay_strings_charpos >= it->end_charpos)
5539 it->overlay_strings_at_end_processed_p = true;
5540 /* Note: we reset overlay_strings_charpos only here, to make
5541 sure the just-processed overlays were indeed at EOB.
5542 Otherwise, overlays on text with invisible text property,
5543 which are processed with IT's position past the invisible
5544 text, might fool us into thinking the overlays at EOB were
5545 already processed (linum-mode can cause this, for
5546 example). */
5547 it->overlay_strings_charpos = -1;
5548 }
5549 else
5550 {
5551 /* There are more overlay strings to process. If
5552 IT->current.overlay_string_index has advanced to a position
5553 where we must load IT->overlay_strings with more strings, do
5554 it. We must load at the IT->overlay_strings_charpos where
5555 IT->n_overlay_strings was originally computed; when invisible
5556 text is present, this might not be IT_CHARPOS (Bug#7016). */
5557 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5558
5559 if (it->current.overlay_string_index && i == 0)
5560 load_overlay_strings (it, it->overlay_strings_charpos);
5561
5562 /* Initialize IT to deliver display elements from the overlay
5563 string. */
5564 it->string = it->overlay_strings[i];
5565 it->multibyte_p = STRING_MULTIBYTE (it->string);
5566 SET_TEXT_POS (it->current.string_pos, 0, 0);
5567 it->method = GET_FROM_STRING;
5568 it->stop_charpos = 0;
5569 it->end_charpos = SCHARS (it->string);
5570 if (it->cmp_it.stop_pos >= 0)
5571 it->cmp_it.stop_pos = 0;
5572 it->prev_stop = 0;
5573 it->base_level_stop = 0;
5574
5575 /* Set up the bidi iterator for this overlay string. */
5576 if (it->bidi_p)
5577 {
5578 it->bidi_it.string.lstring = it->string;
5579 it->bidi_it.string.s = NULL;
5580 it->bidi_it.string.schars = SCHARS (it->string);
5581 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5582 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5583 it->bidi_it.string.unibyte = !it->multibyte_p;
5584 it->bidi_it.w = it->w;
5585 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5586 }
5587 }
5588
5589 CHECK_IT (it);
5590 }
5591
5592
5593 /* Compare two overlay_entry structures E1 and E2. Used as a
5594 comparison function for qsort in load_overlay_strings. Overlay
5595 strings for the same position are sorted so that
5596
5597 1. All after-strings come in front of before-strings, except
5598 when they come from the same overlay.
5599
5600 2. Within after-strings, strings are sorted so that overlay strings
5601 from overlays with higher priorities come first.
5602
5603 2. Within before-strings, strings are sorted so that overlay
5604 strings from overlays with higher priorities come last.
5605
5606 Value is analogous to strcmp. */
5607
5608
5609 static int
5610 compare_overlay_entries (const void *e1, const void *e2)
5611 {
5612 struct overlay_entry const *entry1 = e1;
5613 struct overlay_entry const *entry2 = e2;
5614 int result;
5615
5616 if (entry1->after_string_p != entry2->after_string_p)
5617 {
5618 /* Let after-strings appear in front of before-strings if
5619 they come from different overlays. */
5620 if (EQ (entry1->overlay, entry2->overlay))
5621 result = entry1->after_string_p ? 1 : -1;
5622 else
5623 result = entry1->after_string_p ? -1 : 1;
5624 }
5625 else if (entry1->priority != entry2->priority)
5626 {
5627 if (entry1->after_string_p)
5628 /* After-strings sorted in order of decreasing priority. */
5629 result = entry2->priority < entry1->priority ? -1 : 1;
5630 else
5631 /* Before-strings sorted in order of increasing priority. */
5632 result = entry1->priority < entry2->priority ? -1 : 1;
5633 }
5634 else
5635 result = 0;
5636
5637 return result;
5638 }
5639
5640
5641 /* Load the vector IT->overlay_strings with overlay strings from IT's
5642 current buffer position, or from CHARPOS if that is > 0. Set
5643 IT->n_overlays to the total number of overlay strings found.
5644
5645 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5646 a time. On entry into load_overlay_strings,
5647 IT->current.overlay_string_index gives the number of overlay
5648 strings that have already been loaded by previous calls to this
5649 function.
5650
5651 IT->add_overlay_start contains an additional overlay start
5652 position to consider for taking overlay strings from, if non-zero.
5653 This position comes into play when the overlay has an `invisible'
5654 property, and both before and after-strings. When we've skipped to
5655 the end of the overlay, because of its `invisible' property, we
5656 nevertheless want its before-string to appear.
5657 IT->add_overlay_start will contain the overlay start position
5658 in this case.
5659
5660 Overlay strings are sorted so that after-string strings come in
5661 front of before-string strings. Within before and after-strings,
5662 strings are sorted by overlay priority. See also function
5663 compare_overlay_entries. */
5664
5665 static void
5666 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5667 {
5668 Lisp_Object overlay, window, str, invisible;
5669 struct Lisp_Overlay *ov;
5670 ptrdiff_t start, end;
5671 ptrdiff_t n = 0, i, j;
5672 int invis;
5673 struct overlay_entry entriesbuf[20];
5674 ptrdiff_t size = ARRAYELTS (entriesbuf);
5675 struct overlay_entry *entries = entriesbuf;
5676 USE_SAFE_ALLOCA;
5677
5678 if (charpos <= 0)
5679 charpos = IT_CHARPOS (*it);
5680
5681 /* Append the overlay string STRING of overlay OVERLAY to vector
5682 `entries' which has size `size' and currently contains `n'
5683 elements. AFTER_P means STRING is an after-string of
5684 OVERLAY. */
5685 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5686 do \
5687 { \
5688 Lisp_Object priority; \
5689 \
5690 if (n == size) \
5691 { \
5692 struct overlay_entry *old = entries; \
5693 SAFE_NALLOCA (entries, 2, size); \
5694 memcpy (entries, old, size * sizeof *entries); \
5695 size *= 2; \
5696 } \
5697 \
5698 entries[n].string = (STRING); \
5699 entries[n].overlay = (OVERLAY); \
5700 priority = Foverlay_get ((OVERLAY), Qpriority); \
5701 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5702 entries[n].after_string_p = (AFTER_P); \
5703 ++n; \
5704 } \
5705 while (false)
5706
5707 /* Process overlay before the overlay center. */
5708 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5709 {
5710 XSETMISC (overlay, ov);
5711 eassert (OVERLAYP (overlay));
5712 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5713 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5714
5715 if (end < charpos)
5716 break;
5717
5718 /* Skip this overlay if it doesn't start or end at IT's current
5719 position. */
5720 if (end != charpos && start != charpos)
5721 continue;
5722
5723 /* Skip this overlay if it doesn't apply to IT->w. */
5724 window = Foverlay_get (overlay, Qwindow);
5725 if (WINDOWP (window) && XWINDOW (window) != it->w)
5726 continue;
5727
5728 /* If the text ``under'' the overlay is invisible, both before-
5729 and after-strings from this overlay are visible; start and
5730 end position are indistinguishable. */
5731 invisible = Foverlay_get (overlay, Qinvisible);
5732 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5733
5734 /* If overlay has a non-empty before-string, record it. */
5735 if ((start == charpos || (end == charpos && invis != 0))
5736 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5737 && SCHARS (str))
5738 RECORD_OVERLAY_STRING (overlay, str, false);
5739
5740 /* If overlay has a non-empty after-string, record it. */
5741 if ((end == charpos || (start == charpos && invis != 0))
5742 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5743 && SCHARS (str))
5744 RECORD_OVERLAY_STRING (overlay, str, true);
5745 }
5746
5747 /* Process overlays after the overlay center. */
5748 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5749 {
5750 XSETMISC (overlay, ov);
5751 eassert (OVERLAYP (overlay));
5752 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5753 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5754
5755 if (start > charpos)
5756 break;
5757
5758 /* Skip this overlay if it doesn't start or end at IT's current
5759 position. */
5760 if (end != charpos && start != charpos)
5761 continue;
5762
5763 /* Skip this overlay if it doesn't apply to IT->w. */
5764 window = Foverlay_get (overlay, Qwindow);
5765 if (WINDOWP (window) && XWINDOW (window) != it->w)
5766 continue;
5767
5768 /* If the text ``under'' the overlay is invisible, it has a zero
5769 dimension, and both before- and after-strings apply. */
5770 invisible = Foverlay_get (overlay, Qinvisible);
5771 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5772
5773 /* If overlay has a non-empty before-string, record it. */
5774 if ((start == charpos || (end == charpos && invis != 0))
5775 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5776 && SCHARS (str))
5777 RECORD_OVERLAY_STRING (overlay, str, false);
5778
5779 /* If overlay has a non-empty after-string, record it. */
5780 if ((end == charpos || (start == charpos && invis != 0))
5781 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5782 && SCHARS (str))
5783 RECORD_OVERLAY_STRING (overlay, str, true);
5784 }
5785
5786 #undef RECORD_OVERLAY_STRING
5787
5788 /* Sort entries. */
5789 if (n > 1)
5790 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5791
5792 /* Record number of overlay strings, and where we computed it. */
5793 it->n_overlay_strings = n;
5794 it->overlay_strings_charpos = charpos;
5795
5796 /* IT->current.overlay_string_index is the number of overlay strings
5797 that have already been consumed by IT. Copy some of the
5798 remaining overlay strings to IT->overlay_strings. */
5799 i = 0;
5800 j = it->current.overlay_string_index;
5801 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5802 {
5803 it->overlay_strings[i] = entries[j].string;
5804 it->string_overlays[i++] = entries[j++].overlay;
5805 }
5806
5807 CHECK_IT (it);
5808 SAFE_FREE ();
5809 }
5810
5811
5812 /* Get the first chunk of overlay strings at IT's current buffer
5813 position, or at CHARPOS if that is > 0. Value is true if at
5814 least one overlay string was found. */
5815
5816 static bool
5817 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5818 {
5819 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5820 process. This fills IT->overlay_strings with strings, and sets
5821 IT->n_overlay_strings to the total number of strings to process.
5822 IT->pos.overlay_string_index has to be set temporarily to zero
5823 because load_overlay_strings needs this; it must be set to -1
5824 when no overlay strings are found because a zero value would
5825 indicate a position in the first overlay string. */
5826 it->current.overlay_string_index = 0;
5827 load_overlay_strings (it, charpos);
5828
5829 /* If we found overlay strings, set up IT to deliver display
5830 elements from the first one. Otherwise set up IT to deliver
5831 from current_buffer. */
5832 if (it->n_overlay_strings)
5833 {
5834 /* Make sure we know settings in current_buffer, so that we can
5835 restore meaningful values when we're done with the overlay
5836 strings. */
5837 if (compute_stop_p)
5838 compute_stop_pos (it);
5839 eassert (it->face_id >= 0);
5840
5841 /* Save IT's settings. They are restored after all overlay
5842 strings have been processed. */
5843 eassert (!compute_stop_p || it->sp == 0);
5844
5845 /* When called from handle_stop, there might be an empty display
5846 string loaded. In that case, don't bother saving it. But
5847 don't use this optimization with the bidi iterator, since we
5848 need the corresponding pop_it call to resync the bidi
5849 iterator's position with IT's position, after we are done
5850 with the overlay strings. (The corresponding call to pop_it
5851 in case of an empty display string is in
5852 next_overlay_string.) */
5853 if (!(!it->bidi_p
5854 && STRINGP (it->string) && !SCHARS (it->string)))
5855 push_it (it, NULL);
5856
5857 /* Set up IT to deliver display elements from the first overlay
5858 string. */
5859 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5860 it->string = it->overlay_strings[0];
5861 it->from_overlay = Qnil;
5862 it->stop_charpos = 0;
5863 eassert (STRINGP (it->string));
5864 it->end_charpos = SCHARS (it->string);
5865 it->prev_stop = 0;
5866 it->base_level_stop = 0;
5867 it->multibyte_p = STRING_MULTIBYTE (it->string);
5868 it->method = GET_FROM_STRING;
5869 it->from_disp_prop_p = 0;
5870
5871 /* Force paragraph direction to be that of the parent
5872 buffer. */
5873 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5874 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5875 else
5876 it->paragraph_embedding = L2R;
5877
5878 /* Set up the bidi iterator for this overlay string. */
5879 if (it->bidi_p)
5880 {
5881 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5882
5883 it->bidi_it.string.lstring = it->string;
5884 it->bidi_it.string.s = NULL;
5885 it->bidi_it.string.schars = SCHARS (it->string);
5886 it->bidi_it.string.bufpos = pos;
5887 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5888 it->bidi_it.string.unibyte = !it->multibyte_p;
5889 it->bidi_it.w = it->w;
5890 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5891 }
5892 return true;
5893 }
5894
5895 it->current.overlay_string_index = -1;
5896 return false;
5897 }
5898
5899 static bool
5900 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5901 {
5902 it->string = Qnil;
5903 it->method = GET_FROM_BUFFER;
5904
5905 get_overlay_strings_1 (it, charpos, true);
5906
5907 CHECK_IT (it);
5908
5909 /* Value is true if we found at least one overlay string. */
5910 return STRINGP (it->string);
5911 }
5912
5913
5914 \f
5915 /***********************************************************************
5916 Saving and restoring state
5917 ***********************************************************************/
5918
5919 /* Save current settings of IT on IT->stack. Called, for example,
5920 before setting up IT for an overlay string, to be able to restore
5921 IT's settings to what they were after the overlay string has been
5922 processed. If POSITION is non-NULL, it is the position to save on
5923 the stack instead of IT->position. */
5924
5925 static void
5926 push_it (struct it *it, struct text_pos *position)
5927 {
5928 struct iterator_stack_entry *p;
5929
5930 eassert (it->sp < IT_STACK_SIZE);
5931 p = it->stack + it->sp;
5932
5933 p->stop_charpos = it->stop_charpos;
5934 p->prev_stop = it->prev_stop;
5935 p->base_level_stop = it->base_level_stop;
5936 p->cmp_it = it->cmp_it;
5937 eassert (it->face_id >= 0);
5938 p->face_id = it->face_id;
5939 p->string = it->string;
5940 p->method = it->method;
5941 p->from_overlay = it->from_overlay;
5942 switch (p->method)
5943 {
5944 case GET_FROM_IMAGE:
5945 p->u.image.object = it->object;
5946 p->u.image.image_id = it->image_id;
5947 p->u.image.slice = it->slice;
5948 break;
5949 case GET_FROM_STRETCH:
5950 p->u.stretch.object = it->object;
5951 break;
5952 case GET_FROM_BUFFER:
5953 case GET_FROM_DISPLAY_VECTOR:
5954 case GET_FROM_STRING:
5955 case GET_FROM_C_STRING:
5956 break;
5957 default:
5958 emacs_abort ();
5959 }
5960 p->position = position ? *position : it->position;
5961 p->current = it->current;
5962 p->end_charpos = it->end_charpos;
5963 p->string_nchars = it->string_nchars;
5964 p->area = it->area;
5965 p->multibyte_p = it->multibyte_p;
5966 p->avoid_cursor_p = it->avoid_cursor_p;
5967 p->space_width = it->space_width;
5968 p->font_height = it->font_height;
5969 p->voffset = it->voffset;
5970 p->string_from_display_prop_p = it->string_from_display_prop_p;
5971 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5972 p->display_ellipsis_p = false;
5973 p->line_wrap = it->line_wrap;
5974 p->bidi_p = it->bidi_p;
5975 p->paragraph_embedding = it->paragraph_embedding;
5976 p->from_disp_prop_p = it->from_disp_prop_p;
5977 ++it->sp;
5978
5979 /* Save the state of the bidi iterator as well. */
5980 if (it->bidi_p)
5981 bidi_push_it (&it->bidi_it);
5982 }
5983
5984 static void
5985 iterate_out_of_display_property (struct it *it)
5986 {
5987 bool buffer_p = !STRINGP (it->string);
5988 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5989 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5990
5991 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5992
5993 /* Maybe initialize paragraph direction. If we are at the beginning
5994 of a new paragraph, next_element_from_buffer may not have a
5995 chance to do that. */
5996 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5997 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
5998 /* prev_stop can be zero, so check against BEGV as well. */
5999 while (it->bidi_it.charpos >= bob
6000 && it->prev_stop <= it->bidi_it.charpos
6001 && it->bidi_it.charpos < CHARPOS (it->position)
6002 && it->bidi_it.charpos < eob)
6003 bidi_move_to_visually_next (&it->bidi_it);
6004 /* Record the stop_pos we just crossed, for when we cross it
6005 back, maybe. */
6006 if (it->bidi_it.charpos > CHARPOS (it->position))
6007 it->prev_stop = CHARPOS (it->position);
6008 /* If we ended up not where pop_it put us, resync IT's
6009 positional members with the bidi iterator. */
6010 if (it->bidi_it.charpos != CHARPOS (it->position))
6011 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6012 if (buffer_p)
6013 it->current.pos = it->position;
6014 else
6015 it->current.string_pos = it->position;
6016 }
6017
6018 /* Restore IT's settings from IT->stack. Called, for example, when no
6019 more overlay strings must be processed, and we return to delivering
6020 display elements from a buffer, or when the end of a string from a
6021 `display' property is reached and we return to delivering display
6022 elements from an overlay string, or from a buffer. */
6023
6024 static void
6025 pop_it (struct it *it)
6026 {
6027 struct iterator_stack_entry *p;
6028 bool from_display_prop = it->from_disp_prop_p;
6029 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6030
6031 eassert (it->sp > 0);
6032 --it->sp;
6033 p = it->stack + it->sp;
6034 it->stop_charpos = p->stop_charpos;
6035 it->prev_stop = p->prev_stop;
6036 it->base_level_stop = p->base_level_stop;
6037 it->cmp_it = p->cmp_it;
6038 it->face_id = p->face_id;
6039 it->current = p->current;
6040 it->position = p->position;
6041 it->string = p->string;
6042 it->from_overlay = p->from_overlay;
6043 if (NILP (it->string))
6044 SET_TEXT_POS (it->current.string_pos, -1, -1);
6045 it->method = p->method;
6046 switch (it->method)
6047 {
6048 case GET_FROM_IMAGE:
6049 it->image_id = p->u.image.image_id;
6050 it->object = p->u.image.object;
6051 it->slice = p->u.image.slice;
6052 break;
6053 case GET_FROM_STRETCH:
6054 it->object = p->u.stretch.object;
6055 break;
6056 case GET_FROM_BUFFER:
6057 it->object = it->w->contents;
6058 break;
6059 case GET_FROM_STRING:
6060 {
6061 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6062
6063 /* Restore the face_box_p flag, since it could have been
6064 overwritten by the face of the object that we just finished
6065 displaying. */
6066 if (face)
6067 it->face_box_p = face->box != FACE_NO_BOX;
6068 it->object = it->string;
6069 }
6070 break;
6071 case GET_FROM_DISPLAY_VECTOR:
6072 if (it->s)
6073 it->method = GET_FROM_C_STRING;
6074 else if (STRINGP (it->string))
6075 it->method = GET_FROM_STRING;
6076 else
6077 {
6078 it->method = GET_FROM_BUFFER;
6079 it->object = it->w->contents;
6080 }
6081 break;
6082 case GET_FROM_C_STRING:
6083 break;
6084 default:
6085 emacs_abort ();
6086 }
6087 it->end_charpos = p->end_charpos;
6088 it->string_nchars = p->string_nchars;
6089 it->area = p->area;
6090 it->multibyte_p = p->multibyte_p;
6091 it->avoid_cursor_p = p->avoid_cursor_p;
6092 it->space_width = p->space_width;
6093 it->font_height = p->font_height;
6094 it->voffset = p->voffset;
6095 it->string_from_display_prop_p = p->string_from_display_prop_p;
6096 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6097 it->line_wrap = p->line_wrap;
6098 it->bidi_p = p->bidi_p;
6099 it->paragraph_embedding = p->paragraph_embedding;
6100 it->from_disp_prop_p = p->from_disp_prop_p;
6101 if (it->bidi_p)
6102 {
6103 bidi_pop_it (&it->bidi_it);
6104 /* Bidi-iterate until we get out of the portion of text, if any,
6105 covered by a `display' text property or by an overlay with
6106 `display' property. (We cannot just jump there, because the
6107 internal coherency of the bidi iterator state can not be
6108 preserved across such jumps.) We also must determine the
6109 paragraph base direction if the overlay we just processed is
6110 at the beginning of a new paragraph. */
6111 if (from_display_prop
6112 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6113 iterate_out_of_display_property (it);
6114
6115 eassert ((BUFFERP (it->object)
6116 && IT_CHARPOS (*it) == it->bidi_it.charpos
6117 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6118 || (STRINGP (it->object)
6119 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6120 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6121 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6122 }
6123 /* If we move the iterator over text covered by a display property
6124 to a new buffer position, any info about previously seen overlays
6125 is no longer valid. */
6126 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6127 it->ignore_overlay_strings_at_pos_p = false;
6128 }
6129
6130
6131 \f
6132 /***********************************************************************
6133 Moving over lines
6134 ***********************************************************************/
6135
6136 /* Set IT's current position to the previous line start. */
6137
6138 static void
6139 back_to_previous_line_start (struct it *it)
6140 {
6141 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6142
6143 DEC_BOTH (cp, bp);
6144 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6145 }
6146
6147
6148 /* Move IT to the next line start.
6149
6150 Value is true if a newline was found. Set *SKIPPED_P to true if
6151 we skipped over part of the text (as opposed to moving the iterator
6152 continuously over the text). Otherwise, don't change the value
6153 of *SKIPPED_P.
6154
6155 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6156 iterator on the newline, if it was found.
6157
6158 Newlines may come from buffer text, overlay strings, or strings
6159 displayed via the `display' property. That's the reason we can't
6160 simply use find_newline_no_quit.
6161
6162 Note that this function may not skip over invisible text that is so
6163 because of text properties and immediately follows a newline. If
6164 it would, function reseat_at_next_visible_line_start, when called
6165 from set_iterator_to_next, would effectively make invisible
6166 characters following a newline part of the wrong glyph row, which
6167 leads to wrong cursor motion. */
6168
6169 static bool
6170 forward_to_next_line_start (struct it *it, bool *skipped_p,
6171 struct bidi_it *bidi_it_prev)
6172 {
6173 ptrdiff_t old_selective;
6174 bool newline_found_p = false;
6175 int n;
6176 const int MAX_NEWLINE_DISTANCE = 500;
6177
6178 /* If already on a newline, just consume it to avoid unintended
6179 skipping over invisible text below. */
6180 if (it->what == IT_CHARACTER
6181 && it->c == '\n'
6182 && CHARPOS (it->position) == IT_CHARPOS (*it))
6183 {
6184 if (it->bidi_p && bidi_it_prev)
6185 *bidi_it_prev = it->bidi_it;
6186 set_iterator_to_next (it, false);
6187 it->c = 0;
6188 return true;
6189 }
6190
6191 /* Don't handle selective display in the following. It's (a)
6192 unnecessary because it's done by the caller, and (b) leads to an
6193 infinite recursion because next_element_from_ellipsis indirectly
6194 calls this function. */
6195 old_selective = it->selective;
6196 it->selective = 0;
6197
6198 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6199 from buffer text. */
6200 for (n = 0;
6201 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6202 n += !STRINGP (it->string))
6203 {
6204 if (!get_next_display_element (it))
6205 return false;
6206 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6207 if (newline_found_p && it->bidi_p && bidi_it_prev)
6208 *bidi_it_prev = it->bidi_it;
6209 set_iterator_to_next (it, false);
6210 }
6211
6212 /* If we didn't find a newline near enough, see if we can use a
6213 short-cut. */
6214 if (!newline_found_p)
6215 {
6216 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6217 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6218 1, &bytepos);
6219 Lisp_Object pos;
6220
6221 eassert (!STRINGP (it->string));
6222
6223 /* If there isn't any `display' property in sight, and no
6224 overlays, we can just use the position of the newline in
6225 buffer text. */
6226 if (it->stop_charpos >= limit
6227 || ((pos = Fnext_single_property_change (make_number (start),
6228 Qdisplay, Qnil,
6229 make_number (limit)),
6230 NILP (pos))
6231 && next_overlay_change (start) == ZV))
6232 {
6233 if (!it->bidi_p)
6234 {
6235 IT_CHARPOS (*it) = limit;
6236 IT_BYTEPOS (*it) = bytepos;
6237 }
6238 else
6239 {
6240 struct bidi_it bprev;
6241
6242 /* Help bidi.c avoid expensive searches for display
6243 properties and overlays, by telling it that there are
6244 none up to `limit'. */
6245 if (it->bidi_it.disp_pos < limit)
6246 {
6247 it->bidi_it.disp_pos = limit;
6248 it->bidi_it.disp_prop = 0;
6249 }
6250 do {
6251 bprev = it->bidi_it;
6252 bidi_move_to_visually_next (&it->bidi_it);
6253 } while (it->bidi_it.charpos != limit);
6254 IT_CHARPOS (*it) = limit;
6255 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6256 if (bidi_it_prev)
6257 *bidi_it_prev = bprev;
6258 }
6259 *skipped_p = newline_found_p = true;
6260 }
6261 else
6262 {
6263 while (get_next_display_element (it)
6264 && !newline_found_p)
6265 {
6266 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6267 if (newline_found_p && it->bidi_p && bidi_it_prev)
6268 *bidi_it_prev = it->bidi_it;
6269 set_iterator_to_next (it, false);
6270 }
6271 }
6272 }
6273
6274 it->selective = old_selective;
6275 return newline_found_p;
6276 }
6277
6278
6279 /* Set IT's current position to the previous visible line start. Skip
6280 invisible text that is so either due to text properties or due to
6281 selective display. Caution: this does not change IT->current_x and
6282 IT->hpos. */
6283
6284 static void
6285 back_to_previous_visible_line_start (struct it *it)
6286 {
6287 while (IT_CHARPOS (*it) > BEGV)
6288 {
6289 back_to_previous_line_start (it);
6290
6291 if (IT_CHARPOS (*it) <= BEGV)
6292 break;
6293
6294 /* If selective > 0, then lines indented more than its value are
6295 invisible. */
6296 if (it->selective > 0
6297 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6298 it->selective))
6299 continue;
6300
6301 /* Check the newline before point for invisibility. */
6302 {
6303 Lisp_Object prop;
6304 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6305 Qinvisible, it->window);
6306 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6307 continue;
6308 }
6309
6310 if (IT_CHARPOS (*it) <= BEGV)
6311 break;
6312
6313 {
6314 struct it it2;
6315 void *it2data = NULL;
6316 ptrdiff_t pos;
6317 ptrdiff_t beg, end;
6318 Lisp_Object val, overlay;
6319
6320 SAVE_IT (it2, *it, it2data);
6321
6322 /* If newline is part of a composition, continue from start of composition */
6323 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6324 && beg < IT_CHARPOS (*it))
6325 goto replaced;
6326
6327 /* If newline is replaced by a display property, find start of overlay
6328 or interval and continue search from that point. */
6329 pos = --IT_CHARPOS (it2);
6330 --IT_BYTEPOS (it2);
6331 it2.sp = 0;
6332 bidi_unshelve_cache (NULL, false);
6333 it2.string_from_display_prop_p = false;
6334 it2.from_disp_prop_p = false;
6335 if (handle_display_prop (&it2) == HANDLED_RETURN
6336 && !NILP (val = get_char_property_and_overlay
6337 (make_number (pos), Qdisplay, Qnil, &overlay))
6338 && (OVERLAYP (overlay)
6339 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6340 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6341 {
6342 RESTORE_IT (it, it, it2data);
6343 goto replaced;
6344 }
6345
6346 /* Newline is not replaced by anything -- so we are done. */
6347 RESTORE_IT (it, it, it2data);
6348 break;
6349
6350 replaced:
6351 if (beg < BEGV)
6352 beg = BEGV;
6353 IT_CHARPOS (*it) = beg;
6354 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6355 }
6356 }
6357
6358 it->continuation_lines_width = 0;
6359
6360 eassert (IT_CHARPOS (*it) >= BEGV);
6361 eassert (IT_CHARPOS (*it) == BEGV
6362 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6363 CHECK_IT (it);
6364 }
6365
6366
6367 /* Reseat iterator IT at the previous visible line start. Skip
6368 invisible text that is so either due to text properties or due to
6369 selective display. At the end, update IT's overlay information,
6370 face information etc. */
6371
6372 void
6373 reseat_at_previous_visible_line_start (struct it *it)
6374 {
6375 back_to_previous_visible_line_start (it);
6376 reseat (it, it->current.pos, true);
6377 CHECK_IT (it);
6378 }
6379
6380
6381 /* Reseat iterator IT on the next visible line start in the current
6382 buffer. ON_NEWLINE_P means position IT on the newline
6383 preceding the line start. Skip over invisible text that is so
6384 because of selective display. Compute faces, overlays etc at the
6385 new position. Note that this function does not skip over text that
6386 is invisible because of text properties. */
6387
6388 static void
6389 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6390 {
6391 bool skipped_p = false;
6392 struct bidi_it bidi_it_prev;
6393 bool newline_found_p
6394 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6395
6396 /* Skip over lines that are invisible because they are indented
6397 more than the value of IT->selective. */
6398 if (it->selective > 0)
6399 while (IT_CHARPOS (*it) < ZV
6400 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6401 it->selective))
6402 {
6403 eassert (IT_BYTEPOS (*it) == BEGV
6404 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6405 newline_found_p =
6406 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6407 }
6408
6409 /* Position on the newline if that's what's requested. */
6410 if (on_newline_p && newline_found_p)
6411 {
6412 if (STRINGP (it->string))
6413 {
6414 if (IT_STRING_CHARPOS (*it) > 0)
6415 {
6416 if (!it->bidi_p)
6417 {
6418 --IT_STRING_CHARPOS (*it);
6419 --IT_STRING_BYTEPOS (*it);
6420 }
6421 else
6422 {
6423 /* We need to restore the bidi iterator to the state
6424 it had on the newline, and resync the IT's
6425 position with that. */
6426 it->bidi_it = bidi_it_prev;
6427 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6428 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6429 }
6430 }
6431 }
6432 else if (IT_CHARPOS (*it) > BEGV)
6433 {
6434 if (!it->bidi_p)
6435 {
6436 --IT_CHARPOS (*it);
6437 --IT_BYTEPOS (*it);
6438 }
6439 else
6440 {
6441 /* We need to restore the bidi iterator to the state it
6442 had on the newline and resync IT with that. */
6443 it->bidi_it = bidi_it_prev;
6444 IT_CHARPOS (*it) = it->bidi_it.charpos;
6445 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6446 }
6447 reseat (it, it->current.pos, false);
6448 }
6449 }
6450 else if (skipped_p)
6451 reseat (it, it->current.pos, false);
6452
6453 CHECK_IT (it);
6454 }
6455
6456
6457 \f
6458 /***********************************************************************
6459 Changing an iterator's position
6460 ***********************************************************************/
6461
6462 /* Change IT's current position to POS in current_buffer.
6463 If FORCE_P, always check for text properties at the new position.
6464 Otherwise, text properties are only looked up if POS >=
6465 IT->check_charpos of a property. */
6466
6467 static void
6468 reseat (struct it *it, struct text_pos pos, bool force_p)
6469 {
6470 ptrdiff_t original_pos = IT_CHARPOS (*it);
6471
6472 reseat_1 (it, pos, false);
6473
6474 /* Determine where to check text properties. Avoid doing it
6475 where possible because text property lookup is very expensive. */
6476 if (force_p
6477 || CHARPOS (pos) > it->stop_charpos
6478 || CHARPOS (pos) < original_pos)
6479 {
6480 if (it->bidi_p)
6481 {
6482 /* For bidi iteration, we need to prime prev_stop and
6483 base_level_stop with our best estimations. */
6484 /* Implementation note: Of course, POS is not necessarily a
6485 stop position, so assigning prev_pos to it is a lie; we
6486 should have called compute_stop_backwards. However, if
6487 the current buffer does not include any R2L characters,
6488 that call would be a waste of cycles, because the
6489 iterator will never move back, and thus never cross this
6490 "fake" stop position. So we delay that backward search
6491 until the time we really need it, in next_element_from_buffer. */
6492 if (CHARPOS (pos) != it->prev_stop)
6493 it->prev_stop = CHARPOS (pos);
6494 if (CHARPOS (pos) < it->base_level_stop)
6495 it->base_level_stop = 0; /* meaning it's unknown */
6496 handle_stop (it);
6497 }
6498 else
6499 {
6500 handle_stop (it);
6501 it->prev_stop = it->base_level_stop = 0;
6502 }
6503
6504 }
6505
6506 CHECK_IT (it);
6507 }
6508
6509
6510 /* Change IT's buffer position to POS. SET_STOP_P means set
6511 IT->stop_pos to POS, also. */
6512
6513 static void
6514 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6515 {
6516 /* Don't call this function when scanning a C string. */
6517 eassert (it->s == NULL);
6518
6519 /* POS must be a reasonable value. */
6520 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6521
6522 it->current.pos = it->position = pos;
6523 it->end_charpos = ZV;
6524 it->dpvec = NULL;
6525 it->current.dpvec_index = -1;
6526 it->current.overlay_string_index = -1;
6527 IT_STRING_CHARPOS (*it) = -1;
6528 IT_STRING_BYTEPOS (*it) = -1;
6529 it->string = Qnil;
6530 it->method = GET_FROM_BUFFER;
6531 it->object = it->w->contents;
6532 it->area = TEXT_AREA;
6533 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6534 it->sp = 0;
6535 it->string_from_display_prop_p = false;
6536 it->string_from_prefix_prop_p = false;
6537
6538 it->from_disp_prop_p = false;
6539 it->face_before_selective_p = false;
6540 if (it->bidi_p)
6541 {
6542 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6543 &it->bidi_it);
6544 bidi_unshelve_cache (NULL, false);
6545 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6546 it->bidi_it.string.s = NULL;
6547 it->bidi_it.string.lstring = Qnil;
6548 it->bidi_it.string.bufpos = 0;
6549 it->bidi_it.string.from_disp_str = false;
6550 it->bidi_it.string.unibyte = false;
6551 it->bidi_it.w = it->w;
6552 }
6553
6554 if (set_stop_p)
6555 {
6556 it->stop_charpos = CHARPOS (pos);
6557 it->base_level_stop = CHARPOS (pos);
6558 }
6559 /* This make the information stored in it->cmp_it invalidate. */
6560 it->cmp_it.id = -1;
6561 }
6562
6563
6564 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6565 If S is non-null, it is a C string to iterate over. Otherwise,
6566 STRING gives a Lisp string to iterate over.
6567
6568 If PRECISION > 0, don't return more then PRECISION number of
6569 characters from the string.
6570
6571 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6572 characters have been returned. FIELD_WIDTH < 0 means an infinite
6573 field width.
6574
6575 MULTIBYTE = 0 means disable processing of multibyte characters,
6576 MULTIBYTE > 0 means enable it,
6577 MULTIBYTE < 0 means use IT->multibyte_p.
6578
6579 IT must be initialized via a prior call to init_iterator before
6580 calling this function. */
6581
6582 static void
6583 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6584 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6585 int multibyte)
6586 {
6587 /* No text property checks performed by default, but see below. */
6588 it->stop_charpos = -1;
6589
6590 /* Set iterator position and end position. */
6591 memset (&it->current, 0, sizeof it->current);
6592 it->current.overlay_string_index = -1;
6593 it->current.dpvec_index = -1;
6594 eassert (charpos >= 0);
6595
6596 /* If STRING is specified, use its multibyteness, otherwise use the
6597 setting of MULTIBYTE, if specified. */
6598 if (multibyte >= 0)
6599 it->multibyte_p = multibyte > 0;
6600
6601 /* Bidirectional reordering of strings is controlled by the default
6602 value of bidi-display-reordering. Don't try to reorder while
6603 loading loadup.el, as the necessary character property tables are
6604 not yet available. */
6605 it->bidi_p =
6606 NILP (Vpurify_flag)
6607 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6608
6609 if (s == NULL)
6610 {
6611 eassert (STRINGP (string));
6612 it->string = string;
6613 it->s = NULL;
6614 it->end_charpos = it->string_nchars = SCHARS (string);
6615 it->method = GET_FROM_STRING;
6616 it->current.string_pos = string_pos (charpos, string);
6617
6618 if (it->bidi_p)
6619 {
6620 it->bidi_it.string.lstring = string;
6621 it->bidi_it.string.s = NULL;
6622 it->bidi_it.string.schars = it->end_charpos;
6623 it->bidi_it.string.bufpos = 0;
6624 it->bidi_it.string.from_disp_str = false;
6625 it->bidi_it.string.unibyte = !it->multibyte_p;
6626 it->bidi_it.w = it->w;
6627 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6628 FRAME_WINDOW_P (it->f), &it->bidi_it);
6629 }
6630 }
6631 else
6632 {
6633 it->s = (const unsigned char *) s;
6634 it->string = Qnil;
6635
6636 /* Note that we use IT->current.pos, not it->current.string_pos,
6637 for displaying C strings. */
6638 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6639 if (it->multibyte_p)
6640 {
6641 it->current.pos = c_string_pos (charpos, s, true);
6642 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6643 }
6644 else
6645 {
6646 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6647 it->end_charpos = it->string_nchars = strlen (s);
6648 }
6649
6650 if (it->bidi_p)
6651 {
6652 it->bidi_it.string.lstring = Qnil;
6653 it->bidi_it.string.s = (const unsigned char *) s;
6654 it->bidi_it.string.schars = it->end_charpos;
6655 it->bidi_it.string.bufpos = 0;
6656 it->bidi_it.string.from_disp_str = false;
6657 it->bidi_it.string.unibyte = !it->multibyte_p;
6658 it->bidi_it.w = it->w;
6659 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6660 &it->bidi_it);
6661 }
6662 it->method = GET_FROM_C_STRING;
6663 }
6664
6665 /* PRECISION > 0 means don't return more than PRECISION characters
6666 from the string. */
6667 if (precision > 0 && it->end_charpos - charpos > precision)
6668 {
6669 it->end_charpos = it->string_nchars = charpos + precision;
6670 if (it->bidi_p)
6671 it->bidi_it.string.schars = it->end_charpos;
6672 }
6673
6674 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6675 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6676 FIELD_WIDTH < 0 means infinite field width. This is useful for
6677 padding with `-' at the end of a mode line. */
6678 if (field_width < 0)
6679 field_width = INFINITY;
6680 /* Implementation note: We deliberately don't enlarge
6681 it->bidi_it.string.schars here to fit it->end_charpos, because
6682 the bidi iterator cannot produce characters out of thin air. */
6683 if (field_width > it->end_charpos - charpos)
6684 it->end_charpos = charpos + field_width;
6685
6686 /* Use the standard display table for displaying strings. */
6687 if (DISP_TABLE_P (Vstandard_display_table))
6688 it->dp = XCHAR_TABLE (Vstandard_display_table);
6689
6690 it->stop_charpos = charpos;
6691 it->prev_stop = charpos;
6692 it->base_level_stop = 0;
6693 if (it->bidi_p)
6694 {
6695 it->bidi_it.first_elt = true;
6696 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6697 it->bidi_it.disp_pos = -1;
6698 }
6699 if (s == NULL && it->multibyte_p)
6700 {
6701 ptrdiff_t endpos = SCHARS (it->string);
6702 if (endpos > it->end_charpos)
6703 endpos = it->end_charpos;
6704 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6705 it->string);
6706 }
6707 CHECK_IT (it);
6708 }
6709
6710
6711 \f
6712 /***********************************************************************
6713 Iteration
6714 ***********************************************************************/
6715
6716 /* Map enum it_method value to corresponding next_element_from_* function. */
6717
6718 typedef bool (*next_element_function) (struct it *);
6719
6720 static next_element_function const get_next_element[NUM_IT_METHODS] =
6721 {
6722 next_element_from_buffer,
6723 next_element_from_display_vector,
6724 next_element_from_string,
6725 next_element_from_c_string,
6726 next_element_from_image,
6727 next_element_from_stretch
6728 };
6729
6730 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6731
6732
6733 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6734 (possibly with the following characters). */
6735
6736 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6737 ((IT)->cmp_it.id >= 0 \
6738 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6739 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6740 END_CHARPOS, (IT)->w, \
6741 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6742 (IT)->string)))
6743
6744
6745 /* Lookup the char-table Vglyphless_char_display for character C (-1
6746 if we want information for no-font case), and return the display
6747 method symbol. By side-effect, update it->what and
6748 it->glyphless_method. This function is called from
6749 get_next_display_element for each character element, and from
6750 x_produce_glyphs when no suitable font was found. */
6751
6752 Lisp_Object
6753 lookup_glyphless_char_display (int c, struct it *it)
6754 {
6755 Lisp_Object glyphless_method = Qnil;
6756
6757 if (CHAR_TABLE_P (Vglyphless_char_display)
6758 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6759 {
6760 if (c >= 0)
6761 {
6762 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6763 if (CONSP (glyphless_method))
6764 glyphless_method = FRAME_WINDOW_P (it->f)
6765 ? XCAR (glyphless_method)
6766 : XCDR (glyphless_method);
6767 }
6768 else
6769 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6770 }
6771
6772 retry:
6773 if (NILP (glyphless_method))
6774 {
6775 if (c >= 0)
6776 /* The default is to display the character by a proper font. */
6777 return Qnil;
6778 /* The default for the no-font case is to display an empty box. */
6779 glyphless_method = Qempty_box;
6780 }
6781 if (EQ (glyphless_method, Qzero_width))
6782 {
6783 if (c >= 0)
6784 return glyphless_method;
6785 /* This method can't be used for the no-font case. */
6786 glyphless_method = Qempty_box;
6787 }
6788 if (EQ (glyphless_method, Qthin_space))
6789 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6790 else if (EQ (glyphless_method, Qempty_box))
6791 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6792 else if (EQ (glyphless_method, Qhex_code))
6793 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6794 else if (STRINGP (glyphless_method))
6795 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6796 else
6797 {
6798 /* Invalid value. We use the default method. */
6799 glyphless_method = Qnil;
6800 goto retry;
6801 }
6802 it->what = IT_GLYPHLESS;
6803 return glyphless_method;
6804 }
6805
6806 /* Merge escape glyph face and cache the result. */
6807
6808 static struct frame *last_escape_glyph_frame = NULL;
6809 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6810 static int last_escape_glyph_merged_face_id = 0;
6811
6812 static int
6813 merge_escape_glyph_face (struct it *it)
6814 {
6815 int face_id;
6816
6817 if (it->f == last_escape_glyph_frame
6818 && it->face_id == last_escape_glyph_face_id)
6819 face_id = last_escape_glyph_merged_face_id;
6820 else
6821 {
6822 /* Merge the `escape-glyph' face into the current face. */
6823 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6824 last_escape_glyph_frame = it->f;
6825 last_escape_glyph_face_id = it->face_id;
6826 last_escape_glyph_merged_face_id = face_id;
6827 }
6828 return face_id;
6829 }
6830
6831 /* Likewise for glyphless glyph face. */
6832
6833 static struct frame *last_glyphless_glyph_frame = NULL;
6834 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6835 static int last_glyphless_glyph_merged_face_id = 0;
6836
6837 int
6838 merge_glyphless_glyph_face (struct it *it)
6839 {
6840 int face_id;
6841
6842 if (it->f == last_glyphless_glyph_frame
6843 && it->face_id == last_glyphless_glyph_face_id)
6844 face_id = last_glyphless_glyph_merged_face_id;
6845 else
6846 {
6847 /* Merge the `glyphless-char' face into the current face. */
6848 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6849 last_glyphless_glyph_frame = it->f;
6850 last_glyphless_glyph_face_id = it->face_id;
6851 last_glyphless_glyph_merged_face_id = face_id;
6852 }
6853 return face_id;
6854 }
6855
6856 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6857 be called before redisplaying windows, and when the frame's face
6858 cache is freed. */
6859 void
6860 forget_escape_and_glyphless_faces (void)
6861 {
6862 last_escape_glyph_frame = NULL;
6863 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6864 last_glyphless_glyph_frame = NULL;
6865 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6866 }
6867
6868 /* Load IT's display element fields with information about the next
6869 display element from the current position of IT. Value is false if
6870 end of buffer (or C string) is reached. */
6871
6872 static bool
6873 get_next_display_element (struct it *it)
6874 {
6875 /* True means that we found a display element. False means that
6876 we hit the end of what we iterate over. Performance note: the
6877 function pointer `method' used here turns out to be faster than
6878 using a sequence of if-statements. */
6879 bool success_p;
6880
6881 get_next:
6882 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6883
6884 if (it->what == IT_CHARACTER)
6885 {
6886 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6887 and only if (a) the resolved directionality of that character
6888 is R..." */
6889 /* FIXME: Do we need an exception for characters from display
6890 tables? */
6891 if (it->bidi_p && it->bidi_it.type == STRONG_R
6892 && !inhibit_bidi_mirroring)
6893 it->c = bidi_mirror_char (it->c);
6894 /* Map via display table or translate control characters.
6895 IT->c, IT->len etc. have been set to the next character by
6896 the function call above. If we have a display table, and it
6897 contains an entry for IT->c, translate it. Don't do this if
6898 IT->c itself comes from a display table, otherwise we could
6899 end up in an infinite recursion. (An alternative could be to
6900 count the recursion depth of this function and signal an
6901 error when a certain maximum depth is reached.) Is it worth
6902 it? */
6903 if (success_p && it->dpvec == NULL)
6904 {
6905 Lisp_Object dv;
6906 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6907 bool nonascii_space_p = false;
6908 bool nonascii_hyphen_p = false;
6909 int c = it->c; /* This is the character to display. */
6910
6911 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6912 {
6913 eassert (SINGLE_BYTE_CHAR_P (c));
6914 if (unibyte_display_via_language_environment)
6915 {
6916 c = DECODE_CHAR (unibyte, c);
6917 if (c < 0)
6918 c = BYTE8_TO_CHAR (it->c);
6919 }
6920 else
6921 c = BYTE8_TO_CHAR (it->c);
6922 }
6923
6924 if (it->dp
6925 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6926 VECTORP (dv)))
6927 {
6928 struct Lisp_Vector *v = XVECTOR (dv);
6929
6930 /* Return the first character from the display table
6931 entry, if not empty. If empty, don't display the
6932 current character. */
6933 if (v->header.size)
6934 {
6935 it->dpvec_char_len = it->len;
6936 it->dpvec = v->contents;
6937 it->dpend = v->contents + v->header.size;
6938 it->current.dpvec_index = 0;
6939 it->dpvec_face_id = -1;
6940 it->saved_face_id = it->face_id;
6941 it->method = GET_FROM_DISPLAY_VECTOR;
6942 it->ellipsis_p = false;
6943 }
6944 else
6945 {
6946 set_iterator_to_next (it, false);
6947 }
6948 goto get_next;
6949 }
6950
6951 if (! NILP (lookup_glyphless_char_display (c, it)))
6952 {
6953 if (it->what == IT_GLYPHLESS)
6954 goto done;
6955 /* Don't display this character. */
6956 set_iterator_to_next (it, false);
6957 goto get_next;
6958 }
6959
6960 /* If `nobreak-char-display' is non-nil, we display
6961 non-ASCII spaces and hyphens specially. */
6962 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6963 {
6964 if (c == NO_BREAK_SPACE)
6965 nonascii_space_p = true;
6966 else if (c == SOFT_HYPHEN || c == HYPHEN
6967 || c == NON_BREAKING_HYPHEN)
6968 nonascii_hyphen_p = true;
6969 }
6970
6971 /* Translate control characters into `\003' or `^C' form.
6972 Control characters coming from a display table entry are
6973 currently not translated because we use IT->dpvec to hold
6974 the translation. This could easily be changed but I
6975 don't believe that it is worth doing.
6976
6977 The characters handled by `nobreak-char-display' must be
6978 translated too.
6979
6980 Non-printable characters and raw-byte characters are also
6981 translated to octal form. */
6982 if (((c < ' ' || c == 127) /* ASCII control chars. */
6983 ? (it->area != TEXT_AREA
6984 /* In mode line, treat \n, \t like other crl chars. */
6985 || (c != '\t'
6986 && it->glyph_row
6987 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6988 || (c != '\n' && c != '\t'))
6989 : (nonascii_space_p
6990 || nonascii_hyphen_p
6991 || CHAR_BYTE8_P (c)
6992 || ! CHAR_PRINTABLE_P (c))))
6993 {
6994 /* C is a control character, non-ASCII space/hyphen,
6995 raw-byte, or a non-printable character which must be
6996 displayed either as '\003' or as `^C' where the '\\'
6997 and '^' can be defined in the display table. Fill
6998 IT->ctl_chars with glyphs for what we have to
6999 display. Then, set IT->dpvec to these glyphs. */
7000 Lisp_Object gc;
7001 int ctl_len;
7002 int face_id;
7003 int lface_id = 0;
7004 int escape_glyph;
7005
7006 /* Handle control characters with ^. */
7007
7008 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7009 {
7010 int g;
7011
7012 g = '^'; /* default glyph for Control */
7013 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7014 if (it->dp
7015 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7016 {
7017 g = GLYPH_CODE_CHAR (gc);
7018 lface_id = GLYPH_CODE_FACE (gc);
7019 }
7020
7021 face_id = (lface_id
7022 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7023 : merge_escape_glyph_face (it));
7024
7025 XSETINT (it->ctl_chars[0], g);
7026 XSETINT (it->ctl_chars[1], c ^ 0100);
7027 ctl_len = 2;
7028 goto display_control;
7029 }
7030
7031 /* Handle non-ascii space in the mode where it only gets
7032 highlighting. */
7033
7034 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7035 {
7036 /* Merge `nobreak-space' into the current face. */
7037 face_id = merge_faces (it->f, Qnobreak_space, 0,
7038 it->face_id);
7039 XSETINT (it->ctl_chars[0], ' ');
7040 ctl_len = 1;
7041 goto display_control;
7042 }
7043
7044 /* Handle sequences that start with the "escape glyph". */
7045
7046 /* the default escape glyph is \. */
7047 escape_glyph = '\\';
7048
7049 if (it->dp
7050 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7051 {
7052 escape_glyph = GLYPH_CODE_CHAR (gc);
7053 lface_id = GLYPH_CODE_FACE (gc);
7054 }
7055
7056 face_id = (lface_id
7057 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7058 : merge_escape_glyph_face (it));
7059
7060 /* Draw non-ASCII hyphen with just highlighting: */
7061
7062 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7063 {
7064 XSETINT (it->ctl_chars[0], '-');
7065 ctl_len = 1;
7066 goto display_control;
7067 }
7068
7069 /* Draw non-ASCII space/hyphen with escape glyph: */
7070
7071 if (nonascii_space_p || nonascii_hyphen_p)
7072 {
7073 XSETINT (it->ctl_chars[0], escape_glyph);
7074 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7075 ctl_len = 2;
7076 goto display_control;
7077 }
7078
7079 {
7080 char str[10];
7081 int len, i;
7082
7083 if (CHAR_BYTE8_P (c))
7084 /* Display \200 instead of \17777600. */
7085 c = CHAR_TO_BYTE8 (c);
7086 len = sprintf (str, "%03o", c + 0u);
7087
7088 XSETINT (it->ctl_chars[0], escape_glyph);
7089 for (i = 0; i < len; i++)
7090 XSETINT (it->ctl_chars[i + 1], str[i]);
7091 ctl_len = len + 1;
7092 }
7093
7094 display_control:
7095 /* Set up IT->dpvec and return first character from it. */
7096 it->dpvec_char_len = it->len;
7097 it->dpvec = it->ctl_chars;
7098 it->dpend = it->dpvec + ctl_len;
7099 it->current.dpvec_index = 0;
7100 it->dpvec_face_id = face_id;
7101 it->saved_face_id = it->face_id;
7102 it->method = GET_FROM_DISPLAY_VECTOR;
7103 it->ellipsis_p = false;
7104 goto get_next;
7105 }
7106 it->char_to_display = c;
7107 }
7108 else if (success_p)
7109 {
7110 it->char_to_display = it->c;
7111 }
7112 }
7113
7114 #ifdef HAVE_WINDOW_SYSTEM
7115 /* Adjust face id for a multibyte character. There are no multibyte
7116 character in unibyte text. */
7117 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7118 && it->multibyte_p
7119 && success_p
7120 && FRAME_WINDOW_P (it->f))
7121 {
7122 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7123
7124 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7125 {
7126 /* Automatic composition with glyph-string. */
7127 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7128
7129 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7130 }
7131 else
7132 {
7133 ptrdiff_t pos = (it->s ? -1
7134 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7135 : IT_CHARPOS (*it));
7136 int c;
7137
7138 if (it->what == IT_CHARACTER)
7139 c = it->char_to_display;
7140 else
7141 {
7142 struct composition *cmp = composition_table[it->cmp_it.id];
7143 int i;
7144
7145 c = ' ';
7146 for (i = 0; i < cmp->glyph_len; i++)
7147 /* TAB in a composition means display glyphs with
7148 padding space on the left or right. */
7149 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7150 break;
7151 }
7152 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7153 }
7154 }
7155 #endif /* HAVE_WINDOW_SYSTEM */
7156
7157 done:
7158 /* Is this character the last one of a run of characters with
7159 box? If yes, set IT->end_of_box_run_p to true. */
7160 if (it->face_box_p
7161 && it->s == NULL)
7162 {
7163 if (it->method == GET_FROM_STRING && it->sp)
7164 {
7165 int face_id = underlying_face_id (it);
7166 struct face *face = FACE_FROM_ID (it->f, face_id);
7167
7168 if (face)
7169 {
7170 if (face->box == FACE_NO_BOX)
7171 {
7172 /* If the box comes from face properties in a
7173 display string, check faces in that string. */
7174 int string_face_id = face_after_it_pos (it);
7175 it->end_of_box_run_p
7176 = (FACE_FROM_ID (it->f, string_face_id)->box
7177 == FACE_NO_BOX);
7178 }
7179 /* Otherwise, the box comes from the underlying face.
7180 If this is the last string character displayed, check
7181 the next buffer location. */
7182 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7183 /* n_overlay_strings is unreliable unless
7184 overlay_string_index is non-negative. */
7185 && ((it->current.overlay_string_index >= 0
7186 && (it->current.overlay_string_index
7187 == it->n_overlay_strings - 1))
7188 /* A string from display property. */
7189 || it->from_disp_prop_p))
7190 {
7191 ptrdiff_t ignore;
7192 int next_face_id;
7193 struct text_pos pos = it->current.pos;
7194
7195 /* For a string from a display property, the next
7196 buffer position is stored in the 'position'
7197 member of the iteration stack slot below the
7198 current one, see handle_single_display_spec. By
7199 contrast, it->current.pos was is not yet updated
7200 to point to that buffer position; that will
7201 happen in pop_it, after we finish displaying the
7202 current string. Note that we already checked
7203 above that it->sp is positive, so subtracting one
7204 from it is safe. */
7205 if (it->from_disp_prop_p)
7206 pos = (it->stack + it->sp - 1)->position;
7207 else
7208 INC_TEXT_POS (pos, it->multibyte_p);
7209
7210 if (CHARPOS (pos) >= ZV)
7211 it->end_of_box_run_p = true;
7212 else
7213 {
7214 next_face_id = face_at_buffer_position
7215 (it->w, CHARPOS (pos), &ignore,
7216 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, false, -1);
7217 it->end_of_box_run_p
7218 = (FACE_FROM_ID (it->f, next_face_id)->box
7219 == FACE_NO_BOX);
7220 }
7221 }
7222 }
7223 }
7224 /* next_element_from_display_vector sets this flag according to
7225 faces of the display vector glyphs, see there. */
7226 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7227 {
7228 int face_id = face_after_it_pos (it);
7229 it->end_of_box_run_p
7230 = (face_id != it->face_id
7231 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7232 }
7233 }
7234 /* If we reached the end of the object we've been iterating (e.g., a
7235 display string or an overlay string), and there's something on
7236 IT->stack, proceed with what's on the stack. It doesn't make
7237 sense to return false if there's unprocessed stuff on the stack,
7238 because otherwise that stuff will never be displayed. */
7239 if (!success_p && it->sp > 0)
7240 {
7241 set_iterator_to_next (it, false);
7242 success_p = get_next_display_element (it);
7243 }
7244
7245 /* Value is false if end of buffer or string reached. */
7246 return success_p;
7247 }
7248
7249
7250 /* Move IT to the next display element.
7251
7252 RESEAT_P means if called on a newline in buffer text,
7253 skip to the next visible line start.
7254
7255 Functions get_next_display_element and set_iterator_to_next are
7256 separate because I find this arrangement easier to handle than a
7257 get_next_display_element function that also increments IT's
7258 position. The way it is we can first look at an iterator's current
7259 display element, decide whether it fits on a line, and if it does,
7260 increment the iterator position. The other way around we probably
7261 would either need a flag indicating whether the iterator has to be
7262 incremented the next time, or we would have to implement a
7263 decrement position function which would not be easy to write. */
7264
7265 void
7266 set_iterator_to_next (struct it *it, bool reseat_p)
7267 {
7268 /* Reset flags indicating start and end of a sequence of characters
7269 with box. Reset them at the start of this function because
7270 moving the iterator to a new position might set them. */
7271 it->start_of_box_run_p = it->end_of_box_run_p = false;
7272
7273 switch (it->method)
7274 {
7275 case GET_FROM_BUFFER:
7276 /* The current display element of IT is a character from
7277 current_buffer. Advance in the buffer, and maybe skip over
7278 invisible lines that are so because of selective display. */
7279 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7280 reseat_at_next_visible_line_start (it, false);
7281 else if (it->cmp_it.id >= 0)
7282 {
7283 /* We are currently getting glyphs from a composition. */
7284 if (! it->bidi_p)
7285 {
7286 IT_CHARPOS (*it) += it->cmp_it.nchars;
7287 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7288 }
7289 else
7290 {
7291 int i;
7292
7293 /* Update IT's char/byte positions to point to the first
7294 character of the next grapheme cluster, or to the
7295 character visually after the current composition. */
7296 for (i = 0; i < it->cmp_it.nchars; i++)
7297 bidi_move_to_visually_next (&it->bidi_it);
7298 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7299 IT_CHARPOS (*it) = it->bidi_it.charpos;
7300 }
7301
7302 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7303 && it->cmp_it.to < it->cmp_it.nglyphs)
7304 {
7305 /* Composition created while scanning forward. Proceed
7306 to the next grapheme cluster. */
7307 it->cmp_it.from = it->cmp_it.to;
7308 }
7309 else if ((it->bidi_p && it->cmp_it.reversed_p)
7310 && it->cmp_it.from > 0)
7311 {
7312 /* Composition created while scanning backward. Proceed
7313 to the previous grapheme cluster. */
7314 it->cmp_it.to = it->cmp_it.from;
7315 }
7316 else
7317 {
7318 /* No more grapheme clusters in this composition.
7319 Find the next stop position. */
7320 ptrdiff_t stop = it->end_charpos;
7321
7322 if (it->bidi_it.scan_dir < 0)
7323 /* Now we are scanning backward and don't know
7324 where to stop. */
7325 stop = -1;
7326 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7327 IT_BYTEPOS (*it), stop, Qnil);
7328 }
7329 }
7330 else
7331 {
7332 eassert (it->len != 0);
7333
7334 if (!it->bidi_p)
7335 {
7336 IT_BYTEPOS (*it) += it->len;
7337 IT_CHARPOS (*it) += 1;
7338 }
7339 else
7340 {
7341 int prev_scan_dir = it->bidi_it.scan_dir;
7342 /* If this is a new paragraph, determine its base
7343 direction (a.k.a. its base embedding level). */
7344 if (it->bidi_it.new_paragraph)
7345 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7346 false);
7347 bidi_move_to_visually_next (&it->bidi_it);
7348 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7349 IT_CHARPOS (*it) = it->bidi_it.charpos;
7350 if (prev_scan_dir != it->bidi_it.scan_dir)
7351 {
7352 /* As the scan direction was changed, we must
7353 re-compute the stop position for composition. */
7354 ptrdiff_t stop = it->end_charpos;
7355 if (it->bidi_it.scan_dir < 0)
7356 stop = -1;
7357 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7358 IT_BYTEPOS (*it), stop, Qnil);
7359 }
7360 }
7361 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7362 }
7363 break;
7364
7365 case GET_FROM_C_STRING:
7366 /* Current display element of IT is from a C string. */
7367 if (!it->bidi_p
7368 /* If the string position is beyond string's end, it means
7369 next_element_from_c_string is padding the string with
7370 blanks, in which case we bypass the bidi iterator,
7371 because it cannot deal with such virtual characters. */
7372 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7373 {
7374 IT_BYTEPOS (*it) += it->len;
7375 IT_CHARPOS (*it) += 1;
7376 }
7377 else
7378 {
7379 bidi_move_to_visually_next (&it->bidi_it);
7380 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7381 IT_CHARPOS (*it) = it->bidi_it.charpos;
7382 }
7383 break;
7384
7385 case GET_FROM_DISPLAY_VECTOR:
7386 /* Current display element of IT is from a display table entry.
7387 Advance in the display table definition. Reset it to null if
7388 end reached, and continue with characters from buffers/
7389 strings. */
7390 ++it->current.dpvec_index;
7391
7392 /* Restore face of the iterator to what they were before the
7393 display vector entry (these entries may contain faces). */
7394 it->face_id = it->saved_face_id;
7395
7396 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7397 {
7398 bool recheck_faces = it->ellipsis_p;
7399
7400 if (it->s)
7401 it->method = GET_FROM_C_STRING;
7402 else if (STRINGP (it->string))
7403 it->method = GET_FROM_STRING;
7404 else
7405 {
7406 it->method = GET_FROM_BUFFER;
7407 it->object = it->w->contents;
7408 }
7409
7410 it->dpvec = NULL;
7411 it->current.dpvec_index = -1;
7412
7413 /* Skip over characters which were displayed via IT->dpvec. */
7414 if (it->dpvec_char_len < 0)
7415 reseat_at_next_visible_line_start (it, true);
7416 else if (it->dpvec_char_len > 0)
7417 {
7418 it->len = it->dpvec_char_len;
7419 set_iterator_to_next (it, reseat_p);
7420 }
7421
7422 /* Maybe recheck faces after display vector. */
7423 if (recheck_faces)
7424 {
7425 if (it->method == GET_FROM_STRING)
7426 it->stop_charpos = IT_STRING_CHARPOS (*it);
7427 else
7428 it->stop_charpos = IT_CHARPOS (*it);
7429 }
7430 }
7431 break;
7432
7433 case GET_FROM_STRING:
7434 /* Current display element is a character from a Lisp string. */
7435 eassert (it->s == NULL && STRINGP (it->string));
7436 /* Don't advance past string end. These conditions are true
7437 when set_iterator_to_next is called at the end of
7438 get_next_display_element, in which case the Lisp string is
7439 already exhausted, and all we want is pop the iterator
7440 stack. */
7441 if (it->current.overlay_string_index >= 0)
7442 {
7443 /* This is an overlay string, so there's no padding with
7444 spaces, and the number of characters in the string is
7445 where the string ends. */
7446 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7447 goto consider_string_end;
7448 }
7449 else
7450 {
7451 /* Not an overlay string. There could be padding, so test
7452 against it->end_charpos. */
7453 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7454 goto consider_string_end;
7455 }
7456 if (it->cmp_it.id >= 0)
7457 {
7458 /* We are delivering display elements from a composition.
7459 Update the string position past the grapheme cluster
7460 we've just processed. */
7461 if (! it->bidi_p)
7462 {
7463 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7464 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7465 }
7466 else
7467 {
7468 int i;
7469
7470 for (i = 0; i < it->cmp_it.nchars; i++)
7471 bidi_move_to_visually_next (&it->bidi_it);
7472 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7473 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7474 }
7475
7476 /* Did we exhaust all the grapheme clusters of this
7477 composition? */
7478 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7479 && (it->cmp_it.to < it->cmp_it.nglyphs))
7480 {
7481 /* Not all the grapheme clusters were processed yet;
7482 advance to the next cluster. */
7483 it->cmp_it.from = it->cmp_it.to;
7484 }
7485 else if ((it->bidi_p && it->cmp_it.reversed_p)
7486 && it->cmp_it.from > 0)
7487 {
7488 /* Likewise: advance to the next cluster, but going in
7489 the reverse direction. */
7490 it->cmp_it.to = it->cmp_it.from;
7491 }
7492 else
7493 {
7494 /* This composition was fully processed; find the next
7495 candidate place for checking for composed
7496 characters. */
7497 /* Always limit string searches to the string length;
7498 any padding spaces are not part of the string, and
7499 there cannot be any compositions in that padding. */
7500 ptrdiff_t stop = SCHARS (it->string);
7501
7502 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7503 stop = -1;
7504 else if (it->end_charpos < stop)
7505 {
7506 /* Cf. PRECISION in reseat_to_string: we might be
7507 limited in how many of the string characters we
7508 need to deliver. */
7509 stop = it->end_charpos;
7510 }
7511 composition_compute_stop_pos (&it->cmp_it,
7512 IT_STRING_CHARPOS (*it),
7513 IT_STRING_BYTEPOS (*it), stop,
7514 it->string);
7515 }
7516 }
7517 else
7518 {
7519 if (!it->bidi_p
7520 /* If the string position is beyond string's end, it
7521 means next_element_from_string is padding the string
7522 with blanks, in which case we bypass the bidi
7523 iterator, because it cannot deal with such virtual
7524 characters. */
7525 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7526 {
7527 IT_STRING_BYTEPOS (*it) += it->len;
7528 IT_STRING_CHARPOS (*it) += 1;
7529 }
7530 else
7531 {
7532 int prev_scan_dir = it->bidi_it.scan_dir;
7533
7534 bidi_move_to_visually_next (&it->bidi_it);
7535 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7536 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7537 /* If the scan direction changes, we may need to update
7538 the place where to check for composed characters. */
7539 if (prev_scan_dir != it->bidi_it.scan_dir)
7540 {
7541 ptrdiff_t stop = SCHARS (it->string);
7542
7543 if (it->bidi_it.scan_dir < 0)
7544 stop = -1;
7545 else if (it->end_charpos < stop)
7546 stop = it->end_charpos;
7547
7548 composition_compute_stop_pos (&it->cmp_it,
7549 IT_STRING_CHARPOS (*it),
7550 IT_STRING_BYTEPOS (*it), stop,
7551 it->string);
7552 }
7553 }
7554 }
7555
7556 consider_string_end:
7557
7558 if (it->current.overlay_string_index >= 0)
7559 {
7560 /* IT->string is an overlay string. Advance to the
7561 next, if there is one. */
7562 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7563 {
7564 it->ellipsis_p = false;
7565 next_overlay_string (it);
7566 if (it->ellipsis_p)
7567 setup_for_ellipsis (it, 0);
7568 }
7569 }
7570 else
7571 {
7572 /* IT->string is not an overlay string. If we reached
7573 its end, and there is something on IT->stack, proceed
7574 with what is on the stack. This can be either another
7575 string, this time an overlay string, or a buffer. */
7576 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7577 && it->sp > 0)
7578 {
7579 pop_it (it);
7580 if (it->method == GET_FROM_STRING)
7581 goto consider_string_end;
7582 }
7583 }
7584 break;
7585
7586 case GET_FROM_IMAGE:
7587 case GET_FROM_STRETCH:
7588 /* The position etc with which we have to proceed are on
7589 the stack. The position may be at the end of a string,
7590 if the `display' property takes up the whole string. */
7591 eassert (it->sp > 0);
7592 pop_it (it);
7593 if (it->method == GET_FROM_STRING)
7594 goto consider_string_end;
7595 break;
7596
7597 default:
7598 /* There are no other methods defined, so this should be a bug. */
7599 emacs_abort ();
7600 }
7601
7602 eassert (it->method != GET_FROM_STRING
7603 || (STRINGP (it->string)
7604 && IT_STRING_CHARPOS (*it) >= 0));
7605 }
7606
7607 /* Load IT's display element fields with information about the next
7608 display element which comes from a display table entry or from the
7609 result of translating a control character to one of the forms `^C'
7610 or `\003'.
7611
7612 IT->dpvec holds the glyphs to return as characters.
7613 IT->saved_face_id holds the face id before the display vector--it
7614 is restored into IT->face_id in set_iterator_to_next. */
7615
7616 static bool
7617 next_element_from_display_vector (struct it *it)
7618 {
7619 Lisp_Object gc;
7620 int prev_face_id = it->face_id;
7621 int next_face_id;
7622
7623 /* Precondition. */
7624 eassert (it->dpvec && it->current.dpvec_index >= 0);
7625
7626 it->face_id = it->saved_face_id;
7627
7628 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7629 That seemed totally bogus - so I changed it... */
7630 gc = it->dpvec[it->current.dpvec_index];
7631
7632 if (GLYPH_CODE_P (gc))
7633 {
7634 struct face *this_face, *prev_face, *next_face;
7635
7636 it->c = GLYPH_CODE_CHAR (gc);
7637 it->len = CHAR_BYTES (it->c);
7638
7639 /* The entry may contain a face id to use. Such a face id is
7640 the id of a Lisp face, not a realized face. A face id of
7641 zero means no face is specified. */
7642 if (it->dpvec_face_id >= 0)
7643 it->face_id = it->dpvec_face_id;
7644 else
7645 {
7646 int lface_id = GLYPH_CODE_FACE (gc);
7647 if (lface_id > 0)
7648 it->face_id = merge_faces (it->f, Qt, lface_id,
7649 it->saved_face_id);
7650 }
7651
7652 /* Glyphs in the display vector could have the box face, so we
7653 need to set the related flags in the iterator, as
7654 appropriate. */
7655 this_face = FACE_FROM_ID (it->f, it->face_id);
7656 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7657
7658 /* Is this character the first character of a box-face run? */
7659 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7660 && (!prev_face
7661 || prev_face->box == FACE_NO_BOX));
7662
7663 /* For the last character of the box-face run, we need to look
7664 either at the next glyph from the display vector, or at the
7665 face we saw before the display vector. */
7666 next_face_id = it->saved_face_id;
7667 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7668 {
7669 if (it->dpvec_face_id >= 0)
7670 next_face_id = it->dpvec_face_id;
7671 else
7672 {
7673 int lface_id =
7674 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7675
7676 if (lface_id > 0)
7677 next_face_id = merge_faces (it->f, Qt, lface_id,
7678 it->saved_face_id);
7679 }
7680 }
7681 next_face = FACE_FROM_ID (it->f, next_face_id);
7682 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7683 && (!next_face
7684 || next_face->box == FACE_NO_BOX));
7685 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7686 }
7687 else
7688 /* Display table entry is invalid. Return a space. */
7689 it->c = ' ', it->len = 1;
7690
7691 /* Don't change position and object of the iterator here. They are
7692 still the values of the character that had this display table
7693 entry or was translated, and that's what we want. */
7694 it->what = IT_CHARACTER;
7695 return true;
7696 }
7697
7698 /* Get the first element of string/buffer in the visual order, after
7699 being reseated to a new position in a string or a buffer. */
7700 static void
7701 get_visually_first_element (struct it *it)
7702 {
7703 bool string_p = STRINGP (it->string) || it->s;
7704 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7705 ptrdiff_t bob = (string_p ? 0 : BEGV);
7706
7707 if (STRINGP (it->string))
7708 {
7709 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7710 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7711 }
7712 else
7713 {
7714 it->bidi_it.charpos = IT_CHARPOS (*it);
7715 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7716 }
7717
7718 if (it->bidi_it.charpos == eob)
7719 {
7720 /* Nothing to do, but reset the FIRST_ELT flag, like
7721 bidi_paragraph_init does, because we are not going to
7722 call it. */
7723 it->bidi_it.first_elt = false;
7724 }
7725 else if (it->bidi_it.charpos == bob
7726 || (!string_p
7727 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7728 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7729 {
7730 /* If we are at the beginning of a line/string, we can produce
7731 the next element right away. */
7732 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7733 bidi_move_to_visually_next (&it->bidi_it);
7734 }
7735 else
7736 {
7737 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7738
7739 /* We need to prime the bidi iterator starting at the line's or
7740 string's beginning, before we will be able to produce the
7741 next element. */
7742 if (string_p)
7743 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7744 else
7745 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7746 IT_BYTEPOS (*it), -1,
7747 &it->bidi_it.bytepos);
7748 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7749 do
7750 {
7751 /* Now return to buffer/string position where we were asked
7752 to get the next display element, and produce that. */
7753 bidi_move_to_visually_next (&it->bidi_it);
7754 }
7755 while (it->bidi_it.bytepos != orig_bytepos
7756 && it->bidi_it.charpos < eob);
7757 }
7758
7759 /* Adjust IT's position information to where we ended up. */
7760 if (STRINGP (it->string))
7761 {
7762 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7763 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7764 }
7765 else
7766 {
7767 IT_CHARPOS (*it) = it->bidi_it.charpos;
7768 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7769 }
7770
7771 if (STRINGP (it->string) || !it->s)
7772 {
7773 ptrdiff_t stop, charpos, bytepos;
7774
7775 if (STRINGP (it->string))
7776 {
7777 eassert (!it->s);
7778 stop = SCHARS (it->string);
7779 if (stop > it->end_charpos)
7780 stop = it->end_charpos;
7781 charpos = IT_STRING_CHARPOS (*it);
7782 bytepos = IT_STRING_BYTEPOS (*it);
7783 }
7784 else
7785 {
7786 stop = it->end_charpos;
7787 charpos = IT_CHARPOS (*it);
7788 bytepos = IT_BYTEPOS (*it);
7789 }
7790 if (it->bidi_it.scan_dir < 0)
7791 stop = -1;
7792 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7793 it->string);
7794 }
7795 }
7796
7797 /* Load IT with the next display element from Lisp string IT->string.
7798 IT->current.string_pos is the current position within the string.
7799 If IT->current.overlay_string_index >= 0, the Lisp string is an
7800 overlay string. */
7801
7802 static bool
7803 next_element_from_string (struct it *it)
7804 {
7805 struct text_pos position;
7806
7807 eassert (STRINGP (it->string));
7808 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7809 eassert (IT_STRING_CHARPOS (*it) >= 0);
7810 position = it->current.string_pos;
7811
7812 /* With bidi reordering, the character to display might not be the
7813 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7814 that we were reseat()ed to a new string, whose paragraph
7815 direction is not known. */
7816 if (it->bidi_p && it->bidi_it.first_elt)
7817 {
7818 get_visually_first_element (it);
7819 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7820 }
7821
7822 /* Time to check for invisible text? */
7823 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7824 {
7825 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7826 {
7827 if (!(!it->bidi_p
7828 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7829 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7830 {
7831 /* With bidi non-linear iteration, we could find
7832 ourselves far beyond the last computed stop_charpos,
7833 with several other stop positions in between that we
7834 missed. Scan them all now, in buffer's logical
7835 order, until we find and handle the last stop_charpos
7836 that precedes our current position. */
7837 handle_stop_backwards (it, it->stop_charpos);
7838 return GET_NEXT_DISPLAY_ELEMENT (it);
7839 }
7840 else
7841 {
7842 if (it->bidi_p)
7843 {
7844 /* Take note of the stop position we just moved
7845 across, for when we will move back across it. */
7846 it->prev_stop = it->stop_charpos;
7847 /* If we are at base paragraph embedding level, take
7848 note of the last stop position seen at this
7849 level. */
7850 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7851 it->base_level_stop = it->stop_charpos;
7852 }
7853 handle_stop (it);
7854
7855 /* Since a handler may have changed IT->method, we must
7856 recurse here. */
7857 return GET_NEXT_DISPLAY_ELEMENT (it);
7858 }
7859 }
7860 else if (it->bidi_p
7861 /* If we are before prev_stop, we may have overstepped
7862 on our way backwards a stop_pos, and if so, we need
7863 to handle that stop_pos. */
7864 && IT_STRING_CHARPOS (*it) < it->prev_stop
7865 /* We can sometimes back up for reasons that have nothing
7866 to do with bidi reordering. E.g., compositions. The
7867 code below is only needed when we are above the base
7868 embedding level, so test for that explicitly. */
7869 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7870 {
7871 /* If we lost track of base_level_stop, we have no better
7872 place for handle_stop_backwards to start from than string
7873 beginning. This happens, e.g., when we were reseated to
7874 the previous screenful of text by vertical-motion. */
7875 if (it->base_level_stop <= 0
7876 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7877 it->base_level_stop = 0;
7878 handle_stop_backwards (it, it->base_level_stop);
7879 return GET_NEXT_DISPLAY_ELEMENT (it);
7880 }
7881 }
7882
7883 if (it->current.overlay_string_index >= 0)
7884 {
7885 /* Get the next character from an overlay string. In overlay
7886 strings, there is no field width or padding with spaces to
7887 do. */
7888 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7889 {
7890 it->what = IT_EOB;
7891 return false;
7892 }
7893 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7894 IT_STRING_BYTEPOS (*it),
7895 it->bidi_it.scan_dir < 0
7896 ? -1
7897 : SCHARS (it->string))
7898 && next_element_from_composition (it))
7899 {
7900 return true;
7901 }
7902 else if (STRING_MULTIBYTE (it->string))
7903 {
7904 const unsigned char *s = (SDATA (it->string)
7905 + IT_STRING_BYTEPOS (*it));
7906 it->c = string_char_and_length (s, &it->len);
7907 }
7908 else
7909 {
7910 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7911 it->len = 1;
7912 }
7913 }
7914 else
7915 {
7916 /* Get the next character from a Lisp string that is not an
7917 overlay string. Such strings come from the mode line, for
7918 example. We may have to pad with spaces, or truncate the
7919 string. See also next_element_from_c_string. */
7920 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7921 {
7922 it->what = IT_EOB;
7923 return false;
7924 }
7925 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7926 {
7927 /* Pad with spaces. */
7928 it->c = ' ', it->len = 1;
7929 CHARPOS (position) = BYTEPOS (position) = -1;
7930 }
7931 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7932 IT_STRING_BYTEPOS (*it),
7933 it->bidi_it.scan_dir < 0
7934 ? -1
7935 : it->string_nchars)
7936 && next_element_from_composition (it))
7937 {
7938 return true;
7939 }
7940 else if (STRING_MULTIBYTE (it->string))
7941 {
7942 const unsigned char *s = (SDATA (it->string)
7943 + IT_STRING_BYTEPOS (*it));
7944 it->c = string_char_and_length (s, &it->len);
7945 }
7946 else
7947 {
7948 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7949 it->len = 1;
7950 }
7951 }
7952
7953 /* Record what we have and where it came from. */
7954 it->what = IT_CHARACTER;
7955 it->object = it->string;
7956 it->position = position;
7957 return true;
7958 }
7959
7960
7961 /* Load IT with next display element from C string IT->s.
7962 IT->string_nchars is the maximum number of characters to return
7963 from the string. IT->end_charpos may be greater than
7964 IT->string_nchars when this function is called, in which case we
7965 may have to return padding spaces. Value is false if end of string
7966 reached, including padding spaces. */
7967
7968 static bool
7969 next_element_from_c_string (struct it *it)
7970 {
7971 bool success_p = true;
7972
7973 eassert (it->s);
7974 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7975 it->what = IT_CHARACTER;
7976 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7977 it->object = make_number (0);
7978
7979 /* With bidi reordering, the character to display might not be the
7980 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
7981 we were reseated to a new string, whose paragraph direction is
7982 not known. */
7983 if (it->bidi_p && it->bidi_it.first_elt)
7984 get_visually_first_element (it);
7985
7986 /* IT's position can be greater than IT->string_nchars in case a
7987 field width or precision has been specified when the iterator was
7988 initialized. */
7989 if (IT_CHARPOS (*it) >= it->end_charpos)
7990 {
7991 /* End of the game. */
7992 it->what = IT_EOB;
7993 success_p = false;
7994 }
7995 else if (IT_CHARPOS (*it) >= it->string_nchars)
7996 {
7997 /* Pad with spaces. */
7998 it->c = ' ', it->len = 1;
7999 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8000 }
8001 else if (it->multibyte_p)
8002 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8003 else
8004 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8005
8006 return success_p;
8007 }
8008
8009
8010 /* Set up IT to return characters from an ellipsis, if appropriate.
8011 The definition of the ellipsis glyphs may come from a display table
8012 entry. This function fills IT with the first glyph from the
8013 ellipsis if an ellipsis is to be displayed. */
8014
8015 static bool
8016 next_element_from_ellipsis (struct it *it)
8017 {
8018 if (it->selective_display_ellipsis_p)
8019 setup_for_ellipsis (it, it->len);
8020 else
8021 {
8022 /* The face at the current position may be different from the
8023 face we find after the invisible text. Remember what it
8024 was in IT->saved_face_id, and signal that it's there by
8025 setting face_before_selective_p. */
8026 it->saved_face_id = it->face_id;
8027 it->method = GET_FROM_BUFFER;
8028 it->object = it->w->contents;
8029 reseat_at_next_visible_line_start (it, true);
8030 it->face_before_selective_p = true;
8031 }
8032
8033 return GET_NEXT_DISPLAY_ELEMENT (it);
8034 }
8035
8036
8037 /* Deliver an image display element. The iterator IT is already
8038 filled with image information (done in handle_display_prop). Value
8039 is always true. */
8040
8041
8042 static bool
8043 next_element_from_image (struct it *it)
8044 {
8045 it->what = IT_IMAGE;
8046 return true;
8047 }
8048
8049
8050 /* Fill iterator IT with next display element from a stretch glyph
8051 property. IT->object is the value of the text property. Value is
8052 always true. */
8053
8054 static bool
8055 next_element_from_stretch (struct it *it)
8056 {
8057 it->what = IT_STRETCH;
8058 return true;
8059 }
8060
8061 /* Scan backwards from IT's current position until we find a stop
8062 position, or until BEGV. This is called when we find ourself
8063 before both the last known prev_stop and base_level_stop while
8064 reordering bidirectional text. */
8065
8066 static void
8067 compute_stop_pos_backwards (struct it *it)
8068 {
8069 const int SCAN_BACK_LIMIT = 1000;
8070 struct text_pos pos;
8071 struct display_pos save_current = it->current;
8072 struct text_pos save_position = it->position;
8073 ptrdiff_t charpos = IT_CHARPOS (*it);
8074 ptrdiff_t where_we_are = charpos;
8075 ptrdiff_t save_stop_pos = it->stop_charpos;
8076 ptrdiff_t save_end_pos = it->end_charpos;
8077
8078 eassert (NILP (it->string) && !it->s);
8079 eassert (it->bidi_p);
8080 it->bidi_p = false;
8081 do
8082 {
8083 it->end_charpos = min (charpos + 1, ZV);
8084 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8085 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8086 reseat_1 (it, pos, false);
8087 compute_stop_pos (it);
8088 /* We must advance forward, right? */
8089 if (it->stop_charpos <= charpos)
8090 emacs_abort ();
8091 }
8092 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8093
8094 if (it->stop_charpos <= where_we_are)
8095 it->prev_stop = it->stop_charpos;
8096 else
8097 it->prev_stop = BEGV;
8098 it->bidi_p = true;
8099 it->current = save_current;
8100 it->position = save_position;
8101 it->stop_charpos = save_stop_pos;
8102 it->end_charpos = save_end_pos;
8103 }
8104
8105 /* Scan forward from CHARPOS in the current buffer/string, until we
8106 find a stop position > current IT's position. Then handle the stop
8107 position before that. This is called when we bump into a stop
8108 position while reordering bidirectional text. CHARPOS should be
8109 the last previously processed stop_pos (or BEGV/0, if none were
8110 processed yet) whose position is less that IT's current
8111 position. */
8112
8113 static void
8114 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8115 {
8116 bool bufp = !STRINGP (it->string);
8117 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8118 struct display_pos save_current = it->current;
8119 struct text_pos save_position = it->position;
8120 struct text_pos pos1;
8121 ptrdiff_t next_stop;
8122
8123 /* Scan in strict logical order. */
8124 eassert (it->bidi_p);
8125 it->bidi_p = false;
8126 do
8127 {
8128 it->prev_stop = charpos;
8129 if (bufp)
8130 {
8131 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8132 reseat_1 (it, pos1, false);
8133 }
8134 else
8135 it->current.string_pos = string_pos (charpos, it->string);
8136 compute_stop_pos (it);
8137 /* We must advance forward, right? */
8138 if (it->stop_charpos <= it->prev_stop)
8139 emacs_abort ();
8140 charpos = it->stop_charpos;
8141 }
8142 while (charpos <= where_we_are);
8143
8144 it->bidi_p = true;
8145 it->current = save_current;
8146 it->position = save_position;
8147 next_stop = it->stop_charpos;
8148 it->stop_charpos = it->prev_stop;
8149 handle_stop (it);
8150 it->stop_charpos = next_stop;
8151 }
8152
8153 /* Load IT with the next display element from current_buffer. Value
8154 is false if end of buffer reached. IT->stop_charpos is the next
8155 position at which to stop and check for text properties or buffer
8156 end. */
8157
8158 static bool
8159 next_element_from_buffer (struct it *it)
8160 {
8161 bool success_p = true;
8162
8163 eassert (IT_CHARPOS (*it) >= BEGV);
8164 eassert (NILP (it->string) && !it->s);
8165 eassert (!it->bidi_p
8166 || (EQ (it->bidi_it.string.lstring, Qnil)
8167 && it->bidi_it.string.s == NULL));
8168
8169 /* With bidi reordering, the character to display might not be the
8170 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8171 we were reseat()ed to a new buffer position, which is potentially
8172 a different paragraph. */
8173 if (it->bidi_p && it->bidi_it.first_elt)
8174 {
8175 get_visually_first_element (it);
8176 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8177 }
8178
8179 if (IT_CHARPOS (*it) >= it->stop_charpos)
8180 {
8181 if (IT_CHARPOS (*it) >= it->end_charpos)
8182 {
8183 bool overlay_strings_follow_p;
8184
8185 /* End of the game, except when overlay strings follow that
8186 haven't been returned yet. */
8187 if (it->overlay_strings_at_end_processed_p)
8188 overlay_strings_follow_p = false;
8189 else
8190 {
8191 it->overlay_strings_at_end_processed_p = true;
8192 overlay_strings_follow_p = get_overlay_strings (it, 0);
8193 }
8194
8195 if (overlay_strings_follow_p)
8196 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8197 else
8198 {
8199 it->what = IT_EOB;
8200 it->position = it->current.pos;
8201 success_p = false;
8202 }
8203 }
8204 else if (!(!it->bidi_p
8205 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8206 || IT_CHARPOS (*it) == it->stop_charpos))
8207 {
8208 /* With bidi non-linear iteration, we could find ourselves
8209 far beyond the last computed stop_charpos, with several
8210 other stop positions in between that we missed. Scan
8211 them all now, in buffer's logical order, until we find
8212 and handle the last stop_charpos that precedes our
8213 current position. */
8214 handle_stop_backwards (it, it->stop_charpos);
8215 it->ignore_overlay_strings_at_pos_p = false;
8216 return GET_NEXT_DISPLAY_ELEMENT (it);
8217 }
8218 else
8219 {
8220 if (it->bidi_p)
8221 {
8222 /* Take note of the stop position we just moved across,
8223 for when we will move back across it. */
8224 it->prev_stop = it->stop_charpos;
8225 /* If we are at base paragraph embedding level, take
8226 note of the last stop position seen at this
8227 level. */
8228 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8229 it->base_level_stop = it->stop_charpos;
8230 }
8231 handle_stop (it);
8232 it->ignore_overlay_strings_at_pos_p = false;
8233 return GET_NEXT_DISPLAY_ELEMENT (it);
8234 }
8235 }
8236 else if (it->bidi_p
8237 /* If we are before prev_stop, we may have overstepped on
8238 our way backwards a stop_pos, and if so, we need to
8239 handle that stop_pos. */
8240 && IT_CHARPOS (*it) < it->prev_stop
8241 /* We can sometimes back up for reasons that have nothing
8242 to do with bidi reordering. E.g., compositions. The
8243 code below is only needed when we are above the base
8244 embedding level, so test for that explicitly. */
8245 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8246 {
8247 if (it->base_level_stop <= 0
8248 || IT_CHARPOS (*it) < it->base_level_stop)
8249 {
8250 /* If we lost track of base_level_stop, we need to find
8251 prev_stop by looking backwards. This happens, e.g., when
8252 we were reseated to the previous screenful of text by
8253 vertical-motion. */
8254 it->base_level_stop = BEGV;
8255 compute_stop_pos_backwards (it);
8256 handle_stop_backwards (it, it->prev_stop);
8257 }
8258 else
8259 handle_stop_backwards (it, it->base_level_stop);
8260 it->ignore_overlay_strings_at_pos_p = false;
8261 return GET_NEXT_DISPLAY_ELEMENT (it);
8262 }
8263 else
8264 {
8265 /* No face changes, overlays etc. in sight, so just return a
8266 character from current_buffer. */
8267 unsigned char *p;
8268 ptrdiff_t stop;
8269
8270 /* We moved to the next buffer position, so any info about
8271 previously seen overlays is no longer valid. */
8272 it->ignore_overlay_strings_at_pos_p = false;
8273
8274 /* Maybe run the redisplay end trigger hook. Performance note:
8275 This doesn't seem to cost measurable time. */
8276 if (it->redisplay_end_trigger_charpos
8277 && it->glyph_row
8278 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8279 run_redisplay_end_trigger_hook (it);
8280
8281 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8282 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8283 stop)
8284 && next_element_from_composition (it))
8285 {
8286 return true;
8287 }
8288
8289 /* Get the next character, maybe multibyte. */
8290 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8291 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8292 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8293 else
8294 it->c = *p, it->len = 1;
8295
8296 /* Record what we have and where it came from. */
8297 it->what = IT_CHARACTER;
8298 it->object = it->w->contents;
8299 it->position = it->current.pos;
8300
8301 /* Normally we return the character found above, except when we
8302 really want to return an ellipsis for selective display. */
8303 if (it->selective)
8304 {
8305 if (it->c == '\n')
8306 {
8307 /* A value of selective > 0 means hide lines indented more
8308 than that number of columns. */
8309 if (it->selective > 0
8310 && IT_CHARPOS (*it) + 1 < ZV
8311 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8312 IT_BYTEPOS (*it) + 1,
8313 it->selective))
8314 {
8315 success_p = next_element_from_ellipsis (it);
8316 it->dpvec_char_len = -1;
8317 }
8318 }
8319 else if (it->c == '\r' && it->selective == -1)
8320 {
8321 /* A value of selective == -1 means that everything from the
8322 CR to the end of the line is invisible, with maybe an
8323 ellipsis displayed for it. */
8324 success_p = next_element_from_ellipsis (it);
8325 it->dpvec_char_len = -1;
8326 }
8327 }
8328 }
8329
8330 /* Value is false if end of buffer reached. */
8331 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8332 return success_p;
8333 }
8334
8335
8336 /* Run the redisplay end trigger hook for IT. */
8337
8338 static void
8339 run_redisplay_end_trigger_hook (struct it *it)
8340 {
8341 /* IT->glyph_row should be non-null, i.e. we should be actually
8342 displaying something, or otherwise we should not run the hook. */
8343 eassert (it->glyph_row);
8344
8345 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8346 it->redisplay_end_trigger_charpos = 0;
8347
8348 /* Since we are *trying* to run these functions, don't try to run
8349 them again, even if they get an error. */
8350 wset_redisplay_end_trigger (it->w, Qnil);
8351 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8352 make_number (charpos));
8353
8354 /* Notice if it changed the face of the character we are on. */
8355 handle_face_prop (it);
8356 }
8357
8358
8359 /* Deliver a composition display element. Unlike the other
8360 next_element_from_XXX, this function is not registered in the array
8361 get_next_element[]. It is called from next_element_from_buffer and
8362 next_element_from_string when necessary. */
8363
8364 static bool
8365 next_element_from_composition (struct it *it)
8366 {
8367 it->what = IT_COMPOSITION;
8368 it->len = it->cmp_it.nbytes;
8369 if (STRINGP (it->string))
8370 {
8371 if (it->c < 0)
8372 {
8373 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8374 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8375 return false;
8376 }
8377 it->position = it->current.string_pos;
8378 it->object = it->string;
8379 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8380 IT_STRING_BYTEPOS (*it), it->string);
8381 }
8382 else
8383 {
8384 if (it->c < 0)
8385 {
8386 IT_CHARPOS (*it) += it->cmp_it.nchars;
8387 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8388 if (it->bidi_p)
8389 {
8390 if (it->bidi_it.new_paragraph)
8391 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8392 false);
8393 /* Resync the bidi iterator with IT's new position.
8394 FIXME: this doesn't support bidirectional text. */
8395 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8396 bidi_move_to_visually_next (&it->bidi_it);
8397 }
8398 return false;
8399 }
8400 it->position = it->current.pos;
8401 it->object = it->w->contents;
8402 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8403 IT_BYTEPOS (*it), Qnil);
8404 }
8405 return true;
8406 }
8407
8408
8409 \f
8410 /***********************************************************************
8411 Moving an iterator without producing glyphs
8412 ***********************************************************************/
8413
8414 /* Check if iterator is at a position corresponding to a valid buffer
8415 position after some move_it_ call. */
8416
8417 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8418 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8419
8420
8421 /* Move iterator IT to a specified buffer or X position within one
8422 line on the display without producing glyphs.
8423
8424 OP should be a bit mask including some or all of these bits:
8425 MOVE_TO_X: Stop upon reaching x-position TO_X.
8426 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8427 Regardless of OP's value, stop upon reaching the end of the display line.
8428
8429 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8430 This means, in particular, that TO_X includes window's horizontal
8431 scroll amount.
8432
8433 The return value has several possible values that
8434 say what condition caused the scan to stop:
8435
8436 MOVE_POS_MATCH_OR_ZV
8437 - when TO_POS or ZV was reached.
8438
8439 MOVE_X_REACHED
8440 -when TO_X was reached before TO_POS or ZV were reached.
8441
8442 MOVE_LINE_CONTINUED
8443 - when we reached the end of the display area and the line must
8444 be continued.
8445
8446 MOVE_LINE_TRUNCATED
8447 - when we reached the end of the display area and the line is
8448 truncated.
8449
8450 MOVE_NEWLINE_OR_CR
8451 - when we stopped at a line end, i.e. a newline or a CR and selective
8452 display is on. */
8453
8454 static enum move_it_result
8455 move_it_in_display_line_to (struct it *it,
8456 ptrdiff_t to_charpos, int to_x,
8457 enum move_operation_enum op)
8458 {
8459 enum move_it_result result = MOVE_UNDEFINED;
8460 struct glyph_row *saved_glyph_row;
8461 struct it wrap_it, atpos_it, atx_it, ppos_it;
8462 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8463 void *ppos_data = NULL;
8464 bool may_wrap = false;
8465 enum it_method prev_method = it->method;
8466 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8467 bool saw_smaller_pos = prev_pos < to_charpos;
8468
8469 /* Don't produce glyphs in produce_glyphs. */
8470 saved_glyph_row = it->glyph_row;
8471 it->glyph_row = NULL;
8472
8473 /* Use wrap_it to save a copy of IT wherever a word wrap could
8474 occur. Use atpos_it to save a copy of IT at the desired buffer
8475 position, if found, so that we can scan ahead and check if the
8476 word later overshoots the window edge. Use atx_it similarly, for
8477 pixel positions. */
8478 wrap_it.sp = -1;
8479 atpos_it.sp = -1;
8480 atx_it.sp = -1;
8481
8482 /* Use ppos_it under bidi reordering to save a copy of IT for the
8483 initial position. We restore that position in IT when we have
8484 scanned the entire display line without finding a match for
8485 TO_CHARPOS and all the character positions are greater than
8486 TO_CHARPOS. We then restart the scan from the initial position,
8487 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8488 the closest to TO_CHARPOS. */
8489 if (it->bidi_p)
8490 {
8491 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8492 {
8493 SAVE_IT (ppos_it, *it, ppos_data);
8494 closest_pos = IT_CHARPOS (*it);
8495 }
8496 else
8497 closest_pos = ZV;
8498 }
8499
8500 #define BUFFER_POS_REACHED_P() \
8501 ((op & MOVE_TO_POS) != 0 \
8502 && BUFFERP (it->object) \
8503 && (IT_CHARPOS (*it) == to_charpos \
8504 || ((!it->bidi_p \
8505 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8506 && IT_CHARPOS (*it) > to_charpos) \
8507 || (it->what == IT_COMPOSITION \
8508 && ((IT_CHARPOS (*it) > to_charpos \
8509 && to_charpos >= it->cmp_it.charpos) \
8510 || (IT_CHARPOS (*it) < to_charpos \
8511 && to_charpos <= it->cmp_it.charpos)))) \
8512 && (it->method == GET_FROM_BUFFER \
8513 || (it->method == GET_FROM_DISPLAY_VECTOR \
8514 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8515
8516 /* If there's a line-/wrap-prefix, handle it. */
8517 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8518 && it->current_y < it->last_visible_y)
8519 handle_line_prefix (it);
8520
8521 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8522 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8523
8524 while (true)
8525 {
8526 int x, i, ascent = 0, descent = 0;
8527
8528 /* Utility macro to reset an iterator with x, ascent, and descent. */
8529 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8530 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8531 (IT)->max_descent = descent)
8532
8533 /* Stop if we move beyond TO_CHARPOS (after an image or a
8534 display string or stretch glyph). */
8535 if ((op & MOVE_TO_POS) != 0
8536 && BUFFERP (it->object)
8537 && it->method == GET_FROM_BUFFER
8538 && (((!it->bidi_p
8539 /* When the iterator is at base embedding level, we
8540 are guaranteed that characters are delivered for
8541 display in strictly increasing order of their
8542 buffer positions. */
8543 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8544 && IT_CHARPOS (*it) > to_charpos)
8545 || (it->bidi_p
8546 && (prev_method == GET_FROM_IMAGE
8547 || prev_method == GET_FROM_STRETCH
8548 || prev_method == GET_FROM_STRING)
8549 /* Passed TO_CHARPOS from left to right. */
8550 && ((prev_pos < to_charpos
8551 && IT_CHARPOS (*it) > to_charpos)
8552 /* Passed TO_CHARPOS from right to left. */
8553 || (prev_pos > to_charpos
8554 && IT_CHARPOS (*it) < to_charpos)))))
8555 {
8556 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8557 {
8558 result = MOVE_POS_MATCH_OR_ZV;
8559 break;
8560 }
8561 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8562 /* If wrap_it is valid, the current position might be in a
8563 word that is wrapped. So, save the iterator in
8564 atpos_it and continue to see if wrapping happens. */
8565 SAVE_IT (atpos_it, *it, atpos_data);
8566 }
8567
8568 /* Stop when ZV reached.
8569 We used to stop here when TO_CHARPOS reached as well, but that is
8570 too soon if this glyph does not fit on this line. So we handle it
8571 explicitly below. */
8572 if (!get_next_display_element (it))
8573 {
8574 result = MOVE_POS_MATCH_OR_ZV;
8575 break;
8576 }
8577
8578 if (it->line_wrap == TRUNCATE)
8579 {
8580 if (BUFFER_POS_REACHED_P ())
8581 {
8582 result = MOVE_POS_MATCH_OR_ZV;
8583 break;
8584 }
8585 }
8586 else
8587 {
8588 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8589 {
8590 if (IT_DISPLAYING_WHITESPACE (it))
8591 may_wrap = true;
8592 else if (may_wrap)
8593 {
8594 /* We have reached a glyph that follows one or more
8595 whitespace characters. If the position is
8596 already found, we are done. */
8597 if (atpos_it.sp >= 0)
8598 {
8599 RESTORE_IT (it, &atpos_it, atpos_data);
8600 result = MOVE_POS_MATCH_OR_ZV;
8601 goto done;
8602 }
8603 if (atx_it.sp >= 0)
8604 {
8605 RESTORE_IT (it, &atx_it, atx_data);
8606 result = MOVE_X_REACHED;
8607 goto done;
8608 }
8609 /* Otherwise, we can wrap here. */
8610 SAVE_IT (wrap_it, *it, wrap_data);
8611 may_wrap = false;
8612 }
8613 }
8614 }
8615
8616 /* Remember the line height for the current line, in case
8617 the next element doesn't fit on the line. */
8618 ascent = it->max_ascent;
8619 descent = it->max_descent;
8620
8621 /* The call to produce_glyphs will get the metrics of the
8622 display element IT is loaded with. Record the x-position
8623 before this display element, in case it doesn't fit on the
8624 line. */
8625 x = it->current_x;
8626
8627 PRODUCE_GLYPHS (it);
8628
8629 if (it->area != TEXT_AREA)
8630 {
8631 prev_method = it->method;
8632 if (it->method == GET_FROM_BUFFER)
8633 prev_pos = IT_CHARPOS (*it);
8634 set_iterator_to_next (it, true);
8635 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8636 SET_TEXT_POS (this_line_min_pos,
8637 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8638 if (it->bidi_p
8639 && (op & MOVE_TO_POS)
8640 && IT_CHARPOS (*it) > to_charpos
8641 && IT_CHARPOS (*it) < closest_pos)
8642 closest_pos = IT_CHARPOS (*it);
8643 continue;
8644 }
8645
8646 /* The number of glyphs we get back in IT->nglyphs will normally
8647 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8648 character on a terminal frame, or (iii) a line end. For the
8649 second case, IT->nglyphs - 1 padding glyphs will be present.
8650 (On X frames, there is only one glyph produced for a
8651 composite character.)
8652
8653 The behavior implemented below means, for continuation lines,
8654 that as many spaces of a TAB as fit on the current line are
8655 displayed there. For terminal frames, as many glyphs of a
8656 multi-glyph character are displayed in the current line, too.
8657 This is what the old redisplay code did, and we keep it that
8658 way. Under X, the whole shape of a complex character must
8659 fit on the line or it will be completely displayed in the
8660 next line.
8661
8662 Note that both for tabs and padding glyphs, all glyphs have
8663 the same width. */
8664 if (it->nglyphs)
8665 {
8666 /* More than one glyph or glyph doesn't fit on line. All
8667 glyphs have the same width. */
8668 int single_glyph_width = it->pixel_width / it->nglyphs;
8669 int new_x;
8670 int x_before_this_char = x;
8671 int hpos_before_this_char = it->hpos;
8672
8673 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8674 {
8675 new_x = x + single_glyph_width;
8676
8677 /* We want to leave anything reaching TO_X to the caller. */
8678 if ((op & MOVE_TO_X) && new_x > to_x)
8679 {
8680 if (BUFFER_POS_REACHED_P ())
8681 {
8682 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8683 goto buffer_pos_reached;
8684 if (atpos_it.sp < 0)
8685 {
8686 SAVE_IT (atpos_it, *it, atpos_data);
8687 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8688 }
8689 }
8690 else
8691 {
8692 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8693 {
8694 it->current_x = x;
8695 result = MOVE_X_REACHED;
8696 break;
8697 }
8698 if (atx_it.sp < 0)
8699 {
8700 SAVE_IT (atx_it, *it, atx_data);
8701 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8702 }
8703 }
8704 }
8705
8706 if (/* Lines are continued. */
8707 it->line_wrap != TRUNCATE
8708 && (/* And glyph doesn't fit on the line. */
8709 new_x > it->last_visible_x
8710 /* Or it fits exactly and we're on a window
8711 system frame. */
8712 || (new_x == it->last_visible_x
8713 && FRAME_WINDOW_P (it->f)
8714 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8715 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8716 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8717 {
8718 if (/* IT->hpos == 0 means the very first glyph
8719 doesn't fit on the line, e.g. a wide image. */
8720 it->hpos == 0
8721 || (new_x == it->last_visible_x
8722 && FRAME_WINDOW_P (it->f)))
8723 {
8724 ++it->hpos;
8725 it->current_x = new_x;
8726
8727 /* The character's last glyph just barely fits
8728 in this row. */
8729 if (i == it->nglyphs - 1)
8730 {
8731 /* If this is the destination position,
8732 return a position *before* it in this row,
8733 now that we know it fits in this row. */
8734 if (BUFFER_POS_REACHED_P ())
8735 {
8736 if (it->line_wrap != WORD_WRAP
8737 || wrap_it.sp < 0
8738 /* If we've just found whitespace to
8739 wrap, effectively ignore the
8740 previous wrap point -- it is no
8741 longer relevant, but we won't
8742 have an opportunity to update it,
8743 since we've reached the edge of
8744 this screen line. */
8745 || (may_wrap
8746 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8747 {
8748 it->hpos = hpos_before_this_char;
8749 it->current_x = x_before_this_char;
8750 result = MOVE_POS_MATCH_OR_ZV;
8751 break;
8752 }
8753 if (it->line_wrap == WORD_WRAP
8754 && atpos_it.sp < 0)
8755 {
8756 SAVE_IT (atpos_it, *it, atpos_data);
8757 atpos_it.current_x = x_before_this_char;
8758 atpos_it.hpos = hpos_before_this_char;
8759 }
8760 }
8761
8762 prev_method = it->method;
8763 if (it->method == GET_FROM_BUFFER)
8764 prev_pos = IT_CHARPOS (*it);
8765 set_iterator_to_next (it, true);
8766 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8767 SET_TEXT_POS (this_line_min_pos,
8768 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8769 /* On graphical terminals, newlines may
8770 "overflow" into the fringe if
8771 overflow-newline-into-fringe is non-nil.
8772 On text terminals, and on graphical
8773 terminals with no right margin, newlines
8774 may overflow into the last glyph on the
8775 display line.*/
8776 if (!FRAME_WINDOW_P (it->f)
8777 || ((it->bidi_p
8778 && it->bidi_it.paragraph_dir == R2L)
8779 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8780 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8781 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8782 {
8783 if (!get_next_display_element (it))
8784 {
8785 result = MOVE_POS_MATCH_OR_ZV;
8786 break;
8787 }
8788 if (BUFFER_POS_REACHED_P ())
8789 {
8790 if (ITERATOR_AT_END_OF_LINE_P (it))
8791 result = MOVE_POS_MATCH_OR_ZV;
8792 else
8793 result = MOVE_LINE_CONTINUED;
8794 break;
8795 }
8796 if (ITERATOR_AT_END_OF_LINE_P (it)
8797 && (it->line_wrap != WORD_WRAP
8798 || wrap_it.sp < 0
8799 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8800 {
8801 result = MOVE_NEWLINE_OR_CR;
8802 break;
8803 }
8804 }
8805 }
8806 }
8807 else
8808 IT_RESET_X_ASCENT_DESCENT (it);
8809
8810 /* If the screen line ends with whitespace, and we
8811 are under word-wrap, don't use wrap_it: it is no
8812 longer relevant, but we won't have an opportunity
8813 to update it, since we are done with this screen
8814 line. */
8815 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8816 {
8817 /* If we've found TO_X, go back there, as we now
8818 know the last word fits on this screen line. */
8819 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8820 && atx_it.sp >= 0)
8821 {
8822 RESTORE_IT (it, &atx_it, atx_data);
8823 atpos_it.sp = -1;
8824 atx_it.sp = -1;
8825 result = MOVE_X_REACHED;
8826 break;
8827 }
8828 }
8829 else if (wrap_it.sp >= 0)
8830 {
8831 RESTORE_IT (it, &wrap_it, wrap_data);
8832 atpos_it.sp = -1;
8833 atx_it.sp = -1;
8834 }
8835
8836 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8837 IT_CHARPOS (*it)));
8838 result = MOVE_LINE_CONTINUED;
8839 break;
8840 }
8841
8842 if (BUFFER_POS_REACHED_P ())
8843 {
8844 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8845 goto buffer_pos_reached;
8846 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8847 {
8848 SAVE_IT (atpos_it, *it, atpos_data);
8849 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8850 }
8851 }
8852
8853 if (new_x > it->first_visible_x)
8854 {
8855 /* Glyph is visible. Increment number of glyphs that
8856 would be displayed. */
8857 ++it->hpos;
8858 }
8859 }
8860
8861 if (result != MOVE_UNDEFINED)
8862 break;
8863 }
8864 else if (BUFFER_POS_REACHED_P ())
8865 {
8866 buffer_pos_reached:
8867 IT_RESET_X_ASCENT_DESCENT (it);
8868 result = MOVE_POS_MATCH_OR_ZV;
8869 break;
8870 }
8871 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8872 {
8873 /* Stop when TO_X specified and reached. This check is
8874 necessary here because of lines consisting of a line end,
8875 only. The line end will not produce any glyphs and we
8876 would never get MOVE_X_REACHED. */
8877 eassert (it->nglyphs == 0);
8878 result = MOVE_X_REACHED;
8879 break;
8880 }
8881
8882 /* Is this a line end? If yes, we're done. */
8883 if (ITERATOR_AT_END_OF_LINE_P (it))
8884 {
8885 /* If we are past TO_CHARPOS, but never saw any character
8886 positions smaller than TO_CHARPOS, return
8887 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8888 did. */
8889 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8890 {
8891 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8892 {
8893 if (closest_pos < ZV)
8894 {
8895 RESTORE_IT (it, &ppos_it, ppos_data);
8896 /* Don't recurse if closest_pos is equal to
8897 to_charpos, since we have just tried that. */
8898 if (closest_pos != to_charpos)
8899 move_it_in_display_line_to (it, closest_pos, -1,
8900 MOVE_TO_POS);
8901 result = MOVE_POS_MATCH_OR_ZV;
8902 }
8903 else
8904 goto buffer_pos_reached;
8905 }
8906 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8907 && IT_CHARPOS (*it) > to_charpos)
8908 goto buffer_pos_reached;
8909 else
8910 result = MOVE_NEWLINE_OR_CR;
8911 }
8912 else
8913 result = MOVE_NEWLINE_OR_CR;
8914 break;
8915 }
8916
8917 prev_method = it->method;
8918 if (it->method == GET_FROM_BUFFER)
8919 prev_pos = IT_CHARPOS (*it);
8920 /* The current display element has been consumed. Advance
8921 to the next. */
8922 set_iterator_to_next (it, true);
8923 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8924 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8925 if (IT_CHARPOS (*it) < to_charpos)
8926 saw_smaller_pos = true;
8927 if (it->bidi_p
8928 && (op & MOVE_TO_POS)
8929 && IT_CHARPOS (*it) >= to_charpos
8930 && IT_CHARPOS (*it) < closest_pos)
8931 closest_pos = IT_CHARPOS (*it);
8932
8933 /* Stop if lines are truncated and IT's current x-position is
8934 past the right edge of the window now. */
8935 if (it->line_wrap == TRUNCATE
8936 && it->current_x >= it->last_visible_x)
8937 {
8938 if (!FRAME_WINDOW_P (it->f)
8939 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8940 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8941 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8942 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8943 {
8944 bool at_eob_p = false;
8945
8946 if ((at_eob_p = !get_next_display_element (it))
8947 || BUFFER_POS_REACHED_P ()
8948 /* If we are past TO_CHARPOS, but never saw any
8949 character positions smaller than TO_CHARPOS,
8950 return MOVE_POS_MATCH_OR_ZV, like the
8951 unidirectional display did. */
8952 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8953 && !saw_smaller_pos
8954 && IT_CHARPOS (*it) > to_charpos))
8955 {
8956 if (it->bidi_p
8957 && !BUFFER_POS_REACHED_P ()
8958 && !at_eob_p && closest_pos < ZV)
8959 {
8960 RESTORE_IT (it, &ppos_it, ppos_data);
8961 if (closest_pos != to_charpos)
8962 move_it_in_display_line_to (it, closest_pos, -1,
8963 MOVE_TO_POS);
8964 }
8965 result = MOVE_POS_MATCH_OR_ZV;
8966 break;
8967 }
8968 if (ITERATOR_AT_END_OF_LINE_P (it))
8969 {
8970 result = MOVE_NEWLINE_OR_CR;
8971 break;
8972 }
8973 }
8974 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8975 && !saw_smaller_pos
8976 && IT_CHARPOS (*it) > to_charpos)
8977 {
8978 if (closest_pos < ZV)
8979 {
8980 RESTORE_IT (it, &ppos_it, ppos_data);
8981 if (closest_pos != to_charpos)
8982 move_it_in_display_line_to (it, closest_pos, -1,
8983 MOVE_TO_POS);
8984 }
8985 result = MOVE_POS_MATCH_OR_ZV;
8986 break;
8987 }
8988 result = MOVE_LINE_TRUNCATED;
8989 break;
8990 }
8991 #undef IT_RESET_X_ASCENT_DESCENT
8992 }
8993
8994 #undef BUFFER_POS_REACHED_P
8995
8996 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8997 restore the saved iterator. */
8998 if (atpos_it.sp >= 0)
8999 RESTORE_IT (it, &atpos_it, atpos_data);
9000 else if (atx_it.sp >= 0)
9001 RESTORE_IT (it, &atx_it, atx_data);
9002
9003 done:
9004
9005 if (atpos_data)
9006 bidi_unshelve_cache (atpos_data, true);
9007 if (atx_data)
9008 bidi_unshelve_cache (atx_data, true);
9009 if (wrap_data)
9010 bidi_unshelve_cache (wrap_data, true);
9011 if (ppos_data)
9012 bidi_unshelve_cache (ppos_data, true);
9013
9014 /* Restore the iterator settings altered at the beginning of this
9015 function. */
9016 it->glyph_row = saved_glyph_row;
9017 return result;
9018 }
9019
9020 /* For external use. */
9021 void
9022 move_it_in_display_line (struct it *it,
9023 ptrdiff_t to_charpos, int to_x,
9024 enum move_operation_enum op)
9025 {
9026 if (it->line_wrap == WORD_WRAP
9027 && (op & MOVE_TO_X))
9028 {
9029 struct it save_it;
9030 void *save_data = NULL;
9031 int skip;
9032
9033 SAVE_IT (save_it, *it, save_data);
9034 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9035 /* When word-wrap is on, TO_X may lie past the end
9036 of a wrapped line. Then it->current is the
9037 character on the next line, so backtrack to the
9038 space before the wrap point. */
9039 if (skip == MOVE_LINE_CONTINUED)
9040 {
9041 int prev_x = max (it->current_x - 1, 0);
9042 RESTORE_IT (it, &save_it, save_data);
9043 move_it_in_display_line_to
9044 (it, -1, prev_x, MOVE_TO_X);
9045 }
9046 else
9047 bidi_unshelve_cache (save_data, true);
9048 }
9049 else
9050 move_it_in_display_line_to (it, to_charpos, to_x, op);
9051 }
9052
9053
9054 /* Move IT forward until it satisfies one or more of the criteria in
9055 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9056
9057 OP is a bit-mask that specifies where to stop, and in particular,
9058 which of those four position arguments makes a difference. See the
9059 description of enum move_operation_enum.
9060
9061 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9062 screen line, this function will set IT to the next position that is
9063 displayed to the right of TO_CHARPOS on the screen.
9064
9065 Return the maximum pixel length of any line scanned but never more
9066 than it.last_visible_x. */
9067
9068 int
9069 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9070 {
9071 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9072 int line_height, line_start_x = 0, reached = 0;
9073 int max_current_x = 0;
9074 void *backup_data = NULL;
9075
9076 for (;;)
9077 {
9078 if (op & MOVE_TO_VPOS)
9079 {
9080 /* If no TO_CHARPOS and no TO_X specified, stop at the
9081 start of the line TO_VPOS. */
9082 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9083 {
9084 if (it->vpos == to_vpos)
9085 {
9086 reached = 1;
9087 break;
9088 }
9089 else
9090 skip = move_it_in_display_line_to (it, -1, -1, 0);
9091 }
9092 else
9093 {
9094 /* TO_VPOS >= 0 means stop at TO_X in the line at
9095 TO_VPOS, or at TO_POS, whichever comes first. */
9096 if (it->vpos == to_vpos)
9097 {
9098 reached = 2;
9099 break;
9100 }
9101
9102 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9103
9104 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9105 {
9106 reached = 3;
9107 break;
9108 }
9109 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9110 {
9111 /* We have reached TO_X but not in the line we want. */
9112 skip = move_it_in_display_line_to (it, to_charpos,
9113 -1, MOVE_TO_POS);
9114 if (skip == MOVE_POS_MATCH_OR_ZV)
9115 {
9116 reached = 4;
9117 break;
9118 }
9119 }
9120 }
9121 }
9122 else if (op & MOVE_TO_Y)
9123 {
9124 struct it it_backup;
9125
9126 if (it->line_wrap == WORD_WRAP)
9127 SAVE_IT (it_backup, *it, backup_data);
9128
9129 /* TO_Y specified means stop at TO_X in the line containing
9130 TO_Y---or at TO_CHARPOS if this is reached first. The
9131 problem is that we can't really tell whether the line
9132 contains TO_Y before we have completely scanned it, and
9133 this may skip past TO_X. What we do is to first scan to
9134 TO_X.
9135
9136 If TO_X is not specified, use a TO_X of zero. The reason
9137 is to make the outcome of this function more predictable.
9138 If we didn't use TO_X == 0, we would stop at the end of
9139 the line which is probably not what a caller would expect
9140 to happen. */
9141 skip = move_it_in_display_line_to
9142 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9143 (MOVE_TO_X | (op & MOVE_TO_POS)));
9144
9145 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9146 if (skip == MOVE_POS_MATCH_OR_ZV)
9147 reached = 5;
9148 else if (skip == MOVE_X_REACHED)
9149 {
9150 /* If TO_X was reached, we want to know whether TO_Y is
9151 in the line. We know this is the case if the already
9152 scanned glyphs make the line tall enough. Otherwise,
9153 we must check by scanning the rest of the line. */
9154 line_height = it->max_ascent + it->max_descent;
9155 if (to_y >= it->current_y
9156 && to_y < it->current_y + line_height)
9157 {
9158 reached = 6;
9159 break;
9160 }
9161 SAVE_IT (it_backup, *it, backup_data);
9162 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9163 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9164 op & MOVE_TO_POS);
9165 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9166 line_height = it->max_ascent + it->max_descent;
9167 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9168
9169 if (to_y >= it->current_y
9170 && to_y < it->current_y + line_height)
9171 {
9172 /* If TO_Y is in this line and TO_X was reached
9173 above, we scanned too far. We have to restore
9174 IT's settings to the ones before skipping. But
9175 keep the more accurate values of max_ascent and
9176 max_descent we've found while skipping the rest
9177 of the line, for the sake of callers, such as
9178 pos_visible_p, that need to know the line
9179 height. */
9180 int max_ascent = it->max_ascent;
9181 int max_descent = it->max_descent;
9182
9183 RESTORE_IT (it, &it_backup, backup_data);
9184 it->max_ascent = max_ascent;
9185 it->max_descent = max_descent;
9186 reached = 6;
9187 }
9188 else
9189 {
9190 skip = skip2;
9191 if (skip == MOVE_POS_MATCH_OR_ZV)
9192 reached = 7;
9193 }
9194 }
9195 else
9196 {
9197 /* Check whether TO_Y is in this line. */
9198 line_height = it->max_ascent + it->max_descent;
9199 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9200
9201 if (to_y >= it->current_y
9202 && to_y < it->current_y + line_height)
9203 {
9204 if (to_y > it->current_y)
9205 max_current_x = max (it->current_x, max_current_x);
9206
9207 /* When word-wrap is on, TO_X may lie past the end
9208 of a wrapped line. Then it->current is the
9209 character on the next line, so backtrack to the
9210 space before the wrap point. */
9211 if (skip == MOVE_LINE_CONTINUED
9212 && it->line_wrap == WORD_WRAP)
9213 {
9214 int prev_x = max (it->current_x - 1, 0);
9215 RESTORE_IT (it, &it_backup, backup_data);
9216 skip = move_it_in_display_line_to
9217 (it, -1, prev_x, MOVE_TO_X);
9218 }
9219
9220 reached = 6;
9221 }
9222 }
9223
9224 if (reached)
9225 {
9226 max_current_x = max (it->current_x, max_current_x);
9227 break;
9228 }
9229 }
9230 else if (BUFFERP (it->object)
9231 && (it->method == GET_FROM_BUFFER
9232 || it->method == GET_FROM_STRETCH)
9233 && IT_CHARPOS (*it) >= to_charpos
9234 /* Under bidi iteration, a call to set_iterator_to_next
9235 can scan far beyond to_charpos if the initial
9236 portion of the next line needs to be reordered. In
9237 that case, give move_it_in_display_line_to another
9238 chance below. */
9239 && !(it->bidi_p
9240 && it->bidi_it.scan_dir == -1))
9241 skip = MOVE_POS_MATCH_OR_ZV;
9242 else
9243 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9244
9245 switch (skip)
9246 {
9247 case MOVE_POS_MATCH_OR_ZV:
9248 max_current_x = max (it->current_x, max_current_x);
9249 reached = 8;
9250 goto out;
9251
9252 case MOVE_NEWLINE_OR_CR:
9253 max_current_x = max (it->current_x, max_current_x);
9254 set_iterator_to_next (it, true);
9255 it->continuation_lines_width = 0;
9256 break;
9257
9258 case MOVE_LINE_TRUNCATED:
9259 max_current_x = it->last_visible_x;
9260 it->continuation_lines_width = 0;
9261 reseat_at_next_visible_line_start (it, false);
9262 if ((op & MOVE_TO_POS) != 0
9263 && IT_CHARPOS (*it) > to_charpos)
9264 {
9265 reached = 9;
9266 goto out;
9267 }
9268 break;
9269
9270 case MOVE_LINE_CONTINUED:
9271 max_current_x = it->last_visible_x;
9272 /* For continued lines ending in a tab, some of the glyphs
9273 associated with the tab are displayed on the current
9274 line. Since it->current_x does not include these glyphs,
9275 we use it->last_visible_x instead. */
9276 if (it->c == '\t')
9277 {
9278 it->continuation_lines_width += it->last_visible_x;
9279 /* When moving by vpos, ensure that the iterator really
9280 advances to the next line (bug#847, bug#969). Fixme:
9281 do we need to do this in other circumstances? */
9282 if (it->current_x != it->last_visible_x
9283 && (op & MOVE_TO_VPOS)
9284 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9285 {
9286 line_start_x = it->current_x + it->pixel_width
9287 - it->last_visible_x;
9288 if (FRAME_WINDOW_P (it->f))
9289 {
9290 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9291 struct font *face_font = face->font;
9292
9293 /* When display_line produces a continued line
9294 that ends in a TAB, it skips a tab stop that
9295 is closer than the font's space character
9296 width (see x_produce_glyphs where it produces
9297 the stretch glyph which represents a TAB).
9298 We need to reproduce the same logic here. */
9299 eassert (face_font);
9300 if (face_font)
9301 {
9302 if (line_start_x < face_font->space_width)
9303 line_start_x
9304 += it->tab_width * face_font->space_width;
9305 }
9306 }
9307 set_iterator_to_next (it, false);
9308 }
9309 }
9310 else
9311 it->continuation_lines_width += it->current_x;
9312 break;
9313
9314 default:
9315 emacs_abort ();
9316 }
9317
9318 /* Reset/increment for the next run. */
9319 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9320 it->current_x = line_start_x;
9321 line_start_x = 0;
9322 it->hpos = 0;
9323 it->current_y += it->max_ascent + it->max_descent;
9324 ++it->vpos;
9325 last_height = it->max_ascent + it->max_descent;
9326 it->max_ascent = it->max_descent = 0;
9327 }
9328
9329 out:
9330
9331 /* On text terminals, we may stop at the end of a line in the middle
9332 of a multi-character glyph. If the glyph itself is continued,
9333 i.e. it is actually displayed on the next line, don't treat this
9334 stopping point as valid; move to the next line instead (unless
9335 that brings us offscreen). */
9336 if (!FRAME_WINDOW_P (it->f)
9337 && op & MOVE_TO_POS
9338 && IT_CHARPOS (*it) == to_charpos
9339 && it->what == IT_CHARACTER
9340 && it->nglyphs > 1
9341 && it->line_wrap == WINDOW_WRAP
9342 && it->current_x == it->last_visible_x - 1
9343 && it->c != '\n'
9344 && it->c != '\t'
9345 && it->w->window_end_valid
9346 && it->vpos < it->w->window_end_vpos)
9347 {
9348 it->continuation_lines_width += it->current_x;
9349 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9350 it->current_y += it->max_ascent + it->max_descent;
9351 ++it->vpos;
9352 last_height = it->max_ascent + it->max_descent;
9353 }
9354
9355 if (backup_data)
9356 bidi_unshelve_cache (backup_data, true);
9357
9358 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9359
9360 return max_current_x;
9361 }
9362
9363
9364 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9365
9366 If DY > 0, move IT backward at least that many pixels. DY = 0
9367 means move IT backward to the preceding line start or BEGV. This
9368 function may move over more than DY pixels if IT->current_y - DY
9369 ends up in the middle of a line; in this case IT->current_y will be
9370 set to the top of the line moved to. */
9371
9372 void
9373 move_it_vertically_backward (struct it *it, int dy)
9374 {
9375 int nlines, h;
9376 struct it it2, it3;
9377 void *it2data = NULL, *it3data = NULL;
9378 ptrdiff_t start_pos;
9379 int nchars_per_row
9380 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9381 ptrdiff_t pos_limit;
9382
9383 move_further_back:
9384 eassert (dy >= 0);
9385
9386 start_pos = IT_CHARPOS (*it);
9387
9388 /* Estimate how many newlines we must move back. */
9389 nlines = max (1, dy / default_line_pixel_height (it->w));
9390 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9391 pos_limit = BEGV;
9392 else
9393 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9394
9395 /* Set the iterator's position that many lines back. But don't go
9396 back more than NLINES full screen lines -- this wins a day with
9397 buffers which have very long lines. */
9398 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9399 back_to_previous_visible_line_start (it);
9400
9401 /* Reseat the iterator here. When moving backward, we don't want
9402 reseat to skip forward over invisible text, set up the iterator
9403 to deliver from overlay strings at the new position etc. So,
9404 use reseat_1 here. */
9405 reseat_1 (it, it->current.pos, true);
9406
9407 /* We are now surely at a line start. */
9408 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9409 reordering is in effect. */
9410 it->continuation_lines_width = 0;
9411
9412 /* Move forward and see what y-distance we moved. First move to the
9413 start of the next line so that we get its height. We need this
9414 height to be able to tell whether we reached the specified
9415 y-distance. */
9416 SAVE_IT (it2, *it, it2data);
9417 it2.max_ascent = it2.max_descent = 0;
9418 do
9419 {
9420 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9421 MOVE_TO_POS | MOVE_TO_VPOS);
9422 }
9423 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9424 /* If we are in a display string which starts at START_POS,
9425 and that display string includes a newline, and we are
9426 right after that newline (i.e. at the beginning of a
9427 display line), exit the loop, because otherwise we will
9428 infloop, since move_it_to will see that it is already at
9429 START_POS and will not move. */
9430 || (it2.method == GET_FROM_STRING
9431 && IT_CHARPOS (it2) == start_pos
9432 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9433 eassert (IT_CHARPOS (*it) >= BEGV);
9434 SAVE_IT (it3, it2, it3data);
9435
9436 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9437 eassert (IT_CHARPOS (*it) >= BEGV);
9438 /* H is the actual vertical distance from the position in *IT
9439 and the starting position. */
9440 h = it2.current_y - it->current_y;
9441 /* NLINES is the distance in number of lines. */
9442 nlines = it2.vpos - it->vpos;
9443
9444 /* Correct IT's y and vpos position
9445 so that they are relative to the starting point. */
9446 it->vpos -= nlines;
9447 it->current_y -= h;
9448
9449 if (dy == 0)
9450 {
9451 /* DY == 0 means move to the start of the screen line. The
9452 value of nlines is > 0 if continuation lines were involved,
9453 or if the original IT position was at start of a line. */
9454 RESTORE_IT (it, it, it2data);
9455 if (nlines > 0)
9456 move_it_by_lines (it, nlines);
9457 /* The above code moves us to some position NLINES down,
9458 usually to its first glyph (leftmost in an L2R line), but
9459 that's not necessarily the start of the line, under bidi
9460 reordering. We want to get to the character position
9461 that is immediately after the newline of the previous
9462 line. */
9463 if (it->bidi_p
9464 && !it->continuation_lines_width
9465 && !STRINGP (it->string)
9466 && IT_CHARPOS (*it) > BEGV
9467 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9468 {
9469 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9470
9471 DEC_BOTH (cp, bp);
9472 cp = find_newline_no_quit (cp, bp, -1, NULL);
9473 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9474 }
9475 bidi_unshelve_cache (it3data, true);
9476 }
9477 else
9478 {
9479 /* The y-position we try to reach, relative to *IT.
9480 Note that H has been subtracted in front of the if-statement. */
9481 int target_y = it->current_y + h - dy;
9482 int y0 = it3.current_y;
9483 int y1;
9484 int line_height;
9485
9486 RESTORE_IT (&it3, &it3, it3data);
9487 y1 = line_bottom_y (&it3);
9488 line_height = y1 - y0;
9489 RESTORE_IT (it, it, it2data);
9490 /* If we did not reach target_y, try to move further backward if
9491 we can. If we moved too far backward, try to move forward. */
9492 if (target_y < it->current_y
9493 /* This is heuristic. In a window that's 3 lines high, with
9494 a line height of 13 pixels each, recentering with point
9495 on the bottom line will try to move -39/2 = 19 pixels
9496 backward. Try to avoid moving into the first line. */
9497 && (it->current_y - target_y
9498 > min (window_box_height (it->w), line_height * 2 / 3))
9499 && IT_CHARPOS (*it) > BEGV)
9500 {
9501 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9502 target_y - it->current_y));
9503 dy = it->current_y - target_y;
9504 goto move_further_back;
9505 }
9506 else if (target_y >= it->current_y + line_height
9507 && IT_CHARPOS (*it) < ZV)
9508 {
9509 /* Should move forward by at least one line, maybe more.
9510
9511 Note: Calling move_it_by_lines can be expensive on
9512 terminal frames, where compute_motion is used (via
9513 vmotion) to do the job, when there are very long lines
9514 and truncate-lines is nil. That's the reason for
9515 treating terminal frames specially here. */
9516
9517 if (!FRAME_WINDOW_P (it->f))
9518 move_it_vertically (it, target_y - it->current_y);
9519 else
9520 {
9521 do
9522 {
9523 move_it_by_lines (it, 1);
9524 }
9525 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9526 }
9527 }
9528 }
9529 }
9530
9531
9532 /* Move IT by a specified amount of pixel lines DY. DY negative means
9533 move backwards. DY = 0 means move to start of screen line. At the
9534 end, IT will be on the start of a screen line. */
9535
9536 void
9537 move_it_vertically (struct it *it, int dy)
9538 {
9539 if (dy <= 0)
9540 move_it_vertically_backward (it, -dy);
9541 else
9542 {
9543 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9544 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9545 MOVE_TO_POS | MOVE_TO_Y);
9546 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9547
9548 /* If buffer ends in ZV without a newline, move to the start of
9549 the line to satisfy the post-condition. */
9550 if (IT_CHARPOS (*it) == ZV
9551 && ZV > BEGV
9552 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9553 move_it_by_lines (it, 0);
9554 }
9555 }
9556
9557
9558 /* Move iterator IT past the end of the text line it is in. */
9559
9560 void
9561 move_it_past_eol (struct it *it)
9562 {
9563 enum move_it_result rc;
9564
9565 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9566 if (rc == MOVE_NEWLINE_OR_CR)
9567 set_iterator_to_next (it, false);
9568 }
9569
9570
9571 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9572 negative means move up. DVPOS == 0 means move to the start of the
9573 screen line.
9574
9575 Optimization idea: If we would know that IT->f doesn't use
9576 a face with proportional font, we could be faster for
9577 truncate-lines nil. */
9578
9579 void
9580 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9581 {
9582
9583 /* The commented-out optimization uses vmotion on terminals. This
9584 gives bad results, because elements like it->what, on which
9585 callers such as pos_visible_p rely, aren't updated. */
9586 /* struct position pos;
9587 if (!FRAME_WINDOW_P (it->f))
9588 {
9589 struct text_pos textpos;
9590
9591 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9592 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9593 reseat (it, textpos, true);
9594 it->vpos += pos.vpos;
9595 it->current_y += pos.vpos;
9596 }
9597 else */
9598
9599 if (dvpos == 0)
9600 {
9601 /* DVPOS == 0 means move to the start of the screen line. */
9602 move_it_vertically_backward (it, 0);
9603 /* Let next call to line_bottom_y calculate real line height. */
9604 last_height = 0;
9605 }
9606 else if (dvpos > 0)
9607 {
9608 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9609 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9610 {
9611 /* Only move to the next buffer position if we ended up in a
9612 string from display property, not in an overlay string
9613 (before-string or after-string). That is because the
9614 latter don't conceal the underlying buffer position, so
9615 we can ask to move the iterator to the exact position we
9616 are interested in. Note that, even if we are already at
9617 IT_CHARPOS (*it), the call below is not a no-op, as it
9618 will detect that we are at the end of the string, pop the
9619 iterator, and compute it->current_x and it->hpos
9620 correctly. */
9621 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9622 -1, -1, -1, MOVE_TO_POS);
9623 }
9624 }
9625 else
9626 {
9627 struct it it2;
9628 void *it2data = NULL;
9629 ptrdiff_t start_charpos, i;
9630 int nchars_per_row
9631 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9632 bool hit_pos_limit = false;
9633 ptrdiff_t pos_limit;
9634
9635 /* Start at the beginning of the screen line containing IT's
9636 position. This may actually move vertically backwards,
9637 in case of overlays, so adjust dvpos accordingly. */
9638 dvpos += it->vpos;
9639 move_it_vertically_backward (it, 0);
9640 dvpos -= it->vpos;
9641
9642 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9643 screen lines, and reseat the iterator there. */
9644 start_charpos = IT_CHARPOS (*it);
9645 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9646 pos_limit = BEGV;
9647 else
9648 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9649
9650 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9651 back_to_previous_visible_line_start (it);
9652 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9653 hit_pos_limit = true;
9654 reseat (it, it->current.pos, true);
9655
9656 /* Move further back if we end up in a string or an image. */
9657 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9658 {
9659 /* First try to move to start of display line. */
9660 dvpos += it->vpos;
9661 move_it_vertically_backward (it, 0);
9662 dvpos -= it->vpos;
9663 if (IT_POS_VALID_AFTER_MOVE_P (it))
9664 break;
9665 /* If start of line is still in string or image,
9666 move further back. */
9667 back_to_previous_visible_line_start (it);
9668 reseat (it, it->current.pos, true);
9669 dvpos--;
9670 }
9671
9672 it->current_x = it->hpos = 0;
9673
9674 /* Above call may have moved too far if continuation lines
9675 are involved. Scan forward and see if it did. */
9676 SAVE_IT (it2, *it, it2data);
9677 it2.vpos = it2.current_y = 0;
9678 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9679 it->vpos -= it2.vpos;
9680 it->current_y -= it2.current_y;
9681 it->current_x = it->hpos = 0;
9682
9683 /* If we moved too far back, move IT some lines forward. */
9684 if (it2.vpos > -dvpos)
9685 {
9686 int delta = it2.vpos + dvpos;
9687
9688 RESTORE_IT (&it2, &it2, it2data);
9689 SAVE_IT (it2, *it, it2data);
9690 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9691 /* Move back again if we got too far ahead. */
9692 if (IT_CHARPOS (*it) >= start_charpos)
9693 RESTORE_IT (it, &it2, it2data);
9694 else
9695 bidi_unshelve_cache (it2data, true);
9696 }
9697 else if (hit_pos_limit && pos_limit > BEGV
9698 && dvpos < 0 && it2.vpos < -dvpos)
9699 {
9700 /* If we hit the limit, but still didn't make it far enough
9701 back, that means there's a display string with a newline
9702 covering a large chunk of text, and that caused
9703 back_to_previous_visible_line_start try to go too far.
9704 Punish those who commit such atrocities by going back
9705 until we've reached DVPOS, after lifting the limit, which
9706 could make it slow for very long lines. "If it hurts,
9707 don't do that!" */
9708 dvpos += it2.vpos;
9709 RESTORE_IT (it, it, it2data);
9710 for (i = -dvpos; i > 0; --i)
9711 {
9712 back_to_previous_visible_line_start (it);
9713 it->vpos--;
9714 }
9715 reseat_1 (it, it->current.pos, true);
9716 }
9717 else
9718 RESTORE_IT (it, it, it2data);
9719 }
9720 }
9721
9722 /* Return true if IT points into the middle of a display vector. */
9723
9724 bool
9725 in_display_vector_p (struct it *it)
9726 {
9727 return (it->method == GET_FROM_DISPLAY_VECTOR
9728 && it->current.dpvec_index > 0
9729 && it->dpvec + it->current.dpvec_index != it->dpend);
9730 }
9731
9732 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9733 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9734 WINDOW must be a live window and defaults to the selected one. The
9735 return value is a cons of the maximum pixel-width of any text line and
9736 the maximum pixel-height of all text lines.
9737
9738 The optional argument FROM, if non-nil, specifies the first text
9739 position and defaults to the minimum accessible position of the buffer.
9740 If FROM is t, use the minimum accessible position that is not a newline
9741 character. TO, if non-nil, specifies the last text position and
9742 defaults to the maximum accessible position of the buffer. If TO is t,
9743 use the maximum accessible position that is not a newline character.
9744
9745 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9746 width that can be returned. X-LIMIT nil or omitted, means to use the
9747 pixel-width of WINDOW's body; use this if you do not intend to change
9748 the width of WINDOW. Use the maximum width WINDOW may assume if you
9749 intend to change WINDOW's width. In any case, text whose x-coordinate
9750 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9751 can take some time, it's always a good idea to make this argument as
9752 small as possible; in particular, if the buffer contains long lines that
9753 shall be truncated anyway.
9754
9755 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9756 height that can be returned. Text lines whose y-coordinate is beyond
9757 Y-LIMIT are ignored. Since calculating the text height of a large
9758 buffer can take some time, it makes sense to specify this argument if
9759 the size of the buffer is unknown.
9760
9761 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9762 include the height of the mode- or header-line of WINDOW in the return
9763 value. If it is either the symbol `mode-line' or `header-line', include
9764 only the height of that line, if present, in the return value. If t,
9765 include the height of both, if present, in the return value. */)
9766 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9767 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9768 {
9769 struct window *w = decode_live_window (window);
9770 Lisp_Object buffer = w->contents;
9771 struct buffer *b;
9772 struct it it;
9773 struct buffer *old_b = NULL;
9774 ptrdiff_t start, end, pos;
9775 struct text_pos startp;
9776 void *itdata = NULL;
9777 int c, max_y = -1, x = 0, y = 0;
9778
9779 CHECK_BUFFER (buffer);
9780 b = XBUFFER (buffer);
9781
9782 if (b != current_buffer)
9783 {
9784 old_b = current_buffer;
9785 set_buffer_internal (b);
9786 }
9787
9788 if (NILP (from))
9789 start = BEGV;
9790 else if (EQ (from, Qt))
9791 {
9792 start = pos = BEGV;
9793 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9794 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9795 start = pos;
9796 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9797 start = pos;
9798 }
9799 else
9800 {
9801 CHECK_NUMBER_COERCE_MARKER (from);
9802 start = min (max (XINT (from), BEGV), ZV);
9803 }
9804
9805 if (NILP (to))
9806 end = ZV;
9807 else if (EQ (to, Qt))
9808 {
9809 end = pos = ZV;
9810 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9811 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9812 end = pos;
9813 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9814 end = pos;
9815 }
9816 else
9817 {
9818 CHECK_NUMBER_COERCE_MARKER (to);
9819 end = max (start, min (XINT (to), ZV));
9820 }
9821
9822 if (!NILP (y_limit))
9823 {
9824 CHECK_NUMBER (y_limit);
9825 max_y = min (XINT (y_limit), INT_MAX);
9826 }
9827
9828 itdata = bidi_shelve_cache ();
9829 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9830 start_display (&it, w, startp);
9831
9832 if (NILP (x_limit))
9833 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9834 else
9835 {
9836 CHECK_NUMBER (x_limit);
9837 it.last_visible_x = min (XINT (x_limit), INFINITY);
9838 /* Actually, we never want move_it_to stop at to_x. But to make
9839 sure that move_it_in_display_line_to always moves far enough,
9840 we set it to INT_MAX and specify MOVE_TO_X. */
9841 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9842 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9843 }
9844
9845 y = it.current_y + it.max_ascent + it.max_descent;
9846
9847 if (!EQ (mode_and_header_line, Qheader_line)
9848 && !EQ (mode_and_header_line, Qt))
9849 /* Do not count the header-line which was counted automatically by
9850 start_display. */
9851 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9852
9853 if (EQ (mode_and_header_line, Qmode_line)
9854 || EQ (mode_and_header_line, Qt))
9855 /* Do count the mode-line which is not included automatically by
9856 start_display. */
9857 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9858
9859 bidi_unshelve_cache (itdata, false);
9860
9861 if (old_b)
9862 set_buffer_internal (old_b);
9863
9864 return Fcons (make_number (x), make_number (y));
9865 }
9866 \f
9867 /***********************************************************************
9868 Messages
9869 ***********************************************************************/
9870
9871 /* Return the number of arguments the format string FORMAT needs. */
9872
9873 static ptrdiff_t
9874 format_nargs (char const *format)
9875 {
9876 ptrdiff_t nargs = 0;
9877 for (char const *p = format; (p = strchr (p, '%')); p++)
9878 if (p[1] == '%')
9879 p++;
9880 else
9881 nargs++;
9882 return nargs;
9883 }
9884
9885 /* Add a message with format string FORMAT and formatted arguments
9886 to *Messages*. */
9887
9888 void
9889 add_to_log (const char *format, ...)
9890 {
9891 va_list ap;
9892 va_start (ap, format);
9893 vadd_to_log (format, ap);
9894 va_end (ap);
9895 }
9896
9897 void
9898 vadd_to_log (char const *format, va_list ap)
9899 {
9900 ptrdiff_t form_nargs = format_nargs (format);
9901 ptrdiff_t nargs = 1 + form_nargs;
9902 Lisp_Object args[10];
9903 eassert (nargs <= ARRAYELTS (args));
9904 AUTO_STRING (args0, format);
9905 args[0] = args0;
9906 for (ptrdiff_t i = 1; i <= nargs; i++)
9907 args[i] = va_arg (ap, Lisp_Object);
9908 Lisp_Object msg = Qnil;
9909 msg = Fformat_message (nargs, args);
9910
9911 ptrdiff_t len = SBYTES (msg) + 1;
9912 USE_SAFE_ALLOCA;
9913 char *buffer = SAFE_ALLOCA (len);
9914 memcpy (buffer, SDATA (msg), len);
9915
9916 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
9917 SAFE_FREE ();
9918 }
9919
9920
9921 /* Output a newline in the *Messages* buffer if "needs" one. */
9922
9923 void
9924 message_log_maybe_newline (void)
9925 {
9926 if (message_log_need_newline)
9927 message_dolog ("", 0, true, false);
9928 }
9929
9930
9931 /* Add a string M of length NBYTES to the message log, optionally
9932 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9933 true, means interpret the contents of M as multibyte. This
9934 function calls low-level routines in order to bypass text property
9935 hooks, etc. which might not be safe to run.
9936
9937 This may GC (insert may run before/after change hooks),
9938 so the buffer M must NOT point to a Lisp string. */
9939
9940 void
9941 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9942 {
9943 const unsigned char *msg = (const unsigned char *) m;
9944
9945 if (!NILP (Vmemory_full))
9946 return;
9947
9948 if (!NILP (Vmessage_log_max))
9949 {
9950 struct buffer *oldbuf;
9951 Lisp_Object oldpoint, oldbegv, oldzv;
9952 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9953 ptrdiff_t point_at_end = 0;
9954 ptrdiff_t zv_at_end = 0;
9955 Lisp_Object old_deactivate_mark;
9956
9957 old_deactivate_mark = Vdeactivate_mark;
9958 oldbuf = current_buffer;
9959
9960 /* Ensure the Messages buffer exists, and switch to it.
9961 If we created it, set the major-mode. */
9962 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
9963 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9964 if (newbuffer
9965 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9966 call0 (intern ("messages-buffer-mode"));
9967
9968 bset_undo_list (current_buffer, Qt);
9969 bset_cache_long_scans (current_buffer, Qnil);
9970
9971 oldpoint = message_dolog_marker1;
9972 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9973 oldbegv = message_dolog_marker2;
9974 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9975 oldzv = message_dolog_marker3;
9976 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9977
9978 if (PT == Z)
9979 point_at_end = 1;
9980 if (ZV == Z)
9981 zv_at_end = 1;
9982
9983 BEGV = BEG;
9984 BEGV_BYTE = BEG_BYTE;
9985 ZV = Z;
9986 ZV_BYTE = Z_BYTE;
9987 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9988
9989 /* Insert the string--maybe converting multibyte to single byte
9990 or vice versa, so that all the text fits the buffer. */
9991 if (multibyte
9992 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9993 {
9994 ptrdiff_t i;
9995 int c, char_bytes;
9996 char work[1];
9997
9998 /* Convert a multibyte string to single-byte
9999 for the *Message* buffer. */
10000 for (i = 0; i < nbytes; i += char_bytes)
10001 {
10002 c = string_char_and_length (msg + i, &char_bytes);
10003 work[0] = CHAR_TO_BYTE8 (c);
10004 insert_1_both (work, 1, 1, true, false, false);
10005 }
10006 }
10007 else if (! multibyte
10008 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10009 {
10010 ptrdiff_t i;
10011 int c, char_bytes;
10012 unsigned char str[MAX_MULTIBYTE_LENGTH];
10013 /* Convert a single-byte string to multibyte
10014 for the *Message* buffer. */
10015 for (i = 0; i < nbytes; i++)
10016 {
10017 c = msg[i];
10018 MAKE_CHAR_MULTIBYTE (c);
10019 char_bytes = CHAR_STRING (c, str);
10020 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10021 }
10022 }
10023 else if (nbytes)
10024 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10025 true, false, false);
10026
10027 if (nlflag)
10028 {
10029 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10030 printmax_t dups;
10031
10032 insert_1_both ("\n", 1, 1, true, false, false);
10033
10034 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10035 this_bol = PT;
10036 this_bol_byte = PT_BYTE;
10037
10038 /* See if this line duplicates the previous one.
10039 If so, combine duplicates. */
10040 if (this_bol > BEG)
10041 {
10042 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10043 prev_bol = PT;
10044 prev_bol_byte = PT_BYTE;
10045
10046 dups = message_log_check_duplicate (prev_bol_byte,
10047 this_bol_byte);
10048 if (dups)
10049 {
10050 del_range_both (prev_bol, prev_bol_byte,
10051 this_bol, this_bol_byte, false);
10052 if (dups > 1)
10053 {
10054 char dupstr[sizeof " [ times]"
10055 + INT_STRLEN_BOUND (printmax_t)];
10056
10057 /* If you change this format, don't forget to also
10058 change message_log_check_duplicate. */
10059 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10060 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10061 insert_1_both (dupstr, duplen, duplen,
10062 true, false, true);
10063 }
10064 }
10065 }
10066
10067 /* If we have more than the desired maximum number of lines
10068 in the *Messages* buffer now, delete the oldest ones.
10069 This is safe because we don't have undo in this buffer. */
10070
10071 if (NATNUMP (Vmessage_log_max))
10072 {
10073 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10074 -XFASTINT (Vmessage_log_max) - 1, false);
10075 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10076 }
10077 }
10078 BEGV = marker_position (oldbegv);
10079 BEGV_BYTE = marker_byte_position (oldbegv);
10080
10081 if (zv_at_end)
10082 {
10083 ZV = Z;
10084 ZV_BYTE = Z_BYTE;
10085 }
10086 else
10087 {
10088 ZV = marker_position (oldzv);
10089 ZV_BYTE = marker_byte_position (oldzv);
10090 }
10091
10092 if (point_at_end)
10093 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10094 else
10095 /* We can't do Fgoto_char (oldpoint) because it will run some
10096 Lisp code. */
10097 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10098 marker_byte_position (oldpoint));
10099
10100 unchain_marker (XMARKER (oldpoint));
10101 unchain_marker (XMARKER (oldbegv));
10102 unchain_marker (XMARKER (oldzv));
10103
10104 /* We called insert_1_both above with its 5th argument (PREPARE)
10105 false, which prevents insert_1_both from calling
10106 prepare_to_modify_buffer, which in turns prevents us from
10107 incrementing windows_or_buffers_changed even if *Messages* is
10108 shown in some window. So we must manually set
10109 windows_or_buffers_changed here to make up for that. */
10110 windows_or_buffers_changed = old_windows_or_buffers_changed;
10111 bset_redisplay (current_buffer);
10112
10113 set_buffer_internal (oldbuf);
10114
10115 message_log_need_newline = !nlflag;
10116 Vdeactivate_mark = old_deactivate_mark;
10117 }
10118 }
10119
10120
10121 /* We are at the end of the buffer after just having inserted a newline.
10122 (Note: We depend on the fact we won't be crossing the gap.)
10123 Check to see if the most recent message looks a lot like the previous one.
10124 Return 0 if different, 1 if the new one should just replace it, or a
10125 value N > 1 if we should also append " [N times]". */
10126
10127 static intmax_t
10128 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10129 {
10130 ptrdiff_t i;
10131 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10132 bool seen_dots = false;
10133 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10134 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10135
10136 for (i = 0; i < len; i++)
10137 {
10138 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10139 seen_dots = true;
10140 if (p1[i] != p2[i])
10141 return seen_dots;
10142 }
10143 p1 += len;
10144 if (*p1 == '\n')
10145 return 2;
10146 if (*p1++ == ' ' && *p1++ == '[')
10147 {
10148 char *pend;
10149 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10150 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10151 return n + 1;
10152 }
10153 return 0;
10154 }
10155 \f
10156
10157 /* Display an echo area message M with a specified length of NBYTES
10158 bytes. The string may include null characters. If M is not a
10159 string, clear out any existing message, and let the mini-buffer
10160 text show through.
10161
10162 This function cancels echoing. */
10163
10164 void
10165 message3 (Lisp_Object m)
10166 {
10167 clear_message (true, true);
10168 cancel_echoing ();
10169
10170 /* First flush out any partial line written with print. */
10171 message_log_maybe_newline ();
10172 if (STRINGP (m))
10173 {
10174 ptrdiff_t nbytes = SBYTES (m);
10175 bool multibyte = STRING_MULTIBYTE (m);
10176 char *buffer;
10177 USE_SAFE_ALLOCA;
10178 SAFE_ALLOCA_STRING (buffer, m);
10179 message_dolog (buffer, nbytes, true, multibyte);
10180 SAFE_FREE ();
10181 }
10182 if (! inhibit_message)
10183 message3_nolog (m);
10184 }
10185
10186 /* Log the message M to stderr. Log an empty line if M is not a string. */
10187
10188 static void
10189 message_to_stderr (Lisp_Object m)
10190 {
10191 if (noninteractive_need_newline)
10192 {
10193 noninteractive_need_newline = false;
10194 fputc ('\n', stderr);
10195 }
10196 if (STRINGP (m))
10197 {
10198 Lisp_Object s = ENCODE_SYSTEM (m);
10199 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10200 }
10201 if (!cursor_in_echo_area)
10202 fputc ('\n', stderr);
10203 fflush (stderr);
10204 }
10205
10206 /* The non-logging version of message3.
10207 This does not cancel echoing, because it is used for echoing.
10208 Perhaps we need to make a separate function for echoing
10209 and make this cancel echoing. */
10210
10211 void
10212 message3_nolog (Lisp_Object m)
10213 {
10214 struct frame *sf = SELECTED_FRAME ();
10215
10216 if (FRAME_INITIAL_P (sf))
10217 message_to_stderr (m);
10218 /* Error messages get reported properly by cmd_error, so this must be just an
10219 informative message; if the frame hasn't really been initialized yet, just
10220 toss it. */
10221 else if (INTERACTIVE && sf->glyphs_initialized_p)
10222 {
10223 /* Get the frame containing the mini-buffer
10224 that the selected frame is using. */
10225 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10226 Lisp_Object frame = XWINDOW (mini_window)->frame;
10227 struct frame *f = XFRAME (frame);
10228
10229 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10230 Fmake_frame_visible (frame);
10231
10232 if (STRINGP (m) && SCHARS (m) > 0)
10233 {
10234 set_message (m);
10235 if (minibuffer_auto_raise)
10236 Fraise_frame (frame);
10237 /* Assume we are not echoing.
10238 (If we are, echo_now will override this.) */
10239 echo_message_buffer = Qnil;
10240 }
10241 else
10242 clear_message (true, true);
10243
10244 do_pending_window_change (false);
10245 echo_area_display (true);
10246 do_pending_window_change (false);
10247 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10248 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10249 }
10250 }
10251
10252
10253 /* Display a null-terminated echo area message M. If M is 0, clear
10254 out any existing message, and let the mini-buffer text show through.
10255
10256 The buffer M must continue to exist until after the echo area gets
10257 cleared or some other message gets displayed there. Do not pass
10258 text that is stored in a Lisp string. Do not pass text in a buffer
10259 that was alloca'd. */
10260
10261 void
10262 message1 (const char *m)
10263 {
10264 message3 (m ? build_unibyte_string (m) : Qnil);
10265 }
10266
10267
10268 /* The non-logging counterpart of message1. */
10269
10270 void
10271 message1_nolog (const char *m)
10272 {
10273 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10274 }
10275
10276 /* Display a message M which contains a single %s
10277 which gets replaced with STRING. */
10278
10279 void
10280 message_with_string (const char *m, Lisp_Object string, bool log)
10281 {
10282 CHECK_STRING (string);
10283
10284 bool need_message;
10285 if (noninteractive)
10286 need_message = !!m;
10287 else if (!INTERACTIVE)
10288 need_message = false;
10289 else
10290 {
10291 /* The frame whose minibuffer we're going to display the message on.
10292 It may be larger than the selected frame, so we need
10293 to use its buffer, not the selected frame's buffer. */
10294 Lisp_Object mini_window;
10295 struct frame *f, *sf = SELECTED_FRAME ();
10296
10297 /* Get the frame containing the minibuffer
10298 that the selected frame is using. */
10299 mini_window = FRAME_MINIBUF_WINDOW (sf);
10300 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10301
10302 /* Error messages get reported properly by cmd_error, so this must be
10303 just an informative message; if the frame hasn't really been
10304 initialized yet, just toss it. */
10305 need_message = f->glyphs_initialized_p;
10306 }
10307
10308 if (need_message)
10309 {
10310 AUTO_STRING (fmt, m);
10311 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10312
10313 if (noninteractive)
10314 message_to_stderr (msg);
10315 else
10316 {
10317 if (log)
10318 message3 (msg);
10319 else
10320 message3_nolog (msg);
10321
10322 /* Print should start at the beginning of the message
10323 buffer next time. */
10324 message_buf_print = false;
10325 }
10326 }
10327 }
10328
10329
10330 /* Dump an informative message to the minibuf. If M is 0, clear out
10331 any existing message, and let the mini-buffer text show through.
10332
10333 The message must be safe ASCII and the format must not contain ` or
10334 '. If your message and format do not fit into this category,
10335 convert your arguments to Lisp objects and use Fmessage instead. */
10336
10337 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10338 vmessage (const char *m, va_list ap)
10339 {
10340 if (noninteractive)
10341 {
10342 if (m)
10343 {
10344 if (noninteractive_need_newline)
10345 putc ('\n', stderr);
10346 noninteractive_need_newline = false;
10347 vfprintf (stderr, m, ap);
10348 if (!cursor_in_echo_area)
10349 fprintf (stderr, "\n");
10350 fflush (stderr);
10351 }
10352 }
10353 else if (INTERACTIVE)
10354 {
10355 /* The frame whose mini-buffer we're going to display the message
10356 on. It may be larger than the selected frame, so we need to
10357 use its buffer, not the selected frame's buffer. */
10358 Lisp_Object mini_window;
10359 struct frame *f, *sf = SELECTED_FRAME ();
10360
10361 /* Get the frame containing the mini-buffer
10362 that the selected frame is using. */
10363 mini_window = FRAME_MINIBUF_WINDOW (sf);
10364 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10365
10366 /* Error messages get reported properly by cmd_error, so this must be
10367 just an informative message; if the frame hasn't really been
10368 initialized yet, just toss it. */
10369 if (f->glyphs_initialized_p)
10370 {
10371 if (m)
10372 {
10373 ptrdiff_t len;
10374 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10375 USE_SAFE_ALLOCA;
10376 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10377
10378 len = doprnt (message_buf, maxsize, m, 0, ap);
10379
10380 message3 (make_string (message_buf, len));
10381 SAFE_FREE ();
10382 }
10383 else
10384 message1 (0);
10385
10386 /* Print should start at the beginning of the message
10387 buffer next time. */
10388 message_buf_print = false;
10389 }
10390 }
10391 }
10392
10393 void
10394 message (const char *m, ...)
10395 {
10396 va_list ap;
10397 va_start (ap, m);
10398 vmessage (m, ap);
10399 va_end (ap);
10400 }
10401
10402
10403 /* Display the current message in the current mini-buffer. This is
10404 only called from error handlers in process.c, and is not time
10405 critical. */
10406
10407 void
10408 update_echo_area (void)
10409 {
10410 if (!NILP (echo_area_buffer[0]))
10411 {
10412 Lisp_Object string;
10413 string = Fcurrent_message ();
10414 message3 (string);
10415 }
10416 }
10417
10418
10419 /* Make sure echo area buffers in `echo_buffers' are live.
10420 If they aren't, make new ones. */
10421
10422 static void
10423 ensure_echo_area_buffers (void)
10424 {
10425 int i;
10426
10427 for (i = 0; i < 2; ++i)
10428 if (!BUFFERP (echo_buffer[i])
10429 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10430 {
10431 char name[30];
10432 Lisp_Object old_buffer;
10433 int j;
10434
10435 old_buffer = echo_buffer[i];
10436 echo_buffer[i] = Fget_buffer_create
10437 (make_formatted_string (name, " *Echo Area %d*", i));
10438 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10439 /* to force word wrap in echo area -
10440 it was decided to postpone this*/
10441 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10442
10443 for (j = 0; j < 2; ++j)
10444 if (EQ (old_buffer, echo_area_buffer[j]))
10445 echo_area_buffer[j] = echo_buffer[i];
10446 }
10447 }
10448
10449
10450 /* Call FN with args A1..A2 with either the current or last displayed
10451 echo_area_buffer as current buffer.
10452
10453 WHICH zero means use the current message buffer
10454 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10455 from echo_buffer[] and clear it.
10456
10457 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10458 suitable buffer from echo_buffer[] and clear it.
10459
10460 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10461 that the current message becomes the last displayed one, make
10462 choose a suitable buffer for echo_area_buffer[0], and clear it.
10463
10464 Value is what FN returns. */
10465
10466 static bool
10467 with_echo_area_buffer (struct window *w, int which,
10468 bool (*fn) (ptrdiff_t, Lisp_Object),
10469 ptrdiff_t a1, Lisp_Object a2)
10470 {
10471 Lisp_Object buffer;
10472 bool this_one, the_other, clear_buffer_p, rc;
10473 ptrdiff_t count = SPECPDL_INDEX ();
10474
10475 /* If buffers aren't live, make new ones. */
10476 ensure_echo_area_buffers ();
10477
10478 clear_buffer_p = false;
10479
10480 if (which == 0)
10481 this_one = false, the_other = true;
10482 else if (which > 0)
10483 this_one = true, the_other = false;
10484 else
10485 {
10486 this_one = false, the_other = true;
10487 clear_buffer_p = true;
10488
10489 /* We need a fresh one in case the current echo buffer equals
10490 the one containing the last displayed echo area message. */
10491 if (!NILP (echo_area_buffer[this_one])
10492 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10493 echo_area_buffer[this_one] = Qnil;
10494 }
10495
10496 /* Choose a suitable buffer from echo_buffer[] is we don't
10497 have one. */
10498 if (NILP (echo_area_buffer[this_one]))
10499 {
10500 echo_area_buffer[this_one]
10501 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10502 ? echo_buffer[the_other]
10503 : echo_buffer[this_one]);
10504 clear_buffer_p = true;
10505 }
10506
10507 buffer = echo_area_buffer[this_one];
10508
10509 /* Don't get confused by reusing the buffer used for echoing
10510 for a different purpose. */
10511 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10512 cancel_echoing ();
10513
10514 record_unwind_protect (unwind_with_echo_area_buffer,
10515 with_echo_area_buffer_unwind_data (w));
10516
10517 /* Make the echo area buffer current. Note that for display
10518 purposes, it is not necessary that the displayed window's buffer
10519 == current_buffer, except for text property lookup. So, let's
10520 only set that buffer temporarily here without doing a full
10521 Fset_window_buffer. We must also change w->pointm, though,
10522 because otherwise an assertions in unshow_buffer fails, and Emacs
10523 aborts. */
10524 set_buffer_internal_1 (XBUFFER (buffer));
10525 if (w)
10526 {
10527 wset_buffer (w, buffer);
10528 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10529 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10530 }
10531
10532 bset_undo_list (current_buffer, Qt);
10533 bset_read_only (current_buffer, Qnil);
10534 specbind (Qinhibit_read_only, Qt);
10535 specbind (Qinhibit_modification_hooks, Qt);
10536
10537 if (clear_buffer_p && Z > BEG)
10538 del_range (BEG, Z);
10539
10540 eassert (BEGV >= BEG);
10541 eassert (ZV <= Z && ZV >= BEGV);
10542
10543 rc = fn (a1, a2);
10544
10545 eassert (BEGV >= BEG);
10546 eassert (ZV <= Z && ZV >= BEGV);
10547
10548 unbind_to (count, Qnil);
10549 return rc;
10550 }
10551
10552
10553 /* Save state that should be preserved around the call to the function
10554 FN called in with_echo_area_buffer. */
10555
10556 static Lisp_Object
10557 with_echo_area_buffer_unwind_data (struct window *w)
10558 {
10559 int i = 0;
10560 Lisp_Object vector, tmp;
10561
10562 /* Reduce consing by keeping one vector in
10563 Vwith_echo_area_save_vector. */
10564 vector = Vwith_echo_area_save_vector;
10565 Vwith_echo_area_save_vector = Qnil;
10566
10567 if (NILP (vector))
10568 vector = Fmake_vector (make_number (11), Qnil);
10569
10570 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10571 ASET (vector, i, Vdeactivate_mark); ++i;
10572 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10573
10574 if (w)
10575 {
10576 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10577 ASET (vector, i, w->contents); ++i;
10578 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10579 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10580 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10581 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10582 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10583 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10584 }
10585 else
10586 {
10587 int end = i + 8;
10588 for (; i < end; ++i)
10589 ASET (vector, i, Qnil);
10590 }
10591
10592 eassert (i == ASIZE (vector));
10593 return vector;
10594 }
10595
10596
10597 /* Restore global state from VECTOR which was created by
10598 with_echo_area_buffer_unwind_data. */
10599
10600 static void
10601 unwind_with_echo_area_buffer (Lisp_Object vector)
10602 {
10603 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10604 Vdeactivate_mark = AREF (vector, 1);
10605 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10606
10607 if (WINDOWP (AREF (vector, 3)))
10608 {
10609 struct window *w;
10610 Lisp_Object buffer;
10611
10612 w = XWINDOW (AREF (vector, 3));
10613 buffer = AREF (vector, 4);
10614
10615 wset_buffer (w, buffer);
10616 set_marker_both (w->pointm, buffer,
10617 XFASTINT (AREF (vector, 5)),
10618 XFASTINT (AREF (vector, 6)));
10619 set_marker_both (w->old_pointm, buffer,
10620 XFASTINT (AREF (vector, 7)),
10621 XFASTINT (AREF (vector, 8)));
10622 set_marker_both (w->start, buffer,
10623 XFASTINT (AREF (vector, 9)),
10624 XFASTINT (AREF (vector, 10)));
10625 }
10626
10627 Vwith_echo_area_save_vector = vector;
10628 }
10629
10630
10631 /* Set up the echo area for use by print functions. MULTIBYTE_P
10632 means we will print multibyte. */
10633
10634 void
10635 setup_echo_area_for_printing (bool multibyte_p)
10636 {
10637 /* If we can't find an echo area any more, exit. */
10638 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10639 Fkill_emacs (Qnil);
10640
10641 ensure_echo_area_buffers ();
10642
10643 if (!message_buf_print)
10644 {
10645 /* A message has been output since the last time we printed.
10646 Choose a fresh echo area buffer. */
10647 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10648 echo_area_buffer[0] = echo_buffer[1];
10649 else
10650 echo_area_buffer[0] = echo_buffer[0];
10651
10652 /* Switch to that buffer and clear it. */
10653 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10654 bset_truncate_lines (current_buffer, Qnil);
10655
10656 if (Z > BEG)
10657 {
10658 ptrdiff_t count = SPECPDL_INDEX ();
10659 specbind (Qinhibit_read_only, Qt);
10660 /* Note that undo recording is always disabled. */
10661 del_range (BEG, Z);
10662 unbind_to (count, Qnil);
10663 }
10664 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10665
10666 /* Set up the buffer for the multibyteness we need. */
10667 if (multibyte_p
10668 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10669 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10670
10671 /* Raise the frame containing the echo area. */
10672 if (minibuffer_auto_raise)
10673 {
10674 struct frame *sf = SELECTED_FRAME ();
10675 Lisp_Object mini_window;
10676 mini_window = FRAME_MINIBUF_WINDOW (sf);
10677 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10678 }
10679
10680 message_log_maybe_newline ();
10681 message_buf_print = true;
10682 }
10683 else
10684 {
10685 if (NILP (echo_area_buffer[0]))
10686 {
10687 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10688 echo_area_buffer[0] = echo_buffer[1];
10689 else
10690 echo_area_buffer[0] = echo_buffer[0];
10691 }
10692
10693 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10694 {
10695 /* Someone switched buffers between print requests. */
10696 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10697 bset_truncate_lines (current_buffer, Qnil);
10698 }
10699 }
10700 }
10701
10702
10703 /* Display an echo area message in window W. Value is true if W's
10704 height is changed. If display_last_displayed_message_p,
10705 display the message that was last displayed, otherwise
10706 display the current message. */
10707
10708 static bool
10709 display_echo_area (struct window *w)
10710 {
10711 bool no_message_p, window_height_changed_p;
10712
10713 /* Temporarily disable garbage collections while displaying the echo
10714 area. This is done because a GC can print a message itself.
10715 That message would modify the echo area buffer's contents while a
10716 redisplay of the buffer is going on, and seriously confuse
10717 redisplay. */
10718 ptrdiff_t count = inhibit_garbage_collection ();
10719
10720 /* If there is no message, we must call display_echo_area_1
10721 nevertheless because it resizes the window. But we will have to
10722 reset the echo_area_buffer in question to nil at the end because
10723 with_echo_area_buffer will sets it to an empty buffer. */
10724 bool i = display_last_displayed_message_p;
10725 no_message_p = NILP (echo_area_buffer[i]);
10726
10727 window_height_changed_p
10728 = with_echo_area_buffer (w, display_last_displayed_message_p,
10729 display_echo_area_1,
10730 (intptr_t) w, Qnil);
10731
10732 if (no_message_p)
10733 echo_area_buffer[i] = Qnil;
10734
10735 unbind_to (count, Qnil);
10736 return window_height_changed_p;
10737 }
10738
10739
10740 /* Helper for display_echo_area. Display the current buffer which
10741 contains the current echo area message in window W, a mini-window,
10742 a pointer to which is passed in A1. A2..A4 are currently not used.
10743 Change the height of W so that all of the message is displayed.
10744 Value is true if height of W was changed. */
10745
10746 static bool
10747 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10748 {
10749 intptr_t i1 = a1;
10750 struct window *w = (struct window *) i1;
10751 Lisp_Object window;
10752 struct text_pos start;
10753
10754 /* We are about to enter redisplay without going through
10755 redisplay_internal, so we need to forget these faces by hand
10756 here. */
10757 forget_escape_and_glyphless_faces ();
10758
10759 /* Do this before displaying, so that we have a large enough glyph
10760 matrix for the display. If we can't get enough space for the
10761 whole text, display the last N lines. That works by setting w->start. */
10762 bool window_height_changed_p = resize_mini_window (w, false);
10763
10764 /* Use the starting position chosen by resize_mini_window. */
10765 SET_TEXT_POS_FROM_MARKER (start, w->start);
10766
10767 /* Display. */
10768 clear_glyph_matrix (w->desired_matrix);
10769 XSETWINDOW (window, w);
10770 try_window (window, start, 0);
10771
10772 return window_height_changed_p;
10773 }
10774
10775
10776 /* Resize the echo area window to exactly the size needed for the
10777 currently displayed message, if there is one. If a mini-buffer
10778 is active, don't shrink it. */
10779
10780 void
10781 resize_echo_area_exactly (void)
10782 {
10783 if (BUFFERP (echo_area_buffer[0])
10784 && WINDOWP (echo_area_window))
10785 {
10786 struct window *w = XWINDOW (echo_area_window);
10787 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10788 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10789 (intptr_t) w, resize_exactly);
10790 if (resized_p)
10791 {
10792 windows_or_buffers_changed = 42;
10793 update_mode_lines = 30;
10794 redisplay_internal ();
10795 }
10796 }
10797 }
10798
10799
10800 /* Callback function for with_echo_area_buffer, when used from
10801 resize_echo_area_exactly. A1 contains a pointer to the window to
10802 resize, EXACTLY non-nil means resize the mini-window exactly to the
10803 size of the text displayed. A3 and A4 are not used. Value is what
10804 resize_mini_window returns. */
10805
10806 static bool
10807 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10808 {
10809 intptr_t i1 = a1;
10810 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10811 }
10812
10813
10814 /* Resize mini-window W to fit the size of its contents. EXACT_P
10815 means size the window exactly to the size needed. Otherwise, it's
10816 only enlarged until W's buffer is empty.
10817
10818 Set W->start to the right place to begin display. If the whole
10819 contents fit, start at the beginning. Otherwise, start so as
10820 to make the end of the contents appear. This is particularly
10821 important for y-or-n-p, but seems desirable generally.
10822
10823 Value is true if the window height has been changed. */
10824
10825 bool
10826 resize_mini_window (struct window *w, bool exact_p)
10827 {
10828 struct frame *f = XFRAME (w->frame);
10829 bool window_height_changed_p = false;
10830
10831 eassert (MINI_WINDOW_P (w));
10832
10833 /* By default, start display at the beginning. */
10834 set_marker_both (w->start, w->contents,
10835 BUF_BEGV (XBUFFER (w->contents)),
10836 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10837
10838 /* Don't resize windows while redisplaying a window; it would
10839 confuse redisplay functions when the size of the window they are
10840 displaying changes from under them. Such a resizing can happen,
10841 for instance, when which-func prints a long message while
10842 we are running fontification-functions. We're running these
10843 functions with safe_call which binds inhibit-redisplay to t. */
10844 if (!NILP (Vinhibit_redisplay))
10845 return false;
10846
10847 /* Nil means don't try to resize. */
10848 if (NILP (Vresize_mini_windows)
10849 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10850 return false;
10851
10852 if (!FRAME_MINIBUF_ONLY_P (f))
10853 {
10854 struct it it;
10855 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10856 + WINDOW_PIXEL_HEIGHT (w));
10857 int unit = FRAME_LINE_HEIGHT (f);
10858 int height, max_height;
10859 struct text_pos start;
10860 struct buffer *old_current_buffer = NULL;
10861
10862 if (current_buffer != XBUFFER (w->contents))
10863 {
10864 old_current_buffer = current_buffer;
10865 set_buffer_internal (XBUFFER (w->contents));
10866 }
10867
10868 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10869
10870 /* Compute the max. number of lines specified by the user. */
10871 if (FLOATP (Vmax_mini_window_height))
10872 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10873 else if (INTEGERP (Vmax_mini_window_height))
10874 max_height = XINT (Vmax_mini_window_height) * unit;
10875 else
10876 max_height = total_height / 4;
10877
10878 /* Correct that max. height if it's bogus. */
10879 max_height = clip_to_bounds (unit, max_height, total_height);
10880
10881 /* Find out the height of the text in the window. */
10882 if (it.line_wrap == TRUNCATE)
10883 height = unit;
10884 else
10885 {
10886 last_height = 0;
10887 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10888 if (it.max_ascent == 0 && it.max_descent == 0)
10889 height = it.current_y + last_height;
10890 else
10891 height = it.current_y + it.max_ascent + it.max_descent;
10892 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10893 }
10894
10895 /* Compute a suitable window start. */
10896 if (height > max_height)
10897 {
10898 height = (max_height / unit) * unit;
10899 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10900 move_it_vertically_backward (&it, height - unit);
10901 start = it.current.pos;
10902 }
10903 else
10904 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10905 SET_MARKER_FROM_TEXT_POS (w->start, start);
10906
10907 if (EQ (Vresize_mini_windows, Qgrow_only))
10908 {
10909 /* Let it grow only, until we display an empty message, in which
10910 case the window shrinks again. */
10911 if (height > WINDOW_PIXEL_HEIGHT (w))
10912 {
10913 int old_height = WINDOW_PIXEL_HEIGHT (w);
10914
10915 FRAME_WINDOWS_FROZEN (f) = true;
10916 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10917 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10918 }
10919 else if (height < WINDOW_PIXEL_HEIGHT (w)
10920 && (exact_p || BEGV == ZV))
10921 {
10922 int old_height = WINDOW_PIXEL_HEIGHT (w);
10923
10924 FRAME_WINDOWS_FROZEN (f) = false;
10925 shrink_mini_window (w, true);
10926 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10927 }
10928 }
10929 else
10930 {
10931 /* Always resize to exact size needed. */
10932 if (height > WINDOW_PIXEL_HEIGHT (w))
10933 {
10934 int old_height = WINDOW_PIXEL_HEIGHT (w);
10935
10936 FRAME_WINDOWS_FROZEN (f) = true;
10937 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10938 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10939 }
10940 else if (height < WINDOW_PIXEL_HEIGHT (w))
10941 {
10942 int old_height = WINDOW_PIXEL_HEIGHT (w);
10943
10944 FRAME_WINDOWS_FROZEN (f) = false;
10945 shrink_mini_window (w, true);
10946
10947 if (height)
10948 {
10949 FRAME_WINDOWS_FROZEN (f) = true;
10950 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10951 }
10952
10953 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10954 }
10955 }
10956
10957 if (old_current_buffer)
10958 set_buffer_internal (old_current_buffer);
10959 }
10960
10961 return window_height_changed_p;
10962 }
10963
10964
10965 /* Value is the current message, a string, or nil if there is no
10966 current message. */
10967
10968 Lisp_Object
10969 current_message (void)
10970 {
10971 Lisp_Object msg;
10972
10973 if (!BUFFERP (echo_area_buffer[0]))
10974 msg = Qnil;
10975 else
10976 {
10977 with_echo_area_buffer (0, 0, current_message_1,
10978 (intptr_t) &msg, Qnil);
10979 if (NILP (msg))
10980 echo_area_buffer[0] = Qnil;
10981 }
10982
10983 return msg;
10984 }
10985
10986
10987 static bool
10988 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10989 {
10990 intptr_t i1 = a1;
10991 Lisp_Object *msg = (Lisp_Object *) i1;
10992
10993 if (Z > BEG)
10994 *msg = make_buffer_string (BEG, Z, true);
10995 else
10996 *msg = Qnil;
10997 return false;
10998 }
10999
11000
11001 /* Push the current message on Vmessage_stack for later restoration
11002 by restore_message. Value is true if the current message isn't
11003 empty. This is a relatively infrequent operation, so it's not
11004 worth optimizing. */
11005
11006 bool
11007 push_message (void)
11008 {
11009 Lisp_Object msg = current_message ();
11010 Vmessage_stack = Fcons (msg, Vmessage_stack);
11011 return STRINGP (msg);
11012 }
11013
11014
11015 /* Restore message display from the top of Vmessage_stack. */
11016
11017 void
11018 restore_message (void)
11019 {
11020 eassert (CONSP (Vmessage_stack));
11021 message3_nolog (XCAR (Vmessage_stack));
11022 }
11023
11024
11025 /* Handler for unwind-protect calling pop_message. */
11026
11027 void
11028 pop_message_unwind (void)
11029 {
11030 /* Pop the top-most entry off Vmessage_stack. */
11031 eassert (CONSP (Vmessage_stack));
11032 Vmessage_stack = XCDR (Vmessage_stack);
11033 }
11034
11035
11036 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11037 exits. If the stack is not empty, we have a missing pop_message
11038 somewhere. */
11039
11040 void
11041 check_message_stack (void)
11042 {
11043 if (!NILP (Vmessage_stack))
11044 emacs_abort ();
11045 }
11046
11047
11048 /* Truncate to NCHARS what will be displayed in the echo area the next
11049 time we display it---but don't redisplay it now. */
11050
11051 void
11052 truncate_echo_area (ptrdiff_t nchars)
11053 {
11054 if (nchars == 0)
11055 echo_area_buffer[0] = Qnil;
11056 else if (!noninteractive
11057 && INTERACTIVE
11058 && !NILP (echo_area_buffer[0]))
11059 {
11060 struct frame *sf = SELECTED_FRAME ();
11061 /* Error messages get reported properly by cmd_error, so this must be
11062 just an informative message; if the frame hasn't really been
11063 initialized yet, just toss it. */
11064 if (sf->glyphs_initialized_p)
11065 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11066 }
11067 }
11068
11069
11070 /* Helper function for truncate_echo_area. Truncate the current
11071 message to at most NCHARS characters. */
11072
11073 static bool
11074 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11075 {
11076 if (BEG + nchars < Z)
11077 del_range (BEG + nchars, Z);
11078 if (Z == BEG)
11079 echo_area_buffer[0] = Qnil;
11080 return false;
11081 }
11082
11083 /* Set the current message to STRING. */
11084
11085 static void
11086 set_message (Lisp_Object string)
11087 {
11088 eassert (STRINGP (string));
11089
11090 message_enable_multibyte = STRING_MULTIBYTE (string);
11091
11092 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11093 message_buf_print = false;
11094 help_echo_showing_p = false;
11095
11096 if (STRINGP (Vdebug_on_message)
11097 && STRINGP (string)
11098 && fast_string_match (Vdebug_on_message, string) >= 0)
11099 call_debugger (list2 (Qerror, string));
11100 }
11101
11102
11103 /* Helper function for set_message. First argument is ignored and second
11104 argument has the same meaning as for set_message.
11105 This function is called with the echo area buffer being current. */
11106
11107 static bool
11108 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11109 {
11110 eassert (STRINGP (string));
11111
11112 /* Change multibyteness of the echo buffer appropriately. */
11113 if (message_enable_multibyte
11114 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11115 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11116
11117 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11118 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11119 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11120
11121 /* Insert new message at BEG. */
11122 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11123
11124 /* This function takes care of single/multibyte conversion.
11125 We just have to ensure that the echo area buffer has the right
11126 setting of enable_multibyte_characters. */
11127 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11128
11129 return false;
11130 }
11131
11132
11133 /* Clear messages. CURRENT_P means clear the current message.
11134 LAST_DISPLAYED_P means clear the message last displayed. */
11135
11136 void
11137 clear_message (bool current_p, bool last_displayed_p)
11138 {
11139 if (current_p)
11140 {
11141 echo_area_buffer[0] = Qnil;
11142 message_cleared_p = true;
11143 }
11144
11145 if (last_displayed_p)
11146 echo_area_buffer[1] = Qnil;
11147
11148 message_buf_print = false;
11149 }
11150
11151 /* Clear garbaged frames.
11152
11153 This function is used where the old redisplay called
11154 redraw_garbaged_frames which in turn called redraw_frame which in
11155 turn called clear_frame. The call to clear_frame was a source of
11156 flickering. I believe a clear_frame is not necessary. It should
11157 suffice in the new redisplay to invalidate all current matrices,
11158 and ensure a complete redisplay of all windows. */
11159
11160 static void
11161 clear_garbaged_frames (void)
11162 {
11163 if (frame_garbaged)
11164 {
11165 Lisp_Object tail, frame;
11166
11167 FOR_EACH_FRAME (tail, frame)
11168 {
11169 struct frame *f = XFRAME (frame);
11170
11171 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11172 {
11173 if (f->resized_p)
11174 redraw_frame (f);
11175 else
11176 clear_current_matrices (f);
11177 fset_redisplay (f);
11178 f->garbaged = false;
11179 f->resized_p = false;
11180 }
11181 }
11182
11183 frame_garbaged = false;
11184 }
11185 }
11186
11187
11188 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11189 selected_frame. */
11190
11191 static void
11192 echo_area_display (bool update_frame_p)
11193 {
11194 Lisp_Object mini_window;
11195 struct window *w;
11196 struct frame *f;
11197 bool window_height_changed_p = false;
11198 struct frame *sf = SELECTED_FRAME ();
11199
11200 mini_window = FRAME_MINIBUF_WINDOW (sf);
11201 w = XWINDOW (mini_window);
11202 f = XFRAME (WINDOW_FRAME (w));
11203
11204 /* Don't display if frame is invisible or not yet initialized. */
11205 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11206 return;
11207
11208 #ifdef HAVE_WINDOW_SYSTEM
11209 /* When Emacs starts, selected_frame may be the initial terminal
11210 frame. If we let this through, a message would be displayed on
11211 the terminal. */
11212 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11213 return;
11214 #endif /* HAVE_WINDOW_SYSTEM */
11215
11216 /* Redraw garbaged frames. */
11217 clear_garbaged_frames ();
11218
11219 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11220 {
11221 echo_area_window = mini_window;
11222 window_height_changed_p = display_echo_area (w);
11223 w->must_be_updated_p = true;
11224
11225 /* Update the display, unless called from redisplay_internal.
11226 Also don't update the screen during redisplay itself. The
11227 update will happen at the end of redisplay, and an update
11228 here could cause confusion. */
11229 if (update_frame_p && !redisplaying_p)
11230 {
11231 int n = 0;
11232
11233 /* If the display update has been interrupted by pending
11234 input, update mode lines in the frame. Due to the
11235 pending input, it might have been that redisplay hasn't
11236 been called, so that mode lines above the echo area are
11237 garbaged. This looks odd, so we prevent it here. */
11238 if (!display_completed)
11239 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11240
11241 if (window_height_changed_p
11242 /* Don't do this if Emacs is shutting down. Redisplay
11243 needs to run hooks. */
11244 && !NILP (Vrun_hooks))
11245 {
11246 /* Must update other windows. Likewise as in other
11247 cases, don't let this update be interrupted by
11248 pending input. */
11249 ptrdiff_t count = SPECPDL_INDEX ();
11250 specbind (Qredisplay_dont_pause, Qt);
11251 fset_redisplay (f);
11252 redisplay_internal ();
11253 unbind_to (count, Qnil);
11254 }
11255 else if (FRAME_WINDOW_P (f) && n == 0)
11256 {
11257 /* Window configuration is the same as before.
11258 Can do with a display update of the echo area,
11259 unless we displayed some mode lines. */
11260 update_single_window (w);
11261 flush_frame (f);
11262 }
11263 else
11264 update_frame (f, true, true);
11265
11266 /* If cursor is in the echo area, make sure that the next
11267 redisplay displays the minibuffer, so that the cursor will
11268 be replaced with what the minibuffer wants. */
11269 if (cursor_in_echo_area)
11270 wset_redisplay (XWINDOW (mini_window));
11271 }
11272 }
11273 else if (!EQ (mini_window, selected_window))
11274 wset_redisplay (XWINDOW (mini_window));
11275
11276 /* Last displayed message is now the current message. */
11277 echo_area_buffer[1] = echo_area_buffer[0];
11278 /* Inform read_char that we're not echoing. */
11279 echo_message_buffer = Qnil;
11280
11281 /* Prevent redisplay optimization in redisplay_internal by resetting
11282 this_line_start_pos. This is done because the mini-buffer now
11283 displays the message instead of its buffer text. */
11284 if (EQ (mini_window, selected_window))
11285 CHARPOS (this_line_start_pos) = 0;
11286
11287 if (window_height_changed_p)
11288 {
11289 fset_redisplay (f);
11290
11291 /* If window configuration was changed, frames may have been
11292 marked garbaged. Clear them or we will experience
11293 surprises wrt scrolling.
11294 FIXME: How/why/when? */
11295 clear_garbaged_frames ();
11296 }
11297 }
11298
11299 /* True if W's buffer was changed but not saved. */
11300
11301 static bool
11302 window_buffer_changed (struct window *w)
11303 {
11304 struct buffer *b = XBUFFER (w->contents);
11305
11306 eassert (BUFFER_LIVE_P (b));
11307
11308 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11309 }
11310
11311 /* True if W has %c in its mode line and mode line should be updated. */
11312
11313 static bool
11314 mode_line_update_needed (struct window *w)
11315 {
11316 return (w->column_number_displayed != -1
11317 && !(PT == w->last_point && !window_outdated (w))
11318 && (w->column_number_displayed != current_column ()));
11319 }
11320
11321 /* True if window start of W is frozen and may not be changed during
11322 redisplay. */
11323
11324 static bool
11325 window_frozen_p (struct window *w)
11326 {
11327 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11328 {
11329 Lisp_Object window;
11330
11331 XSETWINDOW (window, w);
11332 if (MINI_WINDOW_P (w))
11333 return false;
11334 else if (EQ (window, selected_window))
11335 return false;
11336 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11337 && EQ (window, Vminibuf_scroll_window))
11338 /* This special window can't be frozen too. */
11339 return false;
11340 else
11341 return true;
11342 }
11343 return false;
11344 }
11345
11346 /***********************************************************************
11347 Mode Lines and Frame Titles
11348 ***********************************************************************/
11349
11350 /* A buffer for constructing non-propertized mode-line strings and
11351 frame titles in it; allocated from the heap in init_xdisp and
11352 resized as needed in store_mode_line_noprop_char. */
11353
11354 static char *mode_line_noprop_buf;
11355
11356 /* The buffer's end, and a current output position in it. */
11357
11358 static char *mode_line_noprop_buf_end;
11359 static char *mode_line_noprop_ptr;
11360
11361 #define MODE_LINE_NOPROP_LEN(start) \
11362 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11363
11364 static enum {
11365 MODE_LINE_DISPLAY = 0,
11366 MODE_LINE_TITLE,
11367 MODE_LINE_NOPROP,
11368 MODE_LINE_STRING
11369 } mode_line_target;
11370
11371 /* Alist that caches the results of :propertize.
11372 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11373 static Lisp_Object mode_line_proptrans_alist;
11374
11375 /* List of strings making up the mode-line. */
11376 static Lisp_Object mode_line_string_list;
11377
11378 /* Base face property when building propertized mode line string. */
11379 static Lisp_Object mode_line_string_face;
11380 static Lisp_Object mode_line_string_face_prop;
11381
11382
11383 /* Unwind data for mode line strings */
11384
11385 static Lisp_Object Vmode_line_unwind_vector;
11386
11387 static Lisp_Object
11388 format_mode_line_unwind_data (struct frame *target_frame,
11389 struct buffer *obuf,
11390 Lisp_Object owin,
11391 bool save_proptrans)
11392 {
11393 Lisp_Object vector, tmp;
11394
11395 /* Reduce consing by keeping one vector in
11396 Vwith_echo_area_save_vector. */
11397 vector = Vmode_line_unwind_vector;
11398 Vmode_line_unwind_vector = Qnil;
11399
11400 if (NILP (vector))
11401 vector = Fmake_vector (make_number (10), Qnil);
11402
11403 ASET (vector, 0, make_number (mode_line_target));
11404 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11405 ASET (vector, 2, mode_line_string_list);
11406 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11407 ASET (vector, 4, mode_line_string_face);
11408 ASET (vector, 5, mode_line_string_face_prop);
11409
11410 if (obuf)
11411 XSETBUFFER (tmp, obuf);
11412 else
11413 tmp = Qnil;
11414 ASET (vector, 6, tmp);
11415 ASET (vector, 7, owin);
11416 if (target_frame)
11417 {
11418 /* Similarly to `with-selected-window', if the operation selects
11419 a window on another frame, we must restore that frame's
11420 selected window, and (for a tty) the top-frame. */
11421 ASET (vector, 8, target_frame->selected_window);
11422 if (FRAME_TERMCAP_P (target_frame))
11423 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11424 }
11425
11426 return vector;
11427 }
11428
11429 static void
11430 unwind_format_mode_line (Lisp_Object vector)
11431 {
11432 Lisp_Object old_window = AREF (vector, 7);
11433 Lisp_Object target_frame_window = AREF (vector, 8);
11434 Lisp_Object old_top_frame = AREF (vector, 9);
11435
11436 mode_line_target = XINT (AREF (vector, 0));
11437 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11438 mode_line_string_list = AREF (vector, 2);
11439 if (! EQ (AREF (vector, 3), Qt))
11440 mode_line_proptrans_alist = AREF (vector, 3);
11441 mode_line_string_face = AREF (vector, 4);
11442 mode_line_string_face_prop = AREF (vector, 5);
11443
11444 /* Select window before buffer, since it may change the buffer. */
11445 if (!NILP (old_window))
11446 {
11447 /* If the operation that we are unwinding had selected a window
11448 on a different frame, reset its frame-selected-window. For a
11449 text terminal, reset its top-frame if necessary. */
11450 if (!NILP (target_frame_window))
11451 {
11452 Lisp_Object frame
11453 = WINDOW_FRAME (XWINDOW (target_frame_window));
11454
11455 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11456 Fselect_window (target_frame_window, Qt);
11457
11458 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11459 Fselect_frame (old_top_frame, Qt);
11460 }
11461
11462 Fselect_window (old_window, Qt);
11463 }
11464
11465 if (!NILP (AREF (vector, 6)))
11466 {
11467 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11468 ASET (vector, 6, Qnil);
11469 }
11470
11471 Vmode_line_unwind_vector = vector;
11472 }
11473
11474
11475 /* Store a single character C for the frame title in mode_line_noprop_buf.
11476 Re-allocate mode_line_noprop_buf if necessary. */
11477
11478 static void
11479 store_mode_line_noprop_char (char c)
11480 {
11481 /* If output position has reached the end of the allocated buffer,
11482 increase the buffer's size. */
11483 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11484 {
11485 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11486 ptrdiff_t size = len;
11487 mode_line_noprop_buf =
11488 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11489 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11490 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11491 }
11492
11493 *mode_line_noprop_ptr++ = c;
11494 }
11495
11496
11497 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11498 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11499 characters that yield more columns than PRECISION; PRECISION <= 0
11500 means copy the whole string. Pad with spaces until FIELD_WIDTH
11501 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11502 pad. Called from display_mode_element when it is used to build a
11503 frame title. */
11504
11505 static int
11506 store_mode_line_noprop (const char *string, int field_width, int precision)
11507 {
11508 const unsigned char *str = (const unsigned char *) string;
11509 int n = 0;
11510 ptrdiff_t dummy, nbytes;
11511
11512 /* Copy at most PRECISION chars from STR. */
11513 nbytes = strlen (string);
11514 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11515 while (nbytes--)
11516 store_mode_line_noprop_char (*str++);
11517
11518 /* Fill up with spaces until FIELD_WIDTH reached. */
11519 while (field_width > 0
11520 && n < field_width)
11521 {
11522 store_mode_line_noprop_char (' ');
11523 ++n;
11524 }
11525
11526 return n;
11527 }
11528
11529 /***********************************************************************
11530 Frame Titles
11531 ***********************************************************************/
11532
11533 #ifdef HAVE_WINDOW_SYSTEM
11534
11535 /* Set the title of FRAME, if it has changed. The title format is
11536 Vicon_title_format if FRAME is iconified, otherwise it is
11537 frame_title_format. */
11538
11539 static void
11540 x_consider_frame_title (Lisp_Object frame)
11541 {
11542 struct frame *f = XFRAME (frame);
11543
11544 if (FRAME_WINDOW_P (f)
11545 || FRAME_MINIBUF_ONLY_P (f)
11546 || f->explicit_name)
11547 {
11548 /* Do we have more than one visible frame on this X display? */
11549 Lisp_Object tail, other_frame, fmt;
11550 ptrdiff_t title_start;
11551 char *title;
11552 ptrdiff_t len;
11553 struct it it;
11554 ptrdiff_t count = SPECPDL_INDEX ();
11555
11556 FOR_EACH_FRAME (tail, other_frame)
11557 {
11558 struct frame *tf = XFRAME (other_frame);
11559
11560 if (tf != f
11561 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11562 && !FRAME_MINIBUF_ONLY_P (tf)
11563 && !EQ (other_frame, tip_frame)
11564 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11565 break;
11566 }
11567
11568 /* Set global variable indicating that multiple frames exist. */
11569 multiple_frames = CONSP (tail);
11570
11571 /* Switch to the buffer of selected window of the frame. Set up
11572 mode_line_target so that display_mode_element will output into
11573 mode_line_noprop_buf; then display the title. */
11574 record_unwind_protect (unwind_format_mode_line,
11575 format_mode_line_unwind_data
11576 (f, current_buffer, selected_window, false));
11577
11578 Fselect_window (f->selected_window, Qt);
11579 set_buffer_internal_1
11580 (XBUFFER (XWINDOW (f->selected_window)->contents));
11581 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11582
11583 mode_line_target = MODE_LINE_TITLE;
11584 title_start = MODE_LINE_NOPROP_LEN (0);
11585 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11586 NULL, DEFAULT_FACE_ID);
11587 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11588 len = MODE_LINE_NOPROP_LEN (title_start);
11589 title = mode_line_noprop_buf + title_start;
11590 unbind_to (count, Qnil);
11591
11592 /* Set the title only if it's changed. This avoids consing in
11593 the common case where it hasn't. (If it turns out that we've
11594 already wasted too much time by walking through the list with
11595 display_mode_element, then we might need to optimize at a
11596 higher level than this.) */
11597 if (! STRINGP (f->name)
11598 || SBYTES (f->name) != len
11599 || memcmp (title, SDATA (f->name), len) != 0)
11600 x_implicitly_set_name (f, make_string (title, len), Qnil);
11601 }
11602 }
11603
11604 #endif /* not HAVE_WINDOW_SYSTEM */
11605
11606 \f
11607 /***********************************************************************
11608 Menu Bars
11609 ***********************************************************************/
11610
11611 /* True if we will not redisplay all visible windows. */
11612 #define REDISPLAY_SOME_P() \
11613 ((windows_or_buffers_changed == 0 \
11614 || windows_or_buffers_changed == REDISPLAY_SOME) \
11615 && (update_mode_lines == 0 \
11616 || update_mode_lines == REDISPLAY_SOME))
11617
11618 /* Prepare for redisplay by updating menu-bar item lists when
11619 appropriate. This can call eval. */
11620
11621 static void
11622 prepare_menu_bars (void)
11623 {
11624 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11625 bool some_windows = REDISPLAY_SOME_P ();
11626 Lisp_Object tooltip_frame;
11627
11628 #ifdef HAVE_WINDOW_SYSTEM
11629 tooltip_frame = tip_frame;
11630 #else
11631 tooltip_frame = Qnil;
11632 #endif
11633
11634 if (FUNCTIONP (Vpre_redisplay_function))
11635 {
11636 Lisp_Object windows = all_windows ? Qt : Qnil;
11637 if (all_windows && some_windows)
11638 {
11639 Lisp_Object ws = window_list ();
11640 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11641 {
11642 Lisp_Object this = XCAR (ws);
11643 struct window *w = XWINDOW (this);
11644 if (w->redisplay
11645 || XFRAME (w->frame)->redisplay
11646 || XBUFFER (w->contents)->text->redisplay)
11647 {
11648 windows = Fcons (this, windows);
11649 }
11650 }
11651 }
11652 safe__call1 (true, Vpre_redisplay_function, windows);
11653 }
11654
11655 /* Update all frame titles based on their buffer names, etc. We do
11656 this before the menu bars so that the buffer-menu will show the
11657 up-to-date frame titles. */
11658 #ifdef HAVE_WINDOW_SYSTEM
11659 if (all_windows)
11660 {
11661 Lisp_Object tail, frame;
11662
11663 FOR_EACH_FRAME (tail, frame)
11664 {
11665 struct frame *f = XFRAME (frame);
11666 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11667 if (some_windows
11668 && !f->redisplay
11669 && !w->redisplay
11670 && !XBUFFER (w->contents)->text->redisplay)
11671 continue;
11672
11673 if (!EQ (frame, tooltip_frame)
11674 && (FRAME_ICONIFIED_P (f)
11675 || FRAME_VISIBLE_P (f) == 1
11676 /* Exclude TTY frames that are obscured because they
11677 are not the top frame on their console. This is
11678 because x_consider_frame_title actually switches
11679 to the frame, which for TTY frames means it is
11680 marked as garbaged, and will be completely
11681 redrawn on the next redisplay cycle. This causes
11682 TTY frames to be completely redrawn, when there
11683 are more than one of them, even though nothing
11684 should be changed on display. */
11685 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11686 x_consider_frame_title (frame);
11687 }
11688 }
11689 #endif /* HAVE_WINDOW_SYSTEM */
11690
11691 /* Update the menu bar item lists, if appropriate. This has to be
11692 done before any actual redisplay or generation of display lines. */
11693
11694 if (all_windows)
11695 {
11696 Lisp_Object tail, frame;
11697 ptrdiff_t count = SPECPDL_INDEX ();
11698 /* True means that update_menu_bar has run its hooks
11699 so any further calls to update_menu_bar shouldn't do so again. */
11700 bool menu_bar_hooks_run = false;
11701
11702 record_unwind_save_match_data ();
11703
11704 FOR_EACH_FRAME (tail, frame)
11705 {
11706 struct frame *f = XFRAME (frame);
11707 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11708
11709 /* Ignore tooltip frame. */
11710 if (EQ (frame, tooltip_frame))
11711 continue;
11712
11713 if (some_windows
11714 && !f->redisplay
11715 && !w->redisplay
11716 && !XBUFFER (w->contents)->text->redisplay)
11717 continue;
11718
11719 /* If a window on this frame changed size, report that to
11720 the user and clear the size-change flag. */
11721 if (FRAME_WINDOW_SIZES_CHANGED (f))
11722 {
11723 Lisp_Object functions;
11724
11725 /* Clear flag first in case we get an error below. */
11726 FRAME_WINDOW_SIZES_CHANGED (f) = false;
11727 functions = Vwindow_size_change_functions;
11728
11729 while (CONSP (functions))
11730 {
11731 if (!EQ (XCAR (functions), Qt))
11732 call1 (XCAR (functions), frame);
11733 functions = XCDR (functions);
11734 }
11735 }
11736
11737 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11738 #ifdef HAVE_WINDOW_SYSTEM
11739 update_tool_bar (f, false);
11740 #endif
11741 }
11742
11743 unbind_to (count, Qnil);
11744 }
11745 else
11746 {
11747 struct frame *sf = SELECTED_FRAME ();
11748 update_menu_bar (sf, true, false);
11749 #ifdef HAVE_WINDOW_SYSTEM
11750 update_tool_bar (sf, true);
11751 #endif
11752 }
11753 }
11754
11755
11756 /* Update the menu bar item list for frame F. This has to be done
11757 before we start to fill in any display lines, because it can call
11758 eval.
11759
11760 If SAVE_MATCH_DATA, we must save and restore it here.
11761
11762 If HOOKS_RUN, a previous call to update_menu_bar
11763 already ran the menu bar hooks for this redisplay, so there
11764 is no need to run them again. The return value is the
11765 updated value of this flag, to pass to the next call. */
11766
11767 static bool
11768 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11769 {
11770 Lisp_Object window;
11771 struct window *w;
11772
11773 /* If called recursively during a menu update, do nothing. This can
11774 happen when, for instance, an activate-menubar-hook causes a
11775 redisplay. */
11776 if (inhibit_menubar_update)
11777 return hooks_run;
11778
11779 window = FRAME_SELECTED_WINDOW (f);
11780 w = XWINDOW (window);
11781
11782 if (FRAME_WINDOW_P (f)
11783 ?
11784 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11785 || defined (HAVE_NS) || defined (USE_GTK)
11786 FRAME_EXTERNAL_MENU_BAR (f)
11787 #else
11788 FRAME_MENU_BAR_LINES (f) > 0
11789 #endif
11790 : FRAME_MENU_BAR_LINES (f) > 0)
11791 {
11792 /* If the user has switched buffers or windows, we need to
11793 recompute to reflect the new bindings. But we'll
11794 recompute when update_mode_lines is set too; that means
11795 that people can use force-mode-line-update to request
11796 that the menu bar be recomputed. The adverse effect on
11797 the rest of the redisplay algorithm is about the same as
11798 windows_or_buffers_changed anyway. */
11799 if (windows_or_buffers_changed
11800 /* This used to test w->update_mode_line, but we believe
11801 there is no need to recompute the menu in that case. */
11802 || update_mode_lines
11803 || window_buffer_changed (w))
11804 {
11805 struct buffer *prev = current_buffer;
11806 ptrdiff_t count = SPECPDL_INDEX ();
11807
11808 specbind (Qinhibit_menubar_update, Qt);
11809
11810 set_buffer_internal_1 (XBUFFER (w->contents));
11811 if (save_match_data)
11812 record_unwind_save_match_data ();
11813 if (NILP (Voverriding_local_map_menu_flag))
11814 {
11815 specbind (Qoverriding_terminal_local_map, Qnil);
11816 specbind (Qoverriding_local_map, Qnil);
11817 }
11818
11819 if (!hooks_run)
11820 {
11821 /* Run the Lucid hook. */
11822 safe_run_hooks (Qactivate_menubar_hook);
11823
11824 /* If it has changed current-menubar from previous value,
11825 really recompute the menu-bar from the value. */
11826 if (! NILP (Vlucid_menu_bar_dirty_flag))
11827 call0 (Qrecompute_lucid_menubar);
11828
11829 safe_run_hooks (Qmenu_bar_update_hook);
11830
11831 hooks_run = true;
11832 }
11833
11834 XSETFRAME (Vmenu_updating_frame, f);
11835 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11836
11837 /* Redisplay the menu bar in case we changed it. */
11838 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11839 || defined (HAVE_NS) || defined (USE_GTK)
11840 if (FRAME_WINDOW_P (f))
11841 {
11842 #if defined (HAVE_NS)
11843 /* All frames on Mac OS share the same menubar. So only
11844 the selected frame should be allowed to set it. */
11845 if (f == SELECTED_FRAME ())
11846 #endif
11847 set_frame_menubar (f, false, false);
11848 }
11849 else
11850 /* On a terminal screen, the menu bar is an ordinary screen
11851 line, and this makes it get updated. */
11852 w->update_mode_line = true;
11853 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11854 /* In the non-toolkit version, the menu bar is an ordinary screen
11855 line, and this makes it get updated. */
11856 w->update_mode_line = true;
11857 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11858
11859 unbind_to (count, Qnil);
11860 set_buffer_internal_1 (prev);
11861 }
11862 }
11863
11864 return hooks_run;
11865 }
11866
11867 /***********************************************************************
11868 Tool-bars
11869 ***********************************************************************/
11870
11871 #ifdef HAVE_WINDOW_SYSTEM
11872
11873 /* Select `frame' temporarily without running all the code in
11874 do_switch_frame.
11875 FIXME: Maybe do_switch_frame should be trimmed down similarly
11876 when `norecord' is set. */
11877 static void
11878 fast_set_selected_frame (Lisp_Object frame)
11879 {
11880 if (!EQ (selected_frame, frame))
11881 {
11882 selected_frame = frame;
11883 selected_window = XFRAME (frame)->selected_window;
11884 }
11885 }
11886
11887 /* Update the tool-bar item list for frame F. This has to be done
11888 before we start to fill in any display lines. Called from
11889 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
11890 and restore it here. */
11891
11892 static void
11893 update_tool_bar (struct frame *f, bool save_match_data)
11894 {
11895 #if defined (USE_GTK) || defined (HAVE_NS)
11896 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11897 #else
11898 bool do_update = (WINDOWP (f->tool_bar_window)
11899 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11900 #endif
11901
11902 if (do_update)
11903 {
11904 Lisp_Object window;
11905 struct window *w;
11906
11907 window = FRAME_SELECTED_WINDOW (f);
11908 w = XWINDOW (window);
11909
11910 /* If the user has switched buffers or windows, we need to
11911 recompute to reflect the new bindings. But we'll
11912 recompute when update_mode_lines is set too; that means
11913 that people can use force-mode-line-update to request
11914 that the menu bar be recomputed. The adverse effect on
11915 the rest of the redisplay algorithm is about the same as
11916 windows_or_buffers_changed anyway. */
11917 if (windows_or_buffers_changed
11918 || w->update_mode_line
11919 || update_mode_lines
11920 || window_buffer_changed (w))
11921 {
11922 struct buffer *prev = current_buffer;
11923 ptrdiff_t count = SPECPDL_INDEX ();
11924 Lisp_Object frame, new_tool_bar;
11925 int new_n_tool_bar;
11926
11927 /* Set current_buffer to the buffer of the selected
11928 window of the frame, so that we get the right local
11929 keymaps. */
11930 set_buffer_internal_1 (XBUFFER (w->contents));
11931
11932 /* Save match data, if we must. */
11933 if (save_match_data)
11934 record_unwind_save_match_data ();
11935
11936 /* Make sure that we don't accidentally use bogus keymaps. */
11937 if (NILP (Voverriding_local_map_menu_flag))
11938 {
11939 specbind (Qoverriding_terminal_local_map, Qnil);
11940 specbind (Qoverriding_local_map, Qnil);
11941 }
11942
11943 /* We must temporarily set the selected frame to this frame
11944 before calling tool_bar_items, because the calculation of
11945 the tool-bar keymap uses the selected frame (see
11946 `tool-bar-make-keymap' in tool-bar.el). */
11947 eassert (EQ (selected_window,
11948 /* Since we only explicitly preserve selected_frame,
11949 check that selected_window would be redundant. */
11950 XFRAME (selected_frame)->selected_window));
11951 record_unwind_protect (fast_set_selected_frame, selected_frame);
11952 XSETFRAME (frame, f);
11953 fast_set_selected_frame (frame);
11954
11955 /* Build desired tool-bar items from keymaps. */
11956 new_tool_bar
11957 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11958 &new_n_tool_bar);
11959
11960 /* Redisplay the tool-bar if we changed it. */
11961 if (new_n_tool_bar != f->n_tool_bar_items
11962 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11963 {
11964 /* Redisplay that happens asynchronously due to an expose event
11965 may access f->tool_bar_items. Make sure we update both
11966 variables within BLOCK_INPUT so no such event interrupts. */
11967 block_input ();
11968 fset_tool_bar_items (f, new_tool_bar);
11969 f->n_tool_bar_items = new_n_tool_bar;
11970 w->update_mode_line = true;
11971 unblock_input ();
11972 }
11973
11974 unbind_to (count, Qnil);
11975 set_buffer_internal_1 (prev);
11976 }
11977 }
11978 }
11979
11980 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11981
11982 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11983 F's desired tool-bar contents. F->tool_bar_items must have
11984 been set up previously by calling prepare_menu_bars. */
11985
11986 static void
11987 build_desired_tool_bar_string (struct frame *f)
11988 {
11989 int i, size, size_needed;
11990 Lisp_Object image, plist;
11991
11992 image = plist = Qnil;
11993
11994 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11995 Otherwise, make a new string. */
11996
11997 /* The size of the string we might be able to reuse. */
11998 size = (STRINGP (f->desired_tool_bar_string)
11999 ? SCHARS (f->desired_tool_bar_string)
12000 : 0);
12001
12002 /* We need one space in the string for each image. */
12003 size_needed = f->n_tool_bar_items;
12004
12005 /* Reuse f->desired_tool_bar_string, if possible. */
12006 if (size < size_needed || NILP (f->desired_tool_bar_string))
12007 fset_desired_tool_bar_string
12008 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12009 else
12010 {
12011 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12012 Fremove_text_properties (make_number (0), make_number (size),
12013 props, f->desired_tool_bar_string);
12014 }
12015
12016 /* Put a `display' property on the string for the images to display,
12017 put a `menu_item' property on tool-bar items with a value that
12018 is the index of the item in F's tool-bar item vector. */
12019 for (i = 0; i < f->n_tool_bar_items; ++i)
12020 {
12021 #define PROP(IDX) \
12022 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12023
12024 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12025 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12026 int hmargin, vmargin, relief, idx, end;
12027
12028 /* If image is a vector, choose the image according to the
12029 button state. */
12030 image = PROP (TOOL_BAR_ITEM_IMAGES);
12031 if (VECTORP (image))
12032 {
12033 if (enabled_p)
12034 idx = (selected_p
12035 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12036 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12037 else
12038 idx = (selected_p
12039 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12040 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12041
12042 eassert (ASIZE (image) >= idx);
12043 image = AREF (image, idx);
12044 }
12045 else
12046 idx = -1;
12047
12048 /* Ignore invalid image specifications. */
12049 if (!valid_image_p (image))
12050 continue;
12051
12052 /* Display the tool-bar button pressed, or depressed. */
12053 plist = Fcopy_sequence (XCDR (image));
12054
12055 /* Compute margin and relief to draw. */
12056 relief = (tool_bar_button_relief >= 0
12057 ? tool_bar_button_relief
12058 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12059 hmargin = vmargin = relief;
12060
12061 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12062 INT_MAX - max (hmargin, vmargin)))
12063 {
12064 hmargin += XFASTINT (Vtool_bar_button_margin);
12065 vmargin += XFASTINT (Vtool_bar_button_margin);
12066 }
12067 else if (CONSP (Vtool_bar_button_margin))
12068 {
12069 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12070 INT_MAX - hmargin))
12071 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12072
12073 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12074 INT_MAX - vmargin))
12075 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12076 }
12077
12078 if (auto_raise_tool_bar_buttons_p)
12079 {
12080 /* Add a `:relief' property to the image spec if the item is
12081 selected. */
12082 if (selected_p)
12083 {
12084 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12085 hmargin -= relief;
12086 vmargin -= relief;
12087 }
12088 }
12089 else
12090 {
12091 /* If image is selected, display it pressed, i.e. with a
12092 negative relief. If it's not selected, display it with a
12093 raised relief. */
12094 plist = Fplist_put (plist, QCrelief,
12095 (selected_p
12096 ? make_number (-relief)
12097 : make_number (relief)));
12098 hmargin -= relief;
12099 vmargin -= relief;
12100 }
12101
12102 /* Put a margin around the image. */
12103 if (hmargin || vmargin)
12104 {
12105 if (hmargin == vmargin)
12106 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12107 else
12108 plist = Fplist_put (plist, QCmargin,
12109 Fcons (make_number (hmargin),
12110 make_number (vmargin)));
12111 }
12112
12113 /* If button is not enabled, and we don't have special images
12114 for the disabled state, make the image appear disabled by
12115 applying an appropriate algorithm to it. */
12116 if (!enabled_p && idx < 0)
12117 plist = Fplist_put (plist, QCconversion, Qdisabled);
12118
12119 /* Put a `display' text property on the string for the image to
12120 display. Put a `menu-item' property on the string that gives
12121 the start of this item's properties in the tool-bar items
12122 vector. */
12123 image = Fcons (Qimage, plist);
12124 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12125 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12126
12127 /* Let the last image hide all remaining spaces in the tool bar
12128 string. The string can be longer than needed when we reuse a
12129 previous string. */
12130 if (i + 1 == f->n_tool_bar_items)
12131 end = SCHARS (f->desired_tool_bar_string);
12132 else
12133 end = i + 1;
12134 Fadd_text_properties (make_number (i), make_number (end),
12135 props, f->desired_tool_bar_string);
12136 #undef PROP
12137 }
12138 }
12139
12140
12141 /* Display one line of the tool-bar of frame IT->f.
12142
12143 HEIGHT specifies the desired height of the tool-bar line.
12144 If the actual height of the glyph row is less than HEIGHT, the
12145 row's height is increased to HEIGHT, and the icons are centered
12146 vertically in the new height.
12147
12148 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12149 count a final empty row in case the tool-bar width exactly matches
12150 the window width.
12151 */
12152
12153 static void
12154 display_tool_bar_line (struct it *it, int height)
12155 {
12156 struct glyph_row *row = it->glyph_row;
12157 int max_x = it->last_visible_x;
12158 struct glyph *last;
12159
12160 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12161 clear_glyph_row (row);
12162 row->enabled_p = true;
12163 row->y = it->current_y;
12164
12165 /* Note that this isn't made use of if the face hasn't a box,
12166 so there's no need to check the face here. */
12167 it->start_of_box_run_p = true;
12168
12169 while (it->current_x < max_x)
12170 {
12171 int x, n_glyphs_before, i, nglyphs;
12172 struct it it_before;
12173
12174 /* Get the next display element. */
12175 if (!get_next_display_element (it))
12176 {
12177 /* Don't count empty row if we are counting needed tool-bar lines. */
12178 if (height < 0 && !it->hpos)
12179 return;
12180 break;
12181 }
12182
12183 /* Produce glyphs. */
12184 n_glyphs_before = row->used[TEXT_AREA];
12185 it_before = *it;
12186
12187 PRODUCE_GLYPHS (it);
12188
12189 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12190 i = 0;
12191 x = it_before.current_x;
12192 while (i < nglyphs)
12193 {
12194 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12195
12196 if (x + glyph->pixel_width > max_x)
12197 {
12198 /* Glyph doesn't fit on line. Backtrack. */
12199 row->used[TEXT_AREA] = n_glyphs_before;
12200 *it = it_before;
12201 /* If this is the only glyph on this line, it will never fit on the
12202 tool-bar, so skip it. But ensure there is at least one glyph,
12203 so we don't accidentally disable the tool-bar. */
12204 if (n_glyphs_before == 0
12205 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12206 break;
12207 goto out;
12208 }
12209
12210 ++it->hpos;
12211 x += glyph->pixel_width;
12212 ++i;
12213 }
12214
12215 /* Stop at line end. */
12216 if (ITERATOR_AT_END_OF_LINE_P (it))
12217 break;
12218
12219 set_iterator_to_next (it, true);
12220 }
12221
12222 out:;
12223
12224 row->displays_text_p = row->used[TEXT_AREA] != 0;
12225
12226 /* Use default face for the border below the tool bar.
12227
12228 FIXME: When auto-resize-tool-bars is grow-only, there is
12229 no additional border below the possibly empty tool-bar lines.
12230 So to make the extra empty lines look "normal", we have to
12231 use the tool-bar face for the border too. */
12232 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12233 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12234 it->face_id = DEFAULT_FACE_ID;
12235
12236 extend_face_to_end_of_line (it);
12237 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12238 last->right_box_line_p = true;
12239 if (last == row->glyphs[TEXT_AREA])
12240 last->left_box_line_p = true;
12241
12242 /* Make line the desired height and center it vertically. */
12243 if ((height -= it->max_ascent + it->max_descent) > 0)
12244 {
12245 /* Don't add more than one line height. */
12246 height %= FRAME_LINE_HEIGHT (it->f);
12247 it->max_ascent += height / 2;
12248 it->max_descent += (height + 1) / 2;
12249 }
12250
12251 compute_line_metrics (it);
12252
12253 /* If line is empty, make it occupy the rest of the tool-bar. */
12254 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12255 {
12256 row->height = row->phys_height = it->last_visible_y - row->y;
12257 row->visible_height = row->height;
12258 row->ascent = row->phys_ascent = 0;
12259 row->extra_line_spacing = 0;
12260 }
12261
12262 row->full_width_p = true;
12263 row->continued_p = false;
12264 row->truncated_on_left_p = false;
12265 row->truncated_on_right_p = false;
12266
12267 it->current_x = it->hpos = 0;
12268 it->current_y += row->height;
12269 ++it->vpos;
12270 ++it->glyph_row;
12271 }
12272
12273
12274 /* Value is the number of pixels needed to make all tool-bar items of
12275 frame F visible. The actual number of glyph rows needed is
12276 returned in *N_ROWS if non-NULL. */
12277 static int
12278 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12279 {
12280 struct window *w = XWINDOW (f->tool_bar_window);
12281 struct it it;
12282 /* tool_bar_height is called from redisplay_tool_bar after building
12283 the desired matrix, so use (unused) mode-line row as temporary row to
12284 avoid destroying the first tool-bar row. */
12285 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12286
12287 /* Initialize an iterator for iteration over
12288 F->desired_tool_bar_string in the tool-bar window of frame F. */
12289 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12290 temp_row->reversed_p = false;
12291 it.first_visible_x = 0;
12292 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12293 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12294 it.paragraph_embedding = L2R;
12295
12296 while (!ITERATOR_AT_END_P (&it))
12297 {
12298 clear_glyph_row (temp_row);
12299 it.glyph_row = temp_row;
12300 display_tool_bar_line (&it, -1);
12301 }
12302 clear_glyph_row (temp_row);
12303
12304 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12305 if (n_rows)
12306 *n_rows = it.vpos > 0 ? it.vpos : -1;
12307
12308 if (pixelwise)
12309 return it.current_y;
12310 else
12311 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12312 }
12313
12314 #endif /* !USE_GTK && !HAVE_NS */
12315
12316 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12317 0, 2, 0,
12318 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12319 If FRAME is nil or omitted, use the selected frame. Optional argument
12320 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12321 (Lisp_Object frame, Lisp_Object pixelwise)
12322 {
12323 int height = 0;
12324
12325 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12326 struct frame *f = decode_any_frame (frame);
12327
12328 if (WINDOWP (f->tool_bar_window)
12329 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12330 {
12331 update_tool_bar (f, true);
12332 if (f->n_tool_bar_items)
12333 {
12334 build_desired_tool_bar_string (f);
12335 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12336 }
12337 }
12338 #endif
12339
12340 return make_number (height);
12341 }
12342
12343
12344 /* Display the tool-bar of frame F. Value is true if tool-bar's
12345 height should be changed. */
12346 static bool
12347 redisplay_tool_bar (struct frame *f)
12348 {
12349 f->tool_bar_redisplayed = true;
12350 #if defined (USE_GTK) || defined (HAVE_NS)
12351
12352 if (FRAME_EXTERNAL_TOOL_BAR (f))
12353 update_frame_tool_bar (f);
12354 return false;
12355
12356 #else /* !USE_GTK && !HAVE_NS */
12357
12358 struct window *w;
12359 struct it it;
12360 struct glyph_row *row;
12361
12362 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12363 do anything. This means you must start with tool-bar-lines
12364 non-zero to get the auto-sizing effect. Or in other words, you
12365 can turn off tool-bars by specifying tool-bar-lines zero. */
12366 if (!WINDOWP (f->tool_bar_window)
12367 || (w = XWINDOW (f->tool_bar_window),
12368 WINDOW_TOTAL_LINES (w) == 0))
12369 return false;
12370
12371 /* Set up an iterator for the tool-bar window. */
12372 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12373 it.first_visible_x = 0;
12374 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12375 row = it.glyph_row;
12376 row->reversed_p = false;
12377
12378 /* Build a string that represents the contents of the tool-bar. */
12379 build_desired_tool_bar_string (f);
12380 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12381 /* FIXME: This should be controlled by a user option. But it
12382 doesn't make sense to have an R2L tool bar if the menu bar cannot
12383 be drawn also R2L, and making the menu bar R2L is tricky due
12384 toolkit-specific code that implements it. If an R2L tool bar is
12385 ever supported, display_tool_bar_line should also be augmented to
12386 call unproduce_glyphs like display_line and display_string
12387 do. */
12388 it.paragraph_embedding = L2R;
12389
12390 if (f->n_tool_bar_rows == 0)
12391 {
12392 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12393
12394 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12395 {
12396 x_change_tool_bar_height (f, new_height);
12397 frame_default_tool_bar_height = new_height;
12398 /* Always do that now. */
12399 clear_glyph_matrix (w->desired_matrix);
12400 f->fonts_changed = true;
12401 return true;
12402 }
12403 }
12404
12405 /* Display as many lines as needed to display all tool-bar items. */
12406
12407 if (f->n_tool_bar_rows > 0)
12408 {
12409 int border, rows, height, extra;
12410
12411 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12412 border = XINT (Vtool_bar_border);
12413 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12414 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12415 else if (EQ (Vtool_bar_border, Qborder_width))
12416 border = f->border_width;
12417 else
12418 border = 0;
12419 if (border < 0)
12420 border = 0;
12421
12422 rows = f->n_tool_bar_rows;
12423 height = max (1, (it.last_visible_y - border) / rows);
12424 extra = it.last_visible_y - border - height * rows;
12425
12426 while (it.current_y < it.last_visible_y)
12427 {
12428 int h = 0;
12429 if (extra > 0 && rows-- > 0)
12430 {
12431 h = (extra + rows - 1) / rows;
12432 extra -= h;
12433 }
12434 display_tool_bar_line (&it, height + h);
12435 }
12436 }
12437 else
12438 {
12439 while (it.current_y < it.last_visible_y)
12440 display_tool_bar_line (&it, 0);
12441 }
12442
12443 /* It doesn't make much sense to try scrolling in the tool-bar
12444 window, so don't do it. */
12445 w->desired_matrix->no_scrolling_p = true;
12446 w->must_be_updated_p = true;
12447
12448 if (!NILP (Vauto_resize_tool_bars))
12449 {
12450 bool change_height_p = true;
12451
12452 /* If we couldn't display everything, change the tool-bar's
12453 height if there is room for more. */
12454 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12455 change_height_p = true;
12456
12457 /* We subtract 1 because display_tool_bar_line advances the
12458 glyph_row pointer before returning to its caller. We want to
12459 examine the last glyph row produced by
12460 display_tool_bar_line. */
12461 row = it.glyph_row - 1;
12462
12463 /* If there are blank lines at the end, except for a partially
12464 visible blank line at the end that is smaller than
12465 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12466 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12467 && row->height >= FRAME_LINE_HEIGHT (f))
12468 change_height_p = true;
12469
12470 /* If row displays tool-bar items, but is partially visible,
12471 change the tool-bar's height. */
12472 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12473 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12474 change_height_p = true;
12475
12476 /* Resize windows as needed by changing the `tool-bar-lines'
12477 frame parameter. */
12478 if (change_height_p)
12479 {
12480 int nrows;
12481 int new_height = tool_bar_height (f, &nrows, true);
12482
12483 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12484 && !f->minimize_tool_bar_window_p)
12485 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12486 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12487 f->minimize_tool_bar_window_p = false;
12488
12489 if (change_height_p)
12490 {
12491 x_change_tool_bar_height (f, new_height);
12492 frame_default_tool_bar_height = new_height;
12493 clear_glyph_matrix (w->desired_matrix);
12494 f->n_tool_bar_rows = nrows;
12495 f->fonts_changed = true;
12496
12497 return true;
12498 }
12499 }
12500 }
12501
12502 f->minimize_tool_bar_window_p = false;
12503 return false;
12504
12505 #endif /* USE_GTK || HAVE_NS */
12506 }
12507
12508 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12509
12510 /* Get information about the tool-bar item which is displayed in GLYPH
12511 on frame F. Return in *PROP_IDX the index where tool-bar item
12512 properties start in F->tool_bar_items. Value is false if
12513 GLYPH doesn't display a tool-bar item. */
12514
12515 static bool
12516 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12517 {
12518 Lisp_Object prop;
12519 int charpos;
12520
12521 /* This function can be called asynchronously, which means we must
12522 exclude any possibility that Fget_text_property signals an
12523 error. */
12524 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12525 charpos = max (0, charpos);
12526
12527 /* Get the text property `menu-item' at pos. The value of that
12528 property is the start index of this item's properties in
12529 F->tool_bar_items. */
12530 prop = Fget_text_property (make_number (charpos),
12531 Qmenu_item, f->current_tool_bar_string);
12532 if (! INTEGERP (prop))
12533 return false;
12534 *prop_idx = XINT (prop);
12535 return true;
12536 }
12537
12538 \f
12539 /* Get information about the tool-bar item at position X/Y on frame F.
12540 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12541 the current matrix of the tool-bar window of F, or NULL if not
12542 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12543 item in F->tool_bar_items. Value is
12544
12545 -1 if X/Y is not on a tool-bar item
12546 0 if X/Y is on the same item that was highlighted before.
12547 1 otherwise. */
12548
12549 static int
12550 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12551 int *hpos, int *vpos, int *prop_idx)
12552 {
12553 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12554 struct window *w = XWINDOW (f->tool_bar_window);
12555 int area;
12556
12557 /* Find the glyph under X/Y. */
12558 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12559 if (*glyph == NULL)
12560 return -1;
12561
12562 /* Get the start of this tool-bar item's properties in
12563 f->tool_bar_items. */
12564 if (!tool_bar_item_info (f, *glyph, prop_idx))
12565 return -1;
12566
12567 /* Is mouse on the highlighted item? */
12568 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12569 && *vpos >= hlinfo->mouse_face_beg_row
12570 && *vpos <= hlinfo->mouse_face_end_row
12571 && (*vpos > hlinfo->mouse_face_beg_row
12572 || *hpos >= hlinfo->mouse_face_beg_col)
12573 && (*vpos < hlinfo->mouse_face_end_row
12574 || *hpos < hlinfo->mouse_face_end_col
12575 || hlinfo->mouse_face_past_end))
12576 return 0;
12577
12578 return 1;
12579 }
12580
12581
12582 /* EXPORT:
12583 Handle mouse button event on the tool-bar of frame F, at
12584 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12585 false for button release. MODIFIERS is event modifiers for button
12586 release. */
12587
12588 void
12589 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12590 int modifiers)
12591 {
12592 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12593 struct window *w = XWINDOW (f->tool_bar_window);
12594 int hpos, vpos, prop_idx;
12595 struct glyph *glyph;
12596 Lisp_Object enabled_p;
12597 int ts;
12598
12599 /* If not on the highlighted tool-bar item, and mouse-highlight is
12600 non-nil, return. This is so we generate the tool-bar button
12601 click only when the mouse button is released on the same item as
12602 where it was pressed. However, when mouse-highlight is disabled,
12603 generate the click when the button is released regardless of the
12604 highlight, since tool-bar items are not highlighted in that
12605 case. */
12606 frame_to_window_pixel_xy (w, &x, &y);
12607 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12608 if (ts == -1
12609 || (ts != 0 && !NILP (Vmouse_highlight)))
12610 return;
12611
12612 /* When mouse-highlight is off, generate the click for the item
12613 where the button was pressed, disregarding where it was
12614 released. */
12615 if (NILP (Vmouse_highlight) && !down_p)
12616 prop_idx = f->last_tool_bar_item;
12617
12618 /* If item is disabled, do nothing. */
12619 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12620 if (NILP (enabled_p))
12621 return;
12622
12623 if (down_p)
12624 {
12625 /* Show item in pressed state. */
12626 if (!NILP (Vmouse_highlight))
12627 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12628 f->last_tool_bar_item = prop_idx;
12629 }
12630 else
12631 {
12632 Lisp_Object key, frame;
12633 struct input_event event;
12634 EVENT_INIT (event);
12635
12636 /* Show item in released state. */
12637 if (!NILP (Vmouse_highlight))
12638 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12639
12640 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12641
12642 XSETFRAME (frame, f);
12643 event.kind = TOOL_BAR_EVENT;
12644 event.frame_or_window = frame;
12645 event.arg = frame;
12646 kbd_buffer_store_event (&event);
12647
12648 event.kind = TOOL_BAR_EVENT;
12649 event.frame_or_window = frame;
12650 event.arg = key;
12651 event.modifiers = modifiers;
12652 kbd_buffer_store_event (&event);
12653 f->last_tool_bar_item = -1;
12654 }
12655 }
12656
12657
12658 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12659 tool-bar window-relative coordinates X/Y. Called from
12660 note_mouse_highlight. */
12661
12662 static void
12663 note_tool_bar_highlight (struct frame *f, int x, int y)
12664 {
12665 Lisp_Object window = f->tool_bar_window;
12666 struct window *w = XWINDOW (window);
12667 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12668 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12669 int hpos, vpos;
12670 struct glyph *glyph;
12671 struct glyph_row *row;
12672 int i;
12673 Lisp_Object enabled_p;
12674 int prop_idx;
12675 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12676 bool mouse_down_p;
12677 int rc;
12678
12679 /* Function note_mouse_highlight is called with negative X/Y
12680 values when mouse moves outside of the frame. */
12681 if (x <= 0 || y <= 0)
12682 {
12683 clear_mouse_face (hlinfo);
12684 return;
12685 }
12686
12687 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12688 if (rc < 0)
12689 {
12690 /* Not on tool-bar item. */
12691 clear_mouse_face (hlinfo);
12692 return;
12693 }
12694 else if (rc == 0)
12695 /* On same tool-bar item as before. */
12696 goto set_help_echo;
12697
12698 clear_mouse_face (hlinfo);
12699
12700 /* Mouse is down, but on different tool-bar item? */
12701 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12702 && f == dpyinfo->last_mouse_frame);
12703
12704 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12705 return;
12706
12707 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12708
12709 /* If tool-bar item is not enabled, don't highlight it. */
12710 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12711 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12712 {
12713 /* Compute the x-position of the glyph. In front and past the
12714 image is a space. We include this in the highlighted area. */
12715 row = MATRIX_ROW (w->current_matrix, vpos);
12716 for (i = x = 0; i < hpos; ++i)
12717 x += row->glyphs[TEXT_AREA][i].pixel_width;
12718
12719 /* Record this as the current active region. */
12720 hlinfo->mouse_face_beg_col = hpos;
12721 hlinfo->mouse_face_beg_row = vpos;
12722 hlinfo->mouse_face_beg_x = x;
12723 hlinfo->mouse_face_past_end = false;
12724
12725 hlinfo->mouse_face_end_col = hpos + 1;
12726 hlinfo->mouse_face_end_row = vpos;
12727 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12728 hlinfo->mouse_face_window = window;
12729 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12730
12731 /* Display it as active. */
12732 show_mouse_face (hlinfo, draw);
12733 }
12734
12735 set_help_echo:
12736
12737 /* Set help_echo_string to a help string to display for this tool-bar item.
12738 XTread_socket does the rest. */
12739 help_echo_object = help_echo_window = Qnil;
12740 help_echo_pos = -1;
12741 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12742 if (NILP (help_echo_string))
12743 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12744 }
12745
12746 #endif /* !USE_GTK && !HAVE_NS */
12747
12748 #endif /* HAVE_WINDOW_SYSTEM */
12749
12750
12751 \f
12752 /************************************************************************
12753 Horizontal scrolling
12754 ************************************************************************/
12755
12756 /* For all leaf windows in the window tree rooted at WINDOW, set their
12757 hscroll value so that PT is (i) visible in the window, and (ii) so
12758 that it is not within a certain margin at the window's left and
12759 right border. Value is true if any window's hscroll has been
12760 changed. */
12761
12762 static bool
12763 hscroll_window_tree (Lisp_Object window)
12764 {
12765 bool hscrolled_p = false;
12766 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12767 int hscroll_step_abs = 0;
12768 double hscroll_step_rel = 0;
12769
12770 if (hscroll_relative_p)
12771 {
12772 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12773 if (hscroll_step_rel < 0)
12774 {
12775 hscroll_relative_p = false;
12776 hscroll_step_abs = 0;
12777 }
12778 }
12779 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12780 {
12781 hscroll_step_abs = XINT (Vhscroll_step);
12782 if (hscroll_step_abs < 0)
12783 hscroll_step_abs = 0;
12784 }
12785 else
12786 hscroll_step_abs = 0;
12787
12788 while (WINDOWP (window))
12789 {
12790 struct window *w = XWINDOW (window);
12791
12792 if (WINDOWP (w->contents))
12793 hscrolled_p |= hscroll_window_tree (w->contents);
12794 else if (w->cursor.vpos >= 0)
12795 {
12796 int h_margin;
12797 int text_area_width;
12798 struct glyph_row *cursor_row;
12799 struct glyph_row *bottom_row;
12800
12801 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12802 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12803 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12804 else
12805 cursor_row = bottom_row - 1;
12806
12807 if (!cursor_row->enabled_p)
12808 {
12809 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12810 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12811 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12812 else
12813 cursor_row = bottom_row - 1;
12814 }
12815 bool row_r2l_p = cursor_row->reversed_p;
12816
12817 text_area_width = window_box_width (w, TEXT_AREA);
12818
12819 /* Scroll when cursor is inside this scroll margin. */
12820 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12821
12822 /* If the position of this window's point has explicitly
12823 changed, no more suspend auto hscrolling. */
12824 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12825 w->suspend_auto_hscroll = false;
12826
12827 /* Remember window point. */
12828 Fset_marker (w->old_pointm,
12829 ((w == XWINDOW (selected_window))
12830 ? make_number (BUF_PT (XBUFFER (w->contents)))
12831 : Fmarker_position (w->pointm)),
12832 w->contents);
12833
12834 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12835 && !w->suspend_auto_hscroll
12836 /* In some pathological cases, like restoring a window
12837 configuration into a frame that is much smaller than
12838 the one from which the configuration was saved, we
12839 get glyph rows whose start and end have zero buffer
12840 positions, which we cannot handle below. Just skip
12841 such windows. */
12842 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12843 /* For left-to-right rows, hscroll when cursor is either
12844 (i) inside the right hscroll margin, or (ii) if it is
12845 inside the left margin and the window is already
12846 hscrolled. */
12847 && ((!row_r2l_p
12848 && ((w->hscroll && w->cursor.x <= h_margin)
12849 || (cursor_row->enabled_p
12850 && cursor_row->truncated_on_right_p
12851 && (w->cursor.x >= text_area_width - h_margin))))
12852 /* For right-to-left rows, the logic is similar,
12853 except that rules for scrolling to left and right
12854 are reversed. E.g., if cursor.x <= h_margin, we
12855 need to hscroll "to the right" unconditionally,
12856 and that will scroll the screen to the left so as
12857 to reveal the next portion of the row. */
12858 || (row_r2l_p
12859 && ((cursor_row->enabled_p
12860 /* FIXME: It is confusing to set the
12861 truncated_on_right_p flag when R2L rows
12862 are actually truncated on the left. */
12863 && cursor_row->truncated_on_right_p
12864 && w->cursor.x <= h_margin)
12865 || (w->hscroll
12866 && (w->cursor.x >= text_area_width - h_margin))))))
12867 {
12868 struct it it;
12869 ptrdiff_t hscroll;
12870 struct buffer *saved_current_buffer;
12871 ptrdiff_t pt;
12872 int wanted_x;
12873
12874 /* Find point in a display of infinite width. */
12875 saved_current_buffer = current_buffer;
12876 current_buffer = XBUFFER (w->contents);
12877
12878 if (w == XWINDOW (selected_window))
12879 pt = PT;
12880 else
12881 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12882
12883 /* Move iterator to pt starting at cursor_row->start in
12884 a line with infinite width. */
12885 init_to_row_start (&it, w, cursor_row);
12886 it.last_visible_x = INFINITY;
12887 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12888 current_buffer = saved_current_buffer;
12889
12890 /* Position cursor in window. */
12891 if (!hscroll_relative_p && hscroll_step_abs == 0)
12892 hscroll = max (0, (it.current_x
12893 - (ITERATOR_AT_END_OF_LINE_P (&it)
12894 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12895 : (text_area_width / 2))))
12896 / FRAME_COLUMN_WIDTH (it.f);
12897 else if ((!row_r2l_p
12898 && w->cursor.x >= text_area_width - h_margin)
12899 || (row_r2l_p && w->cursor.x <= h_margin))
12900 {
12901 if (hscroll_relative_p)
12902 wanted_x = text_area_width * (1 - hscroll_step_rel)
12903 - h_margin;
12904 else
12905 wanted_x = text_area_width
12906 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12907 - h_margin;
12908 hscroll
12909 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12910 }
12911 else
12912 {
12913 if (hscroll_relative_p)
12914 wanted_x = text_area_width * hscroll_step_rel
12915 + h_margin;
12916 else
12917 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12918 + h_margin;
12919 hscroll
12920 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12921 }
12922 hscroll = max (hscroll, w->min_hscroll);
12923
12924 /* Don't prevent redisplay optimizations if hscroll
12925 hasn't changed, as it will unnecessarily slow down
12926 redisplay. */
12927 if (w->hscroll != hscroll)
12928 {
12929 struct buffer *b = XBUFFER (w->contents);
12930 b->prevent_redisplay_optimizations_p = true;
12931 w->hscroll = hscroll;
12932 hscrolled_p = true;
12933 }
12934 }
12935 }
12936
12937 window = w->next;
12938 }
12939
12940 /* Value is true if hscroll of any leaf window has been changed. */
12941 return hscrolled_p;
12942 }
12943
12944
12945 /* Set hscroll so that cursor is visible and not inside horizontal
12946 scroll margins for all windows in the tree rooted at WINDOW. See
12947 also hscroll_window_tree above. Value is true if any window's
12948 hscroll has been changed. If it has, desired matrices on the frame
12949 of WINDOW are cleared. */
12950
12951 static bool
12952 hscroll_windows (Lisp_Object window)
12953 {
12954 bool hscrolled_p = hscroll_window_tree (window);
12955 if (hscrolled_p)
12956 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12957 return hscrolled_p;
12958 }
12959
12960
12961 \f
12962 /************************************************************************
12963 Redisplay
12964 ************************************************************************/
12965
12966 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
12967 This is sometimes handy to have in a debugger session. */
12968
12969 #ifdef GLYPH_DEBUG
12970
12971 /* First and last unchanged row for try_window_id. */
12972
12973 static int debug_first_unchanged_at_end_vpos;
12974 static int debug_last_unchanged_at_beg_vpos;
12975
12976 /* Delta vpos and y. */
12977
12978 static int debug_dvpos, debug_dy;
12979
12980 /* Delta in characters and bytes for try_window_id. */
12981
12982 static ptrdiff_t debug_delta, debug_delta_bytes;
12983
12984 /* Values of window_end_pos and window_end_vpos at the end of
12985 try_window_id. */
12986
12987 static ptrdiff_t debug_end_vpos;
12988
12989 /* Append a string to W->desired_matrix->method. FMT is a printf
12990 format string. If trace_redisplay_p is true also printf the
12991 resulting string to stderr. */
12992
12993 static void debug_method_add (struct window *, char const *, ...)
12994 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12995
12996 static void
12997 debug_method_add (struct window *w, char const *fmt, ...)
12998 {
12999 void *ptr = w;
13000 char *method = w->desired_matrix->method;
13001 int len = strlen (method);
13002 int size = sizeof w->desired_matrix->method;
13003 int remaining = size - len - 1;
13004 va_list ap;
13005
13006 if (len && remaining)
13007 {
13008 method[len] = '|';
13009 --remaining, ++len;
13010 }
13011
13012 va_start (ap, fmt);
13013 vsnprintf (method + len, remaining + 1, fmt, ap);
13014 va_end (ap);
13015
13016 if (trace_redisplay_p)
13017 fprintf (stderr, "%p (%s): %s\n",
13018 ptr,
13019 ((BUFFERP (w->contents)
13020 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13021 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13022 : "no buffer"),
13023 method + len);
13024 }
13025
13026 #endif /* GLYPH_DEBUG */
13027
13028
13029 /* Value is true if all changes in window W, which displays
13030 current_buffer, are in the text between START and END. START is a
13031 buffer position, END is given as a distance from Z. Used in
13032 redisplay_internal for display optimization. */
13033
13034 static bool
13035 text_outside_line_unchanged_p (struct window *w,
13036 ptrdiff_t start, ptrdiff_t end)
13037 {
13038 bool unchanged_p = true;
13039
13040 /* If text or overlays have changed, see where. */
13041 if (window_outdated (w))
13042 {
13043 /* Gap in the line? */
13044 if (GPT < start || Z - GPT < end)
13045 unchanged_p = false;
13046
13047 /* Changes start in front of the line, or end after it? */
13048 if (unchanged_p
13049 && (BEG_UNCHANGED < start - 1
13050 || END_UNCHANGED < end))
13051 unchanged_p = false;
13052
13053 /* If selective display, can't optimize if changes start at the
13054 beginning of the line. */
13055 if (unchanged_p
13056 && INTEGERP (BVAR (current_buffer, selective_display))
13057 && XINT (BVAR (current_buffer, selective_display)) > 0
13058 && (BEG_UNCHANGED < start || GPT <= start))
13059 unchanged_p = false;
13060
13061 /* If there are overlays at the start or end of the line, these
13062 may have overlay strings with newlines in them. A change at
13063 START, for instance, may actually concern the display of such
13064 overlay strings as well, and they are displayed on different
13065 lines. So, quickly rule out this case. (For the future, it
13066 might be desirable to implement something more telling than
13067 just BEG/END_UNCHANGED.) */
13068 if (unchanged_p)
13069 {
13070 if (BEG + BEG_UNCHANGED == start
13071 && overlay_touches_p (start))
13072 unchanged_p = false;
13073 if (END_UNCHANGED == end
13074 && overlay_touches_p (Z - end))
13075 unchanged_p = false;
13076 }
13077
13078 /* Under bidi reordering, adding or deleting a character in the
13079 beginning of a paragraph, before the first strong directional
13080 character, can change the base direction of the paragraph (unless
13081 the buffer specifies a fixed paragraph direction), which will
13082 require to redisplay the whole paragraph. It might be worthwhile
13083 to find the paragraph limits and widen the range of redisplayed
13084 lines to that, but for now just give up this optimization. */
13085 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13086 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13087 unchanged_p = false;
13088 }
13089
13090 return unchanged_p;
13091 }
13092
13093
13094 /* Do a frame update, taking possible shortcuts into account. This is
13095 the main external entry point for redisplay.
13096
13097 If the last redisplay displayed an echo area message and that message
13098 is no longer requested, we clear the echo area or bring back the
13099 mini-buffer if that is in use. */
13100
13101 void
13102 redisplay (void)
13103 {
13104 redisplay_internal ();
13105 }
13106
13107
13108 static Lisp_Object
13109 overlay_arrow_string_or_property (Lisp_Object var)
13110 {
13111 Lisp_Object val;
13112
13113 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13114 return val;
13115
13116 return Voverlay_arrow_string;
13117 }
13118
13119 /* Return true if there are any overlay-arrows in current_buffer. */
13120 static bool
13121 overlay_arrow_in_current_buffer_p (void)
13122 {
13123 Lisp_Object vlist;
13124
13125 for (vlist = Voverlay_arrow_variable_list;
13126 CONSP (vlist);
13127 vlist = XCDR (vlist))
13128 {
13129 Lisp_Object var = XCAR (vlist);
13130 Lisp_Object val;
13131
13132 if (!SYMBOLP (var))
13133 continue;
13134 val = find_symbol_value (var);
13135 if (MARKERP (val)
13136 && current_buffer == XMARKER (val)->buffer)
13137 return true;
13138 }
13139 return false;
13140 }
13141
13142
13143 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13144 has changed. */
13145
13146 static bool
13147 overlay_arrows_changed_p (void)
13148 {
13149 Lisp_Object vlist;
13150
13151 for (vlist = Voverlay_arrow_variable_list;
13152 CONSP (vlist);
13153 vlist = XCDR (vlist))
13154 {
13155 Lisp_Object var = XCAR (vlist);
13156 Lisp_Object val, pstr;
13157
13158 if (!SYMBOLP (var))
13159 continue;
13160 val = find_symbol_value (var);
13161 if (!MARKERP (val))
13162 continue;
13163 if (! EQ (COERCE_MARKER (val),
13164 Fget (var, Qlast_arrow_position))
13165 || ! (pstr = overlay_arrow_string_or_property (var),
13166 EQ (pstr, Fget (var, Qlast_arrow_string))))
13167 return true;
13168 }
13169 return false;
13170 }
13171
13172 /* Mark overlay arrows to be updated on next redisplay. */
13173
13174 static void
13175 update_overlay_arrows (int up_to_date)
13176 {
13177 Lisp_Object vlist;
13178
13179 for (vlist = Voverlay_arrow_variable_list;
13180 CONSP (vlist);
13181 vlist = XCDR (vlist))
13182 {
13183 Lisp_Object var = XCAR (vlist);
13184
13185 if (!SYMBOLP (var))
13186 continue;
13187
13188 if (up_to_date > 0)
13189 {
13190 Lisp_Object val = find_symbol_value (var);
13191 Fput (var, Qlast_arrow_position,
13192 COERCE_MARKER (val));
13193 Fput (var, Qlast_arrow_string,
13194 overlay_arrow_string_or_property (var));
13195 }
13196 else if (up_to_date < 0
13197 || !NILP (Fget (var, Qlast_arrow_position)))
13198 {
13199 Fput (var, Qlast_arrow_position, Qt);
13200 Fput (var, Qlast_arrow_string, Qt);
13201 }
13202 }
13203 }
13204
13205
13206 /* Return overlay arrow string to display at row.
13207 Return integer (bitmap number) for arrow bitmap in left fringe.
13208 Return nil if no overlay arrow. */
13209
13210 static Lisp_Object
13211 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13212 {
13213 Lisp_Object vlist;
13214
13215 for (vlist = Voverlay_arrow_variable_list;
13216 CONSP (vlist);
13217 vlist = XCDR (vlist))
13218 {
13219 Lisp_Object var = XCAR (vlist);
13220 Lisp_Object val;
13221
13222 if (!SYMBOLP (var))
13223 continue;
13224
13225 val = find_symbol_value (var);
13226
13227 if (MARKERP (val)
13228 && current_buffer == XMARKER (val)->buffer
13229 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13230 {
13231 if (FRAME_WINDOW_P (it->f)
13232 /* FIXME: if ROW->reversed_p is set, this should test
13233 the right fringe, not the left one. */
13234 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13235 {
13236 #ifdef HAVE_WINDOW_SYSTEM
13237 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13238 {
13239 int fringe_bitmap = lookup_fringe_bitmap (val);
13240 if (fringe_bitmap != 0)
13241 return make_number (fringe_bitmap);
13242 }
13243 #endif
13244 return make_number (-1); /* Use default arrow bitmap. */
13245 }
13246 return overlay_arrow_string_or_property (var);
13247 }
13248 }
13249
13250 return Qnil;
13251 }
13252
13253 /* Return true if point moved out of or into a composition. Otherwise
13254 return false. PREV_BUF and PREV_PT are the last point buffer and
13255 position. BUF and PT are the current point buffer and position. */
13256
13257 static bool
13258 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13259 struct buffer *buf, ptrdiff_t pt)
13260 {
13261 ptrdiff_t start, end;
13262 Lisp_Object prop;
13263 Lisp_Object buffer;
13264
13265 XSETBUFFER (buffer, buf);
13266 /* Check a composition at the last point if point moved within the
13267 same buffer. */
13268 if (prev_buf == buf)
13269 {
13270 if (prev_pt == pt)
13271 /* Point didn't move. */
13272 return false;
13273
13274 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13275 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13276 && composition_valid_p (start, end, prop)
13277 && start < prev_pt && end > prev_pt)
13278 /* The last point was within the composition. Return true iff
13279 point moved out of the composition. */
13280 return (pt <= start || pt >= end);
13281 }
13282
13283 /* Check a composition at the current point. */
13284 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13285 && find_composition (pt, -1, &start, &end, &prop, buffer)
13286 && composition_valid_p (start, end, prop)
13287 && start < pt && end > pt);
13288 }
13289
13290 /* Reconsider the clip changes of buffer which is displayed in W. */
13291
13292 static void
13293 reconsider_clip_changes (struct window *w)
13294 {
13295 struct buffer *b = XBUFFER (w->contents);
13296
13297 if (b->clip_changed
13298 && w->window_end_valid
13299 && w->current_matrix->buffer == b
13300 && w->current_matrix->zv == BUF_ZV (b)
13301 && w->current_matrix->begv == BUF_BEGV (b))
13302 b->clip_changed = false;
13303
13304 /* If display wasn't paused, and W is not a tool bar window, see if
13305 point has been moved into or out of a composition. In that case,
13306 set b->clip_changed to force updating the screen. If
13307 b->clip_changed has already been set, skip this check. */
13308 if (!b->clip_changed && w->window_end_valid)
13309 {
13310 ptrdiff_t pt = (w == XWINDOW (selected_window)
13311 ? PT : marker_position (w->pointm));
13312
13313 if ((w->current_matrix->buffer != b || pt != w->last_point)
13314 && check_point_in_composition (w->current_matrix->buffer,
13315 w->last_point, b, pt))
13316 b->clip_changed = true;
13317 }
13318 }
13319
13320 static void
13321 propagate_buffer_redisplay (void)
13322 { /* Resetting b->text->redisplay is problematic!
13323 We can't just reset it in the case that some window that displays
13324 it has not been redisplayed; and such a window can stay
13325 unredisplayed for a long time if it's currently invisible.
13326 But we do want to reset it at the end of redisplay otherwise
13327 its displayed windows will keep being redisplayed over and over
13328 again.
13329 So we copy all b->text->redisplay flags up to their windows here,
13330 such that mark_window_display_accurate can safely reset
13331 b->text->redisplay. */
13332 Lisp_Object ws = window_list ();
13333 for (; CONSP (ws); ws = XCDR (ws))
13334 {
13335 struct window *thisw = XWINDOW (XCAR (ws));
13336 struct buffer *thisb = XBUFFER (thisw->contents);
13337 if (thisb->text->redisplay)
13338 thisw->redisplay = true;
13339 }
13340 }
13341
13342 #define STOP_POLLING \
13343 do { if (! polling_stopped_here) stop_polling (); \
13344 polling_stopped_here = true; } while (false)
13345
13346 #define RESUME_POLLING \
13347 do { if (polling_stopped_here) start_polling (); \
13348 polling_stopped_here = false; } while (false)
13349
13350
13351 /* Perhaps in the future avoid recentering windows if it
13352 is not necessary; currently that causes some problems. */
13353
13354 static void
13355 redisplay_internal (void)
13356 {
13357 struct window *w = XWINDOW (selected_window);
13358 struct window *sw;
13359 struct frame *fr;
13360 bool pending;
13361 bool must_finish = false, match_p;
13362 struct text_pos tlbufpos, tlendpos;
13363 int number_of_visible_frames;
13364 ptrdiff_t count;
13365 struct frame *sf;
13366 bool polling_stopped_here = false;
13367 Lisp_Object tail, frame;
13368
13369 /* True means redisplay has to consider all windows on all
13370 frames. False, only selected_window is considered. */
13371 bool consider_all_windows_p;
13372
13373 /* True means redisplay has to redisplay the miniwindow. */
13374 bool update_miniwindow_p = false;
13375
13376 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13377
13378 /* No redisplay if running in batch mode or frame is not yet fully
13379 initialized, or redisplay is explicitly turned off by setting
13380 Vinhibit_redisplay. */
13381 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13382 || !NILP (Vinhibit_redisplay))
13383 return;
13384
13385 /* Don't examine these until after testing Vinhibit_redisplay.
13386 When Emacs is shutting down, perhaps because its connection to
13387 X has dropped, we should not look at them at all. */
13388 fr = XFRAME (w->frame);
13389 sf = SELECTED_FRAME ();
13390
13391 if (!fr->glyphs_initialized_p)
13392 return;
13393
13394 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13395 if (popup_activated ())
13396 return;
13397 #endif
13398
13399 /* I don't think this happens but let's be paranoid. */
13400 if (redisplaying_p)
13401 return;
13402
13403 /* Record a function that clears redisplaying_p
13404 when we leave this function. */
13405 count = SPECPDL_INDEX ();
13406 record_unwind_protect_void (unwind_redisplay);
13407 redisplaying_p = true;
13408 specbind (Qinhibit_free_realized_faces, Qnil);
13409
13410 /* Record this function, so it appears on the profiler's backtraces. */
13411 record_in_backtrace (Qredisplay_internal, 0, 0);
13412
13413 FOR_EACH_FRAME (tail, frame)
13414 XFRAME (frame)->already_hscrolled_p = false;
13415
13416 retry:
13417 /* Remember the currently selected window. */
13418 sw = w;
13419
13420 pending = false;
13421 forget_escape_and_glyphless_faces ();
13422
13423 inhibit_free_realized_faces = false;
13424
13425 /* If face_change, init_iterator will free all realized faces, which
13426 includes the faces referenced from current matrices. So, we
13427 can't reuse current matrices in this case. */
13428 if (face_change)
13429 windows_or_buffers_changed = 47;
13430
13431 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13432 && FRAME_TTY (sf)->previous_frame != sf)
13433 {
13434 /* Since frames on a single ASCII terminal share the same
13435 display area, displaying a different frame means redisplay
13436 the whole thing. */
13437 SET_FRAME_GARBAGED (sf);
13438 #ifndef DOS_NT
13439 set_tty_color_mode (FRAME_TTY (sf), sf);
13440 #endif
13441 FRAME_TTY (sf)->previous_frame = sf;
13442 }
13443
13444 /* Set the visible flags for all frames. Do this before checking for
13445 resized or garbaged frames; they want to know if their frames are
13446 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13447 number_of_visible_frames = 0;
13448
13449 FOR_EACH_FRAME (tail, frame)
13450 {
13451 struct frame *f = XFRAME (frame);
13452
13453 if (FRAME_VISIBLE_P (f))
13454 {
13455 ++number_of_visible_frames;
13456 /* Adjust matrices for visible frames only. */
13457 if (f->fonts_changed)
13458 {
13459 adjust_frame_glyphs (f);
13460 /* Disable all redisplay optimizations for this frame.
13461 This is because adjust_frame_glyphs resets the
13462 enabled_p flag for all glyph rows of all windows, so
13463 many optimizations will fail anyway, and some might
13464 fail to test that flag and do bogus things as
13465 result. */
13466 SET_FRAME_GARBAGED (f);
13467 f->fonts_changed = false;
13468 }
13469 /* If cursor type has been changed on the frame
13470 other than selected, consider all frames. */
13471 if (f != sf && f->cursor_type_changed)
13472 fset_redisplay (f);
13473 }
13474 clear_desired_matrices (f);
13475 }
13476
13477 /* Notice any pending interrupt request to change frame size. */
13478 do_pending_window_change (true);
13479
13480 /* do_pending_window_change could change the selected_window due to
13481 frame resizing which makes the selected window too small. */
13482 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13483 sw = w;
13484
13485 /* Clear frames marked as garbaged. */
13486 clear_garbaged_frames ();
13487
13488 /* Build menubar and tool-bar items. */
13489 if (NILP (Vmemory_full))
13490 prepare_menu_bars ();
13491
13492 reconsider_clip_changes (w);
13493
13494 /* In most cases selected window displays current buffer. */
13495 match_p = XBUFFER (w->contents) == current_buffer;
13496 if (match_p)
13497 {
13498 /* Detect case that we need to write or remove a star in the mode line. */
13499 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13500 w->update_mode_line = true;
13501
13502 if (mode_line_update_needed (w))
13503 w->update_mode_line = true;
13504
13505 /* If reconsider_clip_changes above decided that the narrowing
13506 in the current buffer changed, make sure all other windows
13507 showing that buffer will be redisplayed. */
13508 if (current_buffer->clip_changed)
13509 bset_update_mode_line (current_buffer);
13510 }
13511
13512 /* Normally the message* functions will have already displayed and
13513 updated the echo area, but the frame may have been trashed, or
13514 the update may have been preempted, so display the echo area
13515 again here. Checking message_cleared_p captures the case that
13516 the echo area should be cleared. */
13517 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13518 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13519 || (message_cleared_p
13520 && minibuf_level == 0
13521 /* If the mini-window is currently selected, this means the
13522 echo-area doesn't show through. */
13523 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13524 {
13525 echo_area_display (false);
13526
13527 if (message_cleared_p)
13528 update_miniwindow_p = true;
13529
13530 must_finish = true;
13531
13532 /* If we don't display the current message, don't clear the
13533 message_cleared_p flag, because, if we did, we wouldn't clear
13534 the echo area in the next redisplay which doesn't preserve
13535 the echo area. */
13536 if (!display_last_displayed_message_p)
13537 message_cleared_p = false;
13538 }
13539 else if (EQ (selected_window, minibuf_window)
13540 && (current_buffer->clip_changed || window_outdated (w))
13541 && resize_mini_window (w, false))
13542 {
13543 /* Resized active mini-window to fit the size of what it is
13544 showing if its contents might have changed. */
13545 must_finish = true;
13546
13547 /* If window configuration was changed, frames may have been
13548 marked garbaged. Clear them or we will experience
13549 surprises wrt scrolling. */
13550 clear_garbaged_frames ();
13551 }
13552
13553 if (windows_or_buffers_changed && !update_mode_lines)
13554 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13555 only the windows's contents needs to be refreshed, or whether the
13556 mode-lines also need a refresh. */
13557 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13558 ? REDISPLAY_SOME : 32);
13559
13560 /* If specs for an arrow have changed, do thorough redisplay
13561 to ensure we remove any arrow that should no longer exist. */
13562 if (overlay_arrows_changed_p ())
13563 /* Apparently, this is the only case where we update other windows,
13564 without updating other mode-lines. */
13565 windows_or_buffers_changed = 49;
13566
13567 consider_all_windows_p = (update_mode_lines
13568 || windows_or_buffers_changed);
13569
13570 #define AINC(a,i) \
13571 { \
13572 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13573 if (INTEGERP (entry)) \
13574 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13575 }
13576
13577 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13578 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13579
13580 /* Optimize the case that only the line containing the cursor in the
13581 selected window has changed. Variables starting with this_ are
13582 set in display_line and record information about the line
13583 containing the cursor. */
13584 tlbufpos = this_line_start_pos;
13585 tlendpos = this_line_end_pos;
13586 if (!consider_all_windows_p
13587 && CHARPOS (tlbufpos) > 0
13588 && !w->update_mode_line
13589 && !current_buffer->clip_changed
13590 && !current_buffer->prevent_redisplay_optimizations_p
13591 && FRAME_VISIBLE_P (XFRAME (w->frame))
13592 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13593 && !XFRAME (w->frame)->cursor_type_changed
13594 && !XFRAME (w->frame)->face_change
13595 /* Make sure recorded data applies to current buffer, etc. */
13596 && this_line_buffer == current_buffer
13597 && match_p
13598 && !w->force_start
13599 && !w->optional_new_start
13600 /* Point must be on the line that we have info recorded about. */
13601 && PT >= CHARPOS (tlbufpos)
13602 && PT <= Z - CHARPOS (tlendpos)
13603 /* All text outside that line, including its final newline,
13604 must be unchanged. */
13605 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13606 CHARPOS (tlendpos)))
13607 {
13608 if (CHARPOS (tlbufpos) > BEGV
13609 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13610 && (CHARPOS (tlbufpos) == ZV
13611 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13612 /* Former continuation line has disappeared by becoming empty. */
13613 goto cancel;
13614 else if (window_outdated (w) || MINI_WINDOW_P (w))
13615 {
13616 /* We have to handle the case of continuation around a
13617 wide-column character (see the comment in indent.c around
13618 line 1340).
13619
13620 For instance, in the following case:
13621
13622 -------- Insert --------
13623 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13624 J_I_ ==> J_I_ `^^' are cursors.
13625 ^^ ^^
13626 -------- --------
13627
13628 As we have to redraw the line above, we cannot use this
13629 optimization. */
13630
13631 struct it it;
13632 int line_height_before = this_line_pixel_height;
13633
13634 /* Note that start_display will handle the case that the
13635 line starting at tlbufpos is a continuation line. */
13636 start_display (&it, w, tlbufpos);
13637
13638 /* Implementation note: It this still necessary? */
13639 if (it.current_x != this_line_start_x)
13640 goto cancel;
13641
13642 TRACE ((stderr, "trying display optimization 1\n"));
13643 w->cursor.vpos = -1;
13644 overlay_arrow_seen = false;
13645 it.vpos = this_line_vpos;
13646 it.current_y = this_line_y;
13647 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13648 display_line (&it);
13649
13650 /* If line contains point, is not continued,
13651 and ends at same distance from eob as before, we win. */
13652 if (w->cursor.vpos >= 0
13653 /* Line is not continued, otherwise this_line_start_pos
13654 would have been set to 0 in display_line. */
13655 && CHARPOS (this_line_start_pos)
13656 /* Line ends as before. */
13657 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13658 /* Line has same height as before. Otherwise other lines
13659 would have to be shifted up or down. */
13660 && this_line_pixel_height == line_height_before)
13661 {
13662 /* If this is not the window's last line, we must adjust
13663 the charstarts of the lines below. */
13664 if (it.current_y < it.last_visible_y)
13665 {
13666 struct glyph_row *row
13667 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13668 ptrdiff_t delta, delta_bytes;
13669
13670 /* We used to distinguish between two cases here,
13671 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13672 when the line ends in a newline or the end of the
13673 buffer's accessible portion. But both cases did
13674 the same, so they were collapsed. */
13675 delta = (Z
13676 - CHARPOS (tlendpos)
13677 - MATRIX_ROW_START_CHARPOS (row));
13678 delta_bytes = (Z_BYTE
13679 - BYTEPOS (tlendpos)
13680 - MATRIX_ROW_START_BYTEPOS (row));
13681
13682 increment_matrix_positions (w->current_matrix,
13683 this_line_vpos + 1,
13684 w->current_matrix->nrows,
13685 delta, delta_bytes);
13686 }
13687
13688 /* If this row displays text now but previously didn't,
13689 or vice versa, w->window_end_vpos may have to be
13690 adjusted. */
13691 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13692 {
13693 if (w->window_end_vpos < this_line_vpos)
13694 w->window_end_vpos = this_line_vpos;
13695 }
13696 else if (w->window_end_vpos == this_line_vpos
13697 && this_line_vpos > 0)
13698 w->window_end_vpos = this_line_vpos - 1;
13699 w->window_end_valid = false;
13700
13701 /* Update hint: No need to try to scroll in update_window. */
13702 w->desired_matrix->no_scrolling_p = true;
13703
13704 #ifdef GLYPH_DEBUG
13705 *w->desired_matrix->method = 0;
13706 debug_method_add (w, "optimization 1");
13707 #endif
13708 #ifdef HAVE_WINDOW_SYSTEM
13709 update_window_fringes (w, false);
13710 #endif
13711 goto update;
13712 }
13713 else
13714 goto cancel;
13715 }
13716 else if (/* Cursor position hasn't changed. */
13717 PT == w->last_point
13718 /* Make sure the cursor was last displayed
13719 in this window. Otherwise we have to reposition it. */
13720
13721 /* PXW: Must be converted to pixels, probably. */
13722 && 0 <= w->cursor.vpos
13723 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13724 {
13725 if (!must_finish)
13726 {
13727 do_pending_window_change (true);
13728 /* If selected_window changed, redisplay again. */
13729 if (WINDOWP (selected_window)
13730 && (w = XWINDOW (selected_window)) != sw)
13731 goto retry;
13732
13733 /* We used to always goto end_of_redisplay here, but this
13734 isn't enough if we have a blinking cursor. */
13735 if (w->cursor_off_p == w->last_cursor_off_p)
13736 goto end_of_redisplay;
13737 }
13738 goto update;
13739 }
13740 /* If highlighting the region, or if the cursor is in the echo area,
13741 then we can't just move the cursor. */
13742 else if (NILP (Vshow_trailing_whitespace)
13743 && !cursor_in_echo_area)
13744 {
13745 struct it it;
13746 struct glyph_row *row;
13747
13748 /* Skip from tlbufpos to PT and see where it is. Note that
13749 PT may be in invisible text. If so, we will end at the
13750 next visible position. */
13751 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13752 NULL, DEFAULT_FACE_ID);
13753 it.current_x = this_line_start_x;
13754 it.current_y = this_line_y;
13755 it.vpos = this_line_vpos;
13756
13757 /* The call to move_it_to stops in front of PT, but
13758 moves over before-strings. */
13759 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13760
13761 if (it.vpos == this_line_vpos
13762 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13763 row->enabled_p))
13764 {
13765 eassert (this_line_vpos == it.vpos);
13766 eassert (this_line_y == it.current_y);
13767 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13768 #ifdef GLYPH_DEBUG
13769 *w->desired_matrix->method = 0;
13770 debug_method_add (w, "optimization 3");
13771 #endif
13772 goto update;
13773 }
13774 else
13775 goto cancel;
13776 }
13777
13778 cancel:
13779 /* Text changed drastically or point moved off of line. */
13780 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13781 }
13782
13783 CHARPOS (this_line_start_pos) = 0;
13784 ++clear_face_cache_count;
13785 #ifdef HAVE_WINDOW_SYSTEM
13786 ++clear_image_cache_count;
13787 #endif
13788
13789 /* Build desired matrices, and update the display. If
13790 consider_all_windows_p, do it for all windows on all frames that
13791 require redisplay, as specified by their 'redisplay' flag.
13792 Otherwise do it for selected_window, only. */
13793
13794 if (consider_all_windows_p)
13795 {
13796 FOR_EACH_FRAME (tail, frame)
13797 XFRAME (frame)->updated_p = false;
13798
13799 propagate_buffer_redisplay ();
13800
13801 FOR_EACH_FRAME (tail, frame)
13802 {
13803 struct frame *f = XFRAME (frame);
13804
13805 /* We don't have to do anything for unselected terminal
13806 frames. */
13807 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13808 && !EQ (FRAME_TTY (f)->top_frame, frame))
13809 continue;
13810
13811 retry_frame:
13812 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13813 {
13814 bool gcscrollbars
13815 /* Only GC scrollbars when we redisplay the whole frame. */
13816 = f->redisplay || !REDISPLAY_SOME_P ();
13817 bool f_redisplay_flag = f->redisplay;
13818 /* Mark all the scroll bars to be removed; we'll redeem
13819 the ones we want when we redisplay their windows. */
13820 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13821 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13822
13823 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13824 redisplay_windows (FRAME_ROOT_WINDOW (f));
13825 /* Remember that the invisible frames need to be redisplayed next
13826 time they're visible. */
13827 else if (!REDISPLAY_SOME_P ())
13828 f->redisplay = true;
13829
13830 /* The X error handler may have deleted that frame. */
13831 if (!FRAME_LIVE_P (f))
13832 continue;
13833
13834 /* Any scroll bars which redisplay_windows should have
13835 nuked should now go away. */
13836 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13837 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13838
13839 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13840 {
13841 /* If fonts changed on visible frame, display again. */
13842 if (f->fonts_changed)
13843 {
13844 adjust_frame_glyphs (f);
13845 /* Disable all redisplay optimizations for this
13846 frame. For the reasons, see the comment near
13847 the previous call to adjust_frame_glyphs above. */
13848 SET_FRAME_GARBAGED (f);
13849 f->fonts_changed = false;
13850 goto retry_frame;
13851 }
13852
13853 /* See if we have to hscroll. */
13854 if (!f->already_hscrolled_p)
13855 {
13856 f->already_hscrolled_p = true;
13857 if (hscroll_windows (f->root_window))
13858 goto retry_frame;
13859 }
13860
13861 /* If the frame's redisplay flag was not set before
13862 we went about redisplaying its windows, but it is
13863 set now, that means we employed some redisplay
13864 optimizations inside redisplay_windows, and
13865 bypassed producing some screen lines. But if
13866 f->redisplay is now set, it might mean the old
13867 faces are no longer valid (e.g., if redisplaying
13868 some window called some Lisp which defined a new
13869 face or redefined an existing face), so trying to
13870 use them in update_frame will segfault.
13871 Therefore, we must redisplay this frame. */
13872 if (!f_redisplay_flag && f->redisplay)
13873 goto retry_frame;
13874
13875 /* Prevent various kinds of signals during display
13876 update. stdio is not robust about handling
13877 signals, which can cause an apparent I/O error. */
13878 if (interrupt_input)
13879 unrequest_sigio ();
13880 STOP_POLLING;
13881
13882 pending |= update_frame (f, false, false);
13883 f->cursor_type_changed = false;
13884 f->updated_p = true;
13885 }
13886 }
13887 }
13888
13889 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13890
13891 if (!pending)
13892 {
13893 /* Do the mark_window_display_accurate after all windows have
13894 been redisplayed because this call resets flags in buffers
13895 which are needed for proper redisplay. */
13896 FOR_EACH_FRAME (tail, frame)
13897 {
13898 struct frame *f = XFRAME (frame);
13899 if (f->updated_p)
13900 {
13901 f->redisplay = false;
13902 mark_window_display_accurate (f->root_window, true);
13903 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13904 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13905 }
13906 }
13907 }
13908 }
13909 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13910 {
13911 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13912 struct frame *mini_frame;
13913
13914 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13915 /* Use list_of_error, not Qerror, so that
13916 we catch only errors and don't run the debugger. */
13917 internal_condition_case_1 (redisplay_window_1, selected_window,
13918 list_of_error,
13919 redisplay_window_error);
13920 if (update_miniwindow_p)
13921 internal_condition_case_1 (redisplay_window_1, mini_window,
13922 list_of_error,
13923 redisplay_window_error);
13924
13925 /* Compare desired and current matrices, perform output. */
13926
13927 update:
13928 /* If fonts changed, display again. Likewise if redisplay_window_1
13929 above caused some change (e.g., a change in faces) that requires
13930 considering the entire frame again. */
13931 if (sf->fonts_changed || sf->redisplay)
13932 {
13933 if (sf->redisplay)
13934 {
13935 /* Set this to force a more thorough redisplay.
13936 Otherwise, we might immediately loop back to the
13937 above "else-if" clause (since all the conditions that
13938 led here might still be true), and we will then
13939 infloop, because the selected-frame's redisplay flag
13940 is not (and cannot be) reset. */
13941 windows_or_buffers_changed = 50;
13942 }
13943 goto retry;
13944 }
13945
13946 /* Prevent freeing of realized faces, since desired matrices are
13947 pending that reference the faces we computed and cached. */
13948 inhibit_free_realized_faces = true;
13949
13950 /* Prevent various kinds of signals during display update.
13951 stdio is not robust about handling signals,
13952 which can cause an apparent I/O error. */
13953 if (interrupt_input)
13954 unrequest_sigio ();
13955 STOP_POLLING;
13956
13957 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13958 {
13959 if (hscroll_windows (selected_window))
13960 goto retry;
13961
13962 XWINDOW (selected_window)->must_be_updated_p = true;
13963 pending = update_frame (sf, false, false);
13964 sf->cursor_type_changed = false;
13965 }
13966
13967 /* We may have called echo_area_display at the top of this
13968 function. If the echo area is on another frame, that may
13969 have put text on a frame other than the selected one, so the
13970 above call to update_frame would not have caught it. Catch
13971 it here. */
13972 mini_window = FRAME_MINIBUF_WINDOW (sf);
13973 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13974
13975 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13976 {
13977 XWINDOW (mini_window)->must_be_updated_p = true;
13978 pending |= update_frame (mini_frame, false, false);
13979 mini_frame->cursor_type_changed = false;
13980 if (!pending && hscroll_windows (mini_window))
13981 goto retry;
13982 }
13983 }
13984
13985 /* If display was paused because of pending input, make sure we do a
13986 thorough update the next time. */
13987 if (pending)
13988 {
13989 /* Prevent the optimization at the beginning of
13990 redisplay_internal that tries a single-line update of the
13991 line containing the cursor in the selected window. */
13992 CHARPOS (this_line_start_pos) = 0;
13993
13994 /* Let the overlay arrow be updated the next time. */
13995 update_overlay_arrows (0);
13996
13997 /* If we pause after scrolling, some rows in the current
13998 matrices of some windows are not valid. */
13999 if (!WINDOW_FULL_WIDTH_P (w)
14000 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14001 update_mode_lines = 36;
14002 }
14003 else
14004 {
14005 if (!consider_all_windows_p)
14006 {
14007 /* This has already been done above if
14008 consider_all_windows_p is set. */
14009 if (XBUFFER (w->contents)->text->redisplay
14010 && buffer_window_count (XBUFFER (w->contents)) > 1)
14011 /* This can happen if b->text->redisplay was set during
14012 jit-lock. */
14013 propagate_buffer_redisplay ();
14014 mark_window_display_accurate_1 (w, true);
14015
14016 /* Say overlay arrows are up to date. */
14017 update_overlay_arrows (1);
14018
14019 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14020 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14021 }
14022
14023 update_mode_lines = 0;
14024 windows_or_buffers_changed = 0;
14025 }
14026
14027 /* Start SIGIO interrupts coming again. Having them off during the
14028 code above makes it less likely one will discard output, but not
14029 impossible, since there might be stuff in the system buffer here.
14030 But it is much hairier to try to do anything about that. */
14031 if (interrupt_input)
14032 request_sigio ();
14033 RESUME_POLLING;
14034
14035 /* If a frame has become visible which was not before, redisplay
14036 again, so that we display it. Expose events for such a frame
14037 (which it gets when becoming visible) don't call the parts of
14038 redisplay constructing glyphs, so simply exposing a frame won't
14039 display anything in this case. So, we have to display these
14040 frames here explicitly. */
14041 if (!pending)
14042 {
14043 int new_count = 0;
14044
14045 FOR_EACH_FRAME (tail, frame)
14046 {
14047 if (XFRAME (frame)->visible)
14048 new_count++;
14049 }
14050
14051 if (new_count != number_of_visible_frames)
14052 windows_or_buffers_changed = 52;
14053 }
14054
14055 /* Change frame size now if a change is pending. */
14056 do_pending_window_change (true);
14057
14058 /* If we just did a pending size change, or have additional
14059 visible frames, or selected_window changed, redisplay again. */
14060 if ((windows_or_buffers_changed && !pending)
14061 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14062 goto retry;
14063
14064 /* Clear the face and image caches.
14065
14066 We used to do this only if consider_all_windows_p. But the cache
14067 needs to be cleared if a timer creates images in the current
14068 buffer (e.g. the test case in Bug#6230). */
14069
14070 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14071 {
14072 clear_face_cache (false);
14073 clear_face_cache_count = 0;
14074 }
14075
14076 #ifdef HAVE_WINDOW_SYSTEM
14077 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14078 {
14079 clear_image_caches (Qnil);
14080 clear_image_cache_count = 0;
14081 }
14082 #endif /* HAVE_WINDOW_SYSTEM */
14083
14084 end_of_redisplay:
14085 #ifdef HAVE_NS
14086 ns_set_doc_edited ();
14087 #endif
14088 if (interrupt_input && interrupts_deferred)
14089 request_sigio ();
14090
14091 unbind_to (count, Qnil);
14092 RESUME_POLLING;
14093 }
14094
14095
14096 /* Redisplay, but leave alone any recent echo area message unless
14097 another message has been requested in its place.
14098
14099 This is useful in situations where you need to redisplay but no
14100 user action has occurred, making it inappropriate for the message
14101 area to be cleared. See tracking_off and
14102 wait_reading_process_output for examples of these situations.
14103
14104 FROM_WHERE is an integer saying from where this function was
14105 called. This is useful for debugging. */
14106
14107 void
14108 redisplay_preserve_echo_area (int from_where)
14109 {
14110 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14111
14112 if (!NILP (echo_area_buffer[1]))
14113 {
14114 /* We have a previously displayed message, but no current
14115 message. Redisplay the previous message. */
14116 display_last_displayed_message_p = true;
14117 redisplay_internal ();
14118 display_last_displayed_message_p = false;
14119 }
14120 else
14121 redisplay_internal ();
14122
14123 flush_frame (SELECTED_FRAME ());
14124 }
14125
14126
14127 /* Function registered with record_unwind_protect in redisplay_internal. */
14128
14129 static void
14130 unwind_redisplay (void)
14131 {
14132 redisplaying_p = false;
14133 }
14134
14135
14136 /* Mark the display of leaf window W as accurate or inaccurate.
14137 If ACCURATE_P, mark display of W as accurate.
14138 If !ACCURATE_P, arrange for W to be redisplayed the next
14139 time redisplay_internal is called. */
14140
14141 static void
14142 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14143 {
14144 struct buffer *b = XBUFFER (w->contents);
14145
14146 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14147 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14148 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14149
14150 if (accurate_p)
14151 {
14152 b->clip_changed = false;
14153 b->prevent_redisplay_optimizations_p = false;
14154 eassert (buffer_window_count (b) > 0);
14155 /* Resetting b->text->redisplay is problematic!
14156 In order to make it safer to do it here, redisplay_internal must
14157 have copied all b->text->redisplay to their respective windows. */
14158 b->text->redisplay = false;
14159
14160 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14161 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14162 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14163 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14164
14165 w->current_matrix->buffer = b;
14166 w->current_matrix->begv = BUF_BEGV (b);
14167 w->current_matrix->zv = BUF_ZV (b);
14168
14169 w->last_cursor_vpos = w->cursor.vpos;
14170 w->last_cursor_off_p = w->cursor_off_p;
14171
14172 if (w == XWINDOW (selected_window))
14173 w->last_point = BUF_PT (b);
14174 else
14175 w->last_point = marker_position (w->pointm);
14176
14177 w->window_end_valid = true;
14178 w->update_mode_line = false;
14179 }
14180
14181 w->redisplay = !accurate_p;
14182 }
14183
14184
14185 /* Mark the display of windows in the window tree rooted at WINDOW as
14186 accurate or inaccurate. If ACCURATE_P, mark display of
14187 windows as accurate. If !ACCURATE_P, arrange for windows to
14188 be redisplayed the next time redisplay_internal is called. */
14189
14190 void
14191 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14192 {
14193 struct window *w;
14194
14195 for (; !NILP (window); window = w->next)
14196 {
14197 w = XWINDOW (window);
14198 if (WINDOWP (w->contents))
14199 mark_window_display_accurate (w->contents, accurate_p);
14200 else
14201 mark_window_display_accurate_1 (w, accurate_p);
14202 }
14203
14204 if (accurate_p)
14205 update_overlay_arrows (1);
14206 else
14207 /* Force a thorough redisplay the next time by setting
14208 last_arrow_position and last_arrow_string to t, which is
14209 unequal to any useful value of Voverlay_arrow_... */
14210 update_overlay_arrows (-1);
14211 }
14212
14213
14214 /* Return value in display table DP (Lisp_Char_Table *) for character
14215 C. Since a display table doesn't have any parent, we don't have to
14216 follow parent. Do not call this function directly but use the
14217 macro DISP_CHAR_VECTOR. */
14218
14219 Lisp_Object
14220 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14221 {
14222 Lisp_Object val;
14223
14224 if (ASCII_CHAR_P (c))
14225 {
14226 val = dp->ascii;
14227 if (SUB_CHAR_TABLE_P (val))
14228 val = XSUB_CHAR_TABLE (val)->contents[c];
14229 }
14230 else
14231 {
14232 Lisp_Object table;
14233
14234 XSETCHAR_TABLE (table, dp);
14235 val = char_table_ref (table, c);
14236 }
14237 if (NILP (val))
14238 val = dp->defalt;
14239 return val;
14240 }
14241
14242
14243 \f
14244 /***********************************************************************
14245 Window Redisplay
14246 ***********************************************************************/
14247
14248 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14249
14250 static void
14251 redisplay_windows (Lisp_Object window)
14252 {
14253 while (!NILP (window))
14254 {
14255 struct window *w = XWINDOW (window);
14256
14257 if (WINDOWP (w->contents))
14258 redisplay_windows (w->contents);
14259 else if (BUFFERP (w->contents))
14260 {
14261 displayed_buffer = XBUFFER (w->contents);
14262 /* Use list_of_error, not Qerror, so that
14263 we catch only errors and don't run the debugger. */
14264 internal_condition_case_1 (redisplay_window_0, window,
14265 list_of_error,
14266 redisplay_window_error);
14267 }
14268
14269 window = w->next;
14270 }
14271 }
14272
14273 static Lisp_Object
14274 redisplay_window_error (Lisp_Object ignore)
14275 {
14276 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14277 return Qnil;
14278 }
14279
14280 static Lisp_Object
14281 redisplay_window_0 (Lisp_Object window)
14282 {
14283 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14284 redisplay_window (window, false);
14285 return Qnil;
14286 }
14287
14288 static Lisp_Object
14289 redisplay_window_1 (Lisp_Object window)
14290 {
14291 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14292 redisplay_window (window, true);
14293 return Qnil;
14294 }
14295 \f
14296
14297 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14298 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14299 which positions recorded in ROW differ from current buffer
14300 positions.
14301
14302 Return true iff cursor is on this row. */
14303
14304 static bool
14305 set_cursor_from_row (struct window *w, struct glyph_row *row,
14306 struct glyph_matrix *matrix,
14307 ptrdiff_t delta, ptrdiff_t delta_bytes,
14308 int dy, int dvpos)
14309 {
14310 struct glyph *glyph = row->glyphs[TEXT_AREA];
14311 struct glyph *end = glyph + row->used[TEXT_AREA];
14312 struct glyph *cursor = NULL;
14313 /* The last known character position in row. */
14314 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14315 int x = row->x;
14316 ptrdiff_t pt_old = PT - delta;
14317 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14318 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14319 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14320 /* A glyph beyond the edge of TEXT_AREA which we should never
14321 touch. */
14322 struct glyph *glyphs_end = end;
14323 /* True means we've found a match for cursor position, but that
14324 glyph has the avoid_cursor_p flag set. */
14325 bool match_with_avoid_cursor = false;
14326 /* True means we've seen at least one glyph that came from a
14327 display string. */
14328 bool string_seen = false;
14329 /* Largest and smallest buffer positions seen so far during scan of
14330 glyph row. */
14331 ptrdiff_t bpos_max = pos_before;
14332 ptrdiff_t bpos_min = pos_after;
14333 /* Last buffer position covered by an overlay string with an integer
14334 `cursor' property. */
14335 ptrdiff_t bpos_covered = 0;
14336 /* True means the display string on which to display the cursor
14337 comes from a text property, not from an overlay. */
14338 bool string_from_text_prop = false;
14339
14340 /* Don't even try doing anything if called for a mode-line or
14341 header-line row, since the rest of the code isn't prepared to
14342 deal with such calamities. */
14343 eassert (!row->mode_line_p);
14344 if (row->mode_line_p)
14345 return false;
14346
14347 /* Skip over glyphs not having an object at the start and the end of
14348 the row. These are special glyphs like truncation marks on
14349 terminal frames. */
14350 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14351 {
14352 if (!row->reversed_p)
14353 {
14354 while (glyph < end
14355 && NILP (glyph->object)
14356 && glyph->charpos < 0)
14357 {
14358 x += glyph->pixel_width;
14359 ++glyph;
14360 }
14361 while (end > glyph
14362 && NILP ((end - 1)->object)
14363 /* CHARPOS is zero for blanks and stretch glyphs
14364 inserted by extend_face_to_end_of_line. */
14365 && (end - 1)->charpos <= 0)
14366 --end;
14367 glyph_before = glyph - 1;
14368 glyph_after = end;
14369 }
14370 else
14371 {
14372 struct glyph *g;
14373
14374 /* If the glyph row is reversed, we need to process it from back
14375 to front, so swap the edge pointers. */
14376 glyphs_end = end = glyph - 1;
14377 glyph += row->used[TEXT_AREA] - 1;
14378
14379 while (glyph > end + 1
14380 && NILP (glyph->object)
14381 && glyph->charpos < 0)
14382 {
14383 --glyph;
14384 x -= glyph->pixel_width;
14385 }
14386 if (NILP (glyph->object) && glyph->charpos < 0)
14387 --glyph;
14388 /* By default, in reversed rows we put the cursor on the
14389 rightmost (first in the reading order) glyph. */
14390 for (g = end + 1; g < glyph; g++)
14391 x += g->pixel_width;
14392 while (end < glyph
14393 && NILP ((end + 1)->object)
14394 && (end + 1)->charpos <= 0)
14395 ++end;
14396 glyph_before = glyph + 1;
14397 glyph_after = end;
14398 }
14399 }
14400 else if (row->reversed_p)
14401 {
14402 /* In R2L rows that don't display text, put the cursor on the
14403 rightmost glyph. Case in point: an empty last line that is
14404 part of an R2L paragraph. */
14405 cursor = end - 1;
14406 /* Avoid placing the cursor on the last glyph of the row, where
14407 on terminal frames we hold the vertical border between
14408 adjacent windows. */
14409 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14410 && !WINDOW_RIGHTMOST_P (w)
14411 && cursor == row->glyphs[LAST_AREA] - 1)
14412 cursor--;
14413 x = -1; /* will be computed below, at label compute_x */
14414 }
14415
14416 /* Step 1: Try to find the glyph whose character position
14417 corresponds to point. If that's not possible, find 2 glyphs
14418 whose character positions are the closest to point, one before
14419 point, the other after it. */
14420 if (!row->reversed_p)
14421 while (/* not marched to end of glyph row */
14422 glyph < end
14423 /* glyph was not inserted by redisplay for internal purposes */
14424 && !NILP (glyph->object))
14425 {
14426 if (BUFFERP (glyph->object))
14427 {
14428 ptrdiff_t dpos = glyph->charpos - pt_old;
14429
14430 if (glyph->charpos > bpos_max)
14431 bpos_max = glyph->charpos;
14432 if (glyph->charpos < bpos_min)
14433 bpos_min = glyph->charpos;
14434 if (!glyph->avoid_cursor_p)
14435 {
14436 /* If we hit point, we've found the glyph on which to
14437 display the cursor. */
14438 if (dpos == 0)
14439 {
14440 match_with_avoid_cursor = false;
14441 break;
14442 }
14443 /* See if we've found a better approximation to
14444 POS_BEFORE or to POS_AFTER. */
14445 if (0 > dpos && dpos > pos_before - pt_old)
14446 {
14447 pos_before = glyph->charpos;
14448 glyph_before = glyph;
14449 }
14450 else if (0 < dpos && dpos < pos_after - pt_old)
14451 {
14452 pos_after = glyph->charpos;
14453 glyph_after = glyph;
14454 }
14455 }
14456 else if (dpos == 0)
14457 match_with_avoid_cursor = true;
14458 }
14459 else if (STRINGP (glyph->object))
14460 {
14461 Lisp_Object chprop;
14462 ptrdiff_t glyph_pos = glyph->charpos;
14463
14464 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14465 glyph->object);
14466 if (!NILP (chprop))
14467 {
14468 /* If the string came from a `display' text property,
14469 look up the buffer position of that property and
14470 use that position to update bpos_max, as if we
14471 actually saw such a position in one of the row's
14472 glyphs. This helps with supporting integer values
14473 of `cursor' property on the display string in
14474 situations where most or all of the row's buffer
14475 text is completely covered by display properties,
14476 so that no glyph with valid buffer positions is
14477 ever seen in the row. */
14478 ptrdiff_t prop_pos =
14479 string_buffer_position_lim (glyph->object, pos_before,
14480 pos_after, false);
14481
14482 if (prop_pos >= pos_before)
14483 bpos_max = prop_pos;
14484 }
14485 if (INTEGERP (chprop))
14486 {
14487 bpos_covered = bpos_max + XINT (chprop);
14488 /* If the `cursor' property covers buffer positions up
14489 to and including point, we should display cursor on
14490 this glyph. Note that, if a `cursor' property on one
14491 of the string's characters has an integer value, we
14492 will break out of the loop below _before_ we get to
14493 the position match above. IOW, integer values of
14494 the `cursor' property override the "exact match for
14495 point" strategy of positioning the cursor. */
14496 /* Implementation note: bpos_max == pt_old when, e.g.,
14497 we are in an empty line, where bpos_max is set to
14498 MATRIX_ROW_START_CHARPOS, see above. */
14499 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14500 {
14501 cursor = glyph;
14502 break;
14503 }
14504 }
14505
14506 string_seen = true;
14507 }
14508 x += glyph->pixel_width;
14509 ++glyph;
14510 }
14511 else if (glyph > end) /* row is reversed */
14512 while (!NILP (glyph->object))
14513 {
14514 if (BUFFERP (glyph->object))
14515 {
14516 ptrdiff_t dpos = glyph->charpos - pt_old;
14517
14518 if (glyph->charpos > bpos_max)
14519 bpos_max = glyph->charpos;
14520 if (glyph->charpos < bpos_min)
14521 bpos_min = glyph->charpos;
14522 if (!glyph->avoid_cursor_p)
14523 {
14524 if (dpos == 0)
14525 {
14526 match_with_avoid_cursor = false;
14527 break;
14528 }
14529 if (0 > dpos && dpos > pos_before - pt_old)
14530 {
14531 pos_before = glyph->charpos;
14532 glyph_before = glyph;
14533 }
14534 else if (0 < dpos && dpos < pos_after - pt_old)
14535 {
14536 pos_after = glyph->charpos;
14537 glyph_after = glyph;
14538 }
14539 }
14540 else if (dpos == 0)
14541 match_with_avoid_cursor = true;
14542 }
14543 else if (STRINGP (glyph->object))
14544 {
14545 Lisp_Object chprop;
14546 ptrdiff_t glyph_pos = glyph->charpos;
14547
14548 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14549 glyph->object);
14550 if (!NILP (chprop))
14551 {
14552 ptrdiff_t prop_pos =
14553 string_buffer_position_lim (glyph->object, pos_before,
14554 pos_after, false);
14555
14556 if (prop_pos >= pos_before)
14557 bpos_max = prop_pos;
14558 }
14559 if (INTEGERP (chprop))
14560 {
14561 bpos_covered = bpos_max + XINT (chprop);
14562 /* If the `cursor' property covers buffer positions up
14563 to and including point, we should display cursor on
14564 this glyph. */
14565 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14566 {
14567 cursor = glyph;
14568 break;
14569 }
14570 }
14571 string_seen = true;
14572 }
14573 --glyph;
14574 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14575 {
14576 x--; /* can't use any pixel_width */
14577 break;
14578 }
14579 x -= glyph->pixel_width;
14580 }
14581
14582 /* Step 2: If we didn't find an exact match for point, we need to
14583 look for a proper place to put the cursor among glyphs between
14584 GLYPH_BEFORE and GLYPH_AFTER. */
14585 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14586 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14587 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14588 {
14589 /* An empty line has a single glyph whose OBJECT is nil and
14590 whose CHARPOS is the position of a newline on that line.
14591 Note that on a TTY, there are more glyphs after that, which
14592 were produced by extend_face_to_end_of_line, but their
14593 CHARPOS is zero or negative. */
14594 bool empty_line_p =
14595 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14596 && NILP (glyph->object) && glyph->charpos > 0
14597 /* On a TTY, continued and truncated rows also have a glyph at
14598 their end whose OBJECT is nil and whose CHARPOS is
14599 positive (the continuation and truncation glyphs), but such
14600 rows are obviously not "empty". */
14601 && !(row->continued_p || row->truncated_on_right_p));
14602
14603 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14604 {
14605 ptrdiff_t ellipsis_pos;
14606
14607 /* Scan back over the ellipsis glyphs. */
14608 if (!row->reversed_p)
14609 {
14610 ellipsis_pos = (glyph - 1)->charpos;
14611 while (glyph > row->glyphs[TEXT_AREA]
14612 && (glyph - 1)->charpos == ellipsis_pos)
14613 glyph--, x -= glyph->pixel_width;
14614 /* That loop always goes one position too far, including
14615 the glyph before the ellipsis. So scan forward over
14616 that one. */
14617 x += glyph->pixel_width;
14618 glyph++;
14619 }
14620 else /* row is reversed */
14621 {
14622 ellipsis_pos = (glyph + 1)->charpos;
14623 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14624 && (glyph + 1)->charpos == ellipsis_pos)
14625 glyph++, x += glyph->pixel_width;
14626 x -= glyph->pixel_width;
14627 glyph--;
14628 }
14629 }
14630 else if (match_with_avoid_cursor)
14631 {
14632 cursor = glyph_after;
14633 x = -1;
14634 }
14635 else if (string_seen)
14636 {
14637 int incr = row->reversed_p ? -1 : +1;
14638
14639 /* Need to find the glyph that came out of a string which is
14640 present at point. That glyph is somewhere between
14641 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14642 positioned between POS_BEFORE and POS_AFTER in the
14643 buffer. */
14644 struct glyph *start, *stop;
14645 ptrdiff_t pos = pos_before;
14646
14647 x = -1;
14648
14649 /* If the row ends in a newline from a display string,
14650 reordering could have moved the glyphs belonging to the
14651 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14652 in this case we extend the search to the last glyph in
14653 the row that was not inserted by redisplay. */
14654 if (row->ends_in_newline_from_string_p)
14655 {
14656 glyph_after = end;
14657 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14658 }
14659
14660 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14661 correspond to POS_BEFORE and POS_AFTER, respectively. We
14662 need START and STOP in the order that corresponds to the
14663 row's direction as given by its reversed_p flag. If the
14664 directionality of characters between POS_BEFORE and
14665 POS_AFTER is the opposite of the row's base direction,
14666 these characters will have been reordered for display,
14667 and we need to reverse START and STOP. */
14668 if (!row->reversed_p)
14669 {
14670 start = min (glyph_before, glyph_after);
14671 stop = max (glyph_before, glyph_after);
14672 }
14673 else
14674 {
14675 start = max (glyph_before, glyph_after);
14676 stop = min (glyph_before, glyph_after);
14677 }
14678 for (glyph = start + incr;
14679 row->reversed_p ? glyph > stop : glyph < stop; )
14680 {
14681
14682 /* Any glyphs that come from the buffer are here because
14683 of bidi reordering. Skip them, and only pay
14684 attention to glyphs that came from some string. */
14685 if (STRINGP (glyph->object))
14686 {
14687 Lisp_Object str;
14688 ptrdiff_t tem;
14689 /* If the display property covers the newline, we
14690 need to search for it one position farther. */
14691 ptrdiff_t lim = pos_after
14692 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14693
14694 string_from_text_prop = false;
14695 str = glyph->object;
14696 tem = string_buffer_position_lim (str, pos, lim, false);
14697 if (tem == 0 /* from overlay */
14698 || pos <= tem)
14699 {
14700 /* If the string from which this glyph came is
14701 found in the buffer at point, or at position
14702 that is closer to point than pos_after, then
14703 we've found the glyph we've been looking for.
14704 If it comes from an overlay (tem == 0), and
14705 it has the `cursor' property on one of its
14706 glyphs, record that glyph as a candidate for
14707 displaying the cursor. (As in the
14708 unidirectional version, we will display the
14709 cursor on the last candidate we find.) */
14710 if (tem == 0
14711 || tem == pt_old
14712 || (tem - pt_old > 0 && tem < pos_after))
14713 {
14714 /* The glyphs from this string could have
14715 been reordered. Find the one with the
14716 smallest string position. Or there could
14717 be a character in the string with the
14718 `cursor' property, which means display
14719 cursor on that character's glyph. */
14720 ptrdiff_t strpos = glyph->charpos;
14721
14722 if (tem)
14723 {
14724 cursor = glyph;
14725 string_from_text_prop = true;
14726 }
14727 for ( ;
14728 (row->reversed_p ? glyph > stop : glyph < stop)
14729 && EQ (glyph->object, str);
14730 glyph += incr)
14731 {
14732 Lisp_Object cprop;
14733 ptrdiff_t gpos = glyph->charpos;
14734
14735 cprop = Fget_char_property (make_number (gpos),
14736 Qcursor,
14737 glyph->object);
14738 if (!NILP (cprop))
14739 {
14740 cursor = glyph;
14741 break;
14742 }
14743 if (tem && glyph->charpos < strpos)
14744 {
14745 strpos = glyph->charpos;
14746 cursor = glyph;
14747 }
14748 }
14749
14750 if (tem == pt_old
14751 || (tem - pt_old > 0 && tem < pos_after))
14752 goto compute_x;
14753 }
14754 if (tem)
14755 pos = tem + 1; /* don't find previous instances */
14756 }
14757 /* This string is not what we want; skip all of the
14758 glyphs that came from it. */
14759 while ((row->reversed_p ? glyph > stop : glyph < stop)
14760 && EQ (glyph->object, str))
14761 glyph += incr;
14762 }
14763 else
14764 glyph += incr;
14765 }
14766
14767 /* If we reached the end of the line, and END was from a string,
14768 the cursor is not on this line. */
14769 if (cursor == NULL
14770 && (row->reversed_p ? glyph <= end : glyph >= end)
14771 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14772 && STRINGP (end->object)
14773 && row->continued_p)
14774 return false;
14775 }
14776 /* A truncated row may not include PT among its character positions.
14777 Setting the cursor inside the scroll margin will trigger
14778 recalculation of hscroll in hscroll_window_tree. But if a
14779 display string covers point, defer to the string-handling
14780 code below to figure this out. */
14781 else if (row->truncated_on_left_p && pt_old < bpos_min)
14782 {
14783 cursor = glyph_before;
14784 x = -1;
14785 }
14786 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14787 /* Zero-width characters produce no glyphs. */
14788 || (!empty_line_p
14789 && (row->reversed_p
14790 ? glyph_after > glyphs_end
14791 : glyph_after < glyphs_end)))
14792 {
14793 cursor = glyph_after;
14794 x = -1;
14795 }
14796 }
14797
14798 compute_x:
14799 if (cursor != NULL)
14800 glyph = cursor;
14801 else if (glyph == glyphs_end
14802 && pos_before == pos_after
14803 && STRINGP ((row->reversed_p
14804 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14805 : row->glyphs[TEXT_AREA])->object))
14806 {
14807 /* If all the glyphs of this row came from strings, put the
14808 cursor on the first glyph of the row. This avoids having the
14809 cursor outside of the text area in this very rare and hard
14810 use case. */
14811 glyph =
14812 row->reversed_p
14813 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14814 : row->glyphs[TEXT_AREA];
14815 }
14816 if (x < 0)
14817 {
14818 struct glyph *g;
14819
14820 /* Need to compute x that corresponds to GLYPH. */
14821 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14822 {
14823 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14824 emacs_abort ();
14825 x += g->pixel_width;
14826 }
14827 }
14828
14829 /* ROW could be part of a continued line, which, under bidi
14830 reordering, might have other rows whose start and end charpos
14831 occlude point. Only set w->cursor if we found a better
14832 approximation to the cursor position than we have from previously
14833 examined candidate rows belonging to the same continued line. */
14834 if (/* We already have a candidate row. */
14835 w->cursor.vpos >= 0
14836 /* That candidate is not the row we are processing. */
14837 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14838 /* Make sure cursor.vpos specifies a row whose start and end
14839 charpos occlude point, and it is valid candidate for being a
14840 cursor-row. This is because some callers of this function
14841 leave cursor.vpos at the row where the cursor was displayed
14842 during the last redisplay cycle. */
14843 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14844 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14845 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14846 {
14847 struct glyph *g1
14848 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14849
14850 /* Don't consider glyphs that are outside TEXT_AREA. */
14851 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14852 return false;
14853 /* Keep the candidate whose buffer position is the closest to
14854 point or has the `cursor' property. */
14855 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14856 w->cursor.hpos >= 0
14857 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14858 && ((BUFFERP (g1->object)
14859 && (g1->charpos == pt_old /* An exact match always wins. */
14860 || (BUFFERP (glyph->object)
14861 && eabs (g1->charpos - pt_old)
14862 < eabs (glyph->charpos - pt_old))))
14863 /* Previous candidate is a glyph from a string that has
14864 a non-nil `cursor' property. */
14865 || (STRINGP (g1->object)
14866 && (!NILP (Fget_char_property (make_number (g1->charpos),
14867 Qcursor, g1->object))
14868 /* Previous candidate is from the same display
14869 string as this one, and the display string
14870 came from a text property. */
14871 || (EQ (g1->object, glyph->object)
14872 && string_from_text_prop)
14873 /* this candidate is from newline and its
14874 position is not an exact match */
14875 || (NILP (glyph->object)
14876 && glyph->charpos != pt_old)))))
14877 return false;
14878 /* If this candidate gives an exact match, use that. */
14879 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14880 /* If this candidate is a glyph created for the
14881 terminating newline of a line, and point is on that
14882 newline, it wins because it's an exact match. */
14883 || (!row->continued_p
14884 && NILP (glyph->object)
14885 && glyph->charpos == 0
14886 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14887 /* Otherwise, keep the candidate that comes from a row
14888 spanning less buffer positions. This may win when one or
14889 both candidate positions are on glyphs that came from
14890 display strings, for which we cannot compare buffer
14891 positions. */
14892 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14893 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14894 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14895 return false;
14896 }
14897 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14898 w->cursor.x = x;
14899 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14900 w->cursor.y = row->y + dy;
14901
14902 if (w == XWINDOW (selected_window))
14903 {
14904 if (!row->continued_p
14905 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14906 && row->x == 0)
14907 {
14908 this_line_buffer = XBUFFER (w->contents);
14909
14910 CHARPOS (this_line_start_pos)
14911 = MATRIX_ROW_START_CHARPOS (row) + delta;
14912 BYTEPOS (this_line_start_pos)
14913 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14914
14915 CHARPOS (this_line_end_pos)
14916 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14917 BYTEPOS (this_line_end_pos)
14918 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14919
14920 this_line_y = w->cursor.y;
14921 this_line_pixel_height = row->height;
14922 this_line_vpos = w->cursor.vpos;
14923 this_line_start_x = row->x;
14924 }
14925 else
14926 CHARPOS (this_line_start_pos) = 0;
14927 }
14928
14929 return true;
14930 }
14931
14932
14933 /* Run window scroll functions, if any, for WINDOW with new window
14934 start STARTP. Sets the window start of WINDOW to that position.
14935
14936 We assume that the window's buffer is really current. */
14937
14938 static struct text_pos
14939 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14940 {
14941 struct window *w = XWINDOW (window);
14942 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14943
14944 eassert (current_buffer == XBUFFER (w->contents));
14945
14946 if (!NILP (Vwindow_scroll_functions))
14947 {
14948 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14949 make_number (CHARPOS (startp)));
14950 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14951 /* In case the hook functions switch buffers. */
14952 set_buffer_internal (XBUFFER (w->contents));
14953 }
14954
14955 return startp;
14956 }
14957
14958
14959 /* Make sure the line containing the cursor is fully visible.
14960 A value of true means there is nothing to be done.
14961 (Either the line is fully visible, or it cannot be made so,
14962 or we cannot tell.)
14963
14964 If FORCE_P, return false even if partial visible cursor row
14965 is higher than window.
14966
14967 If CURRENT_MATRIX_P, use the information from the
14968 window's current glyph matrix; otherwise use the desired glyph
14969 matrix.
14970
14971 A value of false means the caller should do scrolling
14972 as if point had gone off the screen. */
14973
14974 static bool
14975 cursor_row_fully_visible_p (struct window *w, bool force_p,
14976 bool current_matrix_p)
14977 {
14978 struct glyph_matrix *matrix;
14979 struct glyph_row *row;
14980 int window_height;
14981
14982 if (!make_cursor_line_fully_visible_p)
14983 return true;
14984
14985 /* It's not always possible to find the cursor, e.g, when a window
14986 is full of overlay strings. Don't do anything in that case. */
14987 if (w->cursor.vpos < 0)
14988 return true;
14989
14990 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14991 row = MATRIX_ROW (matrix, w->cursor.vpos);
14992
14993 /* If the cursor row is not partially visible, there's nothing to do. */
14994 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14995 return true;
14996
14997 /* If the row the cursor is in is taller than the window's height,
14998 it's not clear what to do, so do nothing. */
14999 window_height = window_box_height (w);
15000 if (row->height >= window_height)
15001 {
15002 if (!force_p || MINI_WINDOW_P (w)
15003 || w->vscroll || w->cursor.vpos == 0)
15004 return true;
15005 }
15006 return false;
15007 }
15008
15009
15010 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15011 means only WINDOW is redisplayed in redisplay_internal.
15012 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15013 in redisplay_window to bring a partially visible line into view in
15014 the case that only the cursor has moved.
15015
15016 LAST_LINE_MISFIT should be true if we're scrolling because the
15017 last screen line's vertical height extends past the end of the screen.
15018
15019 Value is
15020
15021 1 if scrolling succeeded
15022
15023 0 if scrolling didn't find point.
15024
15025 -1 if new fonts have been loaded so that we must interrupt
15026 redisplay, adjust glyph matrices, and try again. */
15027
15028 enum
15029 {
15030 SCROLLING_SUCCESS,
15031 SCROLLING_FAILED,
15032 SCROLLING_NEED_LARGER_MATRICES
15033 };
15034
15035 /* If scroll-conservatively is more than this, never recenter.
15036
15037 If you change this, don't forget to update the doc string of
15038 `scroll-conservatively' and the Emacs manual. */
15039 #define SCROLL_LIMIT 100
15040
15041 static int
15042 try_scrolling (Lisp_Object window, bool just_this_one_p,
15043 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15044 bool temp_scroll_step, bool last_line_misfit)
15045 {
15046 struct window *w = XWINDOW (window);
15047 struct frame *f = XFRAME (w->frame);
15048 struct text_pos pos, startp;
15049 struct it it;
15050 int this_scroll_margin, scroll_max, rc, height;
15051 int dy = 0, amount_to_scroll = 0;
15052 bool scroll_down_p = false;
15053 int extra_scroll_margin_lines = last_line_misfit;
15054 Lisp_Object aggressive;
15055 /* We will never try scrolling more than this number of lines. */
15056 int scroll_limit = SCROLL_LIMIT;
15057 int frame_line_height = default_line_pixel_height (w);
15058 int window_total_lines
15059 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15060
15061 #ifdef GLYPH_DEBUG
15062 debug_method_add (w, "try_scrolling");
15063 #endif
15064
15065 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15066
15067 /* Compute scroll margin height in pixels. We scroll when point is
15068 within this distance from the top or bottom of the window. */
15069 if (scroll_margin > 0)
15070 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15071 * frame_line_height;
15072 else
15073 this_scroll_margin = 0;
15074
15075 /* Force arg_scroll_conservatively to have a reasonable value, to
15076 avoid scrolling too far away with slow move_it_* functions. Note
15077 that the user can supply scroll-conservatively equal to
15078 `most-positive-fixnum', which can be larger than INT_MAX. */
15079 if (arg_scroll_conservatively > scroll_limit)
15080 {
15081 arg_scroll_conservatively = scroll_limit + 1;
15082 scroll_max = scroll_limit * frame_line_height;
15083 }
15084 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15085 /* Compute how much we should try to scroll maximally to bring
15086 point into view. */
15087 scroll_max = (max (scroll_step,
15088 max (arg_scroll_conservatively, temp_scroll_step))
15089 * frame_line_height);
15090 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15091 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15092 /* We're trying to scroll because of aggressive scrolling but no
15093 scroll_step is set. Choose an arbitrary one. */
15094 scroll_max = 10 * frame_line_height;
15095 else
15096 scroll_max = 0;
15097
15098 too_near_end:
15099
15100 /* Decide whether to scroll down. */
15101 if (PT > CHARPOS (startp))
15102 {
15103 int scroll_margin_y;
15104
15105 /* Compute the pixel ypos of the scroll margin, then move IT to
15106 either that ypos or PT, whichever comes first. */
15107 start_display (&it, w, startp);
15108 scroll_margin_y = it.last_visible_y - this_scroll_margin
15109 - frame_line_height * extra_scroll_margin_lines;
15110 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15111 (MOVE_TO_POS | MOVE_TO_Y));
15112
15113 if (PT > CHARPOS (it.current.pos))
15114 {
15115 int y0 = line_bottom_y (&it);
15116 /* Compute how many pixels below window bottom to stop searching
15117 for PT. This avoids costly search for PT that is far away if
15118 the user limited scrolling by a small number of lines, but
15119 always finds PT if scroll_conservatively is set to a large
15120 number, such as most-positive-fixnum. */
15121 int slack = max (scroll_max, 10 * frame_line_height);
15122 int y_to_move = it.last_visible_y + slack;
15123
15124 /* Compute the distance from the scroll margin to PT or to
15125 the scroll limit, whichever comes first. This should
15126 include the height of the cursor line, to make that line
15127 fully visible. */
15128 move_it_to (&it, PT, -1, y_to_move,
15129 -1, MOVE_TO_POS | MOVE_TO_Y);
15130 dy = line_bottom_y (&it) - y0;
15131
15132 if (dy > scroll_max)
15133 return SCROLLING_FAILED;
15134
15135 if (dy > 0)
15136 scroll_down_p = true;
15137 }
15138 }
15139
15140 if (scroll_down_p)
15141 {
15142 /* Point is in or below the bottom scroll margin, so move the
15143 window start down. If scrolling conservatively, move it just
15144 enough down to make point visible. If scroll_step is set,
15145 move it down by scroll_step. */
15146 if (arg_scroll_conservatively)
15147 amount_to_scroll
15148 = min (max (dy, frame_line_height),
15149 frame_line_height * arg_scroll_conservatively);
15150 else if (scroll_step || temp_scroll_step)
15151 amount_to_scroll = scroll_max;
15152 else
15153 {
15154 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15155 height = WINDOW_BOX_TEXT_HEIGHT (w);
15156 if (NUMBERP (aggressive))
15157 {
15158 double float_amount = XFLOATINT (aggressive) * height;
15159 int aggressive_scroll = float_amount;
15160 if (aggressive_scroll == 0 && float_amount > 0)
15161 aggressive_scroll = 1;
15162 /* Don't let point enter the scroll margin near top of
15163 the window. This could happen if the value of
15164 scroll_up_aggressively is too large and there are
15165 non-zero margins, because scroll_up_aggressively
15166 means put point that fraction of window height
15167 _from_the_bottom_margin_. */
15168 if (aggressive_scroll + 2 * this_scroll_margin > height)
15169 aggressive_scroll = height - 2 * this_scroll_margin;
15170 amount_to_scroll = dy + aggressive_scroll;
15171 }
15172 }
15173
15174 if (amount_to_scroll <= 0)
15175 return SCROLLING_FAILED;
15176
15177 start_display (&it, w, startp);
15178 if (arg_scroll_conservatively <= scroll_limit)
15179 move_it_vertically (&it, amount_to_scroll);
15180 else
15181 {
15182 /* Extra precision for users who set scroll-conservatively
15183 to a large number: make sure the amount we scroll
15184 the window start is never less than amount_to_scroll,
15185 which was computed as distance from window bottom to
15186 point. This matters when lines at window top and lines
15187 below window bottom have different height. */
15188 struct it it1;
15189 void *it1data = NULL;
15190 /* We use a temporary it1 because line_bottom_y can modify
15191 its argument, if it moves one line down; see there. */
15192 int start_y;
15193
15194 SAVE_IT (it1, it, it1data);
15195 start_y = line_bottom_y (&it1);
15196 do {
15197 RESTORE_IT (&it, &it, it1data);
15198 move_it_by_lines (&it, 1);
15199 SAVE_IT (it1, it, it1data);
15200 } while (IT_CHARPOS (it) < ZV
15201 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15202 bidi_unshelve_cache (it1data, true);
15203 }
15204
15205 /* If STARTP is unchanged, move it down another screen line. */
15206 if (IT_CHARPOS (it) == CHARPOS (startp))
15207 move_it_by_lines (&it, 1);
15208 startp = it.current.pos;
15209 }
15210 else
15211 {
15212 struct text_pos scroll_margin_pos = startp;
15213 int y_offset = 0;
15214
15215 /* See if point is inside the scroll margin at the top of the
15216 window. */
15217 if (this_scroll_margin)
15218 {
15219 int y_start;
15220
15221 start_display (&it, w, startp);
15222 y_start = it.current_y;
15223 move_it_vertically (&it, this_scroll_margin);
15224 scroll_margin_pos = it.current.pos;
15225 /* If we didn't move enough before hitting ZV, request
15226 additional amount of scroll, to move point out of the
15227 scroll margin. */
15228 if (IT_CHARPOS (it) == ZV
15229 && it.current_y - y_start < this_scroll_margin)
15230 y_offset = this_scroll_margin - (it.current_y - y_start);
15231 }
15232
15233 if (PT < CHARPOS (scroll_margin_pos))
15234 {
15235 /* Point is in the scroll margin at the top of the window or
15236 above what is displayed in the window. */
15237 int y0, y_to_move;
15238
15239 /* Compute the vertical distance from PT to the scroll
15240 margin position. Move as far as scroll_max allows, or
15241 one screenful, or 10 screen lines, whichever is largest.
15242 Give up if distance is greater than scroll_max or if we
15243 didn't reach the scroll margin position. */
15244 SET_TEXT_POS (pos, PT, PT_BYTE);
15245 start_display (&it, w, pos);
15246 y0 = it.current_y;
15247 y_to_move = max (it.last_visible_y,
15248 max (scroll_max, 10 * frame_line_height));
15249 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15250 y_to_move, -1,
15251 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15252 dy = it.current_y - y0;
15253 if (dy > scroll_max
15254 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15255 return SCROLLING_FAILED;
15256
15257 /* Additional scroll for when ZV was too close to point. */
15258 dy += y_offset;
15259
15260 /* Compute new window start. */
15261 start_display (&it, w, startp);
15262
15263 if (arg_scroll_conservatively)
15264 amount_to_scroll = max (dy, frame_line_height
15265 * max (scroll_step, temp_scroll_step));
15266 else if (scroll_step || temp_scroll_step)
15267 amount_to_scroll = scroll_max;
15268 else
15269 {
15270 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15271 height = WINDOW_BOX_TEXT_HEIGHT (w);
15272 if (NUMBERP (aggressive))
15273 {
15274 double float_amount = XFLOATINT (aggressive) * height;
15275 int aggressive_scroll = float_amount;
15276 if (aggressive_scroll == 0 && float_amount > 0)
15277 aggressive_scroll = 1;
15278 /* Don't let point enter the scroll margin near
15279 bottom of the window, if the value of
15280 scroll_down_aggressively happens to be too
15281 large. */
15282 if (aggressive_scroll + 2 * this_scroll_margin > height)
15283 aggressive_scroll = height - 2 * this_scroll_margin;
15284 amount_to_scroll = dy + aggressive_scroll;
15285 }
15286 }
15287
15288 if (amount_to_scroll <= 0)
15289 return SCROLLING_FAILED;
15290
15291 move_it_vertically_backward (&it, amount_to_scroll);
15292 startp = it.current.pos;
15293 }
15294 }
15295
15296 /* Run window scroll functions. */
15297 startp = run_window_scroll_functions (window, startp);
15298
15299 /* Display the window. Give up if new fonts are loaded, or if point
15300 doesn't appear. */
15301 if (!try_window (window, startp, 0))
15302 rc = SCROLLING_NEED_LARGER_MATRICES;
15303 else if (w->cursor.vpos < 0)
15304 {
15305 clear_glyph_matrix (w->desired_matrix);
15306 rc = SCROLLING_FAILED;
15307 }
15308 else
15309 {
15310 /* Maybe forget recorded base line for line number display. */
15311 if (!just_this_one_p
15312 || current_buffer->clip_changed
15313 || BEG_UNCHANGED < CHARPOS (startp))
15314 w->base_line_number = 0;
15315
15316 /* If cursor ends up on a partially visible line,
15317 treat that as being off the bottom of the screen. */
15318 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15319 false)
15320 /* It's possible that the cursor is on the first line of the
15321 buffer, which is partially obscured due to a vscroll
15322 (Bug#7537). In that case, avoid looping forever. */
15323 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15324 {
15325 clear_glyph_matrix (w->desired_matrix);
15326 ++extra_scroll_margin_lines;
15327 goto too_near_end;
15328 }
15329 rc = SCROLLING_SUCCESS;
15330 }
15331
15332 return rc;
15333 }
15334
15335
15336 /* Compute a suitable window start for window W if display of W starts
15337 on a continuation line. Value is true if a new window start
15338 was computed.
15339
15340 The new window start will be computed, based on W's width, starting
15341 from the start of the continued line. It is the start of the
15342 screen line with the minimum distance from the old start W->start. */
15343
15344 static bool
15345 compute_window_start_on_continuation_line (struct window *w)
15346 {
15347 struct text_pos pos, start_pos;
15348 bool window_start_changed_p = false;
15349
15350 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15351
15352 /* If window start is on a continuation line... Window start may be
15353 < BEGV in case there's invisible text at the start of the
15354 buffer (M-x rmail, for example). */
15355 if (CHARPOS (start_pos) > BEGV
15356 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15357 {
15358 struct it it;
15359 struct glyph_row *row;
15360
15361 /* Handle the case that the window start is out of range. */
15362 if (CHARPOS (start_pos) < BEGV)
15363 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15364 else if (CHARPOS (start_pos) > ZV)
15365 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15366
15367 /* Find the start of the continued line. This should be fast
15368 because find_newline is fast (newline cache). */
15369 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15370 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15371 row, DEFAULT_FACE_ID);
15372 reseat_at_previous_visible_line_start (&it);
15373
15374 /* If the line start is "too far" away from the window start,
15375 say it takes too much time to compute a new window start. */
15376 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15377 /* PXW: Do we need upper bounds here? */
15378 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15379 {
15380 int min_distance, distance;
15381
15382 /* Move forward by display lines to find the new window
15383 start. If window width was enlarged, the new start can
15384 be expected to be > the old start. If window width was
15385 decreased, the new window start will be < the old start.
15386 So, we're looking for the display line start with the
15387 minimum distance from the old window start. */
15388 pos = it.current.pos;
15389 min_distance = INFINITY;
15390 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15391 distance < min_distance)
15392 {
15393 min_distance = distance;
15394 pos = it.current.pos;
15395 if (it.line_wrap == WORD_WRAP)
15396 {
15397 /* Under WORD_WRAP, move_it_by_lines is likely to
15398 overshoot and stop not at the first, but the
15399 second character from the left margin. So in
15400 that case, we need a more tight control on the X
15401 coordinate of the iterator than move_it_by_lines
15402 promises in its contract. The method is to first
15403 go to the last (rightmost) visible character of a
15404 line, then move to the leftmost character on the
15405 next line in a separate call. */
15406 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15407 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15408 move_it_to (&it, ZV, 0,
15409 it.current_y + it.max_ascent + it.max_descent, -1,
15410 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15411 }
15412 else
15413 move_it_by_lines (&it, 1);
15414 }
15415
15416 /* Set the window start there. */
15417 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15418 window_start_changed_p = true;
15419 }
15420 }
15421
15422 return window_start_changed_p;
15423 }
15424
15425
15426 /* Try cursor movement in case text has not changed in window WINDOW,
15427 with window start STARTP. Value is
15428
15429 CURSOR_MOVEMENT_SUCCESS if successful
15430
15431 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15432
15433 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15434 display. *SCROLL_STEP is set to true, under certain circumstances, if
15435 we want to scroll as if scroll-step were set to 1. See the code.
15436
15437 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15438 which case we have to abort this redisplay, and adjust matrices
15439 first. */
15440
15441 enum
15442 {
15443 CURSOR_MOVEMENT_SUCCESS,
15444 CURSOR_MOVEMENT_CANNOT_BE_USED,
15445 CURSOR_MOVEMENT_MUST_SCROLL,
15446 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15447 };
15448
15449 static int
15450 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15451 bool *scroll_step)
15452 {
15453 struct window *w = XWINDOW (window);
15454 struct frame *f = XFRAME (w->frame);
15455 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15456
15457 #ifdef GLYPH_DEBUG
15458 if (inhibit_try_cursor_movement)
15459 return rc;
15460 #endif
15461
15462 /* Previously, there was a check for Lisp integer in the
15463 if-statement below. Now, this field is converted to
15464 ptrdiff_t, thus zero means invalid position in a buffer. */
15465 eassert (w->last_point > 0);
15466 /* Likewise there was a check whether window_end_vpos is nil or larger
15467 than the window. Now window_end_vpos is int and so never nil, but
15468 let's leave eassert to check whether it fits in the window. */
15469 eassert (!w->window_end_valid
15470 || w->window_end_vpos < w->current_matrix->nrows);
15471
15472 /* Handle case where text has not changed, only point, and it has
15473 not moved off the frame. */
15474 if (/* Point may be in this window. */
15475 PT >= CHARPOS (startp)
15476 /* Selective display hasn't changed. */
15477 && !current_buffer->clip_changed
15478 /* Function force-mode-line-update is used to force a thorough
15479 redisplay. It sets either windows_or_buffers_changed or
15480 update_mode_lines. So don't take a shortcut here for these
15481 cases. */
15482 && !update_mode_lines
15483 && !windows_or_buffers_changed
15484 && !f->cursor_type_changed
15485 && NILP (Vshow_trailing_whitespace)
15486 /* This code is not used for mini-buffer for the sake of the case
15487 of redisplaying to replace an echo area message; since in
15488 that case the mini-buffer contents per se are usually
15489 unchanged. This code is of no real use in the mini-buffer
15490 since the handling of this_line_start_pos, etc., in redisplay
15491 handles the same cases. */
15492 && !EQ (window, minibuf_window)
15493 && (FRAME_WINDOW_P (f)
15494 || !overlay_arrow_in_current_buffer_p ()))
15495 {
15496 int this_scroll_margin, top_scroll_margin;
15497 struct glyph_row *row = NULL;
15498 int frame_line_height = default_line_pixel_height (w);
15499 int window_total_lines
15500 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15501
15502 #ifdef GLYPH_DEBUG
15503 debug_method_add (w, "cursor movement");
15504 #endif
15505
15506 /* Scroll if point within this distance from the top or bottom
15507 of the window. This is a pixel value. */
15508 if (scroll_margin > 0)
15509 {
15510 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15511 this_scroll_margin *= frame_line_height;
15512 }
15513 else
15514 this_scroll_margin = 0;
15515
15516 top_scroll_margin = this_scroll_margin;
15517 if (WINDOW_WANTS_HEADER_LINE_P (w))
15518 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15519
15520 /* Start with the row the cursor was displayed during the last
15521 not paused redisplay. Give up if that row is not valid. */
15522 if (w->last_cursor_vpos < 0
15523 || w->last_cursor_vpos >= w->current_matrix->nrows)
15524 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15525 else
15526 {
15527 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15528 if (row->mode_line_p)
15529 ++row;
15530 if (!row->enabled_p)
15531 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15532 }
15533
15534 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15535 {
15536 bool scroll_p = false, must_scroll = false;
15537 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15538
15539 if (PT > w->last_point)
15540 {
15541 /* Point has moved forward. */
15542 while (MATRIX_ROW_END_CHARPOS (row) < PT
15543 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15544 {
15545 eassert (row->enabled_p);
15546 ++row;
15547 }
15548
15549 /* If the end position of a row equals the start
15550 position of the next row, and PT is at that position,
15551 we would rather display cursor in the next line. */
15552 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15553 && MATRIX_ROW_END_CHARPOS (row) == PT
15554 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15555 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15556 && !cursor_row_p (row))
15557 ++row;
15558
15559 /* If within the scroll margin, scroll. Note that
15560 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15561 the next line would be drawn, and that
15562 this_scroll_margin can be zero. */
15563 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15564 || PT > MATRIX_ROW_END_CHARPOS (row)
15565 /* Line is completely visible last line in window
15566 and PT is to be set in the next line. */
15567 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15568 && PT == MATRIX_ROW_END_CHARPOS (row)
15569 && !row->ends_at_zv_p
15570 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15571 scroll_p = true;
15572 }
15573 else if (PT < w->last_point)
15574 {
15575 /* Cursor has to be moved backward. Note that PT >=
15576 CHARPOS (startp) because of the outer if-statement. */
15577 while (!row->mode_line_p
15578 && (MATRIX_ROW_START_CHARPOS (row) > PT
15579 || (MATRIX_ROW_START_CHARPOS (row) == PT
15580 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15581 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15582 row > w->current_matrix->rows
15583 && (row-1)->ends_in_newline_from_string_p))))
15584 && (row->y > top_scroll_margin
15585 || CHARPOS (startp) == BEGV))
15586 {
15587 eassert (row->enabled_p);
15588 --row;
15589 }
15590
15591 /* Consider the following case: Window starts at BEGV,
15592 there is invisible, intangible text at BEGV, so that
15593 display starts at some point START > BEGV. It can
15594 happen that we are called with PT somewhere between
15595 BEGV and START. Try to handle that case. */
15596 if (row < w->current_matrix->rows
15597 || row->mode_line_p)
15598 {
15599 row = w->current_matrix->rows;
15600 if (row->mode_line_p)
15601 ++row;
15602 }
15603
15604 /* Due to newlines in overlay strings, we may have to
15605 skip forward over overlay strings. */
15606 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15607 && MATRIX_ROW_END_CHARPOS (row) == PT
15608 && !cursor_row_p (row))
15609 ++row;
15610
15611 /* If within the scroll margin, scroll. */
15612 if (row->y < top_scroll_margin
15613 && CHARPOS (startp) != BEGV)
15614 scroll_p = true;
15615 }
15616 else
15617 {
15618 /* Cursor did not move. So don't scroll even if cursor line
15619 is partially visible, as it was so before. */
15620 rc = CURSOR_MOVEMENT_SUCCESS;
15621 }
15622
15623 if (PT < MATRIX_ROW_START_CHARPOS (row)
15624 || PT > MATRIX_ROW_END_CHARPOS (row))
15625 {
15626 /* if PT is not in the glyph row, give up. */
15627 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15628 must_scroll = true;
15629 }
15630 else if (rc != CURSOR_MOVEMENT_SUCCESS
15631 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15632 {
15633 struct glyph_row *row1;
15634
15635 /* If rows are bidi-reordered and point moved, back up
15636 until we find a row that does not belong to a
15637 continuation line. This is because we must consider
15638 all rows of a continued line as candidates for the
15639 new cursor positioning, since row start and end
15640 positions change non-linearly with vertical position
15641 in such rows. */
15642 /* FIXME: Revisit this when glyph ``spilling'' in
15643 continuation lines' rows is implemented for
15644 bidi-reordered rows. */
15645 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15646 MATRIX_ROW_CONTINUATION_LINE_P (row);
15647 --row)
15648 {
15649 /* If we hit the beginning of the displayed portion
15650 without finding the first row of a continued
15651 line, give up. */
15652 if (row <= row1)
15653 {
15654 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15655 break;
15656 }
15657 eassert (row->enabled_p);
15658 }
15659 }
15660 if (must_scroll)
15661 ;
15662 else if (rc != CURSOR_MOVEMENT_SUCCESS
15663 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15664 /* Make sure this isn't a header line by any chance, since
15665 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15666 && !row->mode_line_p
15667 && make_cursor_line_fully_visible_p)
15668 {
15669 if (PT == MATRIX_ROW_END_CHARPOS (row)
15670 && !row->ends_at_zv_p
15671 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15672 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15673 else if (row->height > window_box_height (w))
15674 {
15675 /* If we end up in a partially visible line, let's
15676 make it fully visible, except when it's taller
15677 than the window, in which case we can't do much
15678 about it. */
15679 *scroll_step = true;
15680 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15681 }
15682 else
15683 {
15684 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15685 if (!cursor_row_fully_visible_p (w, false, true))
15686 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15687 else
15688 rc = CURSOR_MOVEMENT_SUCCESS;
15689 }
15690 }
15691 else if (scroll_p)
15692 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15693 else if (rc != CURSOR_MOVEMENT_SUCCESS
15694 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15695 {
15696 /* With bidi-reordered rows, there could be more than
15697 one candidate row whose start and end positions
15698 occlude point. We need to let set_cursor_from_row
15699 find the best candidate. */
15700 /* FIXME: Revisit this when glyph ``spilling'' in
15701 continuation lines' rows is implemented for
15702 bidi-reordered rows. */
15703 bool rv = false;
15704
15705 do
15706 {
15707 bool at_zv_p = false, exact_match_p = false;
15708
15709 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15710 && PT <= MATRIX_ROW_END_CHARPOS (row)
15711 && cursor_row_p (row))
15712 rv |= set_cursor_from_row (w, row, w->current_matrix,
15713 0, 0, 0, 0);
15714 /* As soon as we've found the exact match for point,
15715 or the first suitable row whose ends_at_zv_p flag
15716 is set, we are done. */
15717 if (rv)
15718 {
15719 at_zv_p = MATRIX_ROW (w->current_matrix,
15720 w->cursor.vpos)->ends_at_zv_p;
15721 if (!at_zv_p
15722 && w->cursor.hpos >= 0
15723 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15724 w->cursor.vpos))
15725 {
15726 struct glyph_row *candidate =
15727 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15728 struct glyph *g =
15729 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15730 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15731
15732 exact_match_p =
15733 (BUFFERP (g->object) && g->charpos == PT)
15734 || (NILP (g->object)
15735 && (g->charpos == PT
15736 || (g->charpos == 0 && endpos - 1 == PT)));
15737 }
15738 if (at_zv_p || exact_match_p)
15739 {
15740 rc = CURSOR_MOVEMENT_SUCCESS;
15741 break;
15742 }
15743 }
15744 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15745 break;
15746 ++row;
15747 }
15748 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15749 || row->continued_p)
15750 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15751 || (MATRIX_ROW_START_CHARPOS (row) == PT
15752 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15753 /* If we didn't find any candidate rows, or exited the
15754 loop before all the candidates were examined, signal
15755 to the caller that this method failed. */
15756 if (rc != CURSOR_MOVEMENT_SUCCESS
15757 && !(rv
15758 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15759 && !row->continued_p))
15760 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15761 else if (rv)
15762 rc = CURSOR_MOVEMENT_SUCCESS;
15763 }
15764 else
15765 {
15766 do
15767 {
15768 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15769 {
15770 rc = CURSOR_MOVEMENT_SUCCESS;
15771 break;
15772 }
15773 ++row;
15774 }
15775 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15776 && MATRIX_ROW_START_CHARPOS (row) == PT
15777 && cursor_row_p (row));
15778 }
15779 }
15780 }
15781
15782 return rc;
15783 }
15784
15785
15786 void
15787 set_vertical_scroll_bar (struct window *w)
15788 {
15789 ptrdiff_t start, end, whole;
15790
15791 /* Calculate the start and end positions for the current window.
15792 At some point, it would be nice to choose between scrollbars
15793 which reflect the whole buffer size, with special markers
15794 indicating narrowing, and scrollbars which reflect only the
15795 visible region.
15796
15797 Note that mini-buffers sometimes aren't displaying any text. */
15798 if (!MINI_WINDOW_P (w)
15799 || (w == XWINDOW (minibuf_window)
15800 && NILP (echo_area_buffer[0])))
15801 {
15802 struct buffer *buf = XBUFFER (w->contents);
15803 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15804 start = marker_position (w->start) - BUF_BEGV (buf);
15805 /* I don't think this is guaranteed to be right. For the
15806 moment, we'll pretend it is. */
15807 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15808
15809 if (end < start)
15810 end = start;
15811 if (whole < (end - start))
15812 whole = end - start;
15813 }
15814 else
15815 start = end = whole = 0;
15816
15817 /* Indicate what this scroll bar ought to be displaying now. */
15818 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15819 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15820 (w, end - start, whole, start);
15821 }
15822
15823
15824 void
15825 set_horizontal_scroll_bar (struct window *w)
15826 {
15827 int start, end, whole, portion;
15828
15829 if (!MINI_WINDOW_P (w)
15830 || (w == XWINDOW (minibuf_window)
15831 && NILP (echo_area_buffer[0])))
15832 {
15833 struct buffer *b = XBUFFER (w->contents);
15834 struct buffer *old_buffer = NULL;
15835 struct it it;
15836 struct text_pos startp;
15837
15838 if (b != current_buffer)
15839 {
15840 old_buffer = current_buffer;
15841 set_buffer_internal (b);
15842 }
15843
15844 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15845 start_display (&it, w, startp);
15846 it.last_visible_x = INT_MAX;
15847 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15848 MOVE_TO_X | MOVE_TO_Y);
15849 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15850 window_box_height (w), -1,
15851 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15852
15853 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15854 end = start + window_box_width (w, TEXT_AREA);
15855 portion = end - start;
15856 /* After enlarging a horizontally scrolled window such that it
15857 gets at least as wide as the text it contains, make sure that
15858 the thumb doesn't fill the entire scroll bar so we can still
15859 drag it back to see the entire text. */
15860 whole = max (whole, end);
15861
15862 if (it.bidi_p)
15863 {
15864 Lisp_Object pdir;
15865
15866 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15867 if (EQ (pdir, Qright_to_left))
15868 {
15869 start = whole - end;
15870 end = start + portion;
15871 }
15872 }
15873
15874 if (old_buffer)
15875 set_buffer_internal (old_buffer);
15876 }
15877 else
15878 start = end = whole = portion = 0;
15879
15880 w->hscroll_whole = whole;
15881
15882 /* Indicate what this scroll bar ought to be displaying now. */
15883 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15884 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15885 (w, portion, whole, start);
15886 }
15887
15888
15889 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
15890 selected_window is redisplayed.
15891
15892 We can return without actually redisplaying the window if fonts has been
15893 changed on window's frame. In that case, redisplay_internal will retry.
15894
15895 As one of the important parts of redisplaying a window, we need to
15896 decide whether the previous window-start position (stored in the
15897 window's w->start marker position) is still valid, and if it isn't,
15898 recompute it. Some details about that:
15899
15900 . The previous window-start could be in a continuation line, in
15901 which case we need to recompute it when the window width
15902 changes. See compute_window_start_on_continuation_line and its
15903 call below.
15904
15905 . The text that changed since last redisplay could include the
15906 previous window-start position. In that case, we try to salvage
15907 what we can from the current glyph matrix by calling
15908 try_scrolling, which see.
15909
15910 . Some Emacs command could force us to use a specific window-start
15911 position by setting the window's force_start flag, or gently
15912 propose doing that by setting the window's optional_new_start
15913 flag. In these cases, we try using the specified start point if
15914 that succeeds (i.e. the window desired matrix is successfully
15915 recomputed, and point location is within the window). In case
15916 of optional_new_start, we first check if the specified start
15917 position is feasible, i.e. if it will allow point to be
15918 displayed in the window. If using the specified start point
15919 fails, e.g., if new fonts are needed to be loaded, we abort the
15920 redisplay cycle and leave it up to the next cycle to figure out
15921 things.
15922
15923 . Note that the window's force_start flag is sometimes set by
15924 redisplay itself, when it decides that the previous window start
15925 point is fine and should be kept. Search for "goto force_start"
15926 below to see the details. Like the values of window-start
15927 specified outside of redisplay, these internally-deduced values
15928 are tested for feasibility, and ignored if found to be
15929 unfeasible.
15930
15931 . Note that the function try_window, used to completely redisplay
15932 a window, accepts the window's start point as its argument.
15933 This is used several times in the redisplay code to control
15934 where the window start will be, according to user options such
15935 as scroll-conservatively, and also to ensure the screen line
15936 showing point will be fully (as opposed to partially) visible on
15937 display. */
15938
15939 static void
15940 redisplay_window (Lisp_Object window, bool just_this_one_p)
15941 {
15942 struct window *w = XWINDOW (window);
15943 struct frame *f = XFRAME (w->frame);
15944 struct buffer *buffer = XBUFFER (w->contents);
15945 struct buffer *old = current_buffer;
15946 struct text_pos lpoint, opoint, startp;
15947 bool update_mode_line;
15948 int tem;
15949 struct it it;
15950 /* Record it now because it's overwritten. */
15951 bool current_matrix_up_to_date_p = false;
15952 bool used_current_matrix_p = false;
15953 /* This is less strict than current_matrix_up_to_date_p.
15954 It indicates that the buffer contents and narrowing are unchanged. */
15955 bool buffer_unchanged_p = false;
15956 bool temp_scroll_step = false;
15957 ptrdiff_t count = SPECPDL_INDEX ();
15958 int rc;
15959 int centering_position = -1;
15960 bool last_line_misfit = false;
15961 ptrdiff_t beg_unchanged, end_unchanged;
15962 int frame_line_height;
15963
15964 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15965 opoint = lpoint;
15966
15967 #ifdef GLYPH_DEBUG
15968 *w->desired_matrix->method = 0;
15969 #endif
15970
15971 if (!just_this_one_p
15972 && REDISPLAY_SOME_P ()
15973 && !w->redisplay
15974 && !w->update_mode_line
15975 && !f->face_change
15976 && !f->redisplay
15977 && !buffer->text->redisplay
15978 && BUF_PT (buffer) == w->last_point)
15979 return;
15980
15981 /* Make sure that both W's markers are valid. */
15982 eassert (XMARKER (w->start)->buffer == buffer);
15983 eassert (XMARKER (w->pointm)->buffer == buffer);
15984
15985 /* We come here again if we need to run window-text-change-functions
15986 below. */
15987 restart:
15988 reconsider_clip_changes (w);
15989 frame_line_height = default_line_pixel_height (w);
15990
15991 /* Has the mode line to be updated? */
15992 update_mode_line = (w->update_mode_line
15993 || update_mode_lines
15994 || buffer->clip_changed
15995 || buffer->prevent_redisplay_optimizations_p);
15996
15997 if (!just_this_one_p)
15998 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15999 cleverly elsewhere. */
16000 w->must_be_updated_p = true;
16001
16002 if (MINI_WINDOW_P (w))
16003 {
16004 if (w == XWINDOW (echo_area_window)
16005 && !NILP (echo_area_buffer[0]))
16006 {
16007 if (update_mode_line)
16008 /* We may have to update a tty frame's menu bar or a
16009 tool-bar. Example `M-x C-h C-h C-g'. */
16010 goto finish_menu_bars;
16011 else
16012 /* We've already displayed the echo area glyphs in this window. */
16013 goto finish_scroll_bars;
16014 }
16015 else if ((w != XWINDOW (minibuf_window)
16016 || minibuf_level == 0)
16017 /* When buffer is nonempty, redisplay window normally. */
16018 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16019 /* Quail displays non-mini buffers in minibuffer window.
16020 In that case, redisplay the window normally. */
16021 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16022 {
16023 /* W is a mini-buffer window, but it's not active, so clear
16024 it. */
16025 int yb = window_text_bottom_y (w);
16026 struct glyph_row *row;
16027 int y;
16028
16029 for (y = 0, row = w->desired_matrix->rows;
16030 y < yb;
16031 y += row->height, ++row)
16032 blank_row (w, row, y);
16033 goto finish_scroll_bars;
16034 }
16035
16036 clear_glyph_matrix (w->desired_matrix);
16037 }
16038
16039 /* Otherwise set up data on this window; select its buffer and point
16040 value. */
16041 /* Really select the buffer, for the sake of buffer-local
16042 variables. */
16043 set_buffer_internal_1 (XBUFFER (w->contents));
16044
16045 current_matrix_up_to_date_p
16046 = (w->window_end_valid
16047 && !current_buffer->clip_changed
16048 && !current_buffer->prevent_redisplay_optimizations_p
16049 && !window_outdated (w));
16050
16051 /* Run the window-text-change-functions
16052 if it is possible that the text on the screen has changed
16053 (either due to modification of the text, or any other reason). */
16054 if (!current_matrix_up_to_date_p
16055 && !NILP (Vwindow_text_change_functions))
16056 {
16057 safe_run_hooks (Qwindow_text_change_functions);
16058 goto restart;
16059 }
16060
16061 beg_unchanged = BEG_UNCHANGED;
16062 end_unchanged = END_UNCHANGED;
16063
16064 SET_TEXT_POS (opoint, PT, PT_BYTE);
16065
16066 specbind (Qinhibit_point_motion_hooks, Qt);
16067
16068 buffer_unchanged_p
16069 = (w->window_end_valid
16070 && !current_buffer->clip_changed
16071 && !window_outdated (w));
16072
16073 /* When windows_or_buffers_changed is non-zero, we can't rely
16074 on the window end being valid, so set it to zero there. */
16075 if (windows_or_buffers_changed)
16076 {
16077 /* If window starts on a continuation line, maybe adjust the
16078 window start in case the window's width changed. */
16079 if (XMARKER (w->start)->buffer == current_buffer)
16080 compute_window_start_on_continuation_line (w);
16081
16082 w->window_end_valid = false;
16083 /* If so, we also can't rely on current matrix
16084 and should not fool try_cursor_movement below. */
16085 current_matrix_up_to_date_p = false;
16086 }
16087
16088 /* Some sanity checks. */
16089 CHECK_WINDOW_END (w);
16090 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16091 emacs_abort ();
16092 if (BYTEPOS (opoint) < CHARPOS (opoint))
16093 emacs_abort ();
16094
16095 if (mode_line_update_needed (w))
16096 update_mode_line = true;
16097
16098 /* Point refers normally to the selected window. For any other
16099 window, set up appropriate value. */
16100 if (!EQ (window, selected_window))
16101 {
16102 ptrdiff_t new_pt = marker_position (w->pointm);
16103 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16104
16105 if (new_pt < BEGV)
16106 {
16107 new_pt = BEGV;
16108 new_pt_byte = BEGV_BYTE;
16109 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16110 }
16111 else if (new_pt > (ZV - 1))
16112 {
16113 new_pt = ZV;
16114 new_pt_byte = ZV_BYTE;
16115 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16116 }
16117
16118 /* We don't use SET_PT so that the point-motion hooks don't run. */
16119 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16120 }
16121
16122 /* If any of the character widths specified in the display table
16123 have changed, invalidate the width run cache. It's true that
16124 this may be a bit late to catch such changes, but the rest of
16125 redisplay goes (non-fatally) haywire when the display table is
16126 changed, so why should we worry about doing any better? */
16127 if (current_buffer->width_run_cache
16128 || (current_buffer->base_buffer
16129 && current_buffer->base_buffer->width_run_cache))
16130 {
16131 struct Lisp_Char_Table *disptab = buffer_display_table ();
16132
16133 if (! disptab_matches_widthtab
16134 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16135 {
16136 struct buffer *buf = current_buffer;
16137
16138 if (buf->base_buffer)
16139 buf = buf->base_buffer;
16140 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16141 recompute_width_table (current_buffer, disptab);
16142 }
16143 }
16144
16145 /* If window-start is screwed up, choose a new one. */
16146 if (XMARKER (w->start)->buffer != current_buffer)
16147 goto recenter;
16148
16149 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16150
16151 /* If someone specified a new starting point but did not insist,
16152 check whether it can be used. */
16153 if ((w->optional_new_start || window_frozen_p (w))
16154 && CHARPOS (startp) >= BEGV
16155 && CHARPOS (startp) <= ZV)
16156 {
16157 ptrdiff_t it_charpos;
16158
16159 w->optional_new_start = false;
16160 start_display (&it, w, startp);
16161 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16162 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16163 /* Record IT's position now, since line_bottom_y might change
16164 that. */
16165 it_charpos = IT_CHARPOS (it);
16166 /* Make sure we set the force_start flag only if the cursor row
16167 will be fully visible. Otherwise, the code under force_start
16168 label below will try to move point back into view, which is
16169 not what the code which sets optional_new_start wants. */
16170 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16171 && !w->force_start)
16172 {
16173 if (it_charpos == PT)
16174 w->force_start = true;
16175 /* IT may overshoot PT if text at PT is invisible. */
16176 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16177 w->force_start = true;
16178 #ifdef GLYPH_DEBUG
16179 if (w->force_start)
16180 {
16181 if (window_frozen_p (w))
16182 debug_method_add (w, "set force_start from frozen window start");
16183 else
16184 debug_method_add (w, "set force_start from optional_new_start");
16185 }
16186 #endif
16187 }
16188 }
16189
16190 force_start:
16191
16192 /* Handle case where place to start displaying has been specified,
16193 unless the specified location is outside the accessible range. */
16194 if (w->force_start)
16195 {
16196 /* We set this later on if we have to adjust point. */
16197 int new_vpos = -1;
16198
16199 w->force_start = false;
16200 w->vscroll = 0;
16201 w->window_end_valid = false;
16202
16203 /* Forget any recorded base line for line number display. */
16204 if (!buffer_unchanged_p)
16205 w->base_line_number = 0;
16206
16207 /* Redisplay the mode line. Select the buffer properly for that.
16208 Also, run the hook window-scroll-functions
16209 because we have scrolled. */
16210 /* Note, we do this after clearing force_start because
16211 if there's an error, it is better to forget about force_start
16212 than to get into an infinite loop calling the hook functions
16213 and having them get more errors. */
16214 if (!update_mode_line
16215 || ! NILP (Vwindow_scroll_functions))
16216 {
16217 update_mode_line = true;
16218 w->update_mode_line = true;
16219 startp = run_window_scroll_functions (window, startp);
16220 }
16221
16222 if (CHARPOS (startp) < BEGV)
16223 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16224 else if (CHARPOS (startp) > ZV)
16225 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16226
16227 /* Redisplay, then check if cursor has been set during the
16228 redisplay. Give up if new fonts were loaded. */
16229 /* We used to issue a CHECK_MARGINS argument to try_window here,
16230 but this causes scrolling to fail when point begins inside
16231 the scroll margin (bug#148) -- cyd */
16232 if (!try_window (window, startp, 0))
16233 {
16234 w->force_start = true;
16235 clear_glyph_matrix (w->desired_matrix);
16236 goto need_larger_matrices;
16237 }
16238
16239 if (w->cursor.vpos < 0)
16240 {
16241 /* If point does not appear, try to move point so it does
16242 appear. The desired matrix has been built above, so we
16243 can use it here. */
16244 new_vpos = window_box_height (w) / 2;
16245 }
16246
16247 if (!cursor_row_fully_visible_p (w, false, false))
16248 {
16249 /* Point does appear, but on a line partly visible at end of window.
16250 Move it back to a fully-visible line. */
16251 new_vpos = window_box_height (w);
16252 /* But if window_box_height suggests a Y coordinate that is
16253 not less than we already have, that line will clearly not
16254 be fully visible, so give up and scroll the display.
16255 This can happen when the default face uses a font whose
16256 dimensions are different from the frame's default
16257 font. */
16258 if (new_vpos >= w->cursor.y)
16259 {
16260 w->cursor.vpos = -1;
16261 clear_glyph_matrix (w->desired_matrix);
16262 goto try_to_scroll;
16263 }
16264 }
16265 else if (w->cursor.vpos >= 0)
16266 {
16267 /* Some people insist on not letting point enter the scroll
16268 margin, even though this part handles windows that didn't
16269 scroll at all. */
16270 int window_total_lines
16271 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16272 int margin = min (scroll_margin, window_total_lines / 4);
16273 int pixel_margin = margin * frame_line_height;
16274 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16275
16276 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16277 below, which finds the row to move point to, advances by
16278 the Y coordinate of the _next_ row, see the definition of
16279 MATRIX_ROW_BOTTOM_Y. */
16280 if (w->cursor.vpos < margin + header_line)
16281 {
16282 w->cursor.vpos = -1;
16283 clear_glyph_matrix (w->desired_matrix);
16284 goto try_to_scroll;
16285 }
16286 else
16287 {
16288 int window_height = window_box_height (w);
16289
16290 if (header_line)
16291 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16292 if (w->cursor.y >= window_height - pixel_margin)
16293 {
16294 w->cursor.vpos = -1;
16295 clear_glyph_matrix (w->desired_matrix);
16296 goto try_to_scroll;
16297 }
16298 }
16299 }
16300
16301 /* If we need to move point for either of the above reasons,
16302 now actually do it. */
16303 if (new_vpos >= 0)
16304 {
16305 struct glyph_row *row;
16306
16307 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16308 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16309 ++row;
16310
16311 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16312 MATRIX_ROW_START_BYTEPOS (row));
16313
16314 if (w != XWINDOW (selected_window))
16315 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16316 else if (current_buffer == old)
16317 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16318
16319 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16320
16321 /* Re-run pre-redisplay-function so it can update the region
16322 according to the new position of point. */
16323 /* Other than the cursor, w's redisplay is done so we can set its
16324 redisplay to false. Also the buffer's redisplay can be set to
16325 false, since propagate_buffer_redisplay should have already
16326 propagated its info to `w' anyway. */
16327 w->redisplay = false;
16328 XBUFFER (w->contents)->text->redisplay = false;
16329 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16330
16331 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16332 {
16333 /* pre-redisplay-function made changes (e.g. move the region)
16334 that require another round of redisplay. */
16335 clear_glyph_matrix (w->desired_matrix);
16336 if (!try_window (window, startp, 0))
16337 goto need_larger_matrices;
16338 }
16339 }
16340 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16341 {
16342 clear_glyph_matrix (w->desired_matrix);
16343 goto try_to_scroll;
16344 }
16345
16346 #ifdef GLYPH_DEBUG
16347 debug_method_add (w, "forced window start");
16348 #endif
16349 goto done;
16350 }
16351
16352 /* Handle case where text has not changed, only point, and it has
16353 not moved off the frame, and we are not retrying after hscroll.
16354 (current_matrix_up_to_date_p is true when retrying.) */
16355 if (current_matrix_up_to_date_p
16356 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16357 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16358 {
16359 switch (rc)
16360 {
16361 case CURSOR_MOVEMENT_SUCCESS:
16362 used_current_matrix_p = true;
16363 goto done;
16364
16365 case CURSOR_MOVEMENT_MUST_SCROLL:
16366 goto try_to_scroll;
16367
16368 default:
16369 emacs_abort ();
16370 }
16371 }
16372 /* If current starting point was originally the beginning of a line
16373 but no longer is, find a new starting point. */
16374 else if (w->start_at_line_beg
16375 && !(CHARPOS (startp) <= BEGV
16376 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16377 {
16378 #ifdef GLYPH_DEBUG
16379 debug_method_add (w, "recenter 1");
16380 #endif
16381 goto recenter;
16382 }
16383
16384 /* Try scrolling with try_window_id. Value is > 0 if update has
16385 been done, it is -1 if we know that the same window start will
16386 not work. It is 0 if unsuccessful for some other reason. */
16387 else if ((tem = try_window_id (w)) != 0)
16388 {
16389 #ifdef GLYPH_DEBUG
16390 debug_method_add (w, "try_window_id %d", tem);
16391 #endif
16392
16393 if (f->fonts_changed)
16394 goto need_larger_matrices;
16395 if (tem > 0)
16396 goto done;
16397
16398 /* Otherwise try_window_id has returned -1 which means that we
16399 don't want the alternative below this comment to execute. */
16400 }
16401 else if (CHARPOS (startp) >= BEGV
16402 && CHARPOS (startp) <= ZV
16403 && PT >= CHARPOS (startp)
16404 && (CHARPOS (startp) < ZV
16405 /* Avoid starting at end of buffer. */
16406 || CHARPOS (startp) == BEGV
16407 || !window_outdated (w)))
16408 {
16409 int d1, d2, d5, d6;
16410 int rtop, rbot;
16411
16412 /* If first window line is a continuation line, and window start
16413 is inside the modified region, but the first change is before
16414 current window start, we must select a new window start.
16415
16416 However, if this is the result of a down-mouse event (e.g. by
16417 extending the mouse-drag-overlay), we don't want to select a
16418 new window start, since that would change the position under
16419 the mouse, resulting in an unwanted mouse-movement rather
16420 than a simple mouse-click. */
16421 if (!w->start_at_line_beg
16422 && NILP (do_mouse_tracking)
16423 && CHARPOS (startp) > BEGV
16424 && CHARPOS (startp) > BEG + beg_unchanged
16425 && CHARPOS (startp) <= Z - end_unchanged
16426 /* Even if w->start_at_line_beg is nil, a new window may
16427 start at a line_beg, since that's how set_buffer_window
16428 sets it. So, we need to check the return value of
16429 compute_window_start_on_continuation_line. (See also
16430 bug#197). */
16431 && XMARKER (w->start)->buffer == current_buffer
16432 && compute_window_start_on_continuation_line (w)
16433 /* It doesn't make sense to force the window start like we
16434 do at label force_start if it is already known that point
16435 will not be fully visible in the resulting window, because
16436 doing so will move point from its correct position
16437 instead of scrolling the window to bring point into view.
16438 See bug#9324. */
16439 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16440 /* A very tall row could need more than the window height,
16441 in which case we accept that it is partially visible. */
16442 && (rtop != 0) == (rbot != 0))
16443 {
16444 w->force_start = true;
16445 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16446 #ifdef GLYPH_DEBUG
16447 debug_method_add (w, "recomputed window start in continuation line");
16448 #endif
16449 goto force_start;
16450 }
16451
16452 #ifdef GLYPH_DEBUG
16453 debug_method_add (w, "same window start");
16454 #endif
16455
16456 /* Try to redisplay starting at same place as before.
16457 If point has not moved off frame, accept the results. */
16458 if (!current_matrix_up_to_date_p
16459 /* Don't use try_window_reusing_current_matrix in this case
16460 because a window scroll function can have changed the
16461 buffer. */
16462 || !NILP (Vwindow_scroll_functions)
16463 || MINI_WINDOW_P (w)
16464 || !(used_current_matrix_p
16465 = try_window_reusing_current_matrix (w)))
16466 {
16467 IF_DEBUG (debug_method_add (w, "1"));
16468 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16469 /* -1 means we need to scroll.
16470 0 means we need new matrices, but fonts_changed
16471 is set in that case, so we will detect it below. */
16472 goto try_to_scroll;
16473 }
16474
16475 if (f->fonts_changed)
16476 goto need_larger_matrices;
16477
16478 if (w->cursor.vpos >= 0)
16479 {
16480 if (!just_this_one_p
16481 || current_buffer->clip_changed
16482 || BEG_UNCHANGED < CHARPOS (startp))
16483 /* Forget any recorded base line for line number display. */
16484 w->base_line_number = 0;
16485
16486 if (!cursor_row_fully_visible_p (w, true, false))
16487 {
16488 clear_glyph_matrix (w->desired_matrix);
16489 last_line_misfit = true;
16490 }
16491 /* Drop through and scroll. */
16492 else
16493 goto done;
16494 }
16495 else
16496 clear_glyph_matrix (w->desired_matrix);
16497 }
16498
16499 try_to_scroll:
16500
16501 /* Redisplay the mode line. Select the buffer properly for that. */
16502 if (!update_mode_line)
16503 {
16504 update_mode_line = true;
16505 w->update_mode_line = true;
16506 }
16507
16508 /* Try to scroll by specified few lines. */
16509 if ((scroll_conservatively
16510 || emacs_scroll_step
16511 || temp_scroll_step
16512 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16513 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16514 && CHARPOS (startp) >= BEGV
16515 && CHARPOS (startp) <= ZV)
16516 {
16517 /* The function returns -1 if new fonts were loaded, 1 if
16518 successful, 0 if not successful. */
16519 int ss = try_scrolling (window, just_this_one_p,
16520 scroll_conservatively,
16521 emacs_scroll_step,
16522 temp_scroll_step, last_line_misfit);
16523 switch (ss)
16524 {
16525 case SCROLLING_SUCCESS:
16526 goto done;
16527
16528 case SCROLLING_NEED_LARGER_MATRICES:
16529 goto need_larger_matrices;
16530
16531 case SCROLLING_FAILED:
16532 break;
16533
16534 default:
16535 emacs_abort ();
16536 }
16537 }
16538
16539 /* Finally, just choose a place to start which positions point
16540 according to user preferences. */
16541
16542 recenter:
16543
16544 #ifdef GLYPH_DEBUG
16545 debug_method_add (w, "recenter");
16546 #endif
16547
16548 /* Forget any previously recorded base line for line number display. */
16549 if (!buffer_unchanged_p)
16550 w->base_line_number = 0;
16551
16552 /* Determine the window start relative to point. */
16553 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16554 it.current_y = it.last_visible_y;
16555 if (centering_position < 0)
16556 {
16557 int window_total_lines
16558 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16559 int margin
16560 = scroll_margin > 0
16561 ? min (scroll_margin, window_total_lines / 4)
16562 : 0;
16563 ptrdiff_t margin_pos = CHARPOS (startp);
16564 Lisp_Object aggressive;
16565 bool scrolling_up;
16566
16567 /* If there is a scroll margin at the top of the window, find
16568 its character position. */
16569 if (margin
16570 /* Cannot call start_display if startp is not in the
16571 accessible region of the buffer. This can happen when we
16572 have just switched to a different buffer and/or changed
16573 its restriction. In that case, startp is initialized to
16574 the character position 1 (BEGV) because we did not yet
16575 have chance to display the buffer even once. */
16576 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16577 {
16578 struct it it1;
16579 void *it1data = NULL;
16580
16581 SAVE_IT (it1, it, it1data);
16582 start_display (&it1, w, startp);
16583 move_it_vertically (&it1, margin * frame_line_height);
16584 margin_pos = IT_CHARPOS (it1);
16585 RESTORE_IT (&it, &it, it1data);
16586 }
16587 scrolling_up = PT > margin_pos;
16588 aggressive =
16589 scrolling_up
16590 ? BVAR (current_buffer, scroll_up_aggressively)
16591 : BVAR (current_buffer, scroll_down_aggressively);
16592
16593 if (!MINI_WINDOW_P (w)
16594 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16595 {
16596 int pt_offset = 0;
16597
16598 /* Setting scroll-conservatively overrides
16599 scroll-*-aggressively. */
16600 if (!scroll_conservatively && NUMBERP (aggressive))
16601 {
16602 double float_amount = XFLOATINT (aggressive);
16603
16604 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16605 if (pt_offset == 0 && float_amount > 0)
16606 pt_offset = 1;
16607 if (pt_offset && margin > 0)
16608 margin -= 1;
16609 }
16610 /* Compute how much to move the window start backward from
16611 point so that point will be displayed where the user
16612 wants it. */
16613 if (scrolling_up)
16614 {
16615 centering_position = it.last_visible_y;
16616 if (pt_offset)
16617 centering_position -= pt_offset;
16618 centering_position -=
16619 (frame_line_height * (1 + margin + last_line_misfit)
16620 + WINDOW_HEADER_LINE_HEIGHT (w));
16621 /* Don't let point enter the scroll margin near top of
16622 the window. */
16623 if (centering_position < margin * frame_line_height)
16624 centering_position = margin * frame_line_height;
16625 }
16626 else
16627 centering_position = margin * frame_line_height + pt_offset;
16628 }
16629 else
16630 /* Set the window start half the height of the window backward
16631 from point. */
16632 centering_position = window_box_height (w) / 2;
16633 }
16634 move_it_vertically_backward (&it, centering_position);
16635
16636 eassert (IT_CHARPOS (it) >= BEGV);
16637
16638 /* The function move_it_vertically_backward may move over more
16639 than the specified y-distance. If it->w is small, e.g. a
16640 mini-buffer window, we may end up in front of the window's
16641 display area. Start displaying at the start of the line
16642 containing PT in this case. */
16643 if (it.current_y <= 0)
16644 {
16645 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16646 move_it_vertically_backward (&it, 0);
16647 it.current_y = 0;
16648 }
16649
16650 it.current_x = it.hpos = 0;
16651
16652 /* Set the window start position here explicitly, to avoid an
16653 infinite loop in case the functions in window-scroll-functions
16654 get errors. */
16655 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16656
16657 /* Run scroll hooks. */
16658 startp = run_window_scroll_functions (window, it.current.pos);
16659
16660 /* Redisplay the window. */
16661 if (!current_matrix_up_to_date_p
16662 || windows_or_buffers_changed
16663 || f->cursor_type_changed
16664 /* Don't use try_window_reusing_current_matrix in this case
16665 because it can have changed the buffer. */
16666 || !NILP (Vwindow_scroll_functions)
16667 || !just_this_one_p
16668 || MINI_WINDOW_P (w)
16669 || !(used_current_matrix_p
16670 = try_window_reusing_current_matrix (w)))
16671 try_window (window, startp, 0);
16672
16673 /* If new fonts have been loaded (due to fontsets), give up. We
16674 have to start a new redisplay since we need to re-adjust glyph
16675 matrices. */
16676 if (f->fonts_changed)
16677 goto need_larger_matrices;
16678
16679 /* If cursor did not appear assume that the middle of the window is
16680 in the first line of the window. Do it again with the next line.
16681 (Imagine a window of height 100, displaying two lines of height
16682 60. Moving back 50 from it->last_visible_y will end in the first
16683 line.) */
16684 if (w->cursor.vpos < 0)
16685 {
16686 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16687 {
16688 clear_glyph_matrix (w->desired_matrix);
16689 move_it_by_lines (&it, 1);
16690 try_window (window, it.current.pos, 0);
16691 }
16692 else if (PT < IT_CHARPOS (it))
16693 {
16694 clear_glyph_matrix (w->desired_matrix);
16695 move_it_by_lines (&it, -1);
16696 try_window (window, it.current.pos, 0);
16697 }
16698 else
16699 {
16700 /* Not much we can do about it. */
16701 }
16702 }
16703
16704 /* Consider the following case: Window starts at BEGV, there is
16705 invisible, intangible text at BEGV, so that display starts at
16706 some point START > BEGV. It can happen that we are called with
16707 PT somewhere between BEGV and START. Try to handle that case,
16708 and similar ones. */
16709 if (w->cursor.vpos < 0)
16710 {
16711 /* First, try locating the proper glyph row for PT. */
16712 struct glyph_row *row =
16713 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16714
16715 /* Sometimes point is at the beginning of invisible text that is
16716 before the 1st character displayed in the row. In that case,
16717 row_containing_pos fails to find the row, because no glyphs
16718 with appropriate buffer positions are present in the row.
16719 Therefore, we next try to find the row which shows the 1st
16720 position after the invisible text. */
16721 if (!row)
16722 {
16723 Lisp_Object val =
16724 get_char_property_and_overlay (make_number (PT), Qinvisible,
16725 Qnil, NULL);
16726
16727 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
16728 {
16729 ptrdiff_t alt_pos;
16730 Lisp_Object invis_end =
16731 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16732 Qnil, Qnil);
16733
16734 if (NATNUMP (invis_end))
16735 alt_pos = XFASTINT (invis_end);
16736 else
16737 alt_pos = ZV;
16738 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16739 NULL, 0);
16740 }
16741 }
16742 /* Finally, fall back on the first row of the window after the
16743 header line (if any). This is slightly better than not
16744 displaying the cursor at all. */
16745 if (!row)
16746 {
16747 row = w->current_matrix->rows;
16748 if (row->mode_line_p)
16749 ++row;
16750 }
16751 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16752 }
16753
16754 if (!cursor_row_fully_visible_p (w, false, false))
16755 {
16756 /* If vscroll is enabled, disable it and try again. */
16757 if (w->vscroll)
16758 {
16759 w->vscroll = 0;
16760 clear_glyph_matrix (w->desired_matrix);
16761 goto recenter;
16762 }
16763
16764 /* Users who set scroll-conservatively to a large number want
16765 point just above/below the scroll margin. If we ended up
16766 with point's row partially visible, move the window start to
16767 make that row fully visible and out of the margin. */
16768 if (scroll_conservatively > SCROLL_LIMIT)
16769 {
16770 int window_total_lines
16771 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16772 int margin =
16773 scroll_margin > 0
16774 ? min (scroll_margin, window_total_lines / 4)
16775 : 0;
16776 bool move_down = w->cursor.vpos >= window_total_lines / 2;
16777
16778 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16779 clear_glyph_matrix (w->desired_matrix);
16780 if (1 == try_window (window, it.current.pos,
16781 TRY_WINDOW_CHECK_MARGINS))
16782 goto done;
16783 }
16784
16785 /* If centering point failed to make the whole line visible,
16786 put point at the top instead. That has to make the whole line
16787 visible, if it can be done. */
16788 if (centering_position == 0)
16789 goto done;
16790
16791 clear_glyph_matrix (w->desired_matrix);
16792 centering_position = 0;
16793 goto recenter;
16794 }
16795
16796 done:
16797
16798 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16799 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16800 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16801
16802 /* Display the mode line, if we must. */
16803 if ((update_mode_line
16804 /* If window not full width, must redo its mode line
16805 if (a) the window to its side is being redone and
16806 (b) we do a frame-based redisplay. This is a consequence
16807 of how inverted lines are drawn in frame-based redisplay. */
16808 || (!just_this_one_p
16809 && !FRAME_WINDOW_P (f)
16810 && !WINDOW_FULL_WIDTH_P (w))
16811 /* Line number to display. */
16812 || w->base_line_pos > 0
16813 /* Column number is displayed and different from the one displayed. */
16814 || (w->column_number_displayed != -1
16815 && (w->column_number_displayed != current_column ())))
16816 /* This means that the window has a mode line. */
16817 && (WINDOW_WANTS_MODELINE_P (w)
16818 || WINDOW_WANTS_HEADER_LINE_P (w)))
16819 {
16820
16821 display_mode_lines (w);
16822
16823 /* If mode line height has changed, arrange for a thorough
16824 immediate redisplay using the correct mode line height. */
16825 if (WINDOW_WANTS_MODELINE_P (w)
16826 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16827 {
16828 f->fonts_changed = true;
16829 w->mode_line_height = -1;
16830 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16831 = DESIRED_MODE_LINE_HEIGHT (w);
16832 }
16833
16834 /* If header line height has changed, arrange for a thorough
16835 immediate redisplay using the correct header line height. */
16836 if (WINDOW_WANTS_HEADER_LINE_P (w)
16837 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16838 {
16839 f->fonts_changed = true;
16840 w->header_line_height = -1;
16841 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16842 = DESIRED_HEADER_LINE_HEIGHT (w);
16843 }
16844
16845 if (f->fonts_changed)
16846 goto need_larger_matrices;
16847 }
16848
16849 if (!line_number_displayed && w->base_line_pos != -1)
16850 {
16851 w->base_line_pos = 0;
16852 w->base_line_number = 0;
16853 }
16854
16855 finish_menu_bars:
16856
16857 /* When we reach a frame's selected window, redo the frame's menu
16858 bar and the frame's title. */
16859 if (update_mode_line
16860 && EQ (FRAME_SELECTED_WINDOW (f), window))
16861 {
16862 bool redisplay_menu_p;
16863
16864 if (FRAME_WINDOW_P (f))
16865 {
16866 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16867 || defined (HAVE_NS) || defined (USE_GTK)
16868 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16869 #else
16870 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16871 #endif
16872 }
16873 else
16874 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16875
16876 if (redisplay_menu_p)
16877 display_menu_bar (w);
16878
16879 #ifdef HAVE_WINDOW_SYSTEM
16880 if (FRAME_WINDOW_P (f))
16881 {
16882 #if defined (USE_GTK) || defined (HAVE_NS)
16883 if (FRAME_EXTERNAL_TOOL_BAR (f))
16884 redisplay_tool_bar (f);
16885 #else
16886 if (WINDOWP (f->tool_bar_window)
16887 && (FRAME_TOOL_BAR_LINES (f) > 0
16888 || !NILP (Vauto_resize_tool_bars))
16889 && redisplay_tool_bar (f))
16890 ignore_mouse_drag_p = true;
16891 #endif
16892 }
16893 x_consider_frame_title (w->frame);
16894 #endif
16895 }
16896
16897 #ifdef HAVE_WINDOW_SYSTEM
16898 if (FRAME_WINDOW_P (f)
16899 && update_window_fringes (w, (just_this_one_p
16900 || (!used_current_matrix_p && !overlay_arrow_seen)
16901 || w->pseudo_window_p)))
16902 {
16903 update_begin (f);
16904 block_input ();
16905 if (draw_window_fringes (w, true))
16906 {
16907 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16908 x_draw_right_divider (w);
16909 else
16910 x_draw_vertical_border (w);
16911 }
16912 unblock_input ();
16913 update_end (f);
16914 }
16915
16916 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16917 x_draw_bottom_divider (w);
16918 #endif /* HAVE_WINDOW_SYSTEM */
16919
16920 /* We go to this label, with fonts_changed set, if it is
16921 necessary to try again using larger glyph matrices.
16922 We have to redeem the scroll bar even in this case,
16923 because the loop in redisplay_internal expects that. */
16924 need_larger_matrices:
16925 ;
16926 finish_scroll_bars:
16927
16928 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16929 {
16930 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16931 /* Set the thumb's position and size. */
16932 set_vertical_scroll_bar (w);
16933
16934 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16935 /* Set the thumb's position and size. */
16936 set_horizontal_scroll_bar (w);
16937
16938 /* Note that we actually used the scroll bar attached to this
16939 window, so it shouldn't be deleted at the end of redisplay. */
16940 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16941 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16942 }
16943
16944 /* Restore current_buffer and value of point in it. The window
16945 update may have changed the buffer, so first make sure `opoint'
16946 is still valid (Bug#6177). */
16947 if (CHARPOS (opoint) < BEGV)
16948 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16949 else if (CHARPOS (opoint) > ZV)
16950 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16951 else
16952 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16953
16954 set_buffer_internal_1 (old);
16955 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16956 shorter. This can be caused by log truncation in *Messages*. */
16957 if (CHARPOS (lpoint) <= ZV)
16958 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16959
16960 unbind_to (count, Qnil);
16961 }
16962
16963
16964 /* Build the complete desired matrix of WINDOW with a window start
16965 buffer position POS.
16966
16967 Value is 1 if successful. It is zero if fonts were loaded during
16968 redisplay which makes re-adjusting glyph matrices necessary, and -1
16969 if point would appear in the scroll margins.
16970 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16971 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16972 set in FLAGS.) */
16973
16974 int
16975 try_window (Lisp_Object window, struct text_pos pos, int flags)
16976 {
16977 struct window *w = XWINDOW (window);
16978 struct it it;
16979 struct glyph_row *last_text_row = NULL;
16980 struct frame *f = XFRAME (w->frame);
16981 int frame_line_height = default_line_pixel_height (w);
16982
16983 /* Make POS the new window start. */
16984 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16985
16986 /* Mark cursor position as unknown. No overlay arrow seen. */
16987 w->cursor.vpos = -1;
16988 overlay_arrow_seen = false;
16989
16990 /* Initialize iterator and info to start at POS. */
16991 start_display (&it, w, pos);
16992 it.glyph_row->reversed_p = false;
16993
16994 /* Display all lines of W. */
16995 while (it.current_y < it.last_visible_y)
16996 {
16997 if (display_line (&it))
16998 last_text_row = it.glyph_row - 1;
16999 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17000 return 0;
17001 }
17002
17003 /* Don't let the cursor end in the scroll margins. */
17004 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17005 && !MINI_WINDOW_P (w))
17006 {
17007 int this_scroll_margin;
17008 int window_total_lines
17009 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17010
17011 if (scroll_margin > 0)
17012 {
17013 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
17014 this_scroll_margin *= frame_line_height;
17015 }
17016 else
17017 this_scroll_margin = 0;
17018
17019 if ((w->cursor.y >= 0 /* not vscrolled */
17020 && w->cursor.y < this_scroll_margin
17021 && CHARPOS (pos) > BEGV
17022 && IT_CHARPOS (it) < ZV)
17023 /* rms: considering make_cursor_line_fully_visible_p here
17024 seems to give wrong results. We don't want to recenter
17025 when the last line is partly visible, we want to allow
17026 that case to be handled in the usual way. */
17027 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
17028 {
17029 w->cursor.vpos = -1;
17030 clear_glyph_matrix (w->desired_matrix);
17031 return -1;
17032 }
17033 }
17034
17035 /* If bottom moved off end of frame, change mode line percentage. */
17036 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
17037 w->update_mode_line = true;
17038
17039 /* Set window_end_pos to the offset of the last character displayed
17040 on the window from the end of current_buffer. Set
17041 window_end_vpos to its row number. */
17042 if (last_text_row)
17043 {
17044 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17045 adjust_window_ends (w, last_text_row, false);
17046 eassert
17047 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17048 w->window_end_vpos)));
17049 }
17050 else
17051 {
17052 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17053 w->window_end_pos = Z - ZV;
17054 w->window_end_vpos = 0;
17055 }
17056
17057 /* But that is not valid info until redisplay finishes. */
17058 w->window_end_valid = false;
17059 return 1;
17060 }
17061
17062
17063 \f
17064 /************************************************************************
17065 Window redisplay reusing current matrix when buffer has not changed
17066 ************************************************************************/
17067
17068 /* Try redisplay of window W showing an unchanged buffer with a
17069 different window start than the last time it was displayed by
17070 reusing its current matrix. Value is true if successful.
17071 W->start is the new window start. */
17072
17073 static bool
17074 try_window_reusing_current_matrix (struct window *w)
17075 {
17076 struct frame *f = XFRAME (w->frame);
17077 struct glyph_row *bottom_row;
17078 struct it it;
17079 struct run run;
17080 struct text_pos start, new_start;
17081 int nrows_scrolled, i;
17082 struct glyph_row *last_text_row;
17083 struct glyph_row *last_reused_text_row;
17084 struct glyph_row *start_row;
17085 int start_vpos, min_y, max_y;
17086
17087 #ifdef GLYPH_DEBUG
17088 if (inhibit_try_window_reusing)
17089 return false;
17090 #endif
17091
17092 if (/* This function doesn't handle terminal frames. */
17093 !FRAME_WINDOW_P (f)
17094 /* Don't try to reuse the display if windows have been split
17095 or such. */
17096 || windows_or_buffers_changed
17097 || f->cursor_type_changed)
17098 return false;
17099
17100 /* Can't do this if showing trailing whitespace. */
17101 if (!NILP (Vshow_trailing_whitespace))
17102 return false;
17103
17104 /* If top-line visibility has changed, give up. */
17105 if (WINDOW_WANTS_HEADER_LINE_P (w)
17106 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17107 return false;
17108
17109 /* Give up if old or new display is scrolled vertically. We could
17110 make this function handle this, but right now it doesn't. */
17111 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17112 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17113 return false;
17114
17115 /* The variable new_start now holds the new window start. The old
17116 start `start' can be determined from the current matrix. */
17117 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17118 start = start_row->minpos;
17119 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17120
17121 /* Clear the desired matrix for the display below. */
17122 clear_glyph_matrix (w->desired_matrix);
17123
17124 if (CHARPOS (new_start) <= CHARPOS (start))
17125 {
17126 /* Don't use this method if the display starts with an ellipsis
17127 displayed for invisible text. It's not easy to handle that case
17128 below, and it's certainly not worth the effort since this is
17129 not a frequent case. */
17130 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17131 return false;
17132
17133 IF_DEBUG (debug_method_add (w, "twu1"));
17134
17135 /* Display up to a row that can be reused. The variable
17136 last_text_row is set to the last row displayed that displays
17137 text. Note that it.vpos == 0 if or if not there is a
17138 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17139 start_display (&it, w, new_start);
17140 w->cursor.vpos = -1;
17141 last_text_row = last_reused_text_row = NULL;
17142
17143 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17144 {
17145 /* If we have reached into the characters in the START row,
17146 that means the line boundaries have changed. So we
17147 can't start copying with the row START. Maybe it will
17148 work to start copying with the following row. */
17149 while (IT_CHARPOS (it) > CHARPOS (start))
17150 {
17151 /* Advance to the next row as the "start". */
17152 start_row++;
17153 start = start_row->minpos;
17154 /* If there are no more rows to try, or just one, give up. */
17155 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17156 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17157 || CHARPOS (start) == ZV)
17158 {
17159 clear_glyph_matrix (w->desired_matrix);
17160 return false;
17161 }
17162
17163 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17164 }
17165 /* If we have reached alignment, we can copy the rest of the
17166 rows. */
17167 if (IT_CHARPOS (it) == CHARPOS (start)
17168 /* Don't accept "alignment" inside a display vector,
17169 since start_row could have started in the middle of
17170 that same display vector (thus their character
17171 positions match), and we have no way of telling if
17172 that is the case. */
17173 && it.current.dpvec_index < 0)
17174 break;
17175
17176 it.glyph_row->reversed_p = false;
17177 if (display_line (&it))
17178 last_text_row = it.glyph_row - 1;
17179
17180 }
17181
17182 /* A value of current_y < last_visible_y means that we stopped
17183 at the previous window start, which in turn means that we
17184 have at least one reusable row. */
17185 if (it.current_y < it.last_visible_y)
17186 {
17187 struct glyph_row *row;
17188
17189 /* IT.vpos always starts from 0; it counts text lines. */
17190 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17191
17192 /* Find PT if not already found in the lines displayed. */
17193 if (w->cursor.vpos < 0)
17194 {
17195 int dy = it.current_y - start_row->y;
17196
17197 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17198 row = row_containing_pos (w, PT, row, NULL, dy);
17199 if (row)
17200 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17201 dy, nrows_scrolled);
17202 else
17203 {
17204 clear_glyph_matrix (w->desired_matrix);
17205 return false;
17206 }
17207 }
17208
17209 /* Scroll the display. Do it before the current matrix is
17210 changed. The problem here is that update has not yet
17211 run, i.e. part of the current matrix is not up to date.
17212 scroll_run_hook will clear the cursor, and use the
17213 current matrix to get the height of the row the cursor is
17214 in. */
17215 run.current_y = start_row->y;
17216 run.desired_y = it.current_y;
17217 run.height = it.last_visible_y - it.current_y;
17218
17219 if (run.height > 0 && run.current_y != run.desired_y)
17220 {
17221 update_begin (f);
17222 FRAME_RIF (f)->update_window_begin_hook (w);
17223 FRAME_RIF (f)->clear_window_mouse_face (w);
17224 FRAME_RIF (f)->scroll_run_hook (w, &run);
17225 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17226 update_end (f);
17227 }
17228
17229 /* Shift current matrix down by nrows_scrolled lines. */
17230 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17231 rotate_matrix (w->current_matrix,
17232 start_vpos,
17233 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17234 nrows_scrolled);
17235
17236 /* Disable lines that must be updated. */
17237 for (i = 0; i < nrows_scrolled; ++i)
17238 (start_row + i)->enabled_p = false;
17239
17240 /* Re-compute Y positions. */
17241 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17242 max_y = it.last_visible_y;
17243 for (row = start_row + nrows_scrolled;
17244 row < bottom_row;
17245 ++row)
17246 {
17247 row->y = it.current_y;
17248 row->visible_height = row->height;
17249
17250 if (row->y < min_y)
17251 row->visible_height -= min_y - row->y;
17252 if (row->y + row->height > max_y)
17253 row->visible_height -= row->y + row->height - max_y;
17254 if (row->fringe_bitmap_periodic_p)
17255 row->redraw_fringe_bitmaps_p = true;
17256
17257 it.current_y += row->height;
17258
17259 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17260 last_reused_text_row = row;
17261 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17262 break;
17263 }
17264
17265 /* Disable lines in the current matrix which are now
17266 below the window. */
17267 for (++row; row < bottom_row; ++row)
17268 row->enabled_p = row->mode_line_p = false;
17269 }
17270
17271 /* Update window_end_pos etc.; last_reused_text_row is the last
17272 reused row from the current matrix containing text, if any.
17273 The value of last_text_row is the last displayed line
17274 containing text. */
17275 if (last_reused_text_row)
17276 adjust_window_ends (w, last_reused_text_row, true);
17277 else if (last_text_row)
17278 adjust_window_ends (w, last_text_row, false);
17279 else
17280 {
17281 /* This window must be completely empty. */
17282 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17283 w->window_end_pos = Z - ZV;
17284 w->window_end_vpos = 0;
17285 }
17286 w->window_end_valid = false;
17287
17288 /* Update hint: don't try scrolling again in update_window. */
17289 w->desired_matrix->no_scrolling_p = true;
17290
17291 #ifdef GLYPH_DEBUG
17292 debug_method_add (w, "try_window_reusing_current_matrix 1");
17293 #endif
17294 return true;
17295 }
17296 else if (CHARPOS (new_start) > CHARPOS (start))
17297 {
17298 struct glyph_row *pt_row, *row;
17299 struct glyph_row *first_reusable_row;
17300 struct glyph_row *first_row_to_display;
17301 int dy;
17302 int yb = window_text_bottom_y (w);
17303
17304 /* Find the row starting at new_start, if there is one. Don't
17305 reuse a partially visible line at the end. */
17306 first_reusable_row = start_row;
17307 while (first_reusable_row->enabled_p
17308 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17309 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17310 < CHARPOS (new_start)))
17311 ++first_reusable_row;
17312
17313 /* Give up if there is no row to reuse. */
17314 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17315 || !first_reusable_row->enabled_p
17316 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17317 != CHARPOS (new_start)))
17318 return false;
17319
17320 /* We can reuse fully visible rows beginning with
17321 first_reusable_row to the end of the window. Set
17322 first_row_to_display to the first row that cannot be reused.
17323 Set pt_row to the row containing point, if there is any. */
17324 pt_row = NULL;
17325 for (first_row_to_display = first_reusable_row;
17326 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17327 ++first_row_to_display)
17328 {
17329 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17330 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17331 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17332 && first_row_to_display->ends_at_zv_p
17333 && pt_row == NULL)))
17334 pt_row = first_row_to_display;
17335 }
17336
17337 /* Start displaying at the start of first_row_to_display. */
17338 eassert (first_row_to_display->y < yb);
17339 init_to_row_start (&it, w, first_row_to_display);
17340
17341 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17342 - start_vpos);
17343 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17344 - nrows_scrolled);
17345 it.current_y = (first_row_to_display->y - first_reusable_row->y
17346 + WINDOW_HEADER_LINE_HEIGHT (w));
17347
17348 /* Display lines beginning with first_row_to_display in the
17349 desired matrix. Set last_text_row to the last row displayed
17350 that displays text. */
17351 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17352 if (pt_row == NULL)
17353 w->cursor.vpos = -1;
17354 last_text_row = NULL;
17355 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17356 if (display_line (&it))
17357 last_text_row = it.glyph_row - 1;
17358
17359 /* If point is in a reused row, adjust y and vpos of the cursor
17360 position. */
17361 if (pt_row)
17362 {
17363 w->cursor.vpos -= nrows_scrolled;
17364 w->cursor.y -= first_reusable_row->y - start_row->y;
17365 }
17366
17367 /* Give up if point isn't in a row displayed or reused. (This
17368 also handles the case where w->cursor.vpos < nrows_scrolled
17369 after the calls to display_line, which can happen with scroll
17370 margins. See bug#1295.) */
17371 if (w->cursor.vpos < 0)
17372 {
17373 clear_glyph_matrix (w->desired_matrix);
17374 return false;
17375 }
17376
17377 /* Scroll the display. */
17378 run.current_y = first_reusable_row->y;
17379 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17380 run.height = it.last_visible_y - run.current_y;
17381 dy = run.current_y - run.desired_y;
17382
17383 if (run.height)
17384 {
17385 update_begin (f);
17386 FRAME_RIF (f)->update_window_begin_hook (w);
17387 FRAME_RIF (f)->clear_window_mouse_face (w);
17388 FRAME_RIF (f)->scroll_run_hook (w, &run);
17389 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17390 update_end (f);
17391 }
17392
17393 /* Adjust Y positions of reused rows. */
17394 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17395 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17396 max_y = it.last_visible_y;
17397 for (row = first_reusable_row; row < first_row_to_display; ++row)
17398 {
17399 row->y -= dy;
17400 row->visible_height = row->height;
17401 if (row->y < min_y)
17402 row->visible_height -= min_y - row->y;
17403 if (row->y + row->height > max_y)
17404 row->visible_height -= row->y + row->height - max_y;
17405 if (row->fringe_bitmap_periodic_p)
17406 row->redraw_fringe_bitmaps_p = true;
17407 }
17408
17409 /* Scroll the current matrix. */
17410 eassert (nrows_scrolled > 0);
17411 rotate_matrix (w->current_matrix,
17412 start_vpos,
17413 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17414 -nrows_scrolled);
17415
17416 /* Disable rows not reused. */
17417 for (row -= nrows_scrolled; row < bottom_row; ++row)
17418 row->enabled_p = false;
17419
17420 /* Point may have moved to a different line, so we cannot assume that
17421 the previous cursor position is valid; locate the correct row. */
17422 if (pt_row)
17423 {
17424 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17425 row < bottom_row
17426 && PT >= MATRIX_ROW_END_CHARPOS (row)
17427 && !row->ends_at_zv_p;
17428 row++)
17429 {
17430 w->cursor.vpos++;
17431 w->cursor.y = row->y;
17432 }
17433 if (row < bottom_row)
17434 {
17435 /* Can't simply scan the row for point with
17436 bidi-reordered glyph rows. Let set_cursor_from_row
17437 figure out where to put the cursor, and if it fails,
17438 give up. */
17439 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17440 {
17441 if (!set_cursor_from_row (w, row, w->current_matrix,
17442 0, 0, 0, 0))
17443 {
17444 clear_glyph_matrix (w->desired_matrix);
17445 return false;
17446 }
17447 }
17448 else
17449 {
17450 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17451 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17452
17453 for (; glyph < end
17454 && (!BUFFERP (glyph->object)
17455 || glyph->charpos < PT);
17456 glyph++)
17457 {
17458 w->cursor.hpos++;
17459 w->cursor.x += glyph->pixel_width;
17460 }
17461 }
17462 }
17463 }
17464
17465 /* Adjust window end. A null value of last_text_row means that
17466 the window end is in reused rows which in turn means that
17467 only its vpos can have changed. */
17468 if (last_text_row)
17469 adjust_window_ends (w, last_text_row, false);
17470 else
17471 w->window_end_vpos -= nrows_scrolled;
17472
17473 w->window_end_valid = false;
17474 w->desired_matrix->no_scrolling_p = true;
17475
17476 #ifdef GLYPH_DEBUG
17477 debug_method_add (w, "try_window_reusing_current_matrix 2");
17478 #endif
17479 return true;
17480 }
17481
17482 return false;
17483 }
17484
17485
17486 \f
17487 /************************************************************************
17488 Window redisplay reusing current matrix when buffer has changed
17489 ************************************************************************/
17490
17491 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17492 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17493 ptrdiff_t *, ptrdiff_t *);
17494 static struct glyph_row *
17495 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17496 struct glyph_row *);
17497
17498
17499 /* Return the last row in MATRIX displaying text. If row START is
17500 non-null, start searching with that row. IT gives the dimensions
17501 of the display. Value is null if matrix is empty; otherwise it is
17502 a pointer to the row found. */
17503
17504 static struct glyph_row *
17505 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17506 struct glyph_row *start)
17507 {
17508 struct glyph_row *row, *row_found;
17509
17510 /* Set row_found to the last row in IT->w's current matrix
17511 displaying text. The loop looks funny but think of partially
17512 visible lines. */
17513 row_found = NULL;
17514 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17515 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17516 {
17517 eassert (row->enabled_p);
17518 row_found = row;
17519 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17520 break;
17521 ++row;
17522 }
17523
17524 return row_found;
17525 }
17526
17527
17528 /* Return the last row in the current matrix of W that is not affected
17529 by changes at the start of current_buffer that occurred since W's
17530 current matrix was built. Value is null if no such row exists.
17531
17532 BEG_UNCHANGED us the number of characters unchanged at the start of
17533 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17534 first changed character in current_buffer. Characters at positions <
17535 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17536 when the current matrix was built. */
17537
17538 static struct glyph_row *
17539 find_last_unchanged_at_beg_row (struct window *w)
17540 {
17541 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17542 struct glyph_row *row;
17543 struct glyph_row *row_found = NULL;
17544 int yb = window_text_bottom_y (w);
17545
17546 /* Find the last row displaying unchanged text. */
17547 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17548 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17549 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17550 ++row)
17551 {
17552 if (/* If row ends before first_changed_pos, it is unchanged,
17553 except in some case. */
17554 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17555 /* When row ends in ZV and we write at ZV it is not
17556 unchanged. */
17557 && !row->ends_at_zv_p
17558 /* When first_changed_pos is the end of a continued line,
17559 row is not unchanged because it may be no longer
17560 continued. */
17561 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17562 && (row->continued_p
17563 || row->exact_window_width_line_p))
17564 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17565 needs to be recomputed, so don't consider this row as
17566 unchanged. This happens when the last line was
17567 bidi-reordered and was killed immediately before this
17568 redisplay cycle. In that case, ROW->end stores the
17569 buffer position of the first visual-order character of
17570 the killed text, which is now beyond ZV. */
17571 && CHARPOS (row->end.pos) <= ZV)
17572 row_found = row;
17573
17574 /* Stop if last visible row. */
17575 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17576 break;
17577 }
17578
17579 return row_found;
17580 }
17581
17582
17583 /* Find the first glyph row in the current matrix of W that is not
17584 affected by changes at the end of current_buffer since the
17585 time W's current matrix was built.
17586
17587 Return in *DELTA the number of chars by which buffer positions in
17588 unchanged text at the end of current_buffer must be adjusted.
17589
17590 Return in *DELTA_BYTES the corresponding number of bytes.
17591
17592 Value is null if no such row exists, i.e. all rows are affected by
17593 changes. */
17594
17595 static struct glyph_row *
17596 find_first_unchanged_at_end_row (struct window *w,
17597 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17598 {
17599 struct glyph_row *row;
17600 struct glyph_row *row_found = NULL;
17601
17602 *delta = *delta_bytes = 0;
17603
17604 /* Display must not have been paused, otherwise the current matrix
17605 is not up to date. */
17606 eassert (w->window_end_valid);
17607
17608 /* A value of window_end_pos >= END_UNCHANGED means that the window
17609 end is in the range of changed text. If so, there is no
17610 unchanged row at the end of W's current matrix. */
17611 if (w->window_end_pos >= END_UNCHANGED)
17612 return NULL;
17613
17614 /* Set row to the last row in W's current matrix displaying text. */
17615 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17616
17617 /* If matrix is entirely empty, no unchanged row exists. */
17618 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17619 {
17620 /* The value of row is the last glyph row in the matrix having a
17621 meaningful buffer position in it. The end position of row
17622 corresponds to window_end_pos. This allows us to translate
17623 buffer positions in the current matrix to current buffer
17624 positions for characters not in changed text. */
17625 ptrdiff_t Z_old =
17626 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17627 ptrdiff_t Z_BYTE_old =
17628 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17629 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17630 struct glyph_row *first_text_row
17631 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17632
17633 *delta = Z - Z_old;
17634 *delta_bytes = Z_BYTE - Z_BYTE_old;
17635
17636 /* Set last_unchanged_pos to the buffer position of the last
17637 character in the buffer that has not been changed. Z is the
17638 index + 1 of the last character in current_buffer, i.e. by
17639 subtracting END_UNCHANGED we get the index of the last
17640 unchanged character, and we have to add BEG to get its buffer
17641 position. */
17642 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17643 last_unchanged_pos_old = last_unchanged_pos - *delta;
17644
17645 /* Search backward from ROW for a row displaying a line that
17646 starts at a minimum position >= last_unchanged_pos_old. */
17647 for (; row > first_text_row; --row)
17648 {
17649 /* This used to abort, but it can happen.
17650 It is ok to just stop the search instead here. KFS. */
17651 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17652 break;
17653
17654 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17655 row_found = row;
17656 }
17657 }
17658
17659 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17660
17661 return row_found;
17662 }
17663
17664
17665 /* Make sure that glyph rows in the current matrix of window W
17666 reference the same glyph memory as corresponding rows in the
17667 frame's frame matrix. This function is called after scrolling W's
17668 current matrix on a terminal frame in try_window_id and
17669 try_window_reusing_current_matrix. */
17670
17671 static void
17672 sync_frame_with_window_matrix_rows (struct window *w)
17673 {
17674 struct frame *f = XFRAME (w->frame);
17675 struct glyph_row *window_row, *window_row_end, *frame_row;
17676
17677 /* Preconditions: W must be a leaf window and full-width. Its frame
17678 must have a frame matrix. */
17679 eassert (BUFFERP (w->contents));
17680 eassert (WINDOW_FULL_WIDTH_P (w));
17681 eassert (!FRAME_WINDOW_P (f));
17682
17683 /* If W is a full-width window, glyph pointers in W's current matrix
17684 have, by definition, to be the same as glyph pointers in the
17685 corresponding frame matrix. Note that frame matrices have no
17686 marginal areas (see build_frame_matrix). */
17687 window_row = w->current_matrix->rows;
17688 window_row_end = window_row + w->current_matrix->nrows;
17689 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17690 while (window_row < window_row_end)
17691 {
17692 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17693 struct glyph *end = window_row->glyphs[LAST_AREA];
17694
17695 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17696 frame_row->glyphs[TEXT_AREA] = start;
17697 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17698 frame_row->glyphs[LAST_AREA] = end;
17699
17700 /* Disable frame rows whose corresponding window rows have
17701 been disabled in try_window_id. */
17702 if (!window_row->enabled_p)
17703 frame_row->enabled_p = false;
17704
17705 ++window_row, ++frame_row;
17706 }
17707 }
17708
17709
17710 /* Find the glyph row in window W containing CHARPOS. Consider all
17711 rows between START and END (not inclusive). END null means search
17712 all rows to the end of the display area of W. Value is the row
17713 containing CHARPOS or null. */
17714
17715 struct glyph_row *
17716 row_containing_pos (struct window *w, ptrdiff_t charpos,
17717 struct glyph_row *start, struct glyph_row *end, int dy)
17718 {
17719 struct glyph_row *row = start;
17720 struct glyph_row *best_row = NULL;
17721 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17722 int last_y;
17723
17724 /* If we happen to start on a header-line, skip that. */
17725 if (row->mode_line_p)
17726 ++row;
17727
17728 if ((end && row >= end) || !row->enabled_p)
17729 return NULL;
17730
17731 last_y = window_text_bottom_y (w) - dy;
17732
17733 while (true)
17734 {
17735 /* Give up if we have gone too far. */
17736 if (end && row >= end)
17737 return NULL;
17738 /* This formerly returned if they were equal.
17739 I think that both quantities are of a "last plus one" type;
17740 if so, when they are equal, the row is within the screen. -- rms. */
17741 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17742 return NULL;
17743
17744 /* If it is in this row, return this row. */
17745 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17746 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17747 /* The end position of a row equals the start
17748 position of the next row. If CHARPOS is there, we
17749 would rather consider it displayed in the next
17750 line, except when this line ends in ZV. */
17751 && !row_for_charpos_p (row, charpos)))
17752 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17753 {
17754 struct glyph *g;
17755
17756 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17757 || (!best_row && !row->continued_p))
17758 return row;
17759 /* In bidi-reordered rows, there could be several rows whose
17760 edges surround CHARPOS, all of these rows belonging to
17761 the same continued line. We need to find the row which
17762 fits CHARPOS the best. */
17763 for (g = row->glyphs[TEXT_AREA];
17764 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17765 g++)
17766 {
17767 if (!STRINGP (g->object))
17768 {
17769 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17770 {
17771 mindif = eabs (g->charpos - charpos);
17772 best_row = row;
17773 /* Exact match always wins. */
17774 if (mindif == 0)
17775 return best_row;
17776 }
17777 }
17778 }
17779 }
17780 else if (best_row && !row->continued_p)
17781 return best_row;
17782 ++row;
17783 }
17784 }
17785
17786
17787 /* Try to redisplay window W by reusing its existing display. W's
17788 current matrix must be up to date when this function is called,
17789 i.e., window_end_valid must be true.
17790
17791 Value is
17792
17793 >= 1 if successful, i.e. display has been updated
17794 specifically:
17795 1 means the changes were in front of a newline that precedes
17796 the window start, and the whole current matrix was reused
17797 2 means the changes were after the last position displayed
17798 in the window, and the whole current matrix was reused
17799 3 means portions of the current matrix were reused, while
17800 some of the screen lines were redrawn
17801 -1 if redisplay with same window start is known not to succeed
17802 0 if otherwise unsuccessful
17803
17804 The following steps are performed:
17805
17806 1. Find the last row in the current matrix of W that is not
17807 affected by changes at the start of current_buffer. If no such row
17808 is found, give up.
17809
17810 2. Find the first row in W's current matrix that is not affected by
17811 changes at the end of current_buffer. Maybe there is no such row.
17812
17813 3. Display lines beginning with the row + 1 found in step 1 to the
17814 row found in step 2 or, if step 2 didn't find a row, to the end of
17815 the window.
17816
17817 4. If cursor is not known to appear on the window, give up.
17818
17819 5. If display stopped at the row found in step 2, scroll the
17820 display and current matrix as needed.
17821
17822 6. Maybe display some lines at the end of W, if we must. This can
17823 happen under various circumstances, like a partially visible line
17824 becoming fully visible, or because newly displayed lines are displayed
17825 in smaller font sizes.
17826
17827 7. Update W's window end information. */
17828
17829 static int
17830 try_window_id (struct window *w)
17831 {
17832 struct frame *f = XFRAME (w->frame);
17833 struct glyph_matrix *current_matrix = w->current_matrix;
17834 struct glyph_matrix *desired_matrix = w->desired_matrix;
17835 struct glyph_row *last_unchanged_at_beg_row;
17836 struct glyph_row *first_unchanged_at_end_row;
17837 struct glyph_row *row;
17838 struct glyph_row *bottom_row;
17839 int bottom_vpos;
17840 struct it it;
17841 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17842 int dvpos, dy;
17843 struct text_pos start_pos;
17844 struct run run;
17845 int first_unchanged_at_end_vpos = 0;
17846 struct glyph_row *last_text_row, *last_text_row_at_end;
17847 struct text_pos start;
17848 ptrdiff_t first_changed_charpos, last_changed_charpos;
17849
17850 #ifdef GLYPH_DEBUG
17851 if (inhibit_try_window_id)
17852 return 0;
17853 #endif
17854
17855 /* This is handy for debugging. */
17856 #if false
17857 #define GIVE_UP(X) \
17858 do { \
17859 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
17860 return 0; \
17861 } while (false)
17862 #else
17863 #define GIVE_UP(X) return 0
17864 #endif
17865
17866 SET_TEXT_POS_FROM_MARKER (start, w->start);
17867
17868 /* Don't use this for mini-windows because these can show
17869 messages and mini-buffers, and we don't handle that here. */
17870 if (MINI_WINDOW_P (w))
17871 GIVE_UP (1);
17872
17873 /* This flag is used to prevent redisplay optimizations. */
17874 if (windows_or_buffers_changed || f->cursor_type_changed)
17875 GIVE_UP (2);
17876
17877 /* This function's optimizations cannot be used if overlays have
17878 changed in the buffer displayed by the window, so give up if they
17879 have. */
17880 if (w->last_overlay_modified != OVERLAY_MODIFF)
17881 GIVE_UP (200);
17882
17883 /* Verify that narrowing has not changed.
17884 Also verify that we were not told to prevent redisplay optimizations.
17885 It would be nice to further
17886 reduce the number of cases where this prevents try_window_id. */
17887 if (current_buffer->clip_changed
17888 || current_buffer->prevent_redisplay_optimizations_p)
17889 GIVE_UP (3);
17890
17891 /* Window must either use window-based redisplay or be full width. */
17892 if (!FRAME_WINDOW_P (f)
17893 && (!FRAME_LINE_INS_DEL_OK (f)
17894 || !WINDOW_FULL_WIDTH_P (w)))
17895 GIVE_UP (4);
17896
17897 /* Give up if point is known NOT to appear in W. */
17898 if (PT < CHARPOS (start))
17899 GIVE_UP (5);
17900
17901 /* Another way to prevent redisplay optimizations. */
17902 if (w->last_modified == 0)
17903 GIVE_UP (6);
17904
17905 /* Verify that window is not hscrolled. */
17906 if (w->hscroll != 0)
17907 GIVE_UP (7);
17908
17909 /* Verify that display wasn't paused. */
17910 if (!w->window_end_valid)
17911 GIVE_UP (8);
17912
17913 /* Likewise if highlighting trailing whitespace. */
17914 if (!NILP (Vshow_trailing_whitespace))
17915 GIVE_UP (11);
17916
17917 /* Can't use this if overlay arrow position and/or string have
17918 changed. */
17919 if (overlay_arrows_changed_p ())
17920 GIVE_UP (12);
17921
17922 /* When word-wrap is on, adding a space to the first word of a
17923 wrapped line can change the wrap position, altering the line
17924 above it. It might be worthwhile to handle this more
17925 intelligently, but for now just redisplay from scratch. */
17926 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17927 GIVE_UP (21);
17928
17929 /* Under bidi reordering, adding or deleting a character in the
17930 beginning of a paragraph, before the first strong directional
17931 character, can change the base direction of the paragraph (unless
17932 the buffer specifies a fixed paragraph direction), which will
17933 require to redisplay the whole paragraph. It might be worthwhile
17934 to find the paragraph limits and widen the range of redisplayed
17935 lines to that, but for now just give up this optimization and
17936 redisplay from scratch. */
17937 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17938 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17939 GIVE_UP (22);
17940
17941 /* Give up if the buffer has line-spacing set, as Lisp-level changes
17942 to that variable require thorough redisplay. */
17943 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
17944 GIVE_UP (23);
17945
17946 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17947 only if buffer has really changed. The reason is that the gap is
17948 initially at Z for freshly visited files. The code below would
17949 set end_unchanged to 0 in that case. */
17950 if (MODIFF > SAVE_MODIFF
17951 /* This seems to happen sometimes after saving a buffer. */
17952 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17953 {
17954 if (GPT - BEG < BEG_UNCHANGED)
17955 BEG_UNCHANGED = GPT - BEG;
17956 if (Z - GPT < END_UNCHANGED)
17957 END_UNCHANGED = Z - GPT;
17958 }
17959
17960 /* The position of the first and last character that has been changed. */
17961 first_changed_charpos = BEG + BEG_UNCHANGED;
17962 last_changed_charpos = Z - END_UNCHANGED;
17963
17964 /* If window starts after a line end, and the last change is in
17965 front of that newline, then changes don't affect the display.
17966 This case happens with stealth-fontification. Note that although
17967 the display is unchanged, glyph positions in the matrix have to
17968 be adjusted, of course. */
17969 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17970 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17971 && ((last_changed_charpos < CHARPOS (start)
17972 && CHARPOS (start) == BEGV)
17973 || (last_changed_charpos < CHARPOS (start) - 1
17974 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17975 {
17976 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17977 struct glyph_row *r0;
17978
17979 /* Compute how many chars/bytes have been added to or removed
17980 from the buffer. */
17981 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17982 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17983 Z_delta = Z - Z_old;
17984 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17985
17986 /* Give up if PT is not in the window. Note that it already has
17987 been checked at the start of try_window_id that PT is not in
17988 front of the window start. */
17989 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17990 GIVE_UP (13);
17991
17992 /* If window start is unchanged, we can reuse the whole matrix
17993 as is, after adjusting glyph positions. No need to compute
17994 the window end again, since its offset from Z hasn't changed. */
17995 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17996 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17997 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17998 /* PT must not be in a partially visible line. */
17999 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18000 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18001 {
18002 /* Adjust positions in the glyph matrix. */
18003 if (Z_delta || Z_delta_bytes)
18004 {
18005 struct glyph_row *r1
18006 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18007 increment_matrix_positions (w->current_matrix,
18008 MATRIX_ROW_VPOS (r0, current_matrix),
18009 MATRIX_ROW_VPOS (r1, current_matrix),
18010 Z_delta, Z_delta_bytes);
18011 }
18012
18013 /* Set the cursor. */
18014 row = row_containing_pos (w, PT, r0, NULL, 0);
18015 if (row)
18016 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18017 return 1;
18018 }
18019 }
18020
18021 /* Handle the case that changes are all below what is displayed in
18022 the window, and that PT is in the window. This shortcut cannot
18023 be taken if ZV is visible in the window, and text has been added
18024 there that is visible in the window. */
18025 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18026 /* ZV is not visible in the window, or there are no
18027 changes at ZV, actually. */
18028 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18029 || first_changed_charpos == last_changed_charpos))
18030 {
18031 struct glyph_row *r0;
18032
18033 /* Give up if PT is not in the window. Note that it already has
18034 been checked at the start of try_window_id that PT is not in
18035 front of the window start. */
18036 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18037 GIVE_UP (14);
18038
18039 /* If window start is unchanged, we can reuse the whole matrix
18040 as is, without changing glyph positions since no text has
18041 been added/removed in front of the window end. */
18042 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18043 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18044 /* PT must not be in a partially visible line. */
18045 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18046 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18047 {
18048 /* We have to compute the window end anew since text
18049 could have been added/removed after it. */
18050 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18051 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18052
18053 /* Set the cursor. */
18054 row = row_containing_pos (w, PT, r0, NULL, 0);
18055 if (row)
18056 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18057 return 2;
18058 }
18059 }
18060
18061 /* Give up if window start is in the changed area.
18062
18063 The condition used to read
18064
18065 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18066
18067 but why that was tested escapes me at the moment. */
18068 if (CHARPOS (start) >= first_changed_charpos
18069 && CHARPOS (start) <= last_changed_charpos)
18070 GIVE_UP (15);
18071
18072 /* Check that window start agrees with the start of the first glyph
18073 row in its current matrix. Check this after we know the window
18074 start is not in changed text, otherwise positions would not be
18075 comparable. */
18076 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18077 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18078 GIVE_UP (16);
18079
18080 /* Give up if the window ends in strings. Overlay strings
18081 at the end are difficult to handle, so don't try. */
18082 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18083 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18084 GIVE_UP (20);
18085
18086 /* Compute the position at which we have to start displaying new
18087 lines. Some of the lines at the top of the window might be
18088 reusable because they are not displaying changed text. Find the
18089 last row in W's current matrix not affected by changes at the
18090 start of current_buffer. Value is null if changes start in the
18091 first line of window. */
18092 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18093 if (last_unchanged_at_beg_row)
18094 {
18095 /* Avoid starting to display in the middle of a character, a TAB
18096 for instance. This is easier than to set up the iterator
18097 exactly, and it's not a frequent case, so the additional
18098 effort wouldn't really pay off. */
18099 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18100 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18101 && last_unchanged_at_beg_row > w->current_matrix->rows)
18102 --last_unchanged_at_beg_row;
18103
18104 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18105 GIVE_UP (17);
18106
18107 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18108 GIVE_UP (18);
18109 start_pos = it.current.pos;
18110
18111 /* Start displaying new lines in the desired matrix at the same
18112 vpos we would use in the current matrix, i.e. below
18113 last_unchanged_at_beg_row. */
18114 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18115 current_matrix);
18116 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18117 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18118
18119 eassert (it.hpos == 0 && it.current_x == 0);
18120 }
18121 else
18122 {
18123 /* There are no reusable lines at the start of the window.
18124 Start displaying in the first text line. */
18125 start_display (&it, w, start);
18126 it.vpos = it.first_vpos;
18127 start_pos = it.current.pos;
18128 }
18129
18130 /* Find the first row that is not affected by changes at the end of
18131 the buffer. Value will be null if there is no unchanged row, in
18132 which case we must redisplay to the end of the window. delta
18133 will be set to the value by which buffer positions beginning with
18134 first_unchanged_at_end_row have to be adjusted due to text
18135 changes. */
18136 first_unchanged_at_end_row
18137 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18138 IF_DEBUG (debug_delta = delta);
18139 IF_DEBUG (debug_delta_bytes = delta_bytes);
18140
18141 /* Set stop_pos to the buffer position up to which we will have to
18142 display new lines. If first_unchanged_at_end_row != NULL, this
18143 is the buffer position of the start of the line displayed in that
18144 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18145 that we don't stop at a buffer position. */
18146 stop_pos = 0;
18147 if (first_unchanged_at_end_row)
18148 {
18149 eassert (last_unchanged_at_beg_row == NULL
18150 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18151
18152 /* If this is a continuation line, move forward to the next one
18153 that isn't. Changes in lines above affect this line.
18154 Caution: this may move first_unchanged_at_end_row to a row
18155 not displaying text. */
18156 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18157 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18158 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18159 < it.last_visible_y))
18160 ++first_unchanged_at_end_row;
18161
18162 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18163 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18164 >= it.last_visible_y))
18165 first_unchanged_at_end_row = NULL;
18166 else
18167 {
18168 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18169 + delta);
18170 first_unchanged_at_end_vpos
18171 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18172 eassert (stop_pos >= Z - END_UNCHANGED);
18173 }
18174 }
18175 else if (last_unchanged_at_beg_row == NULL)
18176 GIVE_UP (19);
18177
18178
18179 #ifdef GLYPH_DEBUG
18180
18181 /* Either there is no unchanged row at the end, or the one we have
18182 now displays text. This is a necessary condition for the window
18183 end pos calculation at the end of this function. */
18184 eassert (first_unchanged_at_end_row == NULL
18185 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18186
18187 debug_last_unchanged_at_beg_vpos
18188 = (last_unchanged_at_beg_row
18189 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18190 : -1);
18191 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18192
18193 #endif /* GLYPH_DEBUG */
18194
18195
18196 /* Display new lines. Set last_text_row to the last new line
18197 displayed which has text on it, i.e. might end up as being the
18198 line where the window_end_vpos is. */
18199 w->cursor.vpos = -1;
18200 last_text_row = NULL;
18201 overlay_arrow_seen = false;
18202 if (it.current_y < it.last_visible_y
18203 && !f->fonts_changed
18204 && (first_unchanged_at_end_row == NULL
18205 || IT_CHARPOS (it) < stop_pos))
18206 it.glyph_row->reversed_p = false;
18207 while (it.current_y < it.last_visible_y
18208 && !f->fonts_changed
18209 && (first_unchanged_at_end_row == NULL
18210 || IT_CHARPOS (it) < stop_pos))
18211 {
18212 if (display_line (&it))
18213 last_text_row = it.glyph_row - 1;
18214 }
18215
18216 if (f->fonts_changed)
18217 return -1;
18218
18219 /* The redisplay iterations in display_line above could have
18220 triggered font-lock, which could have done something that
18221 invalidates IT->w window's end-point information, on which we
18222 rely below. E.g., one package, which will remain unnamed, used
18223 to install a font-lock-fontify-region-function that called
18224 bury-buffer, whose side effect is to switch the buffer displayed
18225 by IT->w, and that predictably resets IT->w's window_end_valid
18226 flag, which we already tested at the entry to this function.
18227 Amply punish such packages/modes by giving up on this
18228 optimization in those cases. */
18229 if (!w->window_end_valid)
18230 {
18231 clear_glyph_matrix (w->desired_matrix);
18232 return -1;
18233 }
18234
18235 /* Compute differences in buffer positions, y-positions etc. for
18236 lines reused at the bottom of the window. Compute what we can
18237 scroll. */
18238 if (first_unchanged_at_end_row
18239 /* No lines reused because we displayed everything up to the
18240 bottom of the window. */
18241 && it.current_y < it.last_visible_y)
18242 {
18243 dvpos = (it.vpos
18244 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18245 current_matrix));
18246 dy = it.current_y - first_unchanged_at_end_row->y;
18247 run.current_y = first_unchanged_at_end_row->y;
18248 run.desired_y = run.current_y + dy;
18249 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18250 }
18251 else
18252 {
18253 delta = delta_bytes = dvpos = dy
18254 = run.current_y = run.desired_y = run.height = 0;
18255 first_unchanged_at_end_row = NULL;
18256 }
18257 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18258
18259
18260 /* Find the cursor if not already found. We have to decide whether
18261 PT will appear on this window (it sometimes doesn't, but this is
18262 not a very frequent case.) This decision has to be made before
18263 the current matrix is altered. A value of cursor.vpos < 0 means
18264 that PT is either in one of the lines beginning at
18265 first_unchanged_at_end_row or below the window. Don't care for
18266 lines that might be displayed later at the window end; as
18267 mentioned, this is not a frequent case. */
18268 if (w->cursor.vpos < 0)
18269 {
18270 /* Cursor in unchanged rows at the top? */
18271 if (PT < CHARPOS (start_pos)
18272 && last_unchanged_at_beg_row)
18273 {
18274 row = row_containing_pos (w, PT,
18275 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18276 last_unchanged_at_beg_row + 1, 0);
18277 if (row)
18278 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18279 }
18280
18281 /* Start from first_unchanged_at_end_row looking for PT. */
18282 else if (first_unchanged_at_end_row)
18283 {
18284 row = row_containing_pos (w, PT - delta,
18285 first_unchanged_at_end_row, NULL, 0);
18286 if (row)
18287 set_cursor_from_row (w, row, w->current_matrix, delta,
18288 delta_bytes, dy, dvpos);
18289 }
18290
18291 /* Give up if cursor was not found. */
18292 if (w->cursor.vpos < 0)
18293 {
18294 clear_glyph_matrix (w->desired_matrix);
18295 return -1;
18296 }
18297 }
18298
18299 /* Don't let the cursor end in the scroll margins. */
18300 {
18301 int this_scroll_margin, cursor_height;
18302 int frame_line_height = default_line_pixel_height (w);
18303 int window_total_lines
18304 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18305
18306 this_scroll_margin =
18307 max (0, min (scroll_margin, window_total_lines / 4));
18308 this_scroll_margin *= frame_line_height;
18309 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18310
18311 if ((w->cursor.y < this_scroll_margin
18312 && CHARPOS (start) > BEGV)
18313 /* Old redisplay didn't take scroll margin into account at the bottom,
18314 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18315 || (w->cursor.y + (make_cursor_line_fully_visible_p
18316 ? cursor_height + this_scroll_margin
18317 : 1)) > it.last_visible_y)
18318 {
18319 w->cursor.vpos = -1;
18320 clear_glyph_matrix (w->desired_matrix);
18321 return -1;
18322 }
18323 }
18324
18325 /* Scroll the display. Do it before changing the current matrix so
18326 that xterm.c doesn't get confused about where the cursor glyph is
18327 found. */
18328 if (dy && run.height)
18329 {
18330 update_begin (f);
18331
18332 if (FRAME_WINDOW_P (f))
18333 {
18334 FRAME_RIF (f)->update_window_begin_hook (w);
18335 FRAME_RIF (f)->clear_window_mouse_face (w);
18336 FRAME_RIF (f)->scroll_run_hook (w, &run);
18337 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18338 }
18339 else
18340 {
18341 /* Terminal frame. In this case, dvpos gives the number of
18342 lines to scroll by; dvpos < 0 means scroll up. */
18343 int from_vpos
18344 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18345 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18346 int end = (WINDOW_TOP_EDGE_LINE (w)
18347 + WINDOW_WANTS_HEADER_LINE_P (w)
18348 + window_internal_height (w));
18349
18350 #if defined (HAVE_GPM) || defined (MSDOS)
18351 x_clear_window_mouse_face (w);
18352 #endif
18353 /* Perform the operation on the screen. */
18354 if (dvpos > 0)
18355 {
18356 /* Scroll last_unchanged_at_beg_row to the end of the
18357 window down dvpos lines. */
18358 set_terminal_window (f, end);
18359
18360 /* On dumb terminals delete dvpos lines at the end
18361 before inserting dvpos empty lines. */
18362 if (!FRAME_SCROLL_REGION_OK (f))
18363 ins_del_lines (f, end - dvpos, -dvpos);
18364
18365 /* Insert dvpos empty lines in front of
18366 last_unchanged_at_beg_row. */
18367 ins_del_lines (f, from, dvpos);
18368 }
18369 else if (dvpos < 0)
18370 {
18371 /* Scroll up last_unchanged_at_beg_vpos to the end of
18372 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18373 set_terminal_window (f, end);
18374
18375 /* Delete dvpos lines in front of
18376 last_unchanged_at_beg_vpos. ins_del_lines will set
18377 the cursor to the given vpos and emit |dvpos| delete
18378 line sequences. */
18379 ins_del_lines (f, from + dvpos, dvpos);
18380
18381 /* On a dumb terminal insert dvpos empty lines at the
18382 end. */
18383 if (!FRAME_SCROLL_REGION_OK (f))
18384 ins_del_lines (f, end + dvpos, -dvpos);
18385 }
18386
18387 set_terminal_window (f, 0);
18388 }
18389
18390 update_end (f);
18391 }
18392
18393 /* Shift reused rows of the current matrix to the right position.
18394 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18395 text. */
18396 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18397 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18398 if (dvpos < 0)
18399 {
18400 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18401 bottom_vpos, dvpos);
18402 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18403 bottom_vpos);
18404 }
18405 else if (dvpos > 0)
18406 {
18407 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18408 bottom_vpos, dvpos);
18409 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18410 first_unchanged_at_end_vpos + dvpos);
18411 }
18412
18413 /* For frame-based redisplay, make sure that current frame and window
18414 matrix are in sync with respect to glyph memory. */
18415 if (!FRAME_WINDOW_P (f))
18416 sync_frame_with_window_matrix_rows (w);
18417
18418 /* Adjust buffer positions in reused rows. */
18419 if (delta || delta_bytes)
18420 increment_matrix_positions (current_matrix,
18421 first_unchanged_at_end_vpos + dvpos,
18422 bottom_vpos, delta, delta_bytes);
18423
18424 /* Adjust Y positions. */
18425 if (dy)
18426 shift_glyph_matrix (w, current_matrix,
18427 first_unchanged_at_end_vpos + dvpos,
18428 bottom_vpos, dy);
18429
18430 if (first_unchanged_at_end_row)
18431 {
18432 first_unchanged_at_end_row += dvpos;
18433 if (first_unchanged_at_end_row->y >= it.last_visible_y
18434 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18435 first_unchanged_at_end_row = NULL;
18436 }
18437
18438 /* If scrolling up, there may be some lines to display at the end of
18439 the window. */
18440 last_text_row_at_end = NULL;
18441 if (dy < 0)
18442 {
18443 /* Scrolling up can leave for example a partially visible line
18444 at the end of the window to be redisplayed. */
18445 /* Set last_row to the glyph row in the current matrix where the
18446 window end line is found. It has been moved up or down in
18447 the matrix by dvpos. */
18448 int last_vpos = w->window_end_vpos + dvpos;
18449 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18450
18451 /* If last_row is the window end line, it should display text. */
18452 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18453
18454 /* If window end line was partially visible before, begin
18455 displaying at that line. Otherwise begin displaying with the
18456 line following it. */
18457 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18458 {
18459 init_to_row_start (&it, w, last_row);
18460 it.vpos = last_vpos;
18461 it.current_y = last_row->y;
18462 }
18463 else
18464 {
18465 init_to_row_end (&it, w, last_row);
18466 it.vpos = 1 + last_vpos;
18467 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18468 ++last_row;
18469 }
18470
18471 /* We may start in a continuation line. If so, we have to
18472 get the right continuation_lines_width and current_x. */
18473 it.continuation_lines_width = last_row->continuation_lines_width;
18474 it.hpos = it.current_x = 0;
18475
18476 /* Display the rest of the lines at the window end. */
18477 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18478 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18479 {
18480 /* Is it always sure that the display agrees with lines in
18481 the current matrix? I don't think so, so we mark rows
18482 displayed invalid in the current matrix by setting their
18483 enabled_p flag to false. */
18484 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18485 if (display_line (&it))
18486 last_text_row_at_end = it.glyph_row - 1;
18487 }
18488 }
18489
18490 /* Update window_end_pos and window_end_vpos. */
18491 if (first_unchanged_at_end_row && !last_text_row_at_end)
18492 {
18493 /* Window end line if one of the preserved rows from the current
18494 matrix. Set row to the last row displaying text in current
18495 matrix starting at first_unchanged_at_end_row, after
18496 scrolling. */
18497 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18498 row = find_last_row_displaying_text (w->current_matrix, &it,
18499 first_unchanged_at_end_row);
18500 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18501 adjust_window_ends (w, row, true);
18502 eassert (w->window_end_bytepos >= 0);
18503 IF_DEBUG (debug_method_add (w, "A"));
18504 }
18505 else if (last_text_row_at_end)
18506 {
18507 adjust_window_ends (w, last_text_row_at_end, false);
18508 eassert (w->window_end_bytepos >= 0);
18509 IF_DEBUG (debug_method_add (w, "B"));
18510 }
18511 else if (last_text_row)
18512 {
18513 /* We have displayed either to the end of the window or at the
18514 end of the window, i.e. the last row with text is to be found
18515 in the desired matrix. */
18516 adjust_window_ends (w, last_text_row, false);
18517 eassert (w->window_end_bytepos >= 0);
18518 }
18519 else if (first_unchanged_at_end_row == NULL
18520 && last_text_row == NULL
18521 && last_text_row_at_end == NULL)
18522 {
18523 /* Displayed to end of window, but no line containing text was
18524 displayed. Lines were deleted at the end of the window. */
18525 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18526 int vpos = w->window_end_vpos;
18527 struct glyph_row *current_row = current_matrix->rows + vpos;
18528 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18529
18530 for (row = NULL;
18531 row == NULL && vpos >= first_vpos;
18532 --vpos, --current_row, --desired_row)
18533 {
18534 if (desired_row->enabled_p)
18535 {
18536 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18537 row = desired_row;
18538 }
18539 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18540 row = current_row;
18541 }
18542
18543 eassert (row != NULL);
18544 w->window_end_vpos = vpos + 1;
18545 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18546 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18547 eassert (w->window_end_bytepos >= 0);
18548 IF_DEBUG (debug_method_add (w, "C"));
18549 }
18550 else
18551 emacs_abort ();
18552
18553 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18554 debug_end_vpos = w->window_end_vpos));
18555
18556 /* Record that display has not been completed. */
18557 w->window_end_valid = false;
18558 w->desired_matrix->no_scrolling_p = true;
18559 return 3;
18560
18561 #undef GIVE_UP
18562 }
18563
18564
18565 \f
18566 /***********************************************************************
18567 More debugging support
18568 ***********************************************************************/
18569
18570 #ifdef GLYPH_DEBUG
18571
18572 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18573 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18574 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18575
18576
18577 /* Dump the contents of glyph matrix MATRIX on stderr.
18578
18579 GLYPHS 0 means don't show glyph contents.
18580 GLYPHS 1 means show glyphs in short form
18581 GLYPHS > 1 means show glyphs in long form. */
18582
18583 void
18584 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18585 {
18586 int i;
18587 for (i = 0; i < matrix->nrows; ++i)
18588 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18589 }
18590
18591
18592 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18593 the glyph row and area where the glyph comes from. */
18594
18595 void
18596 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18597 {
18598 if (glyph->type == CHAR_GLYPH
18599 || glyph->type == GLYPHLESS_GLYPH)
18600 {
18601 fprintf (stderr,
18602 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18603 glyph - row->glyphs[TEXT_AREA],
18604 (glyph->type == CHAR_GLYPH
18605 ? 'C'
18606 : 'G'),
18607 glyph->charpos,
18608 (BUFFERP (glyph->object)
18609 ? 'B'
18610 : (STRINGP (glyph->object)
18611 ? 'S'
18612 : (NILP (glyph->object)
18613 ? '0'
18614 : '-'))),
18615 glyph->pixel_width,
18616 glyph->u.ch,
18617 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18618 ? glyph->u.ch
18619 : '.'),
18620 glyph->face_id,
18621 glyph->left_box_line_p,
18622 glyph->right_box_line_p);
18623 }
18624 else if (glyph->type == STRETCH_GLYPH)
18625 {
18626 fprintf (stderr,
18627 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18628 glyph - row->glyphs[TEXT_AREA],
18629 'S',
18630 glyph->charpos,
18631 (BUFFERP (glyph->object)
18632 ? 'B'
18633 : (STRINGP (glyph->object)
18634 ? 'S'
18635 : (NILP (glyph->object)
18636 ? '0'
18637 : '-'))),
18638 glyph->pixel_width,
18639 0,
18640 ' ',
18641 glyph->face_id,
18642 glyph->left_box_line_p,
18643 glyph->right_box_line_p);
18644 }
18645 else if (glyph->type == IMAGE_GLYPH)
18646 {
18647 fprintf (stderr,
18648 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18649 glyph - row->glyphs[TEXT_AREA],
18650 'I',
18651 glyph->charpos,
18652 (BUFFERP (glyph->object)
18653 ? 'B'
18654 : (STRINGP (glyph->object)
18655 ? 'S'
18656 : (NILP (glyph->object)
18657 ? '0'
18658 : '-'))),
18659 glyph->pixel_width,
18660 glyph->u.img_id,
18661 '.',
18662 glyph->face_id,
18663 glyph->left_box_line_p,
18664 glyph->right_box_line_p);
18665 }
18666 else if (glyph->type == COMPOSITE_GLYPH)
18667 {
18668 fprintf (stderr,
18669 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18670 glyph - row->glyphs[TEXT_AREA],
18671 '+',
18672 glyph->charpos,
18673 (BUFFERP (glyph->object)
18674 ? 'B'
18675 : (STRINGP (glyph->object)
18676 ? 'S'
18677 : (NILP (glyph->object)
18678 ? '0'
18679 : '-'))),
18680 glyph->pixel_width,
18681 glyph->u.cmp.id);
18682 if (glyph->u.cmp.automatic)
18683 fprintf (stderr,
18684 "[%d-%d]",
18685 glyph->slice.cmp.from, glyph->slice.cmp.to);
18686 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18687 glyph->face_id,
18688 glyph->left_box_line_p,
18689 glyph->right_box_line_p);
18690 }
18691 }
18692
18693
18694 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18695 GLYPHS 0 means don't show glyph contents.
18696 GLYPHS 1 means show glyphs in short form
18697 GLYPHS > 1 means show glyphs in long form. */
18698
18699 void
18700 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18701 {
18702 if (glyphs != 1)
18703 {
18704 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18705 fprintf (stderr, "==============================================================================\n");
18706
18707 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18708 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18709 vpos,
18710 MATRIX_ROW_START_CHARPOS (row),
18711 MATRIX_ROW_END_CHARPOS (row),
18712 row->used[TEXT_AREA],
18713 row->contains_overlapping_glyphs_p,
18714 row->enabled_p,
18715 row->truncated_on_left_p,
18716 row->truncated_on_right_p,
18717 row->continued_p,
18718 MATRIX_ROW_CONTINUATION_LINE_P (row),
18719 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18720 row->ends_at_zv_p,
18721 row->fill_line_p,
18722 row->ends_in_middle_of_char_p,
18723 row->starts_in_middle_of_char_p,
18724 row->mouse_face_p,
18725 row->x,
18726 row->y,
18727 row->pixel_width,
18728 row->height,
18729 row->visible_height,
18730 row->ascent,
18731 row->phys_ascent);
18732 /* The next 3 lines should align to "Start" in the header. */
18733 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18734 row->end.overlay_string_index,
18735 row->continuation_lines_width);
18736 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18737 CHARPOS (row->start.string_pos),
18738 CHARPOS (row->end.string_pos));
18739 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18740 row->end.dpvec_index);
18741 }
18742
18743 if (glyphs > 1)
18744 {
18745 int area;
18746
18747 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18748 {
18749 struct glyph *glyph = row->glyphs[area];
18750 struct glyph *glyph_end = glyph + row->used[area];
18751
18752 /* Glyph for a line end in text. */
18753 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18754 ++glyph_end;
18755
18756 if (glyph < glyph_end)
18757 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18758
18759 for (; glyph < glyph_end; ++glyph)
18760 dump_glyph (row, glyph, area);
18761 }
18762 }
18763 else if (glyphs == 1)
18764 {
18765 int area;
18766 char s[SHRT_MAX + 4];
18767
18768 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18769 {
18770 int i;
18771
18772 for (i = 0; i < row->used[area]; ++i)
18773 {
18774 struct glyph *glyph = row->glyphs[area] + i;
18775 if (i == row->used[area] - 1
18776 && area == TEXT_AREA
18777 && NILP (glyph->object)
18778 && glyph->type == CHAR_GLYPH
18779 && glyph->u.ch == ' ')
18780 {
18781 strcpy (&s[i], "[\\n]");
18782 i += 4;
18783 }
18784 else if (glyph->type == CHAR_GLYPH
18785 && glyph->u.ch < 0x80
18786 && glyph->u.ch >= ' ')
18787 s[i] = glyph->u.ch;
18788 else
18789 s[i] = '.';
18790 }
18791
18792 s[i] = '\0';
18793 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18794 }
18795 }
18796 }
18797
18798
18799 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18800 Sdump_glyph_matrix, 0, 1, "p",
18801 doc: /* Dump the current matrix of the selected window to stderr.
18802 Shows contents of glyph row structures. With non-nil
18803 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18804 glyphs in short form, otherwise show glyphs in long form.
18805
18806 Interactively, no argument means show glyphs in short form;
18807 with numeric argument, its value is passed as the GLYPHS flag. */)
18808 (Lisp_Object glyphs)
18809 {
18810 struct window *w = XWINDOW (selected_window);
18811 struct buffer *buffer = XBUFFER (w->contents);
18812
18813 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18814 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18815 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18816 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18817 fprintf (stderr, "=============================================\n");
18818 dump_glyph_matrix (w->current_matrix,
18819 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18820 return Qnil;
18821 }
18822
18823
18824 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18825 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18826 Only text-mode frames have frame glyph matrices. */)
18827 (void)
18828 {
18829 struct frame *f = XFRAME (selected_frame);
18830
18831 if (f->current_matrix)
18832 dump_glyph_matrix (f->current_matrix, 1);
18833 else
18834 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
18835 return Qnil;
18836 }
18837
18838
18839 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18840 doc: /* Dump glyph row ROW to stderr.
18841 GLYPH 0 means don't dump glyphs.
18842 GLYPH 1 means dump glyphs in short form.
18843 GLYPH > 1 or omitted means dump glyphs in long form. */)
18844 (Lisp_Object row, Lisp_Object glyphs)
18845 {
18846 struct glyph_matrix *matrix;
18847 EMACS_INT vpos;
18848
18849 CHECK_NUMBER (row);
18850 matrix = XWINDOW (selected_window)->current_matrix;
18851 vpos = XINT (row);
18852 if (vpos >= 0 && vpos < matrix->nrows)
18853 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18854 vpos,
18855 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18856 return Qnil;
18857 }
18858
18859
18860 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18861 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18862 GLYPH 0 means don't dump glyphs.
18863 GLYPH 1 means dump glyphs in short form.
18864 GLYPH > 1 or omitted means dump glyphs in long form.
18865
18866 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18867 do nothing. */)
18868 (Lisp_Object row, Lisp_Object glyphs)
18869 {
18870 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18871 struct frame *sf = SELECTED_FRAME ();
18872 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18873 EMACS_INT vpos;
18874
18875 CHECK_NUMBER (row);
18876 vpos = XINT (row);
18877 if (vpos >= 0 && vpos < m->nrows)
18878 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18879 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18880 #endif
18881 return Qnil;
18882 }
18883
18884
18885 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18886 doc: /* Toggle tracing of redisplay.
18887 With ARG, turn tracing on if and only if ARG is positive. */)
18888 (Lisp_Object arg)
18889 {
18890 if (NILP (arg))
18891 trace_redisplay_p = !trace_redisplay_p;
18892 else
18893 {
18894 arg = Fprefix_numeric_value (arg);
18895 trace_redisplay_p = XINT (arg) > 0;
18896 }
18897
18898 return Qnil;
18899 }
18900
18901
18902 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18903 doc: /* Like `format', but print result to stderr.
18904 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18905 (ptrdiff_t nargs, Lisp_Object *args)
18906 {
18907 Lisp_Object s = Fformat (nargs, args);
18908 fwrite (SDATA (s), 1, SBYTES (s), stderr);
18909 return Qnil;
18910 }
18911
18912 #endif /* GLYPH_DEBUG */
18913
18914
18915 \f
18916 /***********************************************************************
18917 Building Desired Matrix Rows
18918 ***********************************************************************/
18919
18920 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18921 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18922
18923 static struct glyph_row *
18924 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18925 {
18926 struct frame *f = XFRAME (WINDOW_FRAME (w));
18927 struct buffer *buffer = XBUFFER (w->contents);
18928 struct buffer *old = current_buffer;
18929 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18930 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
18931 const unsigned char *arrow_end = arrow_string + arrow_len;
18932 const unsigned char *p;
18933 struct it it;
18934 bool multibyte_p;
18935 int n_glyphs_before;
18936
18937 set_buffer_temp (buffer);
18938 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18939 scratch_glyph_row.reversed_p = false;
18940 it.glyph_row->used[TEXT_AREA] = 0;
18941 SET_TEXT_POS (it.position, 0, 0);
18942
18943 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18944 p = arrow_string;
18945 while (p < arrow_end)
18946 {
18947 Lisp_Object face, ilisp;
18948
18949 /* Get the next character. */
18950 if (multibyte_p)
18951 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18952 else
18953 {
18954 it.c = it.char_to_display = *p, it.len = 1;
18955 if (! ASCII_CHAR_P (it.c))
18956 it.char_to_display = BYTE8_TO_CHAR (it.c);
18957 }
18958 p += it.len;
18959
18960 /* Get its face. */
18961 ilisp = make_number (p - arrow_string);
18962 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18963 it.face_id = compute_char_face (f, it.char_to_display, face);
18964
18965 /* Compute its width, get its glyphs. */
18966 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18967 SET_TEXT_POS (it.position, -1, -1);
18968 PRODUCE_GLYPHS (&it);
18969
18970 /* If this character doesn't fit any more in the line, we have
18971 to remove some glyphs. */
18972 if (it.current_x > it.last_visible_x)
18973 {
18974 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18975 break;
18976 }
18977 }
18978
18979 set_buffer_temp (old);
18980 return it.glyph_row;
18981 }
18982
18983
18984 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18985 glyphs to insert is determined by produce_special_glyphs. */
18986
18987 static void
18988 insert_left_trunc_glyphs (struct it *it)
18989 {
18990 struct it truncate_it;
18991 struct glyph *from, *end, *to, *toend;
18992
18993 eassert (!FRAME_WINDOW_P (it->f)
18994 || (!it->glyph_row->reversed_p
18995 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18996 || (it->glyph_row->reversed_p
18997 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18998
18999 /* Get the truncation glyphs. */
19000 truncate_it = *it;
19001 truncate_it.current_x = 0;
19002 truncate_it.face_id = DEFAULT_FACE_ID;
19003 truncate_it.glyph_row = &scratch_glyph_row;
19004 truncate_it.area = TEXT_AREA;
19005 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19006 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19007 truncate_it.object = Qnil;
19008 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19009
19010 /* Overwrite glyphs from IT with truncation glyphs. */
19011 if (!it->glyph_row->reversed_p)
19012 {
19013 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19014
19015 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19016 end = from + tused;
19017 to = it->glyph_row->glyphs[TEXT_AREA];
19018 toend = to + it->glyph_row->used[TEXT_AREA];
19019 if (FRAME_WINDOW_P (it->f))
19020 {
19021 /* On GUI frames, when variable-size fonts are displayed,
19022 the truncation glyphs may need more pixels than the row's
19023 glyphs they overwrite. We overwrite more glyphs to free
19024 enough screen real estate, and enlarge the stretch glyph
19025 on the right (see display_line), if there is one, to
19026 preserve the screen position of the truncation glyphs on
19027 the right. */
19028 int w = 0;
19029 struct glyph *g = to;
19030 short used;
19031
19032 /* The first glyph could be partially visible, in which case
19033 it->glyph_row->x will be negative. But we want the left
19034 truncation glyphs to be aligned at the left margin of the
19035 window, so we override the x coordinate at which the row
19036 will begin. */
19037 it->glyph_row->x = 0;
19038 while (g < toend && w < it->truncation_pixel_width)
19039 {
19040 w += g->pixel_width;
19041 ++g;
19042 }
19043 if (g - to - tused > 0)
19044 {
19045 memmove (to + tused, g, (toend - g) * sizeof(*g));
19046 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19047 }
19048 used = it->glyph_row->used[TEXT_AREA];
19049 if (it->glyph_row->truncated_on_right_p
19050 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19051 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19052 == STRETCH_GLYPH)
19053 {
19054 int extra = w - it->truncation_pixel_width;
19055
19056 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19057 }
19058 }
19059
19060 while (from < end)
19061 *to++ = *from++;
19062
19063 /* There may be padding glyphs left over. Overwrite them too. */
19064 if (!FRAME_WINDOW_P (it->f))
19065 {
19066 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19067 {
19068 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19069 while (from < end)
19070 *to++ = *from++;
19071 }
19072 }
19073
19074 if (to > toend)
19075 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19076 }
19077 else
19078 {
19079 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19080
19081 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19082 that back to front. */
19083 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19084 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19085 toend = it->glyph_row->glyphs[TEXT_AREA];
19086 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19087 if (FRAME_WINDOW_P (it->f))
19088 {
19089 int w = 0;
19090 struct glyph *g = to;
19091
19092 while (g >= toend && w < it->truncation_pixel_width)
19093 {
19094 w += g->pixel_width;
19095 --g;
19096 }
19097 if (to - g - tused > 0)
19098 to = g + tused;
19099 if (it->glyph_row->truncated_on_right_p
19100 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19101 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19102 {
19103 int extra = w - it->truncation_pixel_width;
19104
19105 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19106 }
19107 }
19108
19109 while (from >= end && to >= toend)
19110 *to-- = *from--;
19111 if (!FRAME_WINDOW_P (it->f))
19112 {
19113 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19114 {
19115 from =
19116 truncate_it.glyph_row->glyphs[TEXT_AREA]
19117 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19118 while (from >= end && to >= toend)
19119 *to-- = *from--;
19120 }
19121 }
19122 if (from >= end)
19123 {
19124 /* Need to free some room before prepending additional
19125 glyphs. */
19126 int move_by = from - end + 1;
19127 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19128 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19129
19130 for ( ; g >= g0; g--)
19131 g[move_by] = *g;
19132 while (from >= end)
19133 *to-- = *from--;
19134 it->glyph_row->used[TEXT_AREA] += move_by;
19135 }
19136 }
19137 }
19138
19139 /* Compute the hash code for ROW. */
19140 unsigned
19141 row_hash (struct glyph_row *row)
19142 {
19143 int area, k;
19144 unsigned hashval = 0;
19145
19146 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19147 for (k = 0; k < row->used[area]; ++k)
19148 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19149 + row->glyphs[area][k].u.val
19150 + row->glyphs[area][k].face_id
19151 + row->glyphs[area][k].padding_p
19152 + (row->glyphs[area][k].type << 2));
19153
19154 return hashval;
19155 }
19156
19157 /* Compute the pixel height and width of IT->glyph_row.
19158
19159 Most of the time, ascent and height of a display line will be equal
19160 to the max_ascent and max_height values of the display iterator
19161 structure. This is not the case if
19162
19163 1. We hit ZV without displaying anything. In this case, max_ascent
19164 and max_height will be zero.
19165
19166 2. We have some glyphs that don't contribute to the line height.
19167 (The glyph row flag contributes_to_line_height_p is for future
19168 pixmap extensions).
19169
19170 The first case is easily covered by using default values because in
19171 these cases, the line height does not really matter, except that it
19172 must not be zero. */
19173
19174 static void
19175 compute_line_metrics (struct it *it)
19176 {
19177 struct glyph_row *row = it->glyph_row;
19178
19179 if (FRAME_WINDOW_P (it->f))
19180 {
19181 int i, min_y, max_y;
19182
19183 /* The line may consist of one space only, that was added to
19184 place the cursor on it. If so, the row's height hasn't been
19185 computed yet. */
19186 if (row->height == 0)
19187 {
19188 if (it->max_ascent + it->max_descent == 0)
19189 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19190 row->ascent = it->max_ascent;
19191 row->height = it->max_ascent + it->max_descent;
19192 row->phys_ascent = it->max_phys_ascent;
19193 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19194 row->extra_line_spacing = it->max_extra_line_spacing;
19195 }
19196
19197 /* Compute the width of this line. */
19198 row->pixel_width = row->x;
19199 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19200 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19201
19202 eassert (row->pixel_width >= 0);
19203 eassert (row->ascent >= 0 && row->height > 0);
19204
19205 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19206 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19207
19208 /* If first line's physical ascent is larger than its logical
19209 ascent, use the physical ascent, and make the row taller.
19210 This makes accented characters fully visible. */
19211 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19212 && row->phys_ascent > row->ascent)
19213 {
19214 row->height += row->phys_ascent - row->ascent;
19215 row->ascent = row->phys_ascent;
19216 }
19217
19218 /* Compute how much of the line is visible. */
19219 row->visible_height = row->height;
19220
19221 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19222 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19223
19224 if (row->y < min_y)
19225 row->visible_height -= min_y - row->y;
19226 if (row->y + row->height > max_y)
19227 row->visible_height -= row->y + row->height - max_y;
19228 }
19229 else
19230 {
19231 row->pixel_width = row->used[TEXT_AREA];
19232 if (row->continued_p)
19233 row->pixel_width -= it->continuation_pixel_width;
19234 else if (row->truncated_on_right_p)
19235 row->pixel_width -= it->truncation_pixel_width;
19236 row->ascent = row->phys_ascent = 0;
19237 row->height = row->phys_height = row->visible_height = 1;
19238 row->extra_line_spacing = 0;
19239 }
19240
19241 /* Compute a hash code for this row. */
19242 row->hash = row_hash (row);
19243
19244 it->max_ascent = it->max_descent = 0;
19245 it->max_phys_ascent = it->max_phys_descent = 0;
19246 }
19247
19248
19249 /* Append one space to the glyph row of iterator IT if doing a
19250 window-based redisplay. The space has the same face as
19251 IT->face_id. Value is true if a space was added.
19252
19253 This function is called to make sure that there is always one glyph
19254 at the end of a glyph row that the cursor can be set on under
19255 window-systems. (If there weren't such a glyph we would not know
19256 how wide and tall a box cursor should be displayed).
19257
19258 At the same time this space let's a nicely handle clearing to the
19259 end of the line if the row ends in italic text. */
19260
19261 static bool
19262 append_space_for_newline (struct it *it, bool default_face_p)
19263 {
19264 if (FRAME_WINDOW_P (it->f))
19265 {
19266 int n = it->glyph_row->used[TEXT_AREA];
19267
19268 if (it->glyph_row->glyphs[TEXT_AREA] + n
19269 < it->glyph_row->glyphs[1 + TEXT_AREA])
19270 {
19271 /* Save some values that must not be changed.
19272 Must save IT->c and IT->len because otherwise
19273 ITERATOR_AT_END_P wouldn't work anymore after
19274 append_space_for_newline has been called. */
19275 enum display_element_type saved_what = it->what;
19276 int saved_c = it->c, saved_len = it->len;
19277 int saved_char_to_display = it->char_to_display;
19278 int saved_x = it->current_x;
19279 int saved_face_id = it->face_id;
19280 bool saved_box_end = it->end_of_box_run_p;
19281 struct text_pos saved_pos;
19282 Lisp_Object saved_object;
19283 struct face *face;
19284 struct glyph *g;
19285
19286 saved_object = it->object;
19287 saved_pos = it->position;
19288
19289 it->what = IT_CHARACTER;
19290 memset (&it->position, 0, sizeof it->position);
19291 it->object = Qnil;
19292 it->c = it->char_to_display = ' ';
19293 it->len = 1;
19294
19295 /* If the default face was remapped, be sure to use the
19296 remapped face for the appended newline. */
19297 if (default_face_p)
19298 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19299 else if (it->face_before_selective_p)
19300 it->face_id = it->saved_face_id;
19301 face = FACE_FROM_ID (it->f, it->face_id);
19302 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19303 /* In R2L rows, we will prepend a stretch glyph that will
19304 have the end_of_box_run_p flag set for it, so there's no
19305 need for the appended newline glyph to have that flag
19306 set. */
19307 if (it->glyph_row->reversed_p
19308 /* But if the appended newline glyph goes all the way to
19309 the end of the row, there will be no stretch glyph,
19310 so leave the box flag set. */
19311 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19312 it->end_of_box_run_p = false;
19313
19314 PRODUCE_GLYPHS (it);
19315
19316 #ifdef HAVE_WINDOW_SYSTEM
19317 /* Make sure this space glyph has the right ascent and
19318 descent values, or else cursor at end of line will look
19319 funny, and height of empty lines will be incorrect. */
19320 g = it->glyph_row->glyphs[TEXT_AREA] + n;
19321 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19322 if (n == 0)
19323 {
19324 Lisp_Object height, total_height;
19325 int extra_line_spacing = it->extra_line_spacing;
19326 int boff = font->baseline_offset;
19327
19328 if (font->vertical_centering)
19329 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19330
19331 it->object = saved_object; /* get_it_property needs this */
19332 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19333 /* Must do a subset of line height processing from
19334 x_produce_glyph for newline characters. */
19335 height = get_it_property (it, Qline_height);
19336 if (CONSP (height)
19337 && CONSP (XCDR (height))
19338 && NILP (XCDR (XCDR (height))))
19339 {
19340 total_height = XCAR (XCDR (height));
19341 height = XCAR (height);
19342 }
19343 else
19344 total_height = Qnil;
19345 height = calc_line_height_property (it, height, font, boff, true);
19346
19347 if (it->override_ascent >= 0)
19348 {
19349 it->ascent = it->override_ascent;
19350 it->descent = it->override_descent;
19351 boff = it->override_boff;
19352 }
19353 if (EQ (height, Qt))
19354 extra_line_spacing = 0;
19355 else
19356 {
19357 Lisp_Object spacing;
19358
19359 it->phys_ascent = it->ascent;
19360 it->phys_descent = it->descent;
19361 if (!NILP (height)
19362 && XINT (height) > it->ascent + it->descent)
19363 it->ascent = XINT (height) - it->descent;
19364
19365 if (!NILP (total_height))
19366 spacing = calc_line_height_property (it, total_height, font,
19367 boff, false);
19368 else
19369 {
19370 spacing = get_it_property (it, Qline_spacing);
19371 spacing = calc_line_height_property (it, spacing, font,
19372 boff, false);
19373 }
19374 if (INTEGERP (spacing))
19375 {
19376 extra_line_spacing = XINT (spacing);
19377 if (!NILP (total_height))
19378 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19379 }
19380 }
19381 if (extra_line_spacing > 0)
19382 {
19383 it->descent += extra_line_spacing;
19384 if (extra_line_spacing > it->max_extra_line_spacing)
19385 it->max_extra_line_spacing = extra_line_spacing;
19386 }
19387 it->max_ascent = it->ascent;
19388 it->max_descent = it->descent;
19389 /* Make sure compute_line_metrics recomputes the row height. */
19390 it->glyph_row->height = 0;
19391 }
19392
19393 g->ascent = it->max_ascent;
19394 g->descent = it->max_descent;
19395 #endif
19396
19397 it->override_ascent = -1;
19398 it->constrain_row_ascent_descent_p = false;
19399 it->current_x = saved_x;
19400 it->object = saved_object;
19401 it->position = saved_pos;
19402 it->what = saved_what;
19403 it->face_id = saved_face_id;
19404 it->len = saved_len;
19405 it->c = saved_c;
19406 it->char_to_display = saved_char_to_display;
19407 it->end_of_box_run_p = saved_box_end;
19408 return true;
19409 }
19410 }
19411
19412 return false;
19413 }
19414
19415
19416 /* Extend the face of the last glyph in the text area of IT->glyph_row
19417 to the end of the display line. Called from display_line. If the
19418 glyph row is empty, add a space glyph to it so that we know the
19419 face to draw. Set the glyph row flag fill_line_p. If the glyph
19420 row is R2L, prepend a stretch glyph to cover the empty space to the
19421 left of the leftmost glyph. */
19422
19423 static void
19424 extend_face_to_end_of_line (struct it *it)
19425 {
19426 struct face *face, *default_face;
19427 struct frame *f = it->f;
19428
19429 /* If line is already filled, do nothing. Non window-system frames
19430 get a grace of one more ``pixel'' because their characters are
19431 1-``pixel'' wide, so they hit the equality too early. This grace
19432 is needed only for R2L rows that are not continued, to produce
19433 one extra blank where we could display the cursor. */
19434 if ((it->current_x >= it->last_visible_x
19435 + (!FRAME_WINDOW_P (f)
19436 && it->glyph_row->reversed_p
19437 && !it->glyph_row->continued_p))
19438 /* If the window has display margins, we will need to extend
19439 their face even if the text area is filled. */
19440 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19441 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19442 return;
19443
19444 /* The default face, possibly remapped. */
19445 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19446
19447 /* Face extension extends the background and box of IT->face_id
19448 to the end of the line. If the background equals the background
19449 of the frame, we don't have to do anything. */
19450 if (it->face_before_selective_p)
19451 face = FACE_FROM_ID (f, it->saved_face_id);
19452 else
19453 face = FACE_FROM_ID (f, it->face_id);
19454
19455 if (FRAME_WINDOW_P (f)
19456 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19457 && face->box == FACE_NO_BOX
19458 && face->background == FRAME_BACKGROUND_PIXEL (f)
19459 #ifdef HAVE_WINDOW_SYSTEM
19460 && !face->stipple
19461 #endif
19462 && !it->glyph_row->reversed_p)
19463 return;
19464
19465 /* Set the glyph row flag indicating that the face of the last glyph
19466 in the text area has to be drawn to the end of the text area. */
19467 it->glyph_row->fill_line_p = true;
19468
19469 /* If current character of IT is not ASCII, make sure we have the
19470 ASCII face. This will be automatically undone the next time
19471 get_next_display_element returns a multibyte character. Note
19472 that the character will always be single byte in unibyte
19473 text. */
19474 if (!ASCII_CHAR_P (it->c))
19475 {
19476 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19477 }
19478
19479 if (FRAME_WINDOW_P (f))
19480 {
19481 /* If the row is empty, add a space with the current face of IT,
19482 so that we know which face to draw. */
19483 if (it->glyph_row->used[TEXT_AREA] == 0)
19484 {
19485 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19486 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19487 it->glyph_row->used[TEXT_AREA] = 1;
19488 }
19489 /* Mode line and the header line don't have margins, and
19490 likewise the frame's tool-bar window, if there is any. */
19491 if (!(it->glyph_row->mode_line_p
19492 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19493 || (WINDOWP (f->tool_bar_window)
19494 && it->w == XWINDOW (f->tool_bar_window))
19495 #endif
19496 ))
19497 {
19498 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19499 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19500 {
19501 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19502 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19503 default_face->id;
19504 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19505 }
19506 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19507 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19508 {
19509 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19510 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19511 default_face->id;
19512 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19513 }
19514 }
19515 #ifdef HAVE_WINDOW_SYSTEM
19516 if (it->glyph_row->reversed_p)
19517 {
19518 /* Prepend a stretch glyph to the row, such that the
19519 rightmost glyph will be drawn flushed all the way to the
19520 right margin of the window. The stretch glyph that will
19521 occupy the empty space, if any, to the left of the
19522 glyphs. */
19523 struct font *font = face->font ? face->font : FRAME_FONT (f);
19524 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19525 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19526 struct glyph *g;
19527 int row_width, stretch_ascent, stretch_width;
19528 struct text_pos saved_pos;
19529 int saved_face_id;
19530 bool saved_avoid_cursor, saved_box_start;
19531
19532 for (row_width = 0, g = row_start; g < row_end; g++)
19533 row_width += g->pixel_width;
19534
19535 /* FIXME: There are various minor display glitches in R2L
19536 rows when only one of the fringes is missing. The
19537 strange condition below produces the least bad effect. */
19538 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19539 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19540 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19541 stretch_width = window_box_width (it->w, TEXT_AREA);
19542 else
19543 stretch_width = it->last_visible_x - it->first_visible_x;
19544 stretch_width -= row_width;
19545
19546 if (stretch_width > 0)
19547 {
19548 stretch_ascent =
19549 (((it->ascent + it->descent)
19550 * FONT_BASE (font)) / FONT_HEIGHT (font));
19551 saved_pos = it->position;
19552 memset (&it->position, 0, sizeof it->position);
19553 saved_avoid_cursor = it->avoid_cursor_p;
19554 it->avoid_cursor_p = true;
19555 saved_face_id = it->face_id;
19556 saved_box_start = it->start_of_box_run_p;
19557 /* The last row's stretch glyph should get the default
19558 face, to avoid painting the rest of the window with
19559 the region face, if the region ends at ZV. */
19560 if (it->glyph_row->ends_at_zv_p)
19561 it->face_id = default_face->id;
19562 else
19563 it->face_id = face->id;
19564 it->start_of_box_run_p = false;
19565 append_stretch_glyph (it, Qnil, stretch_width,
19566 it->ascent + it->descent, stretch_ascent);
19567 it->position = saved_pos;
19568 it->avoid_cursor_p = saved_avoid_cursor;
19569 it->face_id = saved_face_id;
19570 it->start_of_box_run_p = saved_box_start;
19571 }
19572 /* If stretch_width comes out negative, it means that the
19573 last glyph is only partially visible. In R2L rows, we
19574 want the leftmost glyph to be partially visible, so we
19575 need to give the row the corresponding left offset. */
19576 if (stretch_width < 0)
19577 it->glyph_row->x = stretch_width;
19578 }
19579 #endif /* HAVE_WINDOW_SYSTEM */
19580 }
19581 else
19582 {
19583 /* Save some values that must not be changed. */
19584 int saved_x = it->current_x;
19585 struct text_pos saved_pos;
19586 Lisp_Object saved_object;
19587 enum display_element_type saved_what = it->what;
19588 int saved_face_id = it->face_id;
19589
19590 saved_object = it->object;
19591 saved_pos = it->position;
19592
19593 it->what = IT_CHARACTER;
19594 memset (&it->position, 0, sizeof it->position);
19595 it->object = Qnil;
19596 it->c = it->char_to_display = ' ';
19597 it->len = 1;
19598
19599 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19600 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19601 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19602 && !it->glyph_row->mode_line_p
19603 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19604 {
19605 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19606 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19607
19608 for (it->current_x = 0; g < e; g++)
19609 it->current_x += g->pixel_width;
19610
19611 it->area = LEFT_MARGIN_AREA;
19612 it->face_id = default_face->id;
19613 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19614 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19615 {
19616 PRODUCE_GLYPHS (it);
19617 /* term.c:produce_glyphs advances it->current_x only for
19618 TEXT_AREA. */
19619 it->current_x += it->pixel_width;
19620 }
19621
19622 it->current_x = saved_x;
19623 it->area = TEXT_AREA;
19624 }
19625
19626 /* The last row's blank glyphs should get the default face, to
19627 avoid painting the rest of the window with the region face,
19628 if the region ends at ZV. */
19629 if (it->glyph_row->ends_at_zv_p)
19630 it->face_id = default_face->id;
19631 else
19632 it->face_id = face->id;
19633 PRODUCE_GLYPHS (it);
19634
19635 while (it->current_x <= it->last_visible_x)
19636 PRODUCE_GLYPHS (it);
19637
19638 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19639 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19640 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19641 && !it->glyph_row->mode_line_p
19642 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19643 {
19644 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19645 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19646
19647 for ( ; g < e; g++)
19648 it->current_x += g->pixel_width;
19649
19650 it->area = RIGHT_MARGIN_AREA;
19651 it->face_id = default_face->id;
19652 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19653 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19654 {
19655 PRODUCE_GLYPHS (it);
19656 it->current_x += it->pixel_width;
19657 }
19658
19659 it->area = TEXT_AREA;
19660 }
19661
19662 /* Don't count these blanks really. It would let us insert a left
19663 truncation glyph below and make us set the cursor on them, maybe. */
19664 it->current_x = saved_x;
19665 it->object = saved_object;
19666 it->position = saved_pos;
19667 it->what = saved_what;
19668 it->face_id = saved_face_id;
19669 }
19670 }
19671
19672
19673 /* Value is true if text starting at CHARPOS in current_buffer is
19674 trailing whitespace. */
19675
19676 static bool
19677 trailing_whitespace_p (ptrdiff_t charpos)
19678 {
19679 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19680 int c = 0;
19681
19682 while (bytepos < ZV_BYTE
19683 && (c = FETCH_CHAR (bytepos),
19684 c == ' ' || c == '\t'))
19685 ++bytepos;
19686
19687 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19688 {
19689 if (bytepos != PT_BYTE)
19690 return true;
19691 }
19692 return false;
19693 }
19694
19695
19696 /* Highlight trailing whitespace, if any, in ROW. */
19697
19698 static void
19699 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19700 {
19701 int used = row->used[TEXT_AREA];
19702
19703 if (used)
19704 {
19705 struct glyph *start = row->glyphs[TEXT_AREA];
19706 struct glyph *glyph = start + used - 1;
19707
19708 if (row->reversed_p)
19709 {
19710 /* Right-to-left rows need to be processed in the opposite
19711 direction, so swap the edge pointers. */
19712 glyph = start;
19713 start = row->glyphs[TEXT_AREA] + used - 1;
19714 }
19715
19716 /* Skip over glyphs inserted to display the cursor at the
19717 end of a line, for extending the face of the last glyph
19718 to the end of the line on terminals, and for truncation
19719 and continuation glyphs. */
19720 if (!row->reversed_p)
19721 {
19722 while (glyph >= start
19723 && glyph->type == CHAR_GLYPH
19724 && NILP (glyph->object))
19725 --glyph;
19726 }
19727 else
19728 {
19729 while (glyph <= start
19730 && glyph->type == CHAR_GLYPH
19731 && NILP (glyph->object))
19732 ++glyph;
19733 }
19734
19735 /* If last glyph is a space or stretch, and it's trailing
19736 whitespace, set the face of all trailing whitespace glyphs in
19737 IT->glyph_row to `trailing-whitespace'. */
19738 if ((row->reversed_p ? glyph <= start : glyph >= start)
19739 && BUFFERP (glyph->object)
19740 && (glyph->type == STRETCH_GLYPH
19741 || (glyph->type == CHAR_GLYPH
19742 && glyph->u.ch == ' '))
19743 && trailing_whitespace_p (glyph->charpos))
19744 {
19745 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
19746 if (face_id < 0)
19747 return;
19748
19749 if (!row->reversed_p)
19750 {
19751 while (glyph >= start
19752 && BUFFERP (glyph->object)
19753 && (glyph->type == STRETCH_GLYPH
19754 || (glyph->type == CHAR_GLYPH
19755 && glyph->u.ch == ' ')))
19756 (glyph--)->face_id = face_id;
19757 }
19758 else
19759 {
19760 while (glyph <= start
19761 && BUFFERP (glyph->object)
19762 && (glyph->type == STRETCH_GLYPH
19763 || (glyph->type == CHAR_GLYPH
19764 && glyph->u.ch == ' ')))
19765 (glyph++)->face_id = face_id;
19766 }
19767 }
19768 }
19769 }
19770
19771
19772 /* Value is true if glyph row ROW should be
19773 considered to hold the buffer position CHARPOS. */
19774
19775 static bool
19776 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19777 {
19778 bool result = true;
19779
19780 if (charpos == CHARPOS (row->end.pos)
19781 || charpos == MATRIX_ROW_END_CHARPOS (row))
19782 {
19783 /* Suppose the row ends on a string.
19784 Unless the row is continued, that means it ends on a newline
19785 in the string. If it's anything other than a display string
19786 (e.g., a before-string from an overlay), we don't want the
19787 cursor there. (This heuristic seems to give the optimal
19788 behavior for the various types of multi-line strings.)
19789 One exception: if the string has `cursor' property on one of
19790 its characters, we _do_ want the cursor there. */
19791 if (CHARPOS (row->end.string_pos) >= 0)
19792 {
19793 if (row->continued_p)
19794 result = true;
19795 else
19796 {
19797 /* Check for `display' property. */
19798 struct glyph *beg = row->glyphs[TEXT_AREA];
19799 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19800 struct glyph *glyph;
19801
19802 result = false;
19803 for (glyph = end; glyph >= beg; --glyph)
19804 if (STRINGP (glyph->object))
19805 {
19806 Lisp_Object prop
19807 = Fget_char_property (make_number (charpos),
19808 Qdisplay, Qnil);
19809 result =
19810 (!NILP (prop)
19811 && display_prop_string_p (prop, glyph->object));
19812 /* If there's a `cursor' property on one of the
19813 string's characters, this row is a cursor row,
19814 even though this is not a display string. */
19815 if (!result)
19816 {
19817 Lisp_Object s = glyph->object;
19818
19819 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19820 {
19821 ptrdiff_t gpos = glyph->charpos;
19822
19823 if (!NILP (Fget_char_property (make_number (gpos),
19824 Qcursor, s)))
19825 {
19826 result = true;
19827 break;
19828 }
19829 }
19830 }
19831 break;
19832 }
19833 }
19834 }
19835 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19836 {
19837 /* If the row ends in middle of a real character,
19838 and the line is continued, we want the cursor here.
19839 That's because CHARPOS (ROW->end.pos) would equal
19840 PT if PT is before the character. */
19841 if (!row->ends_in_ellipsis_p)
19842 result = row->continued_p;
19843 else
19844 /* If the row ends in an ellipsis, then
19845 CHARPOS (ROW->end.pos) will equal point after the
19846 invisible text. We want that position to be displayed
19847 after the ellipsis. */
19848 result = false;
19849 }
19850 /* If the row ends at ZV, display the cursor at the end of that
19851 row instead of at the start of the row below. */
19852 else
19853 result = row->ends_at_zv_p;
19854 }
19855
19856 return result;
19857 }
19858
19859 /* Value is true if glyph row ROW should be
19860 used to hold the cursor. */
19861
19862 static bool
19863 cursor_row_p (struct glyph_row *row)
19864 {
19865 return row_for_charpos_p (row, PT);
19866 }
19867
19868 \f
19869
19870 /* Push the property PROP so that it will be rendered at the current
19871 position in IT. Return true if PROP was successfully pushed, false
19872 otherwise. Called from handle_line_prefix to handle the
19873 `line-prefix' and `wrap-prefix' properties. */
19874
19875 static bool
19876 push_prefix_prop (struct it *it, Lisp_Object prop)
19877 {
19878 struct text_pos pos =
19879 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19880
19881 eassert (it->method == GET_FROM_BUFFER
19882 || it->method == GET_FROM_DISPLAY_VECTOR
19883 || it->method == GET_FROM_STRING
19884 || it->method == GET_FROM_IMAGE);
19885
19886 /* We need to save the current buffer/string position, so it will be
19887 restored by pop_it, because iterate_out_of_display_property
19888 depends on that being set correctly, but some situations leave
19889 it->position not yet set when this function is called. */
19890 push_it (it, &pos);
19891
19892 if (STRINGP (prop))
19893 {
19894 if (SCHARS (prop) == 0)
19895 {
19896 pop_it (it);
19897 return false;
19898 }
19899
19900 it->string = prop;
19901 it->string_from_prefix_prop_p = true;
19902 it->multibyte_p = STRING_MULTIBYTE (it->string);
19903 it->current.overlay_string_index = -1;
19904 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19905 it->end_charpos = it->string_nchars = SCHARS (it->string);
19906 it->method = GET_FROM_STRING;
19907 it->stop_charpos = 0;
19908 it->prev_stop = 0;
19909 it->base_level_stop = 0;
19910
19911 /* Force paragraph direction to be that of the parent
19912 buffer/string. */
19913 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19914 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19915 else
19916 it->paragraph_embedding = L2R;
19917
19918 /* Set up the bidi iterator for this display string. */
19919 if (it->bidi_p)
19920 {
19921 it->bidi_it.string.lstring = it->string;
19922 it->bidi_it.string.s = NULL;
19923 it->bidi_it.string.schars = it->end_charpos;
19924 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19925 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19926 it->bidi_it.string.unibyte = !it->multibyte_p;
19927 it->bidi_it.w = it->w;
19928 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19929 }
19930 }
19931 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19932 {
19933 it->method = GET_FROM_STRETCH;
19934 it->object = prop;
19935 }
19936 #ifdef HAVE_WINDOW_SYSTEM
19937 else if (IMAGEP (prop))
19938 {
19939 it->what = IT_IMAGE;
19940 it->image_id = lookup_image (it->f, prop);
19941 it->method = GET_FROM_IMAGE;
19942 }
19943 #endif /* HAVE_WINDOW_SYSTEM */
19944 else
19945 {
19946 pop_it (it); /* bogus display property, give up */
19947 return false;
19948 }
19949
19950 return true;
19951 }
19952
19953 /* Return the character-property PROP at the current position in IT. */
19954
19955 static Lisp_Object
19956 get_it_property (struct it *it, Lisp_Object prop)
19957 {
19958 Lisp_Object position, object = it->object;
19959
19960 if (STRINGP (object))
19961 position = make_number (IT_STRING_CHARPOS (*it));
19962 else if (BUFFERP (object))
19963 {
19964 position = make_number (IT_CHARPOS (*it));
19965 object = it->window;
19966 }
19967 else
19968 return Qnil;
19969
19970 return Fget_char_property (position, prop, object);
19971 }
19972
19973 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19974
19975 static void
19976 handle_line_prefix (struct it *it)
19977 {
19978 Lisp_Object prefix;
19979
19980 if (it->continuation_lines_width > 0)
19981 {
19982 prefix = get_it_property (it, Qwrap_prefix);
19983 if (NILP (prefix))
19984 prefix = Vwrap_prefix;
19985 }
19986 else
19987 {
19988 prefix = get_it_property (it, Qline_prefix);
19989 if (NILP (prefix))
19990 prefix = Vline_prefix;
19991 }
19992 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19993 {
19994 /* If the prefix is wider than the window, and we try to wrap
19995 it, it would acquire its own wrap prefix, and so on till the
19996 iterator stack overflows. So, don't wrap the prefix. */
19997 it->line_wrap = TRUNCATE;
19998 it->avoid_cursor_p = true;
19999 }
20000 }
20001
20002 \f
20003
20004 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20005 only for R2L lines from display_line and display_string, when they
20006 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20007 the line/string needs to be continued on the next glyph row. */
20008 static void
20009 unproduce_glyphs (struct it *it, int n)
20010 {
20011 struct glyph *glyph, *end;
20012
20013 eassert (it->glyph_row);
20014 eassert (it->glyph_row->reversed_p);
20015 eassert (it->area == TEXT_AREA);
20016 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20017
20018 if (n > it->glyph_row->used[TEXT_AREA])
20019 n = it->glyph_row->used[TEXT_AREA];
20020 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20021 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20022 for ( ; glyph < end; glyph++)
20023 glyph[-n] = *glyph;
20024 }
20025
20026 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20027 and ROW->maxpos. */
20028 static void
20029 find_row_edges (struct it *it, struct glyph_row *row,
20030 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20031 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20032 {
20033 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20034 lines' rows is implemented for bidi-reordered rows. */
20035
20036 /* ROW->minpos is the value of min_pos, the minimal buffer position
20037 we have in ROW, or ROW->start.pos if that is smaller. */
20038 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20039 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20040 else
20041 /* We didn't find buffer positions smaller than ROW->start, or
20042 didn't find _any_ valid buffer positions in any of the glyphs,
20043 so we must trust the iterator's computed positions. */
20044 row->minpos = row->start.pos;
20045 if (max_pos <= 0)
20046 {
20047 max_pos = CHARPOS (it->current.pos);
20048 max_bpos = BYTEPOS (it->current.pos);
20049 }
20050
20051 /* Here are the various use-cases for ending the row, and the
20052 corresponding values for ROW->maxpos:
20053
20054 Line ends in a newline from buffer eol_pos + 1
20055 Line is continued from buffer max_pos + 1
20056 Line is truncated on right it->current.pos
20057 Line ends in a newline from string max_pos + 1(*)
20058 (*) + 1 only when line ends in a forward scan
20059 Line is continued from string max_pos
20060 Line is continued from display vector max_pos
20061 Line is entirely from a string min_pos == max_pos
20062 Line is entirely from a display vector min_pos == max_pos
20063 Line that ends at ZV ZV
20064
20065 If you discover other use-cases, please add them here as
20066 appropriate. */
20067 if (row->ends_at_zv_p)
20068 row->maxpos = it->current.pos;
20069 else if (row->used[TEXT_AREA])
20070 {
20071 bool seen_this_string = false;
20072 struct glyph_row *r1 = row - 1;
20073
20074 /* Did we see the same display string on the previous row? */
20075 if (STRINGP (it->object)
20076 /* this is not the first row */
20077 && row > it->w->desired_matrix->rows
20078 /* previous row is not the header line */
20079 && !r1->mode_line_p
20080 /* previous row also ends in a newline from a string */
20081 && r1->ends_in_newline_from_string_p)
20082 {
20083 struct glyph *start, *end;
20084
20085 /* Search for the last glyph of the previous row that came
20086 from buffer or string. Depending on whether the row is
20087 L2R or R2L, we need to process it front to back or the
20088 other way round. */
20089 if (!r1->reversed_p)
20090 {
20091 start = r1->glyphs[TEXT_AREA];
20092 end = start + r1->used[TEXT_AREA];
20093 /* Glyphs inserted by redisplay have nil as their object. */
20094 while (end > start
20095 && NILP ((end - 1)->object)
20096 && (end - 1)->charpos <= 0)
20097 --end;
20098 if (end > start)
20099 {
20100 if (EQ ((end - 1)->object, it->object))
20101 seen_this_string = true;
20102 }
20103 else
20104 /* If all the glyphs of the previous row were inserted
20105 by redisplay, it means the previous row was
20106 produced from a single newline, which is only
20107 possible if that newline came from the same string
20108 as the one which produced this ROW. */
20109 seen_this_string = true;
20110 }
20111 else
20112 {
20113 end = r1->glyphs[TEXT_AREA] - 1;
20114 start = end + r1->used[TEXT_AREA];
20115 while (end < start
20116 && NILP ((end + 1)->object)
20117 && (end + 1)->charpos <= 0)
20118 ++end;
20119 if (end < start)
20120 {
20121 if (EQ ((end + 1)->object, it->object))
20122 seen_this_string = true;
20123 }
20124 else
20125 seen_this_string = true;
20126 }
20127 }
20128 /* Take note of each display string that covers a newline only
20129 once, the first time we see it. This is for when a display
20130 string includes more than one newline in it. */
20131 if (row->ends_in_newline_from_string_p && !seen_this_string)
20132 {
20133 /* If we were scanning the buffer forward when we displayed
20134 the string, we want to account for at least one buffer
20135 position that belongs to this row (position covered by
20136 the display string), so that cursor positioning will
20137 consider this row as a candidate when point is at the end
20138 of the visual line represented by this row. This is not
20139 required when scanning back, because max_pos will already
20140 have a much larger value. */
20141 if (CHARPOS (row->end.pos) > max_pos)
20142 INC_BOTH (max_pos, max_bpos);
20143 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20144 }
20145 else if (CHARPOS (it->eol_pos) > 0)
20146 SET_TEXT_POS (row->maxpos,
20147 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20148 else if (row->continued_p)
20149 {
20150 /* If max_pos is different from IT's current position, it
20151 means IT->method does not belong to the display element
20152 at max_pos. However, it also means that the display
20153 element at max_pos was displayed in its entirety on this
20154 line, which is equivalent to saying that the next line
20155 starts at the next buffer position. */
20156 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20157 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20158 else
20159 {
20160 INC_BOTH (max_pos, max_bpos);
20161 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20162 }
20163 }
20164 else if (row->truncated_on_right_p)
20165 /* display_line already called reseat_at_next_visible_line_start,
20166 which puts the iterator at the beginning of the next line, in
20167 the logical order. */
20168 row->maxpos = it->current.pos;
20169 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20170 /* A line that is entirely from a string/image/stretch... */
20171 row->maxpos = row->minpos;
20172 else
20173 emacs_abort ();
20174 }
20175 else
20176 row->maxpos = it->current.pos;
20177 }
20178
20179 /* Construct the glyph row IT->glyph_row in the desired matrix of
20180 IT->w from text at the current position of IT. See dispextern.h
20181 for an overview of struct it. Value is true if
20182 IT->glyph_row displays text, as opposed to a line displaying ZV
20183 only. */
20184
20185 static bool
20186 display_line (struct it *it)
20187 {
20188 struct glyph_row *row = it->glyph_row;
20189 Lisp_Object overlay_arrow_string;
20190 struct it wrap_it;
20191 void *wrap_data = NULL;
20192 bool may_wrap = false;
20193 int wrap_x IF_LINT (= 0);
20194 int wrap_row_used = -1;
20195 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20196 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20197 int wrap_row_extra_line_spacing IF_LINT (= 0);
20198 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20199 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20200 int cvpos;
20201 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20202 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20203 bool pending_handle_line_prefix = false;
20204
20205 /* We always start displaying at hpos zero even if hscrolled. */
20206 eassert (it->hpos == 0 && it->current_x == 0);
20207
20208 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20209 >= it->w->desired_matrix->nrows)
20210 {
20211 it->w->nrows_scale_factor++;
20212 it->f->fonts_changed = true;
20213 return false;
20214 }
20215
20216 /* Clear the result glyph row and enable it. */
20217 prepare_desired_row (it->w, row, false);
20218
20219 row->y = it->current_y;
20220 row->start = it->start;
20221 row->continuation_lines_width = it->continuation_lines_width;
20222 row->displays_text_p = true;
20223 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20224 it->starts_in_middle_of_char_p = false;
20225
20226 /* Arrange the overlays nicely for our purposes. Usually, we call
20227 display_line on only one line at a time, in which case this
20228 can't really hurt too much, or we call it on lines which appear
20229 one after another in the buffer, in which case all calls to
20230 recenter_overlay_lists but the first will be pretty cheap. */
20231 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20232
20233 /* Move over display elements that are not visible because we are
20234 hscrolled. This may stop at an x-position < IT->first_visible_x
20235 if the first glyph is partially visible or if we hit a line end. */
20236 if (it->current_x < it->first_visible_x)
20237 {
20238 enum move_it_result move_result;
20239
20240 this_line_min_pos = row->start.pos;
20241 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20242 MOVE_TO_POS | MOVE_TO_X);
20243 /* If we are under a large hscroll, move_it_in_display_line_to
20244 could hit the end of the line without reaching
20245 it->first_visible_x. Pretend that we did reach it. This is
20246 especially important on a TTY, where we will call
20247 extend_face_to_end_of_line, which needs to know how many
20248 blank glyphs to produce. */
20249 if (it->current_x < it->first_visible_x
20250 && (move_result == MOVE_NEWLINE_OR_CR
20251 || move_result == MOVE_POS_MATCH_OR_ZV))
20252 it->current_x = it->first_visible_x;
20253
20254 /* Record the smallest positions seen while we moved over
20255 display elements that are not visible. This is needed by
20256 redisplay_internal for optimizing the case where the cursor
20257 stays inside the same line. The rest of this function only
20258 considers positions that are actually displayed, so
20259 RECORD_MAX_MIN_POS will not otherwise record positions that
20260 are hscrolled to the left of the left edge of the window. */
20261 min_pos = CHARPOS (this_line_min_pos);
20262 min_bpos = BYTEPOS (this_line_min_pos);
20263 }
20264 else if (it->area == TEXT_AREA)
20265 {
20266 /* We only do this when not calling move_it_in_display_line_to
20267 above, because that function calls itself handle_line_prefix. */
20268 handle_line_prefix (it);
20269 }
20270 else
20271 {
20272 /* Line-prefix and wrap-prefix are always displayed in the text
20273 area. But if this is the first call to display_line after
20274 init_iterator, the iterator might have been set up to write
20275 into a marginal area, e.g. if the line begins with some
20276 display property that writes to the margins. So we need to
20277 wait with the call to handle_line_prefix until whatever
20278 writes to the margin has done its job. */
20279 pending_handle_line_prefix = true;
20280 }
20281
20282 /* Get the initial row height. This is either the height of the
20283 text hscrolled, if there is any, or zero. */
20284 row->ascent = it->max_ascent;
20285 row->height = it->max_ascent + it->max_descent;
20286 row->phys_ascent = it->max_phys_ascent;
20287 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20288 row->extra_line_spacing = it->max_extra_line_spacing;
20289
20290 /* Utility macro to record max and min buffer positions seen until now. */
20291 #define RECORD_MAX_MIN_POS(IT) \
20292 do \
20293 { \
20294 bool composition_p \
20295 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20296 ptrdiff_t current_pos = \
20297 composition_p ? (IT)->cmp_it.charpos \
20298 : IT_CHARPOS (*(IT)); \
20299 ptrdiff_t current_bpos = \
20300 composition_p ? CHAR_TO_BYTE (current_pos) \
20301 : IT_BYTEPOS (*(IT)); \
20302 if (current_pos < min_pos) \
20303 { \
20304 min_pos = current_pos; \
20305 min_bpos = current_bpos; \
20306 } \
20307 if (IT_CHARPOS (*it) > max_pos) \
20308 { \
20309 max_pos = IT_CHARPOS (*it); \
20310 max_bpos = IT_BYTEPOS (*it); \
20311 } \
20312 } \
20313 while (false)
20314
20315 /* Loop generating characters. The loop is left with IT on the next
20316 character to display. */
20317 while (true)
20318 {
20319 int n_glyphs_before, hpos_before, x_before;
20320 int x, nglyphs;
20321 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20322
20323 /* Retrieve the next thing to display. Value is false if end of
20324 buffer reached. */
20325 if (!get_next_display_element (it))
20326 {
20327 /* Maybe add a space at the end of this line that is used to
20328 display the cursor there under X. Set the charpos of the
20329 first glyph of blank lines not corresponding to any text
20330 to -1. */
20331 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20332 row->exact_window_width_line_p = true;
20333 else if ((append_space_for_newline (it, true)
20334 && row->used[TEXT_AREA] == 1)
20335 || row->used[TEXT_AREA] == 0)
20336 {
20337 row->glyphs[TEXT_AREA]->charpos = -1;
20338 row->displays_text_p = false;
20339
20340 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20341 && (!MINI_WINDOW_P (it->w)
20342 || (minibuf_level && EQ (it->window, minibuf_window))))
20343 row->indicate_empty_line_p = true;
20344 }
20345
20346 it->continuation_lines_width = 0;
20347 row->ends_at_zv_p = true;
20348 /* A row that displays right-to-left text must always have
20349 its last face extended all the way to the end of line,
20350 even if this row ends in ZV, because we still write to
20351 the screen left to right. We also need to extend the
20352 last face if the default face is remapped to some
20353 different face, otherwise the functions that clear
20354 portions of the screen will clear with the default face's
20355 background color. */
20356 if (row->reversed_p
20357 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20358 extend_face_to_end_of_line (it);
20359 break;
20360 }
20361
20362 /* Now, get the metrics of what we want to display. This also
20363 generates glyphs in `row' (which is IT->glyph_row). */
20364 n_glyphs_before = row->used[TEXT_AREA];
20365 x = it->current_x;
20366
20367 /* Remember the line height so far in case the next element doesn't
20368 fit on the line. */
20369 if (it->line_wrap != TRUNCATE)
20370 {
20371 ascent = it->max_ascent;
20372 descent = it->max_descent;
20373 phys_ascent = it->max_phys_ascent;
20374 phys_descent = it->max_phys_descent;
20375
20376 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20377 {
20378 if (IT_DISPLAYING_WHITESPACE (it))
20379 may_wrap = true;
20380 else if (may_wrap)
20381 {
20382 SAVE_IT (wrap_it, *it, wrap_data);
20383 wrap_x = x;
20384 wrap_row_used = row->used[TEXT_AREA];
20385 wrap_row_ascent = row->ascent;
20386 wrap_row_height = row->height;
20387 wrap_row_phys_ascent = row->phys_ascent;
20388 wrap_row_phys_height = row->phys_height;
20389 wrap_row_extra_line_spacing = row->extra_line_spacing;
20390 wrap_row_min_pos = min_pos;
20391 wrap_row_min_bpos = min_bpos;
20392 wrap_row_max_pos = max_pos;
20393 wrap_row_max_bpos = max_bpos;
20394 may_wrap = false;
20395 }
20396 }
20397 }
20398
20399 PRODUCE_GLYPHS (it);
20400
20401 /* If this display element was in marginal areas, continue with
20402 the next one. */
20403 if (it->area != TEXT_AREA)
20404 {
20405 row->ascent = max (row->ascent, it->max_ascent);
20406 row->height = max (row->height, it->max_ascent + it->max_descent);
20407 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20408 row->phys_height = max (row->phys_height,
20409 it->max_phys_ascent + it->max_phys_descent);
20410 row->extra_line_spacing = max (row->extra_line_spacing,
20411 it->max_extra_line_spacing);
20412 set_iterator_to_next (it, true);
20413 /* If we didn't handle the line/wrap prefix above, and the
20414 call to set_iterator_to_next just switched to TEXT_AREA,
20415 process the prefix now. */
20416 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20417 {
20418 pending_handle_line_prefix = false;
20419 handle_line_prefix (it);
20420 }
20421 continue;
20422 }
20423
20424 /* Does the display element fit on the line? If we truncate
20425 lines, we should draw past the right edge of the window. If
20426 we don't truncate, we want to stop so that we can display the
20427 continuation glyph before the right margin. If lines are
20428 continued, there are two possible strategies for characters
20429 resulting in more than 1 glyph (e.g. tabs): Display as many
20430 glyphs as possible in this line and leave the rest for the
20431 continuation line, or display the whole element in the next
20432 line. Original redisplay did the former, so we do it also. */
20433 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20434 hpos_before = it->hpos;
20435 x_before = x;
20436
20437 if (/* Not a newline. */
20438 nglyphs > 0
20439 /* Glyphs produced fit entirely in the line. */
20440 && it->current_x < it->last_visible_x)
20441 {
20442 it->hpos += nglyphs;
20443 row->ascent = max (row->ascent, it->max_ascent);
20444 row->height = max (row->height, it->max_ascent + it->max_descent);
20445 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20446 row->phys_height = max (row->phys_height,
20447 it->max_phys_ascent + it->max_phys_descent);
20448 row->extra_line_spacing = max (row->extra_line_spacing,
20449 it->max_extra_line_spacing);
20450 if (it->current_x - it->pixel_width < it->first_visible_x
20451 /* In R2L rows, we arrange in extend_face_to_end_of_line
20452 to add a right offset to the line, by a suitable
20453 change to the stretch glyph that is the leftmost
20454 glyph of the line. */
20455 && !row->reversed_p)
20456 row->x = x - it->first_visible_x;
20457 /* Record the maximum and minimum buffer positions seen so
20458 far in glyphs that will be displayed by this row. */
20459 if (it->bidi_p)
20460 RECORD_MAX_MIN_POS (it);
20461 }
20462 else
20463 {
20464 int i, new_x;
20465 struct glyph *glyph;
20466
20467 for (i = 0; i < nglyphs; ++i, x = new_x)
20468 {
20469 /* Identify the glyphs added by the last call to
20470 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20471 the previous glyphs. */
20472 if (!row->reversed_p)
20473 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20474 else
20475 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20476 new_x = x + glyph->pixel_width;
20477
20478 if (/* Lines are continued. */
20479 it->line_wrap != TRUNCATE
20480 && (/* Glyph doesn't fit on the line. */
20481 new_x > it->last_visible_x
20482 /* Or it fits exactly on a window system frame. */
20483 || (new_x == it->last_visible_x
20484 && FRAME_WINDOW_P (it->f)
20485 && (row->reversed_p
20486 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20487 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20488 {
20489 /* End of a continued line. */
20490
20491 if (it->hpos == 0
20492 || (new_x == it->last_visible_x
20493 && FRAME_WINDOW_P (it->f)
20494 && (row->reversed_p
20495 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20496 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20497 {
20498 /* Current glyph is the only one on the line or
20499 fits exactly on the line. We must continue
20500 the line because we can't draw the cursor
20501 after the glyph. */
20502 row->continued_p = true;
20503 it->current_x = new_x;
20504 it->continuation_lines_width += new_x;
20505 ++it->hpos;
20506 if (i == nglyphs - 1)
20507 {
20508 /* If line-wrap is on, check if a previous
20509 wrap point was found. */
20510 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20511 && wrap_row_used > 0
20512 /* Even if there is a previous wrap
20513 point, continue the line here as
20514 usual, if (i) the previous character
20515 was a space or tab AND (ii) the
20516 current character is not. */
20517 && (!may_wrap
20518 || IT_DISPLAYING_WHITESPACE (it)))
20519 goto back_to_wrap;
20520
20521 /* Record the maximum and minimum buffer
20522 positions seen so far in glyphs that will be
20523 displayed by this row. */
20524 if (it->bidi_p)
20525 RECORD_MAX_MIN_POS (it);
20526 set_iterator_to_next (it, true);
20527 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20528 {
20529 if (!get_next_display_element (it))
20530 {
20531 row->exact_window_width_line_p = true;
20532 it->continuation_lines_width = 0;
20533 row->continued_p = false;
20534 row->ends_at_zv_p = true;
20535 }
20536 else if (ITERATOR_AT_END_OF_LINE_P (it))
20537 {
20538 row->continued_p = false;
20539 row->exact_window_width_line_p = true;
20540 }
20541 /* If line-wrap is on, check if a
20542 previous wrap point was found. */
20543 else if (wrap_row_used > 0
20544 /* Even if there is a previous wrap
20545 point, continue the line here as
20546 usual, if (i) the previous character
20547 was a space or tab AND (ii) the
20548 current character is not. */
20549 && (!may_wrap
20550 || IT_DISPLAYING_WHITESPACE (it)))
20551 goto back_to_wrap;
20552
20553 }
20554 }
20555 else if (it->bidi_p)
20556 RECORD_MAX_MIN_POS (it);
20557 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20558 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20559 extend_face_to_end_of_line (it);
20560 }
20561 else if (CHAR_GLYPH_PADDING_P (*glyph)
20562 && !FRAME_WINDOW_P (it->f))
20563 {
20564 /* A padding glyph that doesn't fit on this line.
20565 This means the whole character doesn't fit
20566 on the line. */
20567 if (row->reversed_p)
20568 unproduce_glyphs (it, row->used[TEXT_AREA]
20569 - n_glyphs_before);
20570 row->used[TEXT_AREA] = n_glyphs_before;
20571
20572 /* Fill the rest of the row with continuation
20573 glyphs like in 20.x. */
20574 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20575 < row->glyphs[1 + TEXT_AREA])
20576 produce_special_glyphs (it, IT_CONTINUATION);
20577
20578 row->continued_p = true;
20579 it->current_x = x_before;
20580 it->continuation_lines_width += x_before;
20581
20582 /* Restore the height to what it was before the
20583 element not fitting on the line. */
20584 it->max_ascent = ascent;
20585 it->max_descent = descent;
20586 it->max_phys_ascent = phys_ascent;
20587 it->max_phys_descent = phys_descent;
20588 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20589 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20590 extend_face_to_end_of_line (it);
20591 }
20592 else if (wrap_row_used > 0)
20593 {
20594 back_to_wrap:
20595 if (row->reversed_p)
20596 unproduce_glyphs (it,
20597 row->used[TEXT_AREA] - wrap_row_used);
20598 RESTORE_IT (it, &wrap_it, wrap_data);
20599 it->continuation_lines_width += wrap_x;
20600 row->used[TEXT_AREA] = wrap_row_used;
20601 row->ascent = wrap_row_ascent;
20602 row->height = wrap_row_height;
20603 row->phys_ascent = wrap_row_phys_ascent;
20604 row->phys_height = wrap_row_phys_height;
20605 row->extra_line_spacing = wrap_row_extra_line_spacing;
20606 min_pos = wrap_row_min_pos;
20607 min_bpos = wrap_row_min_bpos;
20608 max_pos = wrap_row_max_pos;
20609 max_bpos = wrap_row_max_bpos;
20610 row->continued_p = true;
20611 row->ends_at_zv_p = false;
20612 row->exact_window_width_line_p = false;
20613 it->continuation_lines_width += x;
20614
20615 /* Make sure that a non-default face is extended
20616 up to the right margin of the window. */
20617 extend_face_to_end_of_line (it);
20618 }
20619 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20620 {
20621 /* A TAB that extends past the right edge of the
20622 window. This produces a single glyph on
20623 window system frames. We leave the glyph in
20624 this row and let it fill the row, but don't
20625 consume the TAB. */
20626 if ((row->reversed_p
20627 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20628 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20629 produce_special_glyphs (it, IT_CONTINUATION);
20630 it->continuation_lines_width += it->last_visible_x;
20631 row->ends_in_middle_of_char_p = true;
20632 row->continued_p = true;
20633 glyph->pixel_width = it->last_visible_x - x;
20634 it->starts_in_middle_of_char_p = true;
20635 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20636 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20637 extend_face_to_end_of_line (it);
20638 }
20639 else
20640 {
20641 /* Something other than a TAB that draws past
20642 the right edge of the window. Restore
20643 positions to values before the element. */
20644 if (row->reversed_p)
20645 unproduce_glyphs (it, row->used[TEXT_AREA]
20646 - (n_glyphs_before + i));
20647 row->used[TEXT_AREA] = n_glyphs_before + i;
20648
20649 /* Display continuation glyphs. */
20650 it->current_x = x_before;
20651 it->continuation_lines_width += x;
20652 if (!FRAME_WINDOW_P (it->f)
20653 || (row->reversed_p
20654 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20655 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20656 produce_special_glyphs (it, IT_CONTINUATION);
20657 row->continued_p = true;
20658
20659 extend_face_to_end_of_line (it);
20660
20661 if (nglyphs > 1 && i > 0)
20662 {
20663 row->ends_in_middle_of_char_p = true;
20664 it->starts_in_middle_of_char_p = true;
20665 }
20666
20667 /* Restore the height to what it was before the
20668 element not fitting on the line. */
20669 it->max_ascent = ascent;
20670 it->max_descent = descent;
20671 it->max_phys_ascent = phys_ascent;
20672 it->max_phys_descent = phys_descent;
20673 }
20674
20675 break;
20676 }
20677 else if (new_x > it->first_visible_x)
20678 {
20679 /* Increment number of glyphs actually displayed. */
20680 ++it->hpos;
20681
20682 /* Record the maximum and minimum buffer positions
20683 seen so far in glyphs that will be displayed by
20684 this row. */
20685 if (it->bidi_p)
20686 RECORD_MAX_MIN_POS (it);
20687
20688 if (x < it->first_visible_x && !row->reversed_p)
20689 /* Glyph is partially visible, i.e. row starts at
20690 negative X position. Don't do that in R2L
20691 rows, where we arrange to add a right offset to
20692 the line in extend_face_to_end_of_line, by a
20693 suitable change to the stretch glyph that is
20694 the leftmost glyph of the line. */
20695 row->x = x - it->first_visible_x;
20696 /* When the last glyph of an R2L row only fits
20697 partially on the line, we need to set row->x to a
20698 negative offset, so that the leftmost glyph is
20699 the one that is partially visible. But if we are
20700 going to produce the truncation glyph, this will
20701 be taken care of in produce_special_glyphs. */
20702 if (row->reversed_p
20703 && new_x > it->last_visible_x
20704 && !(it->line_wrap == TRUNCATE
20705 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20706 {
20707 eassert (FRAME_WINDOW_P (it->f));
20708 row->x = it->last_visible_x - new_x;
20709 }
20710 }
20711 else
20712 {
20713 /* Glyph is completely off the left margin of the
20714 window. This should not happen because of the
20715 move_it_in_display_line at the start of this
20716 function, unless the text display area of the
20717 window is empty. */
20718 eassert (it->first_visible_x <= it->last_visible_x);
20719 }
20720 }
20721 /* Even if this display element produced no glyphs at all,
20722 we want to record its position. */
20723 if (it->bidi_p && nglyphs == 0)
20724 RECORD_MAX_MIN_POS (it);
20725
20726 row->ascent = max (row->ascent, it->max_ascent);
20727 row->height = max (row->height, it->max_ascent + it->max_descent);
20728 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20729 row->phys_height = max (row->phys_height,
20730 it->max_phys_ascent + it->max_phys_descent);
20731 row->extra_line_spacing = max (row->extra_line_spacing,
20732 it->max_extra_line_spacing);
20733
20734 /* End of this display line if row is continued. */
20735 if (row->continued_p || row->ends_at_zv_p)
20736 break;
20737 }
20738
20739 at_end_of_line:
20740 /* Is this a line end? If yes, we're also done, after making
20741 sure that a non-default face is extended up to the right
20742 margin of the window. */
20743 if (ITERATOR_AT_END_OF_LINE_P (it))
20744 {
20745 int used_before = row->used[TEXT_AREA];
20746
20747 row->ends_in_newline_from_string_p = STRINGP (it->object);
20748
20749 /* Add a space at the end of the line that is used to
20750 display the cursor there. */
20751 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20752 append_space_for_newline (it, false);
20753
20754 /* Extend the face to the end of the line. */
20755 extend_face_to_end_of_line (it);
20756
20757 /* Make sure we have the position. */
20758 if (used_before == 0)
20759 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20760
20761 /* Record the position of the newline, for use in
20762 find_row_edges. */
20763 it->eol_pos = it->current.pos;
20764
20765 /* Consume the line end. This skips over invisible lines. */
20766 set_iterator_to_next (it, true);
20767 it->continuation_lines_width = 0;
20768 break;
20769 }
20770
20771 /* Proceed with next display element. Note that this skips
20772 over lines invisible because of selective display. */
20773 set_iterator_to_next (it, true);
20774
20775 /* If we truncate lines, we are done when the last displayed
20776 glyphs reach past the right margin of the window. */
20777 if (it->line_wrap == TRUNCATE
20778 && ((FRAME_WINDOW_P (it->f)
20779 /* Images are preprocessed in produce_image_glyph such
20780 that they are cropped at the right edge of the
20781 window, so an image glyph will always end exactly at
20782 last_visible_x, even if there's no right fringe. */
20783 && ((row->reversed_p
20784 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20785 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20786 || it->what == IT_IMAGE))
20787 ? (it->current_x >= it->last_visible_x)
20788 : (it->current_x > it->last_visible_x)))
20789 {
20790 /* Maybe add truncation glyphs. */
20791 if (!FRAME_WINDOW_P (it->f)
20792 || (row->reversed_p
20793 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20794 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20795 {
20796 int i, n;
20797
20798 if (!row->reversed_p)
20799 {
20800 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20801 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20802 break;
20803 }
20804 else
20805 {
20806 for (i = 0; i < row->used[TEXT_AREA]; i++)
20807 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20808 break;
20809 /* Remove any padding glyphs at the front of ROW, to
20810 make room for the truncation glyphs we will be
20811 adding below. The loop below always inserts at
20812 least one truncation glyph, so also remove the
20813 last glyph added to ROW. */
20814 unproduce_glyphs (it, i + 1);
20815 /* Adjust i for the loop below. */
20816 i = row->used[TEXT_AREA] - (i + 1);
20817 }
20818
20819 /* produce_special_glyphs overwrites the last glyph, so
20820 we don't want that if we want to keep that last
20821 glyph, which means it's an image. */
20822 if (it->current_x > it->last_visible_x)
20823 {
20824 it->current_x = x_before;
20825 if (!FRAME_WINDOW_P (it->f))
20826 {
20827 for (n = row->used[TEXT_AREA]; i < n; ++i)
20828 {
20829 row->used[TEXT_AREA] = i;
20830 produce_special_glyphs (it, IT_TRUNCATION);
20831 }
20832 }
20833 else
20834 {
20835 row->used[TEXT_AREA] = i;
20836 produce_special_glyphs (it, IT_TRUNCATION);
20837 }
20838 it->hpos = hpos_before;
20839 }
20840 }
20841 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20842 {
20843 /* Don't truncate if we can overflow newline into fringe. */
20844 if (!get_next_display_element (it))
20845 {
20846 it->continuation_lines_width = 0;
20847 row->ends_at_zv_p = true;
20848 row->exact_window_width_line_p = true;
20849 break;
20850 }
20851 if (ITERATOR_AT_END_OF_LINE_P (it))
20852 {
20853 row->exact_window_width_line_p = true;
20854 goto at_end_of_line;
20855 }
20856 it->current_x = x_before;
20857 it->hpos = hpos_before;
20858 }
20859
20860 row->truncated_on_right_p = true;
20861 it->continuation_lines_width = 0;
20862 reseat_at_next_visible_line_start (it, false);
20863 /* We insist below that IT's position be at ZV because in
20864 bidi-reordered lines the character at visible line start
20865 might not be the character that follows the newline in
20866 the logical order. */
20867 if (IT_BYTEPOS (*it) > BEG_BYTE)
20868 row->ends_at_zv_p =
20869 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20870 else
20871 row->ends_at_zv_p = false;
20872 break;
20873 }
20874 }
20875
20876 if (wrap_data)
20877 bidi_unshelve_cache (wrap_data, true);
20878
20879 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20880 at the left window margin. */
20881 if (it->first_visible_x
20882 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20883 {
20884 if (!FRAME_WINDOW_P (it->f)
20885 || (((row->reversed_p
20886 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20887 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20888 /* Don't let insert_left_trunc_glyphs overwrite the
20889 first glyph of the row if it is an image. */
20890 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20891 insert_left_trunc_glyphs (it);
20892 row->truncated_on_left_p = true;
20893 }
20894
20895 /* Remember the position at which this line ends.
20896
20897 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20898 cannot be before the call to find_row_edges below, since that is
20899 where these positions are determined. */
20900 row->end = it->current;
20901 if (!it->bidi_p)
20902 {
20903 row->minpos = row->start.pos;
20904 row->maxpos = row->end.pos;
20905 }
20906 else
20907 {
20908 /* ROW->minpos and ROW->maxpos must be the smallest and
20909 `1 + the largest' buffer positions in ROW. But if ROW was
20910 bidi-reordered, these two positions can be anywhere in the
20911 row, so we must determine them now. */
20912 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20913 }
20914
20915 /* If the start of this line is the overlay arrow-position, then
20916 mark this glyph row as the one containing the overlay arrow.
20917 This is clearly a mess with variable size fonts. It would be
20918 better to let it be displayed like cursors under X. */
20919 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20920 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20921 !NILP (overlay_arrow_string)))
20922 {
20923 /* Overlay arrow in window redisplay is a fringe bitmap. */
20924 if (STRINGP (overlay_arrow_string))
20925 {
20926 struct glyph_row *arrow_row
20927 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20928 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20929 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20930 struct glyph *p = row->glyphs[TEXT_AREA];
20931 struct glyph *p2, *end;
20932
20933 /* Copy the arrow glyphs. */
20934 while (glyph < arrow_end)
20935 *p++ = *glyph++;
20936
20937 /* Throw away padding glyphs. */
20938 p2 = p;
20939 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20940 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20941 ++p2;
20942 if (p2 > p)
20943 {
20944 while (p2 < end)
20945 *p++ = *p2++;
20946 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20947 }
20948 }
20949 else
20950 {
20951 eassert (INTEGERP (overlay_arrow_string));
20952 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20953 }
20954 overlay_arrow_seen = true;
20955 }
20956
20957 /* Highlight trailing whitespace. */
20958 if (!NILP (Vshow_trailing_whitespace))
20959 highlight_trailing_whitespace (it->f, it->glyph_row);
20960
20961 /* Compute pixel dimensions of this line. */
20962 compute_line_metrics (it);
20963
20964 /* Implementation note: No changes in the glyphs of ROW or in their
20965 faces can be done past this point, because compute_line_metrics
20966 computes ROW's hash value and stores it within the glyph_row
20967 structure. */
20968
20969 /* Record whether this row ends inside an ellipsis. */
20970 row->ends_in_ellipsis_p
20971 = (it->method == GET_FROM_DISPLAY_VECTOR
20972 && it->ellipsis_p);
20973
20974 /* Save fringe bitmaps in this row. */
20975 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20976 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20977 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20978 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20979
20980 it->left_user_fringe_bitmap = 0;
20981 it->left_user_fringe_face_id = 0;
20982 it->right_user_fringe_bitmap = 0;
20983 it->right_user_fringe_face_id = 0;
20984
20985 /* Maybe set the cursor. */
20986 cvpos = it->w->cursor.vpos;
20987 if ((cvpos < 0
20988 /* In bidi-reordered rows, keep checking for proper cursor
20989 position even if one has been found already, because buffer
20990 positions in such rows change non-linearly with ROW->VPOS,
20991 when a line is continued. One exception: when we are at ZV,
20992 display cursor on the first suitable glyph row, since all
20993 the empty rows after that also have their position set to ZV. */
20994 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20995 lines' rows is implemented for bidi-reordered rows. */
20996 || (it->bidi_p
20997 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20998 && PT >= MATRIX_ROW_START_CHARPOS (row)
20999 && PT <= MATRIX_ROW_END_CHARPOS (row)
21000 && cursor_row_p (row))
21001 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21002
21003 /* Prepare for the next line. This line starts horizontally at (X
21004 HPOS) = (0 0). Vertical positions are incremented. As a
21005 convenience for the caller, IT->glyph_row is set to the next
21006 row to be used. */
21007 it->current_x = it->hpos = 0;
21008 it->current_y += row->height;
21009 SET_TEXT_POS (it->eol_pos, 0, 0);
21010 ++it->vpos;
21011 ++it->glyph_row;
21012 /* The next row should by default use the same value of the
21013 reversed_p flag as this one. set_iterator_to_next decides when
21014 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21015 the flag accordingly. */
21016 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21017 it->glyph_row->reversed_p = row->reversed_p;
21018 it->start = row->end;
21019 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21020
21021 #undef RECORD_MAX_MIN_POS
21022 }
21023
21024 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21025 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21026 doc: /* Return paragraph direction at point in BUFFER.
21027 Value is either `left-to-right' or `right-to-left'.
21028 If BUFFER is omitted or nil, it defaults to the current buffer.
21029
21030 Paragraph direction determines how the text in the paragraph is displayed.
21031 In left-to-right paragraphs, text begins at the left margin of the window
21032 and the reading direction is generally left to right. In right-to-left
21033 paragraphs, text begins at the right margin and is read from right to left.
21034
21035 See also `bidi-paragraph-direction'. */)
21036 (Lisp_Object buffer)
21037 {
21038 struct buffer *buf = current_buffer;
21039 struct buffer *old = buf;
21040
21041 if (! NILP (buffer))
21042 {
21043 CHECK_BUFFER (buffer);
21044 buf = XBUFFER (buffer);
21045 }
21046
21047 if (NILP (BVAR (buf, bidi_display_reordering))
21048 || NILP (BVAR (buf, enable_multibyte_characters))
21049 /* When we are loading loadup.el, the character property tables
21050 needed for bidi iteration are not yet available. */
21051 || !NILP (Vpurify_flag))
21052 return Qleft_to_right;
21053 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21054 return BVAR (buf, bidi_paragraph_direction);
21055 else
21056 {
21057 /* Determine the direction from buffer text. We could try to
21058 use current_matrix if it is up to date, but this seems fast
21059 enough as it is. */
21060 struct bidi_it itb;
21061 ptrdiff_t pos = BUF_PT (buf);
21062 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21063 int c;
21064 void *itb_data = bidi_shelve_cache ();
21065
21066 set_buffer_temp (buf);
21067 /* bidi_paragraph_init finds the base direction of the paragraph
21068 by searching forward from paragraph start. We need the base
21069 direction of the current or _previous_ paragraph, so we need
21070 to make sure we are within that paragraph. To that end, find
21071 the previous non-empty line. */
21072 if (pos >= ZV && pos > BEGV)
21073 DEC_BOTH (pos, bytepos);
21074 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21075 if (fast_looking_at (trailing_white_space,
21076 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21077 {
21078 while ((c = FETCH_BYTE (bytepos)) == '\n'
21079 || c == ' ' || c == '\t' || c == '\f')
21080 {
21081 if (bytepos <= BEGV_BYTE)
21082 break;
21083 bytepos--;
21084 pos--;
21085 }
21086 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21087 bytepos--;
21088 }
21089 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21090 itb.paragraph_dir = NEUTRAL_DIR;
21091 itb.string.s = NULL;
21092 itb.string.lstring = Qnil;
21093 itb.string.bufpos = 0;
21094 itb.string.from_disp_str = false;
21095 itb.string.unibyte = false;
21096 /* We have no window to use here for ignoring window-specific
21097 overlays. Using NULL for window pointer will cause
21098 compute_display_string_pos to use the current buffer. */
21099 itb.w = NULL;
21100 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21101 bidi_unshelve_cache (itb_data, false);
21102 set_buffer_temp (old);
21103 switch (itb.paragraph_dir)
21104 {
21105 case L2R:
21106 return Qleft_to_right;
21107 break;
21108 case R2L:
21109 return Qright_to_left;
21110 break;
21111 default:
21112 emacs_abort ();
21113 }
21114 }
21115 }
21116
21117 DEFUN ("bidi-find-overridden-directionality",
21118 Fbidi_find_overridden_directionality,
21119 Sbidi_find_overridden_directionality, 2, 3, 0,
21120 doc: /* Return position between FROM and TO where directionality was overridden.
21121
21122 This function returns the first character position in the specified
21123 region of OBJECT where there is a character whose `bidi-class' property
21124 is `L', but which was forced to display as `R' by a directional
21125 override, and likewise with characters whose `bidi-class' is `R'
21126 or `AL' that were forced to display as `L'.
21127
21128 If no such character is found, the function returns nil.
21129
21130 OBJECT is a Lisp string or buffer to search for overridden
21131 directionality, and defaults to the current buffer if nil or omitted.
21132 OBJECT can also be a window, in which case the function will search
21133 the buffer displayed in that window. Passing the window instead of
21134 a buffer is preferable when the buffer is displayed in some window,
21135 because this function will then be able to correctly account for
21136 window-specific overlays, which can affect the results.
21137
21138 Strong directional characters `L', `R', and `AL' can have their
21139 intrinsic directionality overridden by directional override
21140 control characters RLO (u+202e) and LRO (u+202d). See the
21141 function `get-char-code-property' for a way to inquire about
21142 the `bidi-class' property of a character. */)
21143 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21144 {
21145 struct buffer *buf = current_buffer;
21146 struct buffer *old = buf;
21147 struct window *w = NULL;
21148 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21149 struct bidi_it itb;
21150 ptrdiff_t from_pos, to_pos, from_bpos;
21151 void *itb_data;
21152
21153 if (!NILP (object))
21154 {
21155 if (BUFFERP (object))
21156 buf = XBUFFER (object);
21157 else if (WINDOWP (object))
21158 {
21159 w = decode_live_window (object);
21160 buf = XBUFFER (w->contents);
21161 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21162 }
21163 else
21164 CHECK_STRING (object);
21165 }
21166
21167 if (STRINGP (object))
21168 {
21169 /* Characters in unibyte strings are always treated by bidi.c as
21170 strong LTR. */
21171 if (!STRING_MULTIBYTE (object)
21172 /* When we are loading loadup.el, the character property
21173 tables needed for bidi iteration are not yet
21174 available. */
21175 || !NILP (Vpurify_flag))
21176 return Qnil;
21177
21178 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21179 if (from_pos >= SCHARS (object))
21180 return Qnil;
21181
21182 /* Set up the bidi iterator. */
21183 itb_data = bidi_shelve_cache ();
21184 itb.paragraph_dir = NEUTRAL_DIR;
21185 itb.string.lstring = object;
21186 itb.string.s = NULL;
21187 itb.string.schars = SCHARS (object);
21188 itb.string.bufpos = 0;
21189 itb.string.from_disp_str = false;
21190 itb.string.unibyte = false;
21191 itb.w = w;
21192 bidi_init_it (0, 0, frame_window_p, &itb);
21193 }
21194 else
21195 {
21196 /* Nothing this fancy can happen in unibyte buffers, or in a
21197 buffer that disabled reordering, or if FROM is at EOB. */
21198 if (NILP (BVAR (buf, bidi_display_reordering))
21199 || NILP (BVAR (buf, enable_multibyte_characters))
21200 /* When we are loading loadup.el, the character property
21201 tables needed for bidi iteration are not yet
21202 available. */
21203 || !NILP (Vpurify_flag))
21204 return Qnil;
21205
21206 set_buffer_temp (buf);
21207 validate_region (&from, &to);
21208 from_pos = XINT (from);
21209 to_pos = XINT (to);
21210 if (from_pos >= ZV)
21211 return Qnil;
21212
21213 /* Set up the bidi iterator. */
21214 itb_data = bidi_shelve_cache ();
21215 from_bpos = CHAR_TO_BYTE (from_pos);
21216 if (from_pos == BEGV)
21217 {
21218 itb.charpos = BEGV;
21219 itb.bytepos = BEGV_BYTE;
21220 }
21221 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21222 {
21223 itb.charpos = from_pos;
21224 itb.bytepos = from_bpos;
21225 }
21226 else
21227 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21228 -1, &itb.bytepos);
21229 itb.paragraph_dir = NEUTRAL_DIR;
21230 itb.string.s = NULL;
21231 itb.string.lstring = Qnil;
21232 itb.string.bufpos = 0;
21233 itb.string.from_disp_str = false;
21234 itb.string.unibyte = false;
21235 itb.w = w;
21236 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21237 }
21238
21239 ptrdiff_t found;
21240 do {
21241 /* For the purposes of this function, the actual base direction of
21242 the paragraph doesn't matter, so just set it to L2R. */
21243 bidi_paragraph_init (L2R, &itb, false);
21244 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21245 ;
21246 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21247
21248 bidi_unshelve_cache (itb_data, false);
21249 set_buffer_temp (old);
21250
21251 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21252 }
21253
21254 DEFUN ("move-point-visually", Fmove_point_visually,
21255 Smove_point_visually, 1, 1, 0,
21256 doc: /* Move point in the visual order in the specified DIRECTION.
21257 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21258 left.
21259
21260 Value is the new character position of point. */)
21261 (Lisp_Object direction)
21262 {
21263 struct window *w = XWINDOW (selected_window);
21264 struct buffer *b = XBUFFER (w->contents);
21265 struct glyph_row *row;
21266 int dir;
21267 Lisp_Object paragraph_dir;
21268
21269 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21270 (!(ROW)->continued_p \
21271 && NILP ((GLYPH)->object) \
21272 && (GLYPH)->type == CHAR_GLYPH \
21273 && (GLYPH)->u.ch == ' ' \
21274 && (GLYPH)->charpos >= 0 \
21275 && !(GLYPH)->avoid_cursor_p)
21276
21277 CHECK_NUMBER (direction);
21278 dir = XINT (direction);
21279 if (dir > 0)
21280 dir = 1;
21281 else
21282 dir = -1;
21283
21284 /* If current matrix is up-to-date, we can use the information
21285 recorded in the glyphs, at least as long as the goal is on the
21286 screen. */
21287 if (w->window_end_valid
21288 && !windows_or_buffers_changed
21289 && b
21290 && !b->clip_changed
21291 && !b->prevent_redisplay_optimizations_p
21292 && !window_outdated (w)
21293 /* We rely below on the cursor coordinates to be up to date, but
21294 we cannot trust them if some command moved point since the
21295 last complete redisplay. */
21296 && w->last_point == BUF_PT (b)
21297 && w->cursor.vpos >= 0
21298 && w->cursor.vpos < w->current_matrix->nrows
21299 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21300 {
21301 struct glyph *g = row->glyphs[TEXT_AREA];
21302 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21303 struct glyph *gpt = g + w->cursor.hpos;
21304
21305 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21306 {
21307 if (BUFFERP (g->object) && g->charpos != PT)
21308 {
21309 SET_PT (g->charpos);
21310 w->cursor.vpos = -1;
21311 return make_number (PT);
21312 }
21313 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21314 {
21315 ptrdiff_t new_pos;
21316
21317 if (BUFFERP (gpt->object))
21318 {
21319 new_pos = PT;
21320 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21321 new_pos += (row->reversed_p ? -dir : dir);
21322 else
21323 new_pos -= (row->reversed_p ? -dir : dir);
21324 }
21325 else if (BUFFERP (g->object))
21326 new_pos = g->charpos;
21327 else
21328 break;
21329 SET_PT (new_pos);
21330 w->cursor.vpos = -1;
21331 return make_number (PT);
21332 }
21333 else if (ROW_GLYPH_NEWLINE_P (row, g))
21334 {
21335 /* Glyphs inserted at the end of a non-empty line for
21336 positioning the cursor have zero charpos, so we must
21337 deduce the value of point by other means. */
21338 if (g->charpos > 0)
21339 SET_PT (g->charpos);
21340 else if (row->ends_at_zv_p && PT != ZV)
21341 SET_PT (ZV);
21342 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21343 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21344 else
21345 break;
21346 w->cursor.vpos = -1;
21347 return make_number (PT);
21348 }
21349 }
21350 if (g == e || NILP (g->object))
21351 {
21352 if (row->truncated_on_left_p || row->truncated_on_right_p)
21353 goto simulate_display;
21354 if (!row->reversed_p)
21355 row += dir;
21356 else
21357 row -= dir;
21358 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21359 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21360 goto simulate_display;
21361
21362 if (dir > 0)
21363 {
21364 if (row->reversed_p && !row->continued_p)
21365 {
21366 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21367 w->cursor.vpos = -1;
21368 return make_number (PT);
21369 }
21370 g = row->glyphs[TEXT_AREA];
21371 e = g + row->used[TEXT_AREA];
21372 for ( ; g < e; g++)
21373 {
21374 if (BUFFERP (g->object)
21375 /* Empty lines have only one glyph, which stands
21376 for the newline, and whose charpos is the
21377 buffer position of the newline. */
21378 || ROW_GLYPH_NEWLINE_P (row, g)
21379 /* When the buffer ends in a newline, the line at
21380 EOB also has one glyph, but its charpos is -1. */
21381 || (row->ends_at_zv_p
21382 && !row->reversed_p
21383 && NILP (g->object)
21384 && g->type == CHAR_GLYPH
21385 && g->u.ch == ' '))
21386 {
21387 if (g->charpos > 0)
21388 SET_PT (g->charpos);
21389 else if (!row->reversed_p
21390 && row->ends_at_zv_p
21391 && PT != ZV)
21392 SET_PT (ZV);
21393 else
21394 continue;
21395 w->cursor.vpos = -1;
21396 return make_number (PT);
21397 }
21398 }
21399 }
21400 else
21401 {
21402 if (!row->reversed_p && !row->continued_p)
21403 {
21404 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21405 w->cursor.vpos = -1;
21406 return make_number (PT);
21407 }
21408 e = row->glyphs[TEXT_AREA];
21409 g = e + row->used[TEXT_AREA] - 1;
21410 for ( ; g >= e; g--)
21411 {
21412 if (BUFFERP (g->object)
21413 || (ROW_GLYPH_NEWLINE_P (row, g)
21414 && g->charpos > 0)
21415 /* Empty R2L lines on GUI frames have the buffer
21416 position of the newline stored in the stretch
21417 glyph. */
21418 || g->type == STRETCH_GLYPH
21419 || (row->ends_at_zv_p
21420 && row->reversed_p
21421 && NILP (g->object)
21422 && g->type == CHAR_GLYPH
21423 && g->u.ch == ' '))
21424 {
21425 if (g->charpos > 0)
21426 SET_PT (g->charpos);
21427 else if (row->reversed_p
21428 && row->ends_at_zv_p
21429 && PT != ZV)
21430 SET_PT (ZV);
21431 else
21432 continue;
21433 w->cursor.vpos = -1;
21434 return make_number (PT);
21435 }
21436 }
21437 }
21438 }
21439 }
21440
21441 simulate_display:
21442
21443 /* If we wind up here, we failed to move by using the glyphs, so we
21444 need to simulate display instead. */
21445
21446 if (b)
21447 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21448 else
21449 paragraph_dir = Qleft_to_right;
21450 if (EQ (paragraph_dir, Qright_to_left))
21451 dir = -dir;
21452 if (PT <= BEGV && dir < 0)
21453 xsignal0 (Qbeginning_of_buffer);
21454 else if (PT >= ZV && dir > 0)
21455 xsignal0 (Qend_of_buffer);
21456 else
21457 {
21458 struct text_pos pt;
21459 struct it it;
21460 int pt_x, target_x, pixel_width, pt_vpos;
21461 bool at_eol_p;
21462 bool overshoot_expected = false;
21463 bool target_is_eol_p = false;
21464
21465 /* Setup the arena. */
21466 SET_TEXT_POS (pt, PT, PT_BYTE);
21467 start_display (&it, w, pt);
21468 /* When lines are truncated, we could be called with point
21469 outside of the windows edges, in which case move_it_*
21470 functions either prematurely stop at window's edge or jump to
21471 the next screen line, whereas we rely below on our ability to
21472 reach point, in order to start from its X coordinate. So we
21473 need to disregard the window's horizontal extent in that case. */
21474 if (it.line_wrap == TRUNCATE)
21475 it.last_visible_x = INFINITY;
21476
21477 if (it.cmp_it.id < 0
21478 && it.method == GET_FROM_STRING
21479 && it.area == TEXT_AREA
21480 && it.string_from_display_prop_p
21481 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21482 overshoot_expected = true;
21483
21484 /* Find the X coordinate of point. We start from the beginning
21485 of this or previous line to make sure we are before point in
21486 the logical order (since the move_it_* functions can only
21487 move forward). */
21488 reseat:
21489 reseat_at_previous_visible_line_start (&it);
21490 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21491 if (IT_CHARPOS (it) != PT)
21492 {
21493 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21494 -1, -1, -1, MOVE_TO_POS);
21495 /* If we missed point because the character there is
21496 displayed out of a display vector that has more than one
21497 glyph, retry expecting overshoot. */
21498 if (it.method == GET_FROM_DISPLAY_VECTOR
21499 && it.current.dpvec_index > 0
21500 && !overshoot_expected)
21501 {
21502 overshoot_expected = true;
21503 goto reseat;
21504 }
21505 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21506 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21507 }
21508 pt_x = it.current_x;
21509 pt_vpos = it.vpos;
21510 if (dir > 0 || overshoot_expected)
21511 {
21512 struct glyph_row *row = it.glyph_row;
21513
21514 /* When point is at beginning of line, we don't have
21515 information about the glyph there loaded into struct
21516 it. Calling get_next_display_element fixes that. */
21517 if (pt_x == 0)
21518 get_next_display_element (&it);
21519 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21520 it.glyph_row = NULL;
21521 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21522 it.glyph_row = row;
21523 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21524 it, lest it will become out of sync with it's buffer
21525 position. */
21526 it.current_x = pt_x;
21527 }
21528 else
21529 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21530 pixel_width = it.pixel_width;
21531 if (overshoot_expected && at_eol_p)
21532 pixel_width = 0;
21533 else if (pixel_width <= 0)
21534 pixel_width = 1;
21535
21536 /* If there's a display string (or something similar) at point,
21537 we are actually at the glyph to the left of point, so we need
21538 to correct the X coordinate. */
21539 if (overshoot_expected)
21540 {
21541 if (it.bidi_p)
21542 pt_x += pixel_width * it.bidi_it.scan_dir;
21543 else
21544 pt_x += pixel_width;
21545 }
21546
21547 /* Compute target X coordinate, either to the left or to the
21548 right of point. On TTY frames, all characters have the same
21549 pixel width of 1, so we can use that. On GUI frames we don't
21550 have an easy way of getting at the pixel width of the
21551 character to the left of point, so we use a different method
21552 of getting to that place. */
21553 if (dir > 0)
21554 target_x = pt_x + pixel_width;
21555 else
21556 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21557
21558 /* Target X coordinate could be one line above or below the line
21559 of point, in which case we need to adjust the target X
21560 coordinate. Also, if moving to the left, we need to begin at
21561 the left edge of the point's screen line. */
21562 if (dir < 0)
21563 {
21564 if (pt_x > 0)
21565 {
21566 start_display (&it, w, pt);
21567 if (it.line_wrap == TRUNCATE)
21568 it.last_visible_x = INFINITY;
21569 reseat_at_previous_visible_line_start (&it);
21570 it.current_x = it.current_y = it.hpos = 0;
21571 if (pt_vpos != 0)
21572 move_it_by_lines (&it, pt_vpos);
21573 }
21574 else
21575 {
21576 move_it_by_lines (&it, -1);
21577 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21578 target_is_eol_p = true;
21579 /* Under word-wrap, we don't know the x coordinate of
21580 the last character displayed on the previous line,
21581 which immediately precedes the wrap point. To find
21582 out its x coordinate, we try moving to the right
21583 margin of the window, which will stop at the wrap
21584 point, and then reset target_x to point at the
21585 character that precedes the wrap point. This is not
21586 needed on GUI frames, because (see below) there we
21587 move from the left margin one grapheme cluster at a
21588 time, and stop when we hit the wrap point. */
21589 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21590 {
21591 void *it_data = NULL;
21592 struct it it2;
21593
21594 SAVE_IT (it2, it, it_data);
21595 move_it_in_display_line_to (&it, ZV, target_x,
21596 MOVE_TO_POS | MOVE_TO_X);
21597 /* If we arrived at target_x, that _is_ the last
21598 character on the previous line. */
21599 if (it.current_x != target_x)
21600 target_x = it.current_x - 1;
21601 RESTORE_IT (&it, &it2, it_data);
21602 }
21603 }
21604 }
21605 else
21606 {
21607 if (at_eol_p
21608 || (target_x >= it.last_visible_x
21609 && it.line_wrap != TRUNCATE))
21610 {
21611 if (pt_x > 0)
21612 move_it_by_lines (&it, 0);
21613 move_it_by_lines (&it, 1);
21614 target_x = 0;
21615 }
21616 }
21617
21618 /* Move to the target X coordinate. */
21619 #ifdef HAVE_WINDOW_SYSTEM
21620 /* On GUI frames, as we don't know the X coordinate of the
21621 character to the left of point, moving point to the left
21622 requires walking, one grapheme cluster at a time, until we
21623 find ourself at a place immediately to the left of the
21624 character at point. */
21625 if (FRAME_WINDOW_P (it.f) && dir < 0)
21626 {
21627 struct text_pos new_pos;
21628 enum move_it_result rc = MOVE_X_REACHED;
21629
21630 if (it.current_x == 0)
21631 get_next_display_element (&it);
21632 if (it.what == IT_COMPOSITION)
21633 {
21634 new_pos.charpos = it.cmp_it.charpos;
21635 new_pos.bytepos = -1;
21636 }
21637 else
21638 new_pos = it.current.pos;
21639
21640 while (it.current_x + it.pixel_width <= target_x
21641 && (rc == MOVE_X_REACHED
21642 /* Under word-wrap, move_it_in_display_line_to
21643 stops at correct coordinates, but sometimes
21644 returns MOVE_POS_MATCH_OR_ZV. */
21645 || (it.line_wrap == WORD_WRAP
21646 && rc == MOVE_POS_MATCH_OR_ZV)))
21647 {
21648 int new_x = it.current_x + it.pixel_width;
21649
21650 /* For composed characters, we want the position of the
21651 first character in the grapheme cluster (usually, the
21652 composition's base character), whereas it.current
21653 might give us the position of the _last_ one, e.g. if
21654 the composition is rendered in reverse due to bidi
21655 reordering. */
21656 if (it.what == IT_COMPOSITION)
21657 {
21658 new_pos.charpos = it.cmp_it.charpos;
21659 new_pos.bytepos = -1;
21660 }
21661 else
21662 new_pos = it.current.pos;
21663 if (new_x == it.current_x)
21664 new_x++;
21665 rc = move_it_in_display_line_to (&it, ZV, new_x,
21666 MOVE_TO_POS | MOVE_TO_X);
21667 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21668 break;
21669 }
21670 /* The previous position we saw in the loop is the one we
21671 want. */
21672 if (new_pos.bytepos == -1)
21673 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21674 it.current.pos = new_pos;
21675 }
21676 else
21677 #endif
21678 if (it.current_x != target_x)
21679 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21680
21681 /* If we ended up in a display string that covers point, move to
21682 buffer position to the right in the visual order. */
21683 if (dir > 0)
21684 {
21685 while (IT_CHARPOS (it) == PT)
21686 {
21687 set_iterator_to_next (&it, false);
21688 if (!get_next_display_element (&it))
21689 break;
21690 }
21691 }
21692
21693 /* Move point to that position. */
21694 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21695 }
21696
21697 return make_number (PT);
21698
21699 #undef ROW_GLYPH_NEWLINE_P
21700 }
21701
21702 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21703 Sbidi_resolved_levels, 0, 1, 0,
21704 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21705
21706 The resolved levels are produced by the Emacs bidi reordering engine
21707 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21708 read the Unicode Standard Annex 9 (UAX#9) for background information
21709 about these levels.
21710
21711 VPOS is the zero-based number of the current window's screen line
21712 for which to produce the resolved levels. If VPOS is nil or omitted,
21713 it defaults to the screen line of point. If the window displays a
21714 header line, VPOS of zero will report on the header line, and first
21715 line of text in the window will have VPOS of 1.
21716
21717 Value is an array of resolved levels, indexed by glyph number.
21718 Glyphs are numbered from zero starting from the beginning of the
21719 screen line, i.e. the left edge of the window for left-to-right lines
21720 and from the right edge for right-to-left lines. The resolved levels
21721 are produced only for the window's text area; text in display margins
21722 is not included.
21723
21724 If the selected window's display is not up-to-date, or if the specified
21725 screen line does not display text, this function returns nil. It is
21726 highly recommended to bind this function to some simple key, like F8,
21727 in order to avoid these problems.
21728
21729 This function exists mainly for testing the correctness of the
21730 Emacs UBA implementation, in particular with the test suite. */)
21731 (Lisp_Object vpos)
21732 {
21733 struct window *w = XWINDOW (selected_window);
21734 struct buffer *b = XBUFFER (w->contents);
21735 int nrow;
21736 struct glyph_row *row;
21737
21738 if (NILP (vpos))
21739 {
21740 int d1, d2, d3, d4, d5;
21741
21742 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21743 }
21744 else
21745 {
21746 CHECK_NUMBER_COERCE_MARKER (vpos);
21747 nrow = XINT (vpos);
21748 }
21749
21750 /* We require up-to-date glyph matrix for this window. */
21751 if (w->window_end_valid
21752 && !windows_or_buffers_changed
21753 && b
21754 && !b->clip_changed
21755 && !b->prevent_redisplay_optimizations_p
21756 && !window_outdated (w)
21757 && nrow >= 0
21758 && nrow < w->current_matrix->nrows
21759 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21760 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21761 {
21762 struct glyph *g, *e, *g1;
21763 int nglyphs, i;
21764 Lisp_Object levels;
21765
21766 if (!row->reversed_p) /* Left-to-right glyph row. */
21767 {
21768 g = g1 = row->glyphs[TEXT_AREA];
21769 e = g + row->used[TEXT_AREA];
21770
21771 /* Skip over glyphs at the start of the row that was
21772 generated by redisplay for its own needs. */
21773 while (g < e
21774 && NILP (g->object)
21775 && g->charpos < 0)
21776 g++;
21777 g1 = g;
21778
21779 /* Count the "interesting" glyphs in this row. */
21780 for (nglyphs = 0; g < e && !NILP (g->object); g++)
21781 nglyphs++;
21782
21783 /* Create and fill the array. */
21784 levels = make_uninit_vector (nglyphs);
21785 for (i = 0; g1 < g; i++, g1++)
21786 ASET (levels, i, make_number (g1->resolved_level));
21787 }
21788 else /* Right-to-left glyph row. */
21789 {
21790 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21791 e = row->glyphs[TEXT_AREA] - 1;
21792 while (g > e
21793 && NILP (g->object)
21794 && g->charpos < 0)
21795 g--;
21796 g1 = g;
21797 for (nglyphs = 0; g > e && !NILP (g->object); g--)
21798 nglyphs++;
21799 levels = make_uninit_vector (nglyphs);
21800 for (i = 0; g1 > g; i++, g1--)
21801 ASET (levels, i, make_number (g1->resolved_level));
21802 }
21803 return levels;
21804 }
21805 else
21806 return Qnil;
21807 }
21808
21809
21810 \f
21811 /***********************************************************************
21812 Menu Bar
21813 ***********************************************************************/
21814
21815 /* Redisplay the menu bar in the frame for window W.
21816
21817 The menu bar of X frames that don't have X toolkit support is
21818 displayed in a special window W->frame->menu_bar_window.
21819
21820 The menu bar of terminal frames is treated specially as far as
21821 glyph matrices are concerned. Menu bar lines are not part of
21822 windows, so the update is done directly on the frame matrix rows
21823 for the menu bar. */
21824
21825 static void
21826 display_menu_bar (struct window *w)
21827 {
21828 struct frame *f = XFRAME (WINDOW_FRAME (w));
21829 struct it it;
21830 Lisp_Object items;
21831 int i;
21832
21833 /* Don't do all this for graphical frames. */
21834 #ifdef HAVE_NTGUI
21835 if (FRAME_W32_P (f))
21836 return;
21837 #endif
21838 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21839 if (FRAME_X_P (f))
21840 return;
21841 #endif
21842
21843 #ifdef HAVE_NS
21844 if (FRAME_NS_P (f))
21845 return;
21846 #endif /* HAVE_NS */
21847
21848 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21849 eassert (!FRAME_WINDOW_P (f));
21850 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21851 it.first_visible_x = 0;
21852 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21853 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21854 if (FRAME_WINDOW_P (f))
21855 {
21856 /* Menu bar lines are displayed in the desired matrix of the
21857 dummy window menu_bar_window. */
21858 struct window *menu_w;
21859 menu_w = XWINDOW (f->menu_bar_window);
21860 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21861 MENU_FACE_ID);
21862 it.first_visible_x = 0;
21863 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21864 }
21865 else
21866 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21867 {
21868 /* This is a TTY frame, i.e. character hpos/vpos are used as
21869 pixel x/y. */
21870 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21871 MENU_FACE_ID);
21872 it.first_visible_x = 0;
21873 it.last_visible_x = FRAME_COLS (f);
21874 }
21875
21876 /* FIXME: This should be controlled by a user option. See the
21877 comments in redisplay_tool_bar and display_mode_line about
21878 this. */
21879 it.paragraph_embedding = L2R;
21880
21881 /* Clear all rows of the menu bar. */
21882 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21883 {
21884 struct glyph_row *row = it.glyph_row + i;
21885 clear_glyph_row (row);
21886 row->enabled_p = true;
21887 row->full_width_p = true;
21888 row->reversed_p = false;
21889 }
21890
21891 /* Display all items of the menu bar. */
21892 items = FRAME_MENU_BAR_ITEMS (it.f);
21893 for (i = 0; i < ASIZE (items); i += 4)
21894 {
21895 Lisp_Object string;
21896
21897 /* Stop at nil string. */
21898 string = AREF (items, i + 1);
21899 if (NILP (string))
21900 break;
21901
21902 /* Remember where item was displayed. */
21903 ASET (items, i + 3, make_number (it.hpos));
21904
21905 /* Display the item, pad with one space. */
21906 if (it.current_x < it.last_visible_x)
21907 display_string (NULL, string, Qnil, 0, 0, &it,
21908 SCHARS (string) + 1, 0, 0, -1);
21909 }
21910
21911 /* Fill out the line with spaces. */
21912 if (it.current_x < it.last_visible_x)
21913 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21914
21915 /* Compute the total height of the lines. */
21916 compute_line_metrics (&it);
21917 }
21918
21919 /* Deep copy of a glyph row, including the glyphs. */
21920 static void
21921 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21922 {
21923 struct glyph *pointers[1 + LAST_AREA];
21924 int to_used = to->used[TEXT_AREA];
21925
21926 /* Save glyph pointers of TO. */
21927 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21928
21929 /* Do a structure assignment. */
21930 *to = *from;
21931
21932 /* Restore original glyph pointers of TO. */
21933 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21934
21935 /* Copy the glyphs. */
21936 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21937 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21938
21939 /* If we filled only part of the TO row, fill the rest with
21940 space_glyph (which will display as empty space). */
21941 if (to_used > from->used[TEXT_AREA])
21942 fill_up_frame_row_with_spaces (to, to_used);
21943 }
21944
21945 /* Display one menu item on a TTY, by overwriting the glyphs in the
21946 frame F's desired glyph matrix with glyphs produced from the menu
21947 item text. Called from term.c to display TTY drop-down menus one
21948 item at a time.
21949
21950 ITEM_TEXT is the menu item text as a C string.
21951
21952 FACE_ID is the face ID to be used for this menu item. FACE_ID
21953 could specify one of 3 faces: a face for an enabled item, a face
21954 for a disabled item, or a face for a selected item.
21955
21956 X and Y are coordinates of the first glyph in the frame's desired
21957 matrix to be overwritten by the menu item. Since this is a TTY, Y
21958 is the zero-based number of the glyph row and X is the zero-based
21959 glyph number in the row, starting from left, where to start
21960 displaying the item.
21961
21962 SUBMENU means this menu item drops down a submenu, which
21963 should be indicated by displaying a proper visual cue after the
21964 item text. */
21965
21966 void
21967 display_tty_menu_item (const char *item_text, int width, int face_id,
21968 int x, int y, bool submenu)
21969 {
21970 struct it it;
21971 struct frame *f = SELECTED_FRAME ();
21972 struct window *w = XWINDOW (f->selected_window);
21973 struct glyph_row *row;
21974 size_t item_len = strlen (item_text);
21975
21976 eassert (FRAME_TERMCAP_P (f));
21977
21978 /* Don't write beyond the matrix's last row. This can happen for
21979 TTY screens that are not high enough to show the entire menu.
21980 (This is actually a bit of defensive programming, as
21981 tty_menu_display already limits the number of menu items to one
21982 less than the number of screen lines.) */
21983 if (y >= f->desired_matrix->nrows)
21984 return;
21985
21986 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21987 it.first_visible_x = 0;
21988 it.last_visible_x = FRAME_COLS (f) - 1;
21989 row = it.glyph_row;
21990 /* Start with the row contents from the current matrix. */
21991 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21992 bool saved_width = row->full_width_p;
21993 row->full_width_p = true;
21994 bool saved_reversed = row->reversed_p;
21995 row->reversed_p = false;
21996 row->enabled_p = true;
21997
21998 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21999 desired face. */
22000 eassert (x < f->desired_matrix->matrix_w);
22001 it.current_x = it.hpos = x;
22002 it.current_y = it.vpos = y;
22003 int saved_used = row->used[TEXT_AREA];
22004 bool saved_truncated = row->truncated_on_right_p;
22005 row->used[TEXT_AREA] = x;
22006 it.face_id = face_id;
22007 it.line_wrap = TRUNCATE;
22008
22009 /* FIXME: This should be controlled by a user option. See the
22010 comments in redisplay_tool_bar and display_mode_line about this.
22011 Also, if paragraph_embedding could ever be R2L, changes will be
22012 needed to avoid shifting to the right the row characters in
22013 term.c:append_glyph. */
22014 it.paragraph_embedding = L2R;
22015
22016 /* Pad with a space on the left. */
22017 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22018 width--;
22019 /* Display the menu item, pad with spaces to WIDTH. */
22020 if (submenu)
22021 {
22022 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22023 item_len, 0, FRAME_COLS (f) - 1, -1);
22024 width -= item_len;
22025 /* Indicate with " >" that there's a submenu. */
22026 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22027 FRAME_COLS (f) - 1, -1);
22028 }
22029 else
22030 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22031 width, 0, FRAME_COLS (f) - 1, -1);
22032
22033 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22034 row->truncated_on_right_p = saved_truncated;
22035 row->hash = row_hash (row);
22036 row->full_width_p = saved_width;
22037 row->reversed_p = saved_reversed;
22038 }
22039 \f
22040 /***********************************************************************
22041 Mode Line
22042 ***********************************************************************/
22043
22044 /* Redisplay mode lines in the window tree whose root is WINDOW.
22045 If FORCE, redisplay mode lines unconditionally.
22046 Otherwise, redisplay only mode lines that are garbaged. Value is
22047 the number of windows whose mode lines were redisplayed. */
22048
22049 static int
22050 redisplay_mode_lines (Lisp_Object window, bool force)
22051 {
22052 int nwindows = 0;
22053
22054 while (!NILP (window))
22055 {
22056 struct window *w = XWINDOW (window);
22057
22058 if (WINDOWP (w->contents))
22059 nwindows += redisplay_mode_lines (w->contents, force);
22060 else if (force
22061 || FRAME_GARBAGED_P (XFRAME (w->frame))
22062 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22063 {
22064 struct text_pos lpoint;
22065 struct buffer *old = current_buffer;
22066
22067 /* Set the window's buffer for the mode line display. */
22068 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22069 set_buffer_internal_1 (XBUFFER (w->contents));
22070
22071 /* Point refers normally to the selected window. For any
22072 other window, set up appropriate value. */
22073 if (!EQ (window, selected_window))
22074 {
22075 struct text_pos pt;
22076
22077 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22078 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22079 }
22080
22081 /* Display mode lines. */
22082 clear_glyph_matrix (w->desired_matrix);
22083 if (display_mode_lines (w))
22084 ++nwindows;
22085
22086 /* Restore old settings. */
22087 set_buffer_internal_1 (old);
22088 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22089 }
22090
22091 window = w->next;
22092 }
22093
22094 return nwindows;
22095 }
22096
22097
22098 /* Display the mode and/or header line of window W. Value is the
22099 sum number of mode lines and header lines displayed. */
22100
22101 static int
22102 display_mode_lines (struct window *w)
22103 {
22104 Lisp_Object old_selected_window = selected_window;
22105 Lisp_Object old_selected_frame = selected_frame;
22106 Lisp_Object new_frame = w->frame;
22107 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22108 int n = 0;
22109
22110 selected_frame = new_frame;
22111 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22112 or window's point, then we'd need select_window_1 here as well. */
22113 XSETWINDOW (selected_window, w);
22114 XFRAME (new_frame)->selected_window = selected_window;
22115
22116 /* These will be set while the mode line specs are processed. */
22117 line_number_displayed = false;
22118 w->column_number_displayed = -1;
22119
22120 if (WINDOW_WANTS_MODELINE_P (w))
22121 {
22122 struct window *sel_w = XWINDOW (old_selected_window);
22123
22124 /* Select mode line face based on the real selected window. */
22125 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22126 BVAR (current_buffer, mode_line_format));
22127 ++n;
22128 }
22129
22130 if (WINDOW_WANTS_HEADER_LINE_P (w))
22131 {
22132 display_mode_line (w, HEADER_LINE_FACE_ID,
22133 BVAR (current_buffer, header_line_format));
22134 ++n;
22135 }
22136
22137 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22138 selected_frame = old_selected_frame;
22139 selected_window = old_selected_window;
22140 if (n > 0)
22141 w->must_be_updated_p = true;
22142 return n;
22143 }
22144
22145
22146 /* Display mode or header line of window W. FACE_ID specifies which
22147 line to display; it is either MODE_LINE_FACE_ID or
22148 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22149 display. Value is the pixel height of the mode/header line
22150 displayed. */
22151
22152 static int
22153 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22154 {
22155 struct it it;
22156 struct face *face;
22157 ptrdiff_t count = SPECPDL_INDEX ();
22158
22159 init_iterator (&it, w, -1, -1, NULL, face_id);
22160 /* Don't extend on a previously drawn mode-line.
22161 This may happen if called from pos_visible_p. */
22162 it.glyph_row->enabled_p = false;
22163 prepare_desired_row (w, it.glyph_row, true);
22164
22165 it.glyph_row->mode_line_p = true;
22166
22167 /* FIXME: This should be controlled by a user option. But
22168 supporting such an option is not trivial, since the mode line is
22169 made up of many separate strings. */
22170 it.paragraph_embedding = L2R;
22171
22172 record_unwind_protect (unwind_format_mode_line,
22173 format_mode_line_unwind_data (NULL, NULL,
22174 Qnil, false));
22175
22176 mode_line_target = MODE_LINE_DISPLAY;
22177
22178 /* Temporarily make frame's keyboard the current kboard so that
22179 kboard-local variables in the mode_line_format will get the right
22180 values. */
22181 push_kboard (FRAME_KBOARD (it.f));
22182 record_unwind_save_match_data ();
22183 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22184 pop_kboard ();
22185
22186 unbind_to (count, Qnil);
22187
22188 /* Fill up with spaces. */
22189 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22190
22191 compute_line_metrics (&it);
22192 it.glyph_row->full_width_p = true;
22193 it.glyph_row->continued_p = false;
22194 it.glyph_row->truncated_on_left_p = false;
22195 it.glyph_row->truncated_on_right_p = false;
22196
22197 /* Make a 3D mode-line have a shadow at its right end. */
22198 face = FACE_FROM_ID (it.f, face_id);
22199 extend_face_to_end_of_line (&it);
22200 if (face->box != FACE_NO_BOX)
22201 {
22202 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22203 + it.glyph_row->used[TEXT_AREA] - 1);
22204 last->right_box_line_p = true;
22205 }
22206
22207 return it.glyph_row->height;
22208 }
22209
22210 /* Move element ELT in LIST to the front of LIST.
22211 Return the updated list. */
22212
22213 static Lisp_Object
22214 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22215 {
22216 register Lisp_Object tail, prev;
22217 register Lisp_Object tem;
22218
22219 tail = list;
22220 prev = Qnil;
22221 while (CONSP (tail))
22222 {
22223 tem = XCAR (tail);
22224
22225 if (EQ (elt, tem))
22226 {
22227 /* Splice out the link TAIL. */
22228 if (NILP (prev))
22229 list = XCDR (tail);
22230 else
22231 Fsetcdr (prev, XCDR (tail));
22232
22233 /* Now make it the first. */
22234 Fsetcdr (tail, list);
22235 return tail;
22236 }
22237 else
22238 prev = tail;
22239 tail = XCDR (tail);
22240 QUIT;
22241 }
22242
22243 /* Not found--return unchanged LIST. */
22244 return list;
22245 }
22246
22247 /* Contribute ELT to the mode line for window IT->w. How it
22248 translates into text depends on its data type.
22249
22250 IT describes the display environment in which we display, as usual.
22251
22252 DEPTH is the depth in recursion. It is used to prevent
22253 infinite recursion here.
22254
22255 FIELD_WIDTH is the number of characters the display of ELT should
22256 occupy in the mode line, and PRECISION is the maximum number of
22257 characters to display from ELT's representation. See
22258 display_string for details.
22259
22260 Returns the hpos of the end of the text generated by ELT.
22261
22262 PROPS is a property list to add to any string we encounter.
22263
22264 If RISKY, remove (disregard) any properties in any string
22265 we encounter, and ignore :eval and :propertize.
22266
22267 The global variable `mode_line_target' determines whether the
22268 output is passed to `store_mode_line_noprop',
22269 `store_mode_line_string', or `display_string'. */
22270
22271 static int
22272 display_mode_element (struct it *it, int depth, int field_width, int precision,
22273 Lisp_Object elt, Lisp_Object props, bool risky)
22274 {
22275 int n = 0, field, prec;
22276 bool literal = false;
22277
22278 tail_recurse:
22279 if (depth > 100)
22280 elt = build_string ("*too-deep*");
22281
22282 depth++;
22283
22284 switch (XTYPE (elt))
22285 {
22286 case Lisp_String:
22287 {
22288 /* A string: output it and check for %-constructs within it. */
22289 unsigned char c;
22290 ptrdiff_t offset = 0;
22291
22292 if (SCHARS (elt) > 0
22293 && (!NILP (props) || risky))
22294 {
22295 Lisp_Object oprops, aelt;
22296 oprops = Ftext_properties_at (make_number (0), elt);
22297
22298 /* If the starting string's properties are not what
22299 we want, translate the string. Also, if the string
22300 is risky, do that anyway. */
22301
22302 if (NILP (Fequal (props, oprops)) || risky)
22303 {
22304 /* If the starting string has properties,
22305 merge the specified ones onto the existing ones. */
22306 if (! NILP (oprops) && !risky)
22307 {
22308 Lisp_Object tem;
22309
22310 oprops = Fcopy_sequence (oprops);
22311 tem = props;
22312 while (CONSP (tem))
22313 {
22314 oprops = Fplist_put (oprops, XCAR (tem),
22315 XCAR (XCDR (tem)));
22316 tem = XCDR (XCDR (tem));
22317 }
22318 props = oprops;
22319 }
22320
22321 aelt = Fassoc (elt, mode_line_proptrans_alist);
22322 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22323 {
22324 /* AELT is what we want. Move it to the front
22325 without consing. */
22326 elt = XCAR (aelt);
22327 mode_line_proptrans_alist
22328 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22329 }
22330 else
22331 {
22332 Lisp_Object tem;
22333
22334 /* If AELT has the wrong props, it is useless.
22335 so get rid of it. */
22336 if (! NILP (aelt))
22337 mode_line_proptrans_alist
22338 = Fdelq (aelt, mode_line_proptrans_alist);
22339
22340 elt = Fcopy_sequence (elt);
22341 Fset_text_properties (make_number (0), Flength (elt),
22342 props, elt);
22343 /* Add this item to mode_line_proptrans_alist. */
22344 mode_line_proptrans_alist
22345 = Fcons (Fcons (elt, props),
22346 mode_line_proptrans_alist);
22347 /* Truncate mode_line_proptrans_alist
22348 to at most 50 elements. */
22349 tem = Fnthcdr (make_number (50),
22350 mode_line_proptrans_alist);
22351 if (! NILP (tem))
22352 XSETCDR (tem, Qnil);
22353 }
22354 }
22355 }
22356
22357 offset = 0;
22358
22359 if (literal)
22360 {
22361 prec = precision - n;
22362 switch (mode_line_target)
22363 {
22364 case MODE_LINE_NOPROP:
22365 case MODE_LINE_TITLE:
22366 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22367 break;
22368 case MODE_LINE_STRING:
22369 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22370 break;
22371 case MODE_LINE_DISPLAY:
22372 n += display_string (NULL, elt, Qnil, 0, 0, it,
22373 0, prec, 0, STRING_MULTIBYTE (elt));
22374 break;
22375 }
22376
22377 break;
22378 }
22379
22380 /* Handle the non-literal case. */
22381
22382 while ((precision <= 0 || n < precision)
22383 && SREF (elt, offset) != 0
22384 && (mode_line_target != MODE_LINE_DISPLAY
22385 || it->current_x < it->last_visible_x))
22386 {
22387 ptrdiff_t last_offset = offset;
22388
22389 /* Advance to end of string or next format specifier. */
22390 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22391 ;
22392
22393 if (offset - 1 != last_offset)
22394 {
22395 ptrdiff_t nchars, nbytes;
22396
22397 /* Output to end of string or up to '%'. Field width
22398 is length of string. Don't output more than
22399 PRECISION allows us. */
22400 offset--;
22401
22402 prec = c_string_width (SDATA (elt) + last_offset,
22403 offset - last_offset, precision - n,
22404 &nchars, &nbytes);
22405
22406 switch (mode_line_target)
22407 {
22408 case MODE_LINE_NOPROP:
22409 case MODE_LINE_TITLE:
22410 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22411 break;
22412 case MODE_LINE_STRING:
22413 {
22414 ptrdiff_t bytepos = last_offset;
22415 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22416 ptrdiff_t endpos = (precision <= 0
22417 ? string_byte_to_char (elt, offset)
22418 : charpos + nchars);
22419 Lisp_Object mode_string
22420 = Fsubstring (elt, make_number (charpos),
22421 make_number (endpos));
22422 n += store_mode_line_string (NULL, mode_string, false,
22423 0, 0, Qnil);
22424 }
22425 break;
22426 case MODE_LINE_DISPLAY:
22427 {
22428 ptrdiff_t bytepos = last_offset;
22429 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22430
22431 if (precision <= 0)
22432 nchars = string_byte_to_char (elt, offset) - charpos;
22433 n += display_string (NULL, elt, Qnil, 0, charpos,
22434 it, 0, nchars, 0,
22435 STRING_MULTIBYTE (elt));
22436 }
22437 break;
22438 }
22439 }
22440 else /* c == '%' */
22441 {
22442 ptrdiff_t percent_position = offset;
22443
22444 /* Get the specified minimum width. Zero means
22445 don't pad. */
22446 field = 0;
22447 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22448 field = field * 10 + c - '0';
22449
22450 /* Don't pad beyond the total padding allowed. */
22451 if (field_width - n > 0 && field > field_width - n)
22452 field = field_width - n;
22453
22454 /* Note that either PRECISION <= 0 or N < PRECISION. */
22455 prec = precision - n;
22456
22457 if (c == 'M')
22458 n += display_mode_element (it, depth, field, prec,
22459 Vglobal_mode_string, props,
22460 risky);
22461 else if (c != 0)
22462 {
22463 bool multibyte;
22464 ptrdiff_t bytepos, charpos;
22465 const char *spec;
22466 Lisp_Object string;
22467
22468 bytepos = percent_position;
22469 charpos = (STRING_MULTIBYTE (elt)
22470 ? string_byte_to_char (elt, bytepos)
22471 : bytepos);
22472 spec = decode_mode_spec (it->w, c, field, &string);
22473 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22474
22475 switch (mode_line_target)
22476 {
22477 case MODE_LINE_NOPROP:
22478 case MODE_LINE_TITLE:
22479 n += store_mode_line_noprop (spec, field, prec);
22480 break;
22481 case MODE_LINE_STRING:
22482 {
22483 Lisp_Object tem = build_string (spec);
22484 props = Ftext_properties_at (make_number (charpos), elt);
22485 /* Should only keep face property in props */
22486 n += store_mode_line_string (NULL, tem, false,
22487 field, prec, props);
22488 }
22489 break;
22490 case MODE_LINE_DISPLAY:
22491 {
22492 int nglyphs_before, nwritten;
22493
22494 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22495 nwritten = display_string (spec, string, elt,
22496 charpos, 0, it,
22497 field, prec, 0,
22498 multibyte);
22499
22500 /* Assign to the glyphs written above the
22501 string where the `%x' came from, position
22502 of the `%'. */
22503 if (nwritten > 0)
22504 {
22505 struct glyph *glyph
22506 = (it->glyph_row->glyphs[TEXT_AREA]
22507 + nglyphs_before);
22508 int i;
22509
22510 for (i = 0; i < nwritten; ++i)
22511 {
22512 glyph[i].object = elt;
22513 glyph[i].charpos = charpos;
22514 }
22515
22516 n += nwritten;
22517 }
22518 }
22519 break;
22520 }
22521 }
22522 else /* c == 0 */
22523 break;
22524 }
22525 }
22526 }
22527 break;
22528
22529 case Lisp_Symbol:
22530 /* A symbol: process the value of the symbol recursively
22531 as if it appeared here directly. Avoid error if symbol void.
22532 Special case: if value of symbol is a string, output the string
22533 literally. */
22534 {
22535 register Lisp_Object tem;
22536
22537 /* If the variable is not marked as risky to set
22538 then its contents are risky to use. */
22539 if (NILP (Fget (elt, Qrisky_local_variable)))
22540 risky = true;
22541
22542 tem = Fboundp (elt);
22543 if (!NILP (tem))
22544 {
22545 tem = Fsymbol_value (elt);
22546 /* If value is a string, output that string literally:
22547 don't check for % within it. */
22548 if (STRINGP (tem))
22549 literal = true;
22550
22551 if (!EQ (tem, elt))
22552 {
22553 /* Give up right away for nil or t. */
22554 elt = tem;
22555 goto tail_recurse;
22556 }
22557 }
22558 }
22559 break;
22560
22561 case Lisp_Cons:
22562 {
22563 register Lisp_Object car, tem;
22564
22565 /* A cons cell: five distinct cases.
22566 If first element is :eval or :propertize, do something special.
22567 If first element is a string or a cons, process all the elements
22568 and effectively concatenate them.
22569 If first element is a negative number, truncate displaying cdr to
22570 at most that many characters. If positive, pad (with spaces)
22571 to at least that many characters.
22572 If first element is a symbol, process the cadr or caddr recursively
22573 according to whether the symbol's value is non-nil or nil. */
22574 car = XCAR (elt);
22575 if (EQ (car, QCeval))
22576 {
22577 /* An element of the form (:eval FORM) means evaluate FORM
22578 and use the result as mode line elements. */
22579
22580 if (risky)
22581 break;
22582
22583 if (CONSP (XCDR (elt)))
22584 {
22585 Lisp_Object spec;
22586 spec = safe__eval (true, XCAR (XCDR (elt)));
22587 n += display_mode_element (it, depth, field_width - n,
22588 precision - n, spec, props,
22589 risky);
22590 }
22591 }
22592 else if (EQ (car, QCpropertize))
22593 {
22594 /* An element of the form (:propertize ELT PROPS...)
22595 means display ELT but applying properties PROPS. */
22596
22597 if (risky)
22598 break;
22599
22600 if (CONSP (XCDR (elt)))
22601 n += display_mode_element (it, depth, field_width - n,
22602 precision - n, XCAR (XCDR (elt)),
22603 XCDR (XCDR (elt)), risky);
22604 }
22605 else if (SYMBOLP (car))
22606 {
22607 tem = Fboundp (car);
22608 elt = XCDR (elt);
22609 if (!CONSP (elt))
22610 goto invalid;
22611 /* elt is now the cdr, and we know it is a cons cell.
22612 Use its car if CAR has a non-nil value. */
22613 if (!NILP (tem))
22614 {
22615 tem = Fsymbol_value (car);
22616 if (!NILP (tem))
22617 {
22618 elt = XCAR (elt);
22619 goto tail_recurse;
22620 }
22621 }
22622 /* Symbol's value is nil (or symbol is unbound)
22623 Get the cddr of the original list
22624 and if possible find the caddr and use that. */
22625 elt = XCDR (elt);
22626 if (NILP (elt))
22627 break;
22628 else if (!CONSP (elt))
22629 goto invalid;
22630 elt = XCAR (elt);
22631 goto tail_recurse;
22632 }
22633 else if (INTEGERP (car))
22634 {
22635 register int lim = XINT (car);
22636 elt = XCDR (elt);
22637 if (lim < 0)
22638 {
22639 /* Negative int means reduce maximum width. */
22640 if (precision <= 0)
22641 precision = -lim;
22642 else
22643 precision = min (precision, -lim);
22644 }
22645 else if (lim > 0)
22646 {
22647 /* Padding specified. Don't let it be more than
22648 current maximum. */
22649 if (precision > 0)
22650 lim = min (precision, lim);
22651
22652 /* If that's more padding than already wanted, queue it.
22653 But don't reduce padding already specified even if
22654 that is beyond the current truncation point. */
22655 field_width = max (lim, field_width);
22656 }
22657 goto tail_recurse;
22658 }
22659 else if (STRINGP (car) || CONSP (car))
22660 {
22661 Lisp_Object halftail = elt;
22662 int len = 0;
22663
22664 while (CONSP (elt)
22665 && (precision <= 0 || n < precision))
22666 {
22667 n += display_mode_element (it, depth,
22668 /* Do padding only after the last
22669 element in the list. */
22670 (! CONSP (XCDR (elt))
22671 ? field_width - n
22672 : 0),
22673 precision - n, XCAR (elt),
22674 props, risky);
22675 elt = XCDR (elt);
22676 len++;
22677 if ((len & 1) == 0)
22678 halftail = XCDR (halftail);
22679 /* Check for cycle. */
22680 if (EQ (halftail, elt))
22681 break;
22682 }
22683 }
22684 }
22685 break;
22686
22687 default:
22688 invalid:
22689 elt = build_string ("*invalid*");
22690 goto tail_recurse;
22691 }
22692
22693 /* Pad to FIELD_WIDTH. */
22694 if (field_width > 0 && n < field_width)
22695 {
22696 switch (mode_line_target)
22697 {
22698 case MODE_LINE_NOPROP:
22699 case MODE_LINE_TITLE:
22700 n += store_mode_line_noprop ("", field_width - n, 0);
22701 break;
22702 case MODE_LINE_STRING:
22703 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
22704 Qnil);
22705 break;
22706 case MODE_LINE_DISPLAY:
22707 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22708 0, 0, 0);
22709 break;
22710 }
22711 }
22712
22713 return n;
22714 }
22715
22716 /* Store a mode-line string element in mode_line_string_list.
22717
22718 If STRING is non-null, display that C string. Otherwise, the Lisp
22719 string LISP_STRING is displayed.
22720
22721 FIELD_WIDTH is the minimum number of output glyphs to produce.
22722 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22723 with spaces. FIELD_WIDTH <= 0 means don't pad.
22724
22725 PRECISION is the maximum number of characters to output from
22726 STRING. PRECISION <= 0 means don't truncate the string.
22727
22728 If COPY_STRING, make a copy of LISP_STRING before adding
22729 properties to the string.
22730
22731 PROPS are the properties to add to the string.
22732 The mode_line_string_face face property is always added to the string.
22733 */
22734
22735 static int
22736 store_mode_line_string (const char *string, Lisp_Object lisp_string,
22737 bool copy_string,
22738 int field_width, int precision, Lisp_Object props)
22739 {
22740 ptrdiff_t len;
22741 int n = 0;
22742
22743 if (string != NULL)
22744 {
22745 len = strlen (string);
22746 if (precision > 0 && len > precision)
22747 len = precision;
22748 lisp_string = make_string (string, len);
22749 if (NILP (props))
22750 props = mode_line_string_face_prop;
22751 else if (!NILP (mode_line_string_face))
22752 {
22753 Lisp_Object face = Fplist_get (props, Qface);
22754 props = Fcopy_sequence (props);
22755 if (NILP (face))
22756 face = mode_line_string_face;
22757 else
22758 face = list2 (face, mode_line_string_face);
22759 props = Fplist_put (props, Qface, face);
22760 }
22761 Fadd_text_properties (make_number (0), make_number (len),
22762 props, lisp_string);
22763 }
22764 else
22765 {
22766 len = XFASTINT (Flength (lisp_string));
22767 if (precision > 0 && len > precision)
22768 {
22769 len = precision;
22770 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22771 precision = -1;
22772 }
22773 if (!NILP (mode_line_string_face))
22774 {
22775 Lisp_Object face;
22776 if (NILP (props))
22777 props = Ftext_properties_at (make_number (0), lisp_string);
22778 face = Fplist_get (props, Qface);
22779 if (NILP (face))
22780 face = mode_line_string_face;
22781 else
22782 face = list2 (face, mode_line_string_face);
22783 props = list2 (Qface, face);
22784 if (copy_string)
22785 lisp_string = Fcopy_sequence (lisp_string);
22786 }
22787 if (!NILP (props))
22788 Fadd_text_properties (make_number (0), make_number (len),
22789 props, lisp_string);
22790 }
22791
22792 if (len > 0)
22793 {
22794 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22795 n += len;
22796 }
22797
22798 if (field_width > len)
22799 {
22800 field_width -= len;
22801 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22802 if (!NILP (props))
22803 Fadd_text_properties (make_number (0), make_number (field_width),
22804 props, lisp_string);
22805 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22806 n += field_width;
22807 }
22808
22809 return n;
22810 }
22811
22812
22813 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22814 1, 4, 0,
22815 doc: /* Format a string out of a mode line format specification.
22816 First arg FORMAT specifies the mode line format (see `mode-line-format'
22817 for details) to use.
22818
22819 By default, the format is evaluated for the currently selected window.
22820
22821 Optional second arg FACE specifies the face property to put on all
22822 characters for which no face is specified. The value nil means the
22823 default face. The value t means whatever face the window's mode line
22824 currently uses (either `mode-line' or `mode-line-inactive',
22825 depending on whether the window is the selected window or not).
22826 An integer value means the value string has no text
22827 properties.
22828
22829 Optional third and fourth args WINDOW and BUFFER specify the window
22830 and buffer to use as the context for the formatting (defaults
22831 are the selected window and the WINDOW's buffer). */)
22832 (Lisp_Object format, Lisp_Object face,
22833 Lisp_Object window, Lisp_Object buffer)
22834 {
22835 struct it it;
22836 int len;
22837 struct window *w;
22838 struct buffer *old_buffer = NULL;
22839 int face_id;
22840 bool no_props = INTEGERP (face);
22841 ptrdiff_t count = SPECPDL_INDEX ();
22842 Lisp_Object str;
22843 int string_start = 0;
22844
22845 w = decode_any_window (window);
22846 XSETWINDOW (window, w);
22847
22848 if (NILP (buffer))
22849 buffer = w->contents;
22850 CHECK_BUFFER (buffer);
22851
22852 /* Make formatting the modeline a non-op when noninteractive, otherwise
22853 there will be problems later caused by a partially initialized frame. */
22854 if (NILP (format) || noninteractive)
22855 return empty_unibyte_string;
22856
22857 if (no_props)
22858 face = Qnil;
22859
22860 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22861 : EQ (face, Qt) ? (EQ (window, selected_window)
22862 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22863 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22864 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22865 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22866 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22867 : DEFAULT_FACE_ID;
22868
22869 old_buffer = current_buffer;
22870
22871 /* Save things including mode_line_proptrans_alist,
22872 and set that to nil so that we don't alter the outer value. */
22873 record_unwind_protect (unwind_format_mode_line,
22874 format_mode_line_unwind_data
22875 (XFRAME (WINDOW_FRAME (w)),
22876 old_buffer, selected_window, true));
22877 mode_line_proptrans_alist = Qnil;
22878
22879 Fselect_window (window, Qt);
22880 set_buffer_internal_1 (XBUFFER (buffer));
22881
22882 init_iterator (&it, w, -1, -1, NULL, face_id);
22883
22884 if (no_props)
22885 {
22886 mode_line_target = MODE_LINE_NOPROP;
22887 mode_line_string_face_prop = Qnil;
22888 mode_line_string_list = Qnil;
22889 string_start = MODE_LINE_NOPROP_LEN (0);
22890 }
22891 else
22892 {
22893 mode_line_target = MODE_LINE_STRING;
22894 mode_line_string_list = Qnil;
22895 mode_line_string_face = face;
22896 mode_line_string_face_prop
22897 = NILP (face) ? Qnil : list2 (Qface, face);
22898 }
22899
22900 push_kboard (FRAME_KBOARD (it.f));
22901 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22902 pop_kboard ();
22903
22904 if (no_props)
22905 {
22906 len = MODE_LINE_NOPROP_LEN (string_start);
22907 str = make_string (mode_line_noprop_buf + string_start, len);
22908 }
22909 else
22910 {
22911 mode_line_string_list = Fnreverse (mode_line_string_list);
22912 str = Fmapconcat (Qidentity, mode_line_string_list,
22913 empty_unibyte_string);
22914 }
22915
22916 unbind_to (count, Qnil);
22917 return str;
22918 }
22919
22920 /* Write a null-terminated, right justified decimal representation of
22921 the positive integer D to BUF using a minimal field width WIDTH. */
22922
22923 static void
22924 pint2str (register char *buf, register int width, register ptrdiff_t d)
22925 {
22926 register char *p = buf;
22927
22928 if (d <= 0)
22929 *p++ = '0';
22930 else
22931 {
22932 while (d > 0)
22933 {
22934 *p++ = d % 10 + '0';
22935 d /= 10;
22936 }
22937 }
22938
22939 for (width -= (int) (p - buf); width > 0; --width)
22940 *p++ = ' ';
22941 *p-- = '\0';
22942 while (p > buf)
22943 {
22944 d = *buf;
22945 *buf++ = *p;
22946 *p-- = d;
22947 }
22948 }
22949
22950 /* Write a null-terminated, right justified decimal and "human
22951 readable" representation of the nonnegative integer D to BUF using
22952 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22953
22954 static const char power_letter[] =
22955 {
22956 0, /* no letter */
22957 'k', /* kilo */
22958 'M', /* mega */
22959 'G', /* giga */
22960 'T', /* tera */
22961 'P', /* peta */
22962 'E', /* exa */
22963 'Z', /* zetta */
22964 'Y' /* yotta */
22965 };
22966
22967 static void
22968 pint2hrstr (char *buf, int width, ptrdiff_t d)
22969 {
22970 /* We aim to represent the nonnegative integer D as
22971 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22972 ptrdiff_t quotient = d;
22973 int remainder = 0;
22974 /* -1 means: do not use TENTHS. */
22975 int tenths = -1;
22976 int exponent = 0;
22977
22978 /* Length of QUOTIENT.TENTHS as a string. */
22979 int length;
22980
22981 char * psuffix;
22982 char * p;
22983
22984 if (quotient >= 1000)
22985 {
22986 /* Scale to the appropriate EXPONENT. */
22987 do
22988 {
22989 remainder = quotient % 1000;
22990 quotient /= 1000;
22991 exponent++;
22992 }
22993 while (quotient >= 1000);
22994
22995 /* Round to nearest and decide whether to use TENTHS or not. */
22996 if (quotient <= 9)
22997 {
22998 tenths = remainder / 100;
22999 if (remainder % 100 >= 50)
23000 {
23001 if (tenths < 9)
23002 tenths++;
23003 else
23004 {
23005 quotient++;
23006 if (quotient == 10)
23007 tenths = -1;
23008 else
23009 tenths = 0;
23010 }
23011 }
23012 }
23013 else
23014 if (remainder >= 500)
23015 {
23016 if (quotient < 999)
23017 quotient++;
23018 else
23019 {
23020 quotient = 1;
23021 exponent++;
23022 tenths = 0;
23023 }
23024 }
23025 }
23026
23027 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23028 if (tenths == -1 && quotient <= 99)
23029 if (quotient <= 9)
23030 length = 1;
23031 else
23032 length = 2;
23033 else
23034 length = 3;
23035 p = psuffix = buf + max (width, length);
23036
23037 /* Print EXPONENT. */
23038 *psuffix++ = power_letter[exponent];
23039 *psuffix = '\0';
23040
23041 /* Print TENTHS. */
23042 if (tenths >= 0)
23043 {
23044 *--p = '0' + tenths;
23045 *--p = '.';
23046 }
23047
23048 /* Print QUOTIENT. */
23049 do
23050 {
23051 int digit = quotient % 10;
23052 *--p = '0' + digit;
23053 }
23054 while ((quotient /= 10) != 0);
23055
23056 /* Print leading spaces. */
23057 while (buf < p)
23058 *--p = ' ';
23059 }
23060
23061 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23062 If EOL_FLAG, set also a mnemonic character for end-of-line
23063 type of CODING_SYSTEM. Return updated pointer into BUF. */
23064
23065 static unsigned char invalid_eol_type[] = "(*invalid*)";
23066
23067 static char *
23068 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23069 {
23070 Lisp_Object val;
23071 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23072 const unsigned char *eol_str;
23073 int eol_str_len;
23074 /* The EOL conversion we are using. */
23075 Lisp_Object eoltype;
23076
23077 val = CODING_SYSTEM_SPEC (coding_system);
23078 eoltype = Qnil;
23079
23080 if (!VECTORP (val)) /* Not yet decided. */
23081 {
23082 *buf++ = multibyte ? '-' : ' ';
23083 if (eol_flag)
23084 eoltype = eol_mnemonic_undecided;
23085 /* Don't mention EOL conversion if it isn't decided. */
23086 }
23087 else
23088 {
23089 Lisp_Object attrs;
23090 Lisp_Object eolvalue;
23091
23092 attrs = AREF (val, 0);
23093 eolvalue = AREF (val, 2);
23094
23095 *buf++ = multibyte
23096 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23097 : ' ';
23098
23099 if (eol_flag)
23100 {
23101 /* The EOL conversion that is normal on this system. */
23102
23103 if (NILP (eolvalue)) /* Not yet decided. */
23104 eoltype = eol_mnemonic_undecided;
23105 else if (VECTORP (eolvalue)) /* Not yet decided. */
23106 eoltype = eol_mnemonic_undecided;
23107 else /* eolvalue is Qunix, Qdos, or Qmac. */
23108 eoltype = (EQ (eolvalue, Qunix)
23109 ? eol_mnemonic_unix
23110 : EQ (eolvalue, Qdos)
23111 ? eol_mnemonic_dos : eol_mnemonic_mac);
23112 }
23113 }
23114
23115 if (eol_flag)
23116 {
23117 /* Mention the EOL conversion if it is not the usual one. */
23118 if (STRINGP (eoltype))
23119 {
23120 eol_str = SDATA (eoltype);
23121 eol_str_len = SBYTES (eoltype);
23122 }
23123 else if (CHARACTERP (eoltype))
23124 {
23125 int c = XFASTINT (eoltype);
23126 return buf + CHAR_STRING (c, (unsigned char *) buf);
23127 }
23128 else
23129 {
23130 eol_str = invalid_eol_type;
23131 eol_str_len = sizeof (invalid_eol_type) - 1;
23132 }
23133 memcpy (buf, eol_str, eol_str_len);
23134 buf += eol_str_len;
23135 }
23136
23137 return buf;
23138 }
23139
23140 /* Return a string for the output of a mode line %-spec for window W,
23141 generated by character C. FIELD_WIDTH > 0 means pad the string
23142 returned with spaces to that value. Return a Lisp string in
23143 *STRING if the resulting string is taken from that Lisp string.
23144
23145 Note we operate on the current buffer for most purposes. */
23146
23147 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23148
23149 static const char *
23150 decode_mode_spec (struct window *w, register int c, int field_width,
23151 Lisp_Object *string)
23152 {
23153 Lisp_Object obj;
23154 struct frame *f = XFRAME (WINDOW_FRAME (w));
23155 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23156 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23157 produce strings from numerical values, so limit preposterously
23158 large values of FIELD_WIDTH to avoid overrunning the buffer's
23159 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23160 bytes plus the terminating null. */
23161 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23162 struct buffer *b = current_buffer;
23163
23164 obj = Qnil;
23165 *string = Qnil;
23166
23167 switch (c)
23168 {
23169 case '*':
23170 if (!NILP (BVAR (b, read_only)))
23171 return "%";
23172 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23173 return "*";
23174 return "-";
23175
23176 case '+':
23177 /* This differs from %* only for a modified read-only buffer. */
23178 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23179 return "*";
23180 if (!NILP (BVAR (b, read_only)))
23181 return "%";
23182 return "-";
23183
23184 case '&':
23185 /* This differs from %* in ignoring read-only-ness. */
23186 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23187 return "*";
23188 return "-";
23189
23190 case '%':
23191 return "%";
23192
23193 case '[':
23194 {
23195 int i;
23196 char *p;
23197
23198 if (command_loop_level > 5)
23199 return "[[[... ";
23200 p = decode_mode_spec_buf;
23201 for (i = 0; i < command_loop_level; i++)
23202 *p++ = '[';
23203 *p = 0;
23204 return decode_mode_spec_buf;
23205 }
23206
23207 case ']':
23208 {
23209 int i;
23210 char *p;
23211
23212 if (command_loop_level > 5)
23213 return " ...]]]";
23214 p = decode_mode_spec_buf;
23215 for (i = 0; i < command_loop_level; i++)
23216 *p++ = ']';
23217 *p = 0;
23218 return decode_mode_spec_buf;
23219 }
23220
23221 case '-':
23222 {
23223 register int i;
23224
23225 /* Let lots_of_dashes be a string of infinite length. */
23226 if (mode_line_target == MODE_LINE_NOPROP
23227 || mode_line_target == MODE_LINE_STRING)
23228 return "--";
23229 if (field_width <= 0
23230 || field_width > sizeof (lots_of_dashes))
23231 {
23232 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23233 decode_mode_spec_buf[i] = '-';
23234 decode_mode_spec_buf[i] = '\0';
23235 return decode_mode_spec_buf;
23236 }
23237 else
23238 return lots_of_dashes;
23239 }
23240
23241 case 'b':
23242 obj = BVAR (b, name);
23243 break;
23244
23245 case 'c':
23246 /* %c and %l are ignored in `frame-title-format'.
23247 (In redisplay_internal, the frame title is drawn _before_ the
23248 windows are updated, so the stuff which depends on actual
23249 window contents (such as %l) may fail to render properly, or
23250 even crash emacs.) */
23251 if (mode_line_target == MODE_LINE_TITLE)
23252 return "";
23253 else
23254 {
23255 ptrdiff_t col = current_column ();
23256 w->column_number_displayed = col;
23257 pint2str (decode_mode_spec_buf, width, col);
23258 return decode_mode_spec_buf;
23259 }
23260
23261 case 'e':
23262 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23263 {
23264 if (NILP (Vmemory_full))
23265 return "";
23266 else
23267 return "!MEM FULL! ";
23268 }
23269 #else
23270 return "";
23271 #endif
23272
23273 case 'F':
23274 /* %F displays the frame name. */
23275 if (!NILP (f->title))
23276 return SSDATA (f->title);
23277 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23278 return SSDATA (f->name);
23279 return "Emacs";
23280
23281 case 'f':
23282 obj = BVAR (b, filename);
23283 break;
23284
23285 case 'i':
23286 {
23287 ptrdiff_t size = ZV - BEGV;
23288 pint2str (decode_mode_spec_buf, width, size);
23289 return decode_mode_spec_buf;
23290 }
23291
23292 case 'I':
23293 {
23294 ptrdiff_t size = ZV - BEGV;
23295 pint2hrstr (decode_mode_spec_buf, width, size);
23296 return decode_mode_spec_buf;
23297 }
23298
23299 case 'l':
23300 {
23301 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23302 ptrdiff_t topline, nlines, height;
23303 ptrdiff_t junk;
23304
23305 /* %c and %l are ignored in `frame-title-format'. */
23306 if (mode_line_target == MODE_LINE_TITLE)
23307 return "";
23308
23309 startpos = marker_position (w->start);
23310 startpos_byte = marker_byte_position (w->start);
23311 height = WINDOW_TOTAL_LINES (w);
23312
23313 /* If we decided that this buffer isn't suitable for line numbers,
23314 don't forget that too fast. */
23315 if (w->base_line_pos == -1)
23316 goto no_value;
23317
23318 /* If the buffer is very big, don't waste time. */
23319 if (INTEGERP (Vline_number_display_limit)
23320 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23321 {
23322 w->base_line_pos = 0;
23323 w->base_line_number = 0;
23324 goto no_value;
23325 }
23326
23327 if (w->base_line_number > 0
23328 && w->base_line_pos > 0
23329 && w->base_line_pos <= startpos)
23330 {
23331 line = w->base_line_number;
23332 linepos = w->base_line_pos;
23333 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23334 }
23335 else
23336 {
23337 line = 1;
23338 linepos = BUF_BEGV (b);
23339 linepos_byte = BUF_BEGV_BYTE (b);
23340 }
23341
23342 /* Count lines from base line to window start position. */
23343 nlines = display_count_lines (linepos_byte,
23344 startpos_byte,
23345 startpos, &junk);
23346
23347 topline = nlines + line;
23348
23349 /* Determine a new base line, if the old one is too close
23350 or too far away, or if we did not have one.
23351 "Too close" means it's plausible a scroll-down would
23352 go back past it. */
23353 if (startpos == BUF_BEGV (b))
23354 {
23355 w->base_line_number = topline;
23356 w->base_line_pos = BUF_BEGV (b);
23357 }
23358 else if (nlines < height + 25 || nlines > height * 3 + 50
23359 || linepos == BUF_BEGV (b))
23360 {
23361 ptrdiff_t limit = BUF_BEGV (b);
23362 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23363 ptrdiff_t position;
23364 ptrdiff_t distance =
23365 (height * 2 + 30) * line_number_display_limit_width;
23366
23367 if (startpos - distance > limit)
23368 {
23369 limit = startpos - distance;
23370 limit_byte = CHAR_TO_BYTE (limit);
23371 }
23372
23373 nlines = display_count_lines (startpos_byte,
23374 limit_byte,
23375 - (height * 2 + 30),
23376 &position);
23377 /* If we couldn't find the lines we wanted within
23378 line_number_display_limit_width chars per line,
23379 give up on line numbers for this window. */
23380 if (position == limit_byte && limit == startpos - distance)
23381 {
23382 w->base_line_pos = -1;
23383 w->base_line_number = 0;
23384 goto no_value;
23385 }
23386
23387 w->base_line_number = topline - nlines;
23388 w->base_line_pos = BYTE_TO_CHAR (position);
23389 }
23390
23391 /* Now count lines from the start pos to point. */
23392 nlines = display_count_lines (startpos_byte,
23393 PT_BYTE, PT, &junk);
23394
23395 /* Record that we did display the line number. */
23396 line_number_displayed = true;
23397
23398 /* Make the string to show. */
23399 pint2str (decode_mode_spec_buf, width, topline + nlines);
23400 return decode_mode_spec_buf;
23401 no_value:
23402 {
23403 char *p = decode_mode_spec_buf;
23404 int pad = width - 2;
23405 while (pad-- > 0)
23406 *p++ = ' ';
23407 *p++ = '?';
23408 *p++ = '?';
23409 *p = '\0';
23410 return decode_mode_spec_buf;
23411 }
23412 }
23413 break;
23414
23415 case 'm':
23416 obj = BVAR (b, mode_name);
23417 break;
23418
23419 case 'n':
23420 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23421 return " Narrow";
23422 break;
23423
23424 case 'p':
23425 {
23426 ptrdiff_t pos = marker_position (w->start);
23427 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23428
23429 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23430 {
23431 if (pos <= BUF_BEGV (b))
23432 return "All";
23433 else
23434 return "Bottom";
23435 }
23436 else if (pos <= BUF_BEGV (b))
23437 return "Top";
23438 else
23439 {
23440 if (total > 1000000)
23441 /* Do it differently for a large value, to avoid overflow. */
23442 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23443 else
23444 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23445 /* We can't normally display a 3-digit number,
23446 so get us a 2-digit number that is close. */
23447 if (total == 100)
23448 total = 99;
23449 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23450 return decode_mode_spec_buf;
23451 }
23452 }
23453
23454 /* Display percentage of size above the bottom of the screen. */
23455 case 'P':
23456 {
23457 ptrdiff_t toppos = marker_position (w->start);
23458 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23459 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23460
23461 if (botpos >= BUF_ZV (b))
23462 {
23463 if (toppos <= BUF_BEGV (b))
23464 return "All";
23465 else
23466 return "Bottom";
23467 }
23468 else
23469 {
23470 if (total > 1000000)
23471 /* Do it differently for a large value, to avoid overflow. */
23472 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23473 else
23474 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23475 /* We can't normally display a 3-digit number,
23476 so get us a 2-digit number that is close. */
23477 if (total == 100)
23478 total = 99;
23479 if (toppos <= BUF_BEGV (b))
23480 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23481 else
23482 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23483 return decode_mode_spec_buf;
23484 }
23485 }
23486
23487 case 's':
23488 /* status of process */
23489 obj = Fget_buffer_process (Fcurrent_buffer ());
23490 if (NILP (obj))
23491 return "no process";
23492 #ifndef MSDOS
23493 obj = Fsymbol_name (Fprocess_status (obj));
23494 #endif
23495 break;
23496
23497 case '@':
23498 {
23499 ptrdiff_t count = inhibit_garbage_collection ();
23500 Lisp_Object curdir = BVAR (current_buffer, directory);
23501 Lisp_Object val = Qnil;
23502
23503 if (STRINGP (curdir))
23504 val = call1 (intern ("file-remote-p"), curdir);
23505
23506 unbind_to (count, Qnil);
23507
23508 if (NILP (val))
23509 return "-";
23510 else
23511 return "@";
23512 }
23513
23514 case 'z':
23515 /* coding-system (not including end-of-line format) */
23516 case 'Z':
23517 /* coding-system (including end-of-line type) */
23518 {
23519 bool eol_flag = (c == 'Z');
23520 char *p = decode_mode_spec_buf;
23521
23522 if (! FRAME_WINDOW_P (f))
23523 {
23524 /* No need to mention EOL here--the terminal never needs
23525 to do EOL conversion. */
23526 p = decode_mode_spec_coding (CODING_ID_NAME
23527 (FRAME_KEYBOARD_CODING (f)->id),
23528 p, false);
23529 p = decode_mode_spec_coding (CODING_ID_NAME
23530 (FRAME_TERMINAL_CODING (f)->id),
23531 p, false);
23532 }
23533 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23534 p, eol_flag);
23535
23536 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23537 #ifdef subprocesses
23538 obj = Fget_buffer_process (Fcurrent_buffer ());
23539 if (PROCESSP (obj))
23540 {
23541 p = decode_mode_spec_coding
23542 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23543 p = decode_mode_spec_coding
23544 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23545 }
23546 #endif /* subprocesses */
23547 #endif /* false */
23548 *p = 0;
23549 return decode_mode_spec_buf;
23550 }
23551 }
23552
23553 if (STRINGP (obj))
23554 {
23555 *string = obj;
23556 return SSDATA (obj);
23557 }
23558 else
23559 return "";
23560 }
23561
23562
23563 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23564 means count lines back from START_BYTE. But don't go beyond
23565 LIMIT_BYTE. Return the number of lines thus found (always
23566 nonnegative).
23567
23568 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23569 either the position COUNT lines after/before START_BYTE, if we
23570 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23571 COUNT lines. */
23572
23573 static ptrdiff_t
23574 display_count_lines (ptrdiff_t start_byte,
23575 ptrdiff_t limit_byte, ptrdiff_t count,
23576 ptrdiff_t *byte_pos_ptr)
23577 {
23578 register unsigned char *cursor;
23579 unsigned char *base;
23580
23581 register ptrdiff_t ceiling;
23582 register unsigned char *ceiling_addr;
23583 ptrdiff_t orig_count = count;
23584
23585 /* If we are not in selective display mode,
23586 check only for newlines. */
23587 bool selective_display
23588 = (!NILP (BVAR (current_buffer, selective_display))
23589 && !INTEGERP (BVAR (current_buffer, selective_display)));
23590
23591 if (count > 0)
23592 {
23593 while (start_byte < limit_byte)
23594 {
23595 ceiling = BUFFER_CEILING_OF (start_byte);
23596 ceiling = min (limit_byte - 1, ceiling);
23597 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23598 base = (cursor = BYTE_POS_ADDR (start_byte));
23599
23600 do
23601 {
23602 if (selective_display)
23603 {
23604 while (*cursor != '\n' && *cursor != 015
23605 && ++cursor != ceiling_addr)
23606 continue;
23607 if (cursor == ceiling_addr)
23608 break;
23609 }
23610 else
23611 {
23612 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23613 if (! cursor)
23614 break;
23615 }
23616
23617 cursor++;
23618
23619 if (--count == 0)
23620 {
23621 start_byte += cursor - base;
23622 *byte_pos_ptr = start_byte;
23623 return orig_count;
23624 }
23625 }
23626 while (cursor < ceiling_addr);
23627
23628 start_byte += ceiling_addr - base;
23629 }
23630 }
23631 else
23632 {
23633 while (start_byte > limit_byte)
23634 {
23635 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23636 ceiling = max (limit_byte, ceiling);
23637 ceiling_addr = BYTE_POS_ADDR (ceiling);
23638 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23639 while (true)
23640 {
23641 if (selective_display)
23642 {
23643 while (--cursor >= ceiling_addr
23644 && *cursor != '\n' && *cursor != 015)
23645 continue;
23646 if (cursor < ceiling_addr)
23647 break;
23648 }
23649 else
23650 {
23651 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23652 if (! cursor)
23653 break;
23654 }
23655
23656 if (++count == 0)
23657 {
23658 start_byte += cursor - base + 1;
23659 *byte_pos_ptr = start_byte;
23660 /* When scanning backwards, we should
23661 not count the newline posterior to which we stop. */
23662 return - orig_count - 1;
23663 }
23664 }
23665 start_byte += ceiling_addr - base;
23666 }
23667 }
23668
23669 *byte_pos_ptr = limit_byte;
23670
23671 if (count < 0)
23672 return - orig_count + count;
23673 return orig_count - count;
23674
23675 }
23676
23677
23678 \f
23679 /***********************************************************************
23680 Displaying strings
23681 ***********************************************************************/
23682
23683 /* Display a NUL-terminated string, starting with index START.
23684
23685 If STRING is non-null, display that C string. Otherwise, the Lisp
23686 string LISP_STRING is displayed. There's a case that STRING is
23687 non-null and LISP_STRING is not nil. It means STRING is a string
23688 data of LISP_STRING. In that case, we display LISP_STRING while
23689 ignoring its text properties.
23690
23691 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23692 FACE_STRING. Display STRING or LISP_STRING with the face at
23693 FACE_STRING_POS in FACE_STRING:
23694
23695 Display the string in the environment given by IT, but use the
23696 standard display table, temporarily.
23697
23698 FIELD_WIDTH is the minimum number of output glyphs to produce.
23699 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23700 with spaces. If STRING has more characters, more than FIELD_WIDTH
23701 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23702
23703 PRECISION is the maximum number of characters to output from
23704 STRING. PRECISION < 0 means don't truncate the string.
23705
23706 This is roughly equivalent to printf format specifiers:
23707
23708 FIELD_WIDTH PRECISION PRINTF
23709 ----------------------------------------
23710 -1 -1 %s
23711 -1 10 %.10s
23712 10 -1 %10s
23713 20 10 %20.10s
23714
23715 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23716 display them, and < 0 means obey the current buffer's value of
23717 enable_multibyte_characters.
23718
23719 Value is the number of columns displayed. */
23720
23721 static int
23722 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23723 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23724 int field_width, int precision, int max_x, int multibyte)
23725 {
23726 int hpos_at_start = it->hpos;
23727 int saved_face_id = it->face_id;
23728 struct glyph_row *row = it->glyph_row;
23729 ptrdiff_t it_charpos;
23730
23731 /* Initialize the iterator IT for iteration over STRING beginning
23732 with index START. */
23733 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23734 precision, field_width, multibyte);
23735 if (string && STRINGP (lisp_string))
23736 /* LISP_STRING is the one returned by decode_mode_spec. We should
23737 ignore its text properties. */
23738 it->stop_charpos = it->end_charpos;
23739
23740 /* If displaying STRING, set up the face of the iterator from
23741 FACE_STRING, if that's given. */
23742 if (STRINGP (face_string))
23743 {
23744 ptrdiff_t endptr;
23745 struct face *face;
23746
23747 it->face_id
23748 = face_at_string_position (it->w, face_string, face_string_pos,
23749 0, &endptr, it->base_face_id, false);
23750 face = FACE_FROM_ID (it->f, it->face_id);
23751 it->face_box_p = face->box != FACE_NO_BOX;
23752 }
23753
23754 /* Set max_x to the maximum allowed X position. Don't let it go
23755 beyond the right edge of the window. */
23756 if (max_x <= 0)
23757 max_x = it->last_visible_x;
23758 else
23759 max_x = min (max_x, it->last_visible_x);
23760
23761 /* Skip over display elements that are not visible. because IT->w is
23762 hscrolled. */
23763 if (it->current_x < it->first_visible_x)
23764 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23765 MOVE_TO_POS | MOVE_TO_X);
23766
23767 row->ascent = it->max_ascent;
23768 row->height = it->max_ascent + it->max_descent;
23769 row->phys_ascent = it->max_phys_ascent;
23770 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23771 row->extra_line_spacing = it->max_extra_line_spacing;
23772
23773 if (STRINGP (it->string))
23774 it_charpos = IT_STRING_CHARPOS (*it);
23775 else
23776 it_charpos = IT_CHARPOS (*it);
23777
23778 /* This condition is for the case that we are called with current_x
23779 past last_visible_x. */
23780 while (it->current_x < max_x)
23781 {
23782 int x_before, x, n_glyphs_before, i, nglyphs;
23783
23784 /* Get the next display element. */
23785 if (!get_next_display_element (it))
23786 break;
23787
23788 /* Produce glyphs. */
23789 x_before = it->current_x;
23790 n_glyphs_before = row->used[TEXT_AREA];
23791 PRODUCE_GLYPHS (it);
23792
23793 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23794 i = 0;
23795 x = x_before;
23796 while (i < nglyphs)
23797 {
23798 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23799
23800 if (it->line_wrap != TRUNCATE
23801 && x + glyph->pixel_width > max_x)
23802 {
23803 /* End of continued line or max_x reached. */
23804 if (CHAR_GLYPH_PADDING_P (*glyph))
23805 {
23806 /* A wide character is unbreakable. */
23807 if (row->reversed_p)
23808 unproduce_glyphs (it, row->used[TEXT_AREA]
23809 - n_glyphs_before);
23810 row->used[TEXT_AREA] = n_glyphs_before;
23811 it->current_x = x_before;
23812 }
23813 else
23814 {
23815 if (row->reversed_p)
23816 unproduce_glyphs (it, row->used[TEXT_AREA]
23817 - (n_glyphs_before + i));
23818 row->used[TEXT_AREA] = n_glyphs_before + i;
23819 it->current_x = x;
23820 }
23821 break;
23822 }
23823 else if (x + glyph->pixel_width >= it->first_visible_x)
23824 {
23825 /* Glyph is at least partially visible. */
23826 ++it->hpos;
23827 if (x < it->first_visible_x)
23828 row->x = x - it->first_visible_x;
23829 }
23830 else
23831 {
23832 /* Glyph is off the left margin of the display area.
23833 Should not happen. */
23834 emacs_abort ();
23835 }
23836
23837 row->ascent = max (row->ascent, it->max_ascent);
23838 row->height = max (row->height, it->max_ascent + it->max_descent);
23839 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23840 row->phys_height = max (row->phys_height,
23841 it->max_phys_ascent + it->max_phys_descent);
23842 row->extra_line_spacing = max (row->extra_line_spacing,
23843 it->max_extra_line_spacing);
23844 x += glyph->pixel_width;
23845 ++i;
23846 }
23847
23848 /* Stop if max_x reached. */
23849 if (i < nglyphs)
23850 break;
23851
23852 /* Stop at line ends. */
23853 if (ITERATOR_AT_END_OF_LINE_P (it))
23854 {
23855 it->continuation_lines_width = 0;
23856 break;
23857 }
23858
23859 set_iterator_to_next (it, true);
23860 if (STRINGP (it->string))
23861 it_charpos = IT_STRING_CHARPOS (*it);
23862 else
23863 it_charpos = IT_CHARPOS (*it);
23864
23865 /* Stop if truncating at the right edge. */
23866 if (it->line_wrap == TRUNCATE
23867 && it->current_x >= it->last_visible_x)
23868 {
23869 /* Add truncation mark, but don't do it if the line is
23870 truncated at a padding space. */
23871 if (it_charpos < it->string_nchars)
23872 {
23873 if (!FRAME_WINDOW_P (it->f))
23874 {
23875 int ii, n;
23876
23877 if (it->current_x > it->last_visible_x)
23878 {
23879 if (!row->reversed_p)
23880 {
23881 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23882 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23883 break;
23884 }
23885 else
23886 {
23887 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23888 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23889 break;
23890 unproduce_glyphs (it, ii + 1);
23891 ii = row->used[TEXT_AREA] - (ii + 1);
23892 }
23893 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23894 {
23895 row->used[TEXT_AREA] = ii;
23896 produce_special_glyphs (it, IT_TRUNCATION);
23897 }
23898 }
23899 produce_special_glyphs (it, IT_TRUNCATION);
23900 }
23901 row->truncated_on_right_p = true;
23902 }
23903 break;
23904 }
23905 }
23906
23907 /* Maybe insert a truncation at the left. */
23908 if (it->first_visible_x
23909 && it_charpos > 0)
23910 {
23911 if (!FRAME_WINDOW_P (it->f)
23912 || (row->reversed_p
23913 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23914 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23915 insert_left_trunc_glyphs (it);
23916 row->truncated_on_left_p = true;
23917 }
23918
23919 it->face_id = saved_face_id;
23920
23921 /* Value is number of columns displayed. */
23922 return it->hpos - hpos_at_start;
23923 }
23924
23925
23926 \f
23927 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23928 appears as an element of LIST or as the car of an element of LIST.
23929 If PROPVAL is a list, compare each element against LIST in that
23930 way, and return 1/2 if any element of PROPVAL is found in LIST.
23931 Otherwise return 0. This function cannot quit.
23932 The return value is 2 if the text is invisible but with an ellipsis
23933 and 1 if it's invisible and without an ellipsis. */
23934
23935 int
23936 invisible_prop (Lisp_Object propval, Lisp_Object list)
23937 {
23938 Lisp_Object tail, proptail;
23939
23940 for (tail = list; CONSP (tail); tail = XCDR (tail))
23941 {
23942 register Lisp_Object tem;
23943 tem = XCAR (tail);
23944 if (EQ (propval, tem))
23945 return 1;
23946 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23947 return NILP (XCDR (tem)) ? 1 : 2;
23948 }
23949
23950 if (CONSP (propval))
23951 {
23952 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23953 {
23954 Lisp_Object propelt;
23955 propelt = XCAR (proptail);
23956 for (tail = list; CONSP (tail); tail = XCDR (tail))
23957 {
23958 register Lisp_Object tem;
23959 tem = XCAR (tail);
23960 if (EQ (propelt, tem))
23961 return 1;
23962 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23963 return NILP (XCDR (tem)) ? 1 : 2;
23964 }
23965 }
23966 }
23967
23968 return 0;
23969 }
23970
23971 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23972 doc: /* Non-nil if the property makes the text invisible.
23973 POS-OR-PROP can be a marker or number, in which case it is taken to be
23974 a position in the current buffer and the value of the `invisible' property
23975 is checked; or it can be some other value, which is then presumed to be the
23976 value of the `invisible' property of the text of interest.
23977 The non-nil value returned can be t for truly invisible text or something
23978 else if the text is replaced by an ellipsis. */)
23979 (Lisp_Object pos_or_prop)
23980 {
23981 Lisp_Object prop
23982 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23983 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23984 : pos_or_prop);
23985 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23986 return (invis == 0 ? Qnil
23987 : invis == 1 ? Qt
23988 : make_number (invis));
23989 }
23990
23991 /* Calculate a width or height in pixels from a specification using
23992 the following elements:
23993
23994 SPEC ::=
23995 NUM - a (fractional) multiple of the default font width/height
23996 (NUM) - specifies exactly NUM pixels
23997 UNIT - a fixed number of pixels, see below.
23998 ELEMENT - size of a display element in pixels, see below.
23999 (NUM . SPEC) - equals NUM * SPEC
24000 (+ SPEC SPEC ...) - add pixel values
24001 (- SPEC SPEC ...) - subtract pixel values
24002 (- SPEC) - negate pixel value
24003
24004 NUM ::=
24005 INT or FLOAT - a number constant
24006 SYMBOL - use symbol's (buffer local) variable binding.
24007
24008 UNIT ::=
24009 in - pixels per inch *)
24010 mm - pixels per 1/1000 meter *)
24011 cm - pixels per 1/100 meter *)
24012 width - width of current font in pixels.
24013 height - height of current font in pixels.
24014
24015 *) using the ratio(s) defined in display-pixels-per-inch.
24016
24017 ELEMENT ::=
24018
24019 left-fringe - left fringe width in pixels
24020 right-fringe - right fringe width in pixels
24021
24022 left-margin - left margin width in pixels
24023 right-margin - right margin width in pixels
24024
24025 scroll-bar - scroll-bar area width in pixels
24026
24027 Examples:
24028
24029 Pixels corresponding to 5 inches:
24030 (5 . in)
24031
24032 Total width of non-text areas on left side of window (if scroll-bar is on left):
24033 '(space :width (+ left-fringe left-margin scroll-bar))
24034
24035 Align to first text column (in header line):
24036 '(space :align-to 0)
24037
24038 Align to middle of text area minus half the width of variable `my-image'
24039 containing a loaded image:
24040 '(space :align-to (0.5 . (- text my-image)))
24041
24042 Width of left margin minus width of 1 character in the default font:
24043 '(space :width (- left-margin 1))
24044
24045 Width of left margin minus width of 2 characters in the current font:
24046 '(space :width (- left-margin (2 . width)))
24047
24048 Center 1 character over left-margin (in header line):
24049 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24050
24051 Different ways to express width of left fringe plus left margin minus one pixel:
24052 '(space :width (- (+ left-fringe left-margin) (1)))
24053 '(space :width (+ left-fringe left-margin (- (1))))
24054 '(space :width (+ left-fringe left-margin (-1)))
24055
24056 */
24057
24058 static bool
24059 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24060 struct font *font, bool width_p, int *align_to)
24061 {
24062 double pixels;
24063
24064 # define OK_PIXELS(val) (*res = (val), true)
24065 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24066
24067 if (NILP (prop))
24068 return OK_PIXELS (0);
24069
24070 eassert (FRAME_LIVE_P (it->f));
24071
24072 if (SYMBOLP (prop))
24073 {
24074 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24075 {
24076 char *unit = SSDATA (SYMBOL_NAME (prop));
24077
24078 if (unit[0] == 'i' && unit[1] == 'n')
24079 pixels = 1.0;
24080 else if (unit[0] == 'm' && unit[1] == 'm')
24081 pixels = 25.4;
24082 else if (unit[0] == 'c' && unit[1] == 'm')
24083 pixels = 2.54;
24084 else
24085 pixels = 0;
24086 if (pixels > 0)
24087 {
24088 double ppi = (width_p ? FRAME_RES_X (it->f)
24089 : FRAME_RES_Y (it->f));
24090
24091 if (ppi > 0)
24092 return OK_PIXELS (ppi / pixels);
24093 return false;
24094 }
24095 }
24096
24097 #ifdef HAVE_WINDOW_SYSTEM
24098 if (EQ (prop, Qheight))
24099 return OK_PIXELS (font
24100 ? normal_char_height (font, -1)
24101 : FRAME_LINE_HEIGHT (it->f));
24102 if (EQ (prop, Qwidth))
24103 return OK_PIXELS (font
24104 ? FONT_WIDTH (font)
24105 : FRAME_COLUMN_WIDTH (it->f));
24106 #else
24107 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24108 return OK_PIXELS (1);
24109 #endif
24110
24111 if (EQ (prop, Qtext))
24112 return OK_PIXELS (width_p
24113 ? window_box_width (it->w, TEXT_AREA)
24114 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24115
24116 if (align_to && *align_to < 0)
24117 {
24118 *res = 0;
24119 if (EQ (prop, Qleft))
24120 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24121 if (EQ (prop, Qright))
24122 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24123 if (EQ (prop, Qcenter))
24124 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24125 + window_box_width (it->w, TEXT_AREA) / 2);
24126 if (EQ (prop, Qleft_fringe))
24127 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24128 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24129 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24130 if (EQ (prop, Qright_fringe))
24131 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24132 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24133 : window_box_right_offset (it->w, TEXT_AREA));
24134 if (EQ (prop, Qleft_margin))
24135 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24136 if (EQ (prop, Qright_margin))
24137 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24138 if (EQ (prop, Qscroll_bar))
24139 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24140 ? 0
24141 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24142 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24143 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24144 : 0)));
24145 }
24146 else
24147 {
24148 if (EQ (prop, Qleft_fringe))
24149 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24150 if (EQ (prop, Qright_fringe))
24151 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24152 if (EQ (prop, Qleft_margin))
24153 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24154 if (EQ (prop, Qright_margin))
24155 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24156 if (EQ (prop, Qscroll_bar))
24157 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24158 }
24159
24160 prop = buffer_local_value (prop, it->w->contents);
24161 if (EQ (prop, Qunbound))
24162 prop = Qnil;
24163 }
24164
24165 if (NUMBERP (prop))
24166 {
24167 int base_unit = (width_p
24168 ? FRAME_COLUMN_WIDTH (it->f)
24169 : FRAME_LINE_HEIGHT (it->f));
24170 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24171 }
24172
24173 if (CONSP (prop))
24174 {
24175 Lisp_Object car = XCAR (prop);
24176 Lisp_Object cdr = XCDR (prop);
24177
24178 if (SYMBOLP (car))
24179 {
24180 #ifdef HAVE_WINDOW_SYSTEM
24181 if (FRAME_WINDOW_P (it->f)
24182 && valid_image_p (prop))
24183 {
24184 ptrdiff_t id = lookup_image (it->f, prop);
24185 struct image *img = IMAGE_FROM_ID (it->f, id);
24186
24187 return OK_PIXELS (width_p ? img->width : img->height);
24188 }
24189 #endif
24190 if (EQ (car, Qplus) || EQ (car, Qminus))
24191 {
24192 bool first = true;
24193 double px;
24194
24195 pixels = 0;
24196 while (CONSP (cdr))
24197 {
24198 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24199 font, width_p, align_to))
24200 return false;
24201 if (first)
24202 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24203 else
24204 pixels += px;
24205 cdr = XCDR (cdr);
24206 }
24207 if (EQ (car, Qminus))
24208 pixels = -pixels;
24209 return OK_PIXELS (pixels);
24210 }
24211
24212 car = buffer_local_value (car, it->w->contents);
24213 if (EQ (car, Qunbound))
24214 car = Qnil;
24215 }
24216
24217 if (NUMBERP (car))
24218 {
24219 double fact;
24220 pixels = XFLOATINT (car);
24221 if (NILP (cdr))
24222 return OK_PIXELS (pixels);
24223 if (calc_pixel_width_or_height (&fact, it, cdr,
24224 font, width_p, align_to))
24225 return OK_PIXELS (pixels * fact);
24226 return false;
24227 }
24228
24229 return false;
24230 }
24231
24232 return false;
24233 }
24234
24235 void
24236 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24237 {
24238 #ifdef HAVE_WINDOW_SYSTEM
24239 normal_char_ascent_descent (font, -1, ascent, descent);
24240 #else
24241 *ascent = 1;
24242 *descent = 0;
24243 #endif
24244 }
24245
24246 \f
24247 /***********************************************************************
24248 Glyph Display
24249 ***********************************************************************/
24250
24251 #ifdef HAVE_WINDOW_SYSTEM
24252
24253 #ifdef GLYPH_DEBUG
24254
24255 void
24256 dump_glyph_string (struct glyph_string *s)
24257 {
24258 fprintf (stderr, "glyph string\n");
24259 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24260 s->x, s->y, s->width, s->height);
24261 fprintf (stderr, " ybase = %d\n", s->ybase);
24262 fprintf (stderr, " hl = %d\n", s->hl);
24263 fprintf (stderr, " left overhang = %d, right = %d\n",
24264 s->left_overhang, s->right_overhang);
24265 fprintf (stderr, " nchars = %d\n", s->nchars);
24266 fprintf (stderr, " extends to end of line = %d\n",
24267 s->extends_to_end_of_line_p);
24268 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24269 fprintf (stderr, " bg width = %d\n", s->background_width);
24270 }
24271
24272 #endif /* GLYPH_DEBUG */
24273
24274 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24275 of XChar2b structures for S; it can't be allocated in
24276 init_glyph_string because it must be allocated via `alloca'. W
24277 is the window on which S is drawn. ROW and AREA are the glyph row
24278 and area within the row from which S is constructed. START is the
24279 index of the first glyph structure covered by S. HL is a
24280 face-override for drawing S. */
24281
24282 #ifdef HAVE_NTGUI
24283 #define OPTIONAL_HDC(hdc) HDC hdc,
24284 #define DECLARE_HDC(hdc) HDC hdc;
24285 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24286 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24287 #endif
24288
24289 #ifndef OPTIONAL_HDC
24290 #define OPTIONAL_HDC(hdc)
24291 #define DECLARE_HDC(hdc)
24292 #define ALLOCATE_HDC(hdc, f)
24293 #define RELEASE_HDC(hdc, f)
24294 #endif
24295
24296 static void
24297 init_glyph_string (struct glyph_string *s,
24298 OPTIONAL_HDC (hdc)
24299 XChar2b *char2b, struct window *w, struct glyph_row *row,
24300 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24301 {
24302 memset (s, 0, sizeof *s);
24303 s->w = w;
24304 s->f = XFRAME (w->frame);
24305 #ifdef HAVE_NTGUI
24306 s->hdc = hdc;
24307 #endif
24308 s->display = FRAME_X_DISPLAY (s->f);
24309 s->window = FRAME_X_WINDOW (s->f);
24310 s->char2b = char2b;
24311 s->hl = hl;
24312 s->row = row;
24313 s->area = area;
24314 s->first_glyph = row->glyphs[area] + start;
24315 s->height = row->height;
24316 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24317 s->ybase = s->y + row->ascent;
24318 }
24319
24320
24321 /* Append the list of glyph strings with head H and tail T to the list
24322 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24323
24324 static void
24325 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24326 struct glyph_string *h, struct glyph_string *t)
24327 {
24328 if (h)
24329 {
24330 if (*head)
24331 (*tail)->next = h;
24332 else
24333 *head = h;
24334 h->prev = *tail;
24335 *tail = t;
24336 }
24337 }
24338
24339
24340 /* Prepend the list of glyph strings with head H and tail T to the
24341 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24342 result. */
24343
24344 static void
24345 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24346 struct glyph_string *h, struct glyph_string *t)
24347 {
24348 if (h)
24349 {
24350 if (*head)
24351 (*head)->prev = t;
24352 else
24353 *tail = t;
24354 t->next = *head;
24355 *head = h;
24356 }
24357 }
24358
24359
24360 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24361 Set *HEAD and *TAIL to the resulting list. */
24362
24363 static void
24364 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24365 struct glyph_string *s)
24366 {
24367 s->next = s->prev = NULL;
24368 append_glyph_string_lists (head, tail, s, s);
24369 }
24370
24371
24372 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24373 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24374 make sure that X resources for the face returned are allocated.
24375 Value is a pointer to a realized face that is ready for display if
24376 DISPLAY_P. */
24377
24378 static struct face *
24379 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24380 XChar2b *char2b, bool display_p)
24381 {
24382 struct face *face = FACE_FROM_ID (f, face_id);
24383 unsigned code = 0;
24384
24385 if (face->font)
24386 {
24387 code = face->font->driver->encode_char (face->font, c);
24388
24389 if (code == FONT_INVALID_CODE)
24390 code = 0;
24391 }
24392 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24393
24394 /* Make sure X resources of the face are allocated. */
24395 #ifdef HAVE_X_WINDOWS
24396 if (display_p)
24397 #endif
24398 {
24399 eassert (face != NULL);
24400 prepare_face_for_display (f, face);
24401 }
24402
24403 return face;
24404 }
24405
24406
24407 /* Get face and two-byte form of character glyph GLYPH on frame F.
24408 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24409 a pointer to a realized face that is ready for display. */
24410
24411 static struct face *
24412 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24413 XChar2b *char2b)
24414 {
24415 struct face *face;
24416 unsigned code = 0;
24417
24418 eassert (glyph->type == CHAR_GLYPH);
24419 face = FACE_FROM_ID (f, glyph->face_id);
24420
24421 /* Make sure X resources of the face are allocated. */
24422 eassert (face != NULL);
24423 prepare_face_for_display (f, face);
24424
24425 if (face->font)
24426 {
24427 if (CHAR_BYTE8_P (glyph->u.ch))
24428 code = CHAR_TO_BYTE8 (glyph->u.ch);
24429 else
24430 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24431
24432 if (code == FONT_INVALID_CODE)
24433 code = 0;
24434 }
24435
24436 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24437 return face;
24438 }
24439
24440
24441 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24442 Return true iff FONT has a glyph for C. */
24443
24444 static bool
24445 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24446 {
24447 unsigned code;
24448
24449 if (CHAR_BYTE8_P (c))
24450 code = CHAR_TO_BYTE8 (c);
24451 else
24452 code = font->driver->encode_char (font, c);
24453
24454 if (code == FONT_INVALID_CODE)
24455 return false;
24456 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24457 return true;
24458 }
24459
24460
24461 /* Fill glyph string S with composition components specified by S->cmp.
24462
24463 BASE_FACE is the base face of the composition.
24464 S->cmp_from is the index of the first component for S.
24465
24466 OVERLAPS non-zero means S should draw the foreground only, and use
24467 its physical height for clipping. See also draw_glyphs.
24468
24469 Value is the index of a component not in S. */
24470
24471 static int
24472 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24473 int overlaps)
24474 {
24475 int i;
24476 /* For all glyphs of this composition, starting at the offset
24477 S->cmp_from, until we reach the end of the definition or encounter a
24478 glyph that requires the different face, add it to S. */
24479 struct face *face;
24480
24481 eassert (s);
24482
24483 s->for_overlaps = overlaps;
24484 s->face = NULL;
24485 s->font = NULL;
24486 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24487 {
24488 int c = COMPOSITION_GLYPH (s->cmp, i);
24489
24490 /* TAB in a composition means display glyphs with padding space
24491 on the left or right. */
24492 if (c != '\t')
24493 {
24494 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24495 -1, Qnil);
24496
24497 face = get_char_face_and_encoding (s->f, c, face_id,
24498 s->char2b + i, true);
24499 if (face)
24500 {
24501 if (! s->face)
24502 {
24503 s->face = face;
24504 s->font = s->face->font;
24505 }
24506 else if (s->face != face)
24507 break;
24508 }
24509 }
24510 ++s->nchars;
24511 }
24512 s->cmp_to = i;
24513
24514 if (s->face == NULL)
24515 {
24516 s->face = base_face->ascii_face;
24517 s->font = s->face->font;
24518 }
24519
24520 /* All glyph strings for the same composition has the same width,
24521 i.e. the width set for the first component of the composition. */
24522 s->width = s->first_glyph->pixel_width;
24523
24524 /* If the specified font could not be loaded, use the frame's
24525 default font, but record the fact that we couldn't load it in
24526 the glyph string so that we can draw rectangles for the
24527 characters of the glyph string. */
24528 if (s->font == NULL)
24529 {
24530 s->font_not_found_p = true;
24531 s->font = FRAME_FONT (s->f);
24532 }
24533
24534 /* Adjust base line for subscript/superscript text. */
24535 s->ybase += s->first_glyph->voffset;
24536
24537 return s->cmp_to;
24538 }
24539
24540 static int
24541 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24542 int start, int end, int overlaps)
24543 {
24544 struct glyph *glyph, *last;
24545 Lisp_Object lgstring;
24546 int i;
24547
24548 s->for_overlaps = overlaps;
24549 glyph = s->row->glyphs[s->area] + start;
24550 last = s->row->glyphs[s->area] + end;
24551 s->cmp_id = glyph->u.cmp.id;
24552 s->cmp_from = glyph->slice.cmp.from;
24553 s->cmp_to = glyph->slice.cmp.to + 1;
24554 s->face = FACE_FROM_ID (s->f, face_id);
24555 lgstring = composition_gstring_from_id (s->cmp_id);
24556 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24557 glyph++;
24558 while (glyph < last
24559 && glyph->u.cmp.automatic
24560 && glyph->u.cmp.id == s->cmp_id
24561 && s->cmp_to == glyph->slice.cmp.from)
24562 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24563
24564 for (i = s->cmp_from; i < s->cmp_to; i++)
24565 {
24566 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24567 unsigned code = LGLYPH_CODE (lglyph);
24568
24569 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24570 }
24571 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24572 return glyph - s->row->glyphs[s->area];
24573 }
24574
24575
24576 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24577 See the comment of fill_glyph_string for arguments.
24578 Value is the index of the first glyph not in S. */
24579
24580
24581 static int
24582 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24583 int start, int end, int overlaps)
24584 {
24585 struct glyph *glyph, *last;
24586 int voffset;
24587
24588 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24589 s->for_overlaps = overlaps;
24590 glyph = s->row->glyphs[s->area] + start;
24591 last = s->row->glyphs[s->area] + end;
24592 voffset = glyph->voffset;
24593 s->face = FACE_FROM_ID (s->f, face_id);
24594 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24595 s->nchars = 1;
24596 s->width = glyph->pixel_width;
24597 glyph++;
24598 while (glyph < last
24599 && glyph->type == GLYPHLESS_GLYPH
24600 && glyph->voffset == voffset
24601 && glyph->face_id == face_id)
24602 {
24603 s->nchars++;
24604 s->width += glyph->pixel_width;
24605 glyph++;
24606 }
24607 s->ybase += voffset;
24608 return glyph - s->row->glyphs[s->area];
24609 }
24610
24611
24612 /* Fill glyph string S from a sequence of character glyphs.
24613
24614 FACE_ID is the face id of the string. START is the index of the
24615 first glyph to consider, END is the index of the last + 1.
24616 OVERLAPS non-zero means S should draw the foreground only, and use
24617 its physical height for clipping. See also draw_glyphs.
24618
24619 Value is the index of the first glyph not in S. */
24620
24621 static int
24622 fill_glyph_string (struct glyph_string *s, int face_id,
24623 int start, int end, int overlaps)
24624 {
24625 struct glyph *glyph, *last;
24626 int voffset;
24627 bool glyph_not_available_p;
24628
24629 eassert (s->f == XFRAME (s->w->frame));
24630 eassert (s->nchars == 0);
24631 eassert (start >= 0 && end > start);
24632
24633 s->for_overlaps = overlaps;
24634 glyph = s->row->glyphs[s->area] + start;
24635 last = s->row->glyphs[s->area] + end;
24636 voffset = glyph->voffset;
24637 s->padding_p = glyph->padding_p;
24638 glyph_not_available_p = glyph->glyph_not_available_p;
24639
24640 while (glyph < last
24641 && glyph->type == CHAR_GLYPH
24642 && glyph->voffset == voffset
24643 /* Same face id implies same font, nowadays. */
24644 && glyph->face_id == face_id
24645 && glyph->glyph_not_available_p == glyph_not_available_p)
24646 {
24647 s->face = get_glyph_face_and_encoding (s->f, glyph,
24648 s->char2b + s->nchars);
24649 ++s->nchars;
24650 eassert (s->nchars <= end - start);
24651 s->width += glyph->pixel_width;
24652 if (glyph++->padding_p != s->padding_p)
24653 break;
24654 }
24655
24656 s->font = s->face->font;
24657
24658 /* If the specified font could not be loaded, use the frame's font,
24659 but record the fact that we couldn't load it in
24660 S->font_not_found_p so that we can draw rectangles for the
24661 characters of the glyph string. */
24662 if (s->font == NULL || glyph_not_available_p)
24663 {
24664 s->font_not_found_p = true;
24665 s->font = FRAME_FONT (s->f);
24666 }
24667
24668 /* Adjust base line for subscript/superscript text. */
24669 s->ybase += voffset;
24670
24671 eassert (s->face && s->face->gc);
24672 return glyph - s->row->glyphs[s->area];
24673 }
24674
24675
24676 /* Fill glyph string S from image glyph S->first_glyph. */
24677
24678 static void
24679 fill_image_glyph_string (struct glyph_string *s)
24680 {
24681 eassert (s->first_glyph->type == IMAGE_GLYPH);
24682 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24683 eassert (s->img);
24684 s->slice = s->first_glyph->slice.img;
24685 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24686 s->font = s->face->font;
24687 s->width = s->first_glyph->pixel_width;
24688
24689 /* Adjust base line for subscript/superscript text. */
24690 s->ybase += s->first_glyph->voffset;
24691 }
24692
24693
24694 /* Fill glyph string S from a sequence of stretch glyphs.
24695
24696 START is the index of the first glyph to consider,
24697 END is the index of the last + 1.
24698
24699 Value is the index of the first glyph not in S. */
24700
24701 static int
24702 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24703 {
24704 struct glyph *glyph, *last;
24705 int voffset, face_id;
24706
24707 eassert (s->first_glyph->type == STRETCH_GLYPH);
24708
24709 glyph = s->row->glyphs[s->area] + start;
24710 last = s->row->glyphs[s->area] + end;
24711 face_id = glyph->face_id;
24712 s->face = FACE_FROM_ID (s->f, face_id);
24713 s->font = s->face->font;
24714 s->width = glyph->pixel_width;
24715 s->nchars = 1;
24716 voffset = glyph->voffset;
24717
24718 for (++glyph;
24719 (glyph < last
24720 && glyph->type == STRETCH_GLYPH
24721 && glyph->voffset == voffset
24722 && glyph->face_id == face_id);
24723 ++glyph)
24724 s->width += glyph->pixel_width;
24725
24726 /* Adjust base line for subscript/superscript text. */
24727 s->ybase += voffset;
24728
24729 /* The case that face->gc == 0 is handled when drawing the glyph
24730 string by calling prepare_face_for_display. */
24731 eassert (s->face);
24732 return glyph - s->row->glyphs[s->area];
24733 }
24734
24735 static struct font_metrics *
24736 get_per_char_metric (struct font *font, XChar2b *char2b)
24737 {
24738 static struct font_metrics metrics;
24739 unsigned code;
24740
24741 if (! font)
24742 return NULL;
24743 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24744 if (code == FONT_INVALID_CODE)
24745 return NULL;
24746 font->driver->text_extents (font, &code, 1, &metrics);
24747 return &metrics;
24748 }
24749
24750 /* A subroutine that computes "normal" values of ASCENT and DESCENT
24751 for FONT. Values are taken from font-global ones, except for fonts
24752 that claim preposterously large values, but whose glyphs actually
24753 have reasonable dimensions. C is the character to use for metrics
24754 if the font-global values are too large; if C is negative, the
24755 function selects a default character. */
24756 static void
24757 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
24758 {
24759 *ascent = FONT_BASE (font);
24760 *descent = FONT_DESCENT (font);
24761
24762 if (FONT_TOO_HIGH (font))
24763 {
24764 XChar2b char2b;
24765
24766 /* Get metrics of C, defaulting to a reasonably sized ASCII
24767 character. */
24768 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
24769 {
24770 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
24771
24772 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
24773 {
24774 /* We add 1 pixel to character dimensions as heuristics
24775 that produces nicer display, e.g. when the face has
24776 the box attribute. */
24777 *ascent = pcm->ascent + 1;
24778 *descent = pcm->descent + 1;
24779 }
24780 }
24781 }
24782 }
24783
24784 /* A subroutine that computes a reasonable "normal character height"
24785 for fonts that claim preposterously large vertical dimensions, but
24786 whose glyphs are actually reasonably sized. C is the character
24787 whose metrics to use for those fonts, or -1 for default
24788 character. */
24789 static int
24790 normal_char_height (struct font *font, int c)
24791 {
24792 int ascent, descent;
24793
24794 normal_char_ascent_descent (font, c, &ascent, &descent);
24795
24796 return ascent + descent;
24797 }
24798
24799 /* EXPORT for RIF:
24800 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24801 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24802 assumed to be zero. */
24803
24804 void
24805 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24806 {
24807 *left = *right = 0;
24808
24809 if (glyph->type == CHAR_GLYPH)
24810 {
24811 XChar2b char2b;
24812 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
24813 if (face->font)
24814 {
24815 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
24816 if (pcm)
24817 {
24818 if (pcm->rbearing > pcm->width)
24819 *right = pcm->rbearing - pcm->width;
24820 if (pcm->lbearing < 0)
24821 *left = -pcm->lbearing;
24822 }
24823 }
24824 }
24825 else if (glyph->type == COMPOSITE_GLYPH)
24826 {
24827 if (! glyph->u.cmp.automatic)
24828 {
24829 struct composition *cmp = composition_table[glyph->u.cmp.id];
24830
24831 if (cmp->rbearing > cmp->pixel_width)
24832 *right = cmp->rbearing - cmp->pixel_width;
24833 if (cmp->lbearing < 0)
24834 *left = - cmp->lbearing;
24835 }
24836 else
24837 {
24838 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24839 struct font_metrics metrics;
24840
24841 composition_gstring_width (gstring, glyph->slice.cmp.from,
24842 glyph->slice.cmp.to + 1, &metrics);
24843 if (metrics.rbearing > metrics.width)
24844 *right = metrics.rbearing - metrics.width;
24845 if (metrics.lbearing < 0)
24846 *left = - metrics.lbearing;
24847 }
24848 }
24849 }
24850
24851
24852 /* Return the index of the first glyph preceding glyph string S that
24853 is overwritten by S because of S's left overhang. Value is -1
24854 if no glyphs are overwritten. */
24855
24856 static int
24857 left_overwritten (struct glyph_string *s)
24858 {
24859 int k;
24860
24861 if (s->left_overhang)
24862 {
24863 int x = 0, i;
24864 struct glyph *glyphs = s->row->glyphs[s->area];
24865 int first = s->first_glyph - glyphs;
24866
24867 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24868 x -= glyphs[i].pixel_width;
24869
24870 k = i + 1;
24871 }
24872 else
24873 k = -1;
24874
24875 return k;
24876 }
24877
24878
24879 /* Return the index of the first glyph preceding glyph string S that
24880 is overwriting S because of its right overhang. Value is -1 if no
24881 glyph in front of S overwrites S. */
24882
24883 static int
24884 left_overwriting (struct glyph_string *s)
24885 {
24886 int i, k, x;
24887 struct glyph *glyphs = s->row->glyphs[s->area];
24888 int first = s->first_glyph - glyphs;
24889
24890 k = -1;
24891 x = 0;
24892 for (i = first - 1; i >= 0; --i)
24893 {
24894 int left, right;
24895 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24896 if (x + right > 0)
24897 k = i;
24898 x -= glyphs[i].pixel_width;
24899 }
24900
24901 return k;
24902 }
24903
24904
24905 /* Return the index of the last glyph following glyph string S that is
24906 overwritten by S because of S's right overhang. Value is -1 if
24907 no such glyph is found. */
24908
24909 static int
24910 right_overwritten (struct glyph_string *s)
24911 {
24912 int k = -1;
24913
24914 if (s->right_overhang)
24915 {
24916 int x = 0, i;
24917 struct glyph *glyphs = s->row->glyphs[s->area];
24918 int first = (s->first_glyph - glyphs
24919 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24920 int end = s->row->used[s->area];
24921
24922 for (i = first; i < end && s->right_overhang > x; ++i)
24923 x += glyphs[i].pixel_width;
24924
24925 k = i;
24926 }
24927
24928 return k;
24929 }
24930
24931
24932 /* Return the index of the last glyph following glyph string S that
24933 overwrites S because of its left overhang. Value is negative
24934 if no such glyph is found. */
24935
24936 static int
24937 right_overwriting (struct glyph_string *s)
24938 {
24939 int i, k, x;
24940 int end = s->row->used[s->area];
24941 struct glyph *glyphs = s->row->glyphs[s->area];
24942 int first = (s->first_glyph - glyphs
24943 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24944
24945 k = -1;
24946 x = 0;
24947 for (i = first; i < end; ++i)
24948 {
24949 int left, right;
24950 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24951 if (x - left < 0)
24952 k = i;
24953 x += glyphs[i].pixel_width;
24954 }
24955
24956 return k;
24957 }
24958
24959
24960 /* Set background width of glyph string S. START is the index of the
24961 first glyph following S. LAST_X is the right-most x-position + 1
24962 in the drawing area. */
24963
24964 static void
24965 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24966 {
24967 /* If the face of this glyph string has to be drawn to the end of
24968 the drawing area, set S->extends_to_end_of_line_p. */
24969
24970 if (start == s->row->used[s->area]
24971 && ((s->row->fill_line_p
24972 && (s->hl == DRAW_NORMAL_TEXT
24973 || s->hl == DRAW_IMAGE_RAISED
24974 || s->hl == DRAW_IMAGE_SUNKEN))
24975 || s->hl == DRAW_MOUSE_FACE))
24976 s->extends_to_end_of_line_p = true;
24977
24978 /* If S extends its face to the end of the line, set its
24979 background_width to the distance to the right edge of the drawing
24980 area. */
24981 if (s->extends_to_end_of_line_p)
24982 s->background_width = last_x - s->x + 1;
24983 else
24984 s->background_width = s->width;
24985 }
24986
24987
24988 /* Compute overhangs and x-positions for glyph string S and its
24989 predecessors, or successors. X is the starting x-position for S.
24990 BACKWARD_P means process predecessors. */
24991
24992 static void
24993 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
24994 {
24995 if (backward_p)
24996 {
24997 while (s)
24998 {
24999 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25000 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25001 x -= s->width;
25002 s->x = x;
25003 s = s->prev;
25004 }
25005 }
25006 else
25007 {
25008 while (s)
25009 {
25010 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25011 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25012 s->x = x;
25013 x += s->width;
25014 s = s->next;
25015 }
25016 }
25017 }
25018
25019
25020
25021 /* The following macros are only called from draw_glyphs below.
25022 They reference the following parameters of that function directly:
25023 `w', `row', `area', and `overlap_p'
25024 as well as the following local variables:
25025 `s', `f', and `hdc' (in W32) */
25026
25027 #ifdef HAVE_NTGUI
25028 /* On W32, silently add local `hdc' variable to argument list of
25029 init_glyph_string. */
25030 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25031 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25032 #else
25033 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25034 init_glyph_string (s, char2b, w, row, area, start, hl)
25035 #endif
25036
25037 /* Add a glyph string for a stretch glyph to the list of strings
25038 between HEAD and TAIL. START is the index of the stretch glyph in
25039 row area AREA of glyph row ROW. END is the index of the last glyph
25040 in that glyph row area. X is the current output position assigned
25041 to the new glyph string constructed. HL overrides that face of the
25042 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25043 is the right-most x-position of the drawing area. */
25044
25045 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25046 and below -- keep them on one line. */
25047 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25048 do \
25049 { \
25050 s = alloca (sizeof *s); \
25051 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25052 START = fill_stretch_glyph_string (s, START, END); \
25053 append_glyph_string (&HEAD, &TAIL, s); \
25054 s->x = (X); \
25055 } \
25056 while (false)
25057
25058
25059 /* Add a glyph string for an image glyph to the list of strings
25060 between HEAD and TAIL. START is the index of the image glyph in
25061 row area AREA of glyph row ROW. END is the index of the last glyph
25062 in that glyph row area. X is the current output position assigned
25063 to the new glyph string constructed. HL overrides that face of the
25064 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25065 is the right-most x-position of the drawing area. */
25066
25067 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25068 do \
25069 { \
25070 s = alloca (sizeof *s); \
25071 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25072 fill_image_glyph_string (s); \
25073 append_glyph_string (&HEAD, &TAIL, s); \
25074 ++START; \
25075 s->x = (X); \
25076 } \
25077 while (false)
25078
25079
25080 /* Add a glyph string for a sequence of character glyphs to the list
25081 of strings between HEAD and TAIL. START is the index of the first
25082 glyph in row area AREA of glyph row ROW that is part of the new
25083 glyph string. END is the index of the last glyph in that glyph row
25084 area. X is the current output position assigned to the new glyph
25085 string constructed. HL overrides that face of the glyph; e.g. it
25086 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25087 right-most x-position of the drawing area. */
25088
25089 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25090 do \
25091 { \
25092 int face_id; \
25093 XChar2b *char2b; \
25094 \
25095 face_id = (row)->glyphs[area][START].face_id; \
25096 \
25097 s = alloca (sizeof *s); \
25098 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25099 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25100 append_glyph_string (&HEAD, &TAIL, s); \
25101 s->x = (X); \
25102 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25103 } \
25104 while (false)
25105
25106
25107 /* Add a glyph string for a composite sequence to the list of strings
25108 between HEAD and TAIL. START is the index of the first glyph in
25109 row area AREA of glyph row ROW that is part of the new glyph
25110 string. END is the index of the last glyph in that glyph row area.
25111 X is the current output position assigned to the new glyph string
25112 constructed. HL overrides that face of the glyph; e.g. it is
25113 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25114 x-position of the drawing area. */
25115
25116 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25117 do { \
25118 int face_id = (row)->glyphs[area][START].face_id; \
25119 struct face *base_face = FACE_FROM_ID (f, face_id); \
25120 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25121 struct composition *cmp = composition_table[cmp_id]; \
25122 XChar2b *char2b; \
25123 struct glyph_string *first_s = NULL; \
25124 int n; \
25125 \
25126 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25127 \
25128 /* Make glyph_strings for each glyph sequence that is drawable by \
25129 the same face, and append them to HEAD/TAIL. */ \
25130 for (n = 0; n < cmp->glyph_len;) \
25131 { \
25132 s = alloca (sizeof *s); \
25133 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25134 append_glyph_string (&(HEAD), &(TAIL), s); \
25135 s->cmp = cmp; \
25136 s->cmp_from = n; \
25137 s->x = (X); \
25138 if (n == 0) \
25139 first_s = s; \
25140 n = fill_composite_glyph_string (s, base_face, overlaps); \
25141 } \
25142 \
25143 ++START; \
25144 s = first_s; \
25145 } while (false)
25146
25147
25148 /* Add a glyph string for a glyph-string sequence to the list of strings
25149 between HEAD and TAIL. */
25150
25151 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25152 do { \
25153 int face_id; \
25154 XChar2b *char2b; \
25155 Lisp_Object gstring; \
25156 \
25157 face_id = (row)->glyphs[area][START].face_id; \
25158 gstring = (composition_gstring_from_id \
25159 ((row)->glyphs[area][START].u.cmp.id)); \
25160 s = alloca (sizeof *s); \
25161 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25162 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25163 append_glyph_string (&(HEAD), &(TAIL), s); \
25164 s->x = (X); \
25165 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25166 } while (false)
25167
25168
25169 /* Add a glyph string for a sequence of glyphless character's glyphs
25170 to the list of strings between HEAD and TAIL. The meanings of
25171 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25172
25173 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25174 do \
25175 { \
25176 int face_id; \
25177 \
25178 face_id = (row)->glyphs[area][START].face_id; \
25179 \
25180 s = alloca (sizeof *s); \
25181 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25182 append_glyph_string (&HEAD, &TAIL, s); \
25183 s->x = (X); \
25184 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25185 overlaps); \
25186 } \
25187 while (false)
25188
25189
25190 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25191 of AREA of glyph row ROW on window W between indices START and END.
25192 HL overrides the face for drawing glyph strings, e.g. it is
25193 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25194 x-positions of the drawing area.
25195
25196 This is an ugly monster macro construct because we must use alloca
25197 to allocate glyph strings (because draw_glyphs can be called
25198 asynchronously). */
25199
25200 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25201 do \
25202 { \
25203 HEAD = TAIL = NULL; \
25204 while (START < END) \
25205 { \
25206 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25207 switch (first_glyph->type) \
25208 { \
25209 case CHAR_GLYPH: \
25210 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25211 HL, X, LAST_X); \
25212 break; \
25213 \
25214 case COMPOSITE_GLYPH: \
25215 if (first_glyph->u.cmp.automatic) \
25216 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25217 HL, X, LAST_X); \
25218 else \
25219 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25220 HL, X, LAST_X); \
25221 break; \
25222 \
25223 case STRETCH_GLYPH: \
25224 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25225 HL, X, LAST_X); \
25226 break; \
25227 \
25228 case IMAGE_GLYPH: \
25229 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25230 HL, X, LAST_X); \
25231 break; \
25232 \
25233 case GLYPHLESS_GLYPH: \
25234 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25235 HL, X, LAST_X); \
25236 break; \
25237 \
25238 default: \
25239 emacs_abort (); \
25240 } \
25241 \
25242 if (s) \
25243 { \
25244 set_glyph_string_background_width (s, START, LAST_X); \
25245 (X) += s->width; \
25246 } \
25247 } \
25248 } while (false)
25249
25250
25251 /* Draw glyphs between START and END in AREA of ROW on window W,
25252 starting at x-position X. X is relative to AREA in W. HL is a
25253 face-override with the following meaning:
25254
25255 DRAW_NORMAL_TEXT draw normally
25256 DRAW_CURSOR draw in cursor face
25257 DRAW_MOUSE_FACE draw in mouse face.
25258 DRAW_INVERSE_VIDEO draw in mode line face
25259 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25260 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25261
25262 If OVERLAPS is non-zero, draw only the foreground of characters and
25263 clip to the physical height of ROW. Non-zero value also defines
25264 the overlapping part to be drawn:
25265
25266 OVERLAPS_PRED overlap with preceding rows
25267 OVERLAPS_SUCC overlap with succeeding rows
25268 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25269 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25270
25271 Value is the x-position reached, relative to AREA of W. */
25272
25273 static int
25274 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25275 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25276 enum draw_glyphs_face hl, int overlaps)
25277 {
25278 struct glyph_string *head, *tail;
25279 struct glyph_string *s;
25280 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25281 int i, j, x_reached, last_x, area_left = 0;
25282 struct frame *f = XFRAME (WINDOW_FRAME (w));
25283 DECLARE_HDC (hdc);
25284
25285 ALLOCATE_HDC (hdc, f);
25286
25287 /* Let's rather be paranoid than getting a SEGV. */
25288 end = min (end, row->used[area]);
25289 start = clip_to_bounds (0, start, end);
25290
25291 /* Translate X to frame coordinates. Set last_x to the right
25292 end of the drawing area. */
25293 if (row->full_width_p)
25294 {
25295 /* X is relative to the left edge of W, without scroll bars
25296 or fringes. */
25297 area_left = WINDOW_LEFT_EDGE_X (w);
25298 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25299 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25300 }
25301 else
25302 {
25303 area_left = window_box_left (w, area);
25304 last_x = area_left + window_box_width (w, area);
25305 }
25306 x += area_left;
25307
25308 /* Build a doubly-linked list of glyph_string structures between
25309 head and tail from what we have to draw. Note that the macro
25310 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25311 the reason we use a separate variable `i'. */
25312 i = start;
25313 USE_SAFE_ALLOCA;
25314 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25315 if (tail)
25316 x_reached = tail->x + tail->background_width;
25317 else
25318 x_reached = x;
25319
25320 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25321 the row, redraw some glyphs in front or following the glyph
25322 strings built above. */
25323 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25324 {
25325 struct glyph_string *h, *t;
25326 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25327 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25328 bool check_mouse_face = false;
25329 int dummy_x = 0;
25330
25331 /* If mouse highlighting is on, we may need to draw adjacent
25332 glyphs using mouse-face highlighting. */
25333 if (area == TEXT_AREA && row->mouse_face_p
25334 && hlinfo->mouse_face_beg_row >= 0
25335 && hlinfo->mouse_face_end_row >= 0)
25336 {
25337 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25338
25339 if (row_vpos >= hlinfo->mouse_face_beg_row
25340 && row_vpos <= hlinfo->mouse_face_end_row)
25341 {
25342 check_mouse_face = true;
25343 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25344 ? hlinfo->mouse_face_beg_col : 0;
25345 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25346 ? hlinfo->mouse_face_end_col
25347 : row->used[TEXT_AREA];
25348 }
25349 }
25350
25351 /* Compute overhangs for all glyph strings. */
25352 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25353 for (s = head; s; s = s->next)
25354 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25355
25356 /* Prepend glyph strings for glyphs in front of the first glyph
25357 string that are overwritten because of the first glyph
25358 string's left overhang. The background of all strings
25359 prepended must be drawn because the first glyph string
25360 draws over it. */
25361 i = left_overwritten (head);
25362 if (i >= 0)
25363 {
25364 enum draw_glyphs_face overlap_hl;
25365
25366 /* If this row contains mouse highlighting, attempt to draw
25367 the overlapped glyphs with the correct highlight. This
25368 code fails if the overlap encompasses more than one glyph
25369 and mouse-highlight spans only some of these glyphs.
25370 However, making it work perfectly involves a lot more
25371 code, and I don't know if the pathological case occurs in
25372 practice, so we'll stick to this for now. --- cyd */
25373 if (check_mouse_face
25374 && mouse_beg_col < start && mouse_end_col > i)
25375 overlap_hl = DRAW_MOUSE_FACE;
25376 else
25377 overlap_hl = DRAW_NORMAL_TEXT;
25378
25379 if (hl != overlap_hl)
25380 clip_head = head;
25381 j = i;
25382 BUILD_GLYPH_STRINGS (j, start, h, t,
25383 overlap_hl, dummy_x, last_x);
25384 start = i;
25385 compute_overhangs_and_x (t, head->x, true);
25386 prepend_glyph_string_lists (&head, &tail, h, t);
25387 if (clip_head == NULL)
25388 clip_head = head;
25389 }
25390
25391 /* Prepend glyph strings for glyphs in front of the first glyph
25392 string that overwrite that glyph string because of their
25393 right overhang. For these strings, only the foreground must
25394 be drawn, because it draws over the glyph string at `head'.
25395 The background must not be drawn because this would overwrite
25396 right overhangs of preceding glyphs for which no glyph
25397 strings exist. */
25398 i = left_overwriting (head);
25399 if (i >= 0)
25400 {
25401 enum draw_glyphs_face overlap_hl;
25402
25403 if (check_mouse_face
25404 && mouse_beg_col < start && mouse_end_col > i)
25405 overlap_hl = DRAW_MOUSE_FACE;
25406 else
25407 overlap_hl = DRAW_NORMAL_TEXT;
25408
25409 if (hl == overlap_hl || clip_head == NULL)
25410 clip_head = head;
25411 BUILD_GLYPH_STRINGS (i, start, h, t,
25412 overlap_hl, dummy_x, last_x);
25413 for (s = h; s; s = s->next)
25414 s->background_filled_p = true;
25415 compute_overhangs_and_x (t, head->x, true);
25416 prepend_glyph_string_lists (&head, &tail, h, t);
25417 }
25418
25419 /* Append glyphs strings for glyphs following the last glyph
25420 string tail that are overwritten by tail. The background of
25421 these strings has to be drawn because tail's foreground draws
25422 over it. */
25423 i = right_overwritten (tail);
25424 if (i >= 0)
25425 {
25426 enum draw_glyphs_face overlap_hl;
25427
25428 if (check_mouse_face
25429 && mouse_beg_col < i && mouse_end_col > end)
25430 overlap_hl = DRAW_MOUSE_FACE;
25431 else
25432 overlap_hl = DRAW_NORMAL_TEXT;
25433
25434 if (hl != overlap_hl)
25435 clip_tail = tail;
25436 BUILD_GLYPH_STRINGS (end, i, h, t,
25437 overlap_hl, x, last_x);
25438 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25439 we don't have `end = i;' here. */
25440 compute_overhangs_and_x (h, tail->x + tail->width, false);
25441 append_glyph_string_lists (&head, &tail, h, t);
25442 if (clip_tail == NULL)
25443 clip_tail = tail;
25444 }
25445
25446 /* Append glyph strings for glyphs following the last glyph
25447 string tail that overwrite tail. The foreground of such
25448 glyphs has to be drawn because it writes into the background
25449 of tail. The background must not be drawn because it could
25450 paint over the foreground of following glyphs. */
25451 i = right_overwriting (tail);
25452 if (i >= 0)
25453 {
25454 enum draw_glyphs_face overlap_hl;
25455 if (check_mouse_face
25456 && mouse_beg_col < i && mouse_end_col > end)
25457 overlap_hl = DRAW_MOUSE_FACE;
25458 else
25459 overlap_hl = DRAW_NORMAL_TEXT;
25460
25461 if (hl == overlap_hl || clip_tail == NULL)
25462 clip_tail = tail;
25463 i++; /* We must include the Ith glyph. */
25464 BUILD_GLYPH_STRINGS (end, i, h, t,
25465 overlap_hl, x, last_x);
25466 for (s = h; s; s = s->next)
25467 s->background_filled_p = true;
25468 compute_overhangs_and_x (h, tail->x + tail->width, false);
25469 append_glyph_string_lists (&head, &tail, h, t);
25470 }
25471 if (clip_head || clip_tail)
25472 for (s = head; s; s = s->next)
25473 {
25474 s->clip_head = clip_head;
25475 s->clip_tail = clip_tail;
25476 }
25477 }
25478
25479 /* Draw all strings. */
25480 for (s = head; s; s = s->next)
25481 FRAME_RIF (f)->draw_glyph_string (s);
25482
25483 #ifndef HAVE_NS
25484 /* When focus a sole frame and move horizontally, this clears on_p
25485 causing a failure to erase prev cursor position. */
25486 if (area == TEXT_AREA
25487 && !row->full_width_p
25488 /* When drawing overlapping rows, only the glyph strings'
25489 foreground is drawn, which doesn't erase a cursor
25490 completely. */
25491 && !overlaps)
25492 {
25493 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25494 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25495 : (tail ? tail->x + tail->background_width : x));
25496 x0 -= area_left;
25497 x1 -= area_left;
25498
25499 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25500 row->y, MATRIX_ROW_BOTTOM_Y (row));
25501 }
25502 #endif
25503
25504 /* Value is the x-position up to which drawn, relative to AREA of W.
25505 This doesn't include parts drawn because of overhangs. */
25506 if (row->full_width_p)
25507 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25508 else
25509 x_reached -= area_left;
25510
25511 RELEASE_HDC (hdc, f);
25512
25513 SAFE_FREE ();
25514 return x_reached;
25515 }
25516
25517 /* Expand row matrix if too narrow. Don't expand if area
25518 is not present. */
25519
25520 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25521 { \
25522 if (!it->f->fonts_changed \
25523 && (it->glyph_row->glyphs[area] \
25524 < it->glyph_row->glyphs[area + 1])) \
25525 { \
25526 it->w->ncols_scale_factor++; \
25527 it->f->fonts_changed = true; \
25528 } \
25529 }
25530
25531 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25532 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25533
25534 static void
25535 append_glyph (struct it *it)
25536 {
25537 struct glyph *glyph;
25538 enum glyph_row_area area = it->area;
25539
25540 eassert (it->glyph_row);
25541 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25542
25543 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25544 if (glyph < it->glyph_row->glyphs[area + 1])
25545 {
25546 /* If the glyph row is reversed, we need to prepend the glyph
25547 rather than append it. */
25548 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25549 {
25550 struct glyph *g;
25551
25552 /* Make room for the additional glyph. */
25553 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25554 g[1] = *g;
25555 glyph = it->glyph_row->glyphs[area];
25556 }
25557 glyph->charpos = CHARPOS (it->position);
25558 glyph->object = it->object;
25559 if (it->pixel_width > 0)
25560 {
25561 glyph->pixel_width = it->pixel_width;
25562 glyph->padding_p = false;
25563 }
25564 else
25565 {
25566 /* Assure at least 1-pixel width. Otherwise, cursor can't
25567 be displayed correctly. */
25568 glyph->pixel_width = 1;
25569 glyph->padding_p = true;
25570 }
25571 glyph->ascent = it->ascent;
25572 glyph->descent = it->descent;
25573 glyph->voffset = it->voffset;
25574 glyph->type = CHAR_GLYPH;
25575 glyph->avoid_cursor_p = it->avoid_cursor_p;
25576 glyph->multibyte_p = it->multibyte_p;
25577 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25578 {
25579 /* In R2L rows, the left and the right box edges need to be
25580 drawn in reverse direction. */
25581 glyph->right_box_line_p = it->start_of_box_run_p;
25582 glyph->left_box_line_p = it->end_of_box_run_p;
25583 }
25584 else
25585 {
25586 glyph->left_box_line_p = it->start_of_box_run_p;
25587 glyph->right_box_line_p = it->end_of_box_run_p;
25588 }
25589 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25590 || it->phys_descent > it->descent);
25591 glyph->glyph_not_available_p = it->glyph_not_available_p;
25592 glyph->face_id = it->face_id;
25593 glyph->u.ch = it->char_to_display;
25594 glyph->slice.img = null_glyph_slice;
25595 glyph->font_type = FONT_TYPE_UNKNOWN;
25596 if (it->bidi_p)
25597 {
25598 glyph->resolved_level = it->bidi_it.resolved_level;
25599 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25600 glyph->bidi_type = it->bidi_it.type;
25601 }
25602 else
25603 {
25604 glyph->resolved_level = 0;
25605 glyph->bidi_type = UNKNOWN_BT;
25606 }
25607 ++it->glyph_row->used[area];
25608 }
25609 else
25610 IT_EXPAND_MATRIX_WIDTH (it, area);
25611 }
25612
25613 /* Store one glyph for the composition IT->cmp_it.id in
25614 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25615 non-null. */
25616
25617 static void
25618 append_composite_glyph (struct it *it)
25619 {
25620 struct glyph *glyph;
25621 enum glyph_row_area area = it->area;
25622
25623 eassert (it->glyph_row);
25624
25625 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25626 if (glyph < it->glyph_row->glyphs[area + 1])
25627 {
25628 /* If the glyph row is reversed, we need to prepend the glyph
25629 rather than append it. */
25630 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25631 {
25632 struct glyph *g;
25633
25634 /* Make room for the new glyph. */
25635 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25636 g[1] = *g;
25637 glyph = it->glyph_row->glyphs[it->area];
25638 }
25639 glyph->charpos = it->cmp_it.charpos;
25640 glyph->object = it->object;
25641 glyph->pixel_width = it->pixel_width;
25642 glyph->ascent = it->ascent;
25643 glyph->descent = it->descent;
25644 glyph->voffset = it->voffset;
25645 glyph->type = COMPOSITE_GLYPH;
25646 if (it->cmp_it.ch < 0)
25647 {
25648 glyph->u.cmp.automatic = false;
25649 glyph->u.cmp.id = it->cmp_it.id;
25650 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25651 }
25652 else
25653 {
25654 glyph->u.cmp.automatic = true;
25655 glyph->u.cmp.id = it->cmp_it.id;
25656 glyph->slice.cmp.from = it->cmp_it.from;
25657 glyph->slice.cmp.to = it->cmp_it.to - 1;
25658 }
25659 glyph->avoid_cursor_p = it->avoid_cursor_p;
25660 glyph->multibyte_p = it->multibyte_p;
25661 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25662 {
25663 /* In R2L rows, the left and the right box edges need to be
25664 drawn in reverse direction. */
25665 glyph->right_box_line_p = it->start_of_box_run_p;
25666 glyph->left_box_line_p = it->end_of_box_run_p;
25667 }
25668 else
25669 {
25670 glyph->left_box_line_p = it->start_of_box_run_p;
25671 glyph->right_box_line_p = it->end_of_box_run_p;
25672 }
25673 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25674 || it->phys_descent > it->descent);
25675 glyph->padding_p = false;
25676 glyph->glyph_not_available_p = false;
25677 glyph->face_id = it->face_id;
25678 glyph->font_type = FONT_TYPE_UNKNOWN;
25679 if (it->bidi_p)
25680 {
25681 glyph->resolved_level = it->bidi_it.resolved_level;
25682 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25683 glyph->bidi_type = it->bidi_it.type;
25684 }
25685 ++it->glyph_row->used[area];
25686 }
25687 else
25688 IT_EXPAND_MATRIX_WIDTH (it, area);
25689 }
25690
25691
25692 /* Change IT->ascent and IT->height according to the setting of
25693 IT->voffset. */
25694
25695 static void
25696 take_vertical_position_into_account (struct it *it)
25697 {
25698 if (it->voffset)
25699 {
25700 if (it->voffset < 0)
25701 /* Increase the ascent so that we can display the text higher
25702 in the line. */
25703 it->ascent -= it->voffset;
25704 else
25705 /* Increase the descent so that we can display the text lower
25706 in the line. */
25707 it->descent += it->voffset;
25708 }
25709 }
25710
25711
25712 /* Produce glyphs/get display metrics for the image IT is loaded with.
25713 See the description of struct display_iterator in dispextern.h for
25714 an overview of struct display_iterator. */
25715
25716 static void
25717 produce_image_glyph (struct it *it)
25718 {
25719 struct image *img;
25720 struct face *face;
25721 int glyph_ascent, crop;
25722 struct glyph_slice slice;
25723
25724 eassert (it->what == IT_IMAGE);
25725
25726 face = FACE_FROM_ID (it->f, it->face_id);
25727 eassert (face);
25728 /* Make sure X resources of the face is loaded. */
25729 prepare_face_for_display (it->f, face);
25730
25731 if (it->image_id < 0)
25732 {
25733 /* Fringe bitmap. */
25734 it->ascent = it->phys_ascent = 0;
25735 it->descent = it->phys_descent = 0;
25736 it->pixel_width = 0;
25737 it->nglyphs = 0;
25738 return;
25739 }
25740
25741 img = IMAGE_FROM_ID (it->f, it->image_id);
25742 eassert (img);
25743 /* Make sure X resources of the image is loaded. */
25744 prepare_image_for_display (it->f, img);
25745
25746 slice.x = slice.y = 0;
25747 slice.width = img->width;
25748 slice.height = img->height;
25749
25750 if (INTEGERP (it->slice.x))
25751 slice.x = XINT (it->slice.x);
25752 else if (FLOATP (it->slice.x))
25753 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25754
25755 if (INTEGERP (it->slice.y))
25756 slice.y = XINT (it->slice.y);
25757 else if (FLOATP (it->slice.y))
25758 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25759
25760 if (INTEGERP (it->slice.width))
25761 slice.width = XINT (it->slice.width);
25762 else if (FLOATP (it->slice.width))
25763 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25764
25765 if (INTEGERP (it->slice.height))
25766 slice.height = XINT (it->slice.height);
25767 else if (FLOATP (it->slice.height))
25768 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25769
25770 if (slice.x >= img->width)
25771 slice.x = img->width;
25772 if (slice.y >= img->height)
25773 slice.y = img->height;
25774 if (slice.x + slice.width >= img->width)
25775 slice.width = img->width - slice.x;
25776 if (slice.y + slice.height > img->height)
25777 slice.height = img->height - slice.y;
25778
25779 if (slice.width == 0 || slice.height == 0)
25780 return;
25781
25782 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25783
25784 it->descent = slice.height - glyph_ascent;
25785 if (slice.y == 0)
25786 it->descent += img->vmargin;
25787 if (slice.y + slice.height == img->height)
25788 it->descent += img->vmargin;
25789 it->phys_descent = it->descent;
25790
25791 it->pixel_width = slice.width;
25792 if (slice.x == 0)
25793 it->pixel_width += img->hmargin;
25794 if (slice.x + slice.width == img->width)
25795 it->pixel_width += img->hmargin;
25796
25797 /* It's quite possible for images to have an ascent greater than
25798 their height, so don't get confused in that case. */
25799 if (it->descent < 0)
25800 it->descent = 0;
25801
25802 it->nglyphs = 1;
25803
25804 if (face->box != FACE_NO_BOX)
25805 {
25806 if (face->box_line_width > 0)
25807 {
25808 if (slice.y == 0)
25809 it->ascent += face->box_line_width;
25810 if (slice.y + slice.height == img->height)
25811 it->descent += face->box_line_width;
25812 }
25813
25814 if (it->start_of_box_run_p && slice.x == 0)
25815 it->pixel_width += eabs (face->box_line_width);
25816 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25817 it->pixel_width += eabs (face->box_line_width);
25818 }
25819
25820 take_vertical_position_into_account (it);
25821
25822 /* Automatically crop wide image glyphs at right edge so we can
25823 draw the cursor on same display row. */
25824 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25825 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25826 {
25827 it->pixel_width -= crop;
25828 slice.width -= crop;
25829 }
25830
25831 if (it->glyph_row)
25832 {
25833 struct glyph *glyph;
25834 enum glyph_row_area area = it->area;
25835
25836 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25837 if (it->glyph_row->reversed_p)
25838 {
25839 struct glyph *g;
25840
25841 /* Make room for the new glyph. */
25842 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25843 g[1] = *g;
25844 glyph = it->glyph_row->glyphs[it->area];
25845 }
25846 if (glyph < it->glyph_row->glyphs[area + 1])
25847 {
25848 glyph->charpos = CHARPOS (it->position);
25849 glyph->object = it->object;
25850 glyph->pixel_width = it->pixel_width;
25851 glyph->ascent = glyph_ascent;
25852 glyph->descent = it->descent;
25853 glyph->voffset = it->voffset;
25854 glyph->type = IMAGE_GLYPH;
25855 glyph->avoid_cursor_p = it->avoid_cursor_p;
25856 glyph->multibyte_p = it->multibyte_p;
25857 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25858 {
25859 /* In R2L rows, the left and the right box edges need to be
25860 drawn in reverse direction. */
25861 glyph->right_box_line_p = it->start_of_box_run_p;
25862 glyph->left_box_line_p = it->end_of_box_run_p;
25863 }
25864 else
25865 {
25866 glyph->left_box_line_p = it->start_of_box_run_p;
25867 glyph->right_box_line_p = it->end_of_box_run_p;
25868 }
25869 glyph->overlaps_vertically_p = false;
25870 glyph->padding_p = false;
25871 glyph->glyph_not_available_p = false;
25872 glyph->face_id = it->face_id;
25873 glyph->u.img_id = img->id;
25874 glyph->slice.img = slice;
25875 glyph->font_type = FONT_TYPE_UNKNOWN;
25876 if (it->bidi_p)
25877 {
25878 glyph->resolved_level = it->bidi_it.resolved_level;
25879 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25880 glyph->bidi_type = it->bidi_it.type;
25881 }
25882 ++it->glyph_row->used[area];
25883 }
25884 else
25885 IT_EXPAND_MATRIX_WIDTH (it, area);
25886 }
25887 }
25888
25889
25890 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25891 of the glyph, WIDTH and HEIGHT are the width and height of the
25892 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25893
25894 static void
25895 append_stretch_glyph (struct it *it, Lisp_Object object,
25896 int width, int height, int ascent)
25897 {
25898 struct glyph *glyph;
25899 enum glyph_row_area area = it->area;
25900
25901 eassert (ascent >= 0 && ascent <= height);
25902
25903 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25904 if (glyph < it->glyph_row->glyphs[area + 1])
25905 {
25906 /* If the glyph row is reversed, we need to prepend the glyph
25907 rather than append it. */
25908 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25909 {
25910 struct glyph *g;
25911
25912 /* Make room for the additional glyph. */
25913 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25914 g[1] = *g;
25915 glyph = it->glyph_row->glyphs[area];
25916
25917 /* Decrease the width of the first glyph of the row that
25918 begins before first_visible_x (e.g., due to hscroll).
25919 This is so the overall width of the row becomes smaller
25920 by the scroll amount, and the stretch glyph appended by
25921 extend_face_to_end_of_line will be wider, to shift the
25922 row glyphs to the right. (In L2R rows, the corresponding
25923 left-shift effect is accomplished by setting row->x to a
25924 negative value, which won't work with R2L rows.)
25925
25926 This must leave us with a positive value of WIDTH, since
25927 otherwise the call to move_it_in_display_line_to at the
25928 beginning of display_line would have got past the entire
25929 first glyph, and then it->current_x would have been
25930 greater or equal to it->first_visible_x. */
25931 if (it->current_x < it->first_visible_x)
25932 width -= it->first_visible_x - it->current_x;
25933 eassert (width > 0);
25934 }
25935 glyph->charpos = CHARPOS (it->position);
25936 glyph->object = object;
25937 glyph->pixel_width = width;
25938 glyph->ascent = ascent;
25939 glyph->descent = height - ascent;
25940 glyph->voffset = it->voffset;
25941 glyph->type = STRETCH_GLYPH;
25942 glyph->avoid_cursor_p = it->avoid_cursor_p;
25943 glyph->multibyte_p = it->multibyte_p;
25944 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25945 {
25946 /* In R2L rows, the left and the right box edges need to be
25947 drawn in reverse direction. */
25948 glyph->right_box_line_p = it->start_of_box_run_p;
25949 glyph->left_box_line_p = it->end_of_box_run_p;
25950 }
25951 else
25952 {
25953 glyph->left_box_line_p = it->start_of_box_run_p;
25954 glyph->right_box_line_p = it->end_of_box_run_p;
25955 }
25956 glyph->overlaps_vertically_p = false;
25957 glyph->padding_p = false;
25958 glyph->glyph_not_available_p = false;
25959 glyph->face_id = it->face_id;
25960 glyph->u.stretch.ascent = ascent;
25961 glyph->u.stretch.height = height;
25962 glyph->slice.img = null_glyph_slice;
25963 glyph->font_type = FONT_TYPE_UNKNOWN;
25964 if (it->bidi_p)
25965 {
25966 glyph->resolved_level = it->bidi_it.resolved_level;
25967 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25968 glyph->bidi_type = it->bidi_it.type;
25969 }
25970 else
25971 {
25972 glyph->resolved_level = 0;
25973 glyph->bidi_type = UNKNOWN_BT;
25974 }
25975 ++it->glyph_row->used[area];
25976 }
25977 else
25978 IT_EXPAND_MATRIX_WIDTH (it, area);
25979 }
25980
25981 #endif /* HAVE_WINDOW_SYSTEM */
25982
25983 /* Produce a stretch glyph for iterator IT. IT->object is the value
25984 of the glyph property displayed. The value must be a list
25985 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25986 being recognized:
25987
25988 1. `:width WIDTH' specifies that the space should be WIDTH *
25989 canonical char width wide. WIDTH may be an integer or floating
25990 point number.
25991
25992 2. `:relative-width FACTOR' specifies that the width of the stretch
25993 should be computed from the width of the first character having the
25994 `glyph' property, and should be FACTOR times that width.
25995
25996 3. `:align-to HPOS' specifies that the space should be wide enough
25997 to reach HPOS, a value in canonical character units.
25998
25999 Exactly one of the above pairs must be present.
26000
26001 4. `:height HEIGHT' specifies that the height of the stretch produced
26002 should be HEIGHT, measured in canonical character units.
26003
26004 5. `:relative-height FACTOR' specifies that the height of the
26005 stretch should be FACTOR times the height of the characters having
26006 the glyph property.
26007
26008 Either none or exactly one of 4 or 5 must be present.
26009
26010 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26011 of the stretch should be used for the ascent of the stretch.
26012 ASCENT must be in the range 0 <= ASCENT <= 100. */
26013
26014 void
26015 produce_stretch_glyph (struct it *it)
26016 {
26017 /* (space :width WIDTH :height HEIGHT ...) */
26018 Lisp_Object prop, plist;
26019 int width = 0, height = 0, align_to = -1;
26020 bool zero_width_ok_p = false;
26021 double tem;
26022 struct font *font = NULL;
26023
26024 #ifdef HAVE_WINDOW_SYSTEM
26025 int ascent = 0;
26026 bool zero_height_ok_p = false;
26027
26028 if (FRAME_WINDOW_P (it->f))
26029 {
26030 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26031 font = face->font ? face->font : FRAME_FONT (it->f);
26032 prepare_face_for_display (it->f, face);
26033 }
26034 #endif
26035
26036 /* List should start with `space'. */
26037 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26038 plist = XCDR (it->object);
26039
26040 /* Compute the width of the stretch. */
26041 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26042 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26043 {
26044 /* Absolute width `:width WIDTH' specified and valid. */
26045 zero_width_ok_p = true;
26046 width = (int)tem;
26047 }
26048 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26049 {
26050 /* Relative width `:relative-width FACTOR' specified and valid.
26051 Compute the width of the characters having the `glyph'
26052 property. */
26053 struct it it2;
26054 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26055
26056 it2 = *it;
26057 if (it->multibyte_p)
26058 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26059 else
26060 {
26061 it2.c = it2.char_to_display = *p, it2.len = 1;
26062 if (! ASCII_CHAR_P (it2.c))
26063 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26064 }
26065
26066 it2.glyph_row = NULL;
26067 it2.what = IT_CHARACTER;
26068 PRODUCE_GLYPHS (&it2);
26069 width = NUMVAL (prop) * it2.pixel_width;
26070 }
26071 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26072 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26073 &align_to))
26074 {
26075 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26076 align_to = (align_to < 0
26077 ? 0
26078 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26079 else if (align_to < 0)
26080 align_to = window_box_left_offset (it->w, TEXT_AREA);
26081 width = max (0, (int)tem + align_to - it->current_x);
26082 zero_width_ok_p = true;
26083 }
26084 else
26085 /* Nothing specified -> width defaults to canonical char width. */
26086 width = FRAME_COLUMN_WIDTH (it->f);
26087
26088 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26089 width = 1;
26090
26091 #ifdef HAVE_WINDOW_SYSTEM
26092 /* Compute height. */
26093 if (FRAME_WINDOW_P (it->f))
26094 {
26095 int default_height = normal_char_height (font, ' ');
26096
26097 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26098 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26099 {
26100 height = (int)tem;
26101 zero_height_ok_p = true;
26102 }
26103 else if (prop = Fplist_get (plist, QCrelative_height),
26104 NUMVAL (prop) > 0)
26105 height = default_height * NUMVAL (prop);
26106 else
26107 height = default_height;
26108
26109 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26110 height = 1;
26111
26112 /* Compute percentage of height used for ascent. If
26113 `:ascent ASCENT' is present and valid, use that. Otherwise,
26114 derive the ascent from the font in use. */
26115 if (prop = Fplist_get (plist, QCascent),
26116 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26117 ascent = height * NUMVAL (prop) / 100.0;
26118 else if (!NILP (prop)
26119 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26120 ascent = min (max (0, (int)tem), height);
26121 else
26122 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26123 }
26124 else
26125 #endif /* HAVE_WINDOW_SYSTEM */
26126 height = 1;
26127
26128 if (width > 0 && it->line_wrap != TRUNCATE
26129 && it->current_x + width > it->last_visible_x)
26130 {
26131 width = it->last_visible_x - it->current_x;
26132 #ifdef HAVE_WINDOW_SYSTEM
26133 /* Subtract one more pixel from the stretch width, but only on
26134 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26135 width -= FRAME_WINDOW_P (it->f);
26136 #endif
26137 }
26138
26139 if (width > 0 && height > 0 && it->glyph_row)
26140 {
26141 Lisp_Object o_object = it->object;
26142 Lisp_Object object = it->stack[it->sp - 1].string;
26143 int n = width;
26144
26145 if (!STRINGP (object))
26146 object = it->w->contents;
26147 #ifdef HAVE_WINDOW_SYSTEM
26148 if (FRAME_WINDOW_P (it->f))
26149 append_stretch_glyph (it, object, width, height, ascent);
26150 else
26151 #endif
26152 {
26153 it->object = object;
26154 it->char_to_display = ' ';
26155 it->pixel_width = it->len = 1;
26156 while (n--)
26157 tty_append_glyph (it);
26158 it->object = o_object;
26159 }
26160 }
26161
26162 it->pixel_width = width;
26163 #ifdef HAVE_WINDOW_SYSTEM
26164 if (FRAME_WINDOW_P (it->f))
26165 {
26166 it->ascent = it->phys_ascent = ascent;
26167 it->descent = it->phys_descent = height - it->ascent;
26168 it->nglyphs = width > 0 && height > 0;
26169 take_vertical_position_into_account (it);
26170 }
26171 else
26172 #endif
26173 it->nglyphs = width;
26174 }
26175
26176 /* Get information about special display element WHAT in an
26177 environment described by IT. WHAT is one of IT_TRUNCATION or
26178 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26179 non-null glyph_row member. This function ensures that fields like
26180 face_id, c, len of IT are left untouched. */
26181
26182 static void
26183 produce_special_glyphs (struct it *it, enum display_element_type what)
26184 {
26185 struct it temp_it;
26186 Lisp_Object gc;
26187 GLYPH glyph;
26188
26189 temp_it = *it;
26190 temp_it.object = Qnil;
26191 memset (&temp_it.current, 0, sizeof temp_it.current);
26192
26193 if (what == IT_CONTINUATION)
26194 {
26195 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26196 if (it->bidi_it.paragraph_dir == R2L)
26197 SET_GLYPH_FROM_CHAR (glyph, '/');
26198 else
26199 SET_GLYPH_FROM_CHAR (glyph, '\\');
26200 if (it->dp
26201 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26202 {
26203 /* FIXME: Should we mirror GC for R2L lines? */
26204 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26205 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26206 }
26207 }
26208 else if (what == IT_TRUNCATION)
26209 {
26210 /* Truncation glyph. */
26211 SET_GLYPH_FROM_CHAR (glyph, '$');
26212 if (it->dp
26213 && (gc = DISP_TRUNC_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
26221 emacs_abort ();
26222
26223 #ifdef HAVE_WINDOW_SYSTEM
26224 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26225 is turned off, we precede the truncation/continuation glyphs by a
26226 stretch glyph whose width is computed such that these special
26227 glyphs are aligned at the window margin, even when very different
26228 fonts are used in different glyph rows. */
26229 if (FRAME_WINDOW_P (temp_it.f)
26230 /* init_iterator calls this with it->glyph_row == NULL, and it
26231 wants only the pixel width of the truncation/continuation
26232 glyphs. */
26233 && temp_it.glyph_row
26234 /* insert_left_trunc_glyphs calls us at the beginning of the
26235 row, and it has its own calculation of the stretch glyph
26236 width. */
26237 && temp_it.glyph_row->used[TEXT_AREA] > 0
26238 && (temp_it.glyph_row->reversed_p
26239 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26240 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26241 {
26242 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26243
26244 if (stretch_width > 0)
26245 {
26246 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26247 struct font *font =
26248 face->font ? face->font : FRAME_FONT (temp_it.f);
26249 int stretch_ascent =
26250 (((temp_it.ascent + temp_it.descent)
26251 * FONT_BASE (font)) / FONT_HEIGHT (font));
26252
26253 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26254 temp_it.ascent + temp_it.descent,
26255 stretch_ascent);
26256 }
26257 }
26258 #endif
26259
26260 temp_it.dp = NULL;
26261 temp_it.what = IT_CHARACTER;
26262 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26263 temp_it.face_id = GLYPH_FACE (glyph);
26264 temp_it.len = CHAR_BYTES (temp_it.c);
26265
26266 PRODUCE_GLYPHS (&temp_it);
26267 it->pixel_width = temp_it.pixel_width;
26268 it->nglyphs = temp_it.nglyphs;
26269 }
26270
26271 #ifdef HAVE_WINDOW_SYSTEM
26272
26273 /* Calculate line-height and line-spacing properties.
26274 An integer value specifies explicit pixel value.
26275 A float value specifies relative value to current face height.
26276 A cons (float . face-name) specifies relative value to
26277 height of specified face font.
26278
26279 Returns height in pixels, or nil. */
26280
26281 static Lisp_Object
26282 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26283 int boff, bool override)
26284 {
26285 Lisp_Object face_name = Qnil;
26286 int ascent, descent, height;
26287
26288 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26289 return val;
26290
26291 if (CONSP (val))
26292 {
26293 face_name = XCAR (val);
26294 val = XCDR (val);
26295 if (!NUMBERP (val))
26296 val = make_number (1);
26297 if (NILP (face_name))
26298 {
26299 height = it->ascent + it->descent;
26300 goto scale;
26301 }
26302 }
26303
26304 if (NILP (face_name))
26305 {
26306 font = FRAME_FONT (it->f);
26307 boff = FRAME_BASELINE_OFFSET (it->f);
26308 }
26309 else if (EQ (face_name, Qt))
26310 {
26311 override = false;
26312 }
26313 else
26314 {
26315 int face_id;
26316 struct face *face;
26317
26318 face_id = lookup_named_face (it->f, face_name, false);
26319 if (face_id < 0)
26320 return make_number (-1);
26321
26322 face = FACE_FROM_ID (it->f, face_id);
26323 font = face->font;
26324 if (font == NULL)
26325 return make_number (-1);
26326 boff = font->baseline_offset;
26327 if (font->vertical_centering)
26328 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26329 }
26330
26331 normal_char_ascent_descent (font, -1, &ascent, &descent);
26332
26333 if (override)
26334 {
26335 it->override_ascent = ascent;
26336 it->override_descent = descent;
26337 it->override_boff = boff;
26338 }
26339
26340 height = ascent + descent;
26341
26342 scale:
26343 if (FLOATP (val))
26344 height = (int)(XFLOAT_DATA (val) * height);
26345 else if (INTEGERP (val))
26346 height *= XINT (val);
26347
26348 return make_number (height);
26349 }
26350
26351
26352 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26353 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26354 and only if this is for a character for which no font was found.
26355
26356 If the display method (it->glyphless_method) is
26357 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26358 length of the acronym or the hexadecimal string, UPPER_XOFF and
26359 UPPER_YOFF are pixel offsets for the upper part of the string,
26360 LOWER_XOFF and LOWER_YOFF are for the lower part.
26361
26362 For the other display methods, LEN through LOWER_YOFF are zero. */
26363
26364 static void
26365 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26366 short upper_xoff, short upper_yoff,
26367 short lower_xoff, short lower_yoff)
26368 {
26369 struct glyph *glyph;
26370 enum glyph_row_area area = it->area;
26371
26372 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26373 if (glyph < it->glyph_row->glyphs[area + 1])
26374 {
26375 /* If the glyph row is reversed, we need to prepend the glyph
26376 rather than append it. */
26377 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26378 {
26379 struct glyph *g;
26380
26381 /* Make room for the additional glyph. */
26382 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26383 g[1] = *g;
26384 glyph = it->glyph_row->glyphs[area];
26385 }
26386 glyph->charpos = CHARPOS (it->position);
26387 glyph->object = it->object;
26388 glyph->pixel_width = it->pixel_width;
26389 glyph->ascent = it->ascent;
26390 glyph->descent = it->descent;
26391 glyph->voffset = it->voffset;
26392 glyph->type = GLYPHLESS_GLYPH;
26393 glyph->u.glyphless.method = it->glyphless_method;
26394 glyph->u.glyphless.for_no_font = for_no_font;
26395 glyph->u.glyphless.len = len;
26396 glyph->u.glyphless.ch = it->c;
26397 glyph->slice.glyphless.upper_xoff = upper_xoff;
26398 glyph->slice.glyphless.upper_yoff = upper_yoff;
26399 glyph->slice.glyphless.lower_xoff = lower_xoff;
26400 glyph->slice.glyphless.lower_yoff = lower_yoff;
26401 glyph->avoid_cursor_p = it->avoid_cursor_p;
26402 glyph->multibyte_p = it->multibyte_p;
26403 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26404 {
26405 /* In R2L rows, the left and the right box edges need to be
26406 drawn in reverse direction. */
26407 glyph->right_box_line_p = it->start_of_box_run_p;
26408 glyph->left_box_line_p = it->end_of_box_run_p;
26409 }
26410 else
26411 {
26412 glyph->left_box_line_p = it->start_of_box_run_p;
26413 glyph->right_box_line_p = it->end_of_box_run_p;
26414 }
26415 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26416 || it->phys_descent > it->descent);
26417 glyph->padding_p = false;
26418 glyph->glyph_not_available_p = false;
26419 glyph->face_id = face_id;
26420 glyph->font_type = FONT_TYPE_UNKNOWN;
26421 if (it->bidi_p)
26422 {
26423 glyph->resolved_level = it->bidi_it.resolved_level;
26424 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26425 glyph->bidi_type = it->bidi_it.type;
26426 }
26427 ++it->glyph_row->used[area];
26428 }
26429 else
26430 IT_EXPAND_MATRIX_WIDTH (it, area);
26431 }
26432
26433
26434 /* Produce a glyph for a glyphless character for iterator IT.
26435 IT->glyphless_method specifies which method to use for displaying
26436 the character. See the description of enum
26437 glyphless_display_method in dispextern.h for the detail.
26438
26439 FOR_NO_FONT is true if and only if this is for a character for
26440 which no font was found. ACRONYM, if non-nil, is an acronym string
26441 for the character. */
26442
26443 static void
26444 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26445 {
26446 int face_id;
26447 struct face *face;
26448 struct font *font;
26449 int base_width, base_height, width, height;
26450 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26451 int len;
26452
26453 /* Get the metrics of the base font. We always refer to the current
26454 ASCII face. */
26455 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26456 font = face->font ? face->font : FRAME_FONT (it->f);
26457 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
26458 it->ascent += font->baseline_offset;
26459 it->descent -= font->baseline_offset;
26460 base_height = it->ascent + it->descent;
26461 base_width = font->average_width;
26462
26463 face_id = merge_glyphless_glyph_face (it);
26464
26465 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26466 {
26467 it->pixel_width = THIN_SPACE_WIDTH;
26468 len = 0;
26469 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26470 }
26471 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26472 {
26473 width = CHAR_WIDTH (it->c);
26474 if (width == 0)
26475 width = 1;
26476 else if (width > 4)
26477 width = 4;
26478 it->pixel_width = base_width * width;
26479 len = 0;
26480 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26481 }
26482 else
26483 {
26484 char buf[7];
26485 const char *str;
26486 unsigned int code[6];
26487 int upper_len;
26488 int ascent, descent;
26489 struct font_metrics metrics_upper, metrics_lower;
26490
26491 face = FACE_FROM_ID (it->f, face_id);
26492 font = face->font ? face->font : FRAME_FONT (it->f);
26493 prepare_face_for_display (it->f, face);
26494
26495 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26496 {
26497 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26498 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26499 if (CONSP (acronym))
26500 acronym = XCAR (acronym);
26501 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26502 }
26503 else
26504 {
26505 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26506 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
26507 str = buf;
26508 }
26509 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26510 code[len] = font->driver->encode_char (font, str[len]);
26511 upper_len = (len + 1) / 2;
26512 font->driver->text_extents (font, code, upper_len,
26513 &metrics_upper);
26514 font->driver->text_extents (font, code + upper_len, len - upper_len,
26515 &metrics_lower);
26516
26517
26518
26519 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26520 width = max (metrics_upper.width, metrics_lower.width) + 4;
26521 upper_xoff = upper_yoff = 2; /* the typical case */
26522 if (base_width >= width)
26523 {
26524 /* Align the upper to the left, the lower to the right. */
26525 it->pixel_width = base_width;
26526 lower_xoff = base_width - 2 - metrics_lower.width;
26527 }
26528 else
26529 {
26530 /* Center the shorter one. */
26531 it->pixel_width = width;
26532 if (metrics_upper.width >= metrics_lower.width)
26533 lower_xoff = (width - metrics_lower.width) / 2;
26534 else
26535 {
26536 /* FIXME: This code doesn't look right. It formerly was
26537 missing the "lower_xoff = 0;", which couldn't have
26538 been right since it left lower_xoff uninitialized. */
26539 lower_xoff = 0;
26540 upper_xoff = (width - metrics_upper.width) / 2;
26541 }
26542 }
26543
26544 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26545 top, bottom, and between upper and lower strings. */
26546 height = (metrics_upper.ascent + metrics_upper.descent
26547 + metrics_lower.ascent + metrics_lower.descent) + 5;
26548 /* Center vertically.
26549 H:base_height, D:base_descent
26550 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26551
26552 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26553 descent = D - H/2 + h/2;
26554 lower_yoff = descent - 2 - ld;
26555 upper_yoff = lower_yoff - la - 1 - ud; */
26556 ascent = - (it->descent - (base_height + height + 1) / 2);
26557 descent = it->descent - (base_height - height) / 2;
26558 lower_yoff = descent - 2 - metrics_lower.descent;
26559 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26560 - metrics_upper.descent);
26561 /* Don't make the height shorter than the base height. */
26562 if (height > base_height)
26563 {
26564 it->ascent = ascent;
26565 it->descent = descent;
26566 }
26567 }
26568
26569 it->phys_ascent = it->ascent;
26570 it->phys_descent = it->descent;
26571 if (it->glyph_row)
26572 append_glyphless_glyph (it, face_id, for_no_font, len,
26573 upper_xoff, upper_yoff,
26574 lower_xoff, lower_yoff);
26575 it->nglyphs = 1;
26576 take_vertical_position_into_account (it);
26577 }
26578
26579
26580 /* RIF:
26581 Produce glyphs/get display metrics for the display element IT is
26582 loaded with. See the description of struct it in dispextern.h
26583 for an overview of struct it. */
26584
26585 void
26586 x_produce_glyphs (struct it *it)
26587 {
26588 int extra_line_spacing = it->extra_line_spacing;
26589
26590 it->glyph_not_available_p = false;
26591
26592 if (it->what == IT_CHARACTER)
26593 {
26594 XChar2b char2b;
26595 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26596 struct font *font = face->font;
26597 struct font_metrics *pcm = NULL;
26598 int boff; /* Baseline offset. */
26599
26600 if (font == NULL)
26601 {
26602 /* When no suitable font is found, display this character by
26603 the method specified in the first extra slot of
26604 Vglyphless_char_display. */
26605 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26606
26607 eassert (it->what == IT_GLYPHLESS);
26608 produce_glyphless_glyph (it, true,
26609 STRINGP (acronym) ? acronym : Qnil);
26610 goto done;
26611 }
26612
26613 boff = font->baseline_offset;
26614 if (font->vertical_centering)
26615 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26616
26617 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26618 {
26619 it->nglyphs = 1;
26620
26621 if (it->override_ascent >= 0)
26622 {
26623 it->ascent = it->override_ascent;
26624 it->descent = it->override_descent;
26625 boff = it->override_boff;
26626 }
26627 else
26628 {
26629 it->ascent = FONT_BASE (font) + boff;
26630 it->descent = FONT_DESCENT (font) - boff;
26631 }
26632
26633 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26634 {
26635 pcm = get_per_char_metric (font, &char2b);
26636 if (pcm->width == 0
26637 && pcm->rbearing == 0 && pcm->lbearing == 0)
26638 pcm = NULL;
26639 }
26640
26641 if (pcm)
26642 {
26643 it->phys_ascent = pcm->ascent + boff;
26644 it->phys_descent = pcm->descent - boff;
26645 it->pixel_width = pcm->width;
26646 /* Don't use font-global values for ascent and descent
26647 if they result in an exceedingly large line height. */
26648 if (it->override_ascent < 0)
26649 {
26650 if (FONT_TOO_HIGH (font))
26651 {
26652 it->ascent = it->phys_ascent;
26653 it->descent = it->phys_descent;
26654 /* These limitations are enforced by an
26655 assertion near the end of this function. */
26656 if (it->ascent < 0)
26657 it->ascent = 0;
26658 if (it->descent < 0)
26659 it->descent = 0;
26660 }
26661 }
26662 }
26663 else
26664 {
26665 it->glyph_not_available_p = true;
26666 it->phys_ascent = it->ascent;
26667 it->phys_descent = it->descent;
26668 it->pixel_width = font->space_width;
26669 }
26670
26671 if (it->constrain_row_ascent_descent_p)
26672 {
26673 if (it->descent > it->max_descent)
26674 {
26675 it->ascent += it->descent - it->max_descent;
26676 it->descent = it->max_descent;
26677 }
26678 if (it->ascent > it->max_ascent)
26679 {
26680 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26681 it->ascent = it->max_ascent;
26682 }
26683 it->phys_ascent = min (it->phys_ascent, it->ascent);
26684 it->phys_descent = min (it->phys_descent, it->descent);
26685 extra_line_spacing = 0;
26686 }
26687
26688 /* If this is a space inside a region of text with
26689 `space-width' property, change its width. */
26690 bool stretched_p
26691 = it->char_to_display == ' ' && !NILP (it->space_width);
26692 if (stretched_p)
26693 it->pixel_width *= XFLOATINT (it->space_width);
26694
26695 /* If face has a box, add the box thickness to the character
26696 height. If character has a box line to the left and/or
26697 right, add the box line width to the character's width. */
26698 if (face->box != FACE_NO_BOX)
26699 {
26700 int thick = face->box_line_width;
26701
26702 if (thick > 0)
26703 {
26704 it->ascent += thick;
26705 it->descent += thick;
26706 }
26707 else
26708 thick = -thick;
26709
26710 if (it->start_of_box_run_p)
26711 it->pixel_width += thick;
26712 if (it->end_of_box_run_p)
26713 it->pixel_width += thick;
26714 }
26715
26716 /* If face has an overline, add the height of the overline
26717 (1 pixel) and a 1 pixel margin to the character height. */
26718 if (face->overline_p)
26719 it->ascent += overline_margin;
26720
26721 if (it->constrain_row_ascent_descent_p)
26722 {
26723 if (it->ascent > it->max_ascent)
26724 it->ascent = it->max_ascent;
26725 if (it->descent > it->max_descent)
26726 it->descent = it->max_descent;
26727 }
26728
26729 take_vertical_position_into_account (it);
26730
26731 /* If we have to actually produce glyphs, do it. */
26732 if (it->glyph_row)
26733 {
26734 if (stretched_p)
26735 {
26736 /* Translate a space with a `space-width' property
26737 into a stretch glyph. */
26738 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26739 / FONT_HEIGHT (font));
26740 append_stretch_glyph (it, it->object, it->pixel_width,
26741 it->ascent + it->descent, ascent);
26742 }
26743 else
26744 append_glyph (it);
26745
26746 /* If characters with lbearing or rbearing are displayed
26747 in this line, record that fact in a flag of the
26748 glyph row. This is used to optimize X output code. */
26749 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26750 it->glyph_row->contains_overlapping_glyphs_p = true;
26751 }
26752 if (! stretched_p && it->pixel_width == 0)
26753 /* We assure that all visible glyphs have at least 1-pixel
26754 width. */
26755 it->pixel_width = 1;
26756 }
26757 else if (it->char_to_display == '\n')
26758 {
26759 /* A newline has no width, but we need the height of the
26760 line. But if previous part of the line sets a height,
26761 don't increase that height. */
26762
26763 Lisp_Object height;
26764 Lisp_Object total_height = Qnil;
26765
26766 it->override_ascent = -1;
26767 it->pixel_width = 0;
26768 it->nglyphs = 0;
26769
26770 height = get_it_property (it, Qline_height);
26771 /* Split (line-height total-height) list. */
26772 if (CONSP (height)
26773 && CONSP (XCDR (height))
26774 && NILP (XCDR (XCDR (height))))
26775 {
26776 total_height = XCAR (XCDR (height));
26777 height = XCAR (height);
26778 }
26779 height = calc_line_height_property (it, height, font, boff, true);
26780
26781 if (it->override_ascent >= 0)
26782 {
26783 it->ascent = it->override_ascent;
26784 it->descent = it->override_descent;
26785 boff = it->override_boff;
26786 }
26787 else
26788 {
26789 if (FONT_TOO_HIGH (font))
26790 {
26791 it->ascent = font->pixel_size + boff - 1;
26792 it->descent = -boff + 1;
26793 if (it->descent < 0)
26794 it->descent = 0;
26795 }
26796 else
26797 {
26798 it->ascent = FONT_BASE (font) + boff;
26799 it->descent = FONT_DESCENT (font) - boff;
26800 }
26801 }
26802
26803 if (EQ (height, Qt))
26804 {
26805 if (it->descent > it->max_descent)
26806 {
26807 it->ascent += it->descent - it->max_descent;
26808 it->descent = it->max_descent;
26809 }
26810 if (it->ascent > it->max_ascent)
26811 {
26812 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26813 it->ascent = it->max_ascent;
26814 }
26815 it->phys_ascent = min (it->phys_ascent, it->ascent);
26816 it->phys_descent = min (it->phys_descent, it->descent);
26817 it->constrain_row_ascent_descent_p = true;
26818 extra_line_spacing = 0;
26819 }
26820 else
26821 {
26822 Lisp_Object spacing;
26823
26824 it->phys_ascent = it->ascent;
26825 it->phys_descent = it->descent;
26826
26827 if ((it->max_ascent > 0 || it->max_descent > 0)
26828 && face->box != FACE_NO_BOX
26829 && face->box_line_width > 0)
26830 {
26831 it->ascent += face->box_line_width;
26832 it->descent += face->box_line_width;
26833 }
26834 if (!NILP (height)
26835 && XINT (height) > it->ascent + it->descent)
26836 it->ascent = XINT (height) - it->descent;
26837
26838 if (!NILP (total_height))
26839 spacing = calc_line_height_property (it, total_height, font,
26840 boff, false);
26841 else
26842 {
26843 spacing = get_it_property (it, Qline_spacing);
26844 spacing = calc_line_height_property (it, spacing, font,
26845 boff, false);
26846 }
26847 if (INTEGERP (spacing))
26848 {
26849 extra_line_spacing = XINT (spacing);
26850 if (!NILP (total_height))
26851 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26852 }
26853 }
26854 }
26855 else /* i.e. (it->char_to_display == '\t') */
26856 {
26857 if (font->space_width > 0)
26858 {
26859 int tab_width = it->tab_width * font->space_width;
26860 int x = it->current_x + it->continuation_lines_width;
26861 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26862
26863 /* If the distance from the current position to the next tab
26864 stop is less than a space character width, use the
26865 tab stop after that. */
26866 if (next_tab_x - x < font->space_width)
26867 next_tab_x += tab_width;
26868
26869 it->pixel_width = next_tab_x - x;
26870 it->nglyphs = 1;
26871 if (FONT_TOO_HIGH (font))
26872 {
26873 if (get_char_glyph_code (' ', font, &char2b))
26874 {
26875 pcm = get_per_char_metric (font, &char2b);
26876 if (pcm->width == 0
26877 && pcm->rbearing == 0 && pcm->lbearing == 0)
26878 pcm = NULL;
26879 }
26880
26881 if (pcm)
26882 {
26883 it->ascent = pcm->ascent + boff;
26884 it->descent = pcm->descent - boff;
26885 }
26886 else
26887 {
26888 it->ascent = font->pixel_size + boff - 1;
26889 it->descent = -boff + 1;
26890 }
26891 if (it->ascent < 0)
26892 it->ascent = 0;
26893 if (it->descent < 0)
26894 it->descent = 0;
26895 }
26896 else
26897 {
26898 it->ascent = FONT_BASE (font) + boff;
26899 it->descent = FONT_DESCENT (font) - boff;
26900 }
26901 it->phys_ascent = it->ascent;
26902 it->phys_descent = it->descent;
26903
26904 if (it->glyph_row)
26905 {
26906 append_stretch_glyph (it, it->object, it->pixel_width,
26907 it->ascent + it->descent, it->ascent);
26908 }
26909 }
26910 else
26911 {
26912 it->pixel_width = 0;
26913 it->nglyphs = 1;
26914 }
26915 }
26916
26917 if (FONT_TOO_HIGH (font))
26918 {
26919 int font_ascent, font_descent;
26920
26921 /* For very large fonts, where we ignore the declared font
26922 dimensions, and go by per-character metrics instead,
26923 don't let the row ascent and descent values (and the row
26924 height computed from them) be smaller than the "normal"
26925 character metrics. This avoids unpleasant effects
26926 whereby lines on display would change their height
26927 depending on which characters are shown. */
26928 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
26929 it->max_ascent = max (it->max_ascent, font_ascent);
26930 it->max_descent = max (it->max_descent, font_descent);
26931 }
26932 }
26933 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26934 {
26935 /* A static composition.
26936
26937 Note: A composition is represented as one glyph in the
26938 glyph matrix. There are no padding glyphs.
26939
26940 Important note: pixel_width, ascent, and descent are the
26941 values of what is drawn by draw_glyphs (i.e. the values of
26942 the overall glyphs composed). */
26943 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26944 int boff; /* baseline offset */
26945 struct composition *cmp = composition_table[it->cmp_it.id];
26946 int glyph_len = cmp->glyph_len;
26947 struct font *font = face->font;
26948
26949 it->nglyphs = 1;
26950
26951 /* If we have not yet calculated pixel size data of glyphs of
26952 the composition for the current face font, calculate them
26953 now. Theoretically, we have to check all fonts for the
26954 glyphs, but that requires much time and memory space. So,
26955 here we check only the font of the first glyph. This may
26956 lead to incorrect display, but it's very rare, and C-l
26957 (recenter-top-bottom) can correct the display anyway. */
26958 if (! cmp->font || cmp->font != font)
26959 {
26960 /* Ascent and descent of the font of the first character
26961 of this composition (adjusted by baseline offset).
26962 Ascent and descent of overall glyphs should not be less
26963 than these, respectively. */
26964 int font_ascent, font_descent, font_height;
26965 /* Bounding box of the overall glyphs. */
26966 int leftmost, rightmost, lowest, highest;
26967 int lbearing, rbearing;
26968 int i, width, ascent, descent;
26969 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26970 XChar2b char2b;
26971 struct font_metrics *pcm;
26972 ptrdiff_t pos;
26973
26974 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26975 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26976 break;
26977 bool right_padded = glyph_len < cmp->glyph_len;
26978 for (i = 0; i < glyph_len; i++)
26979 {
26980 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26981 break;
26982 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26983 }
26984 bool left_padded = i > 0;
26985
26986 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26987 : IT_CHARPOS (*it));
26988 /* If no suitable font is found, use the default font. */
26989 bool font_not_found_p = font == NULL;
26990 if (font_not_found_p)
26991 {
26992 face = face->ascii_face;
26993 font = face->font;
26994 }
26995 boff = font->baseline_offset;
26996 if (font->vertical_centering)
26997 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26998 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
26999 font_ascent += boff;
27000 font_descent -= boff;
27001 font_height = font_ascent + font_descent;
27002
27003 cmp->font = font;
27004
27005 pcm = NULL;
27006 if (! font_not_found_p)
27007 {
27008 get_char_face_and_encoding (it->f, c, it->face_id,
27009 &char2b, false);
27010 pcm = get_per_char_metric (font, &char2b);
27011 }
27012
27013 /* Initialize the bounding box. */
27014 if (pcm)
27015 {
27016 width = cmp->glyph_len > 0 ? pcm->width : 0;
27017 ascent = pcm->ascent;
27018 descent = pcm->descent;
27019 lbearing = pcm->lbearing;
27020 rbearing = pcm->rbearing;
27021 }
27022 else
27023 {
27024 width = cmp->glyph_len > 0 ? font->space_width : 0;
27025 ascent = FONT_BASE (font);
27026 descent = FONT_DESCENT (font);
27027 lbearing = 0;
27028 rbearing = width;
27029 }
27030
27031 rightmost = width;
27032 leftmost = 0;
27033 lowest = - descent + boff;
27034 highest = ascent + boff;
27035
27036 if (! font_not_found_p
27037 && font->default_ascent
27038 && CHAR_TABLE_P (Vuse_default_ascent)
27039 && !NILP (Faref (Vuse_default_ascent,
27040 make_number (it->char_to_display))))
27041 highest = font->default_ascent + boff;
27042
27043 /* Draw the first glyph at the normal position. It may be
27044 shifted to right later if some other glyphs are drawn
27045 at the left. */
27046 cmp->offsets[i * 2] = 0;
27047 cmp->offsets[i * 2 + 1] = boff;
27048 cmp->lbearing = lbearing;
27049 cmp->rbearing = rbearing;
27050
27051 /* Set cmp->offsets for the remaining glyphs. */
27052 for (i++; i < glyph_len; i++)
27053 {
27054 int left, right, btm, top;
27055 int ch = COMPOSITION_GLYPH (cmp, i);
27056 int face_id;
27057 struct face *this_face;
27058
27059 if (ch == '\t')
27060 ch = ' ';
27061 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27062 this_face = FACE_FROM_ID (it->f, face_id);
27063 font = this_face->font;
27064
27065 if (font == NULL)
27066 pcm = NULL;
27067 else
27068 {
27069 get_char_face_and_encoding (it->f, ch, face_id,
27070 &char2b, false);
27071 pcm = get_per_char_metric (font, &char2b);
27072 }
27073 if (! pcm)
27074 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27075 else
27076 {
27077 width = pcm->width;
27078 ascent = pcm->ascent;
27079 descent = pcm->descent;
27080 lbearing = pcm->lbearing;
27081 rbearing = pcm->rbearing;
27082 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27083 {
27084 /* Relative composition with or without
27085 alternate chars. */
27086 left = (leftmost + rightmost - width) / 2;
27087 btm = - descent + boff;
27088 if (font->relative_compose
27089 && (! CHAR_TABLE_P (Vignore_relative_composition)
27090 || NILP (Faref (Vignore_relative_composition,
27091 make_number (ch)))))
27092 {
27093
27094 if (- descent >= font->relative_compose)
27095 /* One extra pixel between two glyphs. */
27096 btm = highest + 1;
27097 else if (ascent <= 0)
27098 /* One extra pixel between two glyphs. */
27099 btm = lowest - 1 - ascent - descent;
27100 }
27101 }
27102 else
27103 {
27104 /* A composition rule is specified by an integer
27105 value that encodes global and new reference
27106 points (GREF and NREF). GREF and NREF are
27107 specified by numbers as below:
27108
27109 0---1---2 -- ascent
27110 | |
27111 | |
27112 | |
27113 9--10--11 -- center
27114 | |
27115 ---3---4---5--- baseline
27116 | |
27117 6---7---8 -- descent
27118 */
27119 int rule = COMPOSITION_RULE (cmp, i);
27120 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27121
27122 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27123 grefx = gref % 3, nrefx = nref % 3;
27124 grefy = gref / 3, nrefy = nref / 3;
27125 if (xoff)
27126 xoff = font_height * (xoff - 128) / 256;
27127 if (yoff)
27128 yoff = font_height * (yoff - 128) / 256;
27129
27130 left = (leftmost
27131 + grefx * (rightmost - leftmost) / 2
27132 - nrefx * width / 2
27133 + xoff);
27134
27135 btm = ((grefy == 0 ? highest
27136 : grefy == 1 ? 0
27137 : grefy == 2 ? lowest
27138 : (highest + lowest) / 2)
27139 - (nrefy == 0 ? ascent + descent
27140 : nrefy == 1 ? descent - boff
27141 : nrefy == 2 ? 0
27142 : (ascent + descent) / 2)
27143 + yoff);
27144 }
27145
27146 cmp->offsets[i * 2] = left;
27147 cmp->offsets[i * 2 + 1] = btm + descent;
27148
27149 /* Update the bounding box of the overall glyphs. */
27150 if (width > 0)
27151 {
27152 right = left + width;
27153 if (left < leftmost)
27154 leftmost = left;
27155 if (right > rightmost)
27156 rightmost = right;
27157 }
27158 top = btm + descent + ascent;
27159 if (top > highest)
27160 highest = top;
27161 if (btm < lowest)
27162 lowest = btm;
27163
27164 if (cmp->lbearing > left + lbearing)
27165 cmp->lbearing = left + lbearing;
27166 if (cmp->rbearing < left + rbearing)
27167 cmp->rbearing = left + rbearing;
27168 }
27169 }
27170
27171 /* If there are glyphs whose x-offsets are negative,
27172 shift all glyphs to the right and make all x-offsets
27173 non-negative. */
27174 if (leftmost < 0)
27175 {
27176 for (i = 0; i < cmp->glyph_len; i++)
27177 cmp->offsets[i * 2] -= leftmost;
27178 rightmost -= leftmost;
27179 cmp->lbearing -= leftmost;
27180 cmp->rbearing -= leftmost;
27181 }
27182
27183 if (left_padded && cmp->lbearing < 0)
27184 {
27185 for (i = 0; i < cmp->glyph_len; i++)
27186 cmp->offsets[i * 2] -= cmp->lbearing;
27187 rightmost -= cmp->lbearing;
27188 cmp->rbearing -= cmp->lbearing;
27189 cmp->lbearing = 0;
27190 }
27191 if (right_padded && rightmost < cmp->rbearing)
27192 {
27193 rightmost = cmp->rbearing;
27194 }
27195
27196 cmp->pixel_width = rightmost;
27197 cmp->ascent = highest;
27198 cmp->descent = - lowest;
27199 if (cmp->ascent < font_ascent)
27200 cmp->ascent = font_ascent;
27201 if (cmp->descent < font_descent)
27202 cmp->descent = font_descent;
27203 }
27204
27205 if (it->glyph_row
27206 && (cmp->lbearing < 0
27207 || cmp->rbearing > cmp->pixel_width))
27208 it->glyph_row->contains_overlapping_glyphs_p = true;
27209
27210 it->pixel_width = cmp->pixel_width;
27211 it->ascent = it->phys_ascent = cmp->ascent;
27212 it->descent = it->phys_descent = cmp->descent;
27213 if (face->box != FACE_NO_BOX)
27214 {
27215 int thick = face->box_line_width;
27216
27217 if (thick > 0)
27218 {
27219 it->ascent += thick;
27220 it->descent += thick;
27221 }
27222 else
27223 thick = - thick;
27224
27225 if (it->start_of_box_run_p)
27226 it->pixel_width += thick;
27227 if (it->end_of_box_run_p)
27228 it->pixel_width += thick;
27229 }
27230
27231 /* If face has an overline, add the height of the overline
27232 (1 pixel) and a 1 pixel margin to the character height. */
27233 if (face->overline_p)
27234 it->ascent += overline_margin;
27235
27236 take_vertical_position_into_account (it);
27237 if (it->ascent < 0)
27238 it->ascent = 0;
27239 if (it->descent < 0)
27240 it->descent = 0;
27241
27242 if (it->glyph_row && cmp->glyph_len > 0)
27243 append_composite_glyph (it);
27244 }
27245 else if (it->what == IT_COMPOSITION)
27246 {
27247 /* A dynamic (automatic) composition. */
27248 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27249 Lisp_Object gstring;
27250 struct font_metrics metrics;
27251
27252 it->nglyphs = 1;
27253
27254 gstring = composition_gstring_from_id (it->cmp_it.id);
27255 it->pixel_width
27256 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27257 &metrics);
27258 if (it->glyph_row
27259 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27260 it->glyph_row->contains_overlapping_glyphs_p = true;
27261 it->ascent = it->phys_ascent = metrics.ascent;
27262 it->descent = it->phys_descent = metrics.descent;
27263 if (face->box != FACE_NO_BOX)
27264 {
27265 int thick = face->box_line_width;
27266
27267 if (thick > 0)
27268 {
27269 it->ascent += thick;
27270 it->descent += thick;
27271 }
27272 else
27273 thick = - thick;
27274
27275 if (it->start_of_box_run_p)
27276 it->pixel_width += thick;
27277 if (it->end_of_box_run_p)
27278 it->pixel_width += thick;
27279 }
27280 /* If face has an overline, add the height of the overline
27281 (1 pixel) and a 1 pixel margin to the character height. */
27282 if (face->overline_p)
27283 it->ascent += overline_margin;
27284 take_vertical_position_into_account (it);
27285 if (it->ascent < 0)
27286 it->ascent = 0;
27287 if (it->descent < 0)
27288 it->descent = 0;
27289
27290 if (it->glyph_row)
27291 append_composite_glyph (it);
27292 }
27293 else if (it->what == IT_GLYPHLESS)
27294 produce_glyphless_glyph (it, false, Qnil);
27295 else if (it->what == IT_IMAGE)
27296 produce_image_glyph (it);
27297 else if (it->what == IT_STRETCH)
27298 produce_stretch_glyph (it);
27299
27300 done:
27301 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27302 because this isn't true for images with `:ascent 100'. */
27303 eassert (it->ascent >= 0 && it->descent >= 0);
27304 if (it->area == TEXT_AREA)
27305 it->current_x += it->pixel_width;
27306
27307 if (extra_line_spacing > 0)
27308 {
27309 it->descent += extra_line_spacing;
27310 if (extra_line_spacing > it->max_extra_line_spacing)
27311 it->max_extra_line_spacing = extra_line_spacing;
27312 }
27313
27314 it->max_ascent = max (it->max_ascent, it->ascent);
27315 it->max_descent = max (it->max_descent, it->descent);
27316 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27317 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27318 }
27319
27320 /* EXPORT for RIF:
27321 Output LEN glyphs starting at START at the nominal cursor position.
27322 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27323 being updated, and UPDATED_AREA is the area of that row being updated. */
27324
27325 void
27326 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27327 struct glyph *start, enum glyph_row_area updated_area, int len)
27328 {
27329 int x, hpos, chpos = w->phys_cursor.hpos;
27330
27331 eassert (updated_row);
27332 /* When the window is hscrolled, cursor hpos can legitimately be out
27333 of bounds, but we draw the cursor at the corresponding window
27334 margin in that case. */
27335 if (!updated_row->reversed_p && chpos < 0)
27336 chpos = 0;
27337 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27338 chpos = updated_row->used[TEXT_AREA] - 1;
27339
27340 block_input ();
27341
27342 /* Write glyphs. */
27343
27344 hpos = start - updated_row->glyphs[updated_area];
27345 x = draw_glyphs (w, w->output_cursor.x,
27346 updated_row, updated_area,
27347 hpos, hpos + len,
27348 DRAW_NORMAL_TEXT, 0);
27349
27350 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27351 if (updated_area == TEXT_AREA
27352 && w->phys_cursor_on_p
27353 && w->phys_cursor.vpos == w->output_cursor.vpos
27354 && chpos >= hpos
27355 && chpos < hpos + len)
27356 w->phys_cursor_on_p = false;
27357
27358 unblock_input ();
27359
27360 /* Advance the output cursor. */
27361 w->output_cursor.hpos += len;
27362 w->output_cursor.x = x;
27363 }
27364
27365
27366 /* EXPORT for RIF:
27367 Insert LEN glyphs from START at the nominal cursor position. */
27368
27369 void
27370 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27371 struct glyph *start, enum glyph_row_area updated_area, int len)
27372 {
27373 struct frame *f;
27374 int line_height, shift_by_width, shifted_region_width;
27375 struct glyph_row *row;
27376 struct glyph *glyph;
27377 int frame_x, frame_y;
27378 ptrdiff_t hpos;
27379
27380 eassert (updated_row);
27381 block_input ();
27382 f = XFRAME (WINDOW_FRAME (w));
27383
27384 /* Get the height of the line we are in. */
27385 row = updated_row;
27386 line_height = row->height;
27387
27388 /* Get the width of the glyphs to insert. */
27389 shift_by_width = 0;
27390 for (glyph = start; glyph < start + len; ++glyph)
27391 shift_by_width += glyph->pixel_width;
27392
27393 /* Get the width of the region to shift right. */
27394 shifted_region_width = (window_box_width (w, updated_area)
27395 - w->output_cursor.x
27396 - shift_by_width);
27397
27398 /* Shift right. */
27399 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27400 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27401
27402 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27403 line_height, shift_by_width);
27404
27405 /* Write the glyphs. */
27406 hpos = start - row->glyphs[updated_area];
27407 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27408 hpos, hpos + len,
27409 DRAW_NORMAL_TEXT, 0);
27410
27411 /* Advance the output cursor. */
27412 w->output_cursor.hpos += len;
27413 w->output_cursor.x += shift_by_width;
27414 unblock_input ();
27415 }
27416
27417
27418 /* EXPORT for RIF:
27419 Erase the current text line from the nominal cursor position
27420 (inclusive) to pixel column TO_X (exclusive). The idea is that
27421 everything from TO_X onward is already erased.
27422
27423 TO_X is a pixel position relative to UPDATED_AREA of currently
27424 updated window W. TO_X == -1 means clear to the end of this area. */
27425
27426 void
27427 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27428 enum glyph_row_area updated_area, int to_x)
27429 {
27430 struct frame *f;
27431 int max_x, min_y, max_y;
27432 int from_x, from_y, to_y;
27433
27434 eassert (updated_row);
27435 f = XFRAME (w->frame);
27436
27437 if (updated_row->full_width_p)
27438 max_x = (WINDOW_PIXEL_WIDTH (w)
27439 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27440 else
27441 max_x = window_box_width (w, updated_area);
27442 max_y = window_text_bottom_y (w);
27443
27444 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27445 of window. For TO_X > 0, truncate to end of drawing area. */
27446 if (to_x == 0)
27447 return;
27448 else if (to_x < 0)
27449 to_x = max_x;
27450 else
27451 to_x = min (to_x, max_x);
27452
27453 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27454
27455 /* Notice if the cursor will be cleared by this operation. */
27456 if (!updated_row->full_width_p)
27457 notice_overwritten_cursor (w, updated_area,
27458 w->output_cursor.x, -1,
27459 updated_row->y,
27460 MATRIX_ROW_BOTTOM_Y (updated_row));
27461
27462 from_x = w->output_cursor.x;
27463
27464 /* Translate to frame coordinates. */
27465 if (updated_row->full_width_p)
27466 {
27467 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27468 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27469 }
27470 else
27471 {
27472 int area_left = window_box_left (w, updated_area);
27473 from_x += area_left;
27474 to_x += area_left;
27475 }
27476
27477 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27478 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27479 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27480
27481 /* Prevent inadvertently clearing to end of the X window. */
27482 if (to_x > from_x && to_y > from_y)
27483 {
27484 block_input ();
27485 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27486 to_x - from_x, to_y - from_y);
27487 unblock_input ();
27488 }
27489 }
27490
27491 #endif /* HAVE_WINDOW_SYSTEM */
27492
27493
27494 \f
27495 /***********************************************************************
27496 Cursor types
27497 ***********************************************************************/
27498
27499 /* Value is the internal representation of the specified cursor type
27500 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27501 of the bar cursor. */
27502
27503 static enum text_cursor_kinds
27504 get_specified_cursor_type (Lisp_Object arg, int *width)
27505 {
27506 enum text_cursor_kinds type;
27507
27508 if (NILP (arg))
27509 return NO_CURSOR;
27510
27511 if (EQ (arg, Qbox))
27512 return FILLED_BOX_CURSOR;
27513
27514 if (EQ (arg, Qhollow))
27515 return HOLLOW_BOX_CURSOR;
27516
27517 if (EQ (arg, Qbar))
27518 {
27519 *width = 2;
27520 return BAR_CURSOR;
27521 }
27522
27523 if (CONSP (arg)
27524 && EQ (XCAR (arg), Qbar)
27525 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27526 {
27527 *width = XINT (XCDR (arg));
27528 return BAR_CURSOR;
27529 }
27530
27531 if (EQ (arg, Qhbar))
27532 {
27533 *width = 2;
27534 return HBAR_CURSOR;
27535 }
27536
27537 if (CONSP (arg)
27538 && EQ (XCAR (arg), Qhbar)
27539 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27540 {
27541 *width = XINT (XCDR (arg));
27542 return HBAR_CURSOR;
27543 }
27544
27545 /* Treat anything unknown as "hollow box cursor".
27546 It was bad to signal an error; people have trouble fixing
27547 .Xdefaults with Emacs, when it has something bad in it. */
27548 type = HOLLOW_BOX_CURSOR;
27549
27550 return type;
27551 }
27552
27553 /* Set the default cursor types for specified frame. */
27554 void
27555 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27556 {
27557 int width = 1;
27558 Lisp_Object tem;
27559
27560 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27561 FRAME_CURSOR_WIDTH (f) = width;
27562
27563 /* By default, set up the blink-off state depending on the on-state. */
27564
27565 tem = Fassoc (arg, Vblink_cursor_alist);
27566 if (!NILP (tem))
27567 {
27568 FRAME_BLINK_OFF_CURSOR (f)
27569 = get_specified_cursor_type (XCDR (tem), &width);
27570 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27571 }
27572 else
27573 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27574
27575 /* Make sure the cursor gets redrawn. */
27576 f->cursor_type_changed = true;
27577 }
27578
27579
27580 #ifdef HAVE_WINDOW_SYSTEM
27581
27582 /* Return the cursor we want to be displayed in window W. Return
27583 width of bar/hbar cursor through WIDTH arg. Return with
27584 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
27585 (i.e. if the `system caret' should track this cursor).
27586
27587 In a mini-buffer window, we want the cursor only to appear if we
27588 are reading input from this window. For the selected window, we
27589 want the cursor type given by the frame parameter or buffer local
27590 setting of cursor-type. If explicitly marked off, draw no cursor.
27591 In all other cases, we want a hollow box cursor. */
27592
27593 static enum text_cursor_kinds
27594 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27595 bool *active_cursor)
27596 {
27597 struct frame *f = XFRAME (w->frame);
27598 struct buffer *b = XBUFFER (w->contents);
27599 int cursor_type = DEFAULT_CURSOR;
27600 Lisp_Object alt_cursor;
27601 bool non_selected = false;
27602
27603 *active_cursor = true;
27604
27605 /* Echo area */
27606 if (cursor_in_echo_area
27607 && FRAME_HAS_MINIBUF_P (f)
27608 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27609 {
27610 if (w == XWINDOW (echo_area_window))
27611 {
27612 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27613 {
27614 *width = FRAME_CURSOR_WIDTH (f);
27615 return FRAME_DESIRED_CURSOR (f);
27616 }
27617 else
27618 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27619 }
27620
27621 *active_cursor = false;
27622 non_selected = true;
27623 }
27624
27625 /* Detect a nonselected window or nonselected frame. */
27626 else if (w != XWINDOW (f->selected_window)
27627 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27628 {
27629 *active_cursor = false;
27630
27631 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27632 return NO_CURSOR;
27633
27634 non_selected = true;
27635 }
27636
27637 /* Never display a cursor in a window in which cursor-type is nil. */
27638 if (NILP (BVAR (b, cursor_type)))
27639 return NO_CURSOR;
27640
27641 /* Get the normal cursor type for this window. */
27642 if (EQ (BVAR (b, cursor_type), Qt))
27643 {
27644 cursor_type = FRAME_DESIRED_CURSOR (f);
27645 *width = FRAME_CURSOR_WIDTH (f);
27646 }
27647 else
27648 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27649
27650 /* Use cursor-in-non-selected-windows instead
27651 for non-selected window or frame. */
27652 if (non_selected)
27653 {
27654 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27655 if (!EQ (Qt, alt_cursor))
27656 return get_specified_cursor_type (alt_cursor, width);
27657 /* t means modify the normal cursor type. */
27658 if (cursor_type == FILLED_BOX_CURSOR)
27659 cursor_type = HOLLOW_BOX_CURSOR;
27660 else if (cursor_type == BAR_CURSOR && *width > 1)
27661 --*width;
27662 return cursor_type;
27663 }
27664
27665 /* Use normal cursor if not blinked off. */
27666 if (!w->cursor_off_p)
27667 {
27668 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27669 {
27670 if (cursor_type == FILLED_BOX_CURSOR)
27671 {
27672 /* Using a block cursor on large images can be very annoying.
27673 So use a hollow cursor for "large" images.
27674 If image is not transparent (no mask), also use hollow cursor. */
27675 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27676 if (img != NULL && IMAGEP (img->spec))
27677 {
27678 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27679 where N = size of default frame font size.
27680 This should cover most of the "tiny" icons people may use. */
27681 if (!img->mask
27682 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27683 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27684 cursor_type = HOLLOW_BOX_CURSOR;
27685 }
27686 }
27687 else if (cursor_type != NO_CURSOR)
27688 {
27689 /* Display current only supports BOX and HOLLOW cursors for images.
27690 So for now, unconditionally use a HOLLOW cursor when cursor is
27691 not a solid box cursor. */
27692 cursor_type = HOLLOW_BOX_CURSOR;
27693 }
27694 }
27695 return cursor_type;
27696 }
27697
27698 /* Cursor is blinked off, so determine how to "toggle" it. */
27699
27700 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27701 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27702 return get_specified_cursor_type (XCDR (alt_cursor), width);
27703
27704 /* Then see if frame has specified a specific blink off cursor type. */
27705 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27706 {
27707 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27708 return FRAME_BLINK_OFF_CURSOR (f);
27709 }
27710
27711 #if false
27712 /* Some people liked having a permanently visible blinking cursor,
27713 while others had very strong opinions against it. So it was
27714 decided to remove it. KFS 2003-09-03 */
27715
27716 /* Finally perform built-in cursor blinking:
27717 filled box <-> hollow box
27718 wide [h]bar <-> narrow [h]bar
27719 narrow [h]bar <-> no cursor
27720 other type <-> no cursor */
27721
27722 if (cursor_type == FILLED_BOX_CURSOR)
27723 return HOLLOW_BOX_CURSOR;
27724
27725 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27726 {
27727 *width = 1;
27728 return cursor_type;
27729 }
27730 #endif
27731
27732 return NO_CURSOR;
27733 }
27734
27735
27736 /* Notice when the text cursor of window W has been completely
27737 overwritten by a drawing operation that outputs glyphs in AREA
27738 starting at X0 and ending at X1 in the line starting at Y0 and
27739 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27740 the rest of the line after X0 has been written. Y coordinates
27741 are window-relative. */
27742
27743 static void
27744 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27745 int x0, int x1, int y0, int y1)
27746 {
27747 int cx0, cx1, cy0, cy1;
27748 struct glyph_row *row;
27749
27750 if (!w->phys_cursor_on_p)
27751 return;
27752 if (area != TEXT_AREA)
27753 return;
27754
27755 if (w->phys_cursor.vpos < 0
27756 || w->phys_cursor.vpos >= w->current_matrix->nrows
27757 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27758 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27759 return;
27760
27761 if (row->cursor_in_fringe_p)
27762 {
27763 row->cursor_in_fringe_p = false;
27764 draw_fringe_bitmap (w, row, row->reversed_p);
27765 w->phys_cursor_on_p = false;
27766 return;
27767 }
27768
27769 cx0 = w->phys_cursor.x;
27770 cx1 = cx0 + w->phys_cursor_width;
27771 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27772 return;
27773
27774 /* The cursor image will be completely removed from the
27775 screen if the output area intersects the cursor area in
27776 y-direction. When we draw in [y0 y1[, and some part of
27777 the cursor is at y < y0, that part must have been drawn
27778 before. When scrolling, the cursor is erased before
27779 actually scrolling, so we don't come here. When not
27780 scrolling, the rows above the old cursor row must have
27781 changed, and in this case these rows must have written
27782 over the cursor image.
27783
27784 Likewise if part of the cursor is below y1, with the
27785 exception of the cursor being in the first blank row at
27786 the buffer and window end because update_text_area
27787 doesn't draw that row. (Except when it does, but
27788 that's handled in update_text_area.) */
27789
27790 cy0 = w->phys_cursor.y;
27791 cy1 = cy0 + w->phys_cursor_height;
27792 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27793 return;
27794
27795 w->phys_cursor_on_p = false;
27796 }
27797
27798 #endif /* HAVE_WINDOW_SYSTEM */
27799
27800 \f
27801 /************************************************************************
27802 Mouse Face
27803 ************************************************************************/
27804
27805 #ifdef HAVE_WINDOW_SYSTEM
27806
27807 /* EXPORT for RIF:
27808 Fix the display of area AREA of overlapping row ROW in window W
27809 with respect to the overlapping part OVERLAPS. */
27810
27811 void
27812 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27813 enum glyph_row_area area, int overlaps)
27814 {
27815 int i, x;
27816
27817 block_input ();
27818
27819 x = 0;
27820 for (i = 0; i < row->used[area];)
27821 {
27822 if (row->glyphs[area][i].overlaps_vertically_p)
27823 {
27824 int start = i, start_x = x;
27825
27826 do
27827 {
27828 x += row->glyphs[area][i].pixel_width;
27829 ++i;
27830 }
27831 while (i < row->used[area]
27832 && row->glyphs[area][i].overlaps_vertically_p);
27833
27834 draw_glyphs (w, start_x, row, area,
27835 start, i,
27836 DRAW_NORMAL_TEXT, overlaps);
27837 }
27838 else
27839 {
27840 x += row->glyphs[area][i].pixel_width;
27841 ++i;
27842 }
27843 }
27844
27845 unblock_input ();
27846 }
27847
27848
27849 /* EXPORT:
27850 Draw the cursor glyph of window W in glyph row ROW. See the
27851 comment of draw_glyphs for the meaning of HL. */
27852
27853 void
27854 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27855 enum draw_glyphs_face hl)
27856 {
27857 /* If cursor hpos is out of bounds, don't draw garbage. This can
27858 happen in mini-buffer windows when switching between echo area
27859 glyphs and mini-buffer. */
27860 if ((row->reversed_p
27861 ? (w->phys_cursor.hpos >= 0)
27862 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27863 {
27864 bool on_p = w->phys_cursor_on_p;
27865 int x1;
27866 int hpos = w->phys_cursor.hpos;
27867
27868 /* When the window is hscrolled, cursor hpos can legitimately be
27869 out of bounds, but we draw the cursor at the corresponding
27870 window margin in that case. */
27871 if (!row->reversed_p && hpos < 0)
27872 hpos = 0;
27873 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27874 hpos = row->used[TEXT_AREA] - 1;
27875
27876 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27877 hl, 0);
27878 w->phys_cursor_on_p = on_p;
27879
27880 if (hl == DRAW_CURSOR)
27881 w->phys_cursor_width = x1 - w->phys_cursor.x;
27882 /* When we erase the cursor, and ROW is overlapped by other
27883 rows, make sure that these overlapping parts of other rows
27884 are redrawn. */
27885 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27886 {
27887 w->phys_cursor_width = x1 - w->phys_cursor.x;
27888
27889 if (row > w->current_matrix->rows
27890 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27891 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27892 OVERLAPS_ERASED_CURSOR);
27893
27894 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27895 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27896 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27897 OVERLAPS_ERASED_CURSOR);
27898 }
27899 }
27900 }
27901
27902
27903 /* Erase the image of a cursor of window W from the screen. */
27904
27905 void
27906 erase_phys_cursor (struct window *w)
27907 {
27908 struct frame *f = XFRAME (w->frame);
27909 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27910 int hpos = w->phys_cursor.hpos;
27911 int vpos = w->phys_cursor.vpos;
27912 bool mouse_face_here_p = false;
27913 struct glyph_matrix *active_glyphs = w->current_matrix;
27914 struct glyph_row *cursor_row;
27915 struct glyph *cursor_glyph;
27916 enum draw_glyphs_face hl;
27917
27918 /* No cursor displayed or row invalidated => nothing to do on the
27919 screen. */
27920 if (w->phys_cursor_type == NO_CURSOR)
27921 goto mark_cursor_off;
27922
27923 /* VPOS >= active_glyphs->nrows means that window has been resized.
27924 Don't bother to erase the cursor. */
27925 if (vpos >= active_glyphs->nrows)
27926 goto mark_cursor_off;
27927
27928 /* If row containing cursor is marked invalid, there is nothing we
27929 can do. */
27930 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27931 if (!cursor_row->enabled_p)
27932 goto mark_cursor_off;
27933
27934 /* If line spacing is > 0, old cursor may only be partially visible in
27935 window after split-window. So adjust visible height. */
27936 cursor_row->visible_height = min (cursor_row->visible_height,
27937 window_text_bottom_y (w) - cursor_row->y);
27938
27939 /* If row is completely invisible, don't attempt to delete a cursor which
27940 isn't there. This can happen if cursor is at top of a window, and
27941 we switch to a buffer with a header line in that window. */
27942 if (cursor_row->visible_height <= 0)
27943 goto mark_cursor_off;
27944
27945 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27946 if (cursor_row->cursor_in_fringe_p)
27947 {
27948 cursor_row->cursor_in_fringe_p = false;
27949 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27950 goto mark_cursor_off;
27951 }
27952
27953 /* This can happen when the new row is shorter than the old one.
27954 In this case, either draw_glyphs or clear_end_of_line
27955 should have cleared the cursor. Note that we wouldn't be
27956 able to erase the cursor in this case because we don't have a
27957 cursor glyph at hand. */
27958 if ((cursor_row->reversed_p
27959 ? (w->phys_cursor.hpos < 0)
27960 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27961 goto mark_cursor_off;
27962
27963 /* When the window is hscrolled, cursor hpos can legitimately be out
27964 of bounds, but we draw the cursor at the corresponding window
27965 margin in that case. */
27966 if (!cursor_row->reversed_p && hpos < 0)
27967 hpos = 0;
27968 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27969 hpos = cursor_row->used[TEXT_AREA] - 1;
27970
27971 /* If the cursor is in the mouse face area, redisplay that when
27972 we clear the cursor. */
27973 if (! NILP (hlinfo->mouse_face_window)
27974 && coords_in_mouse_face_p (w, hpos, vpos)
27975 /* Don't redraw the cursor's spot in mouse face if it is at the
27976 end of a line (on a newline). The cursor appears there, but
27977 mouse highlighting does not. */
27978 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27979 mouse_face_here_p = true;
27980
27981 /* Maybe clear the display under the cursor. */
27982 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27983 {
27984 int x, y;
27985 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27986 int width;
27987
27988 cursor_glyph = get_phys_cursor_glyph (w);
27989 if (cursor_glyph == NULL)
27990 goto mark_cursor_off;
27991
27992 width = cursor_glyph->pixel_width;
27993 x = w->phys_cursor.x;
27994 if (x < 0)
27995 {
27996 width += x;
27997 x = 0;
27998 }
27999 width = min (width, window_box_width (w, TEXT_AREA) - x);
28000 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
28001 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28002
28003 if (width > 0)
28004 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28005 }
28006
28007 /* Erase the cursor by redrawing the character underneath it. */
28008 if (mouse_face_here_p)
28009 hl = DRAW_MOUSE_FACE;
28010 else
28011 hl = DRAW_NORMAL_TEXT;
28012 draw_phys_cursor_glyph (w, cursor_row, hl);
28013
28014 mark_cursor_off:
28015 w->phys_cursor_on_p = false;
28016 w->phys_cursor_type = NO_CURSOR;
28017 }
28018
28019
28020 /* Display or clear cursor of window W. If !ON, clear the cursor.
28021 If ON, display the cursor; where to put the cursor is specified by
28022 HPOS, VPOS, X and Y. */
28023
28024 void
28025 display_and_set_cursor (struct window *w, bool on,
28026 int hpos, int vpos, int x, int y)
28027 {
28028 struct frame *f = XFRAME (w->frame);
28029 int new_cursor_type;
28030 int new_cursor_width;
28031 bool active_cursor;
28032 struct glyph_row *glyph_row;
28033 struct glyph *glyph;
28034
28035 /* This is pointless on invisible frames, and dangerous on garbaged
28036 windows and frames; in the latter case, the frame or window may
28037 be in the midst of changing its size, and x and y may be off the
28038 window. */
28039 if (! FRAME_VISIBLE_P (f)
28040 || FRAME_GARBAGED_P (f)
28041 || vpos >= w->current_matrix->nrows
28042 || hpos >= w->current_matrix->matrix_w)
28043 return;
28044
28045 /* If cursor is off and we want it off, return quickly. */
28046 if (!on && !w->phys_cursor_on_p)
28047 return;
28048
28049 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28050 /* If cursor row is not enabled, we don't really know where to
28051 display the cursor. */
28052 if (!glyph_row->enabled_p)
28053 {
28054 w->phys_cursor_on_p = false;
28055 return;
28056 }
28057
28058 glyph = NULL;
28059 if (!glyph_row->exact_window_width_line_p
28060 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
28061 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28062
28063 eassert (input_blocked_p ());
28064
28065 /* Set new_cursor_type to the cursor we want to be displayed. */
28066 new_cursor_type = get_window_cursor_type (w, glyph,
28067 &new_cursor_width, &active_cursor);
28068
28069 /* If cursor is currently being shown and we don't want it to be or
28070 it is in the wrong place, or the cursor type is not what we want,
28071 erase it. */
28072 if (w->phys_cursor_on_p
28073 && (!on
28074 || w->phys_cursor.x != x
28075 || w->phys_cursor.y != y
28076 /* HPOS can be negative in R2L rows whose
28077 exact_window_width_line_p flag is set (i.e. their newline
28078 would "overflow into the fringe"). */
28079 || hpos < 0
28080 || new_cursor_type != w->phys_cursor_type
28081 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28082 && new_cursor_width != w->phys_cursor_width)))
28083 erase_phys_cursor (w);
28084
28085 /* Don't check phys_cursor_on_p here because that flag is only set
28086 to false in some cases where we know that the cursor has been
28087 completely erased, to avoid the extra work of erasing the cursor
28088 twice. In other words, phys_cursor_on_p can be true and the cursor
28089 still not be visible, or it has only been partly erased. */
28090 if (on)
28091 {
28092 w->phys_cursor_ascent = glyph_row->ascent;
28093 w->phys_cursor_height = glyph_row->height;
28094
28095 /* Set phys_cursor_.* before x_draw_.* is called because some
28096 of them may need the information. */
28097 w->phys_cursor.x = x;
28098 w->phys_cursor.y = glyph_row->y;
28099 w->phys_cursor.hpos = hpos;
28100 w->phys_cursor.vpos = vpos;
28101 }
28102
28103 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28104 new_cursor_type, new_cursor_width,
28105 on, active_cursor);
28106 }
28107
28108
28109 /* Switch the display of W's cursor on or off, according to the value
28110 of ON. */
28111
28112 static void
28113 update_window_cursor (struct window *w, bool on)
28114 {
28115 /* Don't update cursor in windows whose frame is in the process
28116 of being deleted. */
28117 if (w->current_matrix)
28118 {
28119 int hpos = w->phys_cursor.hpos;
28120 int vpos = w->phys_cursor.vpos;
28121 struct glyph_row *row;
28122
28123 if (vpos >= w->current_matrix->nrows
28124 || hpos >= w->current_matrix->matrix_w)
28125 return;
28126
28127 row = MATRIX_ROW (w->current_matrix, vpos);
28128
28129 /* When the window is hscrolled, cursor hpos can legitimately be
28130 out of bounds, but we draw the cursor at the corresponding
28131 window margin in that case. */
28132 if (!row->reversed_p && hpos < 0)
28133 hpos = 0;
28134 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28135 hpos = row->used[TEXT_AREA] - 1;
28136
28137 block_input ();
28138 display_and_set_cursor (w, on, hpos, vpos,
28139 w->phys_cursor.x, w->phys_cursor.y);
28140 unblock_input ();
28141 }
28142 }
28143
28144
28145 /* Call update_window_cursor with parameter ON_P on all leaf windows
28146 in the window tree rooted at W. */
28147
28148 static void
28149 update_cursor_in_window_tree (struct window *w, bool on_p)
28150 {
28151 while (w)
28152 {
28153 if (WINDOWP (w->contents))
28154 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28155 else
28156 update_window_cursor (w, on_p);
28157
28158 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28159 }
28160 }
28161
28162
28163 /* EXPORT:
28164 Display the cursor on window W, or clear it, according to ON_P.
28165 Don't change the cursor's position. */
28166
28167 void
28168 x_update_cursor (struct frame *f, bool on_p)
28169 {
28170 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28171 }
28172
28173
28174 /* EXPORT:
28175 Clear the cursor of window W to background color, and mark the
28176 cursor as not shown. This is used when the text where the cursor
28177 is about to be rewritten. */
28178
28179 void
28180 x_clear_cursor (struct window *w)
28181 {
28182 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28183 update_window_cursor (w, false);
28184 }
28185
28186 #endif /* HAVE_WINDOW_SYSTEM */
28187
28188 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28189 and MSDOS. */
28190 static void
28191 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28192 int start_hpos, int end_hpos,
28193 enum draw_glyphs_face draw)
28194 {
28195 #ifdef HAVE_WINDOW_SYSTEM
28196 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28197 {
28198 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28199 return;
28200 }
28201 #endif
28202 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28203 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28204 #endif
28205 }
28206
28207 /* Display the active region described by mouse_face_* according to DRAW. */
28208
28209 static void
28210 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28211 {
28212 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28213 struct frame *f = XFRAME (WINDOW_FRAME (w));
28214
28215 if (/* If window is in the process of being destroyed, don't bother
28216 to do anything. */
28217 w->current_matrix != NULL
28218 /* Don't update mouse highlight if hidden. */
28219 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28220 /* Recognize when we are called to operate on rows that don't exist
28221 anymore. This can happen when a window is split. */
28222 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28223 {
28224 bool phys_cursor_on_p = w->phys_cursor_on_p;
28225 struct glyph_row *row, *first, *last;
28226
28227 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28228 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28229
28230 for (row = first; row <= last && row->enabled_p; ++row)
28231 {
28232 int start_hpos, end_hpos, start_x;
28233
28234 /* For all but the first row, the highlight starts at column 0. */
28235 if (row == first)
28236 {
28237 /* R2L rows have BEG and END in reversed order, but the
28238 screen drawing geometry is always left to right. So
28239 we need to mirror the beginning and end of the
28240 highlighted area in R2L rows. */
28241 if (!row->reversed_p)
28242 {
28243 start_hpos = hlinfo->mouse_face_beg_col;
28244 start_x = hlinfo->mouse_face_beg_x;
28245 }
28246 else if (row == last)
28247 {
28248 start_hpos = hlinfo->mouse_face_end_col;
28249 start_x = hlinfo->mouse_face_end_x;
28250 }
28251 else
28252 {
28253 start_hpos = 0;
28254 start_x = 0;
28255 }
28256 }
28257 else if (row->reversed_p && row == last)
28258 {
28259 start_hpos = hlinfo->mouse_face_end_col;
28260 start_x = hlinfo->mouse_face_end_x;
28261 }
28262 else
28263 {
28264 start_hpos = 0;
28265 start_x = 0;
28266 }
28267
28268 if (row == last)
28269 {
28270 if (!row->reversed_p)
28271 end_hpos = hlinfo->mouse_face_end_col;
28272 else if (row == first)
28273 end_hpos = hlinfo->mouse_face_beg_col;
28274 else
28275 {
28276 end_hpos = row->used[TEXT_AREA];
28277 if (draw == DRAW_NORMAL_TEXT)
28278 row->fill_line_p = true; /* Clear to end of line. */
28279 }
28280 }
28281 else if (row->reversed_p && row == first)
28282 end_hpos = hlinfo->mouse_face_beg_col;
28283 else
28284 {
28285 end_hpos = row->used[TEXT_AREA];
28286 if (draw == DRAW_NORMAL_TEXT)
28287 row->fill_line_p = true; /* Clear to end of line. */
28288 }
28289
28290 if (end_hpos > start_hpos)
28291 {
28292 draw_row_with_mouse_face (w, start_x, row,
28293 start_hpos, end_hpos, draw);
28294
28295 row->mouse_face_p
28296 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28297 }
28298 }
28299
28300 #ifdef HAVE_WINDOW_SYSTEM
28301 /* When we've written over the cursor, arrange for it to
28302 be displayed again. */
28303 if (FRAME_WINDOW_P (f)
28304 && phys_cursor_on_p && !w->phys_cursor_on_p)
28305 {
28306 int hpos = w->phys_cursor.hpos;
28307
28308 /* When the window is hscrolled, cursor hpos can legitimately be
28309 out of bounds, but we draw the cursor at the corresponding
28310 window margin in that case. */
28311 if (!row->reversed_p && hpos < 0)
28312 hpos = 0;
28313 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28314 hpos = row->used[TEXT_AREA] - 1;
28315
28316 block_input ();
28317 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28318 w->phys_cursor.x, w->phys_cursor.y);
28319 unblock_input ();
28320 }
28321 #endif /* HAVE_WINDOW_SYSTEM */
28322 }
28323
28324 #ifdef HAVE_WINDOW_SYSTEM
28325 /* Change the mouse cursor. */
28326 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28327 {
28328 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28329 if (draw == DRAW_NORMAL_TEXT
28330 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28331 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28332 else
28333 #endif
28334 if (draw == DRAW_MOUSE_FACE)
28335 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28336 else
28337 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28338 }
28339 #endif /* HAVE_WINDOW_SYSTEM */
28340 }
28341
28342 /* EXPORT:
28343 Clear out the mouse-highlighted active region.
28344 Redraw it un-highlighted first. Value is true if mouse
28345 face was actually drawn unhighlighted. */
28346
28347 bool
28348 clear_mouse_face (Mouse_HLInfo *hlinfo)
28349 {
28350 bool cleared
28351 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28352 if (cleared)
28353 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28354 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28355 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28356 hlinfo->mouse_face_window = Qnil;
28357 hlinfo->mouse_face_overlay = Qnil;
28358 return cleared;
28359 }
28360
28361 /* Return true if the coordinates HPOS and VPOS on windows W are
28362 within the mouse face on that window. */
28363 static bool
28364 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28365 {
28366 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28367
28368 /* Quickly resolve the easy cases. */
28369 if (!(WINDOWP (hlinfo->mouse_face_window)
28370 && XWINDOW (hlinfo->mouse_face_window) == w))
28371 return false;
28372 if (vpos < hlinfo->mouse_face_beg_row
28373 || vpos > hlinfo->mouse_face_end_row)
28374 return false;
28375 if (vpos > hlinfo->mouse_face_beg_row
28376 && vpos < hlinfo->mouse_face_end_row)
28377 return true;
28378
28379 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28380 {
28381 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28382 {
28383 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28384 return true;
28385 }
28386 else if ((vpos == hlinfo->mouse_face_beg_row
28387 && hpos >= hlinfo->mouse_face_beg_col)
28388 || (vpos == hlinfo->mouse_face_end_row
28389 && hpos < hlinfo->mouse_face_end_col))
28390 return true;
28391 }
28392 else
28393 {
28394 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28395 {
28396 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28397 return true;
28398 }
28399 else if ((vpos == hlinfo->mouse_face_beg_row
28400 && hpos <= hlinfo->mouse_face_beg_col)
28401 || (vpos == hlinfo->mouse_face_end_row
28402 && hpos > hlinfo->mouse_face_end_col))
28403 return true;
28404 }
28405 return false;
28406 }
28407
28408
28409 /* EXPORT:
28410 True if physical cursor of window W is within mouse face. */
28411
28412 bool
28413 cursor_in_mouse_face_p (struct window *w)
28414 {
28415 int hpos = w->phys_cursor.hpos;
28416 int vpos = w->phys_cursor.vpos;
28417 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28418
28419 /* When the window is hscrolled, cursor hpos can legitimately be out
28420 of bounds, but we draw the cursor at the corresponding window
28421 margin in that case. */
28422 if (!row->reversed_p && hpos < 0)
28423 hpos = 0;
28424 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28425 hpos = row->used[TEXT_AREA] - 1;
28426
28427 return coords_in_mouse_face_p (w, hpos, vpos);
28428 }
28429
28430
28431 \f
28432 /* Find the glyph rows START_ROW and END_ROW of window W that display
28433 characters between buffer positions START_CHARPOS and END_CHARPOS
28434 (excluding END_CHARPOS). DISP_STRING is a display string that
28435 covers these buffer positions. This is similar to
28436 row_containing_pos, but is more accurate when bidi reordering makes
28437 buffer positions change non-linearly with glyph rows. */
28438 static void
28439 rows_from_pos_range (struct window *w,
28440 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28441 Lisp_Object disp_string,
28442 struct glyph_row **start, struct glyph_row **end)
28443 {
28444 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28445 int last_y = window_text_bottom_y (w);
28446 struct glyph_row *row;
28447
28448 *start = NULL;
28449 *end = NULL;
28450
28451 while (!first->enabled_p
28452 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28453 first++;
28454
28455 /* Find the START row. */
28456 for (row = first;
28457 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28458 row++)
28459 {
28460 /* A row can potentially be the START row if the range of the
28461 characters it displays intersects the range
28462 [START_CHARPOS..END_CHARPOS). */
28463 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28464 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28465 /* See the commentary in row_containing_pos, for the
28466 explanation of the complicated way to check whether
28467 some position is beyond the end of the characters
28468 displayed by a row. */
28469 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28470 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28471 && !row->ends_at_zv_p
28472 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28473 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28474 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28475 && !row->ends_at_zv_p
28476 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28477 {
28478 /* Found a candidate row. Now make sure at least one of the
28479 glyphs it displays has a charpos from the range
28480 [START_CHARPOS..END_CHARPOS).
28481
28482 This is not obvious because bidi reordering could make
28483 buffer positions of a row be 1,2,3,102,101,100, and if we
28484 want to highlight characters in [50..60), we don't want
28485 this row, even though [50..60) does intersect [1..103),
28486 the range of character positions given by the row's start
28487 and end positions. */
28488 struct glyph *g = row->glyphs[TEXT_AREA];
28489 struct glyph *e = g + row->used[TEXT_AREA];
28490
28491 while (g < e)
28492 {
28493 if (((BUFFERP (g->object) || NILP (g->object))
28494 && start_charpos <= g->charpos && g->charpos < end_charpos)
28495 /* A glyph that comes from DISP_STRING is by
28496 definition to be highlighted. */
28497 || EQ (g->object, disp_string))
28498 *start = row;
28499 g++;
28500 }
28501 if (*start)
28502 break;
28503 }
28504 }
28505
28506 /* Find the END row. */
28507 if (!*start
28508 /* If the last row is partially visible, start looking for END
28509 from that row, instead of starting from FIRST. */
28510 && !(row->enabled_p
28511 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28512 row = first;
28513 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28514 {
28515 struct glyph_row *next = row + 1;
28516 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28517
28518 if (!next->enabled_p
28519 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28520 /* The first row >= START whose range of displayed characters
28521 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28522 is the row END + 1. */
28523 || (start_charpos < next_start
28524 && end_charpos < next_start)
28525 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28526 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28527 && !next->ends_at_zv_p
28528 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28529 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28530 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28531 && !next->ends_at_zv_p
28532 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28533 {
28534 *end = row;
28535 break;
28536 }
28537 else
28538 {
28539 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28540 but none of the characters it displays are in the range, it is
28541 also END + 1. */
28542 struct glyph *g = next->glyphs[TEXT_AREA];
28543 struct glyph *s = g;
28544 struct glyph *e = g + next->used[TEXT_AREA];
28545
28546 while (g < e)
28547 {
28548 if (((BUFFERP (g->object) || NILP (g->object))
28549 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28550 /* If the buffer position of the first glyph in
28551 the row is equal to END_CHARPOS, it means
28552 the last character to be highlighted is the
28553 newline of ROW, and we must consider NEXT as
28554 END, not END+1. */
28555 || (((!next->reversed_p && g == s)
28556 || (next->reversed_p && g == e - 1))
28557 && (g->charpos == end_charpos
28558 /* Special case for when NEXT is an
28559 empty line at ZV. */
28560 || (g->charpos == -1
28561 && !row->ends_at_zv_p
28562 && next_start == end_charpos)))))
28563 /* A glyph that comes from DISP_STRING is by
28564 definition to be highlighted. */
28565 || EQ (g->object, disp_string))
28566 break;
28567 g++;
28568 }
28569 if (g == e)
28570 {
28571 *end = row;
28572 break;
28573 }
28574 /* The first row that ends at ZV must be the last to be
28575 highlighted. */
28576 else if (next->ends_at_zv_p)
28577 {
28578 *end = next;
28579 break;
28580 }
28581 }
28582 }
28583 }
28584
28585 /* This function sets the mouse_face_* elements of HLINFO, assuming
28586 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28587 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28588 for the overlay or run of text properties specifying the mouse
28589 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28590 before-string and after-string that must also be highlighted.
28591 DISP_STRING, if non-nil, is a display string that may cover some
28592 or all of the highlighted text. */
28593
28594 static void
28595 mouse_face_from_buffer_pos (Lisp_Object window,
28596 Mouse_HLInfo *hlinfo,
28597 ptrdiff_t mouse_charpos,
28598 ptrdiff_t start_charpos,
28599 ptrdiff_t end_charpos,
28600 Lisp_Object before_string,
28601 Lisp_Object after_string,
28602 Lisp_Object disp_string)
28603 {
28604 struct window *w = XWINDOW (window);
28605 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28606 struct glyph_row *r1, *r2;
28607 struct glyph *glyph, *end;
28608 ptrdiff_t ignore, pos;
28609 int x;
28610
28611 eassert (NILP (disp_string) || STRINGP (disp_string));
28612 eassert (NILP (before_string) || STRINGP (before_string));
28613 eassert (NILP (after_string) || STRINGP (after_string));
28614
28615 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28616 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28617 if (r1 == NULL)
28618 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28619 /* If the before-string or display-string contains newlines,
28620 rows_from_pos_range skips to its last row. Move back. */
28621 if (!NILP (before_string) || !NILP (disp_string))
28622 {
28623 struct glyph_row *prev;
28624 while ((prev = r1 - 1, prev >= first)
28625 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28626 && prev->used[TEXT_AREA] > 0)
28627 {
28628 struct glyph *beg = prev->glyphs[TEXT_AREA];
28629 glyph = beg + prev->used[TEXT_AREA];
28630 while (--glyph >= beg && NILP (glyph->object));
28631 if (glyph < beg
28632 || !(EQ (glyph->object, before_string)
28633 || EQ (glyph->object, disp_string)))
28634 break;
28635 r1 = prev;
28636 }
28637 }
28638 if (r2 == NULL)
28639 {
28640 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28641 hlinfo->mouse_face_past_end = true;
28642 }
28643 else if (!NILP (after_string))
28644 {
28645 /* If the after-string has newlines, advance to its last row. */
28646 struct glyph_row *next;
28647 struct glyph_row *last
28648 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28649
28650 for (next = r2 + 1;
28651 next <= last
28652 && next->used[TEXT_AREA] > 0
28653 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28654 ++next)
28655 r2 = next;
28656 }
28657 /* The rest of the display engine assumes that mouse_face_beg_row is
28658 either above mouse_face_end_row or identical to it. But with
28659 bidi-reordered continued lines, the row for START_CHARPOS could
28660 be below the row for END_CHARPOS. If so, swap the rows and store
28661 them in correct order. */
28662 if (r1->y > r2->y)
28663 {
28664 struct glyph_row *tem = r2;
28665
28666 r2 = r1;
28667 r1 = tem;
28668 }
28669
28670 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28671 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28672
28673 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28674 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28675 could be anywhere in the row and in any order. The strategy
28676 below is to find the leftmost and the rightmost glyph that
28677 belongs to either of these 3 strings, or whose position is
28678 between START_CHARPOS and END_CHARPOS, and highlight all the
28679 glyphs between those two. This may cover more than just the text
28680 between START_CHARPOS and END_CHARPOS if the range of characters
28681 strides the bidi level boundary, e.g. if the beginning is in R2L
28682 text while the end is in L2R text or vice versa. */
28683 if (!r1->reversed_p)
28684 {
28685 /* This row is in a left to right paragraph. Scan it left to
28686 right. */
28687 glyph = r1->glyphs[TEXT_AREA];
28688 end = glyph + r1->used[TEXT_AREA];
28689 x = r1->x;
28690
28691 /* Skip truncation glyphs at the start of the glyph row. */
28692 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28693 for (; glyph < end
28694 && NILP (glyph->object)
28695 && glyph->charpos < 0;
28696 ++glyph)
28697 x += glyph->pixel_width;
28698
28699 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28700 or DISP_STRING, and the first glyph from buffer whose
28701 position is between START_CHARPOS and END_CHARPOS. */
28702 for (; glyph < end
28703 && !NILP (glyph->object)
28704 && !EQ (glyph->object, disp_string)
28705 && !(BUFFERP (glyph->object)
28706 && (glyph->charpos >= start_charpos
28707 && glyph->charpos < end_charpos));
28708 ++glyph)
28709 {
28710 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28711 are present at buffer positions between START_CHARPOS and
28712 END_CHARPOS, or if they come from an overlay. */
28713 if (EQ (glyph->object, before_string))
28714 {
28715 pos = string_buffer_position (before_string,
28716 start_charpos);
28717 /* If pos == 0, it means before_string came from an
28718 overlay, not from a buffer position. */
28719 if (!pos || (pos >= start_charpos && pos < end_charpos))
28720 break;
28721 }
28722 else if (EQ (glyph->object, after_string))
28723 {
28724 pos = string_buffer_position (after_string, end_charpos);
28725 if (!pos || (pos >= start_charpos && pos < end_charpos))
28726 break;
28727 }
28728 x += glyph->pixel_width;
28729 }
28730 hlinfo->mouse_face_beg_x = x;
28731 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28732 }
28733 else
28734 {
28735 /* This row is in a right to left paragraph. Scan it right to
28736 left. */
28737 struct glyph *g;
28738
28739 end = r1->glyphs[TEXT_AREA] - 1;
28740 glyph = end + r1->used[TEXT_AREA];
28741
28742 /* Skip truncation glyphs at the start of the glyph row. */
28743 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28744 for (; glyph > end
28745 && NILP (glyph->object)
28746 && glyph->charpos < 0;
28747 --glyph)
28748 ;
28749
28750 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28751 or DISP_STRING, and the first glyph from buffer whose
28752 position is between START_CHARPOS and END_CHARPOS. */
28753 for (; glyph > end
28754 && !NILP (glyph->object)
28755 && !EQ (glyph->object, disp_string)
28756 && !(BUFFERP (glyph->object)
28757 && (glyph->charpos >= start_charpos
28758 && glyph->charpos < end_charpos));
28759 --glyph)
28760 {
28761 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28762 are present at buffer positions between START_CHARPOS and
28763 END_CHARPOS, or if they come from an overlay. */
28764 if (EQ (glyph->object, before_string))
28765 {
28766 pos = string_buffer_position (before_string, start_charpos);
28767 /* If pos == 0, it means before_string came from an
28768 overlay, not from a buffer position. */
28769 if (!pos || (pos >= start_charpos && pos < end_charpos))
28770 break;
28771 }
28772 else if (EQ (glyph->object, after_string))
28773 {
28774 pos = string_buffer_position (after_string, end_charpos);
28775 if (!pos || (pos >= start_charpos && pos < end_charpos))
28776 break;
28777 }
28778 }
28779
28780 glyph++; /* first glyph to the right of the highlighted area */
28781 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28782 x += g->pixel_width;
28783 hlinfo->mouse_face_beg_x = x;
28784 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28785 }
28786
28787 /* If the highlight ends in a different row, compute GLYPH and END
28788 for the end row. Otherwise, reuse the values computed above for
28789 the row where the highlight begins. */
28790 if (r2 != r1)
28791 {
28792 if (!r2->reversed_p)
28793 {
28794 glyph = r2->glyphs[TEXT_AREA];
28795 end = glyph + r2->used[TEXT_AREA];
28796 x = r2->x;
28797 }
28798 else
28799 {
28800 end = r2->glyphs[TEXT_AREA] - 1;
28801 glyph = end + r2->used[TEXT_AREA];
28802 }
28803 }
28804
28805 if (!r2->reversed_p)
28806 {
28807 /* Skip truncation and continuation glyphs near the end of the
28808 row, and also blanks and stretch glyphs inserted by
28809 extend_face_to_end_of_line. */
28810 while (end > glyph
28811 && NILP ((end - 1)->object))
28812 --end;
28813 /* Scan the rest of the glyph row from the end, looking for the
28814 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28815 DISP_STRING, or whose position is between START_CHARPOS
28816 and END_CHARPOS */
28817 for (--end;
28818 end > glyph
28819 && !NILP (end->object)
28820 && !EQ (end->object, disp_string)
28821 && !(BUFFERP (end->object)
28822 && (end->charpos >= start_charpos
28823 && end->charpos < end_charpos));
28824 --end)
28825 {
28826 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28827 are present at buffer positions between START_CHARPOS and
28828 END_CHARPOS, or if they come from an overlay. */
28829 if (EQ (end->object, before_string))
28830 {
28831 pos = string_buffer_position (before_string, start_charpos);
28832 if (!pos || (pos >= start_charpos && pos < end_charpos))
28833 break;
28834 }
28835 else if (EQ (end->object, after_string))
28836 {
28837 pos = string_buffer_position (after_string, end_charpos);
28838 if (!pos || (pos >= start_charpos && pos < end_charpos))
28839 break;
28840 }
28841 }
28842 /* Find the X coordinate of the last glyph to be highlighted. */
28843 for (; glyph <= end; ++glyph)
28844 x += glyph->pixel_width;
28845
28846 hlinfo->mouse_face_end_x = x;
28847 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28848 }
28849 else
28850 {
28851 /* Skip truncation and continuation glyphs near the end of the
28852 row, and also blanks and stretch glyphs inserted by
28853 extend_face_to_end_of_line. */
28854 x = r2->x;
28855 end++;
28856 while (end < glyph
28857 && NILP (end->object))
28858 {
28859 x += end->pixel_width;
28860 ++end;
28861 }
28862 /* Scan the rest of the glyph row from the end, looking for the
28863 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28864 DISP_STRING, or whose position is between START_CHARPOS
28865 and END_CHARPOS */
28866 for ( ;
28867 end < glyph
28868 && !NILP (end->object)
28869 && !EQ (end->object, disp_string)
28870 && !(BUFFERP (end->object)
28871 && (end->charpos >= start_charpos
28872 && end->charpos < end_charpos));
28873 ++end)
28874 {
28875 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28876 are present at buffer positions between START_CHARPOS and
28877 END_CHARPOS, or if they come from an overlay. */
28878 if (EQ (end->object, before_string))
28879 {
28880 pos = string_buffer_position (before_string, start_charpos);
28881 if (!pos || (pos >= start_charpos && pos < end_charpos))
28882 break;
28883 }
28884 else if (EQ (end->object, after_string))
28885 {
28886 pos = string_buffer_position (after_string, end_charpos);
28887 if (!pos || (pos >= start_charpos && pos < end_charpos))
28888 break;
28889 }
28890 x += end->pixel_width;
28891 }
28892 /* If we exited the above loop because we arrived at the last
28893 glyph of the row, and its buffer position is still not in
28894 range, it means the last character in range is the preceding
28895 newline. Bump the end column and x values to get past the
28896 last glyph. */
28897 if (end == glyph
28898 && BUFFERP (end->object)
28899 && (end->charpos < start_charpos
28900 || end->charpos >= end_charpos))
28901 {
28902 x += end->pixel_width;
28903 ++end;
28904 }
28905 hlinfo->mouse_face_end_x = x;
28906 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28907 }
28908
28909 hlinfo->mouse_face_window = window;
28910 hlinfo->mouse_face_face_id
28911 = face_at_buffer_position (w, mouse_charpos, &ignore,
28912 mouse_charpos + 1,
28913 !hlinfo->mouse_face_hidden, -1);
28914 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28915 }
28916
28917 /* The following function is not used anymore (replaced with
28918 mouse_face_from_string_pos), but I leave it here for the time
28919 being, in case someone would. */
28920
28921 #if false /* not used */
28922
28923 /* Find the position of the glyph for position POS in OBJECT in
28924 window W's current matrix, and return in *X, *Y the pixel
28925 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28926
28927 RIGHT_P means return the position of the right edge of the glyph.
28928 !RIGHT_P means return the left edge position.
28929
28930 If no glyph for POS exists in the matrix, return the position of
28931 the glyph with the next smaller position that is in the matrix, if
28932 RIGHT_P is false. If RIGHT_P, and no glyph for POS
28933 exists in the matrix, return the position of the glyph with the
28934 next larger position in OBJECT.
28935
28936 Value is true if a glyph was found. */
28937
28938 static bool
28939 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28940 int *hpos, int *vpos, int *x, int *y, bool right_p)
28941 {
28942 int yb = window_text_bottom_y (w);
28943 struct glyph_row *r;
28944 struct glyph *best_glyph = NULL;
28945 struct glyph_row *best_row = NULL;
28946 int best_x = 0;
28947
28948 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28949 r->enabled_p && r->y < yb;
28950 ++r)
28951 {
28952 struct glyph *g = r->glyphs[TEXT_AREA];
28953 struct glyph *e = g + r->used[TEXT_AREA];
28954 int gx;
28955
28956 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28957 if (EQ (g->object, object))
28958 {
28959 if (g->charpos == pos)
28960 {
28961 best_glyph = g;
28962 best_x = gx;
28963 best_row = r;
28964 goto found;
28965 }
28966 else if (best_glyph == NULL
28967 || ((eabs (g->charpos - pos)
28968 < eabs (best_glyph->charpos - pos))
28969 && (right_p
28970 ? g->charpos < pos
28971 : g->charpos > pos)))
28972 {
28973 best_glyph = g;
28974 best_x = gx;
28975 best_row = r;
28976 }
28977 }
28978 }
28979
28980 found:
28981
28982 if (best_glyph)
28983 {
28984 *x = best_x;
28985 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28986
28987 if (right_p)
28988 {
28989 *x += best_glyph->pixel_width;
28990 ++*hpos;
28991 }
28992
28993 *y = best_row->y;
28994 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28995 }
28996
28997 return best_glyph != NULL;
28998 }
28999 #endif /* not used */
29000
29001 /* Find the positions of the first and the last glyphs in window W's
29002 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29003 (assumed to be a string), and return in HLINFO's mouse_face_*
29004 members the pixel and column/row coordinates of those glyphs. */
29005
29006 static void
29007 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29008 Lisp_Object object,
29009 ptrdiff_t startpos, ptrdiff_t endpos)
29010 {
29011 int yb = window_text_bottom_y (w);
29012 struct glyph_row *r;
29013 struct glyph *g, *e;
29014 int gx;
29015 bool found = false;
29016
29017 /* Find the glyph row with at least one position in the range
29018 [STARTPOS..ENDPOS), and the first glyph in that row whose
29019 position belongs to that range. */
29020 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29021 r->enabled_p && r->y < yb;
29022 ++r)
29023 {
29024 if (!r->reversed_p)
29025 {
29026 g = r->glyphs[TEXT_AREA];
29027 e = g + r->used[TEXT_AREA];
29028 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29029 if (EQ (g->object, object)
29030 && startpos <= g->charpos && g->charpos < endpos)
29031 {
29032 hlinfo->mouse_face_beg_row
29033 = MATRIX_ROW_VPOS (r, w->current_matrix);
29034 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29035 hlinfo->mouse_face_beg_x = gx;
29036 found = true;
29037 break;
29038 }
29039 }
29040 else
29041 {
29042 struct glyph *g1;
29043
29044 e = r->glyphs[TEXT_AREA];
29045 g = e + r->used[TEXT_AREA];
29046 for ( ; g > e; --g)
29047 if (EQ ((g-1)->object, object)
29048 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29049 {
29050 hlinfo->mouse_face_beg_row
29051 = MATRIX_ROW_VPOS (r, w->current_matrix);
29052 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29053 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29054 gx += g1->pixel_width;
29055 hlinfo->mouse_face_beg_x = gx;
29056 found = true;
29057 break;
29058 }
29059 }
29060 if (found)
29061 break;
29062 }
29063
29064 if (!found)
29065 return;
29066
29067 /* Starting with the next row, look for the first row which does NOT
29068 include any glyphs whose positions are in the range. */
29069 for (++r; r->enabled_p && r->y < yb; ++r)
29070 {
29071 g = r->glyphs[TEXT_AREA];
29072 e = g + r->used[TEXT_AREA];
29073 found = false;
29074 for ( ; g < e; ++g)
29075 if (EQ (g->object, object)
29076 && startpos <= g->charpos && g->charpos < endpos)
29077 {
29078 found = true;
29079 break;
29080 }
29081 if (!found)
29082 break;
29083 }
29084
29085 /* The highlighted region ends on the previous row. */
29086 r--;
29087
29088 /* Set the end row. */
29089 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29090
29091 /* Compute and set the end column and the end column's horizontal
29092 pixel coordinate. */
29093 if (!r->reversed_p)
29094 {
29095 g = r->glyphs[TEXT_AREA];
29096 e = g + r->used[TEXT_AREA];
29097 for ( ; e > g; --e)
29098 if (EQ ((e-1)->object, object)
29099 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29100 break;
29101 hlinfo->mouse_face_end_col = e - g;
29102
29103 for (gx = r->x; g < e; ++g)
29104 gx += g->pixel_width;
29105 hlinfo->mouse_face_end_x = gx;
29106 }
29107 else
29108 {
29109 e = r->glyphs[TEXT_AREA];
29110 g = e + r->used[TEXT_AREA];
29111 for (gx = r->x ; e < g; ++e)
29112 {
29113 if (EQ (e->object, object)
29114 && startpos <= e->charpos && e->charpos < endpos)
29115 break;
29116 gx += e->pixel_width;
29117 }
29118 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29119 hlinfo->mouse_face_end_x = gx;
29120 }
29121 }
29122
29123 #ifdef HAVE_WINDOW_SYSTEM
29124
29125 /* See if position X, Y is within a hot-spot of an image. */
29126
29127 static bool
29128 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29129 {
29130 if (!CONSP (hot_spot))
29131 return false;
29132
29133 if (EQ (XCAR (hot_spot), Qrect))
29134 {
29135 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29136 Lisp_Object rect = XCDR (hot_spot);
29137 Lisp_Object tem;
29138 if (!CONSP (rect))
29139 return false;
29140 if (!CONSP (XCAR (rect)))
29141 return false;
29142 if (!CONSP (XCDR (rect)))
29143 return false;
29144 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29145 return false;
29146 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29147 return false;
29148 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29149 return false;
29150 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29151 return false;
29152 return true;
29153 }
29154 else if (EQ (XCAR (hot_spot), Qcircle))
29155 {
29156 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29157 Lisp_Object circ = XCDR (hot_spot);
29158 Lisp_Object lr, lx0, ly0;
29159 if (CONSP (circ)
29160 && CONSP (XCAR (circ))
29161 && (lr = XCDR (circ), NUMBERP (lr))
29162 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29163 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29164 {
29165 double r = XFLOATINT (lr);
29166 double dx = XINT (lx0) - x;
29167 double dy = XINT (ly0) - y;
29168 return (dx * dx + dy * dy <= r * r);
29169 }
29170 }
29171 else if (EQ (XCAR (hot_spot), Qpoly))
29172 {
29173 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29174 if (VECTORP (XCDR (hot_spot)))
29175 {
29176 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29177 Lisp_Object *poly = v->contents;
29178 ptrdiff_t n = v->header.size;
29179 ptrdiff_t i;
29180 bool inside = false;
29181 Lisp_Object lx, ly;
29182 int x0, y0;
29183
29184 /* Need an even number of coordinates, and at least 3 edges. */
29185 if (n < 6 || n & 1)
29186 return false;
29187
29188 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29189 If count is odd, we are inside polygon. Pixels on edges
29190 may or may not be included depending on actual geometry of the
29191 polygon. */
29192 if ((lx = poly[n-2], !INTEGERP (lx))
29193 || (ly = poly[n-1], !INTEGERP (lx)))
29194 return false;
29195 x0 = XINT (lx), y0 = XINT (ly);
29196 for (i = 0; i < n; i += 2)
29197 {
29198 int x1 = x0, y1 = y0;
29199 if ((lx = poly[i], !INTEGERP (lx))
29200 || (ly = poly[i+1], !INTEGERP (ly)))
29201 return false;
29202 x0 = XINT (lx), y0 = XINT (ly);
29203
29204 /* Does this segment cross the X line? */
29205 if (x0 >= x)
29206 {
29207 if (x1 >= x)
29208 continue;
29209 }
29210 else if (x1 < x)
29211 continue;
29212 if (y > y0 && y > y1)
29213 continue;
29214 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29215 inside = !inside;
29216 }
29217 return inside;
29218 }
29219 }
29220 return false;
29221 }
29222
29223 Lisp_Object
29224 find_hot_spot (Lisp_Object map, int x, int y)
29225 {
29226 while (CONSP (map))
29227 {
29228 if (CONSP (XCAR (map))
29229 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29230 return XCAR (map);
29231 map = XCDR (map);
29232 }
29233
29234 return Qnil;
29235 }
29236
29237 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29238 3, 3, 0,
29239 doc: /* Lookup in image map MAP coordinates X and Y.
29240 An image map is an alist where each element has the format (AREA ID PLIST).
29241 An AREA is specified as either a rectangle, a circle, or a polygon:
29242 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29243 pixel coordinates of the upper left and bottom right corners.
29244 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29245 and the radius of the circle; r may be a float or integer.
29246 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29247 vector describes one corner in the polygon.
29248 Returns the alist element for the first matching AREA in MAP. */)
29249 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29250 {
29251 if (NILP (map))
29252 return Qnil;
29253
29254 CHECK_NUMBER (x);
29255 CHECK_NUMBER (y);
29256
29257 return find_hot_spot (map,
29258 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29259 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29260 }
29261
29262
29263 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29264 static void
29265 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29266 {
29267 /* Do not change cursor shape while dragging mouse. */
29268 if (EQ (do_mouse_tracking, Qdragging))
29269 return;
29270
29271 if (!NILP (pointer))
29272 {
29273 if (EQ (pointer, Qarrow))
29274 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29275 else if (EQ (pointer, Qhand))
29276 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29277 else if (EQ (pointer, Qtext))
29278 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29279 else if (EQ (pointer, intern ("hdrag")))
29280 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29281 else if (EQ (pointer, intern ("nhdrag")))
29282 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29283 #ifdef HAVE_X_WINDOWS
29284 else if (EQ (pointer, intern ("vdrag")))
29285 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29286 #endif
29287 else if (EQ (pointer, intern ("hourglass")))
29288 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29289 else if (EQ (pointer, Qmodeline))
29290 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29291 else
29292 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29293 }
29294
29295 if (cursor != No_Cursor)
29296 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29297 }
29298
29299 #endif /* HAVE_WINDOW_SYSTEM */
29300
29301 /* Take proper action when mouse has moved to the mode or header line
29302 or marginal area AREA of window W, x-position X and y-position Y.
29303 X is relative to the start of the text display area of W, so the
29304 width of bitmap areas and scroll bars must be subtracted to get a
29305 position relative to the start of the mode line. */
29306
29307 static void
29308 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29309 enum window_part area)
29310 {
29311 struct window *w = XWINDOW (window);
29312 struct frame *f = XFRAME (w->frame);
29313 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29314 #ifdef HAVE_WINDOW_SYSTEM
29315 Display_Info *dpyinfo;
29316 #endif
29317 Cursor cursor = No_Cursor;
29318 Lisp_Object pointer = Qnil;
29319 int dx, dy, width, height;
29320 ptrdiff_t charpos;
29321 Lisp_Object string, object = Qnil;
29322 Lisp_Object pos IF_LINT (= Qnil), help;
29323
29324 Lisp_Object mouse_face;
29325 int original_x_pixel = x;
29326 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29327 struct glyph_row *row IF_LINT (= 0);
29328
29329 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29330 {
29331 int x0;
29332 struct glyph *end;
29333
29334 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29335 returns them in row/column units! */
29336 string = mode_line_string (w, area, &x, &y, &charpos,
29337 &object, &dx, &dy, &width, &height);
29338
29339 row = (area == ON_MODE_LINE
29340 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29341 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29342
29343 /* Find the glyph under the mouse pointer. */
29344 if (row->mode_line_p && row->enabled_p)
29345 {
29346 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29347 end = glyph + row->used[TEXT_AREA];
29348
29349 for (x0 = original_x_pixel;
29350 glyph < end && x0 >= glyph->pixel_width;
29351 ++glyph)
29352 x0 -= glyph->pixel_width;
29353
29354 if (glyph >= end)
29355 glyph = NULL;
29356 }
29357 }
29358 else
29359 {
29360 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29361 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29362 returns them in row/column units! */
29363 string = marginal_area_string (w, area, &x, &y, &charpos,
29364 &object, &dx, &dy, &width, &height);
29365 }
29366
29367 help = Qnil;
29368
29369 #ifdef HAVE_WINDOW_SYSTEM
29370 if (IMAGEP (object))
29371 {
29372 Lisp_Object image_map, hotspot;
29373 if ((image_map = Fplist_get (XCDR (object), QCmap),
29374 !NILP (image_map))
29375 && (hotspot = find_hot_spot (image_map, dx, dy),
29376 CONSP (hotspot))
29377 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29378 {
29379 Lisp_Object plist;
29380
29381 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29382 If so, we could look for mouse-enter, mouse-leave
29383 properties in PLIST (and do something...). */
29384 hotspot = XCDR (hotspot);
29385 if (CONSP (hotspot)
29386 && (plist = XCAR (hotspot), CONSP (plist)))
29387 {
29388 pointer = Fplist_get (plist, Qpointer);
29389 if (NILP (pointer))
29390 pointer = Qhand;
29391 help = Fplist_get (plist, Qhelp_echo);
29392 if (!NILP (help))
29393 {
29394 help_echo_string = help;
29395 XSETWINDOW (help_echo_window, w);
29396 help_echo_object = w->contents;
29397 help_echo_pos = charpos;
29398 }
29399 }
29400 }
29401 if (NILP (pointer))
29402 pointer = Fplist_get (XCDR (object), QCpointer);
29403 }
29404 #endif /* HAVE_WINDOW_SYSTEM */
29405
29406 if (STRINGP (string))
29407 pos = make_number (charpos);
29408
29409 /* Set the help text and mouse pointer. If the mouse is on a part
29410 of the mode line without any text (e.g. past the right edge of
29411 the mode line text), use the default help text and pointer. */
29412 if (STRINGP (string) || area == ON_MODE_LINE)
29413 {
29414 /* Arrange to display the help by setting the global variables
29415 help_echo_string, help_echo_object, and help_echo_pos. */
29416 if (NILP (help))
29417 {
29418 if (STRINGP (string))
29419 help = Fget_text_property (pos, Qhelp_echo, string);
29420
29421 if (!NILP (help))
29422 {
29423 help_echo_string = help;
29424 XSETWINDOW (help_echo_window, w);
29425 help_echo_object = string;
29426 help_echo_pos = charpos;
29427 }
29428 else if (area == ON_MODE_LINE)
29429 {
29430 Lisp_Object default_help
29431 = buffer_local_value (Qmode_line_default_help_echo,
29432 w->contents);
29433
29434 if (STRINGP (default_help))
29435 {
29436 help_echo_string = default_help;
29437 XSETWINDOW (help_echo_window, w);
29438 help_echo_object = Qnil;
29439 help_echo_pos = -1;
29440 }
29441 }
29442 }
29443
29444 #ifdef HAVE_WINDOW_SYSTEM
29445 /* Change the mouse pointer according to what is under it. */
29446 if (FRAME_WINDOW_P (f))
29447 {
29448 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29449 || minibuf_level
29450 || NILP (Vresize_mini_windows));
29451
29452 dpyinfo = FRAME_DISPLAY_INFO (f);
29453 if (STRINGP (string))
29454 {
29455 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29456
29457 if (NILP (pointer))
29458 pointer = Fget_text_property (pos, Qpointer, string);
29459
29460 /* Change the mouse pointer according to what is under X/Y. */
29461 if (NILP (pointer)
29462 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29463 {
29464 Lisp_Object map;
29465 map = Fget_text_property (pos, Qlocal_map, string);
29466 if (!KEYMAPP (map))
29467 map = Fget_text_property (pos, Qkeymap, string);
29468 if (!KEYMAPP (map) && draggable)
29469 cursor = dpyinfo->vertical_scroll_bar_cursor;
29470 }
29471 }
29472 else if (draggable)
29473 /* Default mode-line pointer. */
29474 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29475 }
29476 #endif
29477 }
29478
29479 /* Change the mouse face according to what is under X/Y. */
29480 bool mouse_face_shown = false;
29481 if (STRINGP (string))
29482 {
29483 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29484 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29485 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29486 && glyph)
29487 {
29488 Lisp_Object b, e;
29489
29490 struct glyph * tmp_glyph;
29491
29492 int gpos;
29493 int gseq_length;
29494 int total_pixel_width;
29495 ptrdiff_t begpos, endpos, ignore;
29496
29497 int vpos, hpos;
29498
29499 b = Fprevious_single_property_change (make_number (charpos + 1),
29500 Qmouse_face, string, Qnil);
29501 if (NILP (b))
29502 begpos = 0;
29503 else
29504 begpos = XINT (b);
29505
29506 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29507 if (NILP (e))
29508 endpos = SCHARS (string);
29509 else
29510 endpos = XINT (e);
29511
29512 /* Calculate the glyph position GPOS of GLYPH in the
29513 displayed string, relative to the beginning of the
29514 highlighted part of the string.
29515
29516 Note: GPOS is different from CHARPOS. CHARPOS is the
29517 position of GLYPH in the internal string object. A mode
29518 line string format has structures which are converted to
29519 a flattened string by the Emacs Lisp interpreter. The
29520 internal string is an element of those structures. The
29521 displayed string is the flattened string. */
29522 tmp_glyph = row_start_glyph;
29523 while (tmp_glyph < glyph
29524 && (!(EQ (tmp_glyph->object, glyph->object)
29525 && begpos <= tmp_glyph->charpos
29526 && tmp_glyph->charpos < endpos)))
29527 tmp_glyph++;
29528 gpos = glyph - tmp_glyph;
29529
29530 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29531 the highlighted part of the displayed string to which
29532 GLYPH belongs. Note: GSEQ_LENGTH is different from
29533 SCHARS (STRING), because the latter returns the length of
29534 the internal string. */
29535 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29536 tmp_glyph > glyph
29537 && (!(EQ (tmp_glyph->object, glyph->object)
29538 && begpos <= tmp_glyph->charpos
29539 && tmp_glyph->charpos < endpos));
29540 tmp_glyph--)
29541 ;
29542 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29543
29544 /* Calculate the total pixel width of all the glyphs between
29545 the beginning of the highlighted area and GLYPH. */
29546 total_pixel_width = 0;
29547 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29548 total_pixel_width += tmp_glyph->pixel_width;
29549
29550 /* Pre calculation of re-rendering position. Note: X is in
29551 column units here, after the call to mode_line_string or
29552 marginal_area_string. */
29553 hpos = x - gpos;
29554 vpos = (area == ON_MODE_LINE
29555 ? (w->current_matrix)->nrows - 1
29556 : 0);
29557
29558 /* If GLYPH's position is included in the region that is
29559 already drawn in mouse face, we have nothing to do. */
29560 if ( EQ (window, hlinfo->mouse_face_window)
29561 && (!row->reversed_p
29562 ? (hlinfo->mouse_face_beg_col <= hpos
29563 && hpos < hlinfo->mouse_face_end_col)
29564 /* In R2L rows we swap BEG and END, see below. */
29565 : (hlinfo->mouse_face_end_col <= hpos
29566 && hpos < hlinfo->mouse_face_beg_col))
29567 && hlinfo->mouse_face_beg_row == vpos )
29568 return;
29569
29570 if (clear_mouse_face (hlinfo))
29571 cursor = No_Cursor;
29572
29573 if (!row->reversed_p)
29574 {
29575 hlinfo->mouse_face_beg_col = hpos;
29576 hlinfo->mouse_face_beg_x = original_x_pixel
29577 - (total_pixel_width + dx);
29578 hlinfo->mouse_face_end_col = hpos + gseq_length;
29579 hlinfo->mouse_face_end_x = 0;
29580 }
29581 else
29582 {
29583 /* In R2L rows, show_mouse_face expects BEG and END
29584 coordinates to be swapped. */
29585 hlinfo->mouse_face_end_col = hpos;
29586 hlinfo->mouse_face_end_x = original_x_pixel
29587 - (total_pixel_width + dx);
29588 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29589 hlinfo->mouse_face_beg_x = 0;
29590 }
29591
29592 hlinfo->mouse_face_beg_row = vpos;
29593 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29594 hlinfo->mouse_face_past_end = false;
29595 hlinfo->mouse_face_window = window;
29596
29597 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29598 charpos,
29599 0, &ignore,
29600 glyph->face_id,
29601 true);
29602 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29603 mouse_face_shown = true;
29604
29605 if (NILP (pointer))
29606 pointer = Qhand;
29607 }
29608 }
29609
29610 /* If mouse-face doesn't need to be shown, clear any existing
29611 mouse-face. */
29612 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
29613 clear_mouse_face (hlinfo);
29614
29615 #ifdef HAVE_WINDOW_SYSTEM
29616 if (FRAME_WINDOW_P (f))
29617 define_frame_cursor1 (f, cursor, pointer);
29618 #endif
29619 }
29620
29621
29622 /* EXPORT:
29623 Take proper action when the mouse has moved to position X, Y on
29624 frame F with regards to highlighting portions of display that have
29625 mouse-face properties. Also de-highlight portions of display where
29626 the mouse was before, set the mouse pointer shape as appropriate
29627 for the mouse coordinates, and activate help echo (tooltips).
29628 X and Y can be negative or out of range. */
29629
29630 void
29631 note_mouse_highlight (struct frame *f, int x, int y)
29632 {
29633 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29634 enum window_part part = ON_NOTHING;
29635 Lisp_Object window;
29636 struct window *w;
29637 Cursor cursor = No_Cursor;
29638 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29639 struct buffer *b;
29640
29641 /* When a menu is active, don't highlight because this looks odd. */
29642 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29643 if (popup_activated ())
29644 return;
29645 #endif
29646
29647 if (!f->glyphs_initialized_p
29648 || f->pointer_invisible)
29649 return;
29650
29651 hlinfo->mouse_face_mouse_x = x;
29652 hlinfo->mouse_face_mouse_y = y;
29653 hlinfo->mouse_face_mouse_frame = f;
29654
29655 if (hlinfo->mouse_face_defer)
29656 return;
29657
29658 /* Which window is that in? */
29659 window = window_from_coordinates (f, x, y, &part, true);
29660
29661 /* If displaying active text in another window, clear that. */
29662 if (! EQ (window, hlinfo->mouse_face_window)
29663 /* Also clear if we move out of text area in same window. */
29664 || (!NILP (hlinfo->mouse_face_window)
29665 && !NILP (window)
29666 && part != ON_TEXT
29667 && part != ON_MODE_LINE
29668 && part != ON_HEADER_LINE))
29669 clear_mouse_face (hlinfo);
29670
29671 /* Not on a window -> return. */
29672 if (!WINDOWP (window))
29673 return;
29674
29675 /* Reset help_echo_string. It will get recomputed below. */
29676 help_echo_string = Qnil;
29677
29678 /* Convert to window-relative pixel coordinates. */
29679 w = XWINDOW (window);
29680 frame_to_window_pixel_xy (w, &x, &y);
29681
29682 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29683 /* Handle tool-bar window differently since it doesn't display a
29684 buffer. */
29685 if (EQ (window, f->tool_bar_window))
29686 {
29687 note_tool_bar_highlight (f, x, y);
29688 return;
29689 }
29690 #endif
29691
29692 /* Mouse is on the mode, header line or margin? */
29693 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29694 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29695 {
29696 note_mode_line_or_margin_highlight (window, x, y, part);
29697
29698 #ifdef HAVE_WINDOW_SYSTEM
29699 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29700 {
29701 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29702 /* Show non-text cursor (Bug#16647). */
29703 goto set_cursor;
29704 }
29705 else
29706 #endif
29707 return;
29708 }
29709
29710 #ifdef HAVE_WINDOW_SYSTEM
29711 if (part == ON_VERTICAL_BORDER)
29712 {
29713 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29714 help_echo_string = build_string ("drag-mouse-1: resize");
29715 }
29716 else if (part == ON_RIGHT_DIVIDER)
29717 {
29718 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29719 help_echo_string = build_string ("drag-mouse-1: resize");
29720 }
29721 else if (part == ON_BOTTOM_DIVIDER)
29722 if (! WINDOW_BOTTOMMOST_P (w)
29723 || minibuf_level
29724 || NILP (Vresize_mini_windows))
29725 {
29726 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29727 help_echo_string = build_string ("drag-mouse-1: resize");
29728 }
29729 else
29730 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29731 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29732 || part == ON_VERTICAL_SCROLL_BAR
29733 || part == ON_HORIZONTAL_SCROLL_BAR)
29734 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29735 else
29736 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29737 #endif
29738
29739 /* Are we in a window whose display is up to date?
29740 And verify the buffer's text has not changed. */
29741 b = XBUFFER (w->contents);
29742 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29743 {
29744 int hpos, vpos, dx, dy, area = LAST_AREA;
29745 ptrdiff_t pos;
29746 struct glyph *glyph;
29747 Lisp_Object object;
29748 Lisp_Object mouse_face = Qnil, position;
29749 Lisp_Object *overlay_vec = NULL;
29750 ptrdiff_t i, noverlays;
29751 struct buffer *obuf;
29752 ptrdiff_t obegv, ozv;
29753 bool same_region;
29754
29755 /* Find the glyph under X/Y. */
29756 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29757
29758 #ifdef HAVE_WINDOW_SYSTEM
29759 /* Look for :pointer property on image. */
29760 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29761 {
29762 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29763 if (img != NULL && IMAGEP (img->spec))
29764 {
29765 Lisp_Object image_map, hotspot;
29766 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29767 !NILP (image_map))
29768 && (hotspot = find_hot_spot (image_map,
29769 glyph->slice.img.x + dx,
29770 glyph->slice.img.y + dy),
29771 CONSP (hotspot))
29772 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29773 {
29774 Lisp_Object plist;
29775
29776 /* Could check XCAR (hotspot) to see if we enter/leave
29777 this hot-spot.
29778 If so, we could look for mouse-enter, mouse-leave
29779 properties in PLIST (and do something...). */
29780 hotspot = XCDR (hotspot);
29781 if (CONSP (hotspot)
29782 && (plist = XCAR (hotspot), CONSP (plist)))
29783 {
29784 pointer = Fplist_get (plist, Qpointer);
29785 if (NILP (pointer))
29786 pointer = Qhand;
29787 help_echo_string = Fplist_get (plist, Qhelp_echo);
29788 if (!NILP (help_echo_string))
29789 {
29790 help_echo_window = window;
29791 help_echo_object = glyph->object;
29792 help_echo_pos = glyph->charpos;
29793 }
29794 }
29795 }
29796 if (NILP (pointer))
29797 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29798 }
29799 }
29800 #endif /* HAVE_WINDOW_SYSTEM */
29801
29802 /* Clear mouse face if X/Y not over text. */
29803 if (glyph == NULL
29804 || area != TEXT_AREA
29805 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29806 /* Glyph's OBJECT is nil for glyphs inserted by the
29807 display engine for its internal purposes, like truncation
29808 and continuation glyphs and blanks beyond the end of
29809 line's text on text terminals. If we are over such a
29810 glyph, we are not over any text. */
29811 || NILP (glyph->object)
29812 /* R2L rows have a stretch glyph at their front, which
29813 stands for no text, whereas L2R rows have no glyphs at
29814 all beyond the end of text. Treat such stretch glyphs
29815 like we do with NULL glyphs in L2R rows. */
29816 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29817 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29818 && glyph->type == STRETCH_GLYPH
29819 && glyph->avoid_cursor_p))
29820 {
29821 if (clear_mouse_face (hlinfo))
29822 cursor = No_Cursor;
29823 #ifdef HAVE_WINDOW_SYSTEM
29824 if (FRAME_WINDOW_P (f) && NILP (pointer))
29825 {
29826 if (area != TEXT_AREA)
29827 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29828 else
29829 pointer = Vvoid_text_area_pointer;
29830 }
29831 #endif
29832 goto set_cursor;
29833 }
29834
29835 pos = glyph->charpos;
29836 object = glyph->object;
29837 if (!STRINGP (object) && !BUFFERP (object))
29838 goto set_cursor;
29839
29840 /* If we get an out-of-range value, return now; avoid an error. */
29841 if (BUFFERP (object) && pos > BUF_Z (b))
29842 goto set_cursor;
29843
29844 /* Make the window's buffer temporarily current for
29845 overlays_at and compute_char_face. */
29846 obuf = current_buffer;
29847 current_buffer = b;
29848 obegv = BEGV;
29849 ozv = ZV;
29850 BEGV = BEG;
29851 ZV = Z;
29852
29853 /* Is this char mouse-active or does it have help-echo? */
29854 position = make_number (pos);
29855
29856 USE_SAFE_ALLOCA;
29857
29858 if (BUFFERP (object))
29859 {
29860 /* Put all the overlays we want in a vector in overlay_vec. */
29861 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
29862 /* Sort overlays into increasing priority order. */
29863 noverlays = sort_overlays (overlay_vec, noverlays, w);
29864 }
29865 else
29866 noverlays = 0;
29867
29868 if (NILP (Vmouse_highlight))
29869 {
29870 clear_mouse_face (hlinfo);
29871 goto check_help_echo;
29872 }
29873
29874 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29875
29876 if (same_region)
29877 cursor = No_Cursor;
29878
29879 /* Check mouse-face highlighting. */
29880 if (! same_region
29881 /* If there exists an overlay with mouse-face overlapping
29882 the one we are currently highlighting, we have to
29883 check if we enter the overlapping overlay, and then
29884 highlight only that. */
29885 || (OVERLAYP (hlinfo->mouse_face_overlay)
29886 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29887 {
29888 /* Find the highest priority overlay with a mouse-face. */
29889 Lisp_Object overlay = Qnil;
29890 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29891 {
29892 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29893 if (!NILP (mouse_face))
29894 overlay = overlay_vec[i];
29895 }
29896
29897 /* If we're highlighting the same overlay as before, there's
29898 no need to do that again. */
29899 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29900 goto check_help_echo;
29901 hlinfo->mouse_face_overlay = overlay;
29902
29903 /* Clear the display of the old active region, if any. */
29904 if (clear_mouse_face (hlinfo))
29905 cursor = No_Cursor;
29906
29907 /* If no overlay applies, get a text property. */
29908 if (NILP (overlay))
29909 mouse_face = Fget_text_property (position, Qmouse_face, object);
29910
29911 /* Next, compute the bounds of the mouse highlighting and
29912 display it. */
29913 if (!NILP (mouse_face) && STRINGP (object))
29914 {
29915 /* The mouse-highlighting comes from a display string
29916 with a mouse-face. */
29917 Lisp_Object s, e;
29918 ptrdiff_t ignore;
29919
29920 s = Fprevious_single_property_change
29921 (make_number (pos + 1), Qmouse_face, object, Qnil);
29922 e = Fnext_single_property_change
29923 (position, Qmouse_face, object, Qnil);
29924 if (NILP (s))
29925 s = make_number (0);
29926 if (NILP (e))
29927 e = make_number (SCHARS (object));
29928 mouse_face_from_string_pos (w, hlinfo, object,
29929 XINT (s), XINT (e));
29930 hlinfo->mouse_face_past_end = false;
29931 hlinfo->mouse_face_window = window;
29932 hlinfo->mouse_face_face_id
29933 = face_at_string_position (w, object, pos, 0, &ignore,
29934 glyph->face_id, true);
29935 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29936 cursor = No_Cursor;
29937 }
29938 else
29939 {
29940 /* The mouse-highlighting, if any, comes from an overlay
29941 or text property in the buffer. */
29942 Lisp_Object buffer IF_LINT (= Qnil);
29943 Lisp_Object disp_string IF_LINT (= Qnil);
29944
29945 if (STRINGP (object))
29946 {
29947 /* If we are on a display string with no mouse-face,
29948 check if the text under it has one. */
29949 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29950 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29951 pos = string_buffer_position (object, start);
29952 if (pos > 0)
29953 {
29954 mouse_face = get_char_property_and_overlay
29955 (make_number (pos), Qmouse_face, w->contents, &overlay);
29956 buffer = w->contents;
29957 disp_string = object;
29958 }
29959 }
29960 else
29961 {
29962 buffer = object;
29963 disp_string = Qnil;
29964 }
29965
29966 if (!NILP (mouse_face))
29967 {
29968 Lisp_Object before, after;
29969 Lisp_Object before_string, after_string;
29970 /* To correctly find the limits of mouse highlight
29971 in a bidi-reordered buffer, we must not use the
29972 optimization of limiting the search in
29973 previous-single-property-change and
29974 next-single-property-change, because
29975 rows_from_pos_range needs the real start and end
29976 positions to DTRT in this case. That's because
29977 the first row visible in a window does not
29978 necessarily display the character whose position
29979 is the smallest. */
29980 Lisp_Object lim1
29981 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29982 ? Fmarker_position (w->start)
29983 : Qnil;
29984 Lisp_Object lim2
29985 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29986 ? make_number (BUF_Z (XBUFFER (buffer))
29987 - w->window_end_pos)
29988 : Qnil;
29989
29990 if (NILP (overlay))
29991 {
29992 /* Handle the text property case. */
29993 before = Fprevious_single_property_change
29994 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29995 after = Fnext_single_property_change
29996 (make_number (pos), Qmouse_face, buffer, lim2);
29997 before_string = after_string = Qnil;
29998 }
29999 else
30000 {
30001 /* Handle the overlay case. */
30002 before = Foverlay_start (overlay);
30003 after = Foverlay_end (overlay);
30004 before_string = Foverlay_get (overlay, Qbefore_string);
30005 after_string = Foverlay_get (overlay, Qafter_string);
30006
30007 if (!STRINGP (before_string)) before_string = Qnil;
30008 if (!STRINGP (after_string)) after_string = Qnil;
30009 }
30010
30011 mouse_face_from_buffer_pos (window, hlinfo, pos,
30012 NILP (before)
30013 ? 1
30014 : XFASTINT (before),
30015 NILP (after)
30016 ? BUF_Z (XBUFFER (buffer))
30017 : XFASTINT (after),
30018 before_string, after_string,
30019 disp_string);
30020 cursor = No_Cursor;
30021 }
30022 }
30023 }
30024
30025 check_help_echo:
30026
30027 /* Look for a `help-echo' property. */
30028 if (NILP (help_echo_string)) {
30029 Lisp_Object help, overlay;
30030
30031 /* Check overlays first. */
30032 help = overlay = Qnil;
30033 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30034 {
30035 overlay = overlay_vec[i];
30036 help = Foverlay_get (overlay, Qhelp_echo);
30037 }
30038
30039 if (!NILP (help))
30040 {
30041 help_echo_string = help;
30042 help_echo_window = window;
30043 help_echo_object = overlay;
30044 help_echo_pos = pos;
30045 }
30046 else
30047 {
30048 Lisp_Object obj = glyph->object;
30049 ptrdiff_t charpos = glyph->charpos;
30050
30051 /* Try text properties. */
30052 if (STRINGP (obj)
30053 && charpos >= 0
30054 && charpos < SCHARS (obj))
30055 {
30056 help = Fget_text_property (make_number (charpos),
30057 Qhelp_echo, obj);
30058 if (NILP (help))
30059 {
30060 /* If the string itself doesn't specify a help-echo,
30061 see if the buffer text ``under'' it does. */
30062 struct glyph_row *r
30063 = MATRIX_ROW (w->current_matrix, vpos);
30064 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30065 ptrdiff_t p = string_buffer_position (obj, start);
30066 if (p > 0)
30067 {
30068 help = Fget_char_property (make_number (p),
30069 Qhelp_echo, w->contents);
30070 if (!NILP (help))
30071 {
30072 charpos = p;
30073 obj = w->contents;
30074 }
30075 }
30076 }
30077 }
30078 else if (BUFFERP (obj)
30079 && charpos >= BEGV
30080 && charpos < ZV)
30081 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30082 obj);
30083
30084 if (!NILP (help))
30085 {
30086 help_echo_string = help;
30087 help_echo_window = window;
30088 help_echo_object = obj;
30089 help_echo_pos = charpos;
30090 }
30091 }
30092 }
30093
30094 #ifdef HAVE_WINDOW_SYSTEM
30095 /* Look for a `pointer' property. */
30096 if (FRAME_WINDOW_P (f) && NILP (pointer))
30097 {
30098 /* Check overlays first. */
30099 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30100 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30101
30102 if (NILP (pointer))
30103 {
30104 Lisp_Object obj = glyph->object;
30105 ptrdiff_t charpos = glyph->charpos;
30106
30107 /* Try text properties. */
30108 if (STRINGP (obj)
30109 && charpos >= 0
30110 && charpos < SCHARS (obj))
30111 {
30112 pointer = Fget_text_property (make_number (charpos),
30113 Qpointer, obj);
30114 if (NILP (pointer))
30115 {
30116 /* If the string itself doesn't specify a pointer,
30117 see if the buffer text ``under'' it does. */
30118 struct glyph_row *r
30119 = MATRIX_ROW (w->current_matrix, vpos);
30120 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30121 ptrdiff_t p = string_buffer_position (obj, start);
30122 if (p > 0)
30123 pointer = Fget_char_property (make_number (p),
30124 Qpointer, w->contents);
30125 }
30126 }
30127 else if (BUFFERP (obj)
30128 && charpos >= BEGV
30129 && charpos < ZV)
30130 pointer = Fget_text_property (make_number (charpos),
30131 Qpointer, obj);
30132 }
30133 }
30134 #endif /* HAVE_WINDOW_SYSTEM */
30135
30136 BEGV = obegv;
30137 ZV = ozv;
30138 current_buffer = obuf;
30139 SAFE_FREE ();
30140 }
30141
30142 set_cursor:
30143
30144 #ifdef HAVE_WINDOW_SYSTEM
30145 if (FRAME_WINDOW_P (f))
30146 define_frame_cursor1 (f, cursor, pointer);
30147 #else
30148 /* This is here to prevent a compiler error, about "label at end of
30149 compound statement". */
30150 return;
30151 #endif
30152 }
30153
30154
30155 /* EXPORT for RIF:
30156 Clear any mouse-face on window W. This function is part of the
30157 redisplay interface, and is called from try_window_id and similar
30158 functions to ensure the mouse-highlight is off. */
30159
30160 void
30161 x_clear_window_mouse_face (struct window *w)
30162 {
30163 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30164 Lisp_Object window;
30165
30166 block_input ();
30167 XSETWINDOW (window, w);
30168 if (EQ (window, hlinfo->mouse_face_window))
30169 clear_mouse_face (hlinfo);
30170 unblock_input ();
30171 }
30172
30173
30174 /* EXPORT:
30175 Just discard the mouse face information for frame F, if any.
30176 This is used when the size of F is changed. */
30177
30178 void
30179 cancel_mouse_face (struct frame *f)
30180 {
30181 Lisp_Object window;
30182 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30183
30184 window = hlinfo->mouse_face_window;
30185 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30186 reset_mouse_highlight (hlinfo);
30187 }
30188
30189
30190 \f
30191 /***********************************************************************
30192 Exposure Events
30193 ***********************************************************************/
30194
30195 #ifdef HAVE_WINDOW_SYSTEM
30196
30197 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30198 which intersects rectangle R. R is in window-relative coordinates. */
30199
30200 static void
30201 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30202 enum glyph_row_area area)
30203 {
30204 struct glyph *first = row->glyphs[area];
30205 struct glyph *end = row->glyphs[area] + row->used[area];
30206 struct glyph *last;
30207 int first_x, start_x, x;
30208
30209 if (area == TEXT_AREA && row->fill_line_p)
30210 /* If row extends face to end of line write the whole line. */
30211 draw_glyphs (w, 0, row, area,
30212 0, row->used[area],
30213 DRAW_NORMAL_TEXT, 0);
30214 else
30215 {
30216 /* Set START_X to the window-relative start position for drawing glyphs of
30217 AREA. The first glyph of the text area can be partially visible.
30218 The first glyphs of other areas cannot. */
30219 start_x = window_box_left_offset (w, area);
30220 x = start_x;
30221 if (area == TEXT_AREA)
30222 x += row->x;
30223
30224 /* Find the first glyph that must be redrawn. */
30225 while (first < end
30226 && x + first->pixel_width < r->x)
30227 {
30228 x += first->pixel_width;
30229 ++first;
30230 }
30231
30232 /* Find the last one. */
30233 last = first;
30234 first_x = x;
30235 /* Use a signed int intermediate value to avoid catastrophic
30236 failures due to comparison between signed and unsigned, when
30237 x is negative (can happen for wide images that are hscrolled). */
30238 int r_end = r->x + r->width;
30239 while (last < end && x < r_end)
30240 {
30241 x += last->pixel_width;
30242 ++last;
30243 }
30244
30245 /* Repaint. */
30246 if (last > first)
30247 draw_glyphs (w, first_x - start_x, row, area,
30248 first - row->glyphs[area], last - row->glyphs[area],
30249 DRAW_NORMAL_TEXT, 0);
30250 }
30251 }
30252
30253
30254 /* Redraw the parts of the glyph row ROW on window W intersecting
30255 rectangle R. R is in window-relative coordinates. Value is
30256 true if mouse-face was overwritten. */
30257
30258 static bool
30259 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30260 {
30261 eassert (row->enabled_p);
30262
30263 if (row->mode_line_p || w->pseudo_window_p)
30264 draw_glyphs (w, 0, row, TEXT_AREA,
30265 0, row->used[TEXT_AREA],
30266 DRAW_NORMAL_TEXT, 0);
30267 else
30268 {
30269 if (row->used[LEFT_MARGIN_AREA])
30270 expose_area (w, row, r, LEFT_MARGIN_AREA);
30271 if (row->used[TEXT_AREA])
30272 expose_area (w, row, r, TEXT_AREA);
30273 if (row->used[RIGHT_MARGIN_AREA])
30274 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30275 draw_row_fringe_bitmaps (w, row);
30276 }
30277
30278 return row->mouse_face_p;
30279 }
30280
30281
30282 /* Redraw those parts of glyphs rows during expose event handling that
30283 overlap other rows. Redrawing of an exposed line writes over parts
30284 of lines overlapping that exposed line; this function fixes that.
30285
30286 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30287 row in W's current matrix that is exposed and overlaps other rows.
30288 LAST_OVERLAPPING_ROW is the last such row. */
30289
30290 static void
30291 expose_overlaps (struct window *w,
30292 struct glyph_row *first_overlapping_row,
30293 struct glyph_row *last_overlapping_row,
30294 XRectangle *r)
30295 {
30296 struct glyph_row *row;
30297
30298 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30299 if (row->overlapping_p)
30300 {
30301 eassert (row->enabled_p && !row->mode_line_p);
30302
30303 row->clip = r;
30304 if (row->used[LEFT_MARGIN_AREA])
30305 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30306
30307 if (row->used[TEXT_AREA])
30308 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30309
30310 if (row->used[RIGHT_MARGIN_AREA])
30311 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30312 row->clip = NULL;
30313 }
30314 }
30315
30316
30317 /* Return true if W's cursor intersects rectangle R. */
30318
30319 static bool
30320 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30321 {
30322 XRectangle cr, result;
30323 struct glyph *cursor_glyph;
30324 struct glyph_row *row;
30325
30326 if (w->phys_cursor.vpos >= 0
30327 && w->phys_cursor.vpos < w->current_matrix->nrows
30328 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30329 row->enabled_p)
30330 && row->cursor_in_fringe_p)
30331 {
30332 /* Cursor is in the fringe. */
30333 cr.x = window_box_right_offset (w,
30334 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30335 ? RIGHT_MARGIN_AREA
30336 : TEXT_AREA));
30337 cr.y = row->y;
30338 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30339 cr.height = row->height;
30340 return x_intersect_rectangles (&cr, r, &result);
30341 }
30342
30343 cursor_glyph = get_phys_cursor_glyph (w);
30344 if (cursor_glyph)
30345 {
30346 /* r is relative to W's box, but w->phys_cursor.x is relative
30347 to left edge of W's TEXT area. Adjust it. */
30348 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30349 cr.y = w->phys_cursor.y;
30350 cr.width = cursor_glyph->pixel_width;
30351 cr.height = w->phys_cursor_height;
30352 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30353 I assume the effect is the same -- and this is portable. */
30354 return x_intersect_rectangles (&cr, r, &result);
30355 }
30356 /* If we don't understand the format, pretend we're not in the hot-spot. */
30357 return false;
30358 }
30359
30360
30361 /* EXPORT:
30362 Draw a vertical window border to the right of window W if W doesn't
30363 have vertical scroll bars. */
30364
30365 void
30366 x_draw_vertical_border (struct window *w)
30367 {
30368 struct frame *f = XFRAME (WINDOW_FRAME (w));
30369
30370 /* We could do better, if we knew what type of scroll-bar the adjacent
30371 windows (on either side) have... But we don't :-(
30372 However, I think this works ok. ++KFS 2003-04-25 */
30373
30374 /* Redraw borders between horizontally adjacent windows. Don't
30375 do it for frames with vertical scroll bars because either the
30376 right scroll bar of a window, or the left scroll bar of its
30377 neighbor will suffice as a border. */
30378 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30379 return;
30380
30381 /* Note: It is necessary to redraw both the left and the right
30382 borders, for when only this single window W is being
30383 redisplayed. */
30384 if (!WINDOW_RIGHTMOST_P (w)
30385 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30386 {
30387 int x0, x1, y0, y1;
30388
30389 window_box_edges (w, &x0, &y0, &x1, &y1);
30390 y1 -= 1;
30391
30392 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30393 x1 -= 1;
30394
30395 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30396 }
30397
30398 if (!WINDOW_LEFTMOST_P (w)
30399 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30400 {
30401 int x0, x1, y0, y1;
30402
30403 window_box_edges (w, &x0, &y0, &x1, &y1);
30404 y1 -= 1;
30405
30406 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30407 x0 -= 1;
30408
30409 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30410 }
30411 }
30412
30413
30414 /* Draw window dividers for window W. */
30415
30416 void
30417 x_draw_right_divider (struct window *w)
30418 {
30419 struct frame *f = WINDOW_XFRAME (w);
30420
30421 if (w->mini || w->pseudo_window_p)
30422 return;
30423 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30424 {
30425 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30426 int x1 = WINDOW_RIGHT_EDGE_X (w);
30427 int y0 = WINDOW_TOP_EDGE_Y (w);
30428 /* The bottom divider prevails. */
30429 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30430
30431 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30432 }
30433 }
30434
30435 static void
30436 x_draw_bottom_divider (struct window *w)
30437 {
30438 struct frame *f = XFRAME (WINDOW_FRAME (w));
30439
30440 if (w->mini || w->pseudo_window_p)
30441 return;
30442 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30443 {
30444 int x0 = WINDOW_LEFT_EDGE_X (w);
30445 int x1 = WINDOW_RIGHT_EDGE_X (w);
30446 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30447 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30448
30449 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30450 }
30451 }
30452
30453 /* Redraw the part of window W intersection rectangle FR. Pixel
30454 coordinates in FR are frame-relative. Call this function with
30455 input blocked. Value is true if the exposure overwrites
30456 mouse-face. */
30457
30458 static bool
30459 expose_window (struct window *w, XRectangle *fr)
30460 {
30461 struct frame *f = XFRAME (w->frame);
30462 XRectangle wr, r;
30463 bool mouse_face_overwritten_p = false;
30464
30465 /* If window is not yet fully initialized, do nothing. This can
30466 happen when toolkit scroll bars are used and a window is split.
30467 Reconfiguring the scroll bar will generate an expose for a newly
30468 created window. */
30469 if (w->current_matrix == NULL)
30470 return false;
30471
30472 /* When we're currently updating the window, display and current
30473 matrix usually don't agree. Arrange for a thorough display
30474 later. */
30475 if (w->must_be_updated_p)
30476 {
30477 SET_FRAME_GARBAGED (f);
30478 return false;
30479 }
30480
30481 /* Frame-relative pixel rectangle of W. */
30482 wr.x = WINDOW_LEFT_EDGE_X (w);
30483 wr.y = WINDOW_TOP_EDGE_Y (w);
30484 wr.width = WINDOW_PIXEL_WIDTH (w);
30485 wr.height = WINDOW_PIXEL_HEIGHT (w);
30486
30487 if (x_intersect_rectangles (fr, &wr, &r))
30488 {
30489 int yb = window_text_bottom_y (w);
30490 struct glyph_row *row;
30491 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30492
30493 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30494 r.x, r.y, r.width, r.height));
30495
30496 /* Convert to window coordinates. */
30497 r.x -= WINDOW_LEFT_EDGE_X (w);
30498 r.y -= WINDOW_TOP_EDGE_Y (w);
30499
30500 /* Turn off the cursor. */
30501 bool cursor_cleared_p = (!w->pseudo_window_p
30502 && phys_cursor_in_rect_p (w, &r));
30503 if (cursor_cleared_p)
30504 x_clear_cursor (w);
30505
30506 /* If the row containing the cursor extends face to end of line,
30507 then expose_area might overwrite the cursor outside the
30508 rectangle and thus notice_overwritten_cursor might clear
30509 w->phys_cursor_on_p. We remember the original value and
30510 check later if it is changed. */
30511 bool phys_cursor_on_p = w->phys_cursor_on_p;
30512
30513 /* Use a signed int intermediate value to avoid catastrophic
30514 failures due to comparison between signed and unsigned, when
30515 y0 or y1 is negative (can happen for tall images). */
30516 int r_bottom = r.y + r.height;
30517
30518 /* Update lines intersecting rectangle R. */
30519 first_overlapping_row = last_overlapping_row = NULL;
30520 for (row = w->current_matrix->rows;
30521 row->enabled_p;
30522 ++row)
30523 {
30524 int y0 = row->y;
30525 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30526
30527 if ((y0 >= r.y && y0 < r_bottom)
30528 || (y1 > r.y && y1 < r_bottom)
30529 || (r.y >= y0 && r.y < y1)
30530 || (r_bottom > y0 && r_bottom < y1))
30531 {
30532 /* A header line may be overlapping, but there is no need
30533 to fix overlapping areas for them. KFS 2005-02-12 */
30534 if (row->overlapping_p && !row->mode_line_p)
30535 {
30536 if (first_overlapping_row == NULL)
30537 first_overlapping_row = row;
30538 last_overlapping_row = row;
30539 }
30540
30541 row->clip = fr;
30542 if (expose_line (w, row, &r))
30543 mouse_face_overwritten_p = true;
30544 row->clip = NULL;
30545 }
30546 else if (row->overlapping_p)
30547 {
30548 /* We must redraw a row overlapping the exposed area. */
30549 if (y0 < r.y
30550 ? y0 + row->phys_height > r.y
30551 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30552 {
30553 if (first_overlapping_row == NULL)
30554 first_overlapping_row = row;
30555 last_overlapping_row = row;
30556 }
30557 }
30558
30559 if (y1 >= yb)
30560 break;
30561 }
30562
30563 /* Display the mode line if there is one. */
30564 if (WINDOW_WANTS_MODELINE_P (w)
30565 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30566 row->enabled_p)
30567 && row->y < r_bottom)
30568 {
30569 if (expose_line (w, row, &r))
30570 mouse_face_overwritten_p = true;
30571 }
30572
30573 if (!w->pseudo_window_p)
30574 {
30575 /* Fix the display of overlapping rows. */
30576 if (first_overlapping_row)
30577 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30578 fr);
30579
30580 /* Draw border between windows. */
30581 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30582 x_draw_right_divider (w);
30583 else
30584 x_draw_vertical_border (w);
30585
30586 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30587 x_draw_bottom_divider (w);
30588
30589 /* Turn the cursor on again. */
30590 if (cursor_cleared_p
30591 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30592 update_window_cursor (w, true);
30593 }
30594 }
30595
30596 return mouse_face_overwritten_p;
30597 }
30598
30599
30600
30601 /* Redraw (parts) of all windows in the window tree rooted at W that
30602 intersect R. R contains frame pixel coordinates. Value is
30603 true if the exposure overwrites mouse-face. */
30604
30605 static bool
30606 expose_window_tree (struct window *w, XRectangle *r)
30607 {
30608 struct frame *f = XFRAME (w->frame);
30609 bool mouse_face_overwritten_p = false;
30610
30611 while (w && !FRAME_GARBAGED_P (f))
30612 {
30613 mouse_face_overwritten_p
30614 |= (WINDOWP (w->contents)
30615 ? expose_window_tree (XWINDOW (w->contents), r)
30616 : expose_window (w, r));
30617
30618 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30619 }
30620
30621 return mouse_face_overwritten_p;
30622 }
30623
30624
30625 /* EXPORT:
30626 Redisplay an exposed area of frame F. X and Y are the upper-left
30627 corner of the exposed rectangle. W and H are width and height of
30628 the exposed area. All are pixel values. W or H zero means redraw
30629 the entire frame. */
30630
30631 void
30632 expose_frame (struct frame *f, int x, int y, int w, int h)
30633 {
30634 XRectangle r;
30635 bool mouse_face_overwritten_p = false;
30636
30637 TRACE ((stderr, "expose_frame "));
30638
30639 /* No need to redraw if frame will be redrawn soon. */
30640 if (FRAME_GARBAGED_P (f))
30641 {
30642 TRACE ((stderr, " garbaged\n"));
30643 return;
30644 }
30645
30646 /* If basic faces haven't been realized yet, there is no point in
30647 trying to redraw anything. This can happen when we get an expose
30648 event while Emacs is starting, e.g. by moving another window. */
30649 if (FRAME_FACE_CACHE (f) == NULL
30650 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30651 {
30652 TRACE ((stderr, " no faces\n"));
30653 return;
30654 }
30655
30656 if (w == 0 || h == 0)
30657 {
30658 r.x = r.y = 0;
30659 r.width = FRAME_TEXT_WIDTH (f);
30660 r.height = FRAME_TEXT_HEIGHT (f);
30661 }
30662 else
30663 {
30664 r.x = x;
30665 r.y = y;
30666 r.width = w;
30667 r.height = h;
30668 }
30669
30670 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30671 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30672
30673 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30674 if (WINDOWP (f->tool_bar_window))
30675 mouse_face_overwritten_p
30676 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30677 #endif
30678
30679 #ifdef HAVE_X_WINDOWS
30680 #ifndef MSDOS
30681 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30682 if (WINDOWP (f->menu_bar_window))
30683 mouse_face_overwritten_p
30684 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30685 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30686 #endif
30687 #endif
30688
30689 /* Some window managers support a focus-follows-mouse style with
30690 delayed raising of frames. Imagine a partially obscured frame,
30691 and moving the mouse into partially obscured mouse-face on that
30692 frame. The visible part of the mouse-face will be highlighted,
30693 then the WM raises the obscured frame. With at least one WM, KDE
30694 2.1, Emacs is not getting any event for the raising of the frame
30695 (even tried with SubstructureRedirectMask), only Expose events.
30696 These expose events will draw text normally, i.e. not
30697 highlighted. Which means we must redo the highlight here.
30698 Subsume it under ``we love X''. --gerd 2001-08-15 */
30699 /* Included in Windows version because Windows most likely does not
30700 do the right thing if any third party tool offers
30701 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30702 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30703 {
30704 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30705 if (f == hlinfo->mouse_face_mouse_frame)
30706 {
30707 int mouse_x = hlinfo->mouse_face_mouse_x;
30708 int mouse_y = hlinfo->mouse_face_mouse_y;
30709 clear_mouse_face (hlinfo);
30710 note_mouse_highlight (f, mouse_x, mouse_y);
30711 }
30712 }
30713 }
30714
30715
30716 /* EXPORT:
30717 Determine the intersection of two rectangles R1 and R2. Return
30718 the intersection in *RESULT. Value is true if RESULT is not
30719 empty. */
30720
30721 bool
30722 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30723 {
30724 XRectangle *left, *right;
30725 XRectangle *upper, *lower;
30726 bool intersection_p = false;
30727
30728 /* Rearrange so that R1 is the left-most rectangle. */
30729 if (r1->x < r2->x)
30730 left = r1, right = r2;
30731 else
30732 left = r2, right = r1;
30733
30734 /* X0 of the intersection is right.x0, if this is inside R1,
30735 otherwise there is no intersection. */
30736 if (right->x <= left->x + left->width)
30737 {
30738 result->x = right->x;
30739
30740 /* The right end of the intersection is the minimum of
30741 the right ends of left and right. */
30742 result->width = (min (left->x + left->width, right->x + right->width)
30743 - result->x);
30744
30745 /* Same game for Y. */
30746 if (r1->y < r2->y)
30747 upper = r1, lower = r2;
30748 else
30749 upper = r2, lower = r1;
30750
30751 /* The upper end of the intersection is lower.y0, if this is inside
30752 of upper. Otherwise, there is no intersection. */
30753 if (lower->y <= upper->y + upper->height)
30754 {
30755 result->y = lower->y;
30756
30757 /* The lower end of the intersection is the minimum of the lower
30758 ends of upper and lower. */
30759 result->height = (min (lower->y + lower->height,
30760 upper->y + upper->height)
30761 - result->y);
30762 intersection_p = true;
30763 }
30764 }
30765
30766 return intersection_p;
30767 }
30768
30769 #endif /* HAVE_WINDOW_SYSTEM */
30770
30771 \f
30772 /***********************************************************************
30773 Initialization
30774 ***********************************************************************/
30775
30776 void
30777 syms_of_xdisp (void)
30778 {
30779 Vwith_echo_area_save_vector = Qnil;
30780 staticpro (&Vwith_echo_area_save_vector);
30781
30782 Vmessage_stack = Qnil;
30783 staticpro (&Vmessage_stack);
30784
30785 /* Non-nil means don't actually do any redisplay. */
30786 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30787
30788 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30789
30790 DEFVAR_BOOL("inhibit-message", inhibit_message,
30791 doc: /* Non-nil means calls to `message' are not displayed.
30792 They are still logged to the *Messages* buffer. */);
30793 inhibit_message = 0;
30794
30795 message_dolog_marker1 = Fmake_marker ();
30796 staticpro (&message_dolog_marker1);
30797 message_dolog_marker2 = Fmake_marker ();
30798 staticpro (&message_dolog_marker2);
30799 message_dolog_marker3 = Fmake_marker ();
30800 staticpro (&message_dolog_marker3);
30801
30802 #ifdef GLYPH_DEBUG
30803 defsubr (&Sdump_frame_glyph_matrix);
30804 defsubr (&Sdump_glyph_matrix);
30805 defsubr (&Sdump_glyph_row);
30806 defsubr (&Sdump_tool_bar_row);
30807 defsubr (&Strace_redisplay);
30808 defsubr (&Strace_to_stderr);
30809 #endif
30810 #ifdef HAVE_WINDOW_SYSTEM
30811 defsubr (&Stool_bar_height);
30812 defsubr (&Slookup_image_map);
30813 #endif
30814 defsubr (&Sline_pixel_height);
30815 defsubr (&Sformat_mode_line);
30816 defsubr (&Sinvisible_p);
30817 defsubr (&Scurrent_bidi_paragraph_direction);
30818 defsubr (&Swindow_text_pixel_size);
30819 defsubr (&Smove_point_visually);
30820 defsubr (&Sbidi_find_overridden_directionality);
30821
30822 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30823 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30824 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30825 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30826 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30827 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30828 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30829 DEFSYM (Qeval, "eval");
30830 DEFSYM (QCdata, ":data");
30831
30832 /* Names of text properties relevant for redisplay. */
30833 DEFSYM (Qdisplay, "display");
30834 DEFSYM (Qspace_width, "space-width");
30835 DEFSYM (Qraise, "raise");
30836 DEFSYM (Qslice, "slice");
30837 DEFSYM (Qspace, "space");
30838 DEFSYM (Qmargin, "margin");
30839 DEFSYM (Qpointer, "pointer");
30840 DEFSYM (Qleft_margin, "left-margin");
30841 DEFSYM (Qright_margin, "right-margin");
30842 DEFSYM (Qcenter, "center");
30843 DEFSYM (Qline_height, "line-height");
30844 DEFSYM (QCalign_to, ":align-to");
30845 DEFSYM (QCrelative_width, ":relative-width");
30846 DEFSYM (QCrelative_height, ":relative-height");
30847 DEFSYM (QCeval, ":eval");
30848 DEFSYM (QCpropertize, ":propertize");
30849 DEFSYM (QCfile, ":file");
30850 DEFSYM (Qfontified, "fontified");
30851 DEFSYM (Qfontification_functions, "fontification-functions");
30852
30853 /* Name of the face used to highlight trailing whitespace. */
30854 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30855
30856 /* Name and number of the face used to highlight escape glyphs. */
30857 DEFSYM (Qescape_glyph, "escape-glyph");
30858
30859 /* Name and number of the face used to highlight non-breaking spaces. */
30860 DEFSYM (Qnobreak_space, "nobreak-space");
30861
30862 /* The symbol 'image' which is the car of the lists used to represent
30863 images in Lisp. Also a tool bar style. */
30864 DEFSYM (Qimage, "image");
30865
30866 /* Tool bar styles. */
30867 DEFSYM (Qtext, "text");
30868 DEFSYM (Qboth, "both");
30869 DEFSYM (Qboth_horiz, "both-horiz");
30870 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30871
30872 /* The image map types. */
30873 DEFSYM (QCmap, ":map");
30874 DEFSYM (QCpointer, ":pointer");
30875 DEFSYM (Qrect, "rect");
30876 DEFSYM (Qcircle, "circle");
30877 DEFSYM (Qpoly, "poly");
30878
30879 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30880
30881 DEFSYM (Qgrow_only, "grow-only");
30882 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30883 DEFSYM (Qposition, "position");
30884 DEFSYM (Qbuffer_position, "buffer-position");
30885 DEFSYM (Qobject, "object");
30886
30887 /* Cursor shapes. */
30888 DEFSYM (Qbar, "bar");
30889 DEFSYM (Qhbar, "hbar");
30890 DEFSYM (Qbox, "box");
30891 DEFSYM (Qhollow, "hollow");
30892
30893 /* Pointer shapes. */
30894 DEFSYM (Qhand, "hand");
30895 DEFSYM (Qarrow, "arrow");
30896 /* also Qtext */
30897
30898 DEFSYM (Qdragging, "dragging");
30899
30900 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
30901
30902 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
30903 staticpro (&list_of_error);
30904
30905 /* Values of those variables at last redisplay are stored as
30906 properties on 'overlay-arrow-position' symbol. However, if
30907 Voverlay_arrow_position is a marker, last-arrow-position is its
30908 numerical position. */
30909 DEFSYM (Qlast_arrow_position, "last-arrow-position");
30910 DEFSYM (Qlast_arrow_string, "last-arrow-string");
30911
30912 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
30913 properties on a symbol in overlay-arrow-variable-list. */
30914 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
30915 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
30916
30917 echo_buffer[0] = echo_buffer[1] = Qnil;
30918 staticpro (&echo_buffer[0]);
30919 staticpro (&echo_buffer[1]);
30920
30921 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
30922 staticpro (&echo_area_buffer[0]);
30923 staticpro (&echo_area_buffer[1]);
30924
30925 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
30926 staticpro (&Vmessages_buffer_name);
30927
30928 mode_line_proptrans_alist = Qnil;
30929 staticpro (&mode_line_proptrans_alist);
30930 mode_line_string_list = Qnil;
30931 staticpro (&mode_line_string_list);
30932 mode_line_string_face = Qnil;
30933 staticpro (&mode_line_string_face);
30934 mode_line_string_face_prop = Qnil;
30935 staticpro (&mode_line_string_face_prop);
30936 Vmode_line_unwind_vector = Qnil;
30937 staticpro (&Vmode_line_unwind_vector);
30938
30939 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30940
30941 help_echo_string = Qnil;
30942 staticpro (&help_echo_string);
30943 help_echo_object = Qnil;
30944 staticpro (&help_echo_object);
30945 help_echo_window = Qnil;
30946 staticpro (&help_echo_window);
30947 previous_help_echo_string = Qnil;
30948 staticpro (&previous_help_echo_string);
30949 help_echo_pos = -1;
30950
30951 DEFSYM (Qright_to_left, "right-to-left");
30952 DEFSYM (Qleft_to_right, "left-to-right");
30953 defsubr (&Sbidi_resolved_levels);
30954
30955 #ifdef HAVE_WINDOW_SYSTEM
30956 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30957 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30958 For example, if a block cursor is over a tab, it will be drawn as
30959 wide as that tab on the display. */);
30960 x_stretch_cursor_p = 0;
30961 #endif
30962
30963 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30964 doc: /* Non-nil means highlight trailing whitespace.
30965 The face used for trailing whitespace is `trailing-whitespace'. */);
30966 Vshow_trailing_whitespace = Qnil;
30967
30968 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30969 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30970 If the value is t, Emacs highlights non-ASCII chars which have the
30971 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30972 or `escape-glyph' face respectively.
30973
30974 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30975 U+2011 (non-breaking hyphen) are affected.
30976
30977 Any other non-nil value means to display these characters as a escape
30978 glyph followed by an ordinary space or hyphen.
30979
30980 A value of nil means no special handling of these characters. */);
30981 Vnobreak_char_display = Qt;
30982
30983 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30984 doc: /* The pointer shape to show in void text areas.
30985 A value of nil means to show the text pointer. Other options are
30986 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
30987 `hourglass'. */);
30988 Vvoid_text_area_pointer = Qarrow;
30989
30990 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30991 doc: /* Non-nil means don't actually do any redisplay.
30992 This is used for internal purposes. */);
30993 Vinhibit_redisplay = Qnil;
30994
30995 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30996 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30997 Vglobal_mode_string = Qnil;
30998
30999 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
31000 doc: /* Marker for where to display an arrow on top of the buffer text.
31001 This must be the beginning of a line in order to work.
31002 See also `overlay-arrow-string'. */);
31003 Voverlay_arrow_position = Qnil;
31004
31005 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
31006 doc: /* String to display as an arrow in non-window frames.
31007 See also `overlay-arrow-position'. */);
31008 Voverlay_arrow_string = build_pure_c_string ("=>");
31009
31010 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31011 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31012 The symbols on this list are examined during redisplay to determine
31013 where to display overlay arrows. */);
31014 Voverlay_arrow_variable_list
31015 = list1 (intern_c_string ("overlay-arrow-position"));
31016
31017 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31018 doc: /* The number of lines to try scrolling a window by when point moves out.
31019 If that fails to bring point back on frame, point is centered instead.
31020 If this is zero, point is always centered after it moves off frame.
31021 If you want scrolling to always be a line at a time, you should set
31022 `scroll-conservatively' to a large value rather than set this to 1. */);
31023
31024 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31025 doc: /* Scroll up to this many lines, to bring point back on screen.
31026 If point moves off-screen, redisplay will scroll by up to
31027 `scroll-conservatively' lines in order to bring point just barely
31028 onto the screen again. If that cannot be done, then redisplay
31029 recenters point as usual.
31030
31031 If the value is greater than 100, redisplay will never recenter point,
31032 but will always scroll just enough text to bring point into view, even
31033 if you move far away.
31034
31035 A value of zero means always recenter point if it moves off screen. */);
31036 scroll_conservatively = 0;
31037
31038 DEFVAR_INT ("scroll-margin", scroll_margin,
31039 doc: /* Number of lines of margin at the top and bottom of a window.
31040 Recenter the window whenever point gets within this many lines
31041 of the top or bottom of the window. */);
31042 scroll_margin = 0;
31043
31044 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31045 doc: /* Pixels per inch value for non-window system displays.
31046 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31047 Vdisplay_pixels_per_inch = make_float (72.0);
31048
31049 #ifdef GLYPH_DEBUG
31050 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31051 #endif
31052
31053 DEFVAR_LISP ("truncate-partial-width-windows",
31054 Vtruncate_partial_width_windows,
31055 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31056 For an integer value, truncate lines in each window narrower than the
31057 full frame width, provided the window width is less than that integer;
31058 otherwise, respect the value of `truncate-lines'.
31059
31060 For any other non-nil value, truncate lines in all windows that do
31061 not span the full frame width.
31062
31063 A value of nil means to respect the value of `truncate-lines'.
31064
31065 If `word-wrap' is enabled, you might want to reduce this. */);
31066 Vtruncate_partial_width_windows = make_number (50);
31067
31068 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31069 doc: /* Maximum buffer size for which line number should be displayed.
31070 If the buffer is bigger than this, the line number does not appear
31071 in the mode line. A value of nil means no limit. */);
31072 Vline_number_display_limit = Qnil;
31073
31074 DEFVAR_INT ("line-number-display-limit-width",
31075 line_number_display_limit_width,
31076 doc: /* Maximum line width (in characters) for line number display.
31077 If the average length of the lines near point is bigger than this, then the
31078 line number may be omitted from the mode line. */);
31079 line_number_display_limit_width = 200;
31080
31081 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31082 doc: /* Non-nil means highlight region even in nonselected windows. */);
31083 highlight_nonselected_windows = false;
31084
31085 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31086 doc: /* Non-nil if more than one frame is visible on this display.
31087 Minibuffer-only frames don't count, but iconified frames do.
31088 This variable is not guaranteed to be accurate except while processing
31089 `frame-title-format' and `icon-title-format'. */);
31090
31091 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31092 doc: /* Template for displaying the title bar of visible frames.
31093 (Assuming the window manager supports this feature.)
31094
31095 This variable has the same structure as `mode-line-format', except that
31096 the %c and %l constructs are ignored. It is used only on frames for
31097 which no explicit name has been set (see `modify-frame-parameters'). */);
31098
31099 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31100 doc: /* Template for displaying the title bar of an iconified frame.
31101 (Assuming the window manager supports this feature.)
31102 This variable has the same structure as `mode-line-format' (which see),
31103 and is used only on frames for which no explicit name has been set
31104 (see `modify-frame-parameters'). */);
31105 Vicon_title_format
31106 = Vframe_title_format
31107 = listn (CONSTYPE_PURE, 3,
31108 intern_c_string ("multiple-frames"),
31109 build_pure_c_string ("%b"),
31110 listn (CONSTYPE_PURE, 4,
31111 empty_unibyte_string,
31112 intern_c_string ("invocation-name"),
31113 build_pure_c_string ("@"),
31114 intern_c_string ("system-name")));
31115
31116 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31117 doc: /* Maximum number of lines to keep in the message log buffer.
31118 If nil, disable message logging. If t, log messages but don't truncate
31119 the buffer when it becomes large. */);
31120 Vmessage_log_max = make_number (1000);
31121
31122 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
31123 doc: /* Functions called before redisplay, if window sizes have changed.
31124 The value should be a list of functions that take one argument.
31125 Just before redisplay, for each frame, if any of its windows have changed
31126 size since the last redisplay, or have been split or deleted,
31127 all the functions in the list are called, with the frame as argument. */);
31128 Vwindow_size_change_functions = Qnil;
31129
31130 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31131 doc: /* List of functions to call before redisplaying a window with scrolling.
31132 Each function is called with two arguments, the window and its new
31133 display-start position.
31134 These functions are called whenever the `window-start' marker is modified,
31135 either to point into another buffer (e.g. via `set-window-buffer') or another
31136 place in the same buffer.
31137 Note that the value of `window-end' is not valid when these functions are
31138 called.
31139
31140 Warning: Do not use this feature to alter the way the window
31141 is scrolled. It is not designed for that, and such use probably won't
31142 work. */);
31143 Vwindow_scroll_functions = Qnil;
31144
31145 DEFVAR_LISP ("window-text-change-functions",
31146 Vwindow_text_change_functions,
31147 doc: /* Functions to call in redisplay when text in the window might change. */);
31148 Vwindow_text_change_functions = Qnil;
31149
31150 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31151 doc: /* Functions called when redisplay of a window reaches the end trigger.
31152 Each function is called with two arguments, the window and the end trigger value.
31153 See `set-window-redisplay-end-trigger'. */);
31154 Vredisplay_end_trigger_functions = Qnil;
31155
31156 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31157 doc: /* Non-nil means autoselect window with mouse pointer.
31158 If nil, do not autoselect windows.
31159 A positive number means delay autoselection by that many seconds: a
31160 window is autoselected only after the mouse has remained in that
31161 window for the duration of the delay.
31162 A negative number has a similar effect, but causes windows to be
31163 autoselected only after the mouse has stopped moving. (Because of
31164 the way Emacs compares mouse events, you will occasionally wait twice
31165 that time before the window gets selected.)
31166 Any other value means to autoselect window instantaneously when the
31167 mouse pointer enters it.
31168
31169 Autoselection selects the minibuffer only if it is active, and never
31170 unselects the minibuffer if it is active.
31171
31172 When customizing this variable make sure that the actual value of
31173 `focus-follows-mouse' matches the behavior of your window manager. */);
31174 Vmouse_autoselect_window = Qnil;
31175
31176 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31177 doc: /* Non-nil means automatically resize tool-bars.
31178 This dynamically changes the tool-bar's height to the minimum height
31179 that is needed to make all tool-bar items visible.
31180 If value is `grow-only', the tool-bar's height is only increased
31181 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31182 Vauto_resize_tool_bars = Qt;
31183
31184 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31185 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31186 auto_raise_tool_bar_buttons_p = true;
31187
31188 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31189 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31190 make_cursor_line_fully_visible_p = true;
31191
31192 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31193 doc: /* Border below tool-bar in pixels.
31194 If an integer, use it as the height of the border.
31195 If it is one of `internal-border-width' or `border-width', use the
31196 value of the corresponding frame parameter.
31197 Otherwise, no border is added below the tool-bar. */);
31198 Vtool_bar_border = Qinternal_border_width;
31199
31200 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31201 doc: /* Margin around tool-bar buttons in pixels.
31202 If an integer, use that for both horizontal and vertical margins.
31203 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31204 HORZ specifying the horizontal margin, and VERT specifying the
31205 vertical margin. */);
31206 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31207
31208 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31209 doc: /* Relief thickness of tool-bar buttons. */);
31210 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31211
31212 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31213 doc: /* Tool bar style to use.
31214 It can be one of
31215 image - show images only
31216 text - show text only
31217 both - show both, text below image
31218 both-horiz - show text to the right of the image
31219 text-image-horiz - show text to the left of the image
31220 any other - use system default or image if no system default.
31221
31222 This variable only affects the GTK+ toolkit version of Emacs. */);
31223 Vtool_bar_style = Qnil;
31224
31225 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31226 doc: /* Maximum number of characters a label can have to be shown.
31227 The tool bar style must also show labels for this to have any effect, see
31228 `tool-bar-style'. */);
31229 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31230
31231 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31232 doc: /* List of functions to call to fontify regions of text.
31233 Each function is called with one argument POS. Functions must
31234 fontify a region starting at POS in the current buffer, and give
31235 fontified regions the property `fontified'. */);
31236 Vfontification_functions = Qnil;
31237 Fmake_variable_buffer_local (Qfontification_functions);
31238
31239 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31240 unibyte_display_via_language_environment,
31241 doc: /* Non-nil means display unibyte text according to language environment.
31242 Specifically, this means that raw bytes in the range 160-255 decimal
31243 are displayed by converting them to the equivalent multibyte characters
31244 according to the current language environment. As a result, they are
31245 displayed according to the current fontset.
31246
31247 Note that this variable affects only how these bytes are displayed,
31248 but does not change the fact they are interpreted as raw bytes. */);
31249 unibyte_display_via_language_environment = false;
31250
31251 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31252 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31253 If a float, it specifies a fraction of the mini-window frame's height.
31254 If an integer, it specifies a number of lines. */);
31255 Vmax_mini_window_height = make_float (0.25);
31256
31257 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31258 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31259 A value of nil means don't automatically resize mini-windows.
31260 A value of t means resize them to fit the text displayed in them.
31261 A value of `grow-only', the default, means let mini-windows grow only;
31262 they return to their normal size when the minibuffer is closed, or the
31263 echo area becomes empty. */);
31264 Vresize_mini_windows = Qgrow_only;
31265
31266 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31267 doc: /* Alist specifying how to blink the cursor off.
31268 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31269 `cursor-type' frame-parameter or variable equals ON-STATE,
31270 comparing using `equal', Emacs uses OFF-STATE to specify
31271 how to blink it off. ON-STATE and OFF-STATE are values for
31272 the `cursor-type' frame parameter.
31273
31274 If a frame's ON-STATE has no entry in this list,
31275 the frame's other specifications determine how to blink the cursor off. */);
31276 Vblink_cursor_alist = Qnil;
31277
31278 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31279 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31280 If non-nil, windows are automatically scrolled horizontally to make
31281 point visible. */);
31282 automatic_hscrolling_p = true;
31283 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31284
31285 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31286 doc: /* How many columns away from the window edge point is allowed to get
31287 before automatic hscrolling will horizontally scroll the window. */);
31288 hscroll_margin = 5;
31289
31290 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31291 doc: /* How many columns to scroll the window when point gets too close to the edge.
31292 When point is less than `hscroll-margin' columns from the window
31293 edge, automatic hscrolling will scroll the window by the amount of columns
31294 determined by this variable. If its value is a positive integer, scroll that
31295 many columns. If it's a positive floating-point number, it specifies the
31296 fraction of the window's width to scroll. If it's nil or zero, point will be
31297 centered horizontally after the scroll. Any other value, including negative
31298 numbers, are treated as if the value were zero.
31299
31300 Automatic hscrolling always moves point outside the scroll margin, so if
31301 point was more than scroll step columns inside the margin, the window will
31302 scroll more than the value given by the scroll step.
31303
31304 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31305 and `scroll-right' overrides this variable's effect. */);
31306 Vhscroll_step = make_number (0);
31307
31308 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31309 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31310 Bind this around calls to `message' to let it take effect. */);
31311 message_truncate_lines = false;
31312
31313 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31314 doc: /* Normal hook run to update the menu bar definitions.
31315 Redisplay runs this hook before it redisplays the menu bar.
31316 This is used to update menus such as Buffers, whose contents depend on
31317 various data. */);
31318 Vmenu_bar_update_hook = Qnil;
31319
31320 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31321 doc: /* Frame for which we are updating a menu.
31322 The enable predicate for a menu binding should check this variable. */);
31323 Vmenu_updating_frame = Qnil;
31324
31325 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31326 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31327 inhibit_menubar_update = false;
31328
31329 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31330 doc: /* Prefix prepended to all continuation lines at display time.
31331 The value may be a string, an image, or a stretch-glyph; it is
31332 interpreted in the same way as the value of a `display' text property.
31333
31334 This variable is overridden by any `wrap-prefix' text or overlay
31335 property.
31336
31337 To add a prefix to non-continuation lines, use `line-prefix'. */);
31338 Vwrap_prefix = Qnil;
31339 DEFSYM (Qwrap_prefix, "wrap-prefix");
31340 Fmake_variable_buffer_local (Qwrap_prefix);
31341
31342 DEFVAR_LISP ("line-prefix", Vline_prefix,
31343 doc: /* Prefix prepended to all non-continuation lines at display time.
31344 The value may be a string, an image, or a stretch-glyph; it is
31345 interpreted in the same way as the value of a `display' text property.
31346
31347 This variable is overridden by any `line-prefix' text or overlay
31348 property.
31349
31350 To add a prefix to continuation lines, use `wrap-prefix'. */);
31351 Vline_prefix = Qnil;
31352 DEFSYM (Qline_prefix, "line-prefix");
31353 Fmake_variable_buffer_local (Qline_prefix);
31354
31355 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31356 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31357 inhibit_eval_during_redisplay = false;
31358
31359 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31360 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31361 inhibit_free_realized_faces = false;
31362
31363 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31364 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31365 Intended for use during debugging and for testing bidi display;
31366 see biditest.el in the test suite. */);
31367 inhibit_bidi_mirroring = false;
31368
31369 #ifdef GLYPH_DEBUG
31370 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31371 doc: /* Inhibit try_window_id display optimization. */);
31372 inhibit_try_window_id = false;
31373
31374 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31375 doc: /* Inhibit try_window_reusing display optimization. */);
31376 inhibit_try_window_reusing = false;
31377
31378 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31379 doc: /* Inhibit try_cursor_movement display optimization. */);
31380 inhibit_try_cursor_movement = false;
31381 #endif /* GLYPH_DEBUG */
31382
31383 DEFVAR_INT ("overline-margin", overline_margin,
31384 doc: /* Space between overline and text, in pixels.
31385 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31386 margin to the character height. */);
31387 overline_margin = 2;
31388
31389 DEFVAR_INT ("underline-minimum-offset",
31390 underline_minimum_offset,
31391 doc: /* Minimum distance between baseline and underline.
31392 This can improve legibility of underlined text at small font sizes,
31393 particularly when using variable `x-use-underline-position-properties'
31394 with fonts that specify an UNDERLINE_POSITION relatively close to the
31395 baseline. The default value is 1. */);
31396 underline_minimum_offset = 1;
31397
31398 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31399 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31400 This feature only works when on a window system that can change
31401 cursor shapes. */);
31402 display_hourglass_p = true;
31403
31404 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31405 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31406 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31407
31408 #ifdef HAVE_WINDOW_SYSTEM
31409 hourglass_atimer = NULL;
31410 hourglass_shown_p = false;
31411 #endif /* HAVE_WINDOW_SYSTEM */
31412
31413 /* Name of the face used to display glyphless characters. */
31414 DEFSYM (Qglyphless_char, "glyphless-char");
31415
31416 /* Method symbols for Vglyphless_char_display. */
31417 DEFSYM (Qhex_code, "hex-code");
31418 DEFSYM (Qempty_box, "empty-box");
31419 DEFSYM (Qthin_space, "thin-space");
31420 DEFSYM (Qzero_width, "zero-width");
31421
31422 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31423 doc: /* Function run just before redisplay.
31424 It is called with one argument, which is the set of windows that are to
31425 be redisplayed. This set can be nil (meaning, only the selected window),
31426 or t (meaning all windows). */);
31427 Vpre_redisplay_function = intern ("ignore");
31428
31429 /* Symbol for the purpose of Vglyphless_char_display. */
31430 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31431 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31432
31433 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31434 doc: /* Char-table defining glyphless characters.
31435 Each element, if non-nil, should be one of the following:
31436 an ASCII acronym string: display this string in a box
31437 `hex-code': display the hexadecimal code of a character in a box
31438 `empty-box': display as an empty box
31439 `thin-space': display as 1-pixel width space
31440 `zero-width': don't display
31441 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31442 display method for graphical terminals and text terminals respectively.
31443 GRAPHICAL and TEXT should each have one of the values listed above.
31444
31445 The char-table has one extra slot to control the display of a character for
31446 which no font is found. This slot only takes effect on graphical terminals.
31447 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31448 `thin-space'. The default is `empty-box'.
31449
31450 If a character has a non-nil entry in an active display table, the
31451 display table takes effect; in this case, Emacs does not consult
31452 `glyphless-char-display' at all. */);
31453 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31454 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31455 Qempty_box);
31456
31457 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31458 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31459 Vdebug_on_message = Qnil;
31460
31461 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31462 doc: /* */);
31463 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
31464
31465 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31466 doc: /* */);
31467 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
31468 }
31469
31470
31471 /* Initialize this module when Emacs starts. */
31472
31473 void
31474 init_xdisp (void)
31475 {
31476 CHARPOS (this_line_start_pos) = 0;
31477
31478 if (!noninteractive)
31479 {
31480 struct window *m = XWINDOW (minibuf_window);
31481 Lisp_Object frame = m->frame;
31482 struct frame *f = XFRAME (frame);
31483 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31484 struct window *r = XWINDOW (root);
31485 int i;
31486
31487 echo_area_window = minibuf_window;
31488
31489 r->top_line = FRAME_TOP_MARGIN (f);
31490 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31491 r->total_cols = FRAME_COLS (f);
31492 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31493 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31494 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31495
31496 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31497 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31498 m->total_cols = FRAME_COLS (f);
31499 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31500 m->total_lines = 1;
31501 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31502
31503 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31504 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31505 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31506
31507 /* The default ellipsis glyphs `...'. */
31508 for (i = 0; i < 3; ++i)
31509 default_invis_vector[i] = make_number ('.');
31510 }
31511
31512 {
31513 /* Allocate the buffer for frame titles.
31514 Also used for `format-mode-line'. */
31515 int size = 100;
31516 mode_line_noprop_buf = xmalloc (size);
31517 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31518 mode_line_noprop_ptr = mode_line_noprop_buf;
31519 mode_line_target = MODE_LINE_DISPLAY;
31520 }
31521
31522 help_echo_showing_p = false;
31523 }
31524
31525 #ifdef HAVE_WINDOW_SYSTEM
31526
31527 /* Platform-independent portion of hourglass implementation. */
31528
31529 /* Timer function of hourglass_atimer. */
31530
31531 static void
31532 show_hourglass (struct atimer *timer)
31533 {
31534 /* The timer implementation will cancel this timer automatically
31535 after this function has run. Set hourglass_atimer to null
31536 so that we know the timer doesn't have to be canceled. */
31537 hourglass_atimer = NULL;
31538
31539 if (!hourglass_shown_p)
31540 {
31541 Lisp_Object tail, frame;
31542
31543 block_input ();
31544
31545 FOR_EACH_FRAME (tail, frame)
31546 {
31547 struct frame *f = XFRAME (frame);
31548
31549 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31550 && FRAME_RIF (f)->show_hourglass)
31551 FRAME_RIF (f)->show_hourglass (f);
31552 }
31553
31554 hourglass_shown_p = true;
31555 unblock_input ();
31556 }
31557 }
31558
31559 /* Cancel a currently active hourglass timer, and start a new one. */
31560
31561 void
31562 start_hourglass (void)
31563 {
31564 struct timespec delay;
31565
31566 cancel_hourglass ();
31567
31568 if (INTEGERP (Vhourglass_delay)
31569 && XINT (Vhourglass_delay) > 0)
31570 delay = make_timespec (min (XINT (Vhourglass_delay),
31571 TYPE_MAXIMUM (time_t)),
31572 0);
31573 else if (FLOATP (Vhourglass_delay)
31574 && XFLOAT_DATA (Vhourglass_delay) > 0)
31575 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31576 else
31577 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31578
31579 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31580 show_hourglass, NULL);
31581 }
31582
31583 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31584 shown. */
31585
31586 void
31587 cancel_hourglass (void)
31588 {
31589 if (hourglass_atimer)
31590 {
31591 cancel_atimer (hourglass_atimer);
31592 hourglass_atimer = NULL;
31593 }
31594
31595 if (hourglass_shown_p)
31596 {
31597 Lisp_Object tail, frame;
31598
31599 block_input ();
31600
31601 FOR_EACH_FRAME (tail, frame)
31602 {
31603 struct frame *f = XFRAME (frame);
31604
31605 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31606 && FRAME_RIF (f)->hide_hourglass)
31607 FRAME_RIF (f)->hide_hourglass (f);
31608 #ifdef HAVE_NTGUI
31609 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31610 else if (!FRAME_W32_P (f))
31611 w32_arrow_cursor ();
31612 #endif
31613 }
31614
31615 hourglass_shown_p = false;
31616 unblock_input ();
31617 }
31618 }
31619
31620 #endif /* HAVE_WINDOW_SYSTEM */