]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Fix redisplay with window-start on continuation lines
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2016 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or (at
11 your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
104
105 . try_window
106
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
112
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
117
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
130
131 Desired matrices.
132
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
139
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
146
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
156
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
163
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
175
176 Frame matrices.
177
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
184
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
196
197 Bidirectional display.
198
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
211
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
220
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
239
240 Bidirectional display and character compositions
241
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
246
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
266
267 Moving an iterator in bidirectional text
268 without producing glyphs
269
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
288
289 #include <config.h>
290 #include <stdio.h>
291 #include <limits.h>
292
293 #include "lisp.h"
294 #include "atimer.h"
295 #include "composite.h"
296 #include "keyboard.h"
297 #include "systime.h"
298 #include "frame.h"
299 #include "window.h"
300 #include "termchar.h"
301 #include "dispextern.h"
302 #include "character.h"
303 #include "buffer.h"
304 #include "charset.h"
305 #include "indent.h"
306 #include "commands.h"
307 #include "keymap.h"
308 #include "disptab.h"
309 #include "termhooks.h"
310 #include "termopts.h"
311 #include "intervals.h"
312 #include "coding.h"
313 #include "region-cache.h"
314 #include "font.h"
315 #include "fontset.h"
316 #include "blockinput.h"
317 #include "xwidget.h"
318 #ifdef HAVE_WINDOW_SYSTEM
319 #include TERM_HEADER
320 #endif /* HAVE_WINDOW_SYSTEM */
321
322 #ifndef FRAME_X_OUTPUT
323 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
324 #endif
325
326 #define INFINITY 10000000
327
328 /* Holds the list (error). */
329 static Lisp_Object list_of_error;
330
331 #ifdef HAVE_WINDOW_SYSTEM
332
333 /* Test if overflow newline into fringe. Called with iterator IT
334 at or past right window margin, and with IT->current_x set. */
335
336 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
337 (!NILP (Voverflow_newline_into_fringe) \
338 && FRAME_WINDOW_P ((IT)->f) \
339 && ((IT)->bidi_it.paragraph_dir == R2L \
340 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
341 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
342 && (IT)->current_x == (IT)->last_visible_x)
343
344 #else /* !HAVE_WINDOW_SYSTEM */
345 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
346 #endif /* HAVE_WINDOW_SYSTEM */
347
348 /* Test if the display element loaded in IT, or the underlying buffer
349 or string character, is a space or a TAB character. This is used
350 to determine where word wrapping can occur. */
351
352 #define IT_DISPLAYING_WHITESPACE(it) \
353 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
354 || ((STRINGP (it->string) \
355 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
356 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
357 || (it->s \
358 && (it->s[IT_BYTEPOS (*it)] == ' ' \
359 || it->s[IT_BYTEPOS (*it)] == '\t')) \
360 || (IT_BYTEPOS (*it) < ZV_BYTE \
361 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
362 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
363
364 /* True means print newline to stdout before next mini-buffer message. */
365
366 bool noninteractive_need_newline;
367
368 /* True means print newline to message log before next message. */
369
370 static bool message_log_need_newline;
371
372 /* Three markers that message_dolog uses.
373 It could allocate them itself, but that causes trouble
374 in handling memory-full errors. */
375 static Lisp_Object message_dolog_marker1;
376 static Lisp_Object message_dolog_marker2;
377 static Lisp_Object message_dolog_marker3;
378 \f
379 /* The buffer position of the first character appearing entirely or
380 partially on the line of the selected window which contains the
381 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
382 redisplay optimization in redisplay_internal. */
383
384 static struct text_pos this_line_start_pos;
385
386 /* Number of characters past the end of the line above, including the
387 terminating newline. */
388
389 static struct text_pos this_line_end_pos;
390
391 /* The vertical positions and the height of this line. */
392
393 static int this_line_vpos;
394 static int this_line_y;
395 static int this_line_pixel_height;
396
397 /* X position at which this display line starts. Usually zero;
398 negative if first character is partially visible. */
399
400 static int this_line_start_x;
401
402 /* The smallest character position seen by move_it_* functions as they
403 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
404 hscrolled lines, see display_line. */
405
406 static struct text_pos this_line_min_pos;
407
408 /* Buffer that this_line_.* variables are referring to. */
409
410 static struct buffer *this_line_buffer;
411
412 /* True if an overlay arrow has been displayed in this window. */
413
414 static bool overlay_arrow_seen;
415
416 /* Vector containing glyphs for an ellipsis `...'. */
417
418 static Lisp_Object default_invis_vector[3];
419
420 /* This is the window where the echo area message was displayed. It
421 is always a mini-buffer window, but it may not be the same window
422 currently active as a mini-buffer. */
423
424 Lisp_Object echo_area_window;
425
426 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
427 pushes the current message and the value of
428 message_enable_multibyte on the stack, the function restore_message
429 pops the stack and displays MESSAGE again. */
430
431 static Lisp_Object Vmessage_stack;
432
433 /* True means multibyte characters were enabled when the echo area
434 message was specified. */
435
436 static bool message_enable_multibyte;
437
438 /* At each redisplay cycle, we should refresh everything there is to refresh.
439 To do that efficiently, we use many optimizations that try to make sure we
440 don't waste too much time updating things that haven't changed.
441 The coarsest such optimization is that, in the most common cases, we only
442 look at the selected-window.
443
444 To know whether other windows should be considered for redisplay, we use the
445 variable windows_or_buffers_changed: as long as it is 0, it means that we
446 have not noticed anything that should require updating anything else than
447 the selected-window. If it is set to REDISPLAY_SOME, it means that since
448 last redisplay, some changes have been made which could impact other
449 windows. To know which ones need redisplay, every buffer, window, and frame
450 has a `redisplay' bit, which (if true) means that this object needs to be
451 redisplayed. If windows_or_buffers_changed is 0, we know there's no point
452 looking for those `redisplay' bits (actually, there might be some such bits
453 set, but then only on objects which aren't displayed anyway).
454
455 OTOH if it's non-zero we wil have to loop through all windows and then check
456 the `redisplay' bit of the corresponding window, frame, and buffer, in order
457 to decide whether that window needs attention or not. Note that we can't
458 just look at the frame's redisplay bit to decide that the whole frame can be
459 skipped, since even if the frame's redisplay bit is unset, some of its
460 windows's redisplay bits may be set.
461
462 Mostly for historical reasons, windows_or_buffers_changed can also take
463 other non-zero values. In that case, the precise value doesn't matter (it
464 encodes the cause of the setting but is only used for debugging purposes),
465 and what it means is that we shouldn't pay attention to any `redisplay' bits
466 and we should simply try and redisplay every window out there. */
467
468 int windows_or_buffers_changed;
469
470 /* Nonzero if we should redraw the mode lines on the next redisplay.
471 Similarly to `windows_or_buffers_changed', If it has value REDISPLAY_SOME,
472 then only redisplay the mode lines in those buffers/windows/frames where the
473 `redisplay' bit has been set.
474 For any other value, redisplay all mode lines (the number used is then only
475 used to track down the cause for this full-redisplay).
476
477 Since the frame title uses the same %-constructs as the mode line
478 (except %c and %l), if this variable is non-zero, we also consider
479 redisplaying the title of each frame, see x_consider_frame_title.
480
481 The `redisplay' bits are the same as those used for
482 windows_or_buffers_changed, and setting windows_or_buffers_changed also
483 causes recomputation of the mode lines of all those windows. IOW this
484 variable only has an effect if windows_or_buffers_changed is zero, in which
485 case we should only need to redisplay the mode-line of those objects with
486 a `redisplay' bit set but not the window's text content (tho we may still
487 need to refresh the text content of the selected-window). */
488
489 int update_mode_lines;
490
491 /* True after display_mode_line if %l was used and it displayed a
492 line number. */
493
494 static bool line_number_displayed;
495
496 /* The name of the *Messages* buffer, a string. */
497
498 static Lisp_Object Vmessages_buffer_name;
499
500 /* Current, index 0, and last displayed echo area message. Either
501 buffers from echo_buffers, or nil to indicate no message. */
502
503 Lisp_Object echo_area_buffer[2];
504
505 /* The buffers referenced from echo_area_buffer. */
506
507 static Lisp_Object echo_buffer[2];
508
509 /* A vector saved used in with_area_buffer to reduce consing. */
510
511 static Lisp_Object Vwith_echo_area_save_vector;
512
513 /* True means display_echo_area should display the last echo area
514 message again. Set by redisplay_preserve_echo_area. */
515
516 static bool display_last_displayed_message_p;
517
518 /* True if echo area is being used by print; false if being used by
519 message. */
520
521 static bool message_buf_print;
522
523 /* Set to true in clear_message to make redisplay_internal aware
524 of an emptied echo area. */
525
526 static bool message_cleared_p;
527
528 /* A scratch glyph row with contents used for generating truncation
529 glyphs. Also used in direct_output_for_insert. */
530
531 #define MAX_SCRATCH_GLYPHS 100
532 static struct glyph_row scratch_glyph_row;
533 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
534
535 /* Ascent and height of the last line processed by move_it_to. */
536
537 static int last_height;
538
539 /* True if there's a help-echo in the echo area. */
540
541 bool help_echo_showing_p;
542
543 /* The maximum distance to look ahead for text properties. Values
544 that are too small let us call compute_char_face and similar
545 functions too often which is expensive. Values that are too large
546 let us call compute_char_face and alike too often because we
547 might not be interested in text properties that far away. */
548
549 #define TEXT_PROP_DISTANCE_LIMIT 100
550
551 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
552 iterator state and later restore it. This is needed because the
553 bidi iterator on bidi.c keeps a stacked cache of its states, which
554 is really a singleton. When we use scratch iterator objects to
555 move around the buffer, we can cause the bidi cache to be pushed or
556 popped, and therefore we need to restore the cache state when we
557 return to the original iterator. */
558 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
559 do { \
560 if (CACHE) \
561 bidi_unshelve_cache (CACHE, true); \
562 ITCOPY = ITORIG; \
563 CACHE = bidi_shelve_cache (); \
564 } while (false)
565
566 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
567 do { \
568 if (pITORIG != pITCOPY) \
569 *(pITORIG) = *(pITCOPY); \
570 bidi_unshelve_cache (CACHE, false); \
571 CACHE = NULL; \
572 } while (false)
573
574 /* Functions to mark elements as needing redisplay. */
575 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
576
577 void
578 redisplay_other_windows (void)
579 {
580 if (!windows_or_buffers_changed)
581 windows_or_buffers_changed = REDISPLAY_SOME;
582 }
583
584 void
585 wset_redisplay (struct window *w)
586 {
587 /* Beware: selected_window can be nil during early stages. */
588 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
589 redisplay_other_windows ();
590 w->redisplay = true;
591 }
592
593 void
594 fset_redisplay (struct frame *f)
595 {
596 redisplay_other_windows ();
597 f->redisplay = true;
598 }
599
600 void
601 bset_redisplay (struct buffer *b)
602 {
603 int count = buffer_window_count (b);
604 if (count > 0)
605 {
606 /* ... it's visible in other window than selected, */
607 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
608 redisplay_other_windows ();
609 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
610 so that if we later set windows_or_buffers_changed, this buffer will
611 not be omitted. */
612 b->text->redisplay = true;
613 }
614 }
615
616 void
617 bset_update_mode_line (struct buffer *b)
618 {
619 if (!update_mode_lines)
620 update_mode_lines = REDISPLAY_SOME;
621 b->text->redisplay = true;
622 }
623
624 void
625 maybe_set_redisplay (Lisp_Object symbol)
626 {
627 if (HASH_TABLE_P (Vredisplay__variables)
628 && hash_lookup (XHASH_TABLE (Vredisplay__variables), symbol, NULL) >= 0)
629 {
630 bset_update_mode_line (current_buffer);
631 current_buffer->prevent_redisplay_optimizations_p = true;
632 }
633 }
634
635 #ifdef GLYPH_DEBUG
636
637 /* True means print traces of redisplay if compiled with
638 GLYPH_DEBUG defined. */
639
640 bool trace_redisplay_p;
641
642 #endif /* GLYPH_DEBUG */
643
644 #ifdef DEBUG_TRACE_MOVE
645 /* True means trace with TRACE_MOVE to stderr. */
646 static bool trace_move;
647
648 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
649 #else
650 #define TRACE_MOVE(x) (void) 0
651 #endif
652
653 /* Buffer being redisplayed -- for redisplay_window_error. */
654
655 static struct buffer *displayed_buffer;
656
657 /* Value returned from text property handlers (see below). */
658
659 enum prop_handled
660 {
661 HANDLED_NORMALLY,
662 HANDLED_RECOMPUTE_PROPS,
663 HANDLED_OVERLAY_STRING_CONSUMED,
664 HANDLED_RETURN
665 };
666
667 /* A description of text properties that redisplay is interested
668 in. */
669
670 struct props
671 {
672 /* The symbol index of the name of the property. */
673 short name;
674
675 /* A unique index for the property. */
676 enum prop_idx idx;
677
678 /* A handler function called to set up iterator IT from the property
679 at IT's current position. Value is used to steer handle_stop. */
680 enum prop_handled (*handler) (struct it *it);
681 };
682
683 static enum prop_handled handle_face_prop (struct it *);
684 static enum prop_handled handle_invisible_prop (struct it *);
685 static enum prop_handled handle_display_prop (struct it *);
686 static enum prop_handled handle_composition_prop (struct it *);
687 static enum prop_handled handle_overlay_change (struct it *);
688 static enum prop_handled handle_fontified_prop (struct it *);
689
690 /* Properties handled by iterators. */
691
692 static struct props it_props[] =
693 {
694 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
695 /* Handle `face' before `display' because some sub-properties of
696 `display' need to know the face. */
697 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
698 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
699 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
700 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
701 {0, 0, NULL}
702 };
703
704 /* Value is the position described by X. If X is a marker, value is
705 the marker_position of X. Otherwise, value is X. */
706
707 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
708
709 /* Enumeration returned by some move_it_.* functions internally. */
710
711 enum move_it_result
712 {
713 /* Not used. Undefined value. */
714 MOVE_UNDEFINED,
715
716 /* Move ended at the requested buffer position or ZV. */
717 MOVE_POS_MATCH_OR_ZV,
718
719 /* Move ended at the requested X pixel position. */
720 MOVE_X_REACHED,
721
722 /* Move within a line ended at the end of a line that must be
723 continued. */
724 MOVE_LINE_CONTINUED,
725
726 /* Move within a line ended at the end of a line that would
727 be displayed truncated. */
728 MOVE_LINE_TRUNCATED,
729
730 /* Move within a line ended at a line end. */
731 MOVE_NEWLINE_OR_CR
732 };
733
734 /* This counter is used to clear the face cache every once in a while
735 in redisplay_internal. It is incremented for each redisplay.
736 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
737 cleared. */
738
739 #define CLEAR_FACE_CACHE_COUNT 500
740 static int clear_face_cache_count;
741
742 /* Similarly for the image cache. */
743
744 #ifdef HAVE_WINDOW_SYSTEM
745 #define CLEAR_IMAGE_CACHE_COUNT 101
746 static int clear_image_cache_count;
747
748 /* Null glyph slice */
749 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
750 #endif
751
752 /* True while redisplay_internal is in progress. */
753
754 bool redisplaying_p;
755
756 /* If a string, XTread_socket generates an event to display that string.
757 (The display is done in read_char.) */
758
759 Lisp_Object help_echo_string;
760 Lisp_Object help_echo_window;
761 Lisp_Object help_echo_object;
762 ptrdiff_t help_echo_pos;
763
764 /* Temporary variable for XTread_socket. */
765
766 Lisp_Object previous_help_echo_string;
767
768 /* Platform-independent portion of hourglass implementation. */
769
770 #ifdef HAVE_WINDOW_SYSTEM
771
772 /* True means an hourglass cursor is currently shown. */
773 static bool hourglass_shown_p;
774
775 /* If non-null, an asynchronous timer that, when it expires, displays
776 an hourglass cursor on all frames. */
777 static struct atimer *hourglass_atimer;
778
779 #endif /* HAVE_WINDOW_SYSTEM */
780
781 /* Default number of seconds to wait before displaying an hourglass
782 cursor. */
783 #define DEFAULT_HOURGLASS_DELAY 1
784
785 #ifdef HAVE_WINDOW_SYSTEM
786
787 /* Default pixel width of `thin-space' display method. */
788 #define THIN_SPACE_WIDTH 1
789
790 #endif /* HAVE_WINDOW_SYSTEM */
791
792 /* Function prototypes. */
793
794 static void setup_for_ellipsis (struct it *, int);
795 static void set_iterator_to_next (struct it *, bool);
796 static void mark_window_display_accurate_1 (struct window *, bool);
797 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
798 static bool cursor_row_p (struct glyph_row *);
799 static int redisplay_mode_lines (Lisp_Object, bool);
800
801 static void handle_line_prefix (struct it *);
802
803 static void handle_stop_backwards (struct it *, ptrdiff_t);
804 static void unwind_with_echo_area_buffer (Lisp_Object);
805 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
806 static bool current_message_1 (ptrdiff_t, Lisp_Object);
807 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
808 static void set_message (Lisp_Object);
809 static bool set_message_1 (ptrdiff_t, Lisp_Object);
810 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
811 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
812 static void unwind_redisplay (void);
813 static void extend_face_to_end_of_line (struct it *);
814 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
815 static void push_it (struct it *, struct text_pos *);
816 static void iterate_out_of_display_property (struct it *);
817 static void pop_it (struct it *);
818 static void redisplay_internal (void);
819 static void echo_area_display (bool);
820 static void redisplay_windows (Lisp_Object);
821 static void redisplay_window (Lisp_Object, bool);
822 static Lisp_Object redisplay_window_error (Lisp_Object);
823 static Lisp_Object redisplay_window_0 (Lisp_Object);
824 static Lisp_Object redisplay_window_1 (Lisp_Object);
825 static bool set_cursor_from_row (struct window *, struct glyph_row *,
826 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
827 int, int);
828 static bool cursor_row_fully_visible_p (struct window *, bool, bool);
829 static bool update_menu_bar (struct frame *, bool, bool);
830 static bool try_window_reusing_current_matrix (struct window *);
831 static int try_window_id (struct window *);
832 static bool display_line (struct it *);
833 static int display_mode_lines (struct window *);
834 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
835 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
836 Lisp_Object, bool);
837 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
838 Lisp_Object);
839 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
840 static void display_menu_bar (struct window *);
841 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
842 ptrdiff_t *);
843 static int display_string (const char *, Lisp_Object, Lisp_Object,
844 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
845 static void compute_line_metrics (struct it *);
846 static void run_redisplay_end_trigger_hook (struct it *);
847 static bool get_overlay_strings (struct it *, ptrdiff_t);
848 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
849 static void next_overlay_string (struct it *);
850 static void reseat (struct it *, struct text_pos, bool);
851 static void reseat_1 (struct it *, struct text_pos, bool);
852 static bool next_element_from_display_vector (struct it *);
853 static bool next_element_from_string (struct it *);
854 static bool next_element_from_c_string (struct it *);
855 static bool next_element_from_buffer (struct it *);
856 static bool next_element_from_composition (struct it *);
857 static bool next_element_from_image (struct it *);
858 static bool next_element_from_stretch (struct it *);
859 static bool next_element_from_xwidget (struct it *);
860 static void load_overlay_strings (struct it *, ptrdiff_t);
861 static bool get_next_display_element (struct it *);
862 static enum move_it_result
863 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
864 enum move_operation_enum);
865 static void get_visually_first_element (struct it *);
866 static void compute_stop_pos (struct it *);
867 static int face_before_or_after_it_pos (struct it *, bool);
868 static ptrdiff_t next_overlay_change (ptrdiff_t);
869 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
870 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
871 static int handle_single_display_spec (struct it *, Lisp_Object,
872 Lisp_Object, Lisp_Object,
873 struct text_pos *, ptrdiff_t, int, bool);
874 static int underlying_face_id (struct it *);
875
876 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
877 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
878
879 #ifdef HAVE_WINDOW_SYSTEM
880
881 static void update_tool_bar (struct frame *, bool);
882 static void x_draw_bottom_divider (struct window *w);
883 static void notice_overwritten_cursor (struct window *,
884 enum glyph_row_area,
885 int, int, int, int);
886 static int normal_char_height (struct font *, int);
887 static void normal_char_ascent_descent (struct font *, int, int *, int *);
888
889 static void append_stretch_glyph (struct it *, Lisp_Object,
890 int, int, int);
891
892 static Lisp_Object get_it_property (struct it *, Lisp_Object);
893 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
894 struct font *, int, bool);
895
896 #endif /* HAVE_WINDOW_SYSTEM */
897
898 static void produce_special_glyphs (struct it *, enum display_element_type);
899 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
900 static bool coords_in_mouse_face_p (struct window *, int, int);
901
902
903 \f
904 /***********************************************************************
905 Window display dimensions
906 ***********************************************************************/
907
908 /* Return the bottom boundary y-position for text lines in window W.
909 This is the first y position at which a line cannot start.
910 It is relative to the top of the window.
911
912 This is the height of W minus the height of a mode line, if any. */
913
914 int
915 window_text_bottom_y (struct window *w)
916 {
917 int height = WINDOW_PIXEL_HEIGHT (w);
918
919 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
920
921 if (WINDOW_WANTS_MODELINE_P (w))
922 height -= CURRENT_MODE_LINE_HEIGHT (w);
923
924 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
925
926 return height;
927 }
928
929 /* Return the pixel width of display area AREA of window W.
930 ANY_AREA means return the total width of W, not including
931 fringes to the left and right of the window. */
932
933 int
934 window_box_width (struct window *w, enum glyph_row_area area)
935 {
936 int width = w->pixel_width;
937
938 if (!w->pseudo_window_p)
939 {
940 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
941 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
942
943 if (area == TEXT_AREA)
944 width -= (WINDOW_MARGINS_WIDTH (w)
945 + WINDOW_FRINGES_WIDTH (w));
946 else if (area == LEFT_MARGIN_AREA)
947 width = WINDOW_LEFT_MARGIN_WIDTH (w);
948 else if (area == RIGHT_MARGIN_AREA)
949 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
950 }
951
952 /* With wide margins, fringes, etc. we might end up with a negative
953 width, correct that here. */
954 return max (0, width);
955 }
956
957
958 /* Return the pixel height of the display area of window W, not
959 including mode lines of W, if any. */
960
961 int
962 window_box_height (struct window *w)
963 {
964 struct frame *f = XFRAME (w->frame);
965 int height = WINDOW_PIXEL_HEIGHT (w);
966
967 eassert (height >= 0);
968
969 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
970 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
971
972 /* Note: the code below that determines the mode-line/header-line
973 height is essentially the same as that contained in the macro
974 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
975 the appropriate glyph row has its `mode_line_p' flag set,
976 and if it doesn't, uses estimate_mode_line_height instead. */
977
978 if (WINDOW_WANTS_MODELINE_P (w))
979 {
980 struct glyph_row *ml_row
981 = (w->current_matrix && w->current_matrix->rows
982 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
983 : 0);
984 if (ml_row && ml_row->mode_line_p)
985 height -= ml_row->height;
986 else
987 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
988 }
989
990 if (WINDOW_WANTS_HEADER_LINE_P (w))
991 {
992 struct glyph_row *hl_row
993 = (w->current_matrix && w->current_matrix->rows
994 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
995 : 0);
996 if (hl_row && hl_row->mode_line_p)
997 height -= hl_row->height;
998 else
999 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1000 }
1001
1002 /* With a very small font and a mode-line that's taller than
1003 default, we might end up with a negative height. */
1004 return max (0, height);
1005 }
1006
1007 /* Return the window-relative coordinate of the left edge of display
1008 area AREA of window W. ANY_AREA means return the left edge of the
1009 whole window, to the right of the left fringe of W. */
1010
1011 int
1012 window_box_left_offset (struct window *w, enum glyph_row_area area)
1013 {
1014 int x;
1015
1016 if (w->pseudo_window_p)
1017 return 0;
1018
1019 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1020
1021 if (area == TEXT_AREA)
1022 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1023 + window_box_width (w, LEFT_MARGIN_AREA));
1024 else if (area == RIGHT_MARGIN_AREA)
1025 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1026 + window_box_width (w, LEFT_MARGIN_AREA)
1027 + window_box_width (w, TEXT_AREA)
1028 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1029 ? 0
1030 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1031 else if (area == LEFT_MARGIN_AREA
1032 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1033 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1034
1035 /* Don't return more than the window's pixel width. */
1036 return min (x, w->pixel_width);
1037 }
1038
1039
1040 /* Return the window-relative coordinate of the right edge of display
1041 area AREA of window W. ANY_AREA means return the right edge of the
1042 whole window, to the left of the right fringe of W. */
1043
1044 static int
1045 window_box_right_offset (struct window *w, enum glyph_row_area area)
1046 {
1047 /* Don't return more than the window's pixel width. */
1048 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1049 w->pixel_width);
1050 }
1051
1052 /* Return the frame-relative coordinate of the left edge of display
1053 area AREA of window W. ANY_AREA means return the left edge of the
1054 whole window, to the right of the left fringe of W. */
1055
1056 int
1057 window_box_left (struct window *w, enum glyph_row_area area)
1058 {
1059 struct frame *f = XFRAME (w->frame);
1060 int x;
1061
1062 if (w->pseudo_window_p)
1063 return FRAME_INTERNAL_BORDER_WIDTH (f);
1064
1065 x = (WINDOW_LEFT_EDGE_X (w)
1066 + window_box_left_offset (w, area));
1067
1068 return x;
1069 }
1070
1071
1072 /* Return the frame-relative coordinate of the right edge of display
1073 area AREA of window W. ANY_AREA means return the right edge of the
1074 whole window, to the left of the right fringe of W. */
1075
1076 int
1077 window_box_right (struct window *w, enum glyph_row_area area)
1078 {
1079 return window_box_left (w, area) + window_box_width (w, area);
1080 }
1081
1082 /* Get the bounding box of the display area AREA of window W, without
1083 mode lines, in frame-relative coordinates. ANY_AREA means the
1084 whole window, not including the left and right fringes of
1085 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1086 coordinates of the upper-left corner of the box. Return in
1087 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1088
1089 void
1090 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1091 int *box_y, int *box_width, int *box_height)
1092 {
1093 if (box_width)
1094 *box_width = window_box_width (w, area);
1095 if (box_height)
1096 *box_height = window_box_height (w);
1097 if (box_x)
1098 *box_x = window_box_left (w, area);
1099 if (box_y)
1100 {
1101 *box_y = WINDOW_TOP_EDGE_Y (w);
1102 if (WINDOW_WANTS_HEADER_LINE_P (w))
1103 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1104 }
1105 }
1106
1107 #ifdef HAVE_WINDOW_SYSTEM
1108
1109 /* Get the bounding box of the display area AREA of window W, without
1110 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1111 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1112 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1113 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1114 box. */
1115
1116 static void
1117 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1118 int *bottom_right_x, int *bottom_right_y)
1119 {
1120 window_box (w, ANY_AREA, top_left_x, top_left_y,
1121 bottom_right_x, bottom_right_y);
1122 *bottom_right_x += *top_left_x;
1123 *bottom_right_y += *top_left_y;
1124 }
1125
1126 #endif /* HAVE_WINDOW_SYSTEM */
1127
1128 /***********************************************************************
1129 Utilities
1130 ***********************************************************************/
1131
1132 /* Return the bottom y-position of the line the iterator IT is in.
1133 This can modify IT's settings. */
1134
1135 int
1136 line_bottom_y (struct it *it)
1137 {
1138 int line_height = it->max_ascent + it->max_descent;
1139 int line_top_y = it->current_y;
1140
1141 if (line_height == 0)
1142 {
1143 if (last_height)
1144 line_height = last_height;
1145 else if (IT_CHARPOS (*it) < ZV)
1146 {
1147 move_it_by_lines (it, 1);
1148 line_height = (it->max_ascent || it->max_descent
1149 ? it->max_ascent + it->max_descent
1150 : last_height);
1151 }
1152 else
1153 {
1154 struct glyph_row *row = it->glyph_row;
1155
1156 /* Use the default character height. */
1157 it->glyph_row = NULL;
1158 it->what = IT_CHARACTER;
1159 it->c = ' ';
1160 it->len = 1;
1161 PRODUCE_GLYPHS (it);
1162 line_height = it->ascent + it->descent;
1163 it->glyph_row = row;
1164 }
1165 }
1166
1167 return line_top_y + line_height;
1168 }
1169
1170 DEFUN ("line-pixel-height", Fline_pixel_height,
1171 Sline_pixel_height, 0, 0, 0,
1172 doc: /* Return height in pixels of text line in the selected window.
1173
1174 Value is the height in pixels of the line at point. */)
1175 (void)
1176 {
1177 struct it it;
1178 struct text_pos pt;
1179 struct window *w = XWINDOW (selected_window);
1180 struct buffer *old_buffer = NULL;
1181 Lisp_Object result;
1182
1183 if (XBUFFER (w->contents) != current_buffer)
1184 {
1185 old_buffer = current_buffer;
1186 set_buffer_internal_1 (XBUFFER (w->contents));
1187 }
1188 SET_TEXT_POS (pt, PT, PT_BYTE);
1189 start_display (&it, w, pt);
1190 it.vpos = it.current_y = 0;
1191 last_height = 0;
1192 result = make_number (line_bottom_y (&it));
1193 if (old_buffer)
1194 set_buffer_internal_1 (old_buffer);
1195
1196 return result;
1197 }
1198
1199 /* Return the default pixel height of text lines in window W. The
1200 value is the canonical height of the W frame's default font, plus
1201 any extra space required by the line-spacing variable or frame
1202 parameter.
1203
1204 Implementation note: this ignores any line-spacing text properties
1205 put on the newline characters. This is because those properties
1206 only affect the _screen_ line ending in the newline (i.e., in a
1207 continued line, only the last screen line will be affected), which
1208 means only a small number of lines in a buffer can ever use this
1209 feature. Since this function is used to compute the default pixel
1210 equivalent of text lines in a window, we can safely ignore those
1211 few lines. For the same reasons, we ignore the line-height
1212 properties. */
1213 int
1214 default_line_pixel_height (struct window *w)
1215 {
1216 struct frame *f = WINDOW_XFRAME (w);
1217 int height = FRAME_LINE_HEIGHT (f);
1218
1219 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1220 {
1221 struct buffer *b = XBUFFER (w->contents);
1222 Lisp_Object val = BVAR (b, extra_line_spacing);
1223
1224 if (NILP (val))
1225 val = BVAR (&buffer_defaults, extra_line_spacing);
1226 if (!NILP (val))
1227 {
1228 if (RANGED_INTEGERP (0, val, INT_MAX))
1229 height += XFASTINT (val);
1230 else if (FLOATP (val))
1231 {
1232 int addon = XFLOAT_DATA (val) * height + 0.5;
1233
1234 if (addon >= 0)
1235 height += addon;
1236 }
1237 }
1238 else
1239 height += f->extra_line_spacing;
1240 }
1241
1242 return height;
1243 }
1244
1245 /* Subroutine of pos_visible_p below. Extracts a display string, if
1246 any, from the display spec given as its argument. */
1247 static Lisp_Object
1248 string_from_display_spec (Lisp_Object spec)
1249 {
1250 if (CONSP (spec))
1251 {
1252 while (CONSP (spec))
1253 {
1254 if (STRINGP (XCAR (spec)))
1255 return XCAR (spec);
1256 spec = XCDR (spec);
1257 }
1258 }
1259 else if (VECTORP (spec))
1260 {
1261 ptrdiff_t i;
1262
1263 for (i = 0; i < ASIZE (spec); i++)
1264 {
1265 if (STRINGP (AREF (spec, i)))
1266 return AREF (spec, i);
1267 }
1268 return Qnil;
1269 }
1270
1271 return spec;
1272 }
1273
1274
1275 /* Limit insanely large values of W->hscroll on frame F to the largest
1276 value that will still prevent first_visible_x and last_visible_x of
1277 'struct it' from overflowing an int. */
1278 static int
1279 window_hscroll_limited (struct window *w, struct frame *f)
1280 {
1281 ptrdiff_t window_hscroll = w->hscroll;
1282 int window_text_width = window_box_width (w, TEXT_AREA);
1283 int colwidth = FRAME_COLUMN_WIDTH (f);
1284
1285 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1286 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1287
1288 return window_hscroll;
1289 }
1290
1291 /* Return true if position CHARPOS is visible in window W.
1292 CHARPOS < 0 means return info about WINDOW_END position.
1293 If visible, set *X and *Y to pixel coordinates of top left corner.
1294 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1295 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1296
1297 bool
1298 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1299 int *rtop, int *rbot, int *rowh, int *vpos)
1300 {
1301 struct it it;
1302 void *itdata = bidi_shelve_cache ();
1303 struct text_pos top;
1304 bool visible_p = false;
1305 struct buffer *old_buffer = NULL;
1306 bool r2l = false;
1307
1308 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1309 return visible_p;
1310
1311 if (XBUFFER (w->contents) != current_buffer)
1312 {
1313 old_buffer = current_buffer;
1314 set_buffer_internal_1 (XBUFFER (w->contents));
1315 }
1316
1317 SET_TEXT_POS_FROM_MARKER (top, w->start);
1318 /* Scrolling a minibuffer window via scroll bar when the echo area
1319 shows long text sometimes resets the minibuffer contents behind
1320 our backs. */
1321 if (CHARPOS (top) > ZV)
1322 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1323
1324 /* If the top of the window is after CHARPOS, the latter is surely
1325 not visible. */
1326 if (charpos >= 0 && CHARPOS (top) > charpos)
1327 return visible_p;
1328
1329 /* Compute exact mode line heights. */
1330 if (WINDOW_WANTS_MODELINE_P (w))
1331 w->mode_line_height
1332 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1333 BVAR (current_buffer, mode_line_format));
1334
1335 if (WINDOW_WANTS_HEADER_LINE_P (w))
1336 w->header_line_height
1337 = display_mode_line (w, HEADER_LINE_FACE_ID,
1338 BVAR (current_buffer, header_line_format));
1339
1340 start_display (&it, w, top);
1341 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1342 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1343
1344 if (charpos >= 0
1345 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1346 && IT_CHARPOS (it) >= charpos)
1347 /* When scanning backwards under bidi iteration, move_it_to
1348 stops at or _before_ CHARPOS, because it stops at or to
1349 the _right_ of the character at CHARPOS. */
1350 || (it.bidi_p && it.bidi_it.scan_dir == -1
1351 && IT_CHARPOS (it) <= charpos)))
1352 {
1353 /* We have reached CHARPOS, or passed it. How the call to
1354 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1355 or covered by a display property, move_it_to stops at the end
1356 of the invisible text, to the right of CHARPOS. (ii) If
1357 CHARPOS is in a display vector, move_it_to stops on its last
1358 glyph. */
1359 int top_x = it.current_x;
1360 int top_y = it.current_y;
1361 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1362 int bottom_y;
1363 struct it save_it;
1364 void *save_it_data = NULL;
1365
1366 /* Calling line_bottom_y may change it.method, it.position, etc. */
1367 SAVE_IT (save_it, it, save_it_data);
1368 last_height = 0;
1369 bottom_y = line_bottom_y (&it);
1370 if (top_y < window_top_y)
1371 visible_p = bottom_y > window_top_y;
1372 else if (top_y < it.last_visible_y)
1373 visible_p = true;
1374 if (bottom_y >= it.last_visible_y
1375 && it.bidi_p && it.bidi_it.scan_dir == -1
1376 && IT_CHARPOS (it) < charpos)
1377 {
1378 /* When the last line of the window is scanned backwards
1379 under bidi iteration, we could be duped into thinking
1380 that we have passed CHARPOS, when in fact move_it_to
1381 simply stopped short of CHARPOS because it reached
1382 last_visible_y. To see if that's what happened, we call
1383 move_it_to again with a slightly larger vertical limit,
1384 and see if it actually moved vertically; if it did, we
1385 didn't really reach CHARPOS, which is beyond window end. */
1386 /* Why 10? because we don't know how many canonical lines
1387 will the height of the next line(s) be. So we guess. */
1388 int ten_more_lines = 10 * default_line_pixel_height (w);
1389
1390 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1391 MOVE_TO_POS | MOVE_TO_Y);
1392 if (it.current_y > top_y)
1393 visible_p = false;
1394
1395 }
1396 RESTORE_IT (&it, &save_it, save_it_data);
1397 if (visible_p)
1398 {
1399 if (it.method == GET_FROM_DISPLAY_VECTOR)
1400 {
1401 /* We stopped on the last glyph of a display vector.
1402 Try and recompute. Hack alert! */
1403 if (charpos < 2 || top.charpos >= charpos)
1404 top_x = it.glyph_row->x;
1405 else
1406 {
1407 struct it it2, it2_prev;
1408 /* The idea is to get to the previous buffer
1409 position, consume the character there, and use
1410 the pixel coordinates we get after that. But if
1411 the previous buffer position is also displayed
1412 from a display vector, we need to consume all of
1413 the glyphs from that display vector. */
1414 start_display (&it2, w, top);
1415 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1416 /* If we didn't get to CHARPOS - 1, there's some
1417 replacing display property at that position, and
1418 we stopped after it. That is exactly the place
1419 whose coordinates we want. */
1420 if (IT_CHARPOS (it2) != charpos - 1)
1421 it2_prev = it2;
1422 else
1423 {
1424 /* Iterate until we get out of the display
1425 vector that displays the character at
1426 CHARPOS - 1. */
1427 do {
1428 get_next_display_element (&it2);
1429 PRODUCE_GLYPHS (&it2);
1430 it2_prev = it2;
1431 set_iterator_to_next (&it2, true);
1432 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1433 && IT_CHARPOS (it2) < charpos);
1434 }
1435 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1436 || it2_prev.current_x > it2_prev.last_visible_x)
1437 top_x = it.glyph_row->x;
1438 else
1439 {
1440 top_x = it2_prev.current_x;
1441 top_y = it2_prev.current_y;
1442 }
1443 }
1444 }
1445 else if (IT_CHARPOS (it) != charpos)
1446 {
1447 Lisp_Object cpos = make_number (charpos);
1448 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1449 Lisp_Object string = string_from_display_spec (spec);
1450 struct text_pos tpos;
1451 bool newline_in_string
1452 = (STRINGP (string)
1453 && memchr (SDATA (string), '\n', SBYTES (string)));
1454
1455 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1456 bool replacing_spec_p
1457 = (!NILP (spec)
1458 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1459 charpos, FRAME_WINDOW_P (it.f)));
1460 /* The tricky code below is needed because there's a
1461 discrepancy between move_it_to and how we set cursor
1462 when PT is at the beginning of a portion of text
1463 covered by a display property or an overlay with a
1464 display property, or the display line ends in a
1465 newline from a display string. move_it_to will stop
1466 _after_ such display strings, whereas
1467 set_cursor_from_row conspires with cursor_row_p to
1468 place the cursor on the first glyph produced from the
1469 display string. */
1470
1471 /* We have overshoot PT because it is covered by a
1472 display property that replaces the text it covers.
1473 If the string includes embedded newlines, we are also
1474 in the wrong display line. Backtrack to the correct
1475 line, where the display property begins. */
1476 if (replacing_spec_p)
1477 {
1478 Lisp_Object startpos, endpos;
1479 EMACS_INT start, end;
1480 struct it it3;
1481
1482 /* Find the first and the last buffer positions
1483 covered by the display string. */
1484 endpos =
1485 Fnext_single_char_property_change (cpos, Qdisplay,
1486 Qnil, Qnil);
1487 startpos =
1488 Fprevious_single_char_property_change (endpos, Qdisplay,
1489 Qnil, Qnil);
1490 start = XFASTINT (startpos);
1491 end = XFASTINT (endpos);
1492 /* Move to the last buffer position before the
1493 display property. */
1494 start_display (&it3, w, top);
1495 if (start > CHARPOS (top))
1496 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1497 /* Move forward one more line if the position before
1498 the display string is a newline or if it is the
1499 rightmost character on a line that is
1500 continued or word-wrapped. */
1501 if (it3.method == GET_FROM_BUFFER
1502 && (it3.c == '\n'
1503 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1504 move_it_by_lines (&it3, 1);
1505 else if (move_it_in_display_line_to (&it3, -1,
1506 it3.current_x
1507 + it3.pixel_width,
1508 MOVE_TO_X)
1509 == MOVE_LINE_CONTINUED)
1510 {
1511 move_it_by_lines (&it3, 1);
1512 /* When we are under word-wrap, the #$@%!
1513 move_it_by_lines moves 2 lines, so we need to
1514 fix that up. */
1515 if (it3.line_wrap == WORD_WRAP)
1516 move_it_by_lines (&it3, -1);
1517 }
1518
1519 /* Record the vertical coordinate of the display
1520 line where we wound up. */
1521 top_y = it3.current_y;
1522 if (it3.bidi_p)
1523 {
1524 /* When characters are reordered for display,
1525 the character displayed to the left of the
1526 display string could be _after_ the display
1527 property in the logical order. Use the
1528 smallest vertical position of these two. */
1529 start_display (&it3, w, top);
1530 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1531 if (it3.current_y < top_y)
1532 top_y = it3.current_y;
1533 }
1534 /* Move from the top of the window to the beginning
1535 of the display line where the display string
1536 begins. */
1537 start_display (&it3, w, top);
1538 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1539 /* If it3_moved stays false after the 'while' loop
1540 below, that means we already were at a newline
1541 before the loop (e.g., the display string begins
1542 with a newline), so we don't need to (and cannot)
1543 inspect the glyphs of it3.glyph_row, because
1544 PRODUCE_GLYPHS will not produce anything for a
1545 newline, and thus it3.glyph_row stays at its
1546 stale content it got at top of the window. */
1547 bool it3_moved = false;
1548 /* Finally, advance the iterator until we hit the
1549 first display element whose character position is
1550 CHARPOS, or until the first newline from the
1551 display string, which signals the end of the
1552 display line. */
1553 while (get_next_display_element (&it3))
1554 {
1555 PRODUCE_GLYPHS (&it3);
1556 if (IT_CHARPOS (it3) == charpos
1557 || ITERATOR_AT_END_OF_LINE_P (&it3))
1558 break;
1559 it3_moved = true;
1560 set_iterator_to_next (&it3, false);
1561 }
1562 top_x = it3.current_x - it3.pixel_width;
1563 /* Normally, we would exit the above loop because we
1564 found the display element whose character
1565 position is CHARPOS. For the contingency that we
1566 didn't, and stopped at the first newline from the
1567 display string, move back over the glyphs
1568 produced from the string, until we find the
1569 rightmost glyph not from the string. */
1570 if (it3_moved
1571 && newline_in_string
1572 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1573 {
1574 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1575 + it3.glyph_row->used[TEXT_AREA];
1576
1577 while (EQ ((g - 1)->object, string))
1578 {
1579 --g;
1580 top_x -= g->pixel_width;
1581 }
1582 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1583 + it3.glyph_row->used[TEXT_AREA]);
1584 }
1585 }
1586 }
1587
1588 *x = top_x;
1589 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1590 *rtop = max (0, window_top_y - top_y);
1591 *rbot = max (0, bottom_y - it.last_visible_y);
1592 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1593 - max (top_y, window_top_y)));
1594 *vpos = it.vpos;
1595 if (it.bidi_it.paragraph_dir == R2L)
1596 r2l = true;
1597 }
1598 }
1599 else
1600 {
1601 /* Either we were asked to provide info about WINDOW_END, or
1602 CHARPOS is in the partially visible glyph row at end of
1603 window. */
1604 struct it it2;
1605 void *it2data = NULL;
1606
1607 SAVE_IT (it2, it, it2data);
1608 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1609 move_it_by_lines (&it, 1);
1610 if (charpos < IT_CHARPOS (it)
1611 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1612 {
1613 visible_p = true;
1614 RESTORE_IT (&it2, &it2, it2data);
1615 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1616 *x = it2.current_x;
1617 *y = it2.current_y + it2.max_ascent - it2.ascent;
1618 *rtop = max (0, -it2.current_y);
1619 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1620 - it.last_visible_y));
1621 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1622 it.last_visible_y)
1623 - max (it2.current_y,
1624 WINDOW_HEADER_LINE_HEIGHT (w))));
1625 *vpos = it2.vpos;
1626 if (it2.bidi_it.paragraph_dir == R2L)
1627 r2l = true;
1628 }
1629 else
1630 bidi_unshelve_cache (it2data, true);
1631 }
1632 bidi_unshelve_cache (itdata, false);
1633
1634 if (old_buffer)
1635 set_buffer_internal_1 (old_buffer);
1636
1637 if (visible_p)
1638 {
1639 if (w->hscroll > 0)
1640 *x -=
1641 window_hscroll_limited (w, WINDOW_XFRAME (w))
1642 * WINDOW_FRAME_COLUMN_WIDTH (w);
1643 /* For lines in an R2L paragraph, we need to mirror the X pixel
1644 coordinate wrt the text area. For the reasons, see the
1645 commentary in buffer_posn_from_coords and the explanation of
1646 the geometry used by the move_it_* functions at the end of
1647 the large commentary near the beginning of this file. */
1648 if (r2l)
1649 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1650 }
1651
1652 #if false
1653 /* Debugging code. */
1654 if (visible_p)
1655 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1656 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1657 else
1658 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1659 #endif
1660
1661 return visible_p;
1662 }
1663
1664
1665 /* Return the next character from STR. Return in *LEN the length of
1666 the character. This is like STRING_CHAR_AND_LENGTH but never
1667 returns an invalid character. If we find one, we return a `?', but
1668 with the length of the invalid character. */
1669
1670 static int
1671 string_char_and_length (const unsigned char *str, int *len)
1672 {
1673 int c;
1674
1675 c = STRING_CHAR_AND_LENGTH (str, *len);
1676 if (!CHAR_VALID_P (c))
1677 /* We may not change the length here because other places in Emacs
1678 don't use this function, i.e. they silently accept invalid
1679 characters. */
1680 c = '?';
1681
1682 return c;
1683 }
1684
1685
1686
1687 /* Given a position POS containing a valid character and byte position
1688 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1689
1690 static struct text_pos
1691 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1692 {
1693 eassert (STRINGP (string) && nchars >= 0);
1694
1695 if (STRING_MULTIBYTE (string))
1696 {
1697 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1698 int len;
1699
1700 while (nchars--)
1701 {
1702 string_char_and_length (p, &len);
1703 p += len;
1704 CHARPOS (pos) += 1;
1705 BYTEPOS (pos) += len;
1706 }
1707 }
1708 else
1709 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1710
1711 return pos;
1712 }
1713
1714
1715 /* Value is the text position, i.e. character and byte position,
1716 for character position CHARPOS in STRING. */
1717
1718 static struct text_pos
1719 string_pos (ptrdiff_t charpos, Lisp_Object string)
1720 {
1721 struct text_pos pos;
1722 eassert (STRINGP (string));
1723 eassert (charpos >= 0);
1724 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1725 return pos;
1726 }
1727
1728
1729 /* Value is a text position, i.e. character and byte position, for
1730 character position CHARPOS in C string S. MULTIBYTE_P
1731 means recognize multibyte characters. */
1732
1733 static struct text_pos
1734 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1735 {
1736 struct text_pos pos;
1737
1738 eassert (s != NULL);
1739 eassert (charpos >= 0);
1740
1741 if (multibyte_p)
1742 {
1743 int len;
1744
1745 SET_TEXT_POS (pos, 0, 0);
1746 while (charpos--)
1747 {
1748 string_char_and_length ((const unsigned char *) s, &len);
1749 s += len;
1750 CHARPOS (pos) += 1;
1751 BYTEPOS (pos) += len;
1752 }
1753 }
1754 else
1755 SET_TEXT_POS (pos, charpos, charpos);
1756
1757 return pos;
1758 }
1759
1760
1761 /* Value is the number of characters in C string S. MULTIBYTE_P
1762 means recognize multibyte characters. */
1763
1764 static ptrdiff_t
1765 number_of_chars (const char *s, bool multibyte_p)
1766 {
1767 ptrdiff_t nchars;
1768
1769 if (multibyte_p)
1770 {
1771 ptrdiff_t rest = strlen (s);
1772 int len;
1773 const unsigned char *p = (const unsigned char *) s;
1774
1775 for (nchars = 0; rest > 0; ++nchars)
1776 {
1777 string_char_and_length (p, &len);
1778 rest -= len, p += len;
1779 }
1780 }
1781 else
1782 nchars = strlen (s);
1783
1784 return nchars;
1785 }
1786
1787
1788 /* Compute byte position NEWPOS->bytepos corresponding to
1789 NEWPOS->charpos. POS is a known position in string STRING.
1790 NEWPOS->charpos must be >= POS.charpos. */
1791
1792 static void
1793 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1794 {
1795 eassert (STRINGP (string));
1796 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1797
1798 if (STRING_MULTIBYTE (string))
1799 *newpos = string_pos_nchars_ahead (pos, string,
1800 CHARPOS (*newpos) - CHARPOS (pos));
1801 else
1802 BYTEPOS (*newpos) = CHARPOS (*newpos);
1803 }
1804
1805 /* EXPORT:
1806 Return an estimation of the pixel height of mode or header lines on
1807 frame F. FACE_ID specifies what line's height to estimate. */
1808
1809 int
1810 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1811 {
1812 #ifdef HAVE_WINDOW_SYSTEM
1813 if (FRAME_WINDOW_P (f))
1814 {
1815 int height = FONT_HEIGHT (FRAME_FONT (f));
1816
1817 /* This function is called so early when Emacs starts that the face
1818 cache and mode line face are not yet initialized. */
1819 if (FRAME_FACE_CACHE (f))
1820 {
1821 struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1822 if (face)
1823 {
1824 if (face->font)
1825 height = normal_char_height (face->font, -1);
1826 if (face->box_line_width > 0)
1827 height += 2 * face->box_line_width;
1828 }
1829 }
1830
1831 return height;
1832 }
1833 #endif
1834
1835 return 1;
1836 }
1837
1838 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1839 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1840 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1841 not force the value into range. */
1842
1843 void
1844 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1845 NativeRectangle *bounds, bool noclip)
1846 {
1847
1848 #ifdef HAVE_WINDOW_SYSTEM
1849 if (FRAME_WINDOW_P (f))
1850 {
1851 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1852 even for negative values. */
1853 if (pix_x < 0)
1854 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1855 if (pix_y < 0)
1856 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1857
1858 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1859 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1860
1861 if (bounds)
1862 STORE_NATIVE_RECT (*bounds,
1863 FRAME_COL_TO_PIXEL_X (f, pix_x),
1864 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1865 FRAME_COLUMN_WIDTH (f) - 1,
1866 FRAME_LINE_HEIGHT (f) - 1);
1867
1868 /* PXW: Should we clip pixels before converting to columns/lines? */
1869 if (!noclip)
1870 {
1871 if (pix_x < 0)
1872 pix_x = 0;
1873 else if (pix_x > FRAME_TOTAL_COLS (f))
1874 pix_x = FRAME_TOTAL_COLS (f);
1875
1876 if (pix_y < 0)
1877 pix_y = 0;
1878 else if (pix_y > FRAME_TOTAL_LINES (f))
1879 pix_y = FRAME_TOTAL_LINES (f);
1880 }
1881 }
1882 #endif
1883
1884 *x = pix_x;
1885 *y = pix_y;
1886 }
1887
1888
1889 /* Find the glyph under window-relative coordinates X/Y in window W.
1890 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1891 strings. Return in *HPOS and *VPOS the row and column number of
1892 the glyph found. Return in *AREA the glyph area containing X.
1893 Value is a pointer to the glyph found or null if X/Y is not on
1894 text, or we can't tell because W's current matrix is not up to
1895 date. */
1896
1897 static struct glyph *
1898 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1899 int *dx, int *dy, int *area)
1900 {
1901 struct glyph *glyph, *end;
1902 struct glyph_row *row = NULL;
1903 int x0, i;
1904
1905 /* Find row containing Y. Give up if some row is not enabled. */
1906 for (i = 0; i < w->current_matrix->nrows; ++i)
1907 {
1908 row = MATRIX_ROW (w->current_matrix, i);
1909 if (!row->enabled_p)
1910 return NULL;
1911 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1912 break;
1913 }
1914
1915 *vpos = i;
1916 *hpos = 0;
1917
1918 /* Give up if Y is not in the window. */
1919 if (i == w->current_matrix->nrows)
1920 return NULL;
1921
1922 /* Get the glyph area containing X. */
1923 if (w->pseudo_window_p)
1924 {
1925 *area = TEXT_AREA;
1926 x0 = 0;
1927 }
1928 else
1929 {
1930 if (x < window_box_left_offset (w, TEXT_AREA))
1931 {
1932 *area = LEFT_MARGIN_AREA;
1933 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1934 }
1935 else if (x < window_box_right_offset (w, TEXT_AREA))
1936 {
1937 *area = TEXT_AREA;
1938 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1939 }
1940 else
1941 {
1942 *area = RIGHT_MARGIN_AREA;
1943 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1944 }
1945 }
1946
1947 /* Find glyph containing X. */
1948 glyph = row->glyphs[*area];
1949 end = glyph + row->used[*area];
1950 x -= x0;
1951 while (glyph < end && x >= glyph->pixel_width)
1952 {
1953 x -= glyph->pixel_width;
1954 ++glyph;
1955 }
1956
1957 if (glyph == end)
1958 return NULL;
1959
1960 if (dx)
1961 {
1962 *dx = x;
1963 *dy = y - (row->y + row->ascent - glyph->ascent);
1964 }
1965
1966 *hpos = glyph - row->glyphs[*area];
1967 return glyph;
1968 }
1969
1970 /* Convert frame-relative x/y to coordinates relative to window W.
1971 Takes pseudo-windows into account. */
1972
1973 static void
1974 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1975 {
1976 if (w->pseudo_window_p)
1977 {
1978 /* A pseudo-window is always full-width, and starts at the
1979 left edge of the frame, plus a frame border. */
1980 struct frame *f = XFRAME (w->frame);
1981 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1982 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1983 }
1984 else
1985 {
1986 *x -= WINDOW_LEFT_EDGE_X (w);
1987 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1988 }
1989 }
1990
1991 #ifdef HAVE_WINDOW_SYSTEM
1992
1993 /* EXPORT:
1994 Return in RECTS[] at most N clipping rectangles for glyph string S.
1995 Return the number of stored rectangles. */
1996
1997 int
1998 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1999 {
2000 XRectangle r;
2001
2002 if (n <= 0)
2003 return 0;
2004
2005 if (s->row->full_width_p)
2006 {
2007 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2008 r.x = WINDOW_LEFT_EDGE_X (s->w);
2009 if (s->row->mode_line_p)
2010 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2011 else
2012 r.width = WINDOW_PIXEL_WIDTH (s->w);
2013
2014 /* Unless displaying a mode or menu bar line, which are always
2015 fully visible, clip to the visible part of the row. */
2016 if (s->w->pseudo_window_p)
2017 r.height = s->row->visible_height;
2018 else
2019 r.height = s->height;
2020 }
2021 else
2022 {
2023 /* This is a text line that may be partially visible. */
2024 r.x = window_box_left (s->w, s->area);
2025 r.width = window_box_width (s->w, s->area);
2026 r.height = s->row->visible_height;
2027 }
2028
2029 if (s->clip_head)
2030 if (r.x < s->clip_head->x)
2031 {
2032 if (r.width >= s->clip_head->x - r.x)
2033 r.width -= s->clip_head->x - r.x;
2034 else
2035 r.width = 0;
2036 r.x = s->clip_head->x;
2037 }
2038 if (s->clip_tail)
2039 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2040 {
2041 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2042 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2043 else
2044 r.width = 0;
2045 }
2046
2047 /* If S draws overlapping rows, it's sufficient to use the top and
2048 bottom of the window for clipping because this glyph string
2049 intentionally draws over other lines. */
2050 if (s->for_overlaps)
2051 {
2052 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2053 r.height = window_text_bottom_y (s->w) - r.y;
2054
2055 /* Alas, the above simple strategy does not work for the
2056 environments with anti-aliased text: if the same text is
2057 drawn onto the same place multiple times, it gets thicker.
2058 If the overlap we are processing is for the erased cursor, we
2059 take the intersection with the rectangle of the cursor. */
2060 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2061 {
2062 XRectangle rc, r_save = r;
2063
2064 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2065 rc.y = s->w->phys_cursor.y;
2066 rc.width = s->w->phys_cursor_width;
2067 rc.height = s->w->phys_cursor_height;
2068
2069 x_intersect_rectangles (&r_save, &rc, &r);
2070 }
2071 }
2072 else
2073 {
2074 /* Don't use S->y for clipping because it doesn't take partially
2075 visible lines into account. For example, it can be negative for
2076 partially visible lines at the top of a window. */
2077 if (!s->row->full_width_p
2078 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2079 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2080 else
2081 r.y = max (0, s->row->y);
2082 }
2083
2084 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2085
2086 /* If drawing the cursor, don't let glyph draw outside its
2087 advertised boundaries. Cleartype does this under some circumstances. */
2088 if (s->hl == DRAW_CURSOR)
2089 {
2090 struct glyph *glyph = s->first_glyph;
2091 int height, max_y;
2092
2093 if (s->x > r.x)
2094 {
2095 if (r.width >= s->x - r.x)
2096 r.width -= s->x - r.x;
2097 else /* R2L hscrolled row with cursor outside text area */
2098 r.width = 0;
2099 r.x = s->x;
2100 }
2101 r.width = min (r.width, glyph->pixel_width);
2102
2103 /* If r.y is below window bottom, ensure that we still see a cursor. */
2104 height = min (glyph->ascent + glyph->descent,
2105 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2106 max_y = window_text_bottom_y (s->w) - height;
2107 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2108 if (s->ybase - glyph->ascent > max_y)
2109 {
2110 r.y = max_y;
2111 r.height = height;
2112 }
2113 else
2114 {
2115 /* Don't draw cursor glyph taller than our actual glyph. */
2116 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2117 if (height < r.height)
2118 {
2119 max_y = r.y + r.height;
2120 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2121 r.height = min (max_y - r.y, height);
2122 }
2123 }
2124 }
2125
2126 if (s->row->clip)
2127 {
2128 XRectangle r_save = r;
2129
2130 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2131 r.width = 0;
2132 }
2133
2134 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2135 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2136 {
2137 #ifdef CONVERT_FROM_XRECT
2138 CONVERT_FROM_XRECT (r, *rects);
2139 #else
2140 *rects = r;
2141 #endif
2142 return 1;
2143 }
2144 else
2145 {
2146 /* If we are processing overlapping and allowed to return
2147 multiple clipping rectangles, we exclude the row of the glyph
2148 string from the clipping rectangle. This is to avoid drawing
2149 the same text on the environment with anti-aliasing. */
2150 #ifdef CONVERT_FROM_XRECT
2151 XRectangle rs[2];
2152 #else
2153 XRectangle *rs = rects;
2154 #endif
2155 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2156
2157 if (s->for_overlaps & OVERLAPS_PRED)
2158 {
2159 rs[i] = r;
2160 if (r.y + r.height > row_y)
2161 {
2162 if (r.y < row_y)
2163 rs[i].height = row_y - r.y;
2164 else
2165 rs[i].height = 0;
2166 }
2167 i++;
2168 }
2169 if (s->for_overlaps & OVERLAPS_SUCC)
2170 {
2171 rs[i] = r;
2172 if (r.y < row_y + s->row->visible_height)
2173 {
2174 if (r.y + r.height > row_y + s->row->visible_height)
2175 {
2176 rs[i].y = row_y + s->row->visible_height;
2177 rs[i].height = r.y + r.height - rs[i].y;
2178 }
2179 else
2180 rs[i].height = 0;
2181 }
2182 i++;
2183 }
2184
2185 n = i;
2186 #ifdef CONVERT_FROM_XRECT
2187 for (i = 0; i < n; i++)
2188 CONVERT_FROM_XRECT (rs[i], rects[i]);
2189 #endif
2190 return n;
2191 }
2192 }
2193
2194 /* EXPORT:
2195 Return in *NR the clipping rectangle for glyph string S. */
2196
2197 void
2198 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2199 {
2200 get_glyph_string_clip_rects (s, nr, 1);
2201 }
2202
2203
2204 /* EXPORT:
2205 Return the position and height of the phys cursor in window W.
2206 Set w->phys_cursor_width to width of phys cursor.
2207 */
2208
2209 void
2210 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2211 struct glyph *glyph, int *xp, int *yp, int *heightp)
2212 {
2213 struct frame *f = XFRAME (WINDOW_FRAME (w));
2214 int x, y, wd, h, h0, y0, ascent;
2215
2216 /* Compute the width of the rectangle to draw. If on a stretch
2217 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2218 rectangle as wide as the glyph, but use a canonical character
2219 width instead. */
2220 wd = glyph->pixel_width;
2221
2222 x = w->phys_cursor.x;
2223 if (x < 0)
2224 {
2225 wd += x;
2226 x = 0;
2227 }
2228
2229 if (glyph->type == STRETCH_GLYPH
2230 && !x_stretch_cursor_p)
2231 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2232 w->phys_cursor_width = wd;
2233
2234 /* Don't let the hollow cursor glyph descend below the glyph row's
2235 ascent value, lest the hollow cursor looks funny. */
2236 y = w->phys_cursor.y;
2237 ascent = row->ascent;
2238 if (row->ascent < glyph->ascent)
2239 {
2240 y =- glyph->ascent - row->ascent;
2241 ascent = glyph->ascent;
2242 }
2243
2244 /* If y is below window bottom, ensure that we still see a cursor. */
2245 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2246
2247 h = max (h0, ascent + glyph->descent);
2248 h0 = min (h0, ascent + glyph->descent);
2249
2250 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2251 if (y < y0)
2252 {
2253 h = max (h - (y0 - y) + 1, h0);
2254 y = y0 - 1;
2255 }
2256 else
2257 {
2258 y0 = window_text_bottom_y (w) - h0;
2259 if (y > y0)
2260 {
2261 h += y - y0;
2262 y = y0;
2263 }
2264 }
2265
2266 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2267 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2268 *heightp = h;
2269 }
2270
2271 /*
2272 * Remember which glyph the mouse is over.
2273 */
2274
2275 void
2276 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2277 {
2278 Lisp_Object window;
2279 struct window *w;
2280 struct glyph_row *r, *gr, *end_row;
2281 enum window_part part;
2282 enum glyph_row_area area;
2283 int x, y, width, height;
2284
2285 /* Try to determine frame pixel position and size of the glyph under
2286 frame pixel coordinates X/Y on frame F. */
2287
2288 if (window_resize_pixelwise)
2289 {
2290 width = height = 1;
2291 goto virtual_glyph;
2292 }
2293 else if (!f->glyphs_initialized_p
2294 || (window = window_from_coordinates (f, gx, gy, &part, false),
2295 NILP (window)))
2296 {
2297 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2298 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2299 goto virtual_glyph;
2300 }
2301
2302 w = XWINDOW (window);
2303 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2304 height = WINDOW_FRAME_LINE_HEIGHT (w);
2305
2306 x = window_relative_x_coord (w, part, gx);
2307 y = gy - WINDOW_TOP_EDGE_Y (w);
2308
2309 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2310 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2311
2312 if (w->pseudo_window_p)
2313 {
2314 area = TEXT_AREA;
2315 part = ON_MODE_LINE; /* Don't adjust margin. */
2316 goto text_glyph;
2317 }
2318
2319 switch (part)
2320 {
2321 case ON_LEFT_MARGIN:
2322 area = LEFT_MARGIN_AREA;
2323 goto text_glyph;
2324
2325 case ON_RIGHT_MARGIN:
2326 area = RIGHT_MARGIN_AREA;
2327 goto text_glyph;
2328
2329 case ON_HEADER_LINE:
2330 case ON_MODE_LINE:
2331 gr = (part == ON_HEADER_LINE
2332 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2333 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2334 gy = gr->y;
2335 area = TEXT_AREA;
2336 goto text_glyph_row_found;
2337
2338 case ON_TEXT:
2339 area = TEXT_AREA;
2340
2341 text_glyph:
2342 gr = 0; gy = 0;
2343 for (; r <= end_row && r->enabled_p; ++r)
2344 if (r->y + r->height > y)
2345 {
2346 gr = r; gy = r->y;
2347 break;
2348 }
2349
2350 text_glyph_row_found:
2351 if (gr && gy <= y)
2352 {
2353 struct glyph *g = gr->glyphs[area];
2354 struct glyph *end = g + gr->used[area];
2355
2356 height = gr->height;
2357 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2358 if (gx + g->pixel_width > x)
2359 break;
2360
2361 if (g < end)
2362 {
2363 if (g->type == IMAGE_GLYPH)
2364 {
2365 /* Don't remember when mouse is over image, as
2366 image may have hot-spots. */
2367 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2368 return;
2369 }
2370 width = g->pixel_width;
2371 }
2372 else
2373 {
2374 /* Use nominal char spacing at end of line. */
2375 x -= gx;
2376 gx += (x / width) * width;
2377 }
2378
2379 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2380 {
2381 gx += window_box_left_offset (w, area);
2382 /* Don't expand over the modeline to make sure the vertical
2383 drag cursor is shown early enough. */
2384 height = min (height,
2385 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2386 }
2387 }
2388 else
2389 {
2390 /* Use nominal line height at end of window. */
2391 gx = (x / width) * width;
2392 y -= gy;
2393 gy += (y / height) * height;
2394 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2395 /* See comment above. */
2396 height = min (height,
2397 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2398 }
2399 break;
2400
2401 case ON_LEFT_FRINGE:
2402 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2403 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2404 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2405 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2406 goto row_glyph;
2407
2408 case ON_RIGHT_FRINGE:
2409 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2410 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2411 : window_box_right_offset (w, TEXT_AREA));
2412 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2413 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2414 && !WINDOW_RIGHTMOST_P (w))
2415 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2416 /* Make sure the vertical border can get her own glyph to the
2417 right of the one we build here. */
2418 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2419 else
2420 width = WINDOW_PIXEL_WIDTH (w) - gx;
2421 else
2422 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2423
2424 goto row_glyph;
2425
2426 case ON_VERTICAL_BORDER:
2427 gx = WINDOW_PIXEL_WIDTH (w) - width;
2428 goto row_glyph;
2429
2430 case ON_VERTICAL_SCROLL_BAR:
2431 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2432 ? 0
2433 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2434 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2435 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2436 : 0)));
2437 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2438
2439 row_glyph:
2440 gr = 0, gy = 0;
2441 for (; r <= end_row && r->enabled_p; ++r)
2442 if (r->y + r->height > y)
2443 {
2444 gr = r; gy = r->y;
2445 break;
2446 }
2447
2448 if (gr && gy <= y)
2449 height = gr->height;
2450 else
2451 {
2452 /* Use nominal line height at end of window. */
2453 y -= gy;
2454 gy += (y / height) * height;
2455 }
2456 break;
2457
2458 case ON_RIGHT_DIVIDER:
2459 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2460 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2461 gy = 0;
2462 /* The bottom divider prevails. */
2463 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2464 goto add_edge;
2465
2466 case ON_BOTTOM_DIVIDER:
2467 gx = 0;
2468 width = WINDOW_PIXEL_WIDTH (w);
2469 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2470 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2471 goto add_edge;
2472
2473 default:
2474 ;
2475 virtual_glyph:
2476 /* If there is no glyph under the mouse, then we divide the screen
2477 into a grid of the smallest glyph in the frame, and use that
2478 as our "glyph". */
2479
2480 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2481 round down even for negative values. */
2482 if (gx < 0)
2483 gx -= width - 1;
2484 if (gy < 0)
2485 gy -= height - 1;
2486
2487 gx = (gx / width) * width;
2488 gy = (gy / height) * height;
2489
2490 goto store_rect;
2491 }
2492
2493 add_edge:
2494 gx += WINDOW_LEFT_EDGE_X (w);
2495 gy += WINDOW_TOP_EDGE_Y (w);
2496
2497 store_rect:
2498 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2499
2500 /* Visible feedback for debugging. */
2501 #if false && defined HAVE_X_WINDOWS
2502 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2503 f->output_data.x->normal_gc,
2504 gx, gy, width, height);
2505 #endif
2506 }
2507
2508
2509 #endif /* HAVE_WINDOW_SYSTEM */
2510
2511 static void
2512 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2513 {
2514 eassert (w);
2515 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2516 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2517 w->window_end_vpos
2518 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2519 }
2520
2521 /***********************************************************************
2522 Lisp form evaluation
2523 ***********************************************************************/
2524
2525 /* Error handler for safe_eval and safe_call. */
2526
2527 static Lisp_Object
2528 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2529 {
2530 add_to_log ("Error during redisplay: %S signaled %S",
2531 Flist (nargs, args), arg);
2532 return Qnil;
2533 }
2534
2535 /* Call function FUNC with the rest of NARGS - 1 arguments
2536 following. Return the result, or nil if something went
2537 wrong. Prevent redisplay during the evaluation. */
2538
2539 static Lisp_Object
2540 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2541 {
2542 Lisp_Object val;
2543
2544 if (inhibit_eval_during_redisplay)
2545 val = Qnil;
2546 else
2547 {
2548 ptrdiff_t i;
2549 ptrdiff_t count = SPECPDL_INDEX ();
2550 Lisp_Object *args;
2551 USE_SAFE_ALLOCA;
2552 SAFE_ALLOCA_LISP (args, nargs);
2553
2554 args[0] = func;
2555 for (i = 1; i < nargs; i++)
2556 args[i] = va_arg (ap, Lisp_Object);
2557
2558 specbind (Qinhibit_redisplay, Qt);
2559 if (inhibit_quit)
2560 specbind (Qinhibit_quit, Qt);
2561 /* Use Qt to ensure debugger does not run,
2562 so there is no possibility of wanting to redisplay. */
2563 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2564 safe_eval_handler);
2565 SAFE_FREE ();
2566 val = unbind_to (count, val);
2567 }
2568
2569 return val;
2570 }
2571
2572 Lisp_Object
2573 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2574 {
2575 Lisp_Object retval;
2576 va_list ap;
2577
2578 va_start (ap, func);
2579 retval = safe__call (false, nargs, func, ap);
2580 va_end (ap);
2581 return retval;
2582 }
2583
2584 /* Call function FN with one argument ARG.
2585 Return the result, or nil if something went wrong. */
2586
2587 Lisp_Object
2588 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2589 {
2590 return safe_call (2, fn, arg);
2591 }
2592
2593 static Lisp_Object
2594 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2595 {
2596 Lisp_Object retval;
2597 va_list ap;
2598
2599 va_start (ap, fn);
2600 retval = safe__call (inhibit_quit, 2, fn, ap);
2601 va_end (ap);
2602 return retval;
2603 }
2604
2605 Lisp_Object
2606 safe_eval (Lisp_Object sexpr)
2607 {
2608 return safe__call1 (false, Qeval, sexpr);
2609 }
2610
2611 static Lisp_Object
2612 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2613 {
2614 return safe__call1 (inhibit_quit, Qeval, sexpr);
2615 }
2616
2617 /* Call function FN with two arguments ARG1 and ARG2.
2618 Return the result, or nil if something went wrong. */
2619
2620 Lisp_Object
2621 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2622 {
2623 return safe_call (3, fn, arg1, arg2);
2624 }
2625
2626
2627 \f
2628 /***********************************************************************
2629 Debugging
2630 ***********************************************************************/
2631
2632 /* Define CHECK_IT to perform sanity checks on iterators.
2633 This is for debugging. It is too slow to do unconditionally. */
2634
2635 static void
2636 CHECK_IT (struct it *it)
2637 {
2638 #if false
2639 if (it->method == GET_FROM_STRING)
2640 {
2641 eassert (STRINGP (it->string));
2642 eassert (IT_STRING_CHARPOS (*it) >= 0);
2643 }
2644 else
2645 {
2646 eassert (IT_STRING_CHARPOS (*it) < 0);
2647 if (it->method == GET_FROM_BUFFER)
2648 {
2649 /* Check that character and byte positions agree. */
2650 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2651 }
2652 }
2653
2654 if (it->dpvec)
2655 eassert (it->current.dpvec_index >= 0);
2656 else
2657 eassert (it->current.dpvec_index < 0);
2658 #endif
2659 }
2660
2661
2662 /* Check that the window end of window W is what we expect it
2663 to be---the last row in the current matrix displaying text. */
2664
2665 static void
2666 CHECK_WINDOW_END (struct window *w)
2667 {
2668 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2669 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2670 {
2671 struct glyph_row *row;
2672 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2673 !row->enabled_p
2674 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2675 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2676 }
2677 #endif
2678 }
2679
2680 /***********************************************************************
2681 Iterator initialization
2682 ***********************************************************************/
2683
2684 /* Initialize IT for displaying current_buffer in window W, starting
2685 at character position CHARPOS. CHARPOS < 0 means that no buffer
2686 position is specified which is useful when the iterator is assigned
2687 a position later. BYTEPOS is the byte position corresponding to
2688 CHARPOS.
2689
2690 If ROW is not null, calls to produce_glyphs with IT as parameter
2691 will produce glyphs in that row.
2692
2693 BASE_FACE_ID is the id of a base face to use. It must be one of
2694 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2695 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2696 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2697
2698 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2699 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2700 will be initialized to use the corresponding mode line glyph row of
2701 the desired matrix of W. */
2702
2703 void
2704 init_iterator (struct it *it, struct window *w,
2705 ptrdiff_t charpos, ptrdiff_t bytepos,
2706 struct glyph_row *row, enum face_id base_face_id)
2707 {
2708 enum face_id remapped_base_face_id = base_face_id;
2709
2710 /* Some precondition checks. */
2711 eassert (w != NULL && it != NULL);
2712 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2713 && charpos <= ZV));
2714
2715 /* If face attributes have been changed since the last redisplay,
2716 free realized faces now because they depend on face definitions
2717 that might have changed. Don't free faces while there might be
2718 desired matrices pending which reference these faces. */
2719 if (!inhibit_free_realized_faces)
2720 {
2721 if (face_change)
2722 {
2723 face_change = false;
2724 free_all_realized_faces (Qnil);
2725 }
2726 else if (XFRAME (w->frame)->face_change)
2727 {
2728 XFRAME (w->frame)->face_change = 0;
2729 free_all_realized_faces (w->frame);
2730 }
2731 }
2732
2733 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2734 if (! NILP (Vface_remapping_alist))
2735 remapped_base_face_id
2736 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2737
2738 /* Use one of the mode line rows of W's desired matrix if
2739 appropriate. */
2740 if (row == NULL)
2741 {
2742 if (base_face_id == MODE_LINE_FACE_ID
2743 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2744 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2745 else if (base_face_id == HEADER_LINE_FACE_ID)
2746 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2747 }
2748
2749 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2750 Other parts of redisplay rely on that. */
2751 memclear (it, sizeof *it);
2752 it->current.overlay_string_index = -1;
2753 it->current.dpvec_index = -1;
2754 it->base_face_id = remapped_base_face_id;
2755 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2756 it->paragraph_embedding = L2R;
2757 it->bidi_it.w = w;
2758
2759 /* The window in which we iterate over current_buffer: */
2760 XSETWINDOW (it->window, w);
2761 it->w = w;
2762 it->f = XFRAME (w->frame);
2763
2764 it->cmp_it.id = -1;
2765
2766 /* Extra space between lines (on window systems only). */
2767 if (base_face_id == DEFAULT_FACE_ID
2768 && FRAME_WINDOW_P (it->f))
2769 {
2770 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2771 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2772 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2773 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2774 * FRAME_LINE_HEIGHT (it->f));
2775 else if (it->f->extra_line_spacing > 0)
2776 it->extra_line_spacing = it->f->extra_line_spacing;
2777 }
2778
2779 /* If realized faces have been removed, e.g. because of face
2780 attribute changes of named faces, recompute them. When running
2781 in batch mode, the face cache of the initial frame is null. If
2782 we happen to get called, make a dummy face cache. */
2783 if (FRAME_FACE_CACHE (it->f) == NULL)
2784 init_frame_faces (it->f);
2785 if (FRAME_FACE_CACHE (it->f)->used == 0)
2786 recompute_basic_faces (it->f);
2787
2788 it->override_ascent = -1;
2789
2790 /* Are control characters displayed as `^C'? */
2791 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2792
2793 /* -1 means everything between a CR and the following line end
2794 is invisible. >0 means lines indented more than this value are
2795 invisible. */
2796 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2797 ? (clip_to_bounds
2798 (-1, XINT (BVAR (current_buffer, selective_display)),
2799 PTRDIFF_MAX))
2800 : (!NILP (BVAR (current_buffer, selective_display))
2801 ? -1 : 0));
2802 it->selective_display_ellipsis_p
2803 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2804
2805 /* Display table to use. */
2806 it->dp = window_display_table (w);
2807
2808 /* Are multibyte characters enabled in current_buffer? */
2809 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2810
2811 /* Get the position at which the redisplay_end_trigger hook should
2812 be run, if it is to be run at all. */
2813 if (MARKERP (w->redisplay_end_trigger)
2814 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2815 it->redisplay_end_trigger_charpos
2816 = marker_position (w->redisplay_end_trigger);
2817 else if (INTEGERP (w->redisplay_end_trigger))
2818 it->redisplay_end_trigger_charpos
2819 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2820 PTRDIFF_MAX);
2821
2822 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2823
2824 /* Are lines in the display truncated? */
2825 if (TRUNCATE != 0)
2826 it->line_wrap = TRUNCATE;
2827 if (base_face_id == DEFAULT_FACE_ID
2828 && !it->w->hscroll
2829 && (WINDOW_FULL_WIDTH_P (it->w)
2830 || NILP (Vtruncate_partial_width_windows)
2831 || (INTEGERP (Vtruncate_partial_width_windows)
2832 /* PXW: Shall we do something about this? */
2833 && (XINT (Vtruncate_partial_width_windows)
2834 <= WINDOW_TOTAL_COLS (it->w))))
2835 && NILP (BVAR (current_buffer, truncate_lines)))
2836 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2837 ? WINDOW_WRAP : WORD_WRAP;
2838
2839 /* Get dimensions of truncation and continuation glyphs. These are
2840 displayed as fringe bitmaps under X, but we need them for such
2841 frames when the fringes are turned off. But leave the dimensions
2842 zero for tooltip frames, as these glyphs look ugly there and also
2843 sabotage calculations of tooltip dimensions in x-show-tip. */
2844 #ifdef HAVE_WINDOW_SYSTEM
2845 if (!(FRAME_WINDOW_P (it->f)
2846 && FRAMEP (tip_frame)
2847 && it->f == XFRAME (tip_frame)))
2848 #endif
2849 {
2850 if (it->line_wrap == TRUNCATE)
2851 {
2852 /* We will need the truncation glyph. */
2853 eassert (it->glyph_row == NULL);
2854 produce_special_glyphs (it, IT_TRUNCATION);
2855 it->truncation_pixel_width = it->pixel_width;
2856 }
2857 else
2858 {
2859 /* We will need the continuation glyph. */
2860 eassert (it->glyph_row == NULL);
2861 produce_special_glyphs (it, IT_CONTINUATION);
2862 it->continuation_pixel_width = it->pixel_width;
2863 }
2864 }
2865
2866 /* Reset these values to zero because the produce_special_glyphs
2867 above has changed them. */
2868 it->pixel_width = it->ascent = it->descent = 0;
2869 it->phys_ascent = it->phys_descent = 0;
2870
2871 /* Set this after getting the dimensions of truncation and
2872 continuation glyphs, so that we don't produce glyphs when calling
2873 produce_special_glyphs, above. */
2874 it->glyph_row = row;
2875 it->area = TEXT_AREA;
2876
2877 /* Get the dimensions of the display area. The display area
2878 consists of the visible window area plus a horizontally scrolled
2879 part to the left of the window. All x-values are relative to the
2880 start of this total display area. */
2881 if (base_face_id != DEFAULT_FACE_ID)
2882 {
2883 /* Mode lines, menu bar in terminal frames. */
2884 it->first_visible_x = 0;
2885 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2886 }
2887 else
2888 {
2889 it->first_visible_x
2890 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2891 it->last_visible_x = (it->first_visible_x
2892 + window_box_width (w, TEXT_AREA));
2893
2894 /* If we truncate lines, leave room for the truncation glyph(s) at
2895 the right margin. Otherwise, leave room for the continuation
2896 glyph(s). Done only if the window has no right fringe. */
2897 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2898 {
2899 if (it->line_wrap == TRUNCATE)
2900 it->last_visible_x -= it->truncation_pixel_width;
2901 else
2902 it->last_visible_x -= it->continuation_pixel_width;
2903 }
2904
2905 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2906 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2907 }
2908
2909 /* Leave room for a border glyph. */
2910 if (!FRAME_WINDOW_P (it->f)
2911 && !WINDOW_RIGHTMOST_P (it->w))
2912 it->last_visible_x -= 1;
2913
2914 it->last_visible_y = window_text_bottom_y (w);
2915
2916 /* For mode lines and alike, arrange for the first glyph having a
2917 left box line if the face specifies a box. */
2918 if (base_face_id != DEFAULT_FACE_ID)
2919 {
2920 struct face *face;
2921
2922 it->face_id = remapped_base_face_id;
2923
2924 /* If we have a boxed mode line, make the first character appear
2925 with a left box line. */
2926 face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
2927 if (face && face->box != FACE_NO_BOX)
2928 it->start_of_box_run_p = true;
2929 }
2930
2931 /* If a buffer position was specified, set the iterator there,
2932 getting overlays and face properties from that position. */
2933 if (charpos >= BUF_BEG (current_buffer))
2934 {
2935 it->stop_charpos = charpos;
2936 it->end_charpos = ZV;
2937 eassert (charpos == BYTE_TO_CHAR (bytepos));
2938 IT_CHARPOS (*it) = charpos;
2939 IT_BYTEPOS (*it) = bytepos;
2940
2941 /* We will rely on `reseat' to set this up properly, via
2942 handle_face_prop. */
2943 it->face_id = it->base_face_id;
2944
2945 it->start = it->current;
2946 /* Do we need to reorder bidirectional text? Not if this is a
2947 unibyte buffer: by definition, none of the single-byte
2948 characters are strong R2L, so no reordering is needed. And
2949 bidi.c doesn't support unibyte buffers anyway. Also, don't
2950 reorder while we are loading loadup.el, since the tables of
2951 character properties needed for reordering are not yet
2952 available. */
2953 it->bidi_p =
2954 !redisplay__inhibit_bidi
2955 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2956 && it->multibyte_p;
2957
2958 /* If we are to reorder bidirectional text, init the bidi
2959 iterator. */
2960 if (it->bidi_p)
2961 {
2962 /* Since we don't know at this point whether there will be
2963 any R2L lines in the window, we reserve space for
2964 truncation/continuation glyphs even if only the left
2965 fringe is absent. */
2966 if (base_face_id == DEFAULT_FACE_ID
2967 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2968 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2969 {
2970 if (it->line_wrap == TRUNCATE)
2971 it->last_visible_x -= it->truncation_pixel_width;
2972 else
2973 it->last_visible_x -= it->continuation_pixel_width;
2974 }
2975 /* Note the paragraph direction that this buffer wants to
2976 use. */
2977 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2978 Qleft_to_right))
2979 it->paragraph_embedding = L2R;
2980 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2981 Qright_to_left))
2982 it->paragraph_embedding = R2L;
2983 else
2984 it->paragraph_embedding = NEUTRAL_DIR;
2985 bidi_unshelve_cache (NULL, false);
2986 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2987 &it->bidi_it);
2988 }
2989
2990 /* Compute faces etc. */
2991 reseat (it, it->current.pos, true);
2992 }
2993
2994 CHECK_IT (it);
2995 }
2996
2997
2998 /* Initialize IT for the display of window W with window start POS. */
2999
3000 void
3001 start_display (struct it *it, struct window *w, struct text_pos pos)
3002 {
3003 struct glyph_row *row;
3004 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
3005
3006 row = w->desired_matrix->rows + first_vpos;
3007 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3008 it->first_vpos = first_vpos;
3009
3010 /* Don't reseat to previous visible line start if current start
3011 position is in a string or image. */
3012 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3013 {
3014 int first_y = it->current_y;
3015
3016 /* If window start is not at a line start, skip forward to POS to
3017 get the correct continuation lines width. */
3018 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3019 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3020 if (!start_at_line_beg_p)
3021 {
3022 int new_x;
3023
3024 reseat_at_previous_visible_line_start (it);
3025 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3026
3027 new_x = it->current_x + it->pixel_width;
3028
3029 /* If lines are continued, this line may end in the middle
3030 of a multi-glyph character (e.g. a control character
3031 displayed as \003, or in the middle of an overlay
3032 string). In this case move_it_to above will not have
3033 taken us to the start of the continuation line but to the
3034 end of the continued line. */
3035 if (it->current_x > 0
3036 && it->line_wrap != TRUNCATE /* Lines are continued. */
3037 && (/* And glyph doesn't fit on the line. */
3038 new_x > it->last_visible_x
3039 /* Or it fits exactly and we're on a window
3040 system frame. */
3041 || (new_x == it->last_visible_x
3042 && FRAME_WINDOW_P (it->f)
3043 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3044 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3045 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3046 {
3047 if ((it->current.dpvec_index >= 0
3048 || it->current.overlay_string_index >= 0)
3049 /* If we are on a newline from a display vector or
3050 overlay string, then we are already at the end of
3051 a screen line; no need to go to the next line in
3052 that case, as this line is not really continued.
3053 (If we do go to the next line, C-e will not DTRT.) */
3054 && it->c != '\n')
3055 {
3056 set_iterator_to_next (it, true);
3057 move_it_in_display_line_to (it, -1, -1, 0);
3058 }
3059
3060 it->continuation_lines_width += it->current_x;
3061 }
3062 /* If the character at POS is displayed via a display
3063 vector, move_it_to above stops at the final glyph of
3064 IT->dpvec. To make the caller redisplay that character
3065 again (a.k.a. start at POS), we need to reset the
3066 dpvec_index to the beginning of IT->dpvec. */
3067 else if (it->current.dpvec_index >= 0)
3068 it->current.dpvec_index = 0;
3069
3070 /* We're starting a new display line, not affected by the
3071 height of the continued line, so clear the appropriate
3072 fields in the iterator structure. */
3073 it->max_ascent = it->max_descent = 0;
3074 it->max_phys_ascent = it->max_phys_descent = 0;
3075
3076 it->current_y = first_y;
3077 it->vpos = 0;
3078 it->current_x = it->hpos = 0;
3079 }
3080 }
3081 }
3082
3083
3084 /* Return true if POS is a position in ellipses displayed for invisible
3085 text. W is the window we display, for text property lookup. */
3086
3087 static bool
3088 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3089 {
3090 Lisp_Object prop, window;
3091 bool ellipses_p = false;
3092 ptrdiff_t charpos = CHARPOS (pos->pos);
3093
3094 /* If POS specifies a position in a display vector, this might
3095 be for an ellipsis displayed for invisible text. We won't
3096 get the iterator set up for delivering that ellipsis unless
3097 we make sure that it gets aware of the invisible text. */
3098 if (pos->dpvec_index >= 0
3099 && pos->overlay_string_index < 0
3100 && CHARPOS (pos->string_pos) < 0
3101 && charpos > BEGV
3102 && (XSETWINDOW (window, w),
3103 prop = Fget_char_property (make_number (charpos),
3104 Qinvisible, window),
3105 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3106 {
3107 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3108 window);
3109 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3110 }
3111
3112 return ellipses_p;
3113 }
3114
3115
3116 /* Initialize IT for stepping through current_buffer in window W,
3117 starting at position POS that includes overlay string and display
3118 vector/ control character translation position information. Value
3119 is false if there are overlay strings with newlines at POS. */
3120
3121 static bool
3122 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3123 {
3124 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3125 int i;
3126 bool overlay_strings_with_newlines = false;
3127
3128 /* If POS specifies a position in a display vector, this might
3129 be for an ellipsis displayed for invisible text. We won't
3130 get the iterator set up for delivering that ellipsis unless
3131 we make sure that it gets aware of the invisible text. */
3132 if (in_ellipses_for_invisible_text_p (pos, w))
3133 {
3134 --charpos;
3135 bytepos = 0;
3136 }
3137
3138 /* Keep in mind: the call to reseat in init_iterator skips invisible
3139 text, so we might end up at a position different from POS. This
3140 is only a problem when POS is a row start after a newline and an
3141 overlay starts there with an after-string, and the overlay has an
3142 invisible property. Since we don't skip invisible text in
3143 display_line and elsewhere immediately after consuming the
3144 newline before the row start, such a POS will not be in a string,
3145 but the call to init_iterator below will move us to the
3146 after-string. */
3147 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3148
3149 /* This only scans the current chunk -- it should scan all chunks.
3150 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3151 to 16 in 22.1 to make this a lesser problem. */
3152 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3153 {
3154 const char *s = SSDATA (it->overlay_strings[i]);
3155 const char *e = s + SBYTES (it->overlay_strings[i]);
3156
3157 while (s < e && *s != '\n')
3158 ++s;
3159
3160 if (s < e)
3161 {
3162 overlay_strings_with_newlines = true;
3163 break;
3164 }
3165 }
3166
3167 /* If position is within an overlay string, set up IT to the right
3168 overlay string. */
3169 if (pos->overlay_string_index >= 0)
3170 {
3171 int relative_index;
3172
3173 /* If the first overlay string happens to have a `display'
3174 property for an image, the iterator will be set up for that
3175 image, and we have to undo that setup first before we can
3176 correct the overlay string index. */
3177 if (it->method == GET_FROM_IMAGE)
3178 pop_it (it);
3179
3180 /* We already have the first chunk of overlay strings in
3181 IT->overlay_strings. Load more until the one for
3182 pos->overlay_string_index is in IT->overlay_strings. */
3183 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3184 {
3185 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3186 it->current.overlay_string_index = 0;
3187 while (n--)
3188 {
3189 load_overlay_strings (it, 0);
3190 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3191 }
3192 }
3193
3194 it->current.overlay_string_index = pos->overlay_string_index;
3195 relative_index = (it->current.overlay_string_index
3196 % OVERLAY_STRING_CHUNK_SIZE);
3197 it->string = it->overlay_strings[relative_index];
3198 eassert (STRINGP (it->string));
3199 it->current.string_pos = pos->string_pos;
3200 it->method = GET_FROM_STRING;
3201 it->end_charpos = SCHARS (it->string);
3202 /* Set up the bidi iterator for this overlay string. */
3203 if (it->bidi_p)
3204 {
3205 it->bidi_it.string.lstring = it->string;
3206 it->bidi_it.string.s = NULL;
3207 it->bidi_it.string.schars = SCHARS (it->string);
3208 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3209 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3210 it->bidi_it.string.unibyte = !it->multibyte_p;
3211 it->bidi_it.w = it->w;
3212 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3213 FRAME_WINDOW_P (it->f), &it->bidi_it);
3214
3215 /* Synchronize the state of the bidi iterator with
3216 pos->string_pos. For any string position other than
3217 zero, this will be done automagically when we resume
3218 iteration over the string and get_visually_first_element
3219 is called. But if string_pos is zero, and the string is
3220 to be reordered for display, we need to resync manually,
3221 since it could be that the iteration state recorded in
3222 pos ended at string_pos of 0 moving backwards in string. */
3223 if (CHARPOS (pos->string_pos) == 0)
3224 {
3225 get_visually_first_element (it);
3226 if (IT_STRING_CHARPOS (*it) != 0)
3227 do {
3228 /* Paranoia. */
3229 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3230 bidi_move_to_visually_next (&it->bidi_it);
3231 } while (it->bidi_it.charpos != 0);
3232 }
3233 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3234 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3235 }
3236 }
3237
3238 if (CHARPOS (pos->string_pos) >= 0)
3239 {
3240 /* Recorded position is not in an overlay string, but in another
3241 string. This can only be a string from a `display' property.
3242 IT should already be filled with that string. */
3243 it->current.string_pos = pos->string_pos;
3244 eassert (STRINGP (it->string));
3245 if (it->bidi_p)
3246 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3247 FRAME_WINDOW_P (it->f), &it->bidi_it);
3248 }
3249
3250 /* Restore position in display vector translations, control
3251 character translations or ellipses. */
3252 if (pos->dpvec_index >= 0)
3253 {
3254 if (it->dpvec == NULL)
3255 get_next_display_element (it);
3256 eassert (it->dpvec && it->current.dpvec_index == 0);
3257 it->current.dpvec_index = pos->dpvec_index;
3258 }
3259
3260 CHECK_IT (it);
3261 return !overlay_strings_with_newlines;
3262 }
3263
3264
3265 /* Initialize IT for stepping through current_buffer in window W
3266 starting at ROW->start. */
3267
3268 static void
3269 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3270 {
3271 init_from_display_pos (it, w, &row->start);
3272 it->start = row->start;
3273 it->continuation_lines_width = row->continuation_lines_width;
3274 CHECK_IT (it);
3275 }
3276
3277
3278 /* Initialize IT for stepping through current_buffer in window W
3279 starting in the line following ROW, i.e. starting at ROW->end.
3280 Value is false if there are overlay strings with newlines at ROW's
3281 end position. */
3282
3283 static bool
3284 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3285 {
3286 bool success = false;
3287
3288 if (init_from_display_pos (it, w, &row->end))
3289 {
3290 if (row->continued_p)
3291 it->continuation_lines_width
3292 = row->continuation_lines_width + row->pixel_width;
3293 CHECK_IT (it);
3294 success = true;
3295 }
3296
3297 return success;
3298 }
3299
3300
3301
3302 \f
3303 /***********************************************************************
3304 Text properties
3305 ***********************************************************************/
3306
3307 /* Called when IT reaches IT->stop_charpos. Handle text property and
3308 overlay changes. Set IT->stop_charpos to the next position where
3309 to stop. */
3310
3311 static void
3312 handle_stop (struct it *it)
3313 {
3314 enum prop_handled handled;
3315 bool handle_overlay_change_p;
3316 struct props *p;
3317
3318 it->dpvec = NULL;
3319 it->current.dpvec_index = -1;
3320 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3321 it->ellipsis_p = false;
3322
3323 /* Use face of preceding text for ellipsis (if invisible) */
3324 if (it->selective_display_ellipsis_p)
3325 it->saved_face_id = it->face_id;
3326
3327 /* Here's the description of the semantics of, and the logic behind,
3328 the various HANDLED_* statuses:
3329
3330 HANDLED_NORMALLY means the handler did its job, and the loop
3331 should proceed to calling the next handler in order.
3332
3333 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3334 change in the properties and overlays at current position, so the
3335 loop should be restarted, to re-invoke the handlers that were
3336 already called. This happens when fontification-functions were
3337 called by handle_fontified_prop, and actually fontified
3338 something. Another case where HANDLED_RECOMPUTE_PROPS is
3339 returned is when we discover overlay strings that need to be
3340 displayed right away. The loop below will continue for as long
3341 as the status is HANDLED_RECOMPUTE_PROPS.
3342
3343 HANDLED_RETURN means return immediately to the caller, to
3344 continue iteration without calling any further handlers. This is
3345 used when we need to act on some property right away, for example
3346 when we need to display the ellipsis or a replacing display
3347 property, such as display string or image.
3348
3349 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3350 consumed, and the handler switched to the next overlay string.
3351 This signals the loop below to refrain from looking for more
3352 overlays before all the overlay strings of the current overlay
3353 are processed.
3354
3355 Some of the handlers called by the loop push the iterator state
3356 onto the stack (see 'push_it'), and arrange for the iteration to
3357 continue with another object, such as an image, a display string,
3358 or an overlay string. In most such cases, it->stop_charpos is
3359 set to the first character of the string, so that when the
3360 iteration resumes, this function will immediately be called
3361 again, to examine the properties at the beginning of the string.
3362
3363 When a display or overlay string is exhausted, the iterator state
3364 is popped (see 'pop_it'), and iteration continues with the
3365 previous object. Again, in many such cases this function is
3366 called again to find the next position where properties might
3367 change. */
3368
3369 do
3370 {
3371 handled = HANDLED_NORMALLY;
3372
3373 /* Call text property handlers. */
3374 for (p = it_props; p->handler; ++p)
3375 {
3376 handled = p->handler (it);
3377
3378 if (handled == HANDLED_RECOMPUTE_PROPS)
3379 break;
3380 else if (handled == HANDLED_RETURN)
3381 {
3382 /* We still want to show before and after strings from
3383 overlays even if the actual buffer text is replaced. */
3384 if (!handle_overlay_change_p
3385 || it->sp > 1
3386 /* Don't call get_overlay_strings_1 if we already
3387 have overlay strings loaded, because doing so
3388 will load them again and push the iterator state
3389 onto the stack one more time, which is not
3390 expected by the rest of the code that processes
3391 overlay strings. */
3392 || (it->current.overlay_string_index < 0
3393 && !get_overlay_strings_1 (it, 0, false)))
3394 {
3395 if (it->ellipsis_p)
3396 setup_for_ellipsis (it, 0);
3397 /* When handling a display spec, we might load an
3398 empty string. In that case, discard it here. We
3399 used to discard it in handle_single_display_spec,
3400 but that causes get_overlay_strings_1, above, to
3401 ignore overlay strings that we must check. */
3402 if (STRINGP (it->string) && !SCHARS (it->string))
3403 pop_it (it);
3404 return;
3405 }
3406 else if (STRINGP (it->string) && !SCHARS (it->string))
3407 pop_it (it);
3408 else
3409 {
3410 it->string_from_display_prop_p = false;
3411 it->from_disp_prop_p = false;
3412 handle_overlay_change_p = false;
3413 }
3414 handled = HANDLED_RECOMPUTE_PROPS;
3415 break;
3416 }
3417 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3418 handle_overlay_change_p = false;
3419 }
3420
3421 if (handled != HANDLED_RECOMPUTE_PROPS)
3422 {
3423 /* Don't check for overlay strings below when set to deliver
3424 characters from a display vector. */
3425 if (it->method == GET_FROM_DISPLAY_VECTOR)
3426 handle_overlay_change_p = false;
3427
3428 /* Handle overlay changes.
3429 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3430 if it finds overlays. */
3431 if (handle_overlay_change_p)
3432 handled = handle_overlay_change (it);
3433 }
3434
3435 if (it->ellipsis_p)
3436 {
3437 setup_for_ellipsis (it, 0);
3438 break;
3439 }
3440 }
3441 while (handled == HANDLED_RECOMPUTE_PROPS);
3442
3443 /* Determine where to stop next. */
3444 if (handled == HANDLED_NORMALLY)
3445 compute_stop_pos (it);
3446 }
3447
3448
3449 /* Compute IT->stop_charpos from text property and overlay change
3450 information for IT's current position. */
3451
3452 static void
3453 compute_stop_pos (struct it *it)
3454 {
3455 register INTERVAL iv, next_iv;
3456 Lisp_Object object, limit, position;
3457 ptrdiff_t charpos, bytepos;
3458
3459 if (STRINGP (it->string))
3460 {
3461 /* Strings are usually short, so don't limit the search for
3462 properties. */
3463 it->stop_charpos = it->end_charpos;
3464 object = it->string;
3465 limit = Qnil;
3466 charpos = IT_STRING_CHARPOS (*it);
3467 bytepos = IT_STRING_BYTEPOS (*it);
3468 }
3469 else
3470 {
3471 ptrdiff_t pos;
3472
3473 /* If end_charpos is out of range for some reason, such as a
3474 misbehaving display function, rationalize it (Bug#5984). */
3475 if (it->end_charpos > ZV)
3476 it->end_charpos = ZV;
3477 it->stop_charpos = it->end_charpos;
3478
3479 /* If next overlay change is in front of the current stop pos
3480 (which is IT->end_charpos), stop there. Note: value of
3481 next_overlay_change is point-max if no overlay change
3482 follows. */
3483 charpos = IT_CHARPOS (*it);
3484 bytepos = IT_BYTEPOS (*it);
3485 pos = next_overlay_change (charpos);
3486 if (pos < it->stop_charpos)
3487 it->stop_charpos = pos;
3488
3489 /* Set up variables for computing the stop position from text
3490 property changes. */
3491 XSETBUFFER (object, current_buffer);
3492 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3493 }
3494
3495 /* Get the interval containing IT's position. Value is a null
3496 interval if there isn't such an interval. */
3497 position = make_number (charpos);
3498 iv = validate_interval_range (object, &position, &position, false);
3499 if (iv)
3500 {
3501 Lisp_Object values_here[LAST_PROP_IDX];
3502 struct props *p;
3503
3504 /* Get properties here. */
3505 for (p = it_props; p->handler; ++p)
3506 values_here[p->idx] = textget (iv->plist,
3507 builtin_lisp_symbol (p->name));
3508
3509 /* Look for an interval following iv that has different
3510 properties. */
3511 for (next_iv = next_interval (iv);
3512 (next_iv
3513 && (NILP (limit)
3514 || XFASTINT (limit) > next_iv->position));
3515 next_iv = next_interval (next_iv))
3516 {
3517 for (p = it_props; p->handler; ++p)
3518 {
3519 Lisp_Object new_value = textget (next_iv->plist,
3520 builtin_lisp_symbol (p->name));
3521 if (!EQ (values_here[p->idx], new_value))
3522 break;
3523 }
3524
3525 if (p->handler)
3526 break;
3527 }
3528
3529 if (next_iv)
3530 {
3531 if (INTEGERP (limit)
3532 && next_iv->position >= XFASTINT (limit))
3533 /* No text property change up to limit. */
3534 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3535 else
3536 /* Text properties change in next_iv. */
3537 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3538 }
3539 }
3540
3541 if (it->cmp_it.id < 0)
3542 {
3543 ptrdiff_t stoppos = it->end_charpos;
3544
3545 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3546 stoppos = -1;
3547 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3548 stoppos, it->string);
3549 }
3550
3551 eassert (STRINGP (it->string)
3552 || (it->stop_charpos >= BEGV
3553 && it->stop_charpos >= IT_CHARPOS (*it)));
3554 }
3555
3556
3557 /* Return the position of the next overlay change after POS in
3558 current_buffer. Value is point-max if no overlay change
3559 follows. This is like `next-overlay-change' but doesn't use
3560 xmalloc. */
3561
3562 static ptrdiff_t
3563 next_overlay_change (ptrdiff_t pos)
3564 {
3565 ptrdiff_t i, noverlays;
3566 ptrdiff_t endpos;
3567 Lisp_Object *overlays;
3568 USE_SAFE_ALLOCA;
3569
3570 /* Get all overlays at the given position. */
3571 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3572
3573 /* If any of these overlays ends before endpos,
3574 use its ending point instead. */
3575 for (i = 0; i < noverlays; ++i)
3576 {
3577 Lisp_Object oend;
3578 ptrdiff_t oendpos;
3579
3580 oend = OVERLAY_END (overlays[i]);
3581 oendpos = OVERLAY_POSITION (oend);
3582 endpos = min (endpos, oendpos);
3583 }
3584
3585 SAFE_FREE ();
3586 return endpos;
3587 }
3588
3589 /* How many characters forward to search for a display property or
3590 display string. Searching too far forward makes the bidi display
3591 sluggish, especially in small windows. */
3592 #define MAX_DISP_SCAN 250
3593
3594 /* Return the character position of a display string at or after
3595 position specified by POSITION. If no display string exists at or
3596 after POSITION, return ZV. A display string is either an overlay
3597 with `display' property whose value is a string, or a `display'
3598 text property whose value is a string. STRING is data about the
3599 string to iterate; if STRING->lstring is nil, we are iterating a
3600 buffer. FRAME_WINDOW_P is true when we are displaying a window
3601 on a GUI frame. DISP_PROP is set to zero if we searched
3602 MAX_DISP_SCAN characters forward without finding any display
3603 strings, non-zero otherwise. It is set to 2 if the display string
3604 uses any kind of `(space ...)' spec that will produce a stretch of
3605 white space in the text area. */
3606 ptrdiff_t
3607 compute_display_string_pos (struct text_pos *position,
3608 struct bidi_string_data *string,
3609 struct window *w,
3610 bool frame_window_p, int *disp_prop)
3611 {
3612 /* OBJECT = nil means current buffer. */
3613 Lisp_Object object, object1;
3614 Lisp_Object pos, spec, limpos;
3615 bool string_p = string && (STRINGP (string->lstring) || string->s);
3616 ptrdiff_t eob = string_p ? string->schars : ZV;
3617 ptrdiff_t begb = string_p ? 0 : BEGV;
3618 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3619 ptrdiff_t lim =
3620 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3621 struct text_pos tpos;
3622 int rv = 0;
3623
3624 if (string && STRINGP (string->lstring))
3625 object1 = object = string->lstring;
3626 else if (w && !string_p)
3627 {
3628 XSETWINDOW (object, w);
3629 object1 = Qnil;
3630 }
3631 else
3632 object1 = object = Qnil;
3633
3634 *disp_prop = 1;
3635
3636 if (charpos >= eob
3637 /* We don't support display properties whose values are strings
3638 that have display string properties. */
3639 || string->from_disp_str
3640 /* C strings cannot have display properties. */
3641 || (string->s && !STRINGP (object)))
3642 {
3643 *disp_prop = 0;
3644 return eob;
3645 }
3646
3647 /* If the character at CHARPOS is where the display string begins,
3648 return CHARPOS. */
3649 pos = make_number (charpos);
3650 if (STRINGP (object))
3651 bufpos = string->bufpos;
3652 else
3653 bufpos = charpos;
3654 tpos = *position;
3655 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3656 && (charpos <= begb
3657 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3658 object),
3659 spec))
3660 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3661 frame_window_p)))
3662 {
3663 if (rv == 2)
3664 *disp_prop = 2;
3665 return charpos;
3666 }
3667
3668 /* Look forward for the first character with a `display' property
3669 that will replace the underlying text when displayed. */
3670 limpos = make_number (lim);
3671 do {
3672 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3673 CHARPOS (tpos) = XFASTINT (pos);
3674 if (CHARPOS (tpos) >= lim)
3675 {
3676 *disp_prop = 0;
3677 break;
3678 }
3679 if (STRINGP (object))
3680 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3681 else
3682 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3683 spec = Fget_char_property (pos, Qdisplay, object);
3684 if (!STRINGP (object))
3685 bufpos = CHARPOS (tpos);
3686 } while (NILP (spec)
3687 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3688 bufpos, frame_window_p)));
3689 if (rv == 2)
3690 *disp_prop = 2;
3691
3692 return CHARPOS (tpos);
3693 }
3694
3695 /* Return the character position of the end of the display string that
3696 started at CHARPOS. If there's no display string at CHARPOS,
3697 return -1. A display string is either an overlay with `display'
3698 property whose value is a string or a `display' text property whose
3699 value is a string. */
3700 ptrdiff_t
3701 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3702 {
3703 /* OBJECT = nil means current buffer. */
3704 Lisp_Object object =
3705 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3706 Lisp_Object pos = make_number (charpos);
3707 ptrdiff_t eob =
3708 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3709
3710 if (charpos >= eob || (string->s && !STRINGP (object)))
3711 return eob;
3712
3713 /* It could happen that the display property or overlay was removed
3714 since we found it in compute_display_string_pos above. One way
3715 this can happen is if JIT font-lock was called (through
3716 handle_fontified_prop), and jit-lock-functions remove text
3717 properties or overlays from the portion of buffer that includes
3718 CHARPOS. Muse mode is known to do that, for example. In this
3719 case, we return -1 to the caller, to signal that no display
3720 string is actually present at CHARPOS. See bidi_fetch_char for
3721 how this is handled.
3722
3723 An alternative would be to never look for display properties past
3724 it->stop_charpos. But neither compute_display_string_pos nor
3725 bidi_fetch_char that calls it know or care where the next
3726 stop_charpos is. */
3727 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3728 return -1;
3729
3730 /* Look forward for the first character where the `display' property
3731 changes. */
3732 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3733
3734 return XFASTINT (pos);
3735 }
3736
3737
3738 \f
3739 /***********************************************************************
3740 Fontification
3741 ***********************************************************************/
3742
3743 /* Handle changes in the `fontified' property of the current buffer by
3744 calling hook functions from Qfontification_functions to fontify
3745 regions of text. */
3746
3747 static enum prop_handled
3748 handle_fontified_prop (struct it *it)
3749 {
3750 Lisp_Object prop, pos;
3751 enum prop_handled handled = HANDLED_NORMALLY;
3752
3753 if (!NILP (Vmemory_full))
3754 return handled;
3755
3756 /* Get the value of the `fontified' property at IT's current buffer
3757 position. (The `fontified' property doesn't have a special
3758 meaning in strings.) If the value is nil, call functions from
3759 Qfontification_functions. */
3760 if (!STRINGP (it->string)
3761 && it->s == NULL
3762 && !NILP (Vfontification_functions)
3763 && !NILP (Vrun_hooks)
3764 && (pos = make_number (IT_CHARPOS (*it)),
3765 prop = Fget_char_property (pos, Qfontified, Qnil),
3766 /* Ignore the special cased nil value always present at EOB since
3767 no amount of fontifying will be able to change it. */
3768 NILP (prop) && IT_CHARPOS (*it) < Z))
3769 {
3770 ptrdiff_t count = SPECPDL_INDEX ();
3771 Lisp_Object val;
3772 struct buffer *obuf = current_buffer;
3773 ptrdiff_t begv = BEGV, zv = ZV;
3774 bool old_clip_changed = current_buffer->clip_changed;
3775
3776 val = Vfontification_functions;
3777 specbind (Qfontification_functions, Qnil);
3778
3779 eassert (it->end_charpos == ZV);
3780
3781 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3782 safe_call1 (val, pos);
3783 else
3784 {
3785 Lisp_Object fns, fn;
3786
3787 fns = Qnil;
3788
3789 for (; CONSP (val); val = XCDR (val))
3790 {
3791 fn = XCAR (val);
3792
3793 if (EQ (fn, Qt))
3794 {
3795 /* A value of t indicates this hook has a local
3796 binding; it means to run the global binding too.
3797 In a global value, t should not occur. If it
3798 does, we must ignore it to avoid an endless
3799 loop. */
3800 for (fns = Fdefault_value (Qfontification_functions);
3801 CONSP (fns);
3802 fns = XCDR (fns))
3803 {
3804 fn = XCAR (fns);
3805 if (!EQ (fn, Qt))
3806 safe_call1 (fn, pos);
3807 }
3808 }
3809 else
3810 safe_call1 (fn, pos);
3811 }
3812 }
3813
3814 unbind_to (count, Qnil);
3815
3816 /* Fontification functions routinely call `save-restriction'.
3817 Normally, this tags clip_changed, which can confuse redisplay
3818 (see discussion in Bug#6671). Since we don't perform any
3819 special handling of fontification changes in the case where
3820 `save-restriction' isn't called, there's no point doing so in
3821 this case either. So, if the buffer's restrictions are
3822 actually left unchanged, reset clip_changed. */
3823 if (obuf == current_buffer)
3824 {
3825 if (begv == BEGV && zv == ZV)
3826 current_buffer->clip_changed = old_clip_changed;
3827 }
3828 /* There isn't much we can reasonably do to protect against
3829 misbehaving fontification, but here's a fig leaf. */
3830 else if (BUFFER_LIVE_P (obuf))
3831 set_buffer_internal_1 (obuf);
3832
3833 /* The fontification code may have added/removed text.
3834 It could do even a lot worse, but let's at least protect against
3835 the most obvious case where only the text past `pos' gets changed',
3836 as is/was done in grep.el where some escapes sequences are turned
3837 into face properties (bug#7876). */
3838 it->end_charpos = ZV;
3839
3840 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3841 something. This avoids an endless loop if they failed to
3842 fontify the text for which reason ever. */
3843 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3844 handled = HANDLED_RECOMPUTE_PROPS;
3845 }
3846
3847 return handled;
3848 }
3849
3850
3851 \f
3852 /***********************************************************************
3853 Faces
3854 ***********************************************************************/
3855
3856 /* Set up iterator IT from face properties at its current position.
3857 Called from handle_stop. */
3858
3859 static enum prop_handled
3860 handle_face_prop (struct it *it)
3861 {
3862 int new_face_id;
3863 ptrdiff_t next_stop;
3864
3865 if (!STRINGP (it->string))
3866 {
3867 new_face_id
3868 = face_at_buffer_position (it->w,
3869 IT_CHARPOS (*it),
3870 &next_stop,
3871 (IT_CHARPOS (*it)
3872 + TEXT_PROP_DISTANCE_LIMIT),
3873 false, it->base_face_id);
3874
3875 /* Is this a start of a run of characters with box face?
3876 Caveat: this can be called for a freshly initialized
3877 iterator; face_id is -1 in this case. We know that the new
3878 face will not change until limit, i.e. if the new face has a
3879 box, all characters up to limit will have one. But, as
3880 usual, we don't know whether limit is really the end. */
3881 if (new_face_id != it->face_id)
3882 {
3883 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3884 /* If it->face_id is -1, old_face below will be NULL, see
3885 the definition of FACE_FROM_ID_OR_NULL. This will happen
3886 if this is the initial call that gets the face. */
3887 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3888
3889 /* If the value of face_id of the iterator is -1, we have to
3890 look in front of IT's position and see whether there is a
3891 face there that's different from new_face_id. */
3892 if (!old_face && IT_CHARPOS (*it) > BEG)
3893 {
3894 int prev_face_id = face_before_it_pos (it);
3895
3896 old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
3897 }
3898
3899 /* If the new face has a box, but the old face does not,
3900 this is the start of a run of characters with box face,
3901 i.e. this character has a shadow on the left side. */
3902 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3903 && (old_face == NULL || !old_face->box));
3904 it->face_box_p = new_face->box != FACE_NO_BOX;
3905 }
3906 }
3907 else
3908 {
3909 int base_face_id;
3910 ptrdiff_t bufpos;
3911 int i;
3912 Lisp_Object from_overlay
3913 = (it->current.overlay_string_index >= 0
3914 ? it->string_overlays[it->current.overlay_string_index
3915 % OVERLAY_STRING_CHUNK_SIZE]
3916 : Qnil);
3917
3918 /* See if we got to this string directly or indirectly from
3919 an overlay property. That includes the before-string or
3920 after-string of an overlay, strings in display properties
3921 provided by an overlay, their text properties, etc.
3922
3923 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3924 if (! NILP (from_overlay))
3925 for (i = it->sp - 1; i >= 0; i--)
3926 {
3927 if (it->stack[i].current.overlay_string_index >= 0)
3928 from_overlay
3929 = it->string_overlays[it->stack[i].current.overlay_string_index
3930 % OVERLAY_STRING_CHUNK_SIZE];
3931 else if (! NILP (it->stack[i].from_overlay))
3932 from_overlay = it->stack[i].from_overlay;
3933
3934 if (!NILP (from_overlay))
3935 break;
3936 }
3937
3938 if (! NILP (from_overlay))
3939 {
3940 bufpos = IT_CHARPOS (*it);
3941 /* For a string from an overlay, the base face depends
3942 only on text properties and ignores overlays. */
3943 base_face_id
3944 = face_for_overlay_string (it->w,
3945 IT_CHARPOS (*it),
3946 &next_stop,
3947 (IT_CHARPOS (*it)
3948 + TEXT_PROP_DISTANCE_LIMIT),
3949 false,
3950 from_overlay);
3951 }
3952 else
3953 {
3954 bufpos = 0;
3955
3956 /* For strings from a `display' property, use the face at
3957 IT's current buffer position as the base face to merge
3958 with, so that overlay strings appear in the same face as
3959 surrounding text, unless they specify their own faces.
3960 For strings from wrap-prefix and line-prefix properties,
3961 use the default face, possibly remapped via
3962 Vface_remapping_alist. */
3963 /* Note that the fact that we use the face at _buffer_
3964 position means that a 'display' property on an overlay
3965 string will not inherit the face of that overlay string,
3966 but will instead revert to the face of buffer text
3967 covered by the overlay. This is visible, e.g., when the
3968 overlay specifies a box face, but neither the buffer nor
3969 the display string do. This sounds like a design bug,
3970 but Emacs always did that since v21.1, so changing that
3971 might be a big deal. */
3972 base_face_id = it->string_from_prefix_prop_p
3973 ? (!NILP (Vface_remapping_alist)
3974 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3975 : DEFAULT_FACE_ID)
3976 : underlying_face_id (it);
3977 }
3978
3979 new_face_id = face_at_string_position (it->w,
3980 it->string,
3981 IT_STRING_CHARPOS (*it),
3982 bufpos,
3983 &next_stop,
3984 base_face_id, false);
3985
3986 /* Is this a start of a run of characters with box? Caveat:
3987 this can be called for a freshly allocated iterator; face_id
3988 is -1 is this case. We know that the new face will not
3989 change until the next check pos, i.e. if the new face has a
3990 box, all characters up to that position will have a
3991 box. But, as usual, we don't know whether that position
3992 is really the end. */
3993 if (new_face_id != it->face_id)
3994 {
3995 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3996 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3997
3998 /* If new face has a box but old face hasn't, this is the
3999 start of a run of characters with box, i.e. it has a
4000 shadow on the left side. */
4001 it->start_of_box_run_p
4002 = new_face->box && (old_face == NULL || !old_face->box);
4003 it->face_box_p = new_face->box != FACE_NO_BOX;
4004 }
4005 }
4006
4007 it->face_id = new_face_id;
4008 return HANDLED_NORMALLY;
4009 }
4010
4011
4012 /* Return the ID of the face ``underlying'' IT's current position,
4013 which is in a string. If the iterator is associated with a
4014 buffer, return the face at IT's current buffer position.
4015 Otherwise, use the iterator's base_face_id. */
4016
4017 static int
4018 underlying_face_id (struct it *it)
4019 {
4020 int face_id = it->base_face_id, i;
4021
4022 eassert (STRINGP (it->string));
4023
4024 for (i = it->sp - 1; i >= 0; --i)
4025 if (NILP (it->stack[i].string))
4026 face_id = it->stack[i].face_id;
4027
4028 return face_id;
4029 }
4030
4031
4032 /* Compute the face one character before or after the current position
4033 of IT, in the visual order. BEFORE_P means get the face
4034 in front (to the left in L2R paragraphs, to the right in R2L
4035 paragraphs) of IT's screen position. Value is the ID of the face. */
4036
4037 static int
4038 face_before_or_after_it_pos (struct it *it, bool before_p)
4039 {
4040 int face_id, limit;
4041 ptrdiff_t next_check_charpos;
4042 struct it it_copy;
4043 void *it_copy_data = NULL;
4044
4045 eassert (it->s == NULL);
4046
4047 if (STRINGP (it->string))
4048 {
4049 ptrdiff_t bufpos, charpos;
4050 int base_face_id;
4051
4052 /* No face change past the end of the string (for the case
4053 we are padding with spaces). No face change before the
4054 string start. */
4055 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4056 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4057 return it->face_id;
4058
4059 if (!it->bidi_p)
4060 {
4061 /* Set charpos to the position before or after IT's current
4062 position, in the logical order, which in the non-bidi
4063 case is the same as the visual order. */
4064 if (before_p)
4065 charpos = IT_STRING_CHARPOS (*it) - 1;
4066 else if (it->what == IT_COMPOSITION)
4067 /* For composition, we must check the character after the
4068 composition. */
4069 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4070 else
4071 charpos = IT_STRING_CHARPOS (*it) + 1;
4072 }
4073 else
4074 {
4075 if (before_p)
4076 {
4077 /* With bidi iteration, the character before the current
4078 in the visual order cannot be found by simple
4079 iteration, because "reverse" reordering is not
4080 supported. Instead, we need to start from the string
4081 beginning and go all the way to the current string
4082 position, remembering the previous position. */
4083 /* Ignore face changes before the first visible
4084 character on this display line. */
4085 if (it->current_x <= it->first_visible_x)
4086 return it->face_id;
4087 SAVE_IT (it_copy, *it, it_copy_data);
4088 IT_STRING_CHARPOS (it_copy) = 0;
4089 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4090
4091 do
4092 {
4093 charpos = IT_STRING_CHARPOS (it_copy);
4094 if (charpos >= SCHARS (it->string))
4095 break;
4096 bidi_move_to_visually_next (&it_copy.bidi_it);
4097 }
4098 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4099
4100 RESTORE_IT (it, it, it_copy_data);
4101 }
4102 else
4103 {
4104 /* Set charpos to the string position of the character
4105 that comes after IT's current position in the visual
4106 order. */
4107 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4108
4109 it_copy = *it;
4110 while (n--)
4111 bidi_move_to_visually_next (&it_copy.bidi_it);
4112
4113 charpos = it_copy.bidi_it.charpos;
4114 }
4115 }
4116 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4117
4118 if (it->current.overlay_string_index >= 0)
4119 bufpos = IT_CHARPOS (*it);
4120 else
4121 bufpos = 0;
4122
4123 base_face_id = underlying_face_id (it);
4124
4125 /* Get the face for ASCII, or unibyte. */
4126 face_id = face_at_string_position (it->w,
4127 it->string,
4128 charpos,
4129 bufpos,
4130 &next_check_charpos,
4131 base_face_id, false);
4132
4133 /* Correct the face for charsets different from ASCII. Do it
4134 for the multibyte case only. The face returned above is
4135 suitable for unibyte text if IT->string is unibyte. */
4136 if (STRING_MULTIBYTE (it->string))
4137 {
4138 struct text_pos pos1 = string_pos (charpos, it->string);
4139 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4140 int c, len;
4141 struct face *face = FACE_FROM_ID (it->f, face_id);
4142
4143 c = string_char_and_length (p, &len);
4144 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4145 }
4146 }
4147 else
4148 {
4149 struct text_pos pos;
4150
4151 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4152 || (IT_CHARPOS (*it) <= BEGV && before_p))
4153 return it->face_id;
4154
4155 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4156 pos = it->current.pos;
4157
4158 if (!it->bidi_p)
4159 {
4160 if (before_p)
4161 DEC_TEXT_POS (pos, it->multibyte_p);
4162 else
4163 {
4164 if (it->what == IT_COMPOSITION)
4165 {
4166 /* For composition, we must check the position after
4167 the composition. */
4168 pos.charpos += it->cmp_it.nchars;
4169 pos.bytepos += it->len;
4170 }
4171 else
4172 INC_TEXT_POS (pos, it->multibyte_p);
4173 }
4174 }
4175 else
4176 {
4177 if (before_p)
4178 {
4179 int current_x;
4180
4181 /* With bidi iteration, the character before the current
4182 in the visual order cannot be found by simple
4183 iteration, because "reverse" reordering is not
4184 supported. Instead, we need to use the move_it_*
4185 family of functions, and move to the previous
4186 character starting from the beginning of the visual
4187 line. */
4188 /* Ignore face changes before the first visible
4189 character on this display line. */
4190 if (it->current_x <= it->first_visible_x)
4191 return it->face_id;
4192 SAVE_IT (it_copy, *it, it_copy_data);
4193 /* Implementation note: Since move_it_in_display_line
4194 works in the iterator geometry, and thinks the first
4195 character is always the leftmost, even in R2L lines,
4196 we don't need to distinguish between the R2L and L2R
4197 cases here. */
4198 current_x = it_copy.current_x;
4199 move_it_vertically_backward (&it_copy, 0);
4200 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4201 pos = it_copy.current.pos;
4202 RESTORE_IT (it, it, it_copy_data);
4203 }
4204 else
4205 {
4206 /* Set charpos to the buffer position of the character
4207 that comes after IT's current position in the visual
4208 order. */
4209 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4210
4211 it_copy = *it;
4212 while (n--)
4213 bidi_move_to_visually_next (&it_copy.bidi_it);
4214
4215 SET_TEXT_POS (pos,
4216 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4217 }
4218 }
4219 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4220
4221 /* Determine face for CHARSET_ASCII, or unibyte. */
4222 face_id = face_at_buffer_position (it->w,
4223 CHARPOS (pos),
4224 &next_check_charpos,
4225 limit, false, -1);
4226
4227 /* Correct the face for charsets different from ASCII. Do it
4228 for the multibyte case only. The face returned above is
4229 suitable for unibyte text if current_buffer is unibyte. */
4230 if (it->multibyte_p)
4231 {
4232 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4233 struct face *face = FACE_FROM_ID (it->f, face_id);
4234 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4235 }
4236 }
4237
4238 return face_id;
4239 }
4240
4241
4242 \f
4243 /***********************************************************************
4244 Invisible text
4245 ***********************************************************************/
4246
4247 /* Set up iterator IT from invisible properties at its current
4248 position. Called from handle_stop. */
4249
4250 static enum prop_handled
4251 handle_invisible_prop (struct it *it)
4252 {
4253 enum prop_handled handled = HANDLED_NORMALLY;
4254 int invis;
4255 Lisp_Object prop;
4256
4257 if (STRINGP (it->string))
4258 {
4259 Lisp_Object end_charpos, limit;
4260
4261 /* Get the value of the invisible text property at the
4262 current position. Value will be nil if there is no such
4263 property. */
4264 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4265 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4266 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4267
4268 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4269 {
4270 /* Record whether we have to display an ellipsis for the
4271 invisible text. */
4272 bool display_ellipsis_p = (invis == 2);
4273 ptrdiff_t len, endpos;
4274
4275 handled = HANDLED_RECOMPUTE_PROPS;
4276
4277 /* Get the position at which the next visible text can be
4278 found in IT->string, if any. */
4279 endpos = len = SCHARS (it->string);
4280 XSETINT (limit, len);
4281 do
4282 {
4283 end_charpos
4284 = Fnext_single_property_change (end_charpos, Qinvisible,
4285 it->string, limit);
4286 /* Since LIMIT is always an integer, so should be the
4287 value returned by Fnext_single_property_change. */
4288 eassert (INTEGERP (end_charpos));
4289 if (INTEGERP (end_charpos))
4290 {
4291 endpos = XFASTINT (end_charpos);
4292 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4293 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4294 if (invis == 2)
4295 display_ellipsis_p = true;
4296 }
4297 else /* Should never happen; but if it does, exit the loop. */
4298 endpos = len;
4299 }
4300 while (invis != 0 && endpos < len);
4301
4302 if (display_ellipsis_p)
4303 it->ellipsis_p = true;
4304
4305 if (endpos < len)
4306 {
4307 /* Text at END_CHARPOS is visible. Move IT there. */
4308 struct text_pos old;
4309 ptrdiff_t oldpos;
4310
4311 old = it->current.string_pos;
4312 oldpos = CHARPOS (old);
4313 if (it->bidi_p)
4314 {
4315 if (it->bidi_it.first_elt
4316 && it->bidi_it.charpos < SCHARS (it->string))
4317 bidi_paragraph_init (it->paragraph_embedding,
4318 &it->bidi_it, true);
4319 /* Bidi-iterate out of the invisible text. */
4320 do
4321 {
4322 bidi_move_to_visually_next (&it->bidi_it);
4323 }
4324 while (oldpos <= it->bidi_it.charpos
4325 && it->bidi_it.charpos < endpos);
4326
4327 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4328 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4329 if (IT_CHARPOS (*it) >= endpos)
4330 it->prev_stop = endpos;
4331 }
4332 else
4333 {
4334 IT_STRING_CHARPOS (*it) = endpos;
4335 compute_string_pos (&it->current.string_pos, old, it->string);
4336 }
4337 }
4338 else
4339 {
4340 /* The rest of the string is invisible. If this is an
4341 overlay string, proceed with the next overlay string
4342 or whatever comes and return a character from there. */
4343 if (it->current.overlay_string_index >= 0
4344 && !display_ellipsis_p)
4345 {
4346 next_overlay_string (it);
4347 /* Don't check for overlay strings when we just
4348 finished processing them. */
4349 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4350 }
4351 else
4352 {
4353 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4354 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4355 }
4356 }
4357 }
4358 }
4359 else
4360 {
4361 ptrdiff_t newpos, next_stop, start_charpos, tem;
4362 Lisp_Object pos, overlay;
4363
4364 /* First of all, is there invisible text at this position? */
4365 tem = start_charpos = IT_CHARPOS (*it);
4366 pos = make_number (tem);
4367 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4368 &overlay);
4369 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4370
4371 /* If we are on invisible text, skip over it. */
4372 if (invis != 0 && start_charpos < it->end_charpos)
4373 {
4374 /* Record whether we have to display an ellipsis for the
4375 invisible text. */
4376 bool display_ellipsis_p = invis == 2;
4377
4378 handled = HANDLED_RECOMPUTE_PROPS;
4379
4380 /* Loop skipping over invisible text. The loop is left at
4381 ZV or with IT on the first char being visible again. */
4382 do
4383 {
4384 /* Try to skip some invisible text. Return value is the
4385 position reached which can be equal to where we start
4386 if there is nothing invisible there. This skips both
4387 over invisible text properties and overlays with
4388 invisible property. */
4389 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4390
4391 /* If we skipped nothing at all we weren't at invisible
4392 text in the first place. If everything to the end of
4393 the buffer was skipped, end the loop. */
4394 if (newpos == tem || newpos >= ZV)
4395 invis = 0;
4396 else
4397 {
4398 /* We skipped some characters but not necessarily
4399 all there are. Check if we ended up on visible
4400 text. Fget_char_property returns the property of
4401 the char before the given position, i.e. if we
4402 get invis = 0, this means that the char at
4403 newpos is visible. */
4404 pos = make_number (newpos);
4405 prop = Fget_char_property (pos, Qinvisible, it->window);
4406 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4407 }
4408
4409 /* If we ended up on invisible text, proceed to
4410 skip starting with next_stop. */
4411 if (invis != 0)
4412 tem = next_stop;
4413
4414 /* If there are adjacent invisible texts, don't lose the
4415 second one's ellipsis. */
4416 if (invis == 2)
4417 display_ellipsis_p = true;
4418 }
4419 while (invis != 0);
4420
4421 /* The position newpos is now either ZV or on visible text. */
4422 if (it->bidi_p)
4423 {
4424 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4425 bool on_newline
4426 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4427 bool after_newline
4428 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4429
4430 /* If the invisible text ends on a newline or on a
4431 character after a newline, we can avoid the costly,
4432 character by character, bidi iteration to NEWPOS, and
4433 instead simply reseat the iterator there. That's
4434 because all bidi reordering information is tossed at
4435 the newline. This is a big win for modes that hide
4436 complete lines, like Outline, Org, etc. */
4437 if (on_newline || after_newline)
4438 {
4439 struct text_pos tpos;
4440 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4441
4442 SET_TEXT_POS (tpos, newpos, bpos);
4443 reseat_1 (it, tpos, false);
4444 /* If we reseat on a newline/ZV, we need to prep the
4445 bidi iterator for advancing to the next character
4446 after the newline/EOB, keeping the current paragraph
4447 direction (so that PRODUCE_GLYPHS does TRT wrt
4448 prepending/appending glyphs to a glyph row). */
4449 if (on_newline)
4450 {
4451 it->bidi_it.first_elt = false;
4452 it->bidi_it.paragraph_dir = pdir;
4453 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4454 it->bidi_it.nchars = 1;
4455 it->bidi_it.ch_len = 1;
4456 }
4457 }
4458 else /* Must use the slow method. */
4459 {
4460 /* With bidi iteration, the region of invisible text
4461 could start and/or end in the middle of a
4462 non-base embedding level. Therefore, we need to
4463 skip invisible text using the bidi iterator,
4464 starting at IT's current position, until we find
4465 ourselves outside of the invisible text.
4466 Skipping invisible text _after_ bidi iteration
4467 avoids affecting the visual order of the
4468 displayed text when invisible properties are
4469 added or removed. */
4470 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4471 {
4472 /* If we were `reseat'ed to a new paragraph,
4473 determine the paragraph base direction. We
4474 need to do it now because
4475 next_element_from_buffer may not have a
4476 chance to do it, if we are going to skip any
4477 text at the beginning, which resets the
4478 FIRST_ELT flag. */
4479 bidi_paragraph_init (it->paragraph_embedding,
4480 &it->bidi_it, true);
4481 }
4482 do
4483 {
4484 bidi_move_to_visually_next (&it->bidi_it);
4485 }
4486 while (it->stop_charpos <= it->bidi_it.charpos
4487 && it->bidi_it.charpos < newpos);
4488 IT_CHARPOS (*it) = it->bidi_it.charpos;
4489 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4490 /* If we overstepped NEWPOS, record its position in
4491 the iterator, so that we skip invisible text if
4492 later the bidi iteration lands us in the
4493 invisible region again. */
4494 if (IT_CHARPOS (*it) >= newpos)
4495 it->prev_stop = newpos;
4496 }
4497 }
4498 else
4499 {
4500 IT_CHARPOS (*it) = newpos;
4501 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4502 }
4503
4504 if (display_ellipsis_p)
4505 {
4506 /* Make sure that the glyphs of the ellipsis will get
4507 correct `charpos' values. If we would not update
4508 it->position here, the glyphs would belong to the
4509 last visible character _before_ the invisible
4510 text, which confuses `set_cursor_from_row'.
4511
4512 We use the last invisible position instead of the
4513 first because this way the cursor is always drawn on
4514 the first "." of the ellipsis, whenever PT is inside
4515 the invisible text. Otherwise the cursor would be
4516 placed _after_ the ellipsis when the point is after the
4517 first invisible character. */
4518 if (!STRINGP (it->object))
4519 {
4520 it->position.charpos = newpos - 1;
4521 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4522 }
4523 }
4524
4525 /* If there are before-strings at the start of invisible
4526 text, and the text is invisible because of a text
4527 property, arrange to show before-strings because 20.x did
4528 it that way. (If the text is invisible because of an
4529 overlay property instead of a text property, this is
4530 already handled in the overlay code.) */
4531 if (NILP (overlay)
4532 && get_overlay_strings (it, it->stop_charpos))
4533 {
4534 handled = HANDLED_RECOMPUTE_PROPS;
4535 if (it->sp > 0)
4536 {
4537 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4538 /* The call to get_overlay_strings above recomputes
4539 it->stop_charpos, but it only considers changes
4540 in properties and overlays beyond iterator's
4541 current position. This causes us to miss changes
4542 that happen exactly where the invisible property
4543 ended. So we play it safe here and force the
4544 iterator to check for potential stop positions
4545 immediately after the invisible text. Note that
4546 if get_overlay_strings returns true, it
4547 normally also pushed the iterator stack, so we
4548 need to update the stop position in the slot
4549 below the current one. */
4550 it->stack[it->sp - 1].stop_charpos
4551 = CHARPOS (it->stack[it->sp - 1].current.pos);
4552 }
4553 }
4554 else if (display_ellipsis_p)
4555 {
4556 it->ellipsis_p = true;
4557 /* Let the ellipsis display before
4558 considering any properties of the following char.
4559 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4560 handled = HANDLED_RETURN;
4561 }
4562 }
4563 }
4564
4565 return handled;
4566 }
4567
4568
4569 /* Make iterator IT return `...' next.
4570 Replaces LEN characters from buffer. */
4571
4572 static void
4573 setup_for_ellipsis (struct it *it, int len)
4574 {
4575 /* Use the display table definition for `...'. Invalid glyphs
4576 will be handled by the method returning elements from dpvec. */
4577 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4578 {
4579 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4580 it->dpvec = v->contents;
4581 it->dpend = v->contents + v->header.size;
4582 }
4583 else
4584 {
4585 /* Default `...'. */
4586 it->dpvec = default_invis_vector;
4587 it->dpend = default_invis_vector + 3;
4588 }
4589
4590 it->dpvec_char_len = len;
4591 it->current.dpvec_index = 0;
4592 it->dpvec_face_id = -1;
4593
4594 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4595 face as the preceding text. IT->saved_face_id was set in
4596 handle_stop to the face of the preceding character, and will be
4597 different from IT->face_id only if the invisible text skipped in
4598 handle_invisible_prop has some non-default face on its first
4599 character. We thus ignore the face of the invisible text when we
4600 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4601 if (it->saved_face_id >= 0)
4602 it->face_id = it->saved_face_id;
4603
4604 /* If the ellipsis represents buffer text, it means we advanced in
4605 the buffer, so we should no longer ignore overlay strings. */
4606 if (it->method == GET_FROM_BUFFER)
4607 it->ignore_overlay_strings_at_pos_p = false;
4608
4609 it->method = GET_FROM_DISPLAY_VECTOR;
4610 it->ellipsis_p = true;
4611 }
4612
4613
4614 \f
4615 /***********************************************************************
4616 'display' property
4617 ***********************************************************************/
4618
4619 /* Set up iterator IT from `display' property at its current position.
4620 Called from handle_stop.
4621 We return HANDLED_RETURN if some part of the display property
4622 overrides the display of the buffer text itself.
4623 Otherwise we return HANDLED_NORMALLY. */
4624
4625 static enum prop_handled
4626 handle_display_prop (struct it *it)
4627 {
4628 Lisp_Object propval, object, overlay;
4629 struct text_pos *position;
4630 ptrdiff_t bufpos;
4631 /* Nonzero if some property replaces the display of the text itself. */
4632 int display_replaced = 0;
4633
4634 if (STRINGP (it->string))
4635 {
4636 object = it->string;
4637 position = &it->current.string_pos;
4638 bufpos = CHARPOS (it->current.pos);
4639 }
4640 else
4641 {
4642 XSETWINDOW (object, it->w);
4643 position = &it->current.pos;
4644 bufpos = CHARPOS (*position);
4645 }
4646
4647 /* Reset those iterator values set from display property values. */
4648 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4649 it->space_width = Qnil;
4650 it->font_height = Qnil;
4651 it->voffset = 0;
4652
4653 /* We don't support recursive `display' properties, i.e. string
4654 values that have a string `display' property, that have a string
4655 `display' property etc. */
4656 if (!it->string_from_display_prop_p)
4657 it->area = TEXT_AREA;
4658
4659 propval = get_char_property_and_overlay (make_number (position->charpos),
4660 Qdisplay, object, &overlay);
4661 if (NILP (propval))
4662 return HANDLED_NORMALLY;
4663 /* Now OVERLAY is the overlay that gave us this property, or nil
4664 if it was a text property. */
4665
4666 if (!STRINGP (it->string))
4667 object = it->w->contents;
4668
4669 display_replaced = handle_display_spec (it, propval, object, overlay,
4670 position, bufpos,
4671 FRAME_WINDOW_P (it->f));
4672 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4673 }
4674
4675 /* Subroutine of handle_display_prop. Returns non-zero if the display
4676 specification in SPEC is a replacing specification, i.e. it would
4677 replace the text covered by `display' property with something else,
4678 such as an image or a display string. If SPEC includes any kind or
4679 `(space ...) specification, the value is 2; this is used by
4680 compute_display_string_pos, which see.
4681
4682 See handle_single_display_spec for documentation of arguments.
4683 FRAME_WINDOW_P is true if the window being redisplayed is on a
4684 GUI frame; this argument is used only if IT is NULL, see below.
4685
4686 IT can be NULL, if this is called by the bidi reordering code
4687 through compute_display_string_pos, which see. In that case, this
4688 function only examines SPEC, but does not otherwise "handle" it, in
4689 the sense that it doesn't set up members of IT from the display
4690 spec. */
4691 static int
4692 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4693 Lisp_Object overlay, struct text_pos *position,
4694 ptrdiff_t bufpos, bool frame_window_p)
4695 {
4696 int replacing = 0;
4697
4698 if (CONSP (spec)
4699 /* Simple specifications. */
4700 && !EQ (XCAR (spec), Qimage)
4701 #ifdef HAVE_XWIDGETS
4702 && !EQ (XCAR (spec), Qxwidget)
4703 #endif
4704 && !EQ (XCAR (spec), Qspace)
4705 && !EQ (XCAR (spec), Qwhen)
4706 && !EQ (XCAR (spec), Qslice)
4707 && !EQ (XCAR (spec), Qspace_width)
4708 && !EQ (XCAR (spec), Qheight)
4709 && !EQ (XCAR (spec), Qraise)
4710 /* Marginal area specifications. */
4711 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4712 && !EQ (XCAR (spec), Qleft_fringe)
4713 && !EQ (XCAR (spec), Qright_fringe)
4714 && !NILP (XCAR (spec)))
4715 {
4716 for (; CONSP (spec); spec = XCDR (spec))
4717 {
4718 int rv = handle_single_display_spec (it, XCAR (spec), object,
4719 overlay, position, bufpos,
4720 replacing, frame_window_p);
4721 if (rv != 0)
4722 {
4723 replacing = rv;
4724 /* If some text in a string is replaced, `position' no
4725 longer points to the position of `object'. */
4726 if (!it || STRINGP (object))
4727 break;
4728 }
4729 }
4730 }
4731 else if (VECTORP (spec))
4732 {
4733 ptrdiff_t i;
4734 for (i = 0; i < ASIZE (spec); ++i)
4735 {
4736 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4737 overlay, position, bufpos,
4738 replacing, frame_window_p);
4739 if (rv != 0)
4740 {
4741 replacing = rv;
4742 /* If some text in a string is replaced, `position' no
4743 longer points to the position of `object'. */
4744 if (!it || STRINGP (object))
4745 break;
4746 }
4747 }
4748 }
4749 else
4750 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4751 bufpos, 0, frame_window_p);
4752 return replacing;
4753 }
4754
4755 /* Value is the position of the end of the `display' property starting
4756 at START_POS in OBJECT. */
4757
4758 static struct text_pos
4759 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4760 {
4761 Lisp_Object end;
4762 struct text_pos end_pos;
4763
4764 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4765 Qdisplay, object, Qnil);
4766 CHARPOS (end_pos) = XFASTINT (end);
4767 if (STRINGP (object))
4768 compute_string_pos (&end_pos, start_pos, it->string);
4769 else
4770 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4771
4772 return end_pos;
4773 }
4774
4775
4776 /* Set up IT from a single `display' property specification SPEC. OBJECT
4777 is the object in which the `display' property was found. *POSITION
4778 is the position in OBJECT at which the `display' property was found.
4779 BUFPOS is the buffer position of OBJECT (different from POSITION if
4780 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4781 previously saw a display specification which already replaced text
4782 display with something else, for example an image; we ignore such
4783 properties after the first one has been processed.
4784
4785 OVERLAY is the overlay this `display' property came from,
4786 or nil if it was a text property.
4787
4788 If SPEC is a `space' or `image' specification, and in some other
4789 cases too, set *POSITION to the position where the `display'
4790 property ends.
4791
4792 If IT is NULL, only examine the property specification in SPEC, but
4793 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4794 is intended to be displayed in a window on a GUI frame.
4795
4796 Value is non-zero if something was found which replaces the display
4797 of buffer or string text. */
4798
4799 static int
4800 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4801 Lisp_Object overlay, struct text_pos *position,
4802 ptrdiff_t bufpos, int display_replaced,
4803 bool frame_window_p)
4804 {
4805 Lisp_Object form;
4806 Lisp_Object location, value;
4807 struct text_pos start_pos = *position;
4808
4809 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4810 If the result is non-nil, use VALUE instead of SPEC. */
4811 form = Qt;
4812 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4813 {
4814 spec = XCDR (spec);
4815 if (!CONSP (spec))
4816 return 0;
4817 form = XCAR (spec);
4818 spec = XCDR (spec);
4819 }
4820
4821 if (!NILP (form) && !EQ (form, Qt))
4822 {
4823 ptrdiff_t count = SPECPDL_INDEX ();
4824
4825 /* Bind `object' to the object having the `display' property, a
4826 buffer or string. Bind `position' to the position in the
4827 object where the property was found, and `buffer-position'
4828 to the current position in the buffer. */
4829
4830 if (NILP (object))
4831 XSETBUFFER (object, current_buffer);
4832 specbind (Qobject, object);
4833 specbind (Qposition, make_number (CHARPOS (*position)));
4834 specbind (Qbuffer_position, make_number (bufpos));
4835 form = safe_eval (form);
4836 unbind_to (count, Qnil);
4837 }
4838
4839 if (NILP (form))
4840 return 0;
4841
4842 /* Handle `(height HEIGHT)' specifications. */
4843 if (CONSP (spec)
4844 && EQ (XCAR (spec), Qheight)
4845 && CONSP (XCDR (spec)))
4846 {
4847 if (it)
4848 {
4849 if (!FRAME_WINDOW_P (it->f))
4850 return 0;
4851
4852 it->font_height = XCAR (XCDR (spec));
4853 if (!NILP (it->font_height))
4854 {
4855 int new_height = -1;
4856
4857 if (CONSP (it->font_height)
4858 && (EQ (XCAR (it->font_height), Qplus)
4859 || EQ (XCAR (it->font_height), Qminus))
4860 && CONSP (XCDR (it->font_height))
4861 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4862 {
4863 /* `(+ N)' or `(- N)' where N is an integer. */
4864 int steps = XINT (XCAR (XCDR (it->font_height)));
4865 if (EQ (XCAR (it->font_height), Qplus))
4866 steps = - steps;
4867 it->face_id = smaller_face (it->f, it->face_id, steps);
4868 }
4869 else if (FUNCTIONP (it->font_height))
4870 {
4871 /* Call function with current height as argument.
4872 Value is the new height. */
4873 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4874 Lisp_Object height;
4875 height = safe_call1 (it->font_height,
4876 face->lface[LFACE_HEIGHT_INDEX]);
4877 if (NUMBERP (height))
4878 new_height = XFLOATINT (height);
4879 }
4880 else if (NUMBERP (it->font_height))
4881 {
4882 /* Value is a multiple of the canonical char height. */
4883 struct face *f;
4884
4885 f = FACE_FROM_ID (it->f,
4886 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4887 new_height = (XFLOATINT (it->font_height)
4888 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4889 }
4890 else
4891 {
4892 /* Evaluate IT->font_height with `height' bound to the
4893 current specified height to get the new height. */
4894 ptrdiff_t count = SPECPDL_INDEX ();
4895 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4896
4897 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4898 value = safe_eval (it->font_height);
4899 unbind_to (count, Qnil);
4900
4901 if (NUMBERP (value))
4902 new_height = XFLOATINT (value);
4903 }
4904
4905 if (new_height > 0)
4906 it->face_id = face_with_height (it->f, it->face_id, new_height);
4907 }
4908 }
4909
4910 return 0;
4911 }
4912
4913 /* Handle `(space-width WIDTH)'. */
4914 if (CONSP (spec)
4915 && EQ (XCAR (spec), Qspace_width)
4916 && CONSP (XCDR (spec)))
4917 {
4918 if (it)
4919 {
4920 if (!FRAME_WINDOW_P (it->f))
4921 return 0;
4922
4923 value = XCAR (XCDR (spec));
4924 if (NUMBERP (value) && XFLOATINT (value) > 0)
4925 it->space_width = value;
4926 }
4927
4928 return 0;
4929 }
4930
4931 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4932 if (CONSP (spec)
4933 && EQ (XCAR (spec), Qslice))
4934 {
4935 Lisp_Object tem;
4936
4937 if (it)
4938 {
4939 if (!FRAME_WINDOW_P (it->f))
4940 return 0;
4941
4942 if (tem = XCDR (spec), CONSP (tem))
4943 {
4944 it->slice.x = XCAR (tem);
4945 if (tem = XCDR (tem), CONSP (tem))
4946 {
4947 it->slice.y = XCAR (tem);
4948 if (tem = XCDR (tem), CONSP (tem))
4949 {
4950 it->slice.width = XCAR (tem);
4951 if (tem = XCDR (tem), CONSP (tem))
4952 it->slice.height = XCAR (tem);
4953 }
4954 }
4955 }
4956 }
4957
4958 return 0;
4959 }
4960
4961 /* Handle `(raise FACTOR)'. */
4962 if (CONSP (spec)
4963 && EQ (XCAR (spec), Qraise)
4964 && CONSP (XCDR (spec)))
4965 {
4966 if (it)
4967 {
4968 if (!FRAME_WINDOW_P (it->f))
4969 return 0;
4970
4971 #ifdef HAVE_WINDOW_SYSTEM
4972 value = XCAR (XCDR (spec));
4973 if (NUMBERP (value))
4974 {
4975 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4976 it->voffset = - (XFLOATINT (value)
4977 * (normal_char_height (face->font, -1)));
4978 }
4979 #endif /* HAVE_WINDOW_SYSTEM */
4980 }
4981
4982 return 0;
4983 }
4984
4985 /* Don't handle the other kinds of display specifications
4986 inside a string that we got from a `display' property. */
4987 if (it && it->string_from_display_prop_p)
4988 return 0;
4989
4990 /* Characters having this form of property are not displayed, so
4991 we have to find the end of the property. */
4992 if (it)
4993 {
4994 start_pos = *position;
4995 *position = display_prop_end (it, object, start_pos);
4996 /* If the display property comes from an overlay, don't consider
4997 any potential stop_charpos values before the end of that
4998 overlay. Since display_prop_end will happily find another
4999 'display' property coming from some other overlay or text
5000 property on buffer positions before this overlay's end, we
5001 need to ignore them, or else we risk displaying this
5002 overlay's display string/image twice. */
5003 if (!NILP (overlay))
5004 {
5005 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5006
5007 if (ovendpos > CHARPOS (*position))
5008 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5009 }
5010 }
5011 value = Qnil;
5012
5013 /* Stop the scan at that end position--we assume that all
5014 text properties change there. */
5015 if (it)
5016 it->stop_charpos = position->charpos;
5017
5018 /* Handle `(left-fringe BITMAP [FACE])'
5019 and `(right-fringe BITMAP [FACE])'. */
5020 if (CONSP (spec)
5021 && (EQ (XCAR (spec), Qleft_fringe)
5022 || EQ (XCAR (spec), Qright_fringe))
5023 && CONSP (XCDR (spec)))
5024 {
5025 if (it)
5026 {
5027 if (!FRAME_WINDOW_P (it->f))
5028 /* If we return here, POSITION has been advanced
5029 across the text with this property. */
5030 {
5031 /* Synchronize the bidi iterator with POSITION. This is
5032 needed because we are not going to push the iterator
5033 on behalf of this display property, so there will be
5034 no pop_it call to do this synchronization for us. */
5035 if (it->bidi_p)
5036 {
5037 it->position = *position;
5038 iterate_out_of_display_property (it);
5039 *position = it->position;
5040 }
5041 return 1;
5042 }
5043 }
5044 else if (!frame_window_p)
5045 return 1;
5046
5047 #ifdef HAVE_WINDOW_SYSTEM
5048 value = XCAR (XCDR (spec));
5049 int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
5050 if (! fringe_bitmap)
5051 /* If we return here, POSITION has been advanced
5052 across the text with this property. */
5053 {
5054 if (it && it->bidi_p)
5055 {
5056 it->position = *position;
5057 iterate_out_of_display_property (it);
5058 *position = it->position;
5059 }
5060 return 1;
5061 }
5062
5063 if (it)
5064 {
5065 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5066
5067 if (CONSP (XCDR (XCDR (spec))))
5068 {
5069 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5070 int face_id2 = lookup_derived_face (it->f, face_name,
5071 FRINGE_FACE_ID, false);
5072 if (face_id2 >= 0)
5073 face_id = face_id2;
5074 }
5075
5076 /* Save current settings of IT so that we can restore them
5077 when we are finished with the glyph property value. */
5078 push_it (it, position);
5079
5080 it->area = TEXT_AREA;
5081 it->what = IT_IMAGE;
5082 it->image_id = -1; /* no image */
5083 it->position = start_pos;
5084 it->object = NILP (object) ? it->w->contents : object;
5085 it->method = GET_FROM_IMAGE;
5086 it->from_overlay = Qnil;
5087 it->face_id = face_id;
5088 it->from_disp_prop_p = true;
5089
5090 /* Say that we haven't consumed the characters with
5091 `display' property yet. The call to pop_it in
5092 set_iterator_to_next will clean this up. */
5093 *position = start_pos;
5094
5095 if (EQ (XCAR (spec), Qleft_fringe))
5096 {
5097 it->left_user_fringe_bitmap = fringe_bitmap;
5098 it->left_user_fringe_face_id = face_id;
5099 }
5100 else
5101 {
5102 it->right_user_fringe_bitmap = fringe_bitmap;
5103 it->right_user_fringe_face_id = face_id;
5104 }
5105 }
5106 #endif /* HAVE_WINDOW_SYSTEM */
5107 return 1;
5108 }
5109
5110 /* Prepare to handle `((margin left-margin) ...)',
5111 `((margin right-margin) ...)' and `((margin nil) ...)'
5112 prefixes for display specifications. */
5113 location = Qunbound;
5114 if (CONSP (spec) && CONSP (XCAR (spec)))
5115 {
5116 Lisp_Object tem;
5117
5118 value = XCDR (spec);
5119 if (CONSP (value))
5120 value = XCAR (value);
5121
5122 tem = XCAR (spec);
5123 if (EQ (XCAR (tem), Qmargin)
5124 && (tem = XCDR (tem),
5125 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5126 (NILP (tem)
5127 || EQ (tem, Qleft_margin)
5128 || EQ (tem, Qright_margin))))
5129 location = tem;
5130 }
5131
5132 if (EQ (location, Qunbound))
5133 {
5134 location = Qnil;
5135 value = spec;
5136 }
5137
5138 /* After this point, VALUE is the property after any
5139 margin prefix has been stripped. It must be a string,
5140 an image specification, or `(space ...)'.
5141
5142 LOCATION specifies where to display: `left-margin',
5143 `right-margin' or nil. */
5144
5145 bool valid_p = (STRINGP (value)
5146 #ifdef HAVE_WINDOW_SYSTEM
5147 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5148 && valid_image_p (value))
5149 #endif /* not HAVE_WINDOW_SYSTEM */
5150 || (CONSP (value) && EQ (XCAR (value), Qspace))
5151 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5152 && valid_xwidget_spec_p (value)));
5153
5154 if (valid_p && display_replaced == 0)
5155 {
5156 int retval = 1;
5157
5158 if (!it)
5159 {
5160 /* Callers need to know whether the display spec is any kind
5161 of `(space ...)' spec that is about to affect text-area
5162 display. */
5163 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5164 retval = 2;
5165 return retval;
5166 }
5167
5168 /* Save current settings of IT so that we can restore them
5169 when we are finished with the glyph property value. */
5170 push_it (it, position);
5171 it->from_overlay = overlay;
5172 it->from_disp_prop_p = true;
5173
5174 if (NILP (location))
5175 it->area = TEXT_AREA;
5176 else if (EQ (location, Qleft_margin))
5177 it->area = LEFT_MARGIN_AREA;
5178 else
5179 it->area = RIGHT_MARGIN_AREA;
5180
5181 if (STRINGP (value))
5182 {
5183 it->string = value;
5184 it->multibyte_p = STRING_MULTIBYTE (it->string);
5185 it->current.overlay_string_index = -1;
5186 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5187 it->end_charpos = it->string_nchars = SCHARS (it->string);
5188 it->method = GET_FROM_STRING;
5189 it->stop_charpos = 0;
5190 it->prev_stop = 0;
5191 it->base_level_stop = 0;
5192 it->string_from_display_prop_p = true;
5193 /* Say that we haven't consumed the characters with
5194 `display' property yet. The call to pop_it in
5195 set_iterator_to_next will clean this up. */
5196 if (BUFFERP (object))
5197 *position = start_pos;
5198
5199 /* Force paragraph direction to be that of the parent
5200 object. If the parent object's paragraph direction is
5201 not yet determined, default to L2R. */
5202 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5203 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5204 else
5205 it->paragraph_embedding = L2R;
5206
5207 /* Set up the bidi iterator for this display string. */
5208 if (it->bidi_p)
5209 {
5210 it->bidi_it.string.lstring = it->string;
5211 it->bidi_it.string.s = NULL;
5212 it->bidi_it.string.schars = it->end_charpos;
5213 it->bidi_it.string.bufpos = bufpos;
5214 it->bidi_it.string.from_disp_str = true;
5215 it->bidi_it.string.unibyte = !it->multibyte_p;
5216 it->bidi_it.w = it->w;
5217 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5218 }
5219 }
5220 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5221 {
5222 it->method = GET_FROM_STRETCH;
5223 it->object = value;
5224 *position = it->position = start_pos;
5225 retval = 1 + (it->area == TEXT_AREA);
5226 }
5227 else if (valid_xwidget_spec_p (value))
5228 {
5229 it->what = IT_XWIDGET;
5230 it->method = GET_FROM_XWIDGET;
5231 it->position = start_pos;
5232 it->object = NILP (object) ? it->w->contents : object;
5233 *position = start_pos;
5234 it->xwidget = lookup_xwidget (value);
5235 }
5236 #ifdef HAVE_WINDOW_SYSTEM
5237 else
5238 {
5239 it->what = IT_IMAGE;
5240 it->image_id = lookup_image (it->f, value);
5241 it->position = start_pos;
5242 it->object = NILP (object) ? it->w->contents : object;
5243 it->method = GET_FROM_IMAGE;
5244
5245 /* Say that we haven't consumed the characters with
5246 `display' property yet. The call to pop_it in
5247 set_iterator_to_next will clean this up. */
5248 *position = start_pos;
5249 }
5250 #endif /* HAVE_WINDOW_SYSTEM */
5251
5252 return retval;
5253 }
5254
5255 /* Invalid property or property not supported. Restore
5256 POSITION to what it was before. */
5257 *position = start_pos;
5258 return 0;
5259 }
5260
5261 /* Check if PROP is a display property value whose text should be
5262 treated as intangible. OVERLAY is the overlay from which PROP
5263 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5264 specify the buffer position covered by PROP. */
5265
5266 bool
5267 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5268 ptrdiff_t charpos, ptrdiff_t bytepos)
5269 {
5270 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5271 struct text_pos position;
5272
5273 SET_TEXT_POS (position, charpos, bytepos);
5274 return (handle_display_spec (NULL, prop, Qnil, overlay,
5275 &position, charpos, frame_window_p)
5276 != 0);
5277 }
5278
5279
5280 /* Return true if PROP is a display sub-property value containing STRING.
5281
5282 Implementation note: this and the following function are really
5283 special cases of handle_display_spec and
5284 handle_single_display_spec, and should ideally use the same code.
5285 Until they do, these two pairs must be consistent and must be
5286 modified in sync. */
5287
5288 static bool
5289 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5290 {
5291 if (EQ (string, prop))
5292 return true;
5293
5294 /* Skip over `when FORM'. */
5295 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5296 {
5297 prop = XCDR (prop);
5298 if (!CONSP (prop))
5299 return false;
5300 /* Actually, the condition following `when' should be eval'ed,
5301 like handle_single_display_spec does, and we should return
5302 false if it evaluates to nil. However, this function is
5303 called only when the buffer was already displayed and some
5304 glyph in the glyph matrix was found to come from a display
5305 string. Therefore, the condition was already evaluated, and
5306 the result was non-nil, otherwise the display string wouldn't
5307 have been displayed and we would have never been called for
5308 this property. Thus, we can skip the evaluation and assume
5309 its result is non-nil. */
5310 prop = XCDR (prop);
5311 }
5312
5313 if (CONSP (prop))
5314 /* Skip over `margin LOCATION'. */
5315 if (EQ (XCAR (prop), Qmargin))
5316 {
5317 prop = XCDR (prop);
5318 if (!CONSP (prop))
5319 return false;
5320
5321 prop = XCDR (prop);
5322 if (!CONSP (prop))
5323 return false;
5324 }
5325
5326 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5327 }
5328
5329
5330 /* Return true if STRING appears in the `display' property PROP. */
5331
5332 static bool
5333 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5334 {
5335 if (CONSP (prop)
5336 && !EQ (XCAR (prop), Qwhen)
5337 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5338 {
5339 /* A list of sub-properties. */
5340 while (CONSP (prop))
5341 {
5342 if (single_display_spec_string_p (XCAR (prop), string))
5343 return true;
5344 prop = XCDR (prop);
5345 }
5346 }
5347 else if (VECTORP (prop))
5348 {
5349 /* A vector of sub-properties. */
5350 ptrdiff_t i;
5351 for (i = 0; i < ASIZE (prop); ++i)
5352 if (single_display_spec_string_p (AREF (prop, i), string))
5353 return true;
5354 }
5355 else
5356 return single_display_spec_string_p (prop, string);
5357
5358 return false;
5359 }
5360
5361 /* Look for STRING in overlays and text properties in the current
5362 buffer, between character positions FROM and TO (excluding TO).
5363 BACK_P means look back (in this case, TO is supposed to be
5364 less than FROM).
5365 Value is the first character position where STRING was found, or
5366 zero if it wasn't found before hitting TO.
5367
5368 This function may only use code that doesn't eval because it is
5369 called asynchronously from note_mouse_highlight. */
5370
5371 static ptrdiff_t
5372 string_buffer_position_lim (Lisp_Object string,
5373 ptrdiff_t from, ptrdiff_t to, bool back_p)
5374 {
5375 Lisp_Object limit, prop, pos;
5376 bool found = false;
5377
5378 pos = make_number (max (from, BEGV));
5379
5380 if (!back_p) /* looking forward */
5381 {
5382 limit = make_number (min (to, ZV));
5383 while (!found && !EQ (pos, limit))
5384 {
5385 prop = Fget_char_property (pos, Qdisplay, Qnil);
5386 if (!NILP (prop) && display_prop_string_p (prop, string))
5387 found = true;
5388 else
5389 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5390 limit);
5391 }
5392 }
5393 else /* looking back */
5394 {
5395 limit = make_number (max (to, BEGV));
5396 while (!found && !EQ (pos, limit))
5397 {
5398 prop = Fget_char_property (pos, Qdisplay, Qnil);
5399 if (!NILP (prop) && display_prop_string_p (prop, string))
5400 found = true;
5401 else
5402 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5403 limit);
5404 }
5405 }
5406
5407 return found ? XINT (pos) : 0;
5408 }
5409
5410 /* Determine which buffer position in current buffer STRING comes from.
5411 AROUND_CHARPOS is an approximate position where it could come from.
5412 Value is the buffer position or 0 if it couldn't be determined.
5413
5414 This function is necessary because we don't record buffer positions
5415 in glyphs generated from strings (to keep struct glyph small).
5416 This function may only use code that doesn't eval because it is
5417 called asynchronously from note_mouse_highlight. */
5418
5419 static ptrdiff_t
5420 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5421 {
5422 const int MAX_DISTANCE = 1000;
5423 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5424 around_charpos + MAX_DISTANCE,
5425 false);
5426
5427 if (!found)
5428 found = string_buffer_position_lim (string, around_charpos,
5429 around_charpos - MAX_DISTANCE, true);
5430 return found;
5431 }
5432
5433
5434 \f
5435 /***********************************************************************
5436 `composition' property
5437 ***********************************************************************/
5438
5439 /* Set up iterator IT from `composition' property at its current
5440 position. Called from handle_stop. */
5441
5442 static enum prop_handled
5443 handle_composition_prop (struct it *it)
5444 {
5445 Lisp_Object prop, string;
5446 ptrdiff_t pos, pos_byte, start, end;
5447
5448 if (STRINGP (it->string))
5449 {
5450 unsigned char *s;
5451
5452 pos = IT_STRING_CHARPOS (*it);
5453 pos_byte = IT_STRING_BYTEPOS (*it);
5454 string = it->string;
5455 s = SDATA (string) + pos_byte;
5456 it->c = STRING_CHAR (s);
5457 }
5458 else
5459 {
5460 pos = IT_CHARPOS (*it);
5461 pos_byte = IT_BYTEPOS (*it);
5462 string = Qnil;
5463 it->c = FETCH_CHAR (pos_byte);
5464 }
5465
5466 /* If there's a valid composition and point is not inside of the
5467 composition (in the case that the composition is from the current
5468 buffer), draw a glyph composed from the composition components. */
5469 if (find_composition (pos, -1, &start, &end, &prop, string)
5470 && composition_valid_p (start, end, prop)
5471 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5472 {
5473 if (start < pos)
5474 /* As we can't handle this situation (perhaps font-lock added
5475 a new composition), we just return here hoping that next
5476 redisplay will detect this composition much earlier. */
5477 return HANDLED_NORMALLY;
5478 if (start != pos)
5479 {
5480 if (STRINGP (it->string))
5481 pos_byte = string_char_to_byte (it->string, start);
5482 else
5483 pos_byte = CHAR_TO_BYTE (start);
5484 }
5485 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5486 prop, string);
5487
5488 if (it->cmp_it.id >= 0)
5489 {
5490 it->cmp_it.ch = -1;
5491 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5492 it->cmp_it.nglyphs = -1;
5493 }
5494 }
5495
5496 return HANDLED_NORMALLY;
5497 }
5498
5499
5500 \f
5501 /***********************************************************************
5502 Overlay strings
5503 ***********************************************************************/
5504
5505 /* The following structure is used to record overlay strings for
5506 later sorting in load_overlay_strings. */
5507
5508 struct overlay_entry
5509 {
5510 Lisp_Object overlay;
5511 Lisp_Object string;
5512 EMACS_INT priority;
5513 bool after_string_p;
5514 };
5515
5516
5517 /* Set up iterator IT from overlay strings at its current position.
5518 Called from handle_stop. */
5519
5520 static enum prop_handled
5521 handle_overlay_change (struct it *it)
5522 {
5523 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5524 return HANDLED_RECOMPUTE_PROPS;
5525 else
5526 return HANDLED_NORMALLY;
5527 }
5528
5529
5530 /* Set up the next overlay string for delivery by IT, if there is an
5531 overlay string to deliver. Called by set_iterator_to_next when the
5532 end of the current overlay string is reached. If there are more
5533 overlay strings to display, IT->string and
5534 IT->current.overlay_string_index are set appropriately here.
5535 Otherwise IT->string is set to nil. */
5536
5537 static void
5538 next_overlay_string (struct it *it)
5539 {
5540 ++it->current.overlay_string_index;
5541 if (it->current.overlay_string_index == it->n_overlay_strings)
5542 {
5543 /* No more overlay strings. Restore IT's settings to what
5544 they were before overlay strings were processed, and
5545 continue to deliver from current_buffer. */
5546
5547 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5548 pop_it (it);
5549 eassert (it->sp > 0
5550 || (NILP (it->string)
5551 && it->method == GET_FROM_BUFFER
5552 && it->stop_charpos >= BEGV
5553 && it->stop_charpos <= it->end_charpos));
5554 it->current.overlay_string_index = -1;
5555 it->n_overlay_strings = 0;
5556 /* If there's an empty display string on the stack, pop the
5557 stack, to resync the bidi iterator with IT's position. Such
5558 empty strings are pushed onto the stack in
5559 get_overlay_strings_1. */
5560 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5561 pop_it (it);
5562
5563 /* Since we've exhausted overlay strings at this buffer
5564 position, set the flag to ignore overlays until we move to
5565 another position. The flag is reset in
5566 next_element_from_buffer. */
5567 it->ignore_overlay_strings_at_pos_p = true;
5568
5569 /* If we're at the end of the buffer, record that we have
5570 processed the overlay strings there already, so that
5571 next_element_from_buffer doesn't try it again. */
5572 if (NILP (it->string)
5573 && IT_CHARPOS (*it) >= it->end_charpos
5574 && it->overlay_strings_charpos >= it->end_charpos)
5575 it->overlay_strings_at_end_processed_p = true;
5576 /* Note: we reset overlay_strings_charpos only here, to make
5577 sure the just-processed overlays were indeed at EOB.
5578 Otherwise, overlays on text with invisible text property,
5579 which are processed with IT's position past the invisible
5580 text, might fool us into thinking the overlays at EOB were
5581 already processed (linum-mode can cause this, for
5582 example). */
5583 it->overlay_strings_charpos = -1;
5584 }
5585 else
5586 {
5587 /* There are more overlay strings to process. If
5588 IT->current.overlay_string_index has advanced to a position
5589 where we must load IT->overlay_strings with more strings, do
5590 it. We must load at the IT->overlay_strings_charpos where
5591 IT->n_overlay_strings was originally computed; when invisible
5592 text is present, this might not be IT_CHARPOS (Bug#7016). */
5593 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5594
5595 if (it->current.overlay_string_index && i == 0)
5596 load_overlay_strings (it, it->overlay_strings_charpos);
5597
5598 /* Initialize IT to deliver display elements from the overlay
5599 string. */
5600 it->string = it->overlay_strings[i];
5601 it->multibyte_p = STRING_MULTIBYTE (it->string);
5602 SET_TEXT_POS (it->current.string_pos, 0, 0);
5603 it->method = GET_FROM_STRING;
5604 it->stop_charpos = 0;
5605 it->end_charpos = SCHARS (it->string);
5606 if (it->cmp_it.stop_pos >= 0)
5607 it->cmp_it.stop_pos = 0;
5608 it->prev_stop = 0;
5609 it->base_level_stop = 0;
5610
5611 /* Set up the bidi iterator for this overlay string. */
5612 if (it->bidi_p)
5613 {
5614 it->bidi_it.string.lstring = it->string;
5615 it->bidi_it.string.s = NULL;
5616 it->bidi_it.string.schars = SCHARS (it->string);
5617 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5618 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5619 it->bidi_it.string.unibyte = !it->multibyte_p;
5620 it->bidi_it.w = it->w;
5621 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5622 }
5623 }
5624
5625 CHECK_IT (it);
5626 }
5627
5628
5629 /* Compare two overlay_entry structures E1 and E2. Used as a
5630 comparison function for qsort in load_overlay_strings. Overlay
5631 strings for the same position are sorted so that
5632
5633 1. All after-strings come in front of before-strings, except
5634 when they come from the same overlay.
5635
5636 2. Within after-strings, strings are sorted so that overlay strings
5637 from overlays with higher priorities come first.
5638
5639 2. Within before-strings, strings are sorted so that overlay
5640 strings from overlays with higher priorities come last.
5641
5642 Value is analogous to strcmp. */
5643
5644
5645 static int
5646 compare_overlay_entries (const void *e1, const void *e2)
5647 {
5648 struct overlay_entry const *entry1 = e1;
5649 struct overlay_entry const *entry2 = e2;
5650 int result;
5651
5652 if (entry1->after_string_p != entry2->after_string_p)
5653 {
5654 /* Let after-strings appear in front of before-strings if
5655 they come from different overlays. */
5656 if (EQ (entry1->overlay, entry2->overlay))
5657 result = entry1->after_string_p ? 1 : -1;
5658 else
5659 result = entry1->after_string_p ? -1 : 1;
5660 }
5661 else if (entry1->priority != entry2->priority)
5662 {
5663 if (entry1->after_string_p)
5664 /* After-strings sorted in order of decreasing priority. */
5665 result = entry2->priority < entry1->priority ? -1 : 1;
5666 else
5667 /* Before-strings sorted in order of increasing priority. */
5668 result = entry1->priority < entry2->priority ? -1 : 1;
5669 }
5670 else
5671 result = 0;
5672
5673 return result;
5674 }
5675
5676
5677 /* Load the vector IT->overlay_strings with overlay strings from IT's
5678 current buffer position, or from CHARPOS if that is > 0. Set
5679 IT->n_overlays to the total number of overlay strings found.
5680
5681 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5682 a time. On entry into load_overlay_strings,
5683 IT->current.overlay_string_index gives the number of overlay
5684 strings that have already been loaded by previous calls to this
5685 function.
5686
5687 IT->add_overlay_start contains an additional overlay start
5688 position to consider for taking overlay strings from, if non-zero.
5689 This position comes into play when the overlay has an `invisible'
5690 property, and both before and after-strings. When we've skipped to
5691 the end of the overlay, because of its `invisible' property, we
5692 nevertheless want its before-string to appear.
5693 IT->add_overlay_start will contain the overlay start position
5694 in this case.
5695
5696 Overlay strings are sorted so that after-string strings come in
5697 front of before-string strings. Within before and after-strings,
5698 strings are sorted by overlay priority. See also function
5699 compare_overlay_entries. */
5700
5701 static void
5702 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5703 {
5704 Lisp_Object overlay, window, str, invisible;
5705 struct Lisp_Overlay *ov;
5706 ptrdiff_t start, end;
5707 ptrdiff_t n = 0, i, j;
5708 int invis;
5709 struct overlay_entry entriesbuf[20];
5710 ptrdiff_t size = ARRAYELTS (entriesbuf);
5711 struct overlay_entry *entries = entriesbuf;
5712 USE_SAFE_ALLOCA;
5713
5714 if (charpos <= 0)
5715 charpos = IT_CHARPOS (*it);
5716
5717 /* Append the overlay string STRING of overlay OVERLAY to vector
5718 `entries' which has size `size' and currently contains `n'
5719 elements. AFTER_P means STRING is an after-string of
5720 OVERLAY. */
5721 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5722 do \
5723 { \
5724 Lisp_Object priority; \
5725 \
5726 if (n == size) \
5727 { \
5728 struct overlay_entry *old = entries; \
5729 SAFE_NALLOCA (entries, 2, size); \
5730 memcpy (entries, old, size * sizeof *entries); \
5731 size *= 2; \
5732 } \
5733 \
5734 entries[n].string = (STRING); \
5735 entries[n].overlay = (OVERLAY); \
5736 priority = Foverlay_get ((OVERLAY), Qpriority); \
5737 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5738 entries[n].after_string_p = (AFTER_P); \
5739 ++n; \
5740 } \
5741 while (false)
5742
5743 /* Process overlay before the overlay center. */
5744 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5745 {
5746 XSETMISC (overlay, ov);
5747 eassert (OVERLAYP (overlay));
5748 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5749 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5750
5751 if (end < charpos)
5752 break;
5753
5754 /* Skip this overlay if it doesn't start or end at IT's current
5755 position. */
5756 if (end != charpos && start != charpos)
5757 continue;
5758
5759 /* Skip this overlay if it doesn't apply to IT->w. */
5760 window = Foverlay_get (overlay, Qwindow);
5761 if (WINDOWP (window) && XWINDOW (window) != it->w)
5762 continue;
5763
5764 /* If the text ``under'' the overlay is invisible, both before-
5765 and after-strings from this overlay are visible; start and
5766 end position are indistinguishable. */
5767 invisible = Foverlay_get (overlay, Qinvisible);
5768 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5769
5770 /* If overlay has a non-empty before-string, record it. */
5771 if ((start == charpos || (end == charpos && invis != 0))
5772 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5773 && SCHARS (str))
5774 RECORD_OVERLAY_STRING (overlay, str, false);
5775
5776 /* If overlay has a non-empty after-string, record it. */
5777 if ((end == charpos || (start == charpos && invis != 0))
5778 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5779 && SCHARS (str))
5780 RECORD_OVERLAY_STRING (overlay, str, true);
5781 }
5782
5783 /* Process overlays after the overlay center. */
5784 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5785 {
5786 XSETMISC (overlay, ov);
5787 eassert (OVERLAYP (overlay));
5788 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5789 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5790
5791 if (start > charpos)
5792 break;
5793
5794 /* Skip this overlay if it doesn't start or end at IT's current
5795 position. */
5796 if (end != charpos && start != charpos)
5797 continue;
5798
5799 /* Skip this overlay if it doesn't apply to IT->w. */
5800 window = Foverlay_get (overlay, Qwindow);
5801 if (WINDOWP (window) && XWINDOW (window) != it->w)
5802 continue;
5803
5804 /* If the text ``under'' the overlay is invisible, it has a zero
5805 dimension, and both before- and after-strings apply. */
5806 invisible = Foverlay_get (overlay, Qinvisible);
5807 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5808
5809 /* If overlay has a non-empty before-string, record it. */
5810 if ((start == charpos || (end == charpos && invis != 0))
5811 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5812 && SCHARS (str))
5813 RECORD_OVERLAY_STRING (overlay, str, false);
5814
5815 /* If overlay has a non-empty after-string, record it. */
5816 if ((end == charpos || (start == charpos && invis != 0))
5817 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5818 && SCHARS (str))
5819 RECORD_OVERLAY_STRING (overlay, str, true);
5820 }
5821
5822 #undef RECORD_OVERLAY_STRING
5823
5824 /* Sort entries. */
5825 if (n > 1)
5826 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5827
5828 /* Record number of overlay strings, and where we computed it. */
5829 it->n_overlay_strings = n;
5830 it->overlay_strings_charpos = charpos;
5831
5832 /* IT->current.overlay_string_index is the number of overlay strings
5833 that have already been consumed by IT. Copy some of the
5834 remaining overlay strings to IT->overlay_strings. */
5835 i = 0;
5836 j = it->current.overlay_string_index;
5837 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5838 {
5839 it->overlay_strings[i] = entries[j].string;
5840 it->string_overlays[i++] = entries[j++].overlay;
5841 }
5842
5843 CHECK_IT (it);
5844 SAFE_FREE ();
5845 }
5846
5847
5848 /* Get the first chunk of overlay strings at IT's current buffer
5849 position, or at CHARPOS if that is > 0. Value is true if at
5850 least one overlay string was found. */
5851
5852 static bool
5853 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5854 {
5855 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5856 process. This fills IT->overlay_strings with strings, and sets
5857 IT->n_overlay_strings to the total number of strings to process.
5858 IT->pos.overlay_string_index has to be set temporarily to zero
5859 because load_overlay_strings needs this; it must be set to -1
5860 when no overlay strings are found because a zero value would
5861 indicate a position in the first overlay string. */
5862 it->current.overlay_string_index = 0;
5863 load_overlay_strings (it, charpos);
5864
5865 /* If we found overlay strings, set up IT to deliver display
5866 elements from the first one. Otherwise set up IT to deliver
5867 from current_buffer. */
5868 if (it->n_overlay_strings)
5869 {
5870 /* Make sure we know settings in current_buffer, so that we can
5871 restore meaningful values when we're done with the overlay
5872 strings. */
5873 if (compute_stop_p)
5874 compute_stop_pos (it);
5875 eassert (it->face_id >= 0);
5876
5877 /* Save IT's settings. They are restored after all overlay
5878 strings have been processed. */
5879 eassert (!compute_stop_p || it->sp == 0);
5880
5881 /* When called from handle_stop, there might be an empty display
5882 string loaded. In that case, don't bother saving it. But
5883 don't use this optimization with the bidi iterator, since we
5884 need the corresponding pop_it call to resync the bidi
5885 iterator's position with IT's position, after we are done
5886 with the overlay strings. (The corresponding call to pop_it
5887 in case of an empty display string is in
5888 next_overlay_string.) */
5889 if (!(!it->bidi_p
5890 && STRINGP (it->string) && !SCHARS (it->string)))
5891 push_it (it, NULL);
5892
5893 /* Set up IT to deliver display elements from the first overlay
5894 string. */
5895 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5896 it->string = it->overlay_strings[0];
5897 it->from_overlay = Qnil;
5898 it->stop_charpos = 0;
5899 eassert (STRINGP (it->string));
5900 it->end_charpos = SCHARS (it->string);
5901 it->prev_stop = 0;
5902 it->base_level_stop = 0;
5903 it->multibyte_p = STRING_MULTIBYTE (it->string);
5904 it->method = GET_FROM_STRING;
5905 it->from_disp_prop_p = 0;
5906
5907 /* Force paragraph direction to be that of the parent
5908 buffer. */
5909 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5910 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5911 else
5912 it->paragraph_embedding = L2R;
5913
5914 /* Set up the bidi iterator for this overlay string. */
5915 if (it->bidi_p)
5916 {
5917 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5918
5919 it->bidi_it.string.lstring = it->string;
5920 it->bidi_it.string.s = NULL;
5921 it->bidi_it.string.schars = SCHARS (it->string);
5922 it->bidi_it.string.bufpos = pos;
5923 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5924 it->bidi_it.string.unibyte = !it->multibyte_p;
5925 it->bidi_it.w = it->w;
5926 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5927 }
5928 return true;
5929 }
5930
5931 it->current.overlay_string_index = -1;
5932 return false;
5933 }
5934
5935 static bool
5936 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5937 {
5938 it->string = Qnil;
5939 it->method = GET_FROM_BUFFER;
5940
5941 get_overlay_strings_1 (it, charpos, true);
5942
5943 CHECK_IT (it);
5944
5945 /* Value is true if we found at least one overlay string. */
5946 return STRINGP (it->string);
5947 }
5948
5949
5950 \f
5951 /***********************************************************************
5952 Saving and restoring state
5953 ***********************************************************************/
5954
5955 /* Save current settings of IT on IT->stack. Called, for example,
5956 before setting up IT for an overlay string, to be able to restore
5957 IT's settings to what they were after the overlay string has been
5958 processed. If POSITION is non-NULL, it is the position to save on
5959 the stack instead of IT->position. */
5960
5961 static void
5962 push_it (struct it *it, struct text_pos *position)
5963 {
5964 struct iterator_stack_entry *p;
5965
5966 eassert (it->sp < IT_STACK_SIZE);
5967 p = it->stack + it->sp;
5968
5969 p->stop_charpos = it->stop_charpos;
5970 p->prev_stop = it->prev_stop;
5971 p->base_level_stop = it->base_level_stop;
5972 p->cmp_it = it->cmp_it;
5973 eassert (it->face_id >= 0);
5974 p->face_id = it->face_id;
5975 p->string = it->string;
5976 p->method = it->method;
5977 p->from_overlay = it->from_overlay;
5978 switch (p->method)
5979 {
5980 case GET_FROM_IMAGE:
5981 p->u.image.object = it->object;
5982 p->u.image.image_id = it->image_id;
5983 p->u.image.slice = it->slice;
5984 break;
5985 case GET_FROM_STRETCH:
5986 p->u.stretch.object = it->object;
5987 break;
5988 case GET_FROM_XWIDGET:
5989 p->u.xwidget.object = it->object;
5990 break;
5991 case GET_FROM_BUFFER:
5992 case GET_FROM_DISPLAY_VECTOR:
5993 case GET_FROM_STRING:
5994 case GET_FROM_C_STRING:
5995 break;
5996 default:
5997 emacs_abort ();
5998 }
5999 p->position = position ? *position : it->position;
6000 p->current = it->current;
6001 p->end_charpos = it->end_charpos;
6002 p->string_nchars = it->string_nchars;
6003 p->area = it->area;
6004 p->multibyte_p = it->multibyte_p;
6005 p->avoid_cursor_p = it->avoid_cursor_p;
6006 p->space_width = it->space_width;
6007 p->font_height = it->font_height;
6008 p->voffset = it->voffset;
6009 p->string_from_display_prop_p = it->string_from_display_prop_p;
6010 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6011 p->display_ellipsis_p = false;
6012 p->line_wrap = it->line_wrap;
6013 p->bidi_p = it->bidi_p;
6014 p->paragraph_embedding = it->paragraph_embedding;
6015 p->from_disp_prop_p = it->from_disp_prop_p;
6016 ++it->sp;
6017
6018 /* Save the state of the bidi iterator as well. */
6019 if (it->bidi_p)
6020 bidi_push_it (&it->bidi_it);
6021 }
6022
6023 static void
6024 iterate_out_of_display_property (struct it *it)
6025 {
6026 bool buffer_p = !STRINGP (it->string);
6027 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6028 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6029
6030 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6031
6032 /* Maybe initialize paragraph direction. If we are at the beginning
6033 of a new paragraph, next_element_from_buffer may not have a
6034 chance to do that. */
6035 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6036 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6037 /* prev_stop can be zero, so check against BEGV as well. */
6038 while (it->bidi_it.charpos >= bob
6039 && it->prev_stop <= it->bidi_it.charpos
6040 && it->bidi_it.charpos < CHARPOS (it->position)
6041 && it->bidi_it.charpos < eob)
6042 bidi_move_to_visually_next (&it->bidi_it);
6043 /* Record the stop_pos we just crossed, for when we cross it
6044 back, maybe. */
6045 if (it->bidi_it.charpos > CHARPOS (it->position))
6046 it->prev_stop = CHARPOS (it->position);
6047 /* If we ended up not where pop_it put us, resync IT's
6048 positional members with the bidi iterator. */
6049 if (it->bidi_it.charpos != CHARPOS (it->position))
6050 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6051 if (buffer_p)
6052 it->current.pos = it->position;
6053 else
6054 it->current.string_pos = it->position;
6055 }
6056
6057 /* Restore IT's settings from IT->stack. Called, for example, when no
6058 more overlay strings must be processed, and we return to delivering
6059 display elements from a buffer, or when the end of a string from a
6060 `display' property is reached and we return to delivering display
6061 elements from an overlay string, or from a buffer. */
6062
6063 static void
6064 pop_it (struct it *it)
6065 {
6066 struct iterator_stack_entry *p;
6067 bool from_display_prop = it->from_disp_prop_p;
6068 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6069
6070 eassert (it->sp > 0);
6071 --it->sp;
6072 p = it->stack + it->sp;
6073 it->stop_charpos = p->stop_charpos;
6074 it->prev_stop = p->prev_stop;
6075 it->base_level_stop = p->base_level_stop;
6076 it->cmp_it = p->cmp_it;
6077 it->face_id = p->face_id;
6078 it->current = p->current;
6079 it->position = p->position;
6080 it->string = p->string;
6081 it->from_overlay = p->from_overlay;
6082 if (NILP (it->string))
6083 SET_TEXT_POS (it->current.string_pos, -1, -1);
6084 it->method = p->method;
6085 switch (it->method)
6086 {
6087 case GET_FROM_IMAGE:
6088 it->image_id = p->u.image.image_id;
6089 it->object = p->u.image.object;
6090 it->slice = p->u.image.slice;
6091 break;
6092 case GET_FROM_XWIDGET:
6093 it->object = p->u.xwidget.object;
6094 break;
6095 case GET_FROM_STRETCH:
6096 it->object = p->u.stretch.object;
6097 break;
6098 case GET_FROM_BUFFER:
6099 it->object = it->w->contents;
6100 break;
6101 case GET_FROM_STRING:
6102 {
6103 struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
6104
6105 /* Restore the face_box_p flag, since it could have been
6106 overwritten by the face of the object that we just finished
6107 displaying. */
6108 if (face)
6109 it->face_box_p = face->box != FACE_NO_BOX;
6110 it->object = it->string;
6111 }
6112 break;
6113 case GET_FROM_DISPLAY_VECTOR:
6114 if (it->s)
6115 it->method = GET_FROM_C_STRING;
6116 else if (STRINGP (it->string))
6117 it->method = GET_FROM_STRING;
6118 else
6119 {
6120 it->method = GET_FROM_BUFFER;
6121 it->object = it->w->contents;
6122 }
6123 break;
6124 case GET_FROM_C_STRING:
6125 break;
6126 default:
6127 emacs_abort ();
6128 }
6129 it->end_charpos = p->end_charpos;
6130 it->string_nchars = p->string_nchars;
6131 it->area = p->area;
6132 it->multibyte_p = p->multibyte_p;
6133 it->avoid_cursor_p = p->avoid_cursor_p;
6134 it->space_width = p->space_width;
6135 it->font_height = p->font_height;
6136 it->voffset = p->voffset;
6137 it->string_from_display_prop_p = p->string_from_display_prop_p;
6138 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6139 it->line_wrap = p->line_wrap;
6140 it->bidi_p = p->bidi_p;
6141 it->paragraph_embedding = p->paragraph_embedding;
6142 it->from_disp_prop_p = p->from_disp_prop_p;
6143 if (it->bidi_p)
6144 {
6145 bidi_pop_it (&it->bidi_it);
6146 /* Bidi-iterate until we get out of the portion of text, if any,
6147 covered by a `display' text property or by an overlay with
6148 `display' property. (We cannot just jump there, because the
6149 internal coherency of the bidi iterator state can not be
6150 preserved across such jumps.) We also must determine the
6151 paragraph base direction if the overlay we just processed is
6152 at the beginning of a new paragraph. */
6153 if (from_display_prop
6154 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6155 iterate_out_of_display_property (it);
6156
6157 eassert ((BUFFERP (it->object)
6158 && IT_CHARPOS (*it) == it->bidi_it.charpos
6159 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6160 || (STRINGP (it->object)
6161 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6162 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6163 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6164 }
6165 /* If we move the iterator over text covered by a display property
6166 to a new buffer position, any info about previously seen overlays
6167 is no longer valid. */
6168 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6169 it->ignore_overlay_strings_at_pos_p = false;
6170 }
6171
6172
6173 \f
6174 /***********************************************************************
6175 Moving over lines
6176 ***********************************************************************/
6177
6178 /* Set IT's current position to the previous line start. */
6179
6180 static void
6181 back_to_previous_line_start (struct it *it)
6182 {
6183 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6184
6185 DEC_BOTH (cp, bp);
6186 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6187 }
6188
6189
6190 /* Move IT to the next line start.
6191
6192 Value is true if a newline was found. Set *SKIPPED_P to true if
6193 we skipped over part of the text (as opposed to moving the iterator
6194 continuously over the text). Otherwise, don't change the value
6195 of *SKIPPED_P.
6196
6197 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6198 iterator on the newline, if it was found.
6199
6200 Newlines may come from buffer text, overlay strings, or strings
6201 displayed via the `display' property. That's the reason we can't
6202 simply use find_newline_no_quit.
6203
6204 Note that this function may not skip over invisible text that is so
6205 because of text properties and immediately follows a newline. If
6206 it would, function reseat_at_next_visible_line_start, when called
6207 from set_iterator_to_next, would effectively make invisible
6208 characters following a newline part of the wrong glyph row, which
6209 leads to wrong cursor motion. */
6210
6211 static bool
6212 forward_to_next_line_start (struct it *it, bool *skipped_p,
6213 struct bidi_it *bidi_it_prev)
6214 {
6215 ptrdiff_t old_selective;
6216 bool newline_found_p = false;
6217 int n;
6218 const int MAX_NEWLINE_DISTANCE = 500;
6219
6220 /* If already on a newline, just consume it to avoid unintended
6221 skipping over invisible text below. */
6222 if (it->what == IT_CHARACTER
6223 && it->c == '\n'
6224 && CHARPOS (it->position) == IT_CHARPOS (*it))
6225 {
6226 if (it->bidi_p && bidi_it_prev)
6227 *bidi_it_prev = it->bidi_it;
6228 set_iterator_to_next (it, false);
6229 it->c = 0;
6230 return true;
6231 }
6232
6233 /* Don't handle selective display in the following. It's (a)
6234 unnecessary because it's done by the caller, and (b) leads to an
6235 infinite recursion because next_element_from_ellipsis indirectly
6236 calls this function. */
6237 old_selective = it->selective;
6238 it->selective = 0;
6239
6240 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6241 from buffer text. */
6242 for (n = 0;
6243 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6244 n += !STRINGP (it->string))
6245 {
6246 if (!get_next_display_element (it))
6247 return false;
6248 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6249 if (newline_found_p && it->bidi_p && bidi_it_prev)
6250 *bidi_it_prev = it->bidi_it;
6251 set_iterator_to_next (it, false);
6252 }
6253
6254 /* If we didn't find a newline near enough, see if we can use a
6255 short-cut. */
6256 if (!newline_found_p)
6257 {
6258 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6259 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6260 1, &bytepos);
6261 Lisp_Object pos;
6262
6263 eassert (!STRINGP (it->string));
6264
6265 /* If there isn't any `display' property in sight, and no
6266 overlays, we can just use the position of the newline in
6267 buffer text. */
6268 if (it->stop_charpos >= limit
6269 || ((pos = Fnext_single_property_change (make_number (start),
6270 Qdisplay, Qnil,
6271 make_number (limit)),
6272 NILP (pos))
6273 && next_overlay_change (start) == ZV))
6274 {
6275 if (!it->bidi_p)
6276 {
6277 IT_CHARPOS (*it) = limit;
6278 IT_BYTEPOS (*it) = bytepos;
6279 }
6280 else
6281 {
6282 struct bidi_it bprev;
6283
6284 /* Help bidi.c avoid expensive searches for display
6285 properties and overlays, by telling it that there are
6286 none up to `limit'. */
6287 if (it->bidi_it.disp_pos < limit)
6288 {
6289 it->bidi_it.disp_pos = limit;
6290 it->bidi_it.disp_prop = 0;
6291 }
6292 do {
6293 bprev = it->bidi_it;
6294 bidi_move_to_visually_next (&it->bidi_it);
6295 } while (it->bidi_it.charpos != limit);
6296 IT_CHARPOS (*it) = limit;
6297 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6298 if (bidi_it_prev)
6299 *bidi_it_prev = bprev;
6300 }
6301 *skipped_p = newline_found_p = true;
6302 }
6303 else
6304 {
6305 while (get_next_display_element (it)
6306 && !newline_found_p)
6307 {
6308 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6309 if (newline_found_p && it->bidi_p && bidi_it_prev)
6310 *bidi_it_prev = it->bidi_it;
6311 set_iterator_to_next (it, false);
6312 }
6313 }
6314 }
6315
6316 it->selective = old_selective;
6317 return newline_found_p;
6318 }
6319
6320
6321 /* Set IT's current position to the previous visible line start. Skip
6322 invisible text that is so either due to text properties or due to
6323 selective display. Caution: this does not change IT->current_x and
6324 IT->hpos. */
6325
6326 static void
6327 back_to_previous_visible_line_start (struct it *it)
6328 {
6329 while (IT_CHARPOS (*it) > BEGV)
6330 {
6331 back_to_previous_line_start (it);
6332
6333 if (IT_CHARPOS (*it) <= BEGV)
6334 break;
6335
6336 /* If selective > 0, then lines indented more than its value are
6337 invisible. */
6338 if (it->selective > 0
6339 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6340 it->selective))
6341 continue;
6342
6343 /* Check the newline before point for invisibility. */
6344 {
6345 Lisp_Object prop;
6346 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6347 Qinvisible, it->window);
6348 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6349 continue;
6350 }
6351
6352 if (IT_CHARPOS (*it) <= BEGV)
6353 break;
6354
6355 {
6356 struct it it2;
6357 void *it2data = NULL;
6358 ptrdiff_t pos;
6359 ptrdiff_t beg, end;
6360 Lisp_Object val, overlay;
6361
6362 SAVE_IT (it2, *it, it2data);
6363
6364 /* If newline is part of a composition, continue from start of composition */
6365 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6366 && beg < IT_CHARPOS (*it))
6367 goto replaced;
6368
6369 /* If newline is replaced by a display property, find start of overlay
6370 or interval and continue search from that point. */
6371 pos = --IT_CHARPOS (it2);
6372 --IT_BYTEPOS (it2);
6373 it2.sp = 0;
6374 bidi_unshelve_cache (NULL, false);
6375 it2.string_from_display_prop_p = false;
6376 it2.from_disp_prop_p = false;
6377 if (handle_display_prop (&it2) == HANDLED_RETURN
6378 && !NILP (val = get_char_property_and_overlay
6379 (make_number (pos), Qdisplay, Qnil, &overlay))
6380 && (OVERLAYP (overlay)
6381 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6382 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6383 {
6384 RESTORE_IT (it, it, it2data);
6385 goto replaced;
6386 }
6387
6388 /* Newline is not replaced by anything -- so we are done. */
6389 RESTORE_IT (it, it, it2data);
6390 break;
6391
6392 replaced:
6393 if (beg < BEGV)
6394 beg = BEGV;
6395 IT_CHARPOS (*it) = beg;
6396 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6397 }
6398 }
6399
6400 it->continuation_lines_width = 0;
6401
6402 eassert (IT_CHARPOS (*it) >= BEGV);
6403 eassert (IT_CHARPOS (*it) == BEGV
6404 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6405 CHECK_IT (it);
6406 }
6407
6408
6409 /* Reseat iterator IT at the previous visible line start. Skip
6410 invisible text that is so either due to text properties or due to
6411 selective display. At the end, update IT's overlay information,
6412 face information etc. */
6413
6414 void
6415 reseat_at_previous_visible_line_start (struct it *it)
6416 {
6417 back_to_previous_visible_line_start (it);
6418 reseat (it, it->current.pos, true);
6419 CHECK_IT (it);
6420 }
6421
6422
6423 /* Reseat iterator IT on the next visible line start in the current
6424 buffer. ON_NEWLINE_P means position IT on the newline
6425 preceding the line start. Skip over invisible text that is so
6426 because of selective display. Compute faces, overlays etc at the
6427 new position. Note that this function does not skip over text that
6428 is invisible because of text properties. */
6429
6430 static void
6431 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6432 {
6433 bool skipped_p = false;
6434 struct bidi_it bidi_it_prev;
6435 bool newline_found_p
6436 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6437
6438 /* Skip over lines that are invisible because they are indented
6439 more than the value of IT->selective. */
6440 if (it->selective > 0)
6441 while (IT_CHARPOS (*it) < ZV
6442 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6443 it->selective))
6444 {
6445 eassert (IT_BYTEPOS (*it) == BEGV
6446 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6447 newline_found_p =
6448 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6449 }
6450
6451 /* Position on the newline if that's what's requested. */
6452 if (on_newline_p && newline_found_p)
6453 {
6454 if (STRINGP (it->string))
6455 {
6456 if (IT_STRING_CHARPOS (*it) > 0)
6457 {
6458 if (!it->bidi_p)
6459 {
6460 --IT_STRING_CHARPOS (*it);
6461 --IT_STRING_BYTEPOS (*it);
6462 }
6463 else
6464 {
6465 /* We need to restore the bidi iterator to the state
6466 it had on the newline, and resync the IT's
6467 position with that. */
6468 it->bidi_it = bidi_it_prev;
6469 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6470 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6471 }
6472 }
6473 }
6474 else if (IT_CHARPOS (*it) > BEGV)
6475 {
6476 if (!it->bidi_p)
6477 {
6478 --IT_CHARPOS (*it);
6479 --IT_BYTEPOS (*it);
6480 }
6481 else
6482 {
6483 /* We need to restore the bidi iterator to the state it
6484 had on the newline and resync IT with that. */
6485 it->bidi_it = bidi_it_prev;
6486 IT_CHARPOS (*it) = it->bidi_it.charpos;
6487 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6488 }
6489 reseat (it, it->current.pos, false);
6490 }
6491 }
6492 else if (skipped_p)
6493 reseat (it, it->current.pos, false);
6494
6495 CHECK_IT (it);
6496 }
6497
6498
6499 \f
6500 /***********************************************************************
6501 Changing an iterator's position
6502 ***********************************************************************/
6503
6504 /* Change IT's current position to POS in current_buffer.
6505 If FORCE_P, always check for text properties at the new position.
6506 Otherwise, text properties are only looked up if POS >=
6507 IT->check_charpos of a property. */
6508
6509 static void
6510 reseat (struct it *it, struct text_pos pos, bool force_p)
6511 {
6512 ptrdiff_t original_pos = IT_CHARPOS (*it);
6513
6514 reseat_1 (it, pos, false);
6515
6516 /* Determine where to check text properties. Avoid doing it
6517 where possible because text property lookup is very expensive. */
6518 if (force_p
6519 || CHARPOS (pos) > it->stop_charpos
6520 || CHARPOS (pos) < original_pos)
6521 {
6522 if (it->bidi_p)
6523 {
6524 /* For bidi iteration, we need to prime prev_stop and
6525 base_level_stop with our best estimations. */
6526 /* Implementation note: Of course, POS is not necessarily a
6527 stop position, so assigning prev_pos to it is a lie; we
6528 should have called compute_stop_backwards. However, if
6529 the current buffer does not include any R2L characters,
6530 that call would be a waste of cycles, because the
6531 iterator will never move back, and thus never cross this
6532 "fake" stop position. So we delay that backward search
6533 until the time we really need it, in next_element_from_buffer. */
6534 if (CHARPOS (pos) != it->prev_stop)
6535 it->prev_stop = CHARPOS (pos);
6536 if (CHARPOS (pos) < it->base_level_stop)
6537 it->base_level_stop = 0; /* meaning it's unknown */
6538 handle_stop (it);
6539 }
6540 else
6541 {
6542 handle_stop (it);
6543 it->prev_stop = it->base_level_stop = 0;
6544 }
6545
6546 }
6547
6548 CHECK_IT (it);
6549 }
6550
6551
6552 /* Change IT's buffer position to POS. SET_STOP_P means set
6553 IT->stop_pos to POS, also. */
6554
6555 static void
6556 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6557 {
6558 /* Don't call this function when scanning a C string. */
6559 eassert (it->s == NULL);
6560
6561 /* POS must be a reasonable value. */
6562 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6563
6564 it->current.pos = it->position = pos;
6565 it->end_charpos = ZV;
6566 it->dpvec = NULL;
6567 it->current.dpvec_index = -1;
6568 it->current.overlay_string_index = -1;
6569 IT_STRING_CHARPOS (*it) = -1;
6570 IT_STRING_BYTEPOS (*it) = -1;
6571 it->string = Qnil;
6572 it->method = GET_FROM_BUFFER;
6573 it->object = it->w->contents;
6574 it->area = TEXT_AREA;
6575 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6576 it->sp = 0;
6577 it->string_from_display_prop_p = false;
6578 it->string_from_prefix_prop_p = false;
6579
6580 it->from_disp_prop_p = false;
6581 it->face_before_selective_p = false;
6582 if (it->bidi_p)
6583 {
6584 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6585 &it->bidi_it);
6586 bidi_unshelve_cache (NULL, false);
6587 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6588 it->bidi_it.string.s = NULL;
6589 it->bidi_it.string.lstring = Qnil;
6590 it->bidi_it.string.bufpos = 0;
6591 it->bidi_it.string.from_disp_str = false;
6592 it->bidi_it.string.unibyte = false;
6593 it->bidi_it.w = it->w;
6594 }
6595
6596 if (set_stop_p)
6597 {
6598 it->stop_charpos = CHARPOS (pos);
6599 it->base_level_stop = CHARPOS (pos);
6600 }
6601 /* This make the information stored in it->cmp_it invalidate. */
6602 it->cmp_it.id = -1;
6603 }
6604
6605
6606 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6607 If S is non-null, it is a C string to iterate over. Otherwise,
6608 STRING gives a Lisp string to iterate over.
6609
6610 If PRECISION > 0, don't return more then PRECISION number of
6611 characters from the string.
6612
6613 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6614 characters have been returned. FIELD_WIDTH < 0 means an infinite
6615 field width.
6616
6617 MULTIBYTE = 0 means disable processing of multibyte characters,
6618 MULTIBYTE > 0 means enable it,
6619 MULTIBYTE < 0 means use IT->multibyte_p.
6620
6621 IT must be initialized via a prior call to init_iterator before
6622 calling this function. */
6623
6624 static void
6625 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6626 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6627 int multibyte)
6628 {
6629 /* No text property checks performed by default, but see below. */
6630 it->stop_charpos = -1;
6631
6632 /* Set iterator position and end position. */
6633 memset (&it->current, 0, sizeof it->current);
6634 it->current.overlay_string_index = -1;
6635 it->current.dpvec_index = -1;
6636 eassert (charpos >= 0);
6637
6638 /* If STRING is specified, use its multibyteness, otherwise use the
6639 setting of MULTIBYTE, if specified. */
6640 if (multibyte >= 0)
6641 it->multibyte_p = multibyte > 0;
6642
6643 /* Bidirectional reordering of strings is controlled by the default
6644 value of bidi-display-reordering. Don't try to reorder while
6645 loading loadup.el, as the necessary character property tables are
6646 not yet available. */
6647 it->bidi_p =
6648 !redisplay__inhibit_bidi
6649 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6650
6651 if (s == NULL)
6652 {
6653 eassert (STRINGP (string));
6654 it->string = string;
6655 it->s = NULL;
6656 it->end_charpos = it->string_nchars = SCHARS (string);
6657 it->method = GET_FROM_STRING;
6658 it->current.string_pos = string_pos (charpos, string);
6659
6660 if (it->bidi_p)
6661 {
6662 it->bidi_it.string.lstring = string;
6663 it->bidi_it.string.s = NULL;
6664 it->bidi_it.string.schars = it->end_charpos;
6665 it->bidi_it.string.bufpos = 0;
6666 it->bidi_it.string.from_disp_str = false;
6667 it->bidi_it.string.unibyte = !it->multibyte_p;
6668 it->bidi_it.w = it->w;
6669 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6670 FRAME_WINDOW_P (it->f), &it->bidi_it);
6671 }
6672 }
6673 else
6674 {
6675 it->s = (const unsigned char *) s;
6676 it->string = Qnil;
6677
6678 /* Note that we use IT->current.pos, not it->current.string_pos,
6679 for displaying C strings. */
6680 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6681 if (it->multibyte_p)
6682 {
6683 it->current.pos = c_string_pos (charpos, s, true);
6684 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6685 }
6686 else
6687 {
6688 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6689 it->end_charpos = it->string_nchars = strlen (s);
6690 }
6691
6692 if (it->bidi_p)
6693 {
6694 it->bidi_it.string.lstring = Qnil;
6695 it->bidi_it.string.s = (const unsigned char *) s;
6696 it->bidi_it.string.schars = it->end_charpos;
6697 it->bidi_it.string.bufpos = 0;
6698 it->bidi_it.string.from_disp_str = false;
6699 it->bidi_it.string.unibyte = !it->multibyte_p;
6700 it->bidi_it.w = it->w;
6701 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6702 &it->bidi_it);
6703 }
6704 it->method = GET_FROM_C_STRING;
6705 }
6706
6707 /* PRECISION > 0 means don't return more than PRECISION characters
6708 from the string. */
6709 if (precision > 0 && it->end_charpos - charpos > precision)
6710 {
6711 it->end_charpos = it->string_nchars = charpos + precision;
6712 if (it->bidi_p)
6713 it->bidi_it.string.schars = it->end_charpos;
6714 }
6715
6716 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6717 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6718 FIELD_WIDTH < 0 means infinite field width. This is useful for
6719 padding with `-' at the end of a mode line. */
6720 if (field_width < 0)
6721 field_width = INFINITY;
6722 /* Implementation note: We deliberately don't enlarge
6723 it->bidi_it.string.schars here to fit it->end_charpos, because
6724 the bidi iterator cannot produce characters out of thin air. */
6725 if (field_width > it->end_charpos - charpos)
6726 it->end_charpos = charpos + field_width;
6727
6728 /* Use the standard display table for displaying strings. */
6729 if (DISP_TABLE_P (Vstandard_display_table))
6730 it->dp = XCHAR_TABLE (Vstandard_display_table);
6731
6732 it->stop_charpos = charpos;
6733 it->prev_stop = charpos;
6734 it->base_level_stop = 0;
6735 if (it->bidi_p)
6736 {
6737 it->bidi_it.first_elt = true;
6738 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6739 it->bidi_it.disp_pos = -1;
6740 }
6741 if (s == NULL && it->multibyte_p)
6742 {
6743 ptrdiff_t endpos = SCHARS (it->string);
6744 if (endpos > it->end_charpos)
6745 endpos = it->end_charpos;
6746 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6747 it->string);
6748 }
6749 CHECK_IT (it);
6750 }
6751
6752
6753 \f
6754 /***********************************************************************
6755 Iteration
6756 ***********************************************************************/
6757
6758 /* Map enum it_method value to corresponding next_element_from_* function. */
6759
6760 typedef bool (*next_element_function) (struct it *);
6761
6762 static next_element_function const get_next_element[NUM_IT_METHODS] =
6763 {
6764 next_element_from_buffer,
6765 next_element_from_display_vector,
6766 next_element_from_string,
6767 next_element_from_c_string,
6768 next_element_from_image,
6769 next_element_from_stretch,
6770 next_element_from_xwidget,
6771 };
6772
6773 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6774
6775
6776 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6777 (possibly with the following characters). */
6778
6779 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6780 ((IT)->cmp_it.id >= 0 \
6781 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6782 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6783 END_CHARPOS, (IT)->w, \
6784 FACE_FROM_ID_OR_NULL ((IT)->f, \
6785 (IT)->face_id), \
6786 (IT)->string)))
6787
6788
6789 /* Lookup the char-table Vglyphless_char_display for character C (-1
6790 if we want information for no-font case), and return the display
6791 method symbol. By side-effect, update it->what and
6792 it->glyphless_method. This function is called from
6793 get_next_display_element for each character element, and from
6794 x_produce_glyphs when no suitable font was found. */
6795
6796 Lisp_Object
6797 lookup_glyphless_char_display (int c, struct it *it)
6798 {
6799 Lisp_Object glyphless_method = Qnil;
6800
6801 if (CHAR_TABLE_P (Vglyphless_char_display)
6802 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6803 {
6804 if (c >= 0)
6805 {
6806 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6807 if (CONSP (glyphless_method))
6808 glyphless_method = FRAME_WINDOW_P (it->f)
6809 ? XCAR (glyphless_method)
6810 : XCDR (glyphless_method);
6811 }
6812 else
6813 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6814 }
6815
6816 retry:
6817 if (NILP (glyphless_method))
6818 {
6819 if (c >= 0)
6820 /* The default is to display the character by a proper font. */
6821 return Qnil;
6822 /* The default for the no-font case is to display an empty box. */
6823 glyphless_method = Qempty_box;
6824 }
6825 if (EQ (glyphless_method, Qzero_width))
6826 {
6827 if (c >= 0)
6828 return glyphless_method;
6829 /* This method can't be used for the no-font case. */
6830 glyphless_method = Qempty_box;
6831 }
6832 if (EQ (glyphless_method, Qthin_space))
6833 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6834 else if (EQ (glyphless_method, Qempty_box))
6835 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6836 else if (EQ (glyphless_method, Qhex_code))
6837 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6838 else if (STRINGP (glyphless_method))
6839 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6840 else
6841 {
6842 /* Invalid value. We use the default method. */
6843 glyphless_method = Qnil;
6844 goto retry;
6845 }
6846 it->what = IT_GLYPHLESS;
6847 return glyphless_method;
6848 }
6849
6850 /* Merge escape glyph face and cache the result. */
6851
6852 static struct frame *last_escape_glyph_frame = NULL;
6853 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6854 static int last_escape_glyph_merged_face_id = 0;
6855
6856 static int
6857 merge_escape_glyph_face (struct it *it)
6858 {
6859 int face_id;
6860
6861 if (it->f == last_escape_glyph_frame
6862 && it->face_id == last_escape_glyph_face_id)
6863 face_id = last_escape_glyph_merged_face_id;
6864 else
6865 {
6866 /* Merge the `escape-glyph' face into the current face. */
6867 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6868 last_escape_glyph_frame = it->f;
6869 last_escape_glyph_face_id = it->face_id;
6870 last_escape_glyph_merged_face_id = face_id;
6871 }
6872 return face_id;
6873 }
6874
6875 /* Likewise for glyphless glyph face. */
6876
6877 static struct frame *last_glyphless_glyph_frame = NULL;
6878 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6879 static int last_glyphless_glyph_merged_face_id = 0;
6880
6881 int
6882 merge_glyphless_glyph_face (struct it *it)
6883 {
6884 int face_id;
6885
6886 if (it->f == last_glyphless_glyph_frame
6887 && it->face_id == last_glyphless_glyph_face_id)
6888 face_id = last_glyphless_glyph_merged_face_id;
6889 else
6890 {
6891 /* Merge the `glyphless-char' face into the current face. */
6892 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6893 last_glyphless_glyph_frame = it->f;
6894 last_glyphless_glyph_face_id = it->face_id;
6895 last_glyphless_glyph_merged_face_id = face_id;
6896 }
6897 return face_id;
6898 }
6899
6900 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6901 be called before redisplaying windows, and when the frame's face
6902 cache is freed. */
6903 void
6904 forget_escape_and_glyphless_faces (void)
6905 {
6906 last_escape_glyph_frame = NULL;
6907 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6908 last_glyphless_glyph_frame = NULL;
6909 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6910 }
6911
6912 /* Load IT's display element fields with information about the next
6913 display element from the current position of IT. Value is false if
6914 end of buffer (or C string) is reached. */
6915
6916 static bool
6917 get_next_display_element (struct it *it)
6918 {
6919 /* True means that we found a display element. False means that
6920 we hit the end of what we iterate over. Performance note: the
6921 function pointer `method' used here turns out to be faster than
6922 using a sequence of if-statements. */
6923 bool success_p;
6924
6925 get_next:
6926 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6927
6928 if (it->what == IT_CHARACTER)
6929 {
6930 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6931 and only if (a) the resolved directionality of that character
6932 is R..." */
6933 /* FIXME: Do we need an exception for characters from display
6934 tables? */
6935 if (it->bidi_p && it->bidi_it.type == STRONG_R
6936 && !inhibit_bidi_mirroring)
6937 it->c = bidi_mirror_char (it->c);
6938 /* Map via display table or translate control characters.
6939 IT->c, IT->len etc. have been set to the next character by
6940 the function call above. If we have a display table, and it
6941 contains an entry for IT->c, translate it. Don't do this if
6942 IT->c itself comes from a display table, otherwise we could
6943 end up in an infinite recursion. (An alternative could be to
6944 count the recursion depth of this function and signal an
6945 error when a certain maximum depth is reached.) Is it worth
6946 it? */
6947 if (success_p && it->dpvec == NULL)
6948 {
6949 Lisp_Object dv;
6950 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6951 bool nonascii_space_p = false;
6952 bool nonascii_hyphen_p = false;
6953 int c = it->c; /* This is the character to display. */
6954
6955 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6956 {
6957 eassert (SINGLE_BYTE_CHAR_P (c));
6958 if (unibyte_display_via_language_environment)
6959 {
6960 c = DECODE_CHAR (unibyte, c);
6961 if (c < 0)
6962 c = BYTE8_TO_CHAR (it->c);
6963 }
6964 else
6965 c = BYTE8_TO_CHAR (it->c);
6966 }
6967
6968 if (it->dp
6969 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6970 VECTORP (dv)))
6971 {
6972 struct Lisp_Vector *v = XVECTOR (dv);
6973
6974 /* Return the first character from the display table
6975 entry, if not empty. If empty, don't display the
6976 current character. */
6977 if (v->header.size)
6978 {
6979 it->dpvec_char_len = it->len;
6980 it->dpvec = v->contents;
6981 it->dpend = v->contents + v->header.size;
6982 it->current.dpvec_index = 0;
6983 it->dpvec_face_id = -1;
6984 it->saved_face_id = it->face_id;
6985 it->method = GET_FROM_DISPLAY_VECTOR;
6986 it->ellipsis_p = false;
6987 }
6988 else
6989 {
6990 set_iterator_to_next (it, false);
6991 }
6992 goto get_next;
6993 }
6994
6995 if (! NILP (lookup_glyphless_char_display (c, it)))
6996 {
6997 if (it->what == IT_GLYPHLESS)
6998 goto done;
6999 /* Don't display this character. */
7000 set_iterator_to_next (it, false);
7001 goto get_next;
7002 }
7003
7004 /* If `nobreak-char-display' is non-nil, we display
7005 non-ASCII spaces and hyphens specially. */
7006 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7007 {
7008 if (c == NO_BREAK_SPACE)
7009 nonascii_space_p = true;
7010 else if (c == SOFT_HYPHEN || c == HYPHEN
7011 || c == NON_BREAKING_HYPHEN)
7012 nonascii_hyphen_p = true;
7013 }
7014
7015 /* Translate control characters into `\003' or `^C' form.
7016 Control characters coming from a display table entry are
7017 currently not translated because we use IT->dpvec to hold
7018 the translation. This could easily be changed but I
7019 don't believe that it is worth doing.
7020
7021 The characters handled by `nobreak-char-display' must be
7022 translated too.
7023
7024 Non-printable characters and raw-byte characters are also
7025 translated to octal form. */
7026 if (((c < ' ' || c == 127) /* ASCII control chars. */
7027 ? (it->area != TEXT_AREA
7028 /* In mode line, treat \n, \t like other crl chars. */
7029 || (c != '\t'
7030 && it->glyph_row
7031 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7032 || (c != '\n' && c != '\t'))
7033 : (nonascii_space_p
7034 || nonascii_hyphen_p
7035 || CHAR_BYTE8_P (c)
7036 || ! CHAR_PRINTABLE_P (c))))
7037 {
7038 /* C is a control character, non-ASCII space/hyphen,
7039 raw-byte, or a non-printable character which must be
7040 displayed either as '\003' or as `^C' where the '\\'
7041 and '^' can be defined in the display table. Fill
7042 IT->ctl_chars with glyphs for what we have to
7043 display. Then, set IT->dpvec to these glyphs. */
7044 Lisp_Object gc;
7045 int ctl_len;
7046 int face_id;
7047 int lface_id = 0;
7048 int escape_glyph;
7049
7050 /* Handle control characters with ^. */
7051
7052 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7053 {
7054 int g;
7055
7056 g = '^'; /* default glyph for Control */
7057 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7058 if (it->dp
7059 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7060 {
7061 g = GLYPH_CODE_CHAR (gc);
7062 lface_id = GLYPH_CODE_FACE (gc);
7063 }
7064
7065 face_id = (lface_id
7066 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7067 : merge_escape_glyph_face (it));
7068
7069 XSETINT (it->ctl_chars[0], g);
7070 XSETINT (it->ctl_chars[1], c ^ 0100);
7071 ctl_len = 2;
7072 goto display_control;
7073 }
7074
7075 /* Handle non-ascii space in the mode where it only gets
7076 highlighting. */
7077
7078 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7079 {
7080 /* Merge `nobreak-space' into the current face. */
7081 face_id = merge_faces (it->f, Qnobreak_space, 0,
7082 it->face_id);
7083 XSETINT (it->ctl_chars[0], ' ');
7084 ctl_len = 1;
7085 goto display_control;
7086 }
7087
7088 /* Handle non-ascii hyphens in the mode where it only
7089 gets highlighting. */
7090
7091 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7092 {
7093 /* Merge `nobreak-space' into the current face. */
7094 face_id = merge_faces (it->f, Qnobreak_hyphen, 0,
7095 it->face_id);
7096 XSETINT (it->ctl_chars[0], '-');
7097 ctl_len = 1;
7098 goto display_control;
7099 }
7100
7101 /* Handle sequences that start with the "escape glyph". */
7102
7103 /* the default escape glyph is \. */
7104 escape_glyph = '\\';
7105
7106 if (it->dp
7107 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7108 {
7109 escape_glyph = GLYPH_CODE_CHAR (gc);
7110 lface_id = GLYPH_CODE_FACE (gc);
7111 }
7112
7113 face_id = (lface_id
7114 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7115 : merge_escape_glyph_face (it));
7116
7117 /* Draw non-ASCII space/hyphen with escape glyph: */
7118
7119 if (nonascii_space_p || nonascii_hyphen_p)
7120 {
7121 XSETINT (it->ctl_chars[0], escape_glyph);
7122 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7123 ctl_len = 2;
7124 goto display_control;
7125 }
7126
7127 {
7128 char str[10];
7129 int len, i;
7130
7131 if (CHAR_BYTE8_P (c))
7132 /* Display \200 instead of \17777600. */
7133 c = CHAR_TO_BYTE8 (c);
7134 len = sprintf (str, "%03o", c + 0u);
7135
7136 XSETINT (it->ctl_chars[0], escape_glyph);
7137 for (i = 0; i < len; i++)
7138 XSETINT (it->ctl_chars[i + 1], str[i]);
7139 ctl_len = len + 1;
7140 }
7141
7142 display_control:
7143 /* Set up IT->dpvec and return first character from it. */
7144 it->dpvec_char_len = it->len;
7145 it->dpvec = it->ctl_chars;
7146 it->dpend = it->dpvec + ctl_len;
7147 it->current.dpvec_index = 0;
7148 it->dpvec_face_id = face_id;
7149 it->saved_face_id = it->face_id;
7150 it->method = GET_FROM_DISPLAY_VECTOR;
7151 it->ellipsis_p = false;
7152 goto get_next;
7153 }
7154 it->char_to_display = c;
7155 }
7156 else if (success_p)
7157 {
7158 it->char_to_display = it->c;
7159 }
7160 }
7161
7162 #ifdef HAVE_WINDOW_SYSTEM
7163 /* Adjust face id for a multibyte character. There are no multibyte
7164 character in unibyte text. */
7165 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7166 && it->multibyte_p
7167 && success_p
7168 && FRAME_WINDOW_P (it->f))
7169 {
7170 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7171
7172 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7173 {
7174 /* Automatic composition with glyph-string. */
7175 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7176
7177 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7178 }
7179 else
7180 {
7181 ptrdiff_t pos = (it->s ? -1
7182 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7183 : IT_CHARPOS (*it));
7184 int c;
7185
7186 if (it->what == IT_CHARACTER)
7187 c = it->char_to_display;
7188 else
7189 {
7190 struct composition *cmp = composition_table[it->cmp_it.id];
7191 int i;
7192
7193 c = ' ';
7194 for (i = 0; i < cmp->glyph_len; i++)
7195 /* TAB in a composition means display glyphs with
7196 padding space on the left or right. */
7197 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7198 break;
7199 }
7200 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7201 }
7202 }
7203 #endif /* HAVE_WINDOW_SYSTEM */
7204
7205 done:
7206 /* Is this character the last one of a run of characters with
7207 box? If yes, set IT->end_of_box_run_p to true. */
7208 if (it->face_box_p
7209 && it->s == NULL)
7210 {
7211 if (it->method == GET_FROM_STRING && it->sp)
7212 {
7213 int face_id = underlying_face_id (it);
7214 struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
7215
7216 if (face)
7217 {
7218 if (face->box == FACE_NO_BOX)
7219 {
7220 /* If the box comes from face properties in a
7221 display string, check faces in that string. */
7222 int string_face_id = face_after_it_pos (it);
7223 it->end_of_box_run_p
7224 = (FACE_FROM_ID (it->f, string_face_id)->box
7225 == FACE_NO_BOX);
7226 }
7227 /* Otherwise, the box comes from the underlying face.
7228 If this is the last string character displayed, check
7229 the next buffer location. */
7230 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7231 /* n_overlay_strings is unreliable unless
7232 overlay_string_index is non-negative. */
7233 && ((it->current.overlay_string_index >= 0
7234 && (it->current.overlay_string_index
7235 == it->n_overlay_strings - 1))
7236 /* A string from display property. */
7237 || it->from_disp_prop_p))
7238 {
7239 ptrdiff_t ignore;
7240 int next_face_id;
7241 bool text_from_string = false;
7242 /* Normally, the next buffer location is stored in
7243 IT->current.pos... */
7244 struct text_pos pos = it->current.pos;
7245
7246 /* ...but for a string from a display property, the
7247 next buffer position is stored in the 'position'
7248 member of the iteration stack slot below the
7249 current one, see handle_single_display_spec. By
7250 contrast, it->current.pos was not yet updated to
7251 point to that buffer position; that will happen
7252 in pop_it, after we finish displaying the current
7253 string. Note that we already checked above that
7254 it->sp is positive, so subtracting one from it is
7255 safe. */
7256 if (it->from_disp_prop_p)
7257 {
7258 int stackp = it->sp - 1;
7259
7260 /* Find the stack level with data from buffer. */
7261 while (stackp >= 0
7262 && STRINGP ((it->stack + stackp)->string))
7263 stackp--;
7264 if (stackp < 0)
7265 {
7266 /* If no stack slot was found for iterating
7267 a buffer, we are displaying text from a
7268 string, most probably the mode line or
7269 the header line, and that string has a
7270 display string on some of its
7271 characters. */
7272 text_from_string = true;
7273 pos = it->stack[it->sp - 1].position;
7274 }
7275 else
7276 pos = (it->stack + stackp)->position;
7277 }
7278 else
7279 INC_TEXT_POS (pos, it->multibyte_p);
7280
7281 if (text_from_string)
7282 {
7283 Lisp_Object base_string = it->stack[it->sp - 1].string;
7284
7285 if (CHARPOS (pos) >= SCHARS (base_string) - 1)
7286 it->end_of_box_run_p = true;
7287 else
7288 {
7289 next_face_id
7290 = face_at_string_position (it->w, base_string,
7291 CHARPOS (pos), 0,
7292 &ignore, face_id, false);
7293 it->end_of_box_run_p
7294 = (FACE_FROM_ID (it->f, next_face_id)->box
7295 == FACE_NO_BOX);
7296 }
7297 }
7298 else if (CHARPOS (pos) >= ZV)
7299 it->end_of_box_run_p = true;
7300 else
7301 {
7302 next_face_id =
7303 face_at_buffer_position (it->w, CHARPOS (pos), &ignore,
7304 CHARPOS (pos)
7305 + TEXT_PROP_DISTANCE_LIMIT,
7306 false, -1);
7307 it->end_of_box_run_p
7308 = (FACE_FROM_ID (it->f, next_face_id)->box
7309 == FACE_NO_BOX);
7310 }
7311 }
7312 }
7313 }
7314 /* next_element_from_display_vector sets this flag according to
7315 faces of the display vector glyphs, see there. */
7316 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7317 {
7318 int face_id = face_after_it_pos (it);
7319 it->end_of_box_run_p
7320 = (face_id != it->face_id
7321 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7322 }
7323 }
7324 /* If we reached the end of the object we've been iterating (e.g., a
7325 display string or an overlay string), and there's something on
7326 IT->stack, proceed with what's on the stack. It doesn't make
7327 sense to return false if there's unprocessed stuff on the stack,
7328 because otherwise that stuff will never be displayed. */
7329 if (!success_p && it->sp > 0)
7330 {
7331 set_iterator_to_next (it, false);
7332 success_p = get_next_display_element (it);
7333 }
7334
7335 /* Value is false if end of buffer or string reached. */
7336 return success_p;
7337 }
7338
7339
7340 /* Move IT to the next display element.
7341
7342 RESEAT_P means if called on a newline in buffer text,
7343 skip to the next visible line start.
7344
7345 Functions get_next_display_element and set_iterator_to_next are
7346 separate because I find this arrangement easier to handle than a
7347 get_next_display_element function that also increments IT's
7348 position. The way it is we can first look at an iterator's current
7349 display element, decide whether it fits on a line, and if it does,
7350 increment the iterator position. The other way around we probably
7351 would either need a flag indicating whether the iterator has to be
7352 incremented the next time, or we would have to implement a
7353 decrement position function which would not be easy to write. */
7354
7355 void
7356 set_iterator_to_next (struct it *it, bool reseat_p)
7357 {
7358 /* Reset flags indicating start and end of a sequence of characters
7359 with box. Reset them at the start of this function because
7360 moving the iterator to a new position might set them. */
7361 it->start_of_box_run_p = it->end_of_box_run_p = false;
7362
7363 switch (it->method)
7364 {
7365 case GET_FROM_BUFFER:
7366 /* The current display element of IT is a character from
7367 current_buffer. Advance in the buffer, and maybe skip over
7368 invisible lines that are so because of selective display. */
7369 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7370 reseat_at_next_visible_line_start (it, false);
7371 else if (it->cmp_it.id >= 0)
7372 {
7373 /* We are currently getting glyphs from a composition. */
7374 if (! it->bidi_p)
7375 {
7376 IT_CHARPOS (*it) += it->cmp_it.nchars;
7377 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7378 }
7379 else
7380 {
7381 int i;
7382
7383 /* Update IT's char/byte positions to point to the first
7384 character of the next grapheme cluster, or to the
7385 character visually after the current composition. */
7386 for (i = 0; i < it->cmp_it.nchars; i++)
7387 bidi_move_to_visually_next (&it->bidi_it);
7388 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7389 IT_CHARPOS (*it) = it->bidi_it.charpos;
7390 }
7391
7392 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7393 && it->cmp_it.to < it->cmp_it.nglyphs)
7394 {
7395 /* Composition created while scanning forward. Proceed
7396 to the next grapheme cluster. */
7397 it->cmp_it.from = it->cmp_it.to;
7398 }
7399 else if ((it->bidi_p && it->cmp_it.reversed_p)
7400 && it->cmp_it.from > 0)
7401 {
7402 /* Composition created while scanning backward. Proceed
7403 to the previous grapheme cluster. */
7404 it->cmp_it.to = it->cmp_it.from;
7405 }
7406 else
7407 {
7408 /* No more grapheme clusters in this composition.
7409 Find the next stop position. */
7410 ptrdiff_t stop = it->end_charpos;
7411
7412 if (it->bidi_it.scan_dir < 0)
7413 /* Now we are scanning backward and don't know
7414 where to stop. */
7415 stop = -1;
7416 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7417 IT_BYTEPOS (*it), stop, Qnil);
7418 }
7419 }
7420 else
7421 {
7422 eassert (it->len != 0);
7423
7424 if (!it->bidi_p)
7425 {
7426 IT_BYTEPOS (*it) += it->len;
7427 IT_CHARPOS (*it) += 1;
7428 }
7429 else
7430 {
7431 int prev_scan_dir = it->bidi_it.scan_dir;
7432 /* If this is a new paragraph, determine its base
7433 direction (a.k.a. its base embedding level). */
7434 if (it->bidi_it.new_paragraph)
7435 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7436 false);
7437 bidi_move_to_visually_next (&it->bidi_it);
7438 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7439 IT_CHARPOS (*it) = it->bidi_it.charpos;
7440 if (prev_scan_dir != it->bidi_it.scan_dir)
7441 {
7442 /* As the scan direction was changed, we must
7443 re-compute the stop position for composition. */
7444 ptrdiff_t stop = it->end_charpos;
7445 if (it->bidi_it.scan_dir < 0)
7446 stop = -1;
7447 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7448 IT_BYTEPOS (*it), stop, Qnil);
7449 }
7450 }
7451 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7452 }
7453 break;
7454
7455 case GET_FROM_C_STRING:
7456 /* Current display element of IT is from a C string. */
7457 if (!it->bidi_p
7458 /* If the string position is beyond string's end, it means
7459 next_element_from_c_string is padding the string with
7460 blanks, in which case we bypass the bidi iterator,
7461 because it cannot deal with such virtual characters. */
7462 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7463 {
7464 IT_BYTEPOS (*it) += it->len;
7465 IT_CHARPOS (*it) += 1;
7466 }
7467 else
7468 {
7469 bidi_move_to_visually_next (&it->bidi_it);
7470 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7471 IT_CHARPOS (*it) = it->bidi_it.charpos;
7472 }
7473 break;
7474
7475 case GET_FROM_DISPLAY_VECTOR:
7476 /* Current display element of IT is from a display table entry.
7477 Advance in the display table definition. Reset it to null if
7478 end reached, and continue with characters from buffers/
7479 strings. */
7480 ++it->current.dpvec_index;
7481
7482 /* Restore face of the iterator to what they were before the
7483 display vector entry (these entries may contain faces). */
7484 it->face_id = it->saved_face_id;
7485
7486 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7487 {
7488 bool recheck_faces = it->ellipsis_p;
7489
7490 if (it->s)
7491 it->method = GET_FROM_C_STRING;
7492 else if (STRINGP (it->string))
7493 it->method = GET_FROM_STRING;
7494 else
7495 {
7496 it->method = GET_FROM_BUFFER;
7497 it->object = it->w->contents;
7498 }
7499
7500 it->dpvec = NULL;
7501 it->current.dpvec_index = -1;
7502
7503 /* Skip over characters which were displayed via IT->dpvec. */
7504 if (it->dpvec_char_len < 0)
7505 reseat_at_next_visible_line_start (it, true);
7506 else if (it->dpvec_char_len > 0)
7507 {
7508 it->len = it->dpvec_char_len;
7509 set_iterator_to_next (it, reseat_p);
7510 }
7511
7512 /* Maybe recheck faces after display vector. */
7513 if (recheck_faces)
7514 {
7515 if (it->method == GET_FROM_STRING)
7516 it->stop_charpos = IT_STRING_CHARPOS (*it);
7517 else
7518 it->stop_charpos = IT_CHARPOS (*it);
7519 }
7520 }
7521 break;
7522
7523 case GET_FROM_STRING:
7524 /* Current display element is a character from a Lisp string. */
7525 eassert (it->s == NULL && STRINGP (it->string));
7526 /* Don't advance past string end. These conditions are true
7527 when set_iterator_to_next is called at the end of
7528 get_next_display_element, in which case the Lisp string is
7529 already exhausted, and all we want is pop the iterator
7530 stack. */
7531 if (it->current.overlay_string_index >= 0)
7532 {
7533 /* This is an overlay string, so there's no padding with
7534 spaces, and the number of characters in the string is
7535 where the string ends. */
7536 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7537 goto consider_string_end;
7538 }
7539 else
7540 {
7541 /* Not an overlay string. There could be padding, so test
7542 against it->end_charpos. */
7543 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7544 goto consider_string_end;
7545 }
7546 if (it->cmp_it.id >= 0)
7547 {
7548 /* We are delivering display elements from a composition.
7549 Update the string position past the grapheme cluster
7550 we've just processed. */
7551 if (! it->bidi_p)
7552 {
7553 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7554 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7555 }
7556 else
7557 {
7558 int i;
7559
7560 for (i = 0; i < it->cmp_it.nchars; i++)
7561 bidi_move_to_visually_next (&it->bidi_it);
7562 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7563 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7564 }
7565
7566 /* Did we exhaust all the grapheme clusters of this
7567 composition? */
7568 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7569 && (it->cmp_it.to < it->cmp_it.nglyphs))
7570 {
7571 /* Not all the grapheme clusters were processed yet;
7572 advance to the next cluster. */
7573 it->cmp_it.from = it->cmp_it.to;
7574 }
7575 else if ((it->bidi_p && it->cmp_it.reversed_p)
7576 && it->cmp_it.from > 0)
7577 {
7578 /* Likewise: advance to the next cluster, but going in
7579 the reverse direction. */
7580 it->cmp_it.to = it->cmp_it.from;
7581 }
7582 else
7583 {
7584 /* This composition was fully processed; find the next
7585 candidate place for checking for composed
7586 characters. */
7587 /* Always limit string searches to the string length;
7588 any padding spaces are not part of the string, and
7589 there cannot be any compositions in that padding. */
7590 ptrdiff_t stop = SCHARS (it->string);
7591
7592 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7593 stop = -1;
7594 else if (it->end_charpos < stop)
7595 {
7596 /* Cf. PRECISION in reseat_to_string: we might be
7597 limited in how many of the string characters we
7598 need to deliver. */
7599 stop = it->end_charpos;
7600 }
7601 composition_compute_stop_pos (&it->cmp_it,
7602 IT_STRING_CHARPOS (*it),
7603 IT_STRING_BYTEPOS (*it), stop,
7604 it->string);
7605 }
7606 }
7607 else
7608 {
7609 if (!it->bidi_p
7610 /* If the string position is beyond string's end, it
7611 means next_element_from_string is padding the string
7612 with blanks, in which case we bypass the bidi
7613 iterator, because it cannot deal with such virtual
7614 characters. */
7615 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7616 {
7617 IT_STRING_BYTEPOS (*it) += it->len;
7618 IT_STRING_CHARPOS (*it) += 1;
7619 }
7620 else
7621 {
7622 int prev_scan_dir = it->bidi_it.scan_dir;
7623
7624 bidi_move_to_visually_next (&it->bidi_it);
7625 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7626 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7627 /* If the scan direction changes, we may need to update
7628 the place where to check for composed characters. */
7629 if (prev_scan_dir != it->bidi_it.scan_dir)
7630 {
7631 ptrdiff_t stop = SCHARS (it->string);
7632
7633 if (it->bidi_it.scan_dir < 0)
7634 stop = -1;
7635 else if (it->end_charpos < stop)
7636 stop = it->end_charpos;
7637
7638 composition_compute_stop_pos (&it->cmp_it,
7639 IT_STRING_CHARPOS (*it),
7640 IT_STRING_BYTEPOS (*it), stop,
7641 it->string);
7642 }
7643 }
7644 }
7645
7646 consider_string_end:
7647
7648 if (it->current.overlay_string_index >= 0)
7649 {
7650 /* IT->string is an overlay string. Advance to the
7651 next, if there is one. */
7652 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7653 {
7654 it->ellipsis_p = false;
7655 next_overlay_string (it);
7656 if (it->ellipsis_p)
7657 setup_for_ellipsis (it, 0);
7658 }
7659 }
7660 else
7661 {
7662 /* IT->string is not an overlay string. If we reached
7663 its end, and there is something on IT->stack, proceed
7664 with what is on the stack. This can be either another
7665 string, this time an overlay string, or a buffer. */
7666 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7667 && it->sp > 0)
7668 {
7669 pop_it (it);
7670 if (it->method == GET_FROM_STRING)
7671 goto consider_string_end;
7672 }
7673 }
7674 break;
7675
7676 case GET_FROM_IMAGE:
7677 case GET_FROM_STRETCH:
7678 case GET_FROM_XWIDGET:
7679
7680 /* The position etc with which we have to proceed are on
7681 the stack. The position may be at the end of a string,
7682 if the `display' property takes up the whole string. */
7683 eassert (it->sp > 0);
7684 pop_it (it);
7685 if (it->method == GET_FROM_STRING)
7686 goto consider_string_end;
7687 break;
7688
7689 default:
7690 /* There are no other methods defined, so this should be a bug. */
7691 emacs_abort ();
7692 }
7693
7694 eassert (it->method != GET_FROM_STRING
7695 || (STRINGP (it->string)
7696 && IT_STRING_CHARPOS (*it) >= 0));
7697 }
7698
7699 /* Load IT's display element fields with information about the next
7700 display element which comes from a display table entry or from the
7701 result of translating a control character to one of the forms `^C'
7702 or `\003'.
7703
7704 IT->dpvec holds the glyphs to return as characters.
7705 IT->saved_face_id holds the face id before the display vector--it
7706 is restored into IT->face_id in set_iterator_to_next. */
7707
7708 static bool
7709 next_element_from_display_vector (struct it *it)
7710 {
7711 Lisp_Object gc;
7712 int prev_face_id = it->face_id;
7713 int next_face_id;
7714
7715 /* Precondition. */
7716 eassert (it->dpvec && it->current.dpvec_index >= 0);
7717
7718 it->face_id = it->saved_face_id;
7719
7720 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7721 That seemed totally bogus - so I changed it... */
7722 gc = it->dpvec[it->current.dpvec_index];
7723
7724 if (GLYPH_CODE_P (gc))
7725 {
7726 struct face *this_face, *prev_face, *next_face;
7727
7728 it->c = GLYPH_CODE_CHAR (gc);
7729 it->len = CHAR_BYTES (it->c);
7730
7731 /* The entry may contain a face id to use. Such a face id is
7732 the id of a Lisp face, not a realized face. A face id of
7733 zero means no face is specified. */
7734 if (it->dpvec_face_id >= 0)
7735 it->face_id = it->dpvec_face_id;
7736 else
7737 {
7738 int lface_id = GLYPH_CODE_FACE (gc);
7739 if (lface_id > 0)
7740 it->face_id = merge_faces (it->f, Qt, lface_id,
7741 it->saved_face_id);
7742 }
7743
7744 /* Glyphs in the display vector could have the box face, so we
7745 need to set the related flags in the iterator, as
7746 appropriate. */
7747 this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
7748 prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
7749
7750 /* Is this character the first character of a box-face run? */
7751 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7752 && (!prev_face
7753 || prev_face->box == FACE_NO_BOX));
7754
7755 /* For the last character of the box-face run, we need to look
7756 either at the next glyph from the display vector, or at the
7757 face we saw before the display vector. */
7758 next_face_id = it->saved_face_id;
7759 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7760 {
7761 if (it->dpvec_face_id >= 0)
7762 next_face_id = it->dpvec_face_id;
7763 else
7764 {
7765 int lface_id =
7766 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7767
7768 if (lface_id > 0)
7769 next_face_id = merge_faces (it->f, Qt, lface_id,
7770 it->saved_face_id);
7771 }
7772 }
7773 next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
7774 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7775 && (!next_face
7776 || next_face->box == FACE_NO_BOX));
7777 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7778 }
7779 else
7780 /* Display table entry is invalid. Return a space. */
7781 it->c = ' ', it->len = 1;
7782
7783 /* Don't change position and object of the iterator here. They are
7784 still the values of the character that had this display table
7785 entry or was translated, and that's what we want. */
7786 it->what = IT_CHARACTER;
7787 return true;
7788 }
7789
7790 /* Get the first element of string/buffer in the visual order, after
7791 being reseated to a new position in a string or a buffer. */
7792 static void
7793 get_visually_first_element (struct it *it)
7794 {
7795 bool string_p = STRINGP (it->string) || it->s;
7796 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7797 ptrdiff_t bob = (string_p ? 0 : BEGV);
7798
7799 if (STRINGP (it->string))
7800 {
7801 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7802 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7803 }
7804 else
7805 {
7806 it->bidi_it.charpos = IT_CHARPOS (*it);
7807 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7808 }
7809
7810 if (it->bidi_it.charpos == eob)
7811 {
7812 /* Nothing to do, but reset the FIRST_ELT flag, like
7813 bidi_paragraph_init does, because we are not going to
7814 call it. */
7815 it->bidi_it.first_elt = false;
7816 }
7817 else if (it->bidi_it.charpos == bob
7818 || (!string_p
7819 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7820 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7821 {
7822 /* If we are at the beginning of a line/string, we can produce
7823 the next element right away. */
7824 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7825 bidi_move_to_visually_next (&it->bidi_it);
7826 }
7827 else
7828 {
7829 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7830
7831 /* We need to prime the bidi iterator starting at the line's or
7832 string's beginning, before we will be able to produce the
7833 next element. */
7834 if (string_p)
7835 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7836 else
7837 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7838 IT_BYTEPOS (*it), -1,
7839 &it->bidi_it.bytepos);
7840 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7841 do
7842 {
7843 /* Now return to buffer/string position where we were asked
7844 to get the next display element, and produce that. */
7845 bidi_move_to_visually_next (&it->bidi_it);
7846 }
7847 while (it->bidi_it.bytepos != orig_bytepos
7848 && it->bidi_it.charpos < eob);
7849 }
7850
7851 /* Adjust IT's position information to where we ended up. */
7852 if (STRINGP (it->string))
7853 {
7854 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7855 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7856 }
7857 else
7858 {
7859 IT_CHARPOS (*it) = it->bidi_it.charpos;
7860 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7861 }
7862
7863 if (STRINGP (it->string) || !it->s)
7864 {
7865 ptrdiff_t stop, charpos, bytepos;
7866
7867 if (STRINGP (it->string))
7868 {
7869 eassert (!it->s);
7870 stop = SCHARS (it->string);
7871 if (stop > it->end_charpos)
7872 stop = it->end_charpos;
7873 charpos = IT_STRING_CHARPOS (*it);
7874 bytepos = IT_STRING_BYTEPOS (*it);
7875 }
7876 else
7877 {
7878 stop = it->end_charpos;
7879 charpos = IT_CHARPOS (*it);
7880 bytepos = IT_BYTEPOS (*it);
7881 }
7882 if (it->bidi_it.scan_dir < 0)
7883 stop = -1;
7884 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7885 it->string);
7886 }
7887 }
7888
7889 /* Load IT with the next display element from Lisp string IT->string.
7890 IT->current.string_pos is the current position within the string.
7891 If IT->current.overlay_string_index >= 0, the Lisp string is an
7892 overlay string. */
7893
7894 static bool
7895 next_element_from_string (struct it *it)
7896 {
7897 struct text_pos position;
7898
7899 eassert (STRINGP (it->string));
7900 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7901 eassert (IT_STRING_CHARPOS (*it) >= 0);
7902 position = it->current.string_pos;
7903
7904 /* With bidi reordering, the character to display might not be the
7905 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7906 that we were reseat()ed to a new string, whose paragraph
7907 direction is not known. */
7908 if (it->bidi_p && it->bidi_it.first_elt)
7909 {
7910 get_visually_first_element (it);
7911 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7912 }
7913
7914 /* Time to check for invisible text? */
7915 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7916 {
7917 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7918 {
7919 if (!(!it->bidi_p
7920 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7921 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7922 {
7923 /* With bidi non-linear iteration, we could find
7924 ourselves far beyond the last computed stop_charpos,
7925 with several other stop positions in between that we
7926 missed. Scan them all now, in buffer's logical
7927 order, until we find and handle the last stop_charpos
7928 that precedes our current position. */
7929 handle_stop_backwards (it, it->stop_charpos);
7930 return GET_NEXT_DISPLAY_ELEMENT (it);
7931 }
7932 else
7933 {
7934 if (it->bidi_p)
7935 {
7936 /* Take note of the stop position we just moved
7937 across, for when we will move back across it. */
7938 it->prev_stop = it->stop_charpos;
7939 /* If we are at base paragraph embedding level, take
7940 note of the last stop position seen at this
7941 level. */
7942 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7943 it->base_level_stop = it->stop_charpos;
7944 }
7945 handle_stop (it);
7946
7947 /* Since a handler may have changed IT->method, we must
7948 recurse here. */
7949 return GET_NEXT_DISPLAY_ELEMENT (it);
7950 }
7951 }
7952 else if (it->bidi_p
7953 /* If we are before prev_stop, we may have overstepped
7954 on our way backwards a stop_pos, and if so, we need
7955 to handle that stop_pos. */
7956 && IT_STRING_CHARPOS (*it) < it->prev_stop
7957 /* We can sometimes back up for reasons that have nothing
7958 to do with bidi reordering. E.g., compositions. The
7959 code below is only needed when we are above the base
7960 embedding level, so test for that explicitly. */
7961 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7962 {
7963 /* If we lost track of base_level_stop, we have no better
7964 place for handle_stop_backwards to start from than string
7965 beginning. This happens, e.g., when we were reseated to
7966 the previous screenful of text by vertical-motion. */
7967 if (it->base_level_stop <= 0
7968 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7969 it->base_level_stop = 0;
7970 handle_stop_backwards (it, it->base_level_stop);
7971 return GET_NEXT_DISPLAY_ELEMENT (it);
7972 }
7973 }
7974
7975 if (it->current.overlay_string_index >= 0)
7976 {
7977 /* Get the next character from an overlay string. In overlay
7978 strings, there is no field width or padding with spaces to
7979 do. */
7980 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7981 {
7982 it->what = IT_EOB;
7983 return false;
7984 }
7985 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7986 IT_STRING_BYTEPOS (*it),
7987 it->bidi_it.scan_dir < 0
7988 ? -1
7989 : SCHARS (it->string))
7990 && next_element_from_composition (it))
7991 {
7992 return true;
7993 }
7994 else if (STRING_MULTIBYTE (it->string))
7995 {
7996 const unsigned char *s = (SDATA (it->string)
7997 + IT_STRING_BYTEPOS (*it));
7998 it->c = string_char_and_length (s, &it->len);
7999 }
8000 else
8001 {
8002 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8003 it->len = 1;
8004 }
8005 }
8006 else
8007 {
8008 /* Get the next character from a Lisp string that is not an
8009 overlay string. Such strings come from the mode line, for
8010 example. We may have to pad with spaces, or truncate the
8011 string. See also next_element_from_c_string. */
8012 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
8013 {
8014 it->what = IT_EOB;
8015 return false;
8016 }
8017 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
8018 {
8019 /* Pad with spaces. */
8020 it->c = ' ', it->len = 1;
8021 CHARPOS (position) = BYTEPOS (position) = -1;
8022 }
8023 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8024 IT_STRING_BYTEPOS (*it),
8025 it->bidi_it.scan_dir < 0
8026 ? -1
8027 : it->string_nchars)
8028 && next_element_from_composition (it))
8029 {
8030 return true;
8031 }
8032 else if (STRING_MULTIBYTE (it->string))
8033 {
8034 const unsigned char *s = (SDATA (it->string)
8035 + IT_STRING_BYTEPOS (*it));
8036 it->c = string_char_and_length (s, &it->len);
8037 }
8038 else
8039 {
8040 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8041 it->len = 1;
8042 }
8043 }
8044
8045 /* Record what we have and where it came from. */
8046 it->what = IT_CHARACTER;
8047 it->object = it->string;
8048 it->position = position;
8049 return true;
8050 }
8051
8052
8053 /* Load IT with next display element from C string IT->s.
8054 IT->string_nchars is the maximum number of characters to return
8055 from the string. IT->end_charpos may be greater than
8056 IT->string_nchars when this function is called, in which case we
8057 may have to return padding spaces. Value is false if end of string
8058 reached, including padding spaces. */
8059
8060 static bool
8061 next_element_from_c_string (struct it *it)
8062 {
8063 bool success_p = true;
8064
8065 eassert (it->s);
8066 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8067 it->what = IT_CHARACTER;
8068 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8069 it->object = make_number (0);
8070
8071 /* With bidi reordering, the character to display might not be the
8072 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8073 we were reseated to a new string, whose paragraph direction is
8074 not known. */
8075 if (it->bidi_p && it->bidi_it.first_elt)
8076 get_visually_first_element (it);
8077
8078 /* IT's position can be greater than IT->string_nchars in case a
8079 field width or precision has been specified when the iterator was
8080 initialized. */
8081 if (IT_CHARPOS (*it) >= it->end_charpos)
8082 {
8083 /* End of the game. */
8084 it->what = IT_EOB;
8085 success_p = false;
8086 }
8087 else if (IT_CHARPOS (*it) >= it->string_nchars)
8088 {
8089 /* Pad with spaces. */
8090 it->c = ' ', it->len = 1;
8091 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8092 }
8093 else if (it->multibyte_p)
8094 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8095 else
8096 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8097
8098 return success_p;
8099 }
8100
8101
8102 /* Set up IT to return characters from an ellipsis, if appropriate.
8103 The definition of the ellipsis glyphs may come from a display table
8104 entry. This function fills IT with the first glyph from the
8105 ellipsis if an ellipsis is to be displayed. */
8106
8107 static bool
8108 next_element_from_ellipsis (struct it *it)
8109 {
8110 if (it->selective_display_ellipsis_p)
8111 setup_for_ellipsis (it, it->len);
8112 else
8113 {
8114 /* The face at the current position may be different from the
8115 face we find after the invisible text. Remember what it
8116 was in IT->saved_face_id, and signal that it's there by
8117 setting face_before_selective_p. */
8118 it->saved_face_id = it->face_id;
8119 it->method = GET_FROM_BUFFER;
8120 it->object = it->w->contents;
8121 reseat_at_next_visible_line_start (it, true);
8122 it->face_before_selective_p = true;
8123 }
8124
8125 return GET_NEXT_DISPLAY_ELEMENT (it);
8126 }
8127
8128
8129 /* Deliver an image display element. The iterator IT is already
8130 filled with image information (done in handle_display_prop). Value
8131 is always true. */
8132
8133
8134 static bool
8135 next_element_from_image (struct it *it)
8136 {
8137 it->what = IT_IMAGE;
8138 return true;
8139 }
8140
8141 static bool
8142 next_element_from_xwidget (struct it *it)
8143 {
8144 it->what = IT_XWIDGET;
8145 return true;
8146 }
8147
8148
8149 /* Fill iterator IT with next display element from a stretch glyph
8150 property. IT->object is the value of the text property. Value is
8151 always true. */
8152
8153 static bool
8154 next_element_from_stretch (struct it *it)
8155 {
8156 it->what = IT_STRETCH;
8157 return true;
8158 }
8159
8160 /* Scan backwards from IT's current position until we find a stop
8161 position, or until BEGV. This is called when we find ourself
8162 before both the last known prev_stop and base_level_stop while
8163 reordering bidirectional text. */
8164
8165 static void
8166 compute_stop_pos_backwards (struct it *it)
8167 {
8168 const int SCAN_BACK_LIMIT = 1000;
8169 struct text_pos pos;
8170 struct display_pos save_current = it->current;
8171 struct text_pos save_position = it->position;
8172 ptrdiff_t charpos = IT_CHARPOS (*it);
8173 ptrdiff_t where_we_are = charpos;
8174 ptrdiff_t save_stop_pos = it->stop_charpos;
8175 ptrdiff_t save_end_pos = it->end_charpos;
8176
8177 eassert (NILP (it->string) && !it->s);
8178 eassert (it->bidi_p);
8179 it->bidi_p = false;
8180 do
8181 {
8182 it->end_charpos = min (charpos + 1, ZV);
8183 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8184 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8185 reseat_1 (it, pos, false);
8186 compute_stop_pos (it);
8187 /* We must advance forward, right? */
8188 if (it->stop_charpos <= charpos)
8189 emacs_abort ();
8190 }
8191 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8192
8193 if (it->stop_charpos <= where_we_are)
8194 it->prev_stop = it->stop_charpos;
8195 else
8196 it->prev_stop = BEGV;
8197 it->bidi_p = true;
8198 it->current = save_current;
8199 it->position = save_position;
8200 it->stop_charpos = save_stop_pos;
8201 it->end_charpos = save_end_pos;
8202 }
8203
8204 /* Scan forward from CHARPOS in the current buffer/string, until we
8205 find a stop position > current IT's position. Then handle the stop
8206 position before that. This is called when we bump into a stop
8207 position while reordering bidirectional text. CHARPOS should be
8208 the last previously processed stop_pos (or BEGV/0, if none were
8209 processed yet) whose position is less that IT's current
8210 position. */
8211
8212 static void
8213 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8214 {
8215 bool bufp = !STRINGP (it->string);
8216 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8217 struct display_pos save_current = it->current;
8218 struct text_pos save_position = it->position;
8219 struct text_pos pos1;
8220 ptrdiff_t next_stop;
8221
8222 /* Scan in strict logical order. */
8223 eassert (it->bidi_p);
8224 it->bidi_p = false;
8225 do
8226 {
8227 it->prev_stop = charpos;
8228 if (bufp)
8229 {
8230 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8231 reseat_1 (it, pos1, false);
8232 }
8233 else
8234 it->current.string_pos = string_pos (charpos, it->string);
8235 compute_stop_pos (it);
8236 /* We must advance forward, right? */
8237 if (it->stop_charpos <= it->prev_stop)
8238 emacs_abort ();
8239 charpos = it->stop_charpos;
8240 }
8241 while (charpos <= where_we_are);
8242
8243 it->bidi_p = true;
8244 it->current = save_current;
8245 it->position = save_position;
8246 next_stop = it->stop_charpos;
8247 it->stop_charpos = it->prev_stop;
8248 handle_stop (it);
8249 it->stop_charpos = next_stop;
8250 }
8251
8252 /* Load IT with the next display element from current_buffer. Value
8253 is false if end of buffer reached. IT->stop_charpos is the next
8254 position at which to stop and check for text properties or buffer
8255 end. */
8256
8257 static bool
8258 next_element_from_buffer (struct it *it)
8259 {
8260 bool success_p = true;
8261
8262 eassert (IT_CHARPOS (*it) >= BEGV);
8263 eassert (NILP (it->string) && !it->s);
8264 eassert (!it->bidi_p
8265 || (EQ (it->bidi_it.string.lstring, Qnil)
8266 && it->bidi_it.string.s == NULL));
8267
8268 /* With bidi reordering, the character to display might not be the
8269 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8270 we were reseat()ed to a new buffer position, which is potentially
8271 a different paragraph. */
8272 if (it->bidi_p && it->bidi_it.first_elt)
8273 {
8274 get_visually_first_element (it);
8275 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8276 }
8277
8278 if (IT_CHARPOS (*it) >= it->stop_charpos)
8279 {
8280 if (IT_CHARPOS (*it) >= it->end_charpos)
8281 {
8282 bool overlay_strings_follow_p;
8283
8284 /* End of the game, except when overlay strings follow that
8285 haven't been returned yet. */
8286 if (it->overlay_strings_at_end_processed_p)
8287 overlay_strings_follow_p = false;
8288 else
8289 {
8290 it->overlay_strings_at_end_processed_p = true;
8291 overlay_strings_follow_p = get_overlay_strings (it, 0);
8292 }
8293
8294 if (overlay_strings_follow_p)
8295 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8296 else
8297 {
8298 it->what = IT_EOB;
8299 it->position = it->current.pos;
8300 success_p = false;
8301 }
8302 }
8303 else if (!(!it->bidi_p
8304 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8305 || IT_CHARPOS (*it) == it->stop_charpos))
8306 {
8307 /* With bidi non-linear iteration, we could find ourselves
8308 far beyond the last computed stop_charpos, with several
8309 other stop positions in between that we missed. Scan
8310 them all now, in buffer's logical order, until we find
8311 and handle the last stop_charpos that precedes our
8312 current position. */
8313 handle_stop_backwards (it, it->stop_charpos);
8314 it->ignore_overlay_strings_at_pos_p = false;
8315 return GET_NEXT_DISPLAY_ELEMENT (it);
8316 }
8317 else
8318 {
8319 if (it->bidi_p)
8320 {
8321 /* Take note of the stop position we just moved across,
8322 for when we will move back across it. */
8323 it->prev_stop = it->stop_charpos;
8324 /* If we are at base paragraph embedding level, take
8325 note of the last stop position seen at this
8326 level. */
8327 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8328 it->base_level_stop = it->stop_charpos;
8329 }
8330 handle_stop (it);
8331 it->ignore_overlay_strings_at_pos_p = false;
8332 return GET_NEXT_DISPLAY_ELEMENT (it);
8333 }
8334 }
8335 else if (it->bidi_p
8336 /* If we are before prev_stop, we may have overstepped on
8337 our way backwards a stop_pos, and if so, we need to
8338 handle that stop_pos. */
8339 && IT_CHARPOS (*it) < it->prev_stop
8340 /* We can sometimes back up for reasons that have nothing
8341 to do with bidi reordering. E.g., compositions. The
8342 code below is only needed when we are above the base
8343 embedding level, so test for that explicitly. */
8344 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8345 {
8346 if (it->base_level_stop <= 0
8347 || IT_CHARPOS (*it) < it->base_level_stop)
8348 {
8349 /* If we lost track of base_level_stop, we need to find
8350 prev_stop by looking backwards. This happens, e.g., when
8351 we were reseated to the previous screenful of text by
8352 vertical-motion. */
8353 it->base_level_stop = BEGV;
8354 compute_stop_pos_backwards (it);
8355 handle_stop_backwards (it, it->prev_stop);
8356 }
8357 else
8358 handle_stop_backwards (it, it->base_level_stop);
8359 it->ignore_overlay_strings_at_pos_p = false;
8360 return GET_NEXT_DISPLAY_ELEMENT (it);
8361 }
8362 else
8363 {
8364 /* No face changes, overlays etc. in sight, so just return a
8365 character from current_buffer. */
8366 unsigned char *p;
8367 ptrdiff_t stop;
8368
8369 /* We moved to the next buffer position, so any info about
8370 previously seen overlays is no longer valid. */
8371 it->ignore_overlay_strings_at_pos_p = false;
8372
8373 /* Maybe run the redisplay end trigger hook. Performance note:
8374 This doesn't seem to cost measurable time. */
8375 if (it->redisplay_end_trigger_charpos
8376 && it->glyph_row
8377 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8378 run_redisplay_end_trigger_hook (it);
8379
8380 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8381 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8382 stop)
8383 && next_element_from_composition (it))
8384 {
8385 return true;
8386 }
8387
8388 /* Get the next character, maybe multibyte. */
8389 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8390 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8391 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8392 else
8393 it->c = *p, it->len = 1;
8394
8395 /* Record what we have and where it came from. */
8396 it->what = IT_CHARACTER;
8397 it->object = it->w->contents;
8398 it->position = it->current.pos;
8399
8400 /* Normally we return the character found above, except when we
8401 really want to return an ellipsis for selective display. */
8402 if (it->selective)
8403 {
8404 if (it->c == '\n')
8405 {
8406 /* A value of selective > 0 means hide lines indented more
8407 than that number of columns. */
8408 if (it->selective > 0
8409 && IT_CHARPOS (*it) + 1 < ZV
8410 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8411 IT_BYTEPOS (*it) + 1,
8412 it->selective))
8413 {
8414 success_p = next_element_from_ellipsis (it);
8415 it->dpvec_char_len = -1;
8416 }
8417 }
8418 else if (it->c == '\r' && it->selective == -1)
8419 {
8420 /* A value of selective == -1 means that everything from the
8421 CR to the end of the line is invisible, with maybe an
8422 ellipsis displayed for it. */
8423 success_p = next_element_from_ellipsis (it);
8424 it->dpvec_char_len = -1;
8425 }
8426 }
8427 }
8428
8429 /* Value is false if end of buffer reached. */
8430 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8431 return success_p;
8432 }
8433
8434
8435 /* Run the redisplay end trigger hook for IT. */
8436
8437 static void
8438 run_redisplay_end_trigger_hook (struct it *it)
8439 {
8440 /* IT->glyph_row should be non-null, i.e. we should be actually
8441 displaying something, or otherwise we should not run the hook. */
8442 eassert (it->glyph_row);
8443
8444 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8445 it->redisplay_end_trigger_charpos = 0;
8446
8447 /* Since we are *trying* to run these functions, don't try to run
8448 them again, even if they get an error. */
8449 wset_redisplay_end_trigger (it->w, Qnil);
8450 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8451 make_number (charpos));
8452
8453 /* Notice if it changed the face of the character we are on. */
8454 handle_face_prop (it);
8455 }
8456
8457
8458 /* Deliver a composition display element. Unlike the other
8459 next_element_from_XXX, this function is not registered in the array
8460 get_next_element[]. It is called from next_element_from_buffer and
8461 next_element_from_string when necessary. */
8462
8463 static bool
8464 next_element_from_composition (struct it *it)
8465 {
8466 it->what = IT_COMPOSITION;
8467 it->len = it->cmp_it.nbytes;
8468 if (STRINGP (it->string))
8469 {
8470 if (it->c < 0)
8471 {
8472 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8473 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8474 return false;
8475 }
8476 it->position = it->current.string_pos;
8477 it->object = it->string;
8478 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8479 IT_STRING_BYTEPOS (*it), it->string);
8480 }
8481 else
8482 {
8483 if (it->c < 0)
8484 {
8485 IT_CHARPOS (*it) += it->cmp_it.nchars;
8486 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8487 if (it->bidi_p)
8488 {
8489 if (it->bidi_it.new_paragraph)
8490 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8491 false);
8492 /* Resync the bidi iterator with IT's new position.
8493 FIXME: this doesn't support bidirectional text. */
8494 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8495 bidi_move_to_visually_next (&it->bidi_it);
8496 }
8497 return false;
8498 }
8499 it->position = it->current.pos;
8500 it->object = it->w->contents;
8501 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8502 IT_BYTEPOS (*it), Qnil);
8503 }
8504 return true;
8505 }
8506
8507
8508 \f
8509 /***********************************************************************
8510 Moving an iterator without producing glyphs
8511 ***********************************************************************/
8512
8513 /* Check if iterator is at a position corresponding to a valid buffer
8514 position after some move_it_ call. */
8515
8516 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8517 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8518
8519
8520 /* Move iterator IT to a specified buffer or X position within one
8521 line on the display without producing glyphs.
8522
8523 OP should be a bit mask including some or all of these bits:
8524 MOVE_TO_X: Stop upon reaching x-position TO_X.
8525 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8526 Regardless of OP's value, stop upon reaching the end of the display line.
8527
8528 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8529 This means, in particular, that TO_X includes window's horizontal
8530 scroll amount.
8531
8532 The return value has several possible values that
8533 say what condition caused the scan to stop:
8534
8535 MOVE_POS_MATCH_OR_ZV
8536 - when TO_POS or ZV was reached.
8537
8538 MOVE_X_REACHED
8539 -when TO_X was reached before TO_POS or ZV were reached.
8540
8541 MOVE_LINE_CONTINUED
8542 - when we reached the end of the display area and the line must
8543 be continued.
8544
8545 MOVE_LINE_TRUNCATED
8546 - when we reached the end of the display area and the line is
8547 truncated.
8548
8549 MOVE_NEWLINE_OR_CR
8550 - when we stopped at a line end, i.e. a newline or a CR and selective
8551 display is on. */
8552
8553 static enum move_it_result
8554 move_it_in_display_line_to (struct it *it,
8555 ptrdiff_t to_charpos, int to_x,
8556 enum move_operation_enum op)
8557 {
8558 enum move_it_result result = MOVE_UNDEFINED;
8559 struct glyph_row *saved_glyph_row;
8560 struct it wrap_it, atpos_it, atx_it, ppos_it;
8561 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8562 void *ppos_data = NULL;
8563 bool may_wrap = false;
8564 enum it_method prev_method = it->method;
8565 ptrdiff_t closest_pos UNINIT;
8566 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8567 bool saw_smaller_pos = prev_pos < to_charpos;
8568
8569 /* Don't produce glyphs in produce_glyphs. */
8570 saved_glyph_row = it->glyph_row;
8571 it->glyph_row = NULL;
8572
8573 /* Use wrap_it to save a copy of IT wherever a word wrap could
8574 occur. Use atpos_it to save a copy of IT at the desired buffer
8575 position, if found, so that we can scan ahead and check if the
8576 word later overshoots the window edge. Use atx_it similarly, for
8577 pixel positions. */
8578 wrap_it.sp = -1;
8579 atpos_it.sp = -1;
8580 atx_it.sp = -1;
8581
8582 /* Use ppos_it under bidi reordering to save a copy of IT for the
8583 initial position. We restore that position in IT when we have
8584 scanned the entire display line without finding a match for
8585 TO_CHARPOS and all the character positions are greater than
8586 TO_CHARPOS. We then restart the scan from the initial position,
8587 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8588 the closest to TO_CHARPOS. */
8589 if (it->bidi_p)
8590 {
8591 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8592 {
8593 SAVE_IT (ppos_it, *it, ppos_data);
8594 closest_pos = IT_CHARPOS (*it);
8595 }
8596 else
8597 closest_pos = ZV;
8598 }
8599
8600 #define BUFFER_POS_REACHED_P() \
8601 ((op & MOVE_TO_POS) != 0 \
8602 && BUFFERP (it->object) \
8603 && (IT_CHARPOS (*it) == to_charpos \
8604 || ((!it->bidi_p \
8605 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8606 && IT_CHARPOS (*it) > to_charpos) \
8607 || (it->what == IT_COMPOSITION \
8608 && ((IT_CHARPOS (*it) > to_charpos \
8609 && to_charpos >= it->cmp_it.charpos) \
8610 || (IT_CHARPOS (*it) < to_charpos \
8611 && to_charpos <= it->cmp_it.charpos)))) \
8612 && (it->method == GET_FROM_BUFFER \
8613 || (it->method == GET_FROM_DISPLAY_VECTOR \
8614 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8615
8616 /* If there's a line-/wrap-prefix, handle it. */
8617 if (it->hpos == 0 && it->method == GET_FROM_BUFFER)
8618 handle_line_prefix (it);
8619
8620 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8621 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8622
8623 while (true)
8624 {
8625 int x, i, ascent = 0, descent = 0;
8626
8627 /* Utility macro to reset an iterator with x, ascent, and descent. */
8628 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8629 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8630 (IT)->max_descent = descent)
8631
8632 /* Stop if we move beyond TO_CHARPOS (after an image or a
8633 display string or stretch glyph). */
8634 if ((op & MOVE_TO_POS) != 0
8635 && BUFFERP (it->object)
8636 && it->method == GET_FROM_BUFFER
8637 && (((!it->bidi_p
8638 /* When the iterator is at base embedding level, we
8639 are guaranteed that characters are delivered for
8640 display in strictly increasing order of their
8641 buffer positions. */
8642 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8643 && IT_CHARPOS (*it) > to_charpos)
8644 || (it->bidi_p
8645 && (prev_method == GET_FROM_IMAGE
8646 || prev_method == GET_FROM_STRETCH
8647 || prev_method == GET_FROM_STRING)
8648 /* Passed TO_CHARPOS from left to right. */
8649 && ((prev_pos < to_charpos
8650 && IT_CHARPOS (*it) > to_charpos)
8651 /* Passed TO_CHARPOS from right to left. */
8652 || (prev_pos > to_charpos
8653 && IT_CHARPOS (*it) < to_charpos)))))
8654 {
8655 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8656 {
8657 result = MOVE_POS_MATCH_OR_ZV;
8658 break;
8659 }
8660 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8661 /* If wrap_it is valid, the current position might be in a
8662 word that is wrapped. So, save the iterator in
8663 atpos_it and continue to see if wrapping happens. */
8664 SAVE_IT (atpos_it, *it, atpos_data);
8665 }
8666
8667 /* Stop when ZV reached.
8668 We used to stop here when TO_CHARPOS reached as well, but that is
8669 too soon if this glyph does not fit on this line. So we handle it
8670 explicitly below. */
8671 if (!get_next_display_element (it))
8672 {
8673 result = MOVE_POS_MATCH_OR_ZV;
8674 break;
8675 }
8676
8677 if (it->line_wrap == TRUNCATE)
8678 {
8679 if (BUFFER_POS_REACHED_P ())
8680 {
8681 result = MOVE_POS_MATCH_OR_ZV;
8682 break;
8683 }
8684 }
8685 else
8686 {
8687 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8688 {
8689 if (IT_DISPLAYING_WHITESPACE (it))
8690 may_wrap = true;
8691 else if (may_wrap)
8692 {
8693 /* We have reached a glyph that follows one or more
8694 whitespace characters. If the position is
8695 already found, we are done. */
8696 if (atpos_it.sp >= 0)
8697 {
8698 RESTORE_IT (it, &atpos_it, atpos_data);
8699 result = MOVE_POS_MATCH_OR_ZV;
8700 goto done;
8701 }
8702 if (atx_it.sp >= 0)
8703 {
8704 RESTORE_IT (it, &atx_it, atx_data);
8705 result = MOVE_X_REACHED;
8706 goto done;
8707 }
8708 /* Otherwise, we can wrap here. */
8709 SAVE_IT (wrap_it, *it, wrap_data);
8710 may_wrap = false;
8711 }
8712 }
8713 }
8714
8715 /* Remember the line height for the current line, in case
8716 the next element doesn't fit on the line. */
8717 ascent = it->max_ascent;
8718 descent = it->max_descent;
8719
8720 /* The call to produce_glyphs will get the metrics of the
8721 display element IT is loaded with. Record the x-position
8722 before this display element, in case it doesn't fit on the
8723 line. */
8724 x = it->current_x;
8725
8726 PRODUCE_GLYPHS (it);
8727
8728 if (it->area != TEXT_AREA)
8729 {
8730 prev_method = it->method;
8731 if (it->method == GET_FROM_BUFFER)
8732 prev_pos = IT_CHARPOS (*it);
8733 set_iterator_to_next (it, true);
8734 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8735 SET_TEXT_POS (this_line_min_pos,
8736 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8737 if (it->bidi_p
8738 && (op & MOVE_TO_POS)
8739 && IT_CHARPOS (*it) > to_charpos
8740 && IT_CHARPOS (*it) < closest_pos)
8741 closest_pos = IT_CHARPOS (*it);
8742 continue;
8743 }
8744
8745 /* The number of glyphs we get back in IT->nglyphs will normally
8746 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8747 character on a terminal frame, or (iii) a line end. For the
8748 second case, IT->nglyphs - 1 padding glyphs will be present.
8749 (On X frames, there is only one glyph produced for a
8750 composite character.)
8751
8752 The behavior implemented below means, for continuation lines,
8753 that as many spaces of a TAB as fit on the current line are
8754 displayed there. For terminal frames, as many glyphs of a
8755 multi-glyph character are displayed in the current line, too.
8756 This is what the old redisplay code did, and we keep it that
8757 way. Under X, the whole shape of a complex character must
8758 fit on the line or it will be completely displayed in the
8759 next line.
8760
8761 Note that both for tabs and padding glyphs, all glyphs have
8762 the same width. */
8763 if (it->nglyphs)
8764 {
8765 /* More than one glyph or glyph doesn't fit on line. All
8766 glyphs have the same width. */
8767 int single_glyph_width = it->pixel_width / it->nglyphs;
8768 int new_x;
8769 int x_before_this_char = x;
8770 int hpos_before_this_char = it->hpos;
8771
8772 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8773 {
8774 new_x = x + single_glyph_width;
8775
8776 /* We want to leave anything reaching TO_X to the caller. */
8777 if ((op & MOVE_TO_X) && new_x > to_x)
8778 {
8779 if (BUFFER_POS_REACHED_P ())
8780 {
8781 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8782 goto buffer_pos_reached;
8783 if (atpos_it.sp < 0)
8784 {
8785 SAVE_IT (atpos_it, *it, atpos_data);
8786 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8787 }
8788 }
8789 else
8790 {
8791 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8792 {
8793 it->current_x = x;
8794 result = MOVE_X_REACHED;
8795 break;
8796 }
8797 if (atx_it.sp < 0)
8798 {
8799 SAVE_IT (atx_it, *it, atx_data);
8800 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8801 }
8802 }
8803 }
8804
8805 if (/* Lines are continued. */
8806 it->line_wrap != TRUNCATE
8807 && (/* And glyph doesn't fit on the line. */
8808 new_x > it->last_visible_x
8809 /* Or it fits exactly and we're on a window
8810 system frame. */
8811 || (new_x == it->last_visible_x
8812 && FRAME_WINDOW_P (it->f)
8813 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8814 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8815 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8816 {
8817 bool moved_forward = false;
8818
8819 if (/* IT->hpos == 0 means the very first glyph
8820 doesn't fit on the line, e.g. a wide image. */
8821 it->hpos == 0
8822 || (new_x == it->last_visible_x
8823 && FRAME_WINDOW_P (it->f)))
8824 {
8825 ++it->hpos;
8826 it->current_x = new_x;
8827
8828 /* The character's last glyph just barely fits
8829 in this row. */
8830 if (i == it->nglyphs - 1)
8831 {
8832 /* If this is the destination position,
8833 return a position *before* it in this row,
8834 now that we know it fits in this row. */
8835 if (BUFFER_POS_REACHED_P ())
8836 {
8837 bool can_wrap = true;
8838
8839 /* If we are at a whitespace character
8840 that barely fits on this screen line,
8841 but the next character is also
8842 whitespace, we cannot wrap here. */
8843 if (it->line_wrap == WORD_WRAP
8844 && wrap_it.sp >= 0
8845 && may_wrap
8846 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8847 {
8848 struct it tem_it;
8849 void *tem_data = NULL;
8850
8851 SAVE_IT (tem_it, *it, tem_data);
8852 set_iterator_to_next (it, true);
8853 if (get_next_display_element (it)
8854 && IT_DISPLAYING_WHITESPACE (it))
8855 can_wrap = false;
8856 RESTORE_IT (it, &tem_it, tem_data);
8857 }
8858 if (it->line_wrap != WORD_WRAP
8859 || wrap_it.sp < 0
8860 /* If we've just found whitespace
8861 where we can wrap, effectively
8862 ignore the previous wrap point --
8863 it is no longer relevant, but we
8864 won't have an opportunity to
8865 update it, since we've reached
8866 the edge of this screen line. */
8867 || (may_wrap && can_wrap
8868 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8869 {
8870 it->hpos = hpos_before_this_char;
8871 it->current_x = x_before_this_char;
8872 result = MOVE_POS_MATCH_OR_ZV;
8873 break;
8874 }
8875 if (it->line_wrap == WORD_WRAP
8876 && atpos_it.sp < 0)
8877 {
8878 SAVE_IT (atpos_it, *it, atpos_data);
8879 atpos_it.current_x = x_before_this_char;
8880 atpos_it.hpos = hpos_before_this_char;
8881 }
8882 }
8883
8884 prev_method = it->method;
8885 if (it->method == GET_FROM_BUFFER)
8886 prev_pos = IT_CHARPOS (*it);
8887 set_iterator_to_next (it, true);
8888 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8889 SET_TEXT_POS (this_line_min_pos,
8890 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8891 /* On graphical terminals, newlines may
8892 "overflow" into the fringe if
8893 overflow-newline-into-fringe is non-nil.
8894 On text terminals, and on graphical
8895 terminals with no right margin, newlines
8896 may overflow into the last glyph on the
8897 display line.*/
8898 if (!FRAME_WINDOW_P (it->f)
8899 || ((it->bidi_p
8900 && it->bidi_it.paragraph_dir == R2L)
8901 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8902 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8903 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8904 {
8905 if (!get_next_display_element (it))
8906 {
8907 result = MOVE_POS_MATCH_OR_ZV;
8908 break;
8909 }
8910 moved_forward = true;
8911 if (BUFFER_POS_REACHED_P ())
8912 {
8913 if (ITERATOR_AT_END_OF_LINE_P (it))
8914 result = MOVE_POS_MATCH_OR_ZV;
8915 else
8916 result = MOVE_LINE_CONTINUED;
8917 break;
8918 }
8919 if (ITERATOR_AT_END_OF_LINE_P (it)
8920 && (it->line_wrap != WORD_WRAP
8921 || wrap_it.sp < 0
8922 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8923 {
8924 result = MOVE_NEWLINE_OR_CR;
8925 break;
8926 }
8927 }
8928 }
8929 }
8930 else
8931 IT_RESET_X_ASCENT_DESCENT (it);
8932
8933 /* If the screen line ends with whitespace, and we
8934 are under word-wrap, don't use wrap_it: it is no
8935 longer relevant, but we won't have an opportunity
8936 to update it, since we are done with this screen
8937 line. */
8938 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
8939 /* If the character after the one which set the
8940 may_wrap flag is also whitespace, we can't
8941 wrap here, since the screen line cannot be
8942 wrapped in the middle of whitespace.
8943 Therefore, wrap_it _is_ relevant in that
8944 case. */
8945 && !(moved_forward && IT_DISPLAYING_WHITESPACE (it)))
8946 {
8947 /* If we've found TO_X, go back there, as we now
8948 know the last word fits on this screen line. */
8949 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8950 && atx_it.sp >= 0)
8951 {
8952 RESTORE_IT (it, &atx_it, atx_data);
8953 atpos_it.sp = -1;
8954 atx_it.sp = -1;
8955 result = MOVE_X_REACHED;
8956 break;
8957 }
8958 }
8959 else if (wrap_it.sp >= 0)
8960 {
8961 RESTORE_IT (it, &wrap_it, wrap_data);
8962 atpos_it.sp = -1;
8963 atx_it.sp = -1;
8964 }
8965
8966 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8967 IT_CHARPOS (*it)));
8968 result = MOVE_LINE_CONTINUED;
8969 break;
8970 }
8971
8972 if (BUFFER_POS_REACHED_P ())
8973 {
8974 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8975 goto buffer_pos_reached;
8976 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8977 {
8978 SAVE_IT (atpos_it, *it, atpos_data);
8979 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8980 }
8981 }
8982
8983 if (new_x > it->first_visible_x)
8984 {
8985 /* Glyph is visible. Increment number of glyphs that
8986 would be displayed. */
8987 ++it->hpos;
8988 }
8989 }
8990
8991 if (result != MOVE_UNDEFINED)
8992 break;
8993 }
8994 else if (BUFFER_POS_REACHED_P ())
8995 {
8996 buffer_pos_reached:
8997 IT_RESET_X_ASCENT_DESCENT (it);
8998 result = MOVE_POS_MATCH_OR_ZV;
8999 break;
9000 }
9001 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
9002 {
9003 /* Stop when TO_X specified and reached. This check is
9004 necessary here because of lines consisting of a line end,
9005 only. The line end will not produce any glyphs and we
9006 would never get MOVE_X_REACHED. */
9007 eassert (it->nglyphs == 0);
9008 result = MOVE_X_REACHED;
9009 break;
9010 }
9011
9012 /* Is this a line end? If yes, we're done. */
9013 if (ITERATOR_AT_END_OF_LINE_P (it))
9014 {
9015 /* If we are past TO_CHARPOS, but never saw any character
9016 positions smaller than TO_CHARPOS, return
9017 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
9018 did. */
9019 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
9020 {
9021 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
9022 {
9023 if (closest_pos < ZV)
9024 {
9025 RESTORE_IT (it, &ppos_it, ppos_data);
9026 /* Don't recurse if closest_pos is equal to
9027 to_charpos, since we have just tried that. */
9028 if (closest_pos != to_charpos)
9029 move_it_in_display_line_to (it, closest_pos, -1,
9030 MOVE_TO_POS);
9031 result = MOVE_POS_MATCH_OR_ZV;
9032 }
9033 else
9034 goto buffer_pos_reached;
9035 }
9036 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
9037 && IT_CHARPOS (*it) > to_charpos)
9038 goto buffer_pos_reached;
9039 else
9040 result = MOVE_NEWLINE_OR_CR;
9041 }
9042 else
9043 result = MOVE_NEWLINE_OR_CR;
9044 /* If we've processed the newline, make sure this flag is
9045 reset, as it must only be set when the newline itself is
9046 processed. */
9047 if (result == MOVE_NEWLINE_OR_CR)
9048 it->constrain_row_ascent_descent_p = false;
9049 break;
9050 }
9051
9052 prev_method = it->method;
9053 if (it->method == GET_FROM_BUFFER)
9054 prev_pos = IT_CHARPOS (*it);
9055 /* The current display element has been consumed. Advance
9056 to the next. */
9057 set_iterator_to_next (it, true);
9058 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9059 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
9060 if (IT_CHARPOS (*it) < to_charpos)
9061 saw_smaller_pos = true;
9062 if (it->bidi_p
9063 && (op & MOVE_TO_POS)
9064 && IT_CHARPOS (*it) >= to_charpos
9065 && IT_CHARPOS (*it) < closest_pos)
9066 closest_pos = IT_CHARPOS (*it);
9067
9068 /* Stop if lines are truncated and IT's current x-position is
9069 past the right edge of the window now. */
9070 if (it->line_wrap == TRUNCATE
9071 && it->current_x >= it->last_visible_x)
9072 {
9073 if (!FRAME_WINDOW_P (it->f)
9074 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9075 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9076 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9077 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9078 {
9079 bool at_eob_p = false;
9080
9081 if ((at_eob_p = !get_next_display_element (it))
9082 || BUFFER_POS_REACHED_P ()
9083 /* If we are past TO_CHARPOS, but never saw any
9084 character positions smaller than TO_CHARPOS,
9085 return MOVE_POS_MATCH_OR_ZV, like the
9086 unidirectional display did. */
9087 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9088 && !saw_smaller_pos
9089 && IT_CHARPOS (*it) > to_charpos))
9090 {
9091 if (it->bidi_p
9092 && !BUFFER_POS_REACHED_P ()
9093 && !at_eob_p && closest_pos < ZV)
9094 {
9095 RESTORE_IT (it, &ppos_it, ppos_data);
9096 if (closest_pos != to_charpos)
9097 move_it_in_display_line_to (it, closest_pos, -1,
9098 MOVE_TO_POS);
9099 }
9100 result = MOVE_POS_MATCH_OR_ZV;
9101 break;
9102 }
9103 if (ITERATOR_AT_END_OF_LINE_P (it))
9104 {
9105 result = MOVE_NEWLINE_OR_CR;
9106 break;
9107 }
9108 }
9109 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9110 && !saw_smaller_pos
9111 && IT_CHARPOS (*it) > to_charpos)
9112 {
9113 if (closest_pos < ZV)
9114 {
9115 RESTORE_IT (it, &ppos_it, ppos_data);
9116 if (closest_pos != to_charpos)
9117 move_it_in_display_line_to (it, closest_pos, -1,
9118 MOVE_TO_POS);
9119 }
9120 result = MOVE_POS_MATCH_OR_ZV;
9121 break;
9122 }
9123 result = MOVE_LINE_TRUNCATED;
9124 break;
9125 }
9126 #undef IT_RESET_X_ASCENT_DESCENT
9127 }
9128
9129 #undef BUFFER_POS_REACHED_P
9130
9131 /* If we scanned beyond TO_POS, restore the saved iterator either to
9132 the wrap point (if found), or to atpos/atx location. We decide which
9133 data to use to restore the saved iterator state by their X coordinates,
9134 since buffer positions might increase non-monotonically with screen
9135 coordinates due to bidi reordering. */
9136 if (result == MOVE_LINE_CONTINUED
9137 && it->line_wrap == WORD_WRAP
9138 && wrap_it.sp >= 0
9139 && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
9140 || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
9141 RESTORE_IT (it, &wrap_it, wrap_data);
9142 else if (atpos_it.sp >= 0)
9143 RESTORE_IT (it, &atpos_it, atpos_data);
9144 else if (atx_it.sp >= 0)
9145 RESTORE_IT (it, &atx_it, atx_data);
9146
9147 done:
9148
9149 if (atpos_data)
9150 bidi_unshelve_cache (atpos_data, true);
9151 if (atx_data)
9152 bidi_unshelve_cache (atx_data, true);
9153 if (wrap_data)
9154 bidi_unshelve_cache (wrap_data, true);
9155 if (ppos_data)
9156 bidi_unshelve_cache (ppos_data, true);
9157
9158 /* Restore the iterator settings altered at the beginning of this
9159 function. */
9160 it->glyph_row = saved_glyph_row;
9161 return result;
9162 }
9163
9164 /* For external use. */
9165 void
9166 move_it_in_display_line (struct it *it,
9167 ptrdiff_t to_charpos, int to_x,
9168 enum move_operation_enum op)
9169 {
9170 if (it->line_wrap == WORD_WRAP
9171 && (op & MOVE_TO_X))
9172 {
9173 struct it save_it;
9174 void *save_data = NULL;
9175 int skip;
9176
9177 SAVE_IT (save_it, *it, save_data);
9178 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9179 /* When word-wrap is on, TO_X may lie past the end
9180 of a wrapped line. Then it->current is the
9181 character on the next line, so backtrack to the
9182 space before the wrap point. */
9183 if (skip == MOVE_LINE_CONTINUED)
9184 {
9185 int prev_x = max (it->current_x - 1, 0);
9186 RESTORE_IT (it, &save_it, save_data);
9187 move_it_in_display_line_to
9188 (it, -1, prev_x, MOVE_TO_X);
9189 }
9190 else
9191 bidi_unshelve_cache (save_data, true);
9192 }
9193 else
9194 move_it_in_display_line_to (it, to_charpos, to_x, op);
9195 }
9196
9197
9198 /* Move IT forward until it satisfies one or more of the criteria in
9199 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9200
9201 OP is a bit-mask that specifies where to stop, and in particular,
9202 which of those four position arguments makes a difference. See the
9203 description of enum move_operation_enum.
9204
9205 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9206 screen line, this function will set IT to the next position that is
9207 displayed to the right of TO_CHARPOS on the screen.
9208
9209 Return the maximum pixel length of any line scanned but never more
9210 than it.last_visible_x. */
9211
9212 int
9213 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9214 {
9215 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9216 int line_height, line_start_x = 0, reached = 0;
9217 int max_current_x = 0;
9218 void *backup_data = NULL;
9219
9220 for (;;)
9221 {
9222 if (op & MOVE_TO_VPOS)
9223 {
9224 /* If no TO_CHARPOS and no TO_X specified, stop at the
9225 start of the line TO_VPOS. */
9226 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9227 {
9228 if (it->vpos == to_vpos)
9229 {
9230 reached = 1;
9231 break;
9232 }
9233 else
9234 skip = move_it_in_display_line_to (it, -1, -1, 0);
9235 }
9236 else
9237 {
9238 /* TO_VPOS >= 0 means stop at TO_X in the line at
9239 TO_VPOS, or at TO_POS, whichever comes first. */
9240 if (it->vpos == to_vpos)
9241 {
9242 reached = 2;
9243 break;
9244 }
9245
9246 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9247
9248 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9249 {
9250 reached = 3;
9251 break;
9252 }
9253 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9254 {
9255 /* We have reached TO_X but not in the line we want. */
9256 skip = move_it_in_display_line_to (it, to_charpos,
9257 -1, MOVE_TO_POS);
9258 if (skip == MOVE_POS_MATCH_OR_ZV)
9259 {
9260 reached = 4;
9261 break;
9262 }
9263 }
9264 }
9265 }
9266 else if (op & MOVE_TO_Y)
9267 {
9268 struct it it_backup;
9269
9270 if (it->line_wrap == WORD_WRAP)
9271 SAVE_IT (it_backup, *it, backup_data);
9272
9273 /* TO_Y specified means stop at TO_X in the line containing
9274 TO_Y---or at TO_CHARPOS if this is reached first. The
9275 problem is that we can't really tell whether the line
9276 contains TO_Y before we have completely scanned it, and
9277 this may skip past TO_X. What we do is to first scan to
9278 TO_X.
9279
9280 If TO_X is not specified, use a TO_X of zero. The reason
9281 is to make the outcome of this function more predictable.
9282 If we didn't use TO_X == 0, we would stop at the end of
9283 the line which is probably not what a caller would expect
9284 to happen. */
9285 skip = move_it_in_display_line_to
9286 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9287 (MOVE_TO_X | (op & MOVE_TO_POS)));
9288
9289 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9290 if (skip == MOVE_POS_MATCH_OR_ZV)
9291 reached = 5;
9292 else if (skip == MOVE_X_REACHED)
9293 {
9294 /* If TO_X was reached, we want to know whether TO_Y is
9295 in the line. We know this is the case if the already
9296 scanned glyphs make the line tall enough. Otherwise,
9297 we must check by scanning the rest of the line. */
9298 line_height = it->max_ascent + it->max_descent;
9299 if (to_y >= it->current_y
9300 && to_y < it->current_y + line_height)
9301 {
9302 reached = 6;
9303 break;
9304 }
9305 SAVE_IT (it_backup, *it, backup_data);
9306 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9307 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9308 op & MOVE_TO_POS);
9309 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9310 line_height = it->max_ascent + it->max_descent;
9311 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9312
9313 if (to_y >= it->current_y
9314 && to_y < it->current_y + line_height)
9315 {
9316 /* If TO_Y is in this line and TO_X was reached
9317 above, we scanned too far. We have to restore
9318 IT's settings to the ones before skipping. But
9319 keep the more accurate values of max_ascent and
9320 max_descent we've found while skipping the rest
9321 of the line, for the sake of callers, such as
9322 pos_visible_p, that need to know the line
9323 height. */
9324 int max_ascent = it->max_ascent;
9325 int max_descent = it->max_descent;
9326
9327 RESTORE_IT (it, &it_backup, backup_data);
9328 it->max_ascent = max_ascent;
9329 it->max_descent = max_descent;
9330 reached = 6;
9331 }
9332 else
9333 {
9334 skip = skip2;
9335 if (skip == MOVE_POS_MATCH_OR_ZV)
9336 reached = 7;
9337 }
9338 }
9339 else
9340 {
9341 /* Check whether TO_Y is in this line. */
9342 line_height = it->max_ascent + it->max_descent;
9343 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9344
9345 if (to_y >= it->current_y
9346 && to_y < it->current_y + line_height)
9347 {
9348 if (to_y > it->current_y)
9349 max_current_x = max (it->current_x, max_current_x);
9350
9351 /* When word-wrap is on, TO_X may lie past the end
9352 of a wrapped line. Then it->current is the
9353 character on the next line, so backtrack to the
9354 space before the wrap point. */
9355 if (skip == MOVE_LINE_CONTINUED
9356 && it->line_wrap == WORD_WRAP)
9357 {
9358 int prev_x = max (it->current_x - 1, 0);
9359 RESTORE_IT (it, &it_backup, backup_data);
9360 skip = move_it_in_display_line_to
9361 (it, -1, prev_x, MOVE_TO_X);
9362 }
9363
9364 reached = 6;
9365 }
9366 }
9367
9368 if (reached)
9369 {
9370 max_current_x = max (it->current_x, max_current_x);
9371 break;
9372 }
9373 }
9374 else if (BUFFERP (it->object)
9375 && (it->method == GET_FROM_BUFFER
9376 || it->method == GET_FROM_STRETCH)
9377 && IT_CHARPOS (*it) >= to_charpos
9378 /* Under bidi iteration, a call to set_iterator_to_next
9379 can scan far beyond to_charpos if the initial
9380 portion of the next line needs to be reordered. In
9381 that case, give move_it_in_display_line_to another
9382 chance below. */
9383 && !(it->bidi_p
9384 && it->bidi_it.scan_dir == -1))
9385 skip = MOVE_POS_MATCH_OR_ZV;
9386 else
9387 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9388
9389 switch (skip)
9390 {
9391 case MOVE_POS_MATCH_OR_ZV:
9392 max_current_x = max (it->current_x, max_current_x);
9393 reached = 8;
9394 goto out;
9395
9396 case MOVE_NEWLINE_OR_CR:
9397 max_current_x = max (it->current_x, max_current_x);
9398 set_iterator_to_next (it, true);
9399 it->continuation_lines_width = 0;
9400 break;
9401
9402 case MOVE_LINE_TRUNCATED:
9403 max_current_x = it->last_visible_x;
9404 it->continuation_lines_width = 0;
9405 reseat_at_next_visible_line_start (it, false);
9406 if ((op & MOVE_TO_POS) != 0
9407 && IT_CHARPOS (*it) > to_charpos)
9408 {
9409 reached = 9;
9410 goto out;
9411 }
9412 break;
9413
9414 case MOVE_LINE_CONTINUED:
9415 max_current_x = it->last_visible_x;
9416 /* For continued lines ending in a tab, some of the glyphs
9417 associated with the tab are displayed on the current
9418 line. Since it->current_x does not include these glyphs,
9419 we use it->last_visible_x instead. */
9420 if (it->c == '\t')
9421 {
9422 it->continuation_lines_width += it->last_visible_x;
9423 /* When moving by vpos, ensure that the iterator really
9424 advances to the next line (bug#847, bug#969). Fixme:
9425 do we need to do this in other circumstances? */
9426 if (it->current_x != it->last_visible_x
9427 && (op & MOVE_TO_VPOS)
9428 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9429 {
9430 line_start_x = it->current_x + it->pixel_width
9431 - it->last_visible_x;
9432 if (FRAME_WINDOW_P (it->f))
9433 {
9434 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9435 struct font *face_font = face->font;
9436
9437 /* When display_line produces a continued line
9438 that ends in a TAB, it skips a tab stop that
9439 is closer than the font's space character
9440 width (see x_produce_glyphs where it produces
9441 the stretch glyph which represents a TAB).
9442 We need to reproduce the same logic here. */
9443 eassert (face_font);
9444 if (face_font)
9445 {
9446 if (line_start_x < face_font->space_width)
9447 line_start_x
9448 += it->tab_width * face_font->space_width;
9449 }
9450 }
9451 set_iterator_to_next (it, false);
9452 }
9453 }
9454 else
9455 it->continuation_lines_width += it->current_x;
9456 break;
9457
9458 default:
9459 emacs_abort ();
9460 }
9461
9462 /* Reset/increment for the next run. */
9463 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9464 it->current_x = line_start_x;
9465 line_start_x = 0;
9466 it->hpos = 0;
9467 it->current_y += it->max_ascent + it->max_descent;
9468 ++it->vpos;
9469 last_height = it->max_ascent + it->max_descent;
9470 it->max_ascent = it->max_descent = 0;
9471 }
9472
9473 out:
9474
9475 /* On text terminals, we may stop at the end of a line in the middle
9476 of a multi-character glyph. If the glyph itself is continued,
9477 i.e. it is actually displayed on the next line, don't treat this
9478 stopping point as valid; move to the next line instead (unless
9479 that brings us offscreen). */
9480 if (!FRAME_WINDOW_P (it->f)
9481 && op & MOVE_TO_POS
9482 && IT_CHARPOS (*it) == to_charpos
9483 && it->what == IT_CHARACTER
9484 && it->nglyphs > 1
9485 && it->line_wrap == WINDOW_WRAP
9486 && it->current_x == it->last_visible_x - 1
9487 && it->c != '\n'
9488 && it->c != '\t'
9489 && it->w->window_end_valid
9490 && it->vpos < it->w->window_end_vpos)
9491 {
9492 it->continuation_lines_width += it->current_x;
9493 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9494 it->current_y += it->max_ascent + it->max_descent;
9495 ++it->vpos;
9496 last_height = it->max_ascent + it->max_descent;
9497 }
9498
9499 if (backup_data)
9500 bidi_unshelve_cache (backup_data, true);
9501
9502 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9503
9504 return max_current_x;
9505 }
9506
9507
9508 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9509
9510 If DY > 0, move IT backward at least that many pixels. DY = 0
9511 means move IT backward to the preceding line start or BEGV. This
9512 function may move over more than DY pixels if IT->current_y - DY
9513 ends up in the middle of a line; in this case IT->current_y will be
9514 set to the top of the line moved to. */
9515
9516 void
9517 move_it_vertically_backward (struct it *it, int dy)
9518 {
9519 int nlines, h;
9520 struct it it2, it3;
9521 void *it2data = NULL, *it3data = NULL;
9522 ptrdiff_t start_pos;
9523 int nchars_per_row
9524 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9525 ptrdiff_t pos_limit;
9526
9527 move_further_back:
9528 eassert (dy >= 0);
9529
9530 start_pos = IT_CHARPOS (*it);
9531
9532 /* Estimate how many newlines we must move back. */
9533 nlines = max (1, dy / default_line_pixel_height (it->w));
9534 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9535 pos_limit = BEGV;
9536 else
9537 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9538
9539 /* Set the iterator's position that many lines back. But don't go
9540 back more than NLINES full screen lines -- this wins a day with
9541 buffers which have very long lines. */
9542 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9543 back_to_previous_visible_line_start (it);
9544
9545 /* Reseat the iterator here. When moving backward, we don't want
9546 reseat to skip forward over invisible text, set up the iterator
9547 to deliver from overlay strings at the new position etc. So,
9548 use reseat_1 here. */
9549 reseat_1 (it, it->current.pos, true);
9550
9551 /* We are now surely at a line start. */
9552 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9553 reordering is in effect. */
9554 it->continuation_lines_width = 0;
9555
9556 /* Move forward and see what y-distance we moved. First move to the
9557 start of the next line so that we get its height. We need this
9558 height to be able to tell whether we reached the specified
9559 y-distance. */
9560 SAVE_IT (it2, *it, it2data);
9561 it2.max_ascent = it2.max_descent = 0;
9562 do
9563 {
9564 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9565 MOVE_TO_POS | MOVE_TO_VPOS);
9566 }
9567 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9568 /* If we are in a display string which starts at START_POS,
9569 and that display string includes a newline, and we are
9570 right after that newline (i.e. at the beginning of a
9571 display line), exit the loop, because otherwise we will
9572 infloop, since move_it_to will see that it is already at
9573 START_POS and will not move. */
9574 || (it2.method == GET_FROM_STRING
9575 && IT_CHARPOS (it2) == start_pos
9576 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9577 eassert (IT_CHARPOS (*it) >= BEGV);
9578 SAVE_IT (it3, it2, it3data);
9579
9580 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9581 eassert (IT_CHARPOS (*it) >= BEGV);
9582 /* H is the actual vertical distance from the position in *IT
9583 and the starting position. */
9584 h = it2.current_y - it->current_y;
9585 /* NLINES is the distance in number of lines. */
9586 nlines = it2.vpos - it->vpos;
9587
9588 /* Correct IT's y and vpos position
9589 so that they are relative to the starting point. */
9590 it->vpos -= nlines;
9591 it->current_y -= h;
9592
9593 if (dy == 0)
9594 {
9595 /* DY == 0 means move to the start of the screen line. The
9596 value of nlines is > 0 if continuation lines were involved,
9597 or if the original IT position was at start of a line. */
9598 RESTORE_IT (it, it, it2data);
9599 if (nlines > 0)
9600 move_it_by_lines (it, nlines);
9601 /* The above code moves us to some position NLINES down,
9602 usually to its first glyph (leftmost in an L2R line), but
9603 that's not necessarily the start of the line, under bidi
9604 reordering. We want to get to the character position
9605 that is immediately after the newline of the previous
9606 line. */
9607 if (it->bidi_p
9608 && !it->continuation_lines_width
9609 && !STRINGP (it->string)
9610 && IT_CHARPOS (*it) > BEGV
9611 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9612 {
9613 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9614
9615 DEC_BOTH (cp, bp);
9616 cp = find_newline_no_quit (cp, bp, -1, NULL);
9617 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9618 }
9619 bidi_unshelve_cache (it3data, true);
9620 }
9621 else
9622 {
9623 /* The y-position we try to reach, relative to *IT.
9624 Note that H has been subtracted in front of the if-statement. */
9625 int target_y = it->current_y + h - dy;
9626 int y0 = it3.current_y;
9627 int y1;
9628 int line_height;
9629
9630 RESTORE_IT (&it3, &it3, it3data);
9631 y1 = line_bottom_y (&it3);
9632 line_height = y1 - y0;
9633 RESTORE_IT (it, it, it2data);
9634 /* If we did not reach target_y, try to move further backward if
9635 we can. If we moved too far backward, try to move forward. */
9636 if (target_y < it->current_y
9637 /* This is heuristic. In a window that's 3 lines high, with
9638 a line height of 13 pixels each, recentering with point
9639 on the bottom line will try to move -39/2 = 19 pixels
9640 backward. Try to avoid moving into the first line. */
9641 && (it->current_y - target_y
9642 > min (window_box_height (it->w), line_height * 2 / 3))
9643 && IT_CHARPOS (*it) > BEGV)
9644 {
9645 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9646 target_y - it->current_y));
9647 dy = it->current_y - target_y;
9648 goto move_further_back;
9649 }
9650 else if (target_y >= it->current_y + line_height
9651 && IT_CHARPOS (*it) < ZV)
9652 {
9653 /* Should move forward by at least one line, maybe more.
9654
9655 Note: Calling move_it_by_lines can be expensive on
9656 terminal frames, where compute_motion is used (via
9657 vmotion) to do the job, when there are very long lines
9658 and truncate-lines is nil. That's the reason for
9659 treating terminal frames specially here. */
9660
9661 if (!FRAME_WINDOW_P (it->f))
9662 move_it_vertically (it, target_y - it->current_y);
9663 else
9664 {
9665 do
9666 {
9667 move_it_by_lines (it, 1);
9668 }
9669 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9670 }
9671 }
9672 }
9673 }
9674
9675
9676 /* Move IT by a specified amount of pixel lines DY. DY negative means
9677 move backwards. DY = 0 means move to start of screen line. At the
9678 end, IT will be on the start of a screen line. */
9679
9680 void
9681 move_it_vertically (struct it *it, int dy)
9682 {
9683 if (dy <= 0)
9684 move_it_vertically_backward (it, -dy);
9685 else
9686 {
9687 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9688 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9689 MOVE_TO_POS | MOVE_TO_Y);
9690 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9691
9692 /* If buffer ends in ZV without a newline, move to the start of
9693 the line to satisfy the post-condition. */
9694 if (IT_CHARPOS (*it) == ZV
9695 && ZV > BEGV
9696 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9697 move_it_by_lines (it, 0);
9698 }
9699 }
9700
9701
9702 /* Move iterator IT past the end of the text line it is in. */
9703
9704 void
9705 move_it_past_eol (struct it *it)
9706 {
9707 enum move_it_result rc;
9708
9709 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9710 if (rc == MOVE_NEWLINE_OR_CR)
9711 set_iterator_to_next (it, false);
9712 }
9713
9714
9715 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9716 negative means move up. DVPOS == 0 means move to the start of the
9717 screen line.
9718
9719 Optimization idea: If we would know that IT->f doesn't use
9720 a face with proportional font, we could be faster for
9721 truncate-lines nil. */
9722
9723 void
9724 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9725 {
9726
9727 /* The commented-out optimization uses vmotion on terminals. This
9728 gives bad results, because elements like it->what, on which
9729 callers such as pos_visible_p rely, aren't updated. */
9730 /* struct position pos;
9731 if (!FRAME_WINDOW_P (it->f))
9732 {
9733 struct text_pos textpos;
9734
9735 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9736 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9737 reseat (it, textpos, true);
9738 it->vpos += pos.vpos;
9739 it->current_y += pos.vpos;
9740 }
9741 else */
9742
9743 if (dvpos == 0)
9744 {
9745 /* DVPOS == 0 means move to the start of the screen line. */
9746 move_it_vertically_backward (it, 0);
9747 /* Let next call to line_bottom_y calculate real line height. */
9748 last_height = 0;
9749 }
9750 else if (dvpos > 0)
9751 {
9752 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9753 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9754 {
9755 /* Only move to the next buffer position if we ended up in a
9756 string from display property, not in an overlay string
9757 (before-string or after-string). That is because the
9758 latter don't conceal the underlying buffer position, so
9759 we can ask to move the iterator to the exact position we
9760 are interested in. Note that, even if we are already at
9761 IT_CHARPOS (*it), the call below is not a no-op, as it
9762 will detect that we are at the end of the string, pop the
9763 iterator, and compute it->current_x and it->hpos
9764 correctly. */
9765 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9766 -1, -1, -1, MOVE_TO_POS);
9767 }
9768 }
9769 else
9770 {
9771 struct it it2;
9772 void *it2data = NULL;
9773 ptrdiff_t start_charpos, i;
9774 int nchars_per_row
9775 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9776 bool hit_pos_limit = false;
9777 ptrdiff_t pos_limit;
9778
9779 /* Start at the beginning of the screen line containing IT's
9780 position. This may actually move vertically backwards,
9781 in case of overlays, so adjust dvpos accordingly. */
9782 dvpos += it->vpos;
9783 move_it_vertically_backward (it, 0);
9784 dvpos -= it->vpos;
9785
9786 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9787 screen lines, and reseat the iterator there. */
9788 start_charpos = IT_CHARPOS (*it);
9789 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9790 pos_limit = BEGV;
9791 else
9792 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9793
9794 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9795 back_to_previous_visible_line_start (it);
9796 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9797 hit_pos_limit = true;
9798 reseat (it, it->current.pos, true);
9799
9800 /* Move further back if we end up in a string or an image. */
9801 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9802 {
9803 /* First try to move to start of display line. */
9804 dvpos += it->vpos;
9805 move_it_vertically_backward (it, 0);
9806 dvpos -= it->vpos;
9807 if (IT_POS_VALID_AFTER_MOVE_P (it))
9808 break;
9809 /* If start of line is still in string or image,
9810 move further back. */
9811 back_to_previous_visible_line_start (it);
9812 reseat (it, it->current.pos, true);
9813 dvpos--;
9814 }
9815
9816 it->current_x = it->hpos = 0;
9817
9818 /* Above call may have moved too far if continuation lines
9819 are involved. Scan forward and see if it did. */
9820 SAVE_IT (it2, *it, it2data);
9821 it2.vpos = it2.current_y = 0;
9822 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9823 it->vpos -= it2.vpos;
9824 it->current_y -= it2.current_y;
9825 it->current_x = it->hpos = 0;
9826
9827 /* If we moved too far back, move IT some lines forward. */
9828 if (it2.vpos > -dvpos)
9829 {
9830 int delta = it2.vpos + dvpos;
9831
9832 RESTORE_IT (&it2, &it2, it2data);
9833 SAVE_IT (it2, *it, it2data);
9834 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9835 /* Move back again if we got too far ahead. */
9836 if (IT_CHARPOS (*it) >= start_charpos)
9837 RESTORE_IT (it, &it2, it2data);
9838 else
9839 bidi_unshelve_cache (it2data, true);
9840 }
9841 else if (hit_pos_limit && pos_limit > BEGV
9842 && dvpos < 0 && it2.vpos < -dvpos)
9843 {
9844 /* If we hit the limit, but still didn't make it far enough
9845 back, that means there's a display string with a newline
9846 covering a large chunk of text, and that caused
9847 back_to_previous_visible_line_start try to go too far.
9848 Punish those who commit such atrocities by going back
9849 until we've reached DVPOS, after lifting the limit, which
9850 could make it slow for very long lines. "If it hurts,
9851 don't do that!" */
9852 dvpos += it2.vpos;
9853 RESTORE_IT (it, it, it2data);
9854 for (i = -dvpos; i > 0; --i)
9855 {
9856 back_to_previous_visible_line_start (it);
9857 it->vpos--;
9858 }
9859 reseat_1 (it, it->current.pos, true);
9860 }
9861 else
9862 RESTORE_IT (it, it, it2data);
9863 }
9864 }
9865
9866 /* Return true if IT points into the middle of a display vector. */
9867
9868 bool
9869 in_display_vector_p (struct it *it)
9870 {
9871 return (it->method == GET_FROM_DISPLAY_VECTOR
9872 && it->current.dpvec_index > 0
9873 && it->dpvec + it->current.dpvec_index != it->dpend);
9874 }
9875
9876 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9877 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9878 WINDOW must be a live window and defaults to the selected one. The
9879 return value is a cons of the maximum pixel-width of any text line and
9880 the maximum pixel-height of all text lines.
9881
9882 The optional argument FROM, if non-nil, specifies the first text
9883 position and defaults to the minimum accessible position of the buffer.
9884 If FROM is t, use the minimum accessible position that starts a
9885 non-empty line. TO, if non-nil, specifies the last text position and
9886 defaults to the maximum accessible position of the buffer. If TO is t,
9887 use the maximum accessible position that ends a non-empty line.
9888
9889 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9890 width that can be returned. X-LIMIT nil or omitted, means to use the
9891 pixel-width of WINDOW's body; use this if you want to know how high
9892 WINDOW should be become in order to fit all of its buffer's text with
9893 the width of WINDOW unaltered. Use the maximum width WINDOW may assume
9894 if you intend to change WINDOW's width. In any case, text whose
9895 x-coordinate is beyond X-LIMIT is ignored. Since calculating the width
9896 of long lines can take some time, it's always a good idea to make this
9897 argument as small as possible; in particular, if the buffer contains
9898 long lines that shall be truncated anyway.
9899
9900 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9901 height (excluding the height of the mode- or header-line, if any) that
9902 can be returned. Text lines whose y-coordinate is beyond Y-LIMIT are
9903 ignored. Since calculating the text height of a large buffer can take
9904 some time, it makes sense to specify this argument if the size of the
9905 buffer is large or unknown.
9906
9907 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9908 include the height of the mode- or header-line of WINDOW in the return
9909 value. If it is either the symbol `mode-line' or `header-line', include
9910 only the height of that line, if present, in the return value. If t,
9911 include the height of both, if present, in the return value. */)
9912 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9913 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9914 {
9915 struct window *w = decode_live_window (window);
9916 Lisp_Object buffer = w->contents;
9917 struct buffer *b;
9918 struct it it;
9919 struct buffer *old_b = NULL;
9920 ptrdiff_t start, end, pos;
9921 struct text_pos startp;
9922 void *itdata = NULL;
9923 int c, max_x = 0, max_y = 0, x = 0, y = 0;
9924
9925 CHECK_BUFFER (buffer);
9926 b = XBUFFER (buffer);
9927
9928 if (b != current_buffer)
9929 {
9930 old_b = current_buffer;
9931 set_buffer_internal (b);
9932 }
9933
9934 if (NILP (from))
9935 start = BEGV;
9936 else if (EQ (from, Qt))
9937 {
9938 start = pos = BEGV;
9939 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9940 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9941 start = pos;
9942 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9943 start = pos;
9944 }
9945 else
9946 {
9947 CHECK_NUMBER_COERCE_MARKER (from);
9948 start = min (max (XINT (from), BEGV), ZV);
9949 }
9950
9951 if (NILP (to))
9952 end = ZV;
9953 else if (EQ (to, Qt))
9954 {
9955 end = pos = ZV;
9956 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9957 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9958 end = pos;
9959 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9960 end = pos;
9961 }
9962 else
9963 {
9964 CHECK_NUMBER_COERCE_MARKER (to);
9965 end = max (start, min (XINT (to), ZV));
9966 }
9967
9968 if (!NILP (x_limit) && RANGED_INTEGERP (0, x_limit, INT_MAX))
9969 max_x = XINT (x_limit);
9970
9971 if (NILP (y_limit))
9972 max_y = INT_MAX;
9973 else if (RANGED_INTEGERP (0, y_limit, INT_MAX))
9974 max_y = XINT (y_limit);
9975
9976 itdata = bidi_shelve_cache ();
9977 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9978 start_display (&it, w, startp);
9979
9980 if (NILP (x_limit))
9981 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9982 else
9983 {
9984 it.last_visible_x = max_x;
9985 /* Actually, we never want move_it_to stop at to_x. But to make
9986 sure that move_it_in_display_line_to always moves far enough,
9987 we set it to INT_MAX and specify MOVE_TO_X. */
9988 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9989 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9990 /* Don't return more than X-LIMIT. */
9991 if (x > max_x)
9992 x = max_x;
9993 }
9994
9995 /* Subtract height of header-line which was counted automatically by
9996 start_display. */
9997 y = it.current_y + it.max_ascent + it.max_descent
9998 - WINDOW_HEADER_LINE_HEIGHT (w);
9999 /* Don't return more than Y-LIMIT. */
10000 if (y > max_y)
10001 y = max_y;
10002
10003 if (EQ (mode_and_header_line, Qheader_line)
10004 || EQ (mode_and_header_line, Qt))
10005 /* Re-add height of header-line as requested. */
10006 y = y + WINDOW_HEADER_LINE_HEIGHT (w);
10007
10008 if (EQ (mode_and_header_line, Qmode_line)
10009 || EQ (mode_and_header_line, Qt))
10010 /* Add height of mode-line as requested. */
10011 y = y + WINDOW_MODE_LINE_HEIGHT (w);
10012
10013 bidi_unshelve_cache (itdata, false);
10014
10015 if (old_b)
10016 set_buffer_internal (old_b);
10017
10018 return Fcons (make_number (x), make_number (y));
10019 }
10020 \f
10021 /***********************************************************************
10022 Messages
10023 ***********************************************************************/
10024
10025 /* Return the number of arguments the format string FORMAT needs. */
10026
10027 static ptrdiff_t
10028 format_nargs (char const *format)
10029 {
10030 ptrdiff_t nargs = 0;
10031 for (char const *p = format; (p = strchr (p, '%')); p++)
10032 if (p[1] == '%')
10033 p++;
10034 else
10035 nargs++;
10036 return nargs;
10037 }
10038
10039 /* Add a message with format string FORMAT and formatted arguments
10040 to *Messages*. */
10041
10042 void
10043 add_to_log (const char *format, ...)
10044 {
10045 va_list ap;
10046 va_start (ap, format);
10047 vadd_to_log (format, ap);
10048 va_end (ap);
10049 }
10050
10051 void
10052 vadd_to_log (char const *format, va_list ap)
10053 {
10054 ptrdiff_t form_nargs = format_nargs (format);
10055 ptrdiff_t nargs = 1 + form_nargs;
10056 Lisp_Object args[10];
10057 eassert (nargs <= ARRAYELTS (args));
10058 AUTO_STRING (args0, format);
10059 args[0] = args0;
10060 for (ptrdiff_t i = 1; i <= nargs; i++)
10061 args[i] = va_arg (ap, Lisp_Object);
10062 Lisp_Object msg = Qnil;
10063 msg = Fformat_message (nargs, args);
10064
10065 ptrdiff_t len = SBYTES (msg) + 1;
10066 USE_SAFE_ALLOCA;
10067 char *buffer = SAFE_ALLOCA (len);
10068 memcpy (buffer, SDATA (msg), len);
10069
10070 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
10071 SAFE_FREE ();
10072 }
10073
10074
10075 /* Output a newline in the *Messages* buffer if "needs" one. */
10076
10077 void
10078 message_log_maybe_newline (void)
10079 {
10080 if (message_log_need_newline)
10081 message_dolog ("", 0, true, false);
10082 }
10083
10084
10085 /* Add a string M of length NBYTES to the message log, optionally
10086 terminated with a newline when NLFLAG is true. MULTIBYTE, if
10087 true, means interpret the contents of M as multibyte. This
10088 function calls low-level routines in order to bypass text property
10089 hooks, etc. which might not be safe to run.
10090
10091 This may GC (insert may run before/after change hooks),
10092 so the buffer M must NOT point to a Lisp string. */
10093
10094 void
10095 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10096 {
10097 const unsigned char *msg = (const unsigned char *) m;
10098
10099 if (!NILP (Vmemory_full))
10100 return;
10101
10102 if (!NILP (Vmessage_log_max))
10103 {
10104 struct buffer *oldbuf;
10105 Lisp_Object oldpoint, oldbegv, oldzv;
10106 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10107 ptrdiff_t point_at_end = 0;
10108 ptrdiff_t zv_at_end = 0;
10109 Lisp_Object old_deactivate_mark;
10110
10111 old_deactivate_mark = Vdeactivate_mark;
10112 oldbuf = current_buffer;
10113
10114 /* Ensure the Messages buffer exists, and switch to it.
10115 If we created it, set the major-mode. */
10116 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10117 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10118 if (newbuffer
10119 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10120 call0 (intern ("messages-buffer-mode"));
10121
10122 bset_undo_list (current_buffer, Qt);
10123 bset_cache_long_scans (current_buffer, Qnil);
10124
10125 oldpoint = message_dolog_marker1;
10126 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10127 oldbegv = message_dolog_marker2;
10128 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10129 oldzv = message_dolog_marker3;
10130 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10131
10132 if (PT == Z)
10133 point_at_end = 1;
10134 if (ZV == Z)
10135 zv_at_end = 1;
10136
10137 BEGV = BEG;
10138 BEGV_BYTE = BEG_BYTE;
10139 ZV = Z;
10140 ZV_BYTE = Z_BYTE;
10141 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10142
10143 /* Insert the string--maybe converting multibyte to single byte
10144 or vice versa, so that all the text fits the buffer. */
10145 if (multibyte
10146 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10147 {
10148 ptrdiff_t i;
10149 int c, char_bytes;
10150 char work[1];
10151
10152 /* Convert a multibyte string to single-byte
10153 for the *Message* buffer. */
10154 for (i = 0; i < nbytes; i += char_bytes)
10155 {
10156 c = string_char_and_length (msg + i, &char_bytes);
10157 work[0] = CHAR_TO_BYTE8 (c);
10158 insert_1_both (work, 1, 1, true, false, false);
10159 }
10160 }
10161 else if (! multibyte
10162 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10163 {
10164 ptrdiff_t i;
10165 int c, char_bytes;
10166 unsigned char str[MAX_MULTIBYTE_LENGTH];
10167 /* Convert a single-byte string to multibyte
10168 for the *Message* buffer. */
10169 for (i = 0; i < nbytes; i++)
10170 {
10171 c = msg[i];
10172 MAKE_CHAR_MULTIBYTE (c);
10173 char_bytes = CHAR_STRING (c, str);
10174 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10175 }
10176 }
10177 else if (nbytes)
10178 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10179 true, false, false);
10180
10181 if (nlflag)
10182 {
10183 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10184 printmax_t dups;
10185
10186 insert_1_both ("\n", 1, 1, true, false, false);
10187
10188 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10189 this_bol = PT;
10190 this_bol_byte = PT_BYTE;
10191
10192 /* See if this line duplicates the previous one.
10193 If so, combine duplicates. */
10194 if (this_bol > BEG)
10195 {
10196 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10197 prev_bol = PT;
10198 prev_bol_byte = PT_BYTE;
10199
10200 dups = message_log_check_duplicate (prev_bol_byte,
10201 this_bol_byte);
10202 if (dups)
10203 {
10204 del_range_both (prev_bol, prev_bol_byte,
10205 this_bol, this_bol_byte, false);
10206 if (dups > 1)
10207 {
10208 char dupstr[sizeof " [ times]"
10209 + INT_STRLEN_BOUND (printmax_t)];
10210
10211 /* If you change this format, don't forget to also
10212 change message_log_check_duplicate. */
10213 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10214 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10215 insert_1_both (dupstr, duplen, duplen,
10216 true, false, true);
10217 }
10218 }
10219 }
10220
10221 /* If we have more than the desired maximum number of lines
10222 in the *Messages* buffer now, delete the oldest ones.
10223 This is safe because we don't have undo in this buffer. */
10224
10225 if (NATNUMP (Vmessage_log_max))
10226 {
10227 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10228 -XFASTINT (Vmessage_log_max) - 1, false);
10229 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10230 }
10231 }
10232 BEGV = marker_position (oldbegv);
10233 BEGV_BYTE = marker_byte_position (oldbegv);
10234
10235 if (zv_at_end)
10236 {
10237 ZV = Z;
10238 ZV_BYTE = Z_BYTE;
10239 }
10240 else
10241 {
10242 ZV = marker_position (oldzv);
10243 ZV_BYTE = marker_byte_position (oldzv);
10244 }
10245
10246 if (point_at_end)
10247 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10248 else
10249 /* We can't do Fgoto_char (oldpoint) because it will run some
10250 Lisp code. */
10251 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10252 marker_byte_position (oldpoint));
10253
10254 unchain_marker (XMARKER (oldpoint));
10255 unchain_marker (XMARKER (oldbegv));
10256 unchain_marker (XMARKER (oldzv));
10257
10258 /* We called insert_1_both above with its 5th argument (PREPARE)
10259 false, which prevents insert_1_both from calling
10260 prepare_to_modify_buffer, which in turns prevents us from
10261 incrementing windows_or_buffers_changed even if *Messages* is
10262 shown in some window. So we must manually set
10263 windows_or_buffers_changed here to make up for that. */
10264 windows_or_buffers_changed = old_windows_or_buffers_changed;
10265 bset_redisplay (current_buffer);
10266
10267 set_buffer_internal (oldbuf);
10268
10269 message_log_need_newline = !nlflag;
10270 Vdeactivate_mark = old_deactivate_mark;
10271 }
10272 }
10273
10274
10275 /* We are at the end of the buffer after just having inserted a newline.
10276 (Note: We depend on the fact we won't be crossing the gap.)
10277 Check to see if the most recent message looks a lot like the previous one.
10278 Return 0 if different, 1 if the new one should just replace it, or a
10279 value N > 1 if we should also append " [N times]". */
10280
10281 static intmax_t
10282 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10283 {
10284 ptrdiff_t i;
10285 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10286 bool seen_dots = false;
10287 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10288 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10289
10290 for (i = 0; i < len; i++)
10291 {
10292 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10293 seen_dots = true;
10294 if (p1[i] != p2[i])
10295 return seen_dots;
10296 }
10297 p1 += len;
10298 if (*p1 == '\n')
10299 return 2;
10300 if (*p1++ == ' ' && *p1++ == '[')
10301 {
10302 char *pend;
10303 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10304 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10305 return n + 1;
10306 }
10307 return 0;
10308 }
10309 \f
10310
10311 /* Display an echo area message M with a specified length of NBYTES
10312 bytes. The string may include null characters. If M is not a
10313 string, clear out any existing message, and let the mini-buffer
10314 text show through.
10315
10316 This function cancels echoing. */
10317
10318 void
10319 message3 (Lisp_Object m)
10320 {
10321 clear_message (true, true);
10322 cancel_echoing ();
10323
10324 /* First flush out any partial line written with print. */
10325 message_log_maybe_newline ();
10326 if (STRINGP (m))
10327 {
10328 ptrdiff_t nbytes = SBYTES (m);
10329 bool multibyte = STRING_MULTIBYTE (m);
10330 char *buffer;
10331 USE_SAFE_ALLOCA;
10332 SAFE_ALLOCA_STRING (buffer, m);
10333 message_dolog (buffer, nbytes, true, multibyte);
10334 SAFE_FREE ();
10335 }
10336 if (! inhibit_message)
10337 message3_nolog (m);
10338 }
10339
10340 /* Log the message M to stderr. Log an empty line if M is not a string. */
10341
10342 static void
10343 message_to_stderr (Lisp_Object m)
10344 {
10345 if (noninteractive_need_newline)
10346 {
10347 noninteractive_need_newline = false;
10348 fputc ('\n', stderr);
10349 }
10350 if (STRINGP (m))
10351 {
10352 Lisp_Object coding_system = Vlocale_coding_system;
10353 Lisp_Object s;
10354
10355 if (!NILP (Vcoding_system_for_write))
10356 coding_system = Vcoding_system_for_write;
10357 if (!NILP (coding_system))
10358 s = code_convert_string_norecord (m, coding_system, true);
10359 else
10360 s = m;
10361
10362 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10363 }
10364 if (!cursor_in_echo_area)
10365 fputc ('\n', stderr);
10366 fflush (stderr);
10367 }
10368
10369 /* The non-logging version of message3.
10370 This does not cancel echoing, because it is used for echoing.
10371 Perhaps we need to make a separate function for echoing
10372 and make this cancel echoing. */
10373
10374 void
10375 message3_nolog (Lisp_Object m)
10376 {
10377 struct frame *sf = SELECTED_FRAME ();
10378
10379 if (FRAME_INITIAL_P (sf))
10380 message_to_stderr (m);
10381 /* Error messages get reported properly by cmd_error, so this must be just an
10382 informative message; if the frame hasn't really been initialized yet, just
10383 toss it. */
10384 else if (INTERACTIVE && sf->glyphs_initialized_p)
10385 {
10386 /* Get the frame containing the mini-buffer
10387 that the selected frame is using. */
10388 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10389 Lisp_Object frame = XWINDOW (mini_window)->frame;
10390 struct frame *f = XFRAME (frame);
10391
10392 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10393 Fmake_frame_visible (frame);
10394
10395 if (STRINGP (m) && SCHARS (m) > 0)
10396 {
10397 set_message (m);
10398 if (minibuffer_auto_raise)
10399 Fraise_frame (frame);
10400 /* Assume we are not echoing.
10401 (If we are, echo_now will override this.) */
10402 echo_message_buffer = Qnil;
10403 }
10404 else
10405 clear_message (true, true);
10406
10407 do_pending_window_change (false);
10408 echo_area_display (true);
10409 do_pending_window_change (false);
10410 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10411 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10412 }
10413 }
10414
10415
10416 /* Display a null-terminated echo area message M. If M is 0, clear
10417 out any existing message, and let the mini-buffer text show through.
10418
10419 The buffer M must continue to exist until after the echo area gets
10420 cleared or some other message gets displayed there. Do not pass
10421 text that is stored in a Lisp string. Do not pass text in a buffer
10422 that was alloca'd. */
10423
10424 void
10425 message1 (const char *m)
10426 {
10427 message3 (m ? build_unibyte_string (m) : Qnil);
10428 }
10429
10430
10431 /* The non-logging counterpart of message1. */
10432
10433 void
10434 message1_nolog (const char *m)
10435 {
10436 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10437 }
10438
10439 /* Display a message M which contains a single %s
10440 which gets replaced with STRING. */
10441
10442 void
10443 message_with_string (const char *m, Lisp_Object string, bool log)
10444 {
10445 CHECK_STRING (string);
10446
10447 bool need_message;
10448 if (noninteractive)
10449 need_message = !!m;
10450 else if (!INTERACTIVE)
10451 need_message = false;
10452 else
10453 {
10454 /* The frame whose minibuffer we're going to display the message on.
10455 It may be larger than the selected frame, so we need
10456 to use its buffer, not the selected frame's buffer. */
10457 Lisp_Object mini_window;
10458 struct frame *f, *sf = SELECTED_FRAME ();
10459
10460 /* Get the frame containing the minibuffer
10461 that the selected frame is using. */
10462 mini_window = FRAME_MINIBUF_WINDOW (sf);
10463 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10464
10465 /* Error messages get reported properly by cmd_error, so this must be
10466 just an informative message; if the frame hasn't really been
10467 initialized yet, just toss it. */
10468 need_message = f->glyphs_initialized_p;
10469 }
10470
10471 if (need_message)
10472 {
10473 AUTO_STRING (fmt, m);
10474 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10475
10476 if (noninteractive)
10477 message_to_stderr (msg);
10478 else
10479 {
10480 if (log)
10481 message3 (msg);
10482 else
10483 message3_nolog (msg);
10484
10485 /* Print should start at the beginning of the message
10486 buffer next time. */
10487 message_buf_print = false;
10488 }
10489 }
10490 }
10491
10492
10493 /* Dump an informative message to the minibuf. If M is 0, clear out
10494 any existing message, and let the mini-buffer text show through.
10495
10496 The message must be safe ASCII and the format must not contain ` or
10497 '. If your message and format do not fit into this category,
10498 convert your arguments to Lisp objects and use Fmessage instead. */
10499
10500 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10501 vmessage (const char *m, va_list ap)
10502 {
10503 if (noninteractive)
10504 {
10505 if (m)
10506 {
10507 if (noninteractive_need_newline)
10508 putc ('\n', stderr);
10509 noninteractive_need_newline = false;
10510 vfprintf (stderr, m, ap);
10511 if (!cursor_in_echo_area)
10512 fprintf (stderr, "\n");
10513 fflush (stderr);
10514 }
10515 }
10516 else if (INTERACTIVE)
10517 {
10518 /* The frame whose mini-buffer we're going to display the message
10519 on. It may be larger than the selected frame, so we need to
10520 use its buffer, not the selected frame's buffer. */
10521 Lisp_Object mini_window;
10522 struct frame *f, *sf = SELECTED_FRAME ();
10523
10524 /* Get the frame containing the mini-buffer
10525 that the selected frame is using. */
10526 mini_window = FRAME_MINIBUF_WINDOW (sf);
10527 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10528
10529 /* Error messages get reported properly by cmd_error, so this must be
10530 just an informative message; if the frame hasn't really been
10531 initialized yet, just toss it. */
10532 if (f->glyphs_initialized_p)
10533 {
10534 if (m)
10535 {
10536 ptrdiff_t len;
10537 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10538 USE_SAFE_ALLOCA;
10539 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10540
10541 len = doprnt (message_buf, maxsize, m, 0, ap);
10542
10543 message3 (make_string (message_buf, len));
10544 SAFE_FREE ();
10545 }
10546 else
10547 message1 (0);
10548
10549 /* Print should start at the beginning of the message
10550 buffer next time. */
10551 message_buf_print = false;
10552 }
10553 }
10554 }
10555
10556 void
10557 message (const char *m, ...)
10558 {
10559 va_list ap;
10560 va_start (ap, m);
10561 vmessage (m, ap);
10562 va_end (ap);
10563 }
10564
10565
10566 /* Display the current message in the current mini-buffer. This is
10567 only called from error handlers in process.c, and is not time
10568 critical. */
10569
10570 void
10571 update_echo_area (void)
10572 {
10573 if (!NILP (echo_area_buffer[0]))
10574 {
10575 Lisp_Object string;
10576 string = Fcurrent_message ();
10577 message3 (string);
10578 }
10579 }
10580
10581
10582 /* Make sure echo area buffers in `echo_buffers' are live.
10583 If they aren't, make new ones. */
10584
10585 static void
10586 ensure_echo_area_buffers (void)
10587 {
10588 for (int i = 0; i < 2; i++)
10589 if (!BUFFERP (echo_buffer[i])
10590 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10591 {
10592 Lisp_Object old_buffer = echo_buffer[i];
10593 static char const name_fmt[] = " *Echo Area %d*";
10594 char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
10595 AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
10596 echo_buffer[i] = Fget_buffer_create (lname);
10597 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10598 /* to force word wrap in echo area -
10599 it was decided to postpone this*/
10600 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10601
10602 for (int j = 0; j < 2; j++)
10603 if (EQ (old_buffer, echo_area_buffer[j]))
10604 echo_area_buffer[j] = echo_buffer[i];
10605 }
10606 }
10607
10608
10609 /* Call FN with args A1..A2 with either the current or last displayed
10610 echo_area_buffer as current buffer.
10611
10612 WHICH zero means use the current message buffer
10613 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10614 from echo_buffer[] and clear it.
10615
10616 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10617 suitable buffer from echo_buffer[] and clear it.
10618
10619 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10620 that the current message becomes the last displayed one, choose a
10621 suitable buffer for echo_area_buffer[0], and clear it.
10622
10623 Value is what FN returns. */
10624
10625 static bool
10626 with_echo_area_buffer (struct window *w, int which,
10627 bool (*fn) (ptrdiff_t, Lisp_Object),
10628 ptrdiff_t a1, Lisp_Object a2)
10629 {
10630 Lisp_Object buffer;
10631 bool this_one, the_other, clear_buffer_p, rc;
10632 ptrdiff_t count = SPECPDL_INDEX ();
10633
10634 /* If buffers aren't live, make new ones. */
10635 ensure_echo_area_buffers ();
10636
10637 clear_buffer_p = false;
10638
10639 if (which == 0)
10640 this_one = false, the_other = true;
10641 else if (which > 0)
10642 this_one = true, the_other = false;
10643 else
10644 {
10645 this_one = false, the_other = true;
10646 clear_buffer_p = true;
10647
10648 /* We need a fresh one in case the current echo buffer equals
10649 the one containing the last displayed echo area message. */
10650 if (!NILP (echo_area_buffer[this_one])
10651 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10652 echo_area_buffer[this_one] = Qnil;
10653 }
10654
10655 /* Choose a suitable buffer from echo_buffer[] if we don't
10656 have one. */
10657 if (NILP (echo_area_buffer[this_one]))
10658 {
10659 echo_area_buffer[this_one]
10660 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10661 ? echo_buffer[the_other]
10662 : echo_buffer[this_one]);
10663 clear_buffer_p = true;
10664 }
10665
10666 buffer = echo_area_buffer[this_one];
10667
10668 /* Don't get confused by reusing the buffer used for echoing
10669 for a different purpose. */
10670 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10671 cancel_echoing ();
10672
10673 record_unwind_protect (unwind_with_echo_area_buffer,
10674 with_echo_area_buffer_unwind_data (w));
10675
10676 /* Make the echo area buffer current. Note that for display
10677 purposes, it is not necessary that the displayed window's buffer
10678 == current_buffer, except for text property lookup. So, let's
10679 only set that buffer temporarily here without doing a full
10680 Fset_window_buffer. We must also change w->pointm, though,
10681 because otherwise an assertions in unshow_buffer fails, and Emacs
10682 aborts. */
10683 set_buffer_internal_1 (XBUFFER (buffer));
10684 if (w)
10685 {
10686 wset_buffer (w, buffer);
10687 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10688 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10689 }
10690
10691 bset_undo_list (current_buffer, Qt);
10692 bset_read_only (current_buffer, Qnil);
10693 specbind (Qinhibit_read_only, Qt);
10694 specbind (Qinhibit_modification_hooks, Qt);
10695
10696 if (clear_buffer_p && Z > BEG)
10697 del_range (BEG, Z);
10698
10699 eassert (BEGV >= BEG);
10700 eassert (ZV <= Z && ZV >= BEGV);
10701
10702 rc = fn (a1, a2);
10703
10704 eassert (BEGV >= BEG);
10705 eassert (ZV <= Z && ZV >= BEGV);
10706
10707 unbind_to (count, Qnil);
10708 return rc;
10709 }
10710
10711
10712 /* Save state that should be preserved around the call to the function
10713 FN called in with_echo_area_buffer. */
10714
10715 static Lisp_Object
10716 with_echo_area_buffer_unwind_data (struct window *w)
10717 {
10718 int i = 0;
10719 Lisp_Object vector, tmp;
10720
10721 /* Reduce consing by keeping one vector in
10722 Vwith_echo_area_save_vector. */
10723 vector = Vwith_echo_area_save_vector;
10724 Vwith_echo_area_save_vector = Qnil;
10725
10726 if (NILP (vector))
10727 vector = Fmake_vector (make_number (11), Qnil);
10728
10729 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10730 ASET (vector, i, Vdeactivate_mark); ++i;
10731 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10732
10733 if (w)
10734 {
10735 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10736 ASET (vector, i, w->contents); ++i;
10737 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10738 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10739 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10740 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10741 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10742 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10743 }
10744 else
10745 {
10746 int end = i + 8;
10747 for (; i < end; ++i)
10748 ASET (vector, i, Qnil);
10749 }
10750
10751 eassert (i == ASIZE (vector));
10752 return vector;
10753 }
10754
10755
10756 /* Restore global state from VECTOR which was created by
10757 with_echo_area_buffer_unwind_data. */
10758
10759 static void
10760 unwind_with_echo_area_buffer (Lisp_Object vector)
10761 {
10762 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10763 Vdeactivate_mark = AREF (vector, 1);
10764 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10765
10766 if (WINDOWP (AREF (vector, 3)))
10767 {
10768 struct window *w;
10769 Lisp_Object buffer;
10770
10771 w = XWINDOW (AREF (vector, 3));
10772 buffer = AREF (vector, 4);
10773
10774 wset_buffer (w, buffer);
10775 set_marker_both (w->pointm, buffer,
10776 XFASTINT (AREF (vector, 5)),
10777 XFASTINT (AREF (vector, 6)));
10778 set_marker_both (w->old_pointm, buffer,
10779 XFASTINT (AREF (vector, 7)),
10780 XFASTINT (AREF (vector, 8)));
10781 set_marker_both (w->start, buffer,
10782 XFASTINT (AREF (vector, 9)),
10783 XFASTINT (AREF (vector, 10)));
10784 }
10785
10786 Vwith_echo_area_save_vector = vector;
10787 }
10788
10789
10790 /* Set up the echo area for use by print functions. MULTIBYTE_P
10791 means we will print multibyte. */
10792
10793 void
10794 setup_echo_area_for_printing (bool multibyte_p)
10795 {
10796 /* If we can't find an echo area any more, exit. */
10797 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10798 Fkill_emacs (Qnil);
10799
10800 ensure_echo_area_buffers ();
10801
10802 if (!message_buf_print)
10803 {
10804 /* A message has been output since the last time we printed.
10805 Choose a fresh echo area buffer. */
10806 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10807 echo_area_buffer[0] = echo_buffer[1];
10808 else
10809 echo_area_buffer[0] = echo_buffer[0];
10810
10811 /* Switch to that buffer and clear it. */
10812 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10813 bset_truncate_lines (current_buffer, Qnil);
10814
10815 if (Z > BEG)
10816 {
10817 ptrdiff_t count = SPECPDL_INDEX ();
10818 specbind (Qinhibit_read_only, Qt);
10819 /* Note that undo recording is always disabled. */
10820 del_range (BEG, Z);
10821 unbind_to (count, Qnil);
10822 }
10823 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10824
10825 /* Set up the buffer for the multibyteness we need. */
10826 if (multibyte_p
10827 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10828 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10829
10830 /* Raise the frame containing the echo area. */
10831 if (minibuffer_auto_raise)
10832 {
10833 struct frame *sf = SELECTED_FRAME ();
10834 Lisp_Object mini_window;
10835 mini_window = FRAME_MINIBUF_WINDOW (sf);
10836 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10837 }
10838
10839 message_log_maybe_newline ();
10840 message_buf_print = true;
10841 }
10842 else
10843 {
10844 if (NILP (echo_area_buffer[0]))
10845 {
10846 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10847 echo_area_buffer[0] = echo_buffer[1];
10848 else
10849 echo_area_buffer[0] = echo_buffer[0];
10850 }
10851
10852 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10853 {
10854 /* Someone switched buffers between print requests. */
10855 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10856 bset_truncate_lines (current_buffer, Qnil);
10857 }
10858 }
10859 }
10860
10861
10862 /* Display an echo area message in window W. Value is true if W's
10863 height is changed. If display_last_displayed_message_p,
10864 display the message that was last displayed, otherwise
10865 display the current message. */
10866
10867 static bool
10868 display_echo_area (struct window *w)
10869 {
10870 bool no_message_p, window_height_changed_p;
10871
10872 /* Temporarily disable garbage collections while displaying the echo
10873 area. This is done because a GC can print a message itself.
10874 That message would modify the echo area buffer's contents while a
10875 redisplay of the buffer is going on, and seriously confuse
10876 redisplay. */
10877 ptrdiff_t count = inhibit_garbage_collection ();
10878
10879 /* If there is no message, we must call display_echo_area_1
10880 nevertheless because it resizes the window. But we will have to
10881 reset the echo_area_buffer in question to nil at the end because
10882 with_echo_area_buffer will sets it to an empty buffer. */
10883 bool i = display_last_displayed_message_p;
10884 /* According to the C99, C11 and C++11 standards, the integral value
10885 of a "bool" is always 0 or 1, so this array access is safe here,
10886 if oddly typed. */
10887 no_message_p = NILP (echo_area_buffer[i]);
10888
10889 window_height_changed_p
10890 = with_echo_area_buffer (w, display_last_displayed_message_p,
10891 display_echo_area_1,
10892 (intptr_t) w, Qnil);
10893
10894 if (no_message_p)
10895 echo_area_buffer[i] = Qnil;
10896
10897 unbind_to (count, Qnil);
10898 return window_height_changed_p;
10899 }
10900
10901
10902 /* Helper for display_echo_area. Display the current buffer which
10903 contains the current echo area message in window W, a mini-window,
10904 a pointer to which is passed in A1. A2..A4 are currently not used.
10905 Change the height of W so that all of the message is displayed.
10906 Value is true if height of W was changed. */
10907
10908 static bool
10909 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10910 {
10911 intptr_t i1 = a1;
10912 struct window *w = (struct window *) i1;
10913 Lisp_Object window;
10914 struct text_pos start;
10915
10916 /* We are about to enter redisplay without going through
10917 redisplay_internal, so we need to forget these faces by hand
10918 here. */
10919 forget_escape_and_glyphless_faces ();
10920
10921 /* Do this before displaying, so that we have a large enough glyph
10922 matrix for the display. If we can't get enough space for the
10923 whole text, display the last N lines. That works by setting w->start. */
10924 bool window_height_changed_p = resize_mini_window (w, false);
10925
10926 /* Use the starting position chosen by resize_mini_window. */
10927 SET_TEXT_POS_FROM_MARKER (start, w->start);
10928
10929 /* Display. */
10930 clear_glyph_matrix (w->desired_matrix);
10931 XSETWINDOW (window, w);
10932 try_window (window, start, 0);
10933
10934 return window_height_changed_p;
10935 }
10936
10937
10938 /* Resize the echo area window to exactly the size needed for the
10939 currently displayed message, if there is one. If a mini-buffer
10940 is active, don't shrink it. */
10941
10942 void
10943 resize_echo_area_exactly (void)
10944 {
10945 if (BUFFERP (echo_area_buffer[0])
10946 && WINDOWP (echo_area_window))
10947 {
10948 struct window *w = XWINDOW (echo_area_window);
10949 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10950 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10951 (intptr_t) w, resize_exactly);
10952 if (resized_p)
10953 {
10954 windows_or_buffers_changed = 42;
10955 update_mode_lines = 30;
10956 redisplay_internal ();
10957 }
10958 }
10959 }
10960
10961
10962 /* Callback function for with_echo_area_buffer, when used from
10963 resize_echo_area_exactly. A1 contains a pointer to the window to
10964 resize, EXACTLY non-nil means resize the mini-window exactly to the
10965 size of the text displayed. A3 and A4 are not used. Value is what
10966 resize_mini_window returns. */
10967
10968 static bool
10969 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10970 {
10971 intptr_t i1 = a1;
10972 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10973 }
10974
10975
10976 /* Resize mini-window W to fit the size of its contents. EXACT_P
10977 means size the window exactly to the size needed. Otherwise, it's
10978 only enlarged until W's buffer is empty.
10979
10980 Set W->start to the right place to begin display. If the whole
10981 contents fit, start at the beginning. Otherwise, start so as
10982 to make the end of the contents appear. This is particularly
10983 important for y-or-n-p, but seems desirable generally.
10984
10985 Value is true if the window height has been changed. */
10986
10987 bool
10988 resize_mini_window (struct window *w, bool exact_p)
10989 {
10990 struct frame *f = XFRAME (w->frame);
10991 bool window_height_changed_p = false;
10992
10993 eassert (MINI_WINDOW_P (w));
10994
10995 /* By default, start display at the beginning. */
10996 set_marker_both (w->start, w->contents,
10997 BUF_BEGV (XBUFFER (w->contents)),
10998 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10999
11000 /* Don't resize windows while redisplaying a window; it would
11001 confuse redisplay functions when the size of the window they are
11002 displaying changes from under them. Such a resizing can happen,
11003 for instance, when which-func prints a long message while
11004 we are running fontification-functions. We're running these
11005 functions with safe_call which binds inhibit-redisplay to t. */
11006 if (!NILP (Vinhibit_redisplay))
11007 return false;
11008
11009 /* Nil means don't try to resize. */
11010 if (NILP (Vresize_mini_windows)
11011 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
11012 return false;
11013
11014 if (!FRAME_MINIBUF_ONLY_P (f))
11015 {
11016 struct it it;
11017 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
11018 + WINDOW_PIXEL_HEIGHT (w));
11019 int unit = FRAME_LINE_HEIGHT (f);
11020 int height, max_height;
11021 struct text_pos start;
11022 struct buffer *old_current_buffer = NULL;
11023
11024 if (current_buffer != XBUFFER (w->contents))
11025 {
11026 old_current_buffer = current_buffer;
11027 set_buffer_internal (XBUFFER (w->contents));
11028 }
11029
11030 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
11031
11032 /* Compute the max. number of lines specified by the user. */
11033 if (FLOATP (Vmax_mini_window_height))
11034 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
11035 else if (INTEGERP (Vmax_mini_window_height))
11036 max_height = XINT (Vmax_mini_window_height) * unit;
11037 else
11038 max_height = total_height / 4;
11039
11040 /* Correct that max. height if it's bogus. */
11041 max_height = clip_to_bounds (unit, max_height, total_height);
11042
11043 /* Find out the height of the text in the window. */
11044 if (it.line_wrap == TRUNCATE)
11045 height = unit;
11046 else
11047 {
11048 last_height = 0;
11049 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
11050 if (it.max_ascent == 0 && it.max_descent == 0)
11051 height = it.current_y + last_height;
11052 else
11053 height = it.current_y + it.max_ascent + it.max_descent;
11054 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
11055 }
11056
11057 /* Compute a suitable window start. */
11058 if (height > max_height)
11059 {
11060 height = (max_height / unit) * unit;
11061 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
11062 move_it_vertically_backward (&it, height - unit);
11063 start = it.current.pos;
11064 }
11065 else
11066 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
11067 SET_MARKER_FROM_TEXT_POS (w->start, start);
11068
11069 if (EQ (Vresize_mini_windows, Qgrow_only))
11070 {
11071 /* Let it grow only, until we display an empty message, in which
11072 case the window shrinks again. */
11073 if (height > WINDOW_PIXEL_HEIGHT (w))
11074 {
11075 int old_height = WINDOW_PIXEL_HEIGHT (w);
11076
11077 FRAME_WINDOWS_FROZEN (f) = true;
11078 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11079 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11080 }
11081 else if (height < WINDOW_PIXEL_HEIGHT (w)
11082 && (exact_p || BEGV == ZV))
11083 {
11084 int old_height = WINDOW_PIXEL_HEIGHT (w);
11085
11086 FRAME_WINDOWS_FROZEN (f) = false;
11087 shrink_mini_window (w, true);
11088 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11089 }
11090 }
11091 else
11092 {
11093 /* Always resize to exact size needed. */
11094 if (height > WINDOW_PIXEL_HEIGHT (w))
11095 {
11096 int old_height = WINDOW_PIXEL_HEIGHT (w);
11097
11098 FRAME_WINDOWS_FROZEN (f) = true;
11099 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11100 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11101 }
11102 else if (height < WINDOW_PIXEL_HEIGHT (w))
11103 {
11104 int old_height = WINDOW_PIXEL_HEIGHT (w);
11105
11106 FRAME_WINDOWS_FROZEN (f) = false;
11107 shrink_mini_window (w, true);
11108
11109 if (height)
11110 {
11111 FRAME_WINDOWS_FROZEN (f) = true;
11112 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11113 }
11114
11115 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11116 }
11117 }
11118
11119 if (old_current_buffer)
11120 set_buffer_internal (old_current_buffer);
11121 }
11122
11123 return window_height_changed_p;
11124 }
11125
11126
11127 /* Value is the current message, a string, or nil if there is no
11128 current message. */
11129
11130 Lisp_Object
11131 current_message (void)
11132 {
11133 Lisp_Object msg;
11134
11135 if (!BUFFERP (echo_area_buffer[0]))
11136 msg = Qnil;
11137 else
11138 {
11139 with_echo_area_buffer (0, 0, current_message_1,
11140 (intptr_t) &msg, Qnil);
11141 if (NILP (msg))
11142 echo_area_buffer[0] = Qnil;
11143 }
11144
11145 return msg;
11146 }
11147
11148
11149 static bool
11150 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11151 {
11152 intptr_t i1 = a1;
11153 Lisp_Object *msg = (Lisp_Object *) i1;
11154
11155 if (Z > BEG)
11156 *msg = make_buffer_string (BEG, Z, true);
11157 else
11158 *msg = Qnil;
11159 return false;
11160 }
11161
11162
11163 /* Push the current message on Vmessage_stack for later restoration
11164 by restore_message. Value is true if the current message isn't
11165 empty. This is a relatively infrequent operation, so it's not
11166 worth optimizing. */
11167
11168 bool
11169 push_message (void)
11170 {
11171 Lisp_Object msg = current_message ();
11172 Vmessage_stack = Fcons (msg, Vmessage_stack);
11173 return STRINGP (msg);
11174 }
11175
11176
11177 /* Restore message display from the top of Vmessage_stack. */
11178
11179 void
11180 restore_message (void)
11181 {
11182 eassert (CONSP (Vmessage_stack));
11183 message3_nolog (XCAR (Vmessage_stack));
11184 }
11185
11186
11187 /* Handler for unwind-protect calling pop_message. */
11188
11189 void
11190 pop_message_unwind (void)
11191 {
11192 /* Pop the top-most entry off Vmessage_stack. */
11193 eassert (CONSP (Vmessage_stack));
11194 Vmessage_stack = XCDR (Vmessage_stack);
11195 }
11196
11197
11198 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11199 exits. If the stack is not empty, we have a missing pop_message
11200 somewhere. */
11201
11202 void
11203 check_message_stack (void)
11204 {
11205 if (!NILP (Vmessage_stack))
11206 emacs_abort ();
11207 }
11208
11209
11210 /* Truncate to NCHARS what will be displayed in the echo area the next
11211 time we display it---but don't redisplay it now. */
11212
11213 void
11214 truncate_echo_area (ptrdiff_t nchars)
11215 {
11216 if (nchars == 0)
11217 echo_area_buffer[0] = Qnil;
11218 else if (!noninteractive
11219 && INTERACTIVE
11220 && !NILP (echo_area_buffer[0]))
11221 {
11222 struct frame *sf = SELECTED_FRAME ();
11223 /* Error messages get reported properly by cmd_error, so this must be
11224 just an informative message; if the frame hasn't really been
11225 initialized yet, just toss it. */
11226 if (sf->glyphs_initialized_p)
11227 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11228 }
11229 }
11230
11231
11232 /* Helper function for truncate_echo_area. Truncate the current
11233 message to at most NCHARS characters. */
11234
11235 static bool
11236 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11237 {
11238 if (BEG + nchars < Z)
11239 del_range (BEG + nchars, Z);
11240 if (Z == BEG)
11241 echo_area_buffer[0] = Qnil;
11242 return false;
11243 }
11244
11245 /* Set the current message to STRING. */
11246
11247 static void
11248 set_message (Lisp_Object string)
11249 {
11250 eassert (STRINGP (string));
11251
11252 message_enable_multibyte = STRING_MULTIBYTE (string);
11253
11254 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11255 message_buf_print = false;
11256 help_echo_showing_p = false;
11257
11258 if (STRINGP (Vdebug_on_message)
11259 && STRINGP (string)
11260 && fast_string_match (Vdebug_on_message, string) >= 0)
11261 call_debugger (list2 (Qerror, string));
11262 }
11263
11264
11265 /* Helper function for set_message. First argument is ignored and second
11266 argument has the same meaning as for set_message.
11267 This function is called with the echo area buffer being current. */
11268
11269 static bool
11270 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11271 {
11272 eassert (STRINGP (string));
11273
11274 /* Change multibyteness of the echo buffer appropriately. */
11275 if (message_enable_multibyte
11276 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11277 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11278
11279 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11280 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11281 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11282
11283 /* Insert new message at BEG. */
11284 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11285
11286 /* This function takes care of single/multibyte conversion.
11287 We just have to ensure that the echo area buffer has the right
11288 setting of enable_multibyte_characters. */
11289 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11290
11291 return false;
11292 }
11293
11294
11295 /* Clear messages. CURRENT_P means clear the current message.
11296 LAST_DISPLAYED_P means clear the message last displayed. */
11297
11298 void
11299 clear_message (bool current_p, bool last_displayed_p)
11300 {
11301 if (current_p)
11302 {
11303 echo_area_buffer[0] = Qnil;
11304 message_cleared_p = true;
11305 }
11306
11307 if (last_displayed_p)
11308 echo_area_buffer[1] = Qnil;
11309
11310 message_buf_print = false;
11311 }
11312
11313 /* Clear garbaged frames.
11314
11315 This function is used where the old redisplay called
11316 redraw_garbaged_frames which in turn called redraw_frame which in
11317 turn called clear_frame. The call to clear_frame was a source of
11318 flickering. I believe a clear_frame is not necessary. It should
11319 suffice in the new redisplay to invalidate all current matrices,
11320 and ensure a complete redisplay of all windows. */
11321
11322 static void
11323 clear_garbaged_frames (void)
11324 {
11325 if (frame_garbaged)
11326 {
11327 Lisp_Object tail, frame;
11328 struct frame *sf = SELECTED_FRAME ();
11329
11330 FOR_EACH_FRAME (tail, frame)
11331 {
11332 struct frame *f = XFRAME (frame);
11333
11334 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11335 {
11336 if (f->resized_p
11337 /* It makes no sense to redraw a non-selected TTY
11338 frame, since that will actually clear the
11339 selected frame, and might leave the selected
11340 frame with corrupted display, if it happens not
11341 to be marked garbaged. */
11342 && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
11343 redraw_frame (f);
11344 else
11345 clear_current_matrices (f);
11346 fset_redisplay (f);
11347 f->garbaged = false;
11348 f->resized_p = false;
11349 }
11350 }
11351
11352 frame_garbaged = false;
11353 }
11354 }
11355
11356
11357 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11358 selected_frame. */
11359
11360 static void
11361 echo_area_display (bool update_frame_p)
11362 {
11363 Lisp_Object mini_window;
11364 struct window *w;
11365 struct frame *f;
11366 bool window_height_changed_p = false;
11367 struct frame *sf = SELECTED_FRAME ();
11368
11369 mini_window = FRAME_MINIBUF_WINDOW (sf);
11370 w = XWINDOW (mini_window);
11371 f = XFRAME (WINDOW_FRAME (w));
11372
11373 /* Don't display if frame is invisible or not yet initialized. */
11374 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11375 return;
11376
11377 #ifdef HAVE_WINDOW_SYSTEM
11378 /* When Emacs starts, selected_frame may be the initial terminal
11379 frame. If we let this through, a message would be displayed on
11380 the terminal. */
11381 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11382 return;
11383 #endif /* HAVE_WINDOW_SYSTEM */
11384
11385 /* Redraw garbaged frames. */
11386 clear_garbaged_frames ();
11387
11388 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11389 {
11390 echo_area_window = mini_window;
11391 window_height_changed_p = display_echo_area (w);
11392 w->must_be_updated_p = true;
11393
11394 /* Update the display, unless called from redisplay_internal.
11395 Also don't update the screen during redisplay itself. The
11396 update will happen at the end of redisplay, and an update
11397 here could cause confusion. */
11398 if (update_frame_p && !redisplaying_p)
11399 {
11400 int n = 0;
11401
11402 /* If the display update has been interrupted by pending
11403 input, update mode lines in the frame. Due to the
11404 pending input, it might have been that redisplay hasn't
11405 been called, so that mode lines above the echo area are
11406 garbaged. This looks odd, so we prevent it here. */
11407 if (!display_completed)
11408 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11409
11410 if (window_height_changed_p
11411 /* Don't do this if Emacs is shutting down. Redisplay
11412 needs to run hooks. */
11413 && !NILP (Vrun_hooks))
11414 {
11415 /* Must update other windows. Likewise as in other
11416 cases, don't let this update be interrupted by
11417 pending input. */
11418 ptrdiff_t count = SPECPDL_INDEX ();
11419 specbind (Qredisplay_dont_pause, Qt);
11420 fset_redisplay (f);
11421 redisplay_internal ();
11422 unbind_to (count, Qnil);
11423 }
11424 else if (FRAME_WINDOW_P (f) && n == 0)
11425 {
11426 /* Window configuration is the same as before.
11427 Can do with a display update of the echo area,
11428 unless we displayed some mode lines. */
11429 update_single_window (w);
11430 flush_frame (f);
11431 }
11432 else
11433 update_frame (f, true, true);
11434
11435 /* If cursor is in the echo area, make sure that the next
11436 redisplay displays the minibuffer, so that the cursor will
11437 be replaced with what the minibuffer wants. */
11438 if (cursor_in_echo_area)
11439 wset_redisplay (XWINDOW (mini_window));
11440 }
11441 }
11442 else if (!EQ (mini_window, selected_window))
11443 wset_redisplay (XWINDOW (mini_window));
11444
11445 /* Last displayed message is now the current message. */
11446 echo_area_buffer[1] = echo_area_buffer[0];
11447 /* Inform read_char that we're not echoing. */
11448 echo_message_buffer = Qnil;
11449
11450 /* Prevent redisplay optimization in redisplay_internal by resetting
11451 this_line_start_pos. This is done because the mini-buffer now
11452 displays the message instead of its buffer text. */
11453 if (EQ (mini_window, selected_window))
11454 CHARPOS (this_line_start_pos) = 0;
11455
11456 if (window_height_changed_p)
11457 {
11458 fset_redisplay (f);
11459
11460 /* If window configuration was changed, frames may have been
11461 marked garbaged. Clear them or we will experience
11462 surprises wrt scrolling.
11463 FIXME: How/why/when? */
11464 clear_garbaged_frames ();
11465 }
11466 }
11467
11468 /* True if W's buffer was changed but not saved. */
11469
11470 static bool
11471 window_buffer_changed (struct window *w)
11472 {
11473 struct buffer *b = XBUFFER (w->contents);
11474
11475 eassert (BUFFER_LIVE_P (b));
11476
11477 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11478 }
11479
11480 /* True if W has %c in its mode line and mode line should be updated. */
11481
11482 static bool
11483 mode_line_update_needed (struct window *w)
11484 {
11485 return (w->column_number_displayed != -1
11486 && !(PT == w->last_point && !window_outdated (w))
11487 && (w->column_number_displayed != current_column ()));
11488 }
11489
11490 /* True if window start of W is frozen and may not be changed during
11491 redisplay. */
11492
11493 static bool
11494 window_frozen_p (struct window *w)
11495 {
11496 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11497 {
11498 Lisp_Object window;
11499
11500 XSETWINDOW (window, w);
11501 if (MINI_WINDOW_P (w))
11502 return false;
11503 else if (EQ (window, selected_window))
11504 return false;
11505 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11506 && EQ (window, Vminibuf_scroll_window))
11507 /* This special window can't be frozen too. */
11508 return false;
11509 else
11510 return true;
11511 }
11512 return false;
11513 }
11514
11515 /***********************************************************************
11516 Mode Lines and Frame Titles
11517 ***********************************************************************/
11518
11519 /* A buffer for constructing non-propertized mode-line strings and
11520 frame titles in it; allocated from the heap in init_xdisp and
11521 resized as needed in store_mode_line_noprop_char. */
11522
11523 static char *mode_line_noprop_buf;
11524
11525 /* The buffer's end, and a current output position in it. */
11526
11527 static char *mode_line_noprop_buf_end;
11528 static char *mode_line_noprop_ptr;
11529
11530 #define MODE_LINE_NOPROP_LEN(start) \
11531 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11532
11533 static enum {
11534 MODE_LINE_DISPLAY = 0,
11535 MODE_LINE_TITLE,
11536 MODE_LINE_NOPROP,
11537 MODE_LINE_STRING
11538 } mode_line_target;
11539
11540 /* Alist that caches the results of :propertize.
11541 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11542 static Lisp_Object mode_line_proptrans_alist;
11543
11544 /* List of strings making up the mode-line. */
11545 static Lisp_Object mode_line_string_list;
11546
11547 /* Base face property when building propertized mode line string. */
11548 static Lisp_Object mode_line_string_face;
11549 static Lisp_Object mode_line_string_face_prop;
11550
11551
11552 /* Unwind data for mode line strings */
11553
11554 static Lisp_Object Vmode_line_unwind_vector;
11555
11556 static Lisp_Object
11557 format_mode_line_unwind_data (struct frame *target_frame,
11558 struct buffer *obuf,
11559 Lisp_Object owin,
11560 bool save_proptrans)
11561 {
11562 Lisp_Object vector, tmp;
11563
11564 /* Reduce consing by keeping one vector in
11565 Vwith_echo_area_save_vector. */
11566 vector = Vmode_line_unwind_vector;
11567 Vmode_line_unwind_vector = Qnil;
11568
11569 if (NILP (vector))
11570 vector = Fmake_vector (make_number (10), Qnil);
11571
11572 ASET (vector, 0, make_number (mode_line_target));
11573 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11574 ASET (vector, 2, mode_line_string_list);
11575 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11576 ASET (vector, 4, mode_line_string_face);
11577 ASET (vector, 5, mode_line_string_face_prop);
11578
11579 if (obuf)
11580 XSETBUFFER (tmp, obuf);
11581 else
11582 tmp = Qnil;
11583 ASET (vector, 6, tmp);
11584 ASET (vector, 7, owin);
11585 if (target_frame)
11586 {
11587 /* Similarly to `with-selected-window', if the operation selects
11588 a window on another frame, we must restore that frame's
11589 selected window, and (for a tty) the top-frame. */
11590 ASET (vector, 8, target_frame->selected_window);
11591 if (FRAME_TERMCAP_P (target_frame))
11592 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11593 }
11594
11595 return vector;
11596 }
11597
11598 static void
11599 unwind_format_mode_line (Lisp_Object vector)
11600 {
11601 Lisp_Object old_window = AREF (vector, 7);
11602 Lisp_Object target_frame_window = AREF (vector, 8);
11603 Lisp_Object old_top_frame = AREF (vector, 9);
11604
11605 mode_line_target = XINT (AREF (vector, 0));
11606 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11607 mode_line_string_list = AREF (vector, 2);
11608 if (! EQ (AREF (vector, 3), Qt))
11609 mode_line_proptrans_alist = AREF (vector, 3);
11610 mode_line_string_face = AREF (vector, 4);
11611 mode_line_string_face_prop = AREF (vector, 5);
11612
11613 /* Select window before buffer, since it may change the buffer. */
11614 if (!NILP (old_window))
11615 {
11616 /* If the operation that we are unwinding had selected a window
11617 on a different frame, reset its frame-selected-window. For a
11618 text terminal, reset its top-frame if necessary. */
11619 if (!NILP (target_frame_window))
11620 {
11621 Lisp_Object frame
11622 = WINDOW_FRAME (XWINDOW (target_frame_window));
11623
11624 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11625 Fselect_window (target_frame_window, Qt);
11626
11627 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11628 Fselect_frame (old_top_frame, Qt);
11629 }
11630
11631 Fselect_window (old_window, Qt);
11632 }
11633
11634 if (!NILP (AREF (vector, 6)))
11635 {
11636 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11637 ASET (vector, 6, Qnil);
11638 }
11639
11640 Vmode_line_unwind_vector = vector;
11641 }
11642
11643
11644 /* Store a single character C for the frame title in mode_line_noprop_buf.
11645 Re-allocate mode_line_noprop_buf if necessary. */
11646
11647 static void
11648 store_mode_line_noprop_char (char c)
11649 {
11650 /* If output position has reached the end of the allocated buffer,
11651 increase the buffer's size. */
11652 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11653 {
11654 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11655 ptrdiff_t size = len;
11656 mode_line_noprop_buf =
11657 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11658 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11659 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11660 }
11661
11662 *mode_line_noprop_ptr++ = c;
11663 }
11664
11665
11666 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11667 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11668 characters that yield more columns than PRECISION; PRECISION <= 0
11669 means copy the whole string. Pad with spaces until FIELD_WIDTH
11670 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11671 pad. Called from display_mode_element when it is used to build a
11672 frame title. */
11673
11674 static int
11675 store_mode_line_noprop (const char *string, int field_width, int precision)
11676 {
11677 const unsigned char *str = (const unsigned char *) string;
11678 int n = 0;
11679 ptrdiff_t dummy, nbytes;
11680
11681 /* Copy at most PRECISION chars from STR. */
11682 nbytes = strlen (string);
11683 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11684 while (nbytes--)
11685 store_mode_line_noprop_char (*str++);
11686
11687 /* Fill up with spaces until FIELD_WIDTH reached. */
11688 while (field_width > 0
11689 && n < field_width)
11690 {
11691 store_mode_line_noprop_char (' ');
11692 ++n;
11693 }
11694
11695 return n;
11696 }
11697
11698 /***********************************************************************
11699 Frame Titles
11700 ***********************************************************************/
11701
11702 #ifdef HAVE_WINDOW_SYSTEM
11703
11704 /* Set the title of FRAME, if it has changed. The title format is
11705 Vicon_title_format if FRAME is iconified, otherwise it is
11706 frame_title_format. */
11707
11708 static void
11709 x_consider_frame_title (Lisp_Object frame)
11710 {
11711 struct frame *f = XFRAME (frame);
11712
11713 if ((FRAME_WINDOW_P (f)
11714 || FRAME_MINIBUF_ONLY_P (f)
11715 || f->explicit_name)
11716 && NILP (Fframe_parameter (frame, Qtooltip)))
11717 {
11718 /* Do we have more than one visible frame on this X display? */
11719 Lisp_Object tail, other_frame, fmt;
11720 ptrdiff_t title_start;
11721 char *title;
11722 ptrdiff_t len;
11723 struct it it;
11724 ptrdiff_t count = SPECPDL_INDEX ();
11725
11726 FOR_EACH_FRAME (tail, other_frame)
11727 {
11728 struct frame *tf = XFRAME (other_frame);
11729
11730 if (tf != f
11731 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11732 && !FRAME_MINIBUF_ONLY_P (tf)
11733 && !EQ (other_frame, tip_frame)
11734 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11735 break;
11736 }
11737
11738 /* Set global variable indicating that multiple frames exist. */
11739 multiple_frames = CONSP (tail);
11740
11741 /* Switch to the buffer of selected window of the frame. Set up
11742 mode_line_target so that display_mode_element will output into
11743 mode_line_noprop_buf; then display the title. */
11744 record_unwind_protect (unwind_format_mode_line,
11745 format_mode_line_unwind_data
11746 (f, current_buffer, selected_window, false));
11747
11748 Fselect_window (f->selected_window, Qt);
11749 set_buffer_internal_1
11750 (XBUFFER (XWINDOW (f->selected_window)->contents));
11751 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11752
11753 mode_line_target = MODE_LINE_TITLE;
11754 title_start = MODE_LINE_NOPROP_LEN (0);
11755 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11756 NULL, DEFAULT_FACE_ID);
11757 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11758 len = MODE_LINE_NOPROP_LEN (title_start);
11759 title = mode_line_noprop_buf + title_start;
11760 unbind_to (count, Qnil);
11761
11762 /* Set the title only if it's changed. This avoids consing in
11763 the common case where it hasn't. (If it turns out that we've
11764 already wasted too much time by walking through the list with
11765 display_mode_element, then we might need to optimize at a
11766 higher level than this.) */
11767 if (! STRINGP (f->name)
11768 || SBYTES (f->name) != len
11769 || memcmp (title, SDATA (f->name), len) != 0)
11770 x_implicitly_set_name (f, make_string (title, len), Qnil);
11771 }
11772 }
11773
11774 #endif /* not HAVE_WINDOW_SYSTEM */
11775
11776 \f
11777 /***********************************************************************
11778 Menu Bars
11779 ***********************************************************************/
11780
11781 /* True if we will not redisplay all visible windows. */
11782 #define REDISPLAY_SOME_P() \
11783 ((windows_or_buffers_changed == 0 \
11784 || windows_or_buffers_changed == REDISPLAY_SOME) \
11785 && (update_mode_lines == 0 \
11786 || update_mode_lines == REDISPLAY_SOME))
11787
11788 /* Prepare for redisplay by updating menu-bar item lists when
11789 appropriate. This can call eval. */
11790
11791 static void
11792 prepare_menu_bars (void)
11793 {
11794 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11795 bool some_windows = REDISPLAY_SOME_P ();
11796 Lisp_Object tooltip_frame;
11797
11798 #ifdef HAVE_WINDOW_SYSTEM
11799 tooltip_frame = tip_frame;
11800 #else
11801 tooltip_frame = Qnil;
11802 #endif
11803
11804 if (FUNCTIONP (Vpre_redisplay_function))
11805 {
11806 Lisp_Object windows = all_windows ? Qt : Qnil;
11807 if (all_windows && some_windows)
11808 {
11809 Lisp_Object ws = window_list ();
11810 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11811 {
11812 Lisp_Object this = XCAR (ws);
11813 struct window *w = XWINDOW (this);
11814 if (w->redisplay
11815 || XFRAME (w->frame)->redisplay
11816 || XBUFFER (w->contents)->text->redisplay)
11817 {
11818 windows = Fcons (this, windows);
11819 }
11820 }
11821 }
11822 safe__call1 (true, Vpre_redisplay_function, windows);
11823 }
11824
11825 /* Update all frame titles based on their buffer names, etc. We do
11826 this before the menu bars so that the buffer-menu will show the
11827 up-to-date frame titles. */
11828 #ifdef HAVE_WINDOW_SYSTEM
11829 if (all_windows)
11830 {
11831 Lisp_Object tail, frame;
11832
11833 FOR_EACH_FRAME (tail, frame)
11834 {
11835 struct frame *f = XFRAME (frame);
11836 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11837 if (some_windows
11838 && !f->redisplay
11839 && !w->redisplay
11840 && !XBUFFER (w->contents)->text->redisplay)
11841 continue;
11842
11843 if (!EQ (frame, tooltip_frame)
11844 && (FRAME_ICONIFIED_P (f)
11845 || FRAME_VISIBLE_P (f) == 1
11846 /* Exclude TTY frames that are obscured because they
11847 are not the top frame on their console. This is
11848 because x_consider_frame_title actually switches
11849 to the frame, which for TTY frames means it is
11850 marked as garbaged, and will be completely
11851 redrawn on the next redisplay cycle. This causes
11852 TTY frames to be completely redrawn, when there
11853 are more than one of them, even though nothing
11854 should be changed on display. */
11855 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11856 x_consider_frame_title (frame);
11857 }
11858 }
11859 #endif /* HAVE_WINDOW_SYSTEM */
11860
11861 /* Update the menu bar item lists, if appropriate. This has to be
11862 done before any actual redisplay or generation of display lines. */
11863
11864 if (all_windows)
11865 {
11866 Lisp_Object tail, frame;
11867 ptrdiff_t count = SPECPDL_INDEX ();
11868 /* True means that update_menu_bar has run its hooks
11869 so any further calls to update_menu_bar shouldn't do so again. */
11870 bool menu_bar_hooks_run = false;
11871
11872 record_unwind_save_match_data ();
11873
11874 FOR_EACH_FRAME (tail, frame)
11875 {
11876 struct frame *f = XFRAME (frame);
11877 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11878
11879 /* Ignore tooltip frame. */
11880 if (EQ (frame, tooltip_frame))
11881 continue;
11882
11883 if (some_windows
11884 && !f->redisplay
11885 && !w->redisplay
11886 && !XBUFFER (w->contents)->text->redisplay)
11887 continue;
11888
11889 run_window_size_change_functions (frame);
11890 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11891 #ifdef HAVE_WINDOW_SYSTEM
11892 update_tool_bar (f, false);
11893 #endif
11894 }
11895
11896 unbind_to (count, Qnil);
11897 }
11898 else
11899 {
11900 struct frame *sf = SELECTED_FRAME ();
11901 update_menu_bar (sf, true, false);
11902 #ifdef HAVE_WINDOW_SYSTEM
11903 update_tool_bar (sf, true);
11904 #endif
11905 }
11906 }
11907
11908
11909 /* Update the menu bar item list for frame F. This has to be done
11910 before we start to fill in any display lines, because it can call
11911 eval.
11912
11913 If SAVE_MATCH_DATA, we must save and restore it here.
11914
11915 If HOOKS_RUN, a previous call to update_menu_bar
11916 already ran the menu bar hooks for this redisplay, so there
11917 is no need to run them again. The return value is the
11918 updated value of this flag, to pass to the next call. */
11919
11920 static bool
11921 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11922 {
11923 Lisp_Object window;
11924 struct window *w;
11925
11926 /* If called recursively during a menu update, do nothing. This can
11927 happen when, for instance, an activate-menubar-hook causes a
11928 redisplay. */
11929 if (inhibit_menubar_update)
11930 return hooks_run;
11931
11932 window = FRAME_SELECTED_WINDOW (f);
11933 w = XWINDOW (window);
11934
11935 if (FRAME_WINDOW_P (f)
11936 ?
11937 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11938 || defined (HAVE_NS) || defined (USE_GTK)
11939 FRAME_EXTERNAL_MENU_BAR (f)
11940 #else
11941 FRAME_MENU_BAR_LINES (f) > 0
11942 #endif
11943 : FRAME_MENU_BAR_LINES (f) > 0)
11944 {
11945 /* If the user has switched buffers or windows, we need to
11946 recompute to reflect the new bindings. But we'll
11947 recompute when update_mode_lines is set too; that means
11948 that people can use force-mode-line-update to request
11949 that the menu bar be recomputed. The adverse effect on
11950 the rest of the redisplay algorithm is about the same as
11951 windows_or_buffers_changed anyway. */
11952 if (windows_or_buffers_changed
11953 /* This used to test w->update_mode_line, but we believe
11954 there is no need to recompute the menu in that case. */
11955 || update_mode_lines
11956 || window_buffer_changed (w))
11957 {
11958 struct buffer *prev = current_buffer;
11959 ptrdiff_t count = SPECPDL_INDEX ();
11960
11961 specbind (Qinhibit_menubar_update, Qt);
11962
11963 set_buffer_internal_1 (XBUFFER (w->contents));
11964 if (save_match_data)
11965 record_unwind_save_match_data ();
11966 if (NILP (Voverriding_local_map_menu_flag))
11967 {
11968 specbind (Qoverriding_terminal_local_map, Qnil);
11969 specbind (Qoverriding_local_map, Qnil);
11970 }
11971
11972 if (!hooks_run)
11973 {
11974 /* Run the Lucid hook. */
11975 safe_run_hooks (Qactivate_menubar_hook);
11976
11977 /* If it has changed current-menubar from previous value,
11978 really recompute the menu-bar from the value. */
11979 if (! NILP (Vlucid_menu_bar_dirty_flag))
11980 call0 (Qrecompute_lucid_menubar);
11981
11982 safe_run_hooks (Qmenu_bar_update_hook);
11983
11984 hooks_run = true;
11985 }
11986
11987 XSETFRAME (Vmenu_updating_frame, f);
11988 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11989
11990 /* Redisplay the menu bar in case we changed it. */
11991 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11992 || defined (HAVE_NS) || defined (USE_GTK)
11993 if (FRAME_WINDOW_P (f))
11994 {
11995 #if defined (HAVE_NS)
11996 /* All frames on Mac OS share the same menubar. So only
11997 the selected frame should be allowed to set it. */
11998 if (f == SELECTED_FRAME ())
11999 #endif
12000 set_frame_menubar (f, false, false);
12001 }
12002 else
12003 /* On a terminal screen, the menu bar is an ordinary screen
12004 line, and this makes it get updated. */
12005 w->update_mode_line = true;
12006 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12007 /* In the non-toolkit version, the menu bar is an ordinary screen
12008 line, and this makes it get updated. */
12009 w->update_mode_line = true;
12010 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12011
12012 unbind_to (count, Qnil);
12013 set_buffer_internal_1 (prev);
12014 }
12015 }
12016
12017 return hooks_run;
12018 }
12019
12020 /***********************************************************************
12021 Tool-bars
12022 ***********************************************************************/
12023
12024 #ifdef HAVE_WINDOW_SYSTEM
12025
12026 /* Select `frame' temporarily without running all the code in
12027 do_switch_frame.
12028 FIXME: Maybe do_switch_frame should be trimmed down similarly
12029 when `norecord' is set. */
12030 static void
12031 fast_set_selected_frame (Lisp_Object frame)
12032 {
12033 if (!EQ (selected_frame, frame))
12034 {
12035 selected_frame = frame;
12036 selected_window = XFRAME (frame)->selected_window;
12037 }
12038 }
12039
12040 /* Update the tool-bar item list for frame F. This has to be done
12041 before we start to fill in any display lines. Called from
12042 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
12043 and restore it here. */
12044
12045 static void
12046 update_tool_bar (struct frame *f, bool save_match_data)
12047 {
12048 #if defined (USE_GTK) || defined (HAVE_NS)
12049 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
12050 #else
12051 bool do_update = (WINDOWP (f->tool_bar_window)
12052 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
12053 #endif
12054
12055 if (do_update)
12056 {
12057 Lisp_Object window;
12058 struct window *w;
12059
12060 window = FRAME_SELECTED_WINDOW (f);
12061 w = XWINDOW (window);
12062
12063 /* If the user has switched buffers or windows, we need to
12064 recompute to reflect the new bindings. But we'll
12065 recompute when update_mode_lines is set too; that means
12066 that people can use force-mode-line-update to request
12067 that the menu bar be recomputed. The adverse effect on
12068 the rest of the redisplay algorithm is about the same as
12069 windows_or_buffers_changed anyway. */
12070 if (windows_or_buffers_changed
12071 || w->update_mode_line
12072 || update_mode_lines
12073 || window_buffer_changed (w))
12074 {
12075 struct buffer *prev = current_buffer;
12076 ptrdiff_t count = SPECPDL_INDEX ();
12077 Lisp_Object frame, new_tool_bar;
12078 int new_n_tool_bar;
12079
12080 /* Set current_buffer to the buffer of the selected
12081 window of the frame, so that we get the right local
12082 keymaps. */
12083 set_buffer_internal_1 (XBUFFER (w->contents));
12084
12085 /* Save match data, if we must. */
12086 if (save_match_data)
12087 record_unwind_save_match_data ();
12088
12089 /* Make sure that we don't accidentally use bogus keymaps. */
12090 if (NILP (Voverriding_local_map_menu_flag))
12091 {
12092 specbind (Qoverriding_terminal_local_map, Qnil);
12093 specbind (Qoverriding_local_map, Qnil);
12094 }
12095
12096 /* We must temporarily set the selected frame to this frame
12097 before calling tool_bar_items, because the calculation of
12098 the tool-bar keymap uses the selected frame (see
12099 `tool-bar-make-keymap' in tool-bar.el). */
12100 eassert (EQ (selected_window,
12101 /* Since we only explicitly preserve selected_frame,
12102 check that selected_window would be redundant. */
12103 XFRAME (selected_frame)->selected_window));
12104 record_unwind_protect (fast_set_selected_frame, selected_frame);
12105 XSETFRAME (frame, f);
12106 fast_set_selected_frame (frame);
12107
12108 /* Build desired tool-bar items from keymaps. */
12109 new_tool_bar
12110 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12111 &new_n_tool_bar);
12112
12113 /* Redisplay the tool-bar if we changed it. */
12114 if (new_n_tool_bar != f->n_tool_bar_items
12115 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12116 {
12117 /* Redisplay that happens asynchronously due to an expose event
12118 may access f->tool_bar_items. Make sure we update both
12119 variables within BLOCK_INPUT so no such event interrupts. */
12120 block_input ();
12121 fset_tool_bar_items (f, new_tool_bar);
12122 f->n_tool_bar_items = new_n_tool_bar;
12123 w->update_mode_line = true;
12124 unblock_input ();
12125 }
12126
12127 unbind_to (count, Qnil);
12128 set_buffer_internal_1 (prev);
12129 }
12130 }
12131 }
12132
12133 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12134
12135 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12136 F's desired tool-bar contents. F->tool_bar_items must have
12137 been set up previously by calling prepare_menu_bars. */
12138
12139 static void
12140 build_desired_tool_bar_string (struct frame *f)
12141 {
12142 int i, size, size_needed;
12143 Lisp_Object image, plist;
12144
12145 image = plist = Qnil;
12146
12147 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12148 Otherwise, make a new string. */
12149
12150 /* The size of the string we might be able to reuse. */
12151 size = (STRINGP (f->desired_tool_bar_string)
12152 ? SCHARS (f->desired_tool_bar_string)
12153 : 0);
12154
12155 /* We need one space in the string for each image. */
12156 size_needed = f->n_tool_bar_items;
12157
12158 /* Reuse f->desired_tool_bar_string, if possible. */
12159 if (size < size_needed || NILP (f->desired_tool_bar_string))
12160 fset_desired_tool_bar_string
12161 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12162 else
12163 {
12164 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12165 Fremove_text_properties (make_number (0), make_number (size),
12166 props, f->desired_tool_bar_string);
12167 }
12168
12169 /* Put a `display' property on the string for the images to display,
12170 put a `menu_item' property on tool-bar items with a value that
12171 is the index of the item in F's tool-bar item vector. */
12172 for (i = 0; i < f->n_tool_bar_items; ++i)
12173 {
12174 #define PROP(IDX) \
12175 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12176
12177 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12178 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12179 int hmargin, vmargin, relief, idx, end;
12180
12181 /* If image is a vector, choose the image according to the
12182 button state. */
12183 image = PROP (TOOL_BAR_ITEM_IMAGES);
12184 if (VECTORP (image))
12185 {
12186 if (enabled_p)
12187 idx = (selected_p
12188 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12189 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12190 else
12191 idx = (selected_p
12192 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12193 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12194
12195 eassert (ASIZE (image) >= idx);
12196 image = AREF (image, idx);
12197 }
12198 else
12199 idx = -1;
12200
12201 /* Ignore invalid image specifications. */
12202 if (!valid_image_p (image))
12203 continue;
12204
12205 /* Display the tool-bar button pressed, or depressed. */
12206 plist = Fcopy_sequence (XCDR (image));
12207
12208 /* Compute margin and relief to draw. */
12209 relief = (tool_bar_button_relief >= 0
12210 ? tool_bar_button_relief
12211 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12212 hmargin = vmargin = relief;
12213
12214 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12215 INT_MAX - max (hmargin, vmargin)))
12216 {
12217 hmargin += XFASTINT (Vtool_bar_button_margin);
12218 vmargin += XFASTINT (Vtool_bar_button_margin);
12219 }
12220 else if (CONSP (Vtool_bar_button_margin))
12221 {
12222 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12223 INT_MAX - hmargin))
12224 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12225
12226 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12227 INT_MAX - vmargin))
12228 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12229 }
12230
12231 if (auto_raise_tool_bar_buttons_p)
12232 {
12233 /* Add a `:relief' property to the image spec if the item is
12234 selected. */
12235 if (selected_p)
12236 {
12237 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12238 hmargin -= relief;
12239 vmargin -= relief;
12240 }
12241 }
12242 else
12243 {
12244 /* If image is selected, display it pressed, i.e. with a
12245 negative relief. If it's not selected, display it with a
12246 raised relief. */
12247 plist = Fplist_put (plist, QCrelief,
12248 (selected_p
12249 ? make_number (-relief)
12250 : make_number (relief)));
12251 hmargin -= relief;
12252 vmargin -= relief;
12253 }
12254
12255 /* Put a margin around the image. */
12256 if (hmargin || vmargin)
12257 {
12258 if (hmargin == vmargin)
12259 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12260 else
12261 plist = Fplist_put (plist, QCmargin,
12262 Fcons (make_number (hmargin),
12263 make_number (vmargin)));
12264 }
12265
12266 /* If button is not enabled, and we don't have special images
12267 for the disabled state, make the image appear disabled by
12268 applying an appropriate algorithm to it. */
12269 if (!enabled_p && idx < 0)
12270 plist = Fplist_put (plist, QCconversion, Qdisabled);
12271
12272 /* Put a `display' text property on the string for the image to
12273 display. Put a `menu-item' property on the string that gives
12274 the start of this item's properties in the tool-bar items
12275 vector. */
12276 image = Fcons (Qimage, plist);
12277 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12278 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12279
12280 /* Let the last image hide all remaining spaces in the tool bar
12281 string. The string can be longer than needed when we reuse a
12282 previous string. */
12283 if (i + 1 == f->n_tool_bar_items)
12284 end = SCHARS (f->desired_tool_bar_string);
12285 else
12286 end = i + 1;
12287 Fadd_text_properties (make_number (i), make_number (end),
12288 props, f->desired_tool_bar_string);
12289 #undef PROP
12290 }
12291 }
12292
12293
12294 /* Display one line of the tool-bar of frame IT->f.
12295
12296 HEIGHT specifies the desired height of the tool-bar line.
12297 If the actual height of the glyph row is less than HEIGHT, the
12298 row's height is increased to HEIGHT, and the icons are centered
12299 vertically in the new height.
12300
12301 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12302 count a final empty row in case the tool-bar width exactly matches
12303 the window width.
12304 */
12305
12306 static void
12307 display_tool_bar_line (struct it *it, int height)
12308 {
12309 struct glyph_row *row = it->glyph_row;
12310 int max_x = it->last_visible_x;
12311 struct glyph *last;
12312
12313 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12314 clear_glyph_row (row);
12315 row->enabled_p = true;
12316 row->y = it->current_y;
12317
12318 /* Note that this isn't made use of if the face hasn't a box,
12319 so there's no need to check the face here. */
12320 it->start_of_box_run_p = true;
12321
12322 while (it->current_x < max_x)
12323 {
12324 int x, n_glyphs_before, i, nglyphs;
12325 struct it it_before;
12326
12327 /* Get the next display element. */
12328 if (!get_next_display_element (it))
12329 {
12330 /* Don't count empty row if we are counting needed tool-bar lines. */
12331 if (height < 0 && !it->hpos)
12332 return;
12333 break;
12334 }
12335
12336 /* Produce glyphs. */
12337 n_glyphs_before = row->used[TEXT_AREA];
12338 it_before = *it;
12339
12340 PRODUCE_GLYPHS (it);
12341
12342 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12343 i = 0;
12344 x = it_before.current_x;
12345 while (i < nglyphs)
12346 {
12347 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12348
12349 if (x + glyph->pixel_width > max_x)
12350 {
12351 /* Glyph doesn't fit on line. Backtrack. */
12352 row->used[TEXT_AREA] = n_glyphs_before;
12353 *it = it_before;
12354 /* If this is the only glyph on this line, it will never fit on the
12355 tool-bar, so skip it. But ensure there is at least one glyph,
12356 so we don't accidentally disable the tool-bar. */
12357 if (n_glyphs_before == 0
12358 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12359 break;
12360 goto out;
12361 }
12362
12363 ++it->hpos;
12364 x += glyph->pixel_width;
12365 ++i;
12366 }
12367
12368 /* Stop at line end. */
12369 if (ITERATOR_AT_END_OF_LINE_P (it))
12370 break;
12371
12372 set_iterator_to_next (it, true);
12373 }
12374
12375 out:;
12376
12377 row->displays_text_p = row->used[TEXT_AREA] != 0;
12378
12379 /* Use default face for the border below the tool bar.
12380
12381 FIXME: When auto-resize-tool-bars is grow-only, there is
12382 no additional border below the possibly empty tool-bar lines.
12383 So to make the extra empty lines look "normal", we have to
12384 use the tool-bar face for the border too. */
12385 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12386 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12387 it->face_id = DEFAULT_FACE_ID;
12388
12389 extend_face_to_end_of_line (it);
12390 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12391 last->right_box_line_p = true;
12392 if (last == row->glyphs[TEXT_AREA])
12393 last->left_box_line_p = true;
12394
12395 /* Make line the desired height and center it vertically. */
12396 if ((height -= it->max_ascent + it->max_descent) > 0)
12397 {
12398 /* Don't add more than one line height. */
12399 height %= FRAME_LINE_HEIGHT (it->f);
12400 it->max_ascent += height / 2;
12401 it->max_descent += (height + 1) / 2;
12402 }
12403
12404 compute_line_metrics (it);
12405
12406 /* If line is empty, make it occupy the rest of the tool-bar. */
12407 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12408 {
12409 row->height = row->phys_height = it->last_visible_y - row->y;
12410 row->visible_height = row->height;
12411 row->ascent = row->phys_ascent = 0;
12412 row->extra_line_spacing = 0;
12413 }
12414
12415 row->full_width_p = true;
12416 row->continued_p = false;
12417 row->truncated_on_left_p = false;
12418 row->truncated_on_right_p = false;
12419
12420 it->current_x = it->hpos = 0;
12421 it->current_y += row->height;
12422 ++it->vpos;
12423 ++it->glyph_row;
12424 }
12425
12426
12427 /* Value is the number of pixels needed to make all tool-bar items of
12428 frame F visible. The actual number of glyph rows needed is
12429 returned in *N_ROWS if non-NULL. */
12430 static int
12431 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12432 {
12433 struct window *w = XWINDOW (f->tool_bar_window);
12434 struct it it;
12435 /* tool_bar_height is called from redisplay_tool_bar after building
12436 the desired matrix, so use (unused) mode-line row as temporary row to
12437 avoid destroying the first tool-bar row. */
12438 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12439
12440 /* Initialize an iterator for iteration over
12441 F->desired_tool_bar_string in the tool-bar window of frame F. */
12442 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12443 temp_row->reversed_p = false;
12444 it.first_visible_x = 0;
12445 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12446 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12447 it.paragraph_embedding = L2R;
12448
12449 while (!ITERATOR_AT_END_P (&it))
12450 {
12451 clear_glyph_row (temp_row);
12452 it.glyph_row = temp_row;
12453 display_tool_bar_line (&it, -1);
12454 }
12455 clear_glyph_row (temp_row);
12456
12457 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12458 if (n_rows)
12459 *n_rows = it.vpos > 0 ? it.vpos : -1;
12460
12461 if (pixelwise)
12462 return it.current_y;
12463 else
12464 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12465 }
12466
12467 #endif /* !USE_GTK && !HAVE_NS */
12468
12469 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12470 0, 2, 0,
12471 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12472 If FRAME is nil or omitted, use the selected frame. Optional argument
12473 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12474 (Lisp_Object frame, Lisp_Object pixelwise)
12475 {
12476 int height = 0;
12477
12478 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12479 struct frame *f = decode_any_frame (frame);
12480
12481 if (WINDOWP (f->tool_bar_window)
12482 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12483 {
12484 update_tool_bar (f, true);
12485 if (f->n_tool_bar_items)
12486 {
12487 build_desired_tool_bar_string (f);
12488 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12489 }
12490 }
12491 #endif
12492
12493 return make_number (height);
12494 }
12495
12496
12497 /* Display the tool-bar of frame F. Value is true if tool-bar's
12498 height should be changed. */
12499 static bool
12500 redisplay_tool_bar (struct frame *f)
12501 {
12502 f->tool_bar_redisplayed = true;
12503 #if defined (USE_GTK) || defined (HAVE_NS)
12504
12505 if (FRAME_EXTERNAL_TOOL_BAR (f))
12506 update_frame_tool_bar (f);
12507 return false;
12508
12509 #else /* !USE_GTK && !HAVE_NS */
12510
12511 struct window *w;
12512 struct it it;
12513 struct glyph_row *row;
12514
12515 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12516 do anything. This means you must start with tool-bar-lines
12517 non-zero to get the auto-sizing effect. Or in other words, you
12518 can turn off tool-bars by specifying tool-bar-lines zero. */
12519 if (!WINDOWP (f->tool_bar_window)
12520 || (w = XWINDOW (f->tool_bar_window),
12521 WINDOW_TOTAL_LINES (w) == 0))
12522 return false;
12523
12524 /* Set up an iterator for the tool-bar window. */
12525 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12526 it.first_visible_x = 0;
12527 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12528 row = it.glyph_row;
12529 row->reversed_p = false;
12530
12531 /* Build a string that represents the contents of the tool-bar. */
12532 build_desired_tool_bar_string (f);
12533 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12534 /* FIXME: This should be controlled by a user option. But it
12535 doesn't make sense to have an R2L tool bar if the menu bar cannot
12536 be drawn also R2L, and making the menu bar R2L is tricky due
12537 toolkit-specific code that implements it. If an R2L tool bar is
12538 ever supported, display_tool_bar_line should also be augmented to
12539 call unproduce_glyphs like display_line and display_string
12540 do. */
12541 it.paragraph_embedding = L2R;
12542
12543 if (f->n_tool_bar_rows == 0)
12544 {
12545 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12546
12547 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12548 {
12549 x_change_tool_bar_height (f, new_height);
12550 frame_default_tool_bar_height = new_height;
12551 /* Always do that now. */
12552 clear_glyph_matrix (w->desired_matrix);
12553 f->fonts_changed = true;
12554 return true;
12555 }
12556 }
12557
12558 /* Display as many lines as needed to display all tool-bar items. */
12559
12560 if (f->n_tool_bar_rows > 0)
12561 {
12562 int border, rows, height, extra;
12563
12564 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12565 border = XINT (Vtool_bar_border);
12566 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12567 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12568 else if (EQ (Vtool_bar_border, Qborder_width))
12569 border = f->border_width;
12570 else
12571 border = 0;
12572 if (border < 0)
12573 border = 0;
12574
12575 rows = f->n_tool_bar_rows;
12576 height = max (1, (it.last_visible_y - border) / rows);
12577 extra = it.last_visible_y - border - height * rows;
12578
12579 while (it.current_y < it.last_visible_y)
12580 {
12581 int h = 0;
12582 if (extra > 0 && rows-- > 0)
12583 {
12584 h = (extra + rows - 1) / rows;
12585 extra -= h;
12586 }
12587 display_tool_bar_line (&it, height + h);
12588 }
12589 }
12590 else
12591 {
12592 while (it.current_y < it.last_visible_y)
12593 display_tool_bar_line (&it, 0);
12594 }
12595
12596 /* It doesn't make much sense to try scrolling in the tool-bar
12597 window, so don't do it. */
12598 w->desired_matrix->no_scrolling_p = true;
12599 w->must_be_updated_p = true;
12600
12601 if (!NILP (Vauto_resize_tool_bars))
12602 {
12603 bool change_height_p = true;
12604
12605 /* If we couldn't display everything, change the tool-bar's
12606 height if there is room for more. */
12607 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12608 change_height_p = true;
12609
12610 /* We subtract 1 because display_tool_bar_line advances the
12611 glyph_row pointer before returning to its caller. We want to
12612 examine the last glyph row produced by
12613 display_tool_bar_line. */
12614 row = it.glyph_row - 1;
12615
12616 /* If there are blank lines at the end, except for a partially
12617 visible blank line at the end that is smaller than
12618 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12619 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12620 && row->height >= FRAME_LINE_HEIGHT (f))
12621 change_height_p = true;
12622
12623 /* If row displays tool-bar items, but is partially visible,
12624 change the tool-bar's height. */
12625 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12626 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12627 change_height_p = true;
12628
12629 /* Resize windows as needed by changing the `tool-bar-lines'
12630 frame parameter. */
12631 if (change_height_p)
12632 {
12633 int nrows;
12634 int new_height = tool_bar_height (f, &nrows, true);
12635
12636 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12637 && !f->minimize_tool_bar_window_p)
12638 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12639 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12640 f->minimize_tool_bar_window_p = false;
12641
12642 if (change_height_p)
12643 {
12644 x_change_tool_bar_height (f, new_height);
12645 frame_default_tool_bar_height = new_height;
12646 clear_glyph_matrix (w->desired_matrix);
12647 f->n_tool_bar_rows = nrows;
12648 f->fonts_changed = true;
12649
12650 return true;
12651 }
12652 }
12653 }
12654
12655 f->minimize_tool_bar_window_p = false;
12656 return false;
12657
12658 #endif /* USE_GTK || HAVE_NS */
12659 }
12660
12661 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12662
12663 /* Get information about the tool-bar item which is displayed in GLYPH
12664 on frame F. Return in *PROP_IDX the index where tool-bar item
12665 properties start in F->tool_bar_items. Value is false if
12666 GLYPH doesn't display a tool-bar item. */
12667
12668 static bool
12669 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12670 {
12671 Lisp_Object prop;
12672 int charpos;
12673
12674 /* This function can be called asynchronously, which means we must
12675 exclude any possibility that Fget_text_property signals an
12676 error. */
12677 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12678 charpos = max (0, charpos);
12679
12680 /* Get the text property `menu-item' at pos. The value of that
12681 property is the start index of this item's properties in
12682 F->tool_bar_items. */
12683 prop = Fget_text_property (make_number (charpos),
12684 Qmenu_item, f->current_tool_bar_string);
12685 if (! INTEGERP (prop))
12686 return false;
12687 *prop_idx = XINT (prop);
12688 return true;
12689 }
12690
12691 \f
12692 /* Get information about the tool-bar item at position X/Y on frame F.
12693 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12694 the current matrix of the tool-bar window of F, or NULL if not
12695 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12696 item in F->tool_bar_items. Value is
12697
12698 -1 if X/Y is not on a tool-bar item
12699 0 if X/Y is on the same item that was highlighted before.
12700 1 otherwise. */
12701
12702 static int
12703 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12704 int *hpos, int *vpos, int *prop_idx)
12705 {
12706 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12707 struct window *w = XWINDOW (f->tool_bar_window);
12708 int area;
12709
12710 /* Find the glyph under X/Y. */
12711 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12712 if (*glyph == NULL)
12713 return -1;
12714
12715 /* Get the start of this tool-bar item's properties in
12716 f->tool_bar_items. */
12717 if (!tool_bar_item_info (f, *glyph, prop_idx))
12718 return -1;
12719
12720 /* Is mouse on the highlighted item? */
12721 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12722 && *vpos >= hlinfo->mouse_face_beg_row
12723 && *vpos <= hlinfo->mouse_face_end_row
12724 && (*vpos > hlinfo->mouse_face_beg_row
12725 || *hpos >= hlinfo->mouse_face_beg_col)
12726 && (*vpos < hlinfo->mouse_face_end_row
12727 || *hpos < hlinfo->mouse_face_end_col
12728 || hlinfo->mouse_face_past_end))
12729 return 0;
12730
12731 return 1;
12732 }
12733
12734
12735 /* EXPORT:
12736 Handle mouse button event on the tool-bar of frame F, at
12737 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12738 false for button release. MODIFIERS is event modifiers for button
12739 release. */
12740
12741 void
12742 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12743 int modifiers)
12744 {
12745 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12746 struct window *w = XWINDOW (f->tool_bar_window);
12747 int hpos, vpos, prop_idx;
12748 struct glyph *glyph;
12749 Lisp_Object enabled_p;
12750 int ts;
12751
12752 /* If not on the highlighted tool-bar item, and mouse-highlight is
12753 non-nil, return. This is so we generate the tool-bar button
12754 click only when the mouse button is released on the same item as
12755 where it was pressed. However, when mouse-highlight is disabled,
12756 generate the click when the button is released regardless of the
12757 highlight, since tool-bar items are not highlighted in that
12758 case. */
12759 frame_to_window_pixel_xy (w, &x, &y);
12760 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12761 if (ts == -1
12762 || (ts != 0 && !NILP (Vmouse_highlight)))
12763 return;
12764
12765 /* When mouse-highlight is off, generate the click for the item
12766 where the button was pressed, disregarding where it was
12767 released. */
12768 if (NILP (Vmouse_highlight) && !down_p)
12769 prop_idx = f->last_tool_bar_item;
12770
12771 /* If item is disabled, do nothing. */
12772 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12773 if (NILP (enabled_p))
12774 return;
12775
12776 if (down_p)
12777 {
12778 /* Show item in pressed state. */
12779 if (!NILP (Vmouse_highlight))
12780 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12781 f->last_tool_bar_item = prop_idx;
12782 }
12783 else
12784 {
12785 Lisp_Object key, frame;
12786 struct input_event event;
12787 EVENT_INIT (event);
12788
12789 /* Show item in released state. */
12790 if (!NILP (Vmouse_highlight))
12791 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12792
12793 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12794
12795 XSETFRAME (frame, f);
12796 event.kind = TOOL_BAR_EVENT;
12797 event.frame_or_window = frame;
12798 event.arg = frame;
12799 kbd_buffer_store_event (&event);
12800
12801 event.kind = TOOL_BAR_EVENT;
12802 event.frame_or_window = frame;
12803 event.arg = key;
12804 event.modifiers = modifiers;
12805 kbd_buffer_store_event (&event);
12806 f->last_tool_bar_item = -1;
12807 }
12808 }
12809
12810
12811 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12812 tool-bar window-relative coordinates X/Y. Called from
12813 note_mouse_highlight. */
12814
12815 static void
12816 note_tool_bar_highlight (struct frame *f, int x, int y)
12817 {
12818 Lisp_Object window = f->tool_bar_window;
12819 struct window *w = XWINDOW (window);
12820 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12821 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12822 int hpos, vpos;
12823 struct glyph *glyph;
12824 struct glyph_row *row;
12825 int i;
12826 Lisp_Object enabled_p;
12827 int prop_idx;
12828 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12829 bool mouse_down_p;
12830 int rc;
12831
12832 /* Function note_mouse_highlight is called with negative X/Y
12833 values when mouse moves outside of the frame. */
12834 if (x <= 0 || y <= 0)
12835 {
12836 clear_mouse_face (hlinfo);
12837 return;
12838 }
12839
12840 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12841 if (rc < 0)
12842 {
12843 /* Not on tool-bar item. */
12844 clear_mouse_face (hlinfo);
12845 return;
12846 }
12847 else if (rc == 0)
12848 /* On same tool-bar item as before. */
12849 goto set_help_echo;
12850
12851 clear_mouse_face (hlinfo);
12852
12853 /* Mouse is down, but on different tool-bar item? */
12854 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12855 && f == dpyinfo->last_mouse_frame);
12856
12857 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12858 return;
12859
12860 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12861
12862 /* If tool-bar item is not enabled, don't highlight it. */
12863 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12864 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12865 {
12866 /* Compute the x-position of the glyph. In front and past the
12867 image is a space. We include this in the highlighted area. */
12868 row = MATRIX_ROW (w->current_matrix, vpos);
12869 for (i = x = 0; i < hpos; ++i)
12870 x += row->glyphs[TEXT_AREA][i].pixel_width;
12871
12872 /* Record this as the current active region. */
12873 hlinfo->mouse_face_beg_col = hpos;
12874 hlinfo->mouse_face_beg_row = vpos;
12875 hlinfo->mouse_face_beg_x = x;
12876 hlinfo->mouse_face_past_end = false;
12877
12878 hlinfo->mouse_face_end_col = hpos + 1;
12879 hlinfo->mouse_face_end_row = vpos;
12880 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12881 hlinfo->mouse_face_window = window;
12882 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12883
12884 /* Display it as active. */
12885 show_mouse_face (hlinfo, draw);
12886 }
12887
12888 set_help_echo:
12889
12890 /* Set help_echo_string to a help string to display for this tool-bar item.
12891 XTread_socket does the rest. */
12892 help_echo_object = help_echo_window = Qnil;
12893 help_echo_pos = -1;
12894 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12895 if (NILP (help_echo_string))
12896 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12897 }
12898
12899 #endif /* !USE_GTK && !HAVE_NS */
12900
12901 #endif /* HAVE_WINDOW_SYSTEM */
12902
12903
12904 \f
12905 /************************************************************************
12906 Horizontal scrolling
12907 ************************************************************************/
12908
12909 /* For all leaf windows in the window tree rooted at WINDOW, set their
12910 hscroll value so that PT is (i) visible in the window, and (ii) so
12911 that it is not within a certain margin at the window's left and
12912 right border. Value is true if any window's hscroll has been
12913 changed. */
12914
12915 static bool
12916 hscroll_window_tree (Lisp_Object window)
12917 {
12918 bool hscrolled_p = false;
12919 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12920 int hscroll_step_abs = 0;
12921 double hscroll_step_rel = 0;
12922
12923 if (hscroll_relative_p)
12924 {
12925 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12926 if (hscroll_step_rel < 0)
12927 {
12928 hscroll_relative_p = false;
12929 hscroll_step_abs = 0;
12930 }
12931 }
12932 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12933 {
12934 hscroll_step_abs = XINT (Vhscroll_step);
12935 if (hscroll_step_abs < 0)
12936 hscroll_step_abs = 0;
12937 }
12938 else
12939 hscroll_step_abs = 0;
12940
12941 while (WINDOWP (window))
12942 {
12943 struct window *w = XWINDOW (window);
12944
12945 if (WINDOWP (w->contents))
12946 hscrolled_p |= hscroll_window_tree (w->contents);
12947 else if (w->cursor.vpos >= 0)
12948 {
12949 int h_margin;
12950 int text_area_width;
12951 struct glyph_row *cursor_row;
12952 struct glyph_row *bottom_row;
12953
12954 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12955 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12956 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12957 else
12958 cursor_row = bottom_row - 1;
12959
12960 if (!cursor_row->enabled_p)
12961 {
12962 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12963 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12964 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12965 else
12966 cursor_row = bottom_row - 1;
12967 }
12968 bool row_r2l_p = cursor_row->reversed_p;
12969
12970 text_area_width = window_box_width (w, TEXT_AREA);
12971
12972 /* Scroll when cursor is inside this scroll margin. */
12973 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12974
12975 /* If the position of this window's point has explicitly
12976 changed, no more suspend auto hscrolling. */
12977 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12978 w->suspend_auto_hscroll = false;
12979
12980 /* Remember window point. */
12981 Fset_marker (w->old_pointm,
12982 ((w == XWINDOW (selected_window))
12983 ? make_number (BUF_PT (XBUFFER (w->contents)))
12984 : Fmarker_position (w->pointm)),
12985 w->contents);
12986
12987 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12988 && !w->suspend_auto_hscroll
12989 /* In some pathological cases, like restoring a window
12990 configuration into a frame that is much smaller than
12991 the one from which the configuration was saved, we
12992 get glyph rows whose start and end have zero buffer
12993 positions, which we cannot handle below. Just skip
12994 such windows. */
12995 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12996 /* For left-to-right rows, hscroll when cursor is either
12997 (i) inside the right hscroll margin, or (ii) if it is
12998 inside the left margin and the window is already
12999 hscrolled. */
13000 && ((!row_r2l_p
13001 && ((w->hscroll && w->cursor.x <= h_margin)
13002 || (cursor_row->enabled_p
13003 && cursor_row->truncated_on_right_p
13004 && (w->cursor.x >= text_area_width - h_margin))))
13005 /* For right-to-left rows, the logic is similar,
13006 except that rules for scrolling to left and right
13007 are reversed. E.g., if cursor.x <= h_margin, we
13008 need to hscroll "to the right" unconditionally,
13009 and that will scroll the screen to the left so as
13010 to reveal the next portion of the row. */
13011 || (row_r2l_p
13012 && ((cursor_row->enabled_p
13013 /* FIXME: It is confusing to set the
13014 truncated_on_right_p flag when R2L rows
13015 are actually truncated on the left. */
13016 && cursor_row->truncated_on_right_p
13017 && w->cursor.x <= h_margin)
13018 || (w->hscroll
13019 && (w->cursor.x >= text_area_width - h_margin))))))
13020 {
13021 struct it it;
13022 ptrdiff_t hscroll;
13023 struct buffer *saved_current_buffer;
13024 ptrdiff_t pt;
13025 int wanted_x;
13026
13027 /* Find point in a display of infinite width. */
13028 saved_current_buffer = current_buffer;
13029 current_buffer = XBUFFER (w->contents);
13030
13031 if (w == XWINDOW (selected_window))
13032 pt = PT;
13033 else
13034 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
13035
13036 /* Move iterator to pt starting at cursor_row->start in
13037 a line with infinite width. */
13038 init_to_row_start (&it, w, cursor_row);
13039 it.last_visible_x = INFINITY;
13040 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
13041 current_buffer = saved_current_buffer;
13042
13043 /* Position cursor in window. */
13044 if (!hscroll_relative_p && hscroll_step_abs == 0)
13045 hscroll = max (0, (it.current_x
13046 - (ITERATOR_AT_END_OF_LINE_P (&it)
13047 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13048 : (text_area_width / 2))))
13049 / FRAME_COLUMN_WIDTH (it.f);
13050 else if ((!row_r2l_p
13051 && w->cursor.x >= text_area_width - h_margin)
13052 || (row_r2l_p && w->cursor.x <= h_margin))
13053 {
13054 if (hscroll_relative_p)
13055 wanted_x = text_area_width * (1 - hscroll_step_rel)
13056 - h_margin;
13057 else
13058 wanted_x = text_area_width
13059 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13060 - h_margin;
13061 hscroll
13062 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13063 }
13064 else
13065 {
13066 if (hscroll_relative_p)
13067 wanted_x = text_area_width * hscroll_step_rel
13068 + h_margin;
13069 else
13070 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13071 + h_margin;
13072 hscroll
13073 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13074 }
13075 hscroll = max (hscroll, w->min_hscroll);
13076
13077 /* Don't prevent redisplay optimizations if hscroll
13078 hasn't changed, as it will unnecessarily slow down
13079 redisplay. */
13080 if (w->hscroll != hscroll)
13081 {
13082 struct buffer *b = XBUFFER (w->contents);
13083 b->prevent_redisplay_optimizations_p = true;
13084 w->hscroll = hscroll;
13085 hscrolled_p = true;
13086 }
13087 }
13088 }
13089
13090 window = w->next;
13091 }
13092
13093 /* Value is true if hscroll of any leaf window has been changed. */
13094 return hscrolled_p;
13095 }
13096
13097
13098 /* Set hscroll so that cursor is visible and not inside horizontal
13099 scroll margins for all windows in the tree rooted at WINDOW. See
13100 also hscroll_window_tree above. Value is true if any window's
13101 hscroll has been changed. If it has, desired matrices on the frame
13102 of WINDOW are cleared. */
13103
13104 static bool
13105 hscroll_windows (Lisp_Object window)
13106 {
13107 bool hscrolled_p = hscroll_window_tree (window);
13108 if (hscrolled_p)
13109 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13110 return hscrolled_p;
13111 }
13112
13113
13114 \f
13115 /************************************************************************
13116 Redisplay
13117 ************************************************************************/
13118
13119 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13120 This is sometimes handy to have in a debugger session. */
13121
13122 #ifdef GLYPH_DEBUG
13123
13124 /* First and last unchanged row for try_window_id. */
13125
13126 static int debug_first_unchanged_at_end_vpos;
13127 static int debug_last_unchanged_at_beg_vpos;
13128
13129 /* Delta vpos and y. */
13130
13131 static int debug_dvpos, debug_dy;
13132
13133 /* Delta in characters and bytes for try_window_id. */
13134
13135 static ptrdiff_t debug_delta, debug_delta_bytes;
13136
13137 /* Values of window_end_pos and window_end_vpos at the end of
13138 try_window_id. */
13139
13140 static ptrdiff_t debug_end_vpos;
13141
13142 /* Append a string to W->desired_matrix->method. FMT is a printf
13143 format string. If trace_redisplay_p is true also printf the
13144 resulting string to stderr. */
13145
13146 static void debug_method_add (struct window *, char const *, ...)
13147 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13148
13149 static void
13150 debug_method_add (struct window *w, char const *fmt, ...)
13151 {
13152 void *ptr = w;
13153 char *method = w->desired_matrix->method;
13154 int len = strlen (method);
13155 int size = sizeof w->desired_matrix->method;
13156 int remaining = size - len - 1;
13157 va_list ap;
13158
13159 if (len && remaining)
13160 {
13161 method[len] = '|';
13162 --remaining, ++len;
13163 }
13164
13165 va_start (ap, fmt);
13166 vsnprintf (method + len, remaining + 1, fmt, ap);
13167 va_end (ap);
13168
13169 if (trace_redisplay_p)
13170 fprintf (stderr, "%p (%s): %s\n",
13171 ptr,
13172 ((BUFFERP (w->contents)
13173 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13174 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13175 : "no buffer"),
13176 method + len);
13177 }
13178
13179 #endif /* GLYPH_DEBUG */
13180
13181
13182 /* Value is true if all changes in window W, which displays
13183 current_buffer, are in the text between START and END. START is a
13184 buffer position, END is given as a distance from Z. Used in
13185 redisplay_internal for display optimization. */
13186
13187 static bool
13188 text_outside_line_unchanged_p (struct window *w,
13189 ptrdiff_t start, ptrdiff_t end)
13190 {
13191 bool unchanged_p = true;
13192
13193 /* If text or overlays have changed, see where. */
13194 if (window_outdated (w))
13195 {
13196 /* Gap in the line? */
13197 if (GPT < start || Z - GPT < end)
13198 unchanged_p = false;
13199
13200 /* Changes start in front of the line, or end after it? */
13201 if (unchanged_p
13202 && (BEG_UNCHANGED < start - 1
13203 || END_UNCHANGED < end))
13204 unchanged_p = false;
13205
13206 /* If selective display, can't optimize if changes start at the
13207 beginning of the line. */
13208 if (unchanged_p
13209 && INTEGERP (BVAR (current_buffer, selective_display))
13210 && XINT (BVAR (current_buffer, selective_display)) > 0
13211 && (BEG_UNCHANGED < start || GPT <= start))
13212 unchanged_p = false;
13213
13214 /* If there are overlays at the start or end of the line, these
13215 may have overlay strings with newlines in them. A change at
13216 START, for instance, may actually concern the display of such
13217 overlay strings as well, and they are displayed on different
13218 lines. So, quickly rule out this case. (For the future, it
13219 might be desirable to implement something more telling than
13220 just BEG/END_UNCHANGED.) */
13221 if (unchanged_p)
13222 {
13223 if (BEG + BEG_UNCHANGED == start
13224 && overlay_touches_p (start))
13225 unchanged_p = false;
13226 if (END_UNCHANGED == end
13227 && overlay_touches_p (Z - end))
13228 unchanged_p = false;
13229 }
13230
13231 /* Under bidi reordering, adding or deleting a character in the
13232 beginning of a paragraph, before the first strong directional
13233 character, can change the base direction of the paragraph (unless
13234 the buffer specifies a fixed paragraph direction), which will
13235 require redisplaying the whole paragraph. It might be worthwhile
13236 to find the paragraph limits and widen the range of redisplayed
13237 lines to that, but for now just give up this optimization. */
13238 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13239 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13240 unchanged_p = false;
13241 }
13242
13243 return unchanged_p;
13244 }
13245
13246
13247 /* Do a frame update, taking possible shortcuts into account. This is
13248 the main external entry point for redisplay.
13249
13250 If the last redisplay displayed an echo area message and that message
13251 is no longer requested, we clear the echo area or bring back the
13252 mini-buffer if that is in use. */
13253
13254 void
13255 redisplay (void)
13256 {
13257 redisplay_internal ();
13258 }
13259
13260
13261 static Lisp_Object
13262 overlay_arrow_string_or_property (Lisp_Object var)
13263 {
13264 Lisp_Object val;
13265
13266 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13267 return val;
13268
13269 return Voverlay_arrow_string;
13270 }
13271
13272 /* Return true if there are any overlay-arrows in current_buffer. */
13273 static bool
13274 overlay_arrow_in_current_buffer_p (void)
13275 {
13276 Lisp_Object vlist;
13277
13278 for (vlist = Voverlay_arrow_variable_list;
13279 CONSP (vlist);
13280 vlist = XCDR (vlist))
13281 {
13282 Lisp_Object var = XCAR (vlist);
13283 Lisp_Object val;
13284
13285 if (!SYMBOLP (var))
13286 continue;
13287 val = find_symbol_value (var);
13288 if (MARKERP (val)
13289 && current_buffer == XMARKER (val)->buffer)
13290 return true;
13291 }
13292 return false;
13293 }
13294
13295
13296 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13297 has changed. */
13298
13299 static bool
13300 overlay_arrows_changed_p (void)
13301 {
13302 Lisp_Object vlist;
13303
13304 for (vlist = Voverlay_arrow_variable_list;
13305 CONSP (vlist);
13306 vlist = XCDR (vlist))
13307 {
13308 Lisp_Object var = XCAR (vlist);
13309 Lisp_Object val, pstr;
13310
13311 if (!SYMBOLP (var))
13312 continue;
13313 val = find_symbol_value (var);
13314 if (!MARKERP (val))
13315 continue;
13316 if (! EQ (COERCE_MARKER (val),
13317 Fget (var, Qlast_arrow_position))
13318 || ! (pstr = overlay_arrow_string_or_property (var),
13319 EQ (pstr, Fget (var, Qlast_arrow_string))))
13320 return true;
13321 }
13322 return false;
13323 }
13324
13325 /* Mark overlay arrows to be updated on next redisplay. */
13326
13327 static void
13328 update_overlay_arrows (int up_to_date)
13329 {
13330 Lisp_Object vlist;
13331
13332 for (vlist = Voverlay_arrow_variable_list;
13333 CONSP (vlist);
13334 vlist = XCDR (vlist))
13335 {
13336 Lisp_Object var = XCAR (vlist);
13337
13338 if (!SYMBOLP (var))
13339 continue;
13340
13341 if (up_to_date > 0)
13342 {
13343 Lisp_Object val = find_symbol_value (var);
13344 Fput (var, Qlast_arrow_position,
13345 COERCE_MARKER (val));
13346 Fput (var, Qlast_arrow_string,
13347 overlay_arrow_string_or_property (var));
13348 }
13349 else if (up_to_date < 0
13350 || !NILP (Fget (var, Qlast_arrow_position)))
13351 {
13352 Fput (var, Qlast_arrow_position, Qt);
13353 Fput (var, Qlast_arrow_string, Qt);
13354 }
13355 }
13356 }
13357
13358
13359 /* Return overlay arrow string to display at row.
13360 Return integer (bitmap number) for arrow bitmap in left fringe.
13361 Return nil if no overlay arrow. */
13362
13363 static Lisp_Object
13364 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13365 {
13366 Lisp_Object vlist;
13367
13368 for (vlist = Voverlay_arrow_variable_list;
13369 CONSP (vlist);
13370 vlist = XCDR (vlist))
13371 {
13372 Lisp_Object var = XCAR (vlist);
13373 Lisp_Object val;
13374
13375 if (!SYMBOLP (var))
13376 continue;
13377
13378 val = find_symbol_value (var);
13379
13380 if (MARKERP (val)
13381 && current_buffer == XMARKER (val)->buffer
13382 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13383 {
13384 if (FRAME_WINDOW_P (it->f)
13385 /* FIXME: if ROW->reversed_p is set, this should test
13386 the right fringe, not the left one. */
13387 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13388 {
13389 #ifdef HAVE_WINDOW_SYSTEM
13390 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13391 {
13392 int fringe_bitmap = lookup_fringe_bitmap (val);
13393 if (fringe_bitmap != 0)
13394 return make_number (fringe_bitmap);
13395 }
13396 #endif
13397 return make_number (-1); /* Use default arrow bitmap. */
13398 }
13399 return overlay_arrow_string_or_property (var);
13400 }
13401 }
13402
13403 return Qnil;
13404 }
13405
13406 /* Return true if point moved out of or into a composition. Otherwise
13407 return false. PREV_BUF and PREV_PT are the last point buffer and
13408 position. BUF and PT are the current point buffer and position. */
13409
13410 static bool
13411 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13412 struct buffer *buf, ptrdiff_t pt)
13413 {
13414 ptrdiff_t start, end;
13415 Lisp_Object prop;
13416 Lisp_Object buffer;
13417
13418 XSETBUFFER (buffer, buf);
13419 /* Check a composition at the last point if point moved within the
13420 same buffer. */
13421 if (prev_buf == buf)
13422 {
13423 if (prev_pt == pt)
13424 /* Point didn't move. */
13425 return false;
13426
13427 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13428 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13429 && composition_valid_p (start, end, prop)
13430 && start < prev_pt && end > prev_pt)
13431 /* The last point was within the composition. Return true iff
13432 point moved out of the composition. */
13433 return (pt <= start || pt >= end);
13434 }
13435
13436 /* Check a composition at the current point. */
13437 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13438 && find_composition (pt, -1, &start, &end, &prop, buffer)
13439 && composition_valid_p (start, end, prop)
13440 && start < pt && end > pt);
13441 }
13442
13443 /* Reconsider the clip changes of buffer which is displayed in W. */
13444
13445 static void
13446 reconsider_clip_changes (struct window *w)
13447 {
13448 struct buffer *b = XBUFFER (w->contents);
13449
13450 if (b->clip_changed
13451 && w->window_end_valid
13452 && w->current_matrix->buffer == b
13453 && w->current_matrix->zv == BUF_ZV (b)
13454 && w->current_matrix->begv == BUF_BEGV (b))
13455 b->clip_changed = false;
13456
13457 /* If display wasn't paused, and W is not a tool bar window, see if
13458 point has been moved into or out of a composition. In that case,
13459 set b->clip_changed to force updating the screen. If
13460 b->clip_changed has already been set, skip this check. */
13461 if (!b->clip_changed && w->window_end_valid)
13462 {
13463 ptrdiff_t pt = (w == XWINDOW (selected_window)
13464 ? PT : marker_position (w->pointm));
13465
13466 if ((w->current_matrix->buffer != b || pt != w->last_point)
13467 && check_point_in_composition (w->current_matrix->buffer,
13468 w->last_point, b, pt))
13469 b->clip_changed = true;
13470 }
13471 }
13472
13473 static void
13474 propagate_buffer_redisplay (void)
13475 { /* Resetting b->text->redisplay is problematic!
13476 We can't just reset it in the case that some window that displays
13477 it has not been redisplayed; and such a window can stay
13478 unredisplayed for a long time if it's currently invisible.
13479 But we do want to reset it at the end of redisplay otherwise
13480 its displayed windows will keep being redisplayed over and over
13481 again.
13482 So we copy all b->text->redisplay flags up to their windows here,
13483 such that mark_window_display_accurate can safely reset
13484 b->text->redisplay. */
13485 Lisp_Object ws = window_list ();
13486 for (; CONSP (ws); ws = XCDR (ws))
13487 {
13488 struct window *thisw = XWINDOW (XCAR (ws));
13489 struct buffer *thisb = XBUFFER (thisw->contents);
13490 if (thisb->text->redisplay)
13491 thisw->redisplay = true;
13492 }
13493 }
13494
13495 #define STOP_POLLING \
13496 do { if (! polling_stopped_here) stop_polling (); \
13497 polling_stopped_here = true; } while (false)
13498
13499 #define RESUME_POLLING \
13500 do { if (polling_stopped_here) start_polling (); \
13501 polling_stopped_here = false; } while (false)
13502
13503
13504 /* Perhaps in the future avoid recentering windows if it
13505 is not necessary; currently that causes some problems. */
13506
13507 static void
13508 redisplay_internal (void)
13509 {
13510 struct window *w = XWINDOW (selected_window);
13511 struct window *sw;
13512 struct frame *fr;
13513 bool pending;
13514 bool must_finish = false, match_p;
13515 struct text_pos tlbufpos, tlendpos;
13516 int number_of_visible_frames;
13517 ptrdiff_t count;
13518 struct frame *sf;
13519 bool polling_stopped_here = false;
13520 Lisp_Object tail, frame;
13521
13522 /* True means redisplay has to consider all windows on all
13523 frames. False, only selected_window is considered. */
13524 bool consider_all_windows_p;
13525
13526 /* True means redisplay has to redisplay the miniwindow. */
13527 bool update_miniwindow_p = false;
13528
13529 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13530
13531 /* No redisplay if running in batch mode or frame is not yet fully
13532 initialized, or redisplay is explicitly turned off by setting
13533 Vinhibit_redisplay. */
13534 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13535 || !NILP (Vinhibit_redisplay))
13536 return;
13537
13538 /* Don't examine these until after testing Vinhibit_redisplay.
13539 When Emacs is shutting down, perhaps because its connection to
13540 X has dropped, we should not look at them at all. */
13541 fr = XFRAME (w->frame);
13542 sf = SELECTED_FRAME ();
13543
13544 if (!fr->glyphs_initialized_p)
13545 return;
13546
13547 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13548 if (popup_activated ())
13549 return;
13550 #endif
13551
13552 /* I don't think this happens but let's be paranoid. */
13553 if (redisplaying_p)
13554 return;
13555
13556 /* Record a function that clears redisplaying_p
13557 when we leave this function. */
13558 count = SPECPDL_INDEX ();
13559 record_unwind_protect_void (unwind_redisplay);
13560 redisplaying_p = true;
13561 specbind (Qinhibit_free_realized_faces, Qnil);
13562
13563 /* Record this function, so it appears on the profiler's backtraces. */
13564 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13565
13566 FOR_EACH_FRAME (tail, frame)
13567 XFRAME (frame)->already_hscrolled_p = false;
13568
13569 retry:
13570 /* Remember the currently selected window. */
13571 sw = w;
13572
13573 pending = false;
13574 forget_escape_and_glyphless_faces ();
13575
13576 inhibit_free_realized_faces = false;
13577
13578 /* If face_change, init_iterator will free all realized faces, which
13579 includes the faces referenced from current matrices. So, we
13580 can't reuse current matrices in this case. */
13581 if (face_change)
13582 windows_or_buffers_changed = 47;
13583
13584 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13585 && FRAME_TTY (sf)->previous_frame != sf)
13586 {
13587 /* Since frames on a single ASCII terminal share the same
13588 display area, displaying a different frame means redisplay
13589 the whole thing. */
13590 SET_FRAME_GARBAGED (sf);
13591 #ifndef DOS_NT
13592 set_tty_color_mode (FRAME_TTY (sf), sf);
13593 #endif
13594 FRAME_TTY (sf)->previous_frame = sf;
13595 }
13596
13597 /* Set the visible flags for all frames. Do this before checking for
13598 resized or garbaged frames; they want to know if their frames are
13599 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13600 number_of_visible_frames = 0;
13601
13602 FOR_EACH_FRAME (tail, frame)
13603 {
13604 struct frame *f = XFRAME (frame);
13605
13606 if (FRAME_VISIBLE_P (f))
13607 {
13608 ++number_of_visible_frames;
13609 /* Adjust matrices for visible frames only. */
13610 if (f->fonts_changed)
13611 {
13612 adjust_frame_glyphs (f);
13613 /* Disable all redisplay optimizations for this frame.
13614 This is because adjust_frame_glyphs resets the
13615 enabled_p flag for all glyph rows of all windows, so
13616 many optimizations will fail anyway, and some might
13617 fail to test that flag and do bogus things as
13618 result. */
13619 SET_FRAME_GARBAGED (f);
13620 f->fonts_changed = false;
13621 }
13622 /* If cursor type has been changed on the frame
13623 other than selected, consider all frames. */
13624 if (f != sf && f->cursor_type_changed)
13625 fset_redisplay (f);
13626 }
13627 clear_desired_matrices (f);
13628 }
13629
13630 /* Notice any pending interrupt request to change frame size. */
13631 do_pending_window_change (true);
13632
13633 /* do_pending_window_change could change the selected_window due to
13634 frame resizing which makes the selected window too small. */
13635 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13636 sw = w;
13637
13638 /* Clear frames marked as garbaged. */
13639 clear_garbaged_frames ();
13640
13641 /* Build menubar and tool-bar items. */
13642 if (NILP (Vmemory_full))
13643 prepare_menu_bars ();
13644
13645 reconsider_clip_changes (w);
13646
13647 /* In most cases selected window displays current buffer. */
13648 match_p = XBUFFER (w->contents) == current_buffer;
13649 if (match_p)
13650 {
13651 /* Detect case that we need to write or remove a star in the mode line. */
13652 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13653 w->update_mode_line = true;
13654
13655 if (mode_line_update_needed (w))
13656 w->update_mode_line = true;
13657
13658 /* If reconsider_clip_changes above decided that the narrowing
13659 in the current buffer changed, make sure all other windows
13660 showing that buffer will be redisplayed. */
13661 if (current_buffer->clip_changed)
13662 bset_update_mode_line (current_buffer);
13663 }
13664
13665 /* Normally the message* functions will have already displayed and
13666 updated the echo area, but the frame may have been trashed, or
13667 the update may have been preempted, so display the echo area
13668 again here. Checking message_cleared_p captures the case that
13669 the echo area should be cleared. */
13670 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13671 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13672 || (message_cleared_p
13673 && minibuf_level == 0
13674 /* If the mini-window is currently selected, this means the
13675 echo-area doesn't show through. */
13676 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13677 {
13678 echo_area_display (false);
13679
13680 /* If echo_area_display resizes the mini-window, the redisplay and
13681 window_sizes_changed flags of the selected frame are set, but
13682 it's too late for the hooks in window-size-change-functions,
13683 which have been examined already in prepare_menu_bars. So in
13684 that case we call the hooks here only for the selected frame. */
13685 if (sf->redisplay)
13686 {
13687 ptrdiff_t count1 = SPECPDL_INDEX ();
13688
13689 record_unwind_save_match_data ();
13690 run_window_size_change_functions (selected_frame);
13691 unbind_to (count1, Qnil);
13692 }
13693
13694 if (message_cleared_p)
13695 update_miniwindow_p = true;
13696
13697 must_finish = true;
13698
13699 /* If we don't display the current message, don't clear the
13700 message_cleared_p flag, because, if we did, we wouldn't clear
13701 the echo area in the next redisplay which doesn't preserve
13702 the echo area. */
13703 if (!display_last_displayed_message_p)
13704 message_cleared_p = false;
13705 }
13706 else if (EQ (selected_window, minibuf_window)
13707 && (current_buffer->clip_changed || window_outdated (w))
13708 && resize_mini_window (w, false))
13709 {
13710 if (sf->redisplay)
13711 {
13712 ptrdiff_t count1 = SPECPDL_INDEX ();
13713
13714 record_unwind_save_match_data ();
13715 run_window_size_change_functions (selected_frame);
13716 unbind_to (count1, Qnil);
13717 }
13718
13719 /* Resized active mini-window to fit the size of what it is
13720 showing if its contents might have changed. */
13721 must_finish = true;
13722
13723 /* If window configuration was changed, frames may have been
13724 marked garbaged. Clear them or we will experience
13725 surprises wrt scrolling. */
13726 clear_garbaged_frames ();
13727 }
13728
13729 if (windows_or_buffers_changed && !update_mode_lines)
13730 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13731 only the windows's contents needs to be refreshed, or whether the
13732 mode-lines also need a refresh. */
13733 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13734 ? REDISPLAY_SOME : 32);
13735
13736 /* If specs for an arrow have changed, do thorough redisplay
13737 to ensure we remove any arrow that should no longer exist. */
13738 if (overlay_arrows_changed_p ())
13739 /* Apparently, this is the only case where we update other windows,
13740 without updating other mode-lines. */
13741 windows_or_buffers_changed = 49;
13742
13743 consider_all_windows_p = (update_mode_lines
13744 || windows_or_buffers_changed);
13745
13746 #define AINC(a,i) \
13747 { \
13748 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13749 if (INTEGERP (entry)) \
13750 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13751 }
13752
13753 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13754 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13755
13756 /* Optimize the case that only the line containing the cursor in the
13757 selected window has changed. Variables starting with this_ are
13758 set in display_line and record information about the line
13759 containing the cursor. */
13760 tlbufpos = this_line_start_pos;
13761 tlendpos = this_line_end_pos;
13762 if (!consider_all_windows_p
13763 && CHARPOS (tlbufpos) > 0
13764 && !w->update_mode_line
13765 && !current_buffer->clip_changed
13766 && !current_buffer->prevent_redisplay_optimizations_p
13767 && FRAME_VISIBLE_P (XFRAME (w->frame))
13768 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13769 && !XFRAME (w->frame)->cursor_type_changed
13770 && !XFRAME (w->frame)->face_change
13771 /* Make sure recorded data applies to current buffer, etc. */
13772 && this_line_buffer == current_buffer
13773 && match_p
13774 && !w->force_start
13775 && !w->optional_new_start
13776 /* Point must be on the line that we have info recorded about. */
13777 && PT >= CHARPOS (tlbufpos)
13778 && PT <= Z - CHARPOS (tlendpos)
13779 /* All text outside that line, including its final newline,
13780 must be unchanged. */
13781 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13782 CHARPOS (tlendpos)))
13783 {
13784 if (CHARPOS (tlbufpos) > BEGV
13785 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13786 && (CHARPOS (tlbufpos) == ZV
13787 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13788 /* Former continuation line has disappeared by becoming empty. */
13789 goto cancel;
13790 else if (window_outdated (w) || MINI_WINDOW_P (w))
13791 {
13792 /* We have to handle the case of continuation around a
13793 wide-column character (see the comment in indent.c around
13794 line 1340).
13795
13796 For instance, in the following case:
13797
13798 -------- Insert --------
13799 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13800 J_I_ ==> J_I_ `^^' are cursors.
13801 ^^ ^^
13802 -------- --------
13803
13804 As we have to redraw the line above, we cannot use this
13805 optimization. */
13806
13807 struct it it;
13808 int line_height_before = this_line_pixel_height;
13809
13810 /* Note that start_display will handle the case that the
13811 line starting at tlbufpos is a continuation line. */
13812 start_display (&it, w, tlbufpos);
13813
13814 /* Implementation note: It this still necessary? */
13815 if (it.current_x != this_line_start_x)
13816 goto cancel;
13817
13818 TRACE ((stderr, "trying display optimization 1\n"));
13819 w->cursor.vpos = -1;
13820 overlay_arrow_seen = false;
13821 it.vpos = this_line_vpos;
13822 it.current_y = this_line_y;
13823 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13824 display_line (&it);
13825
13826 /* If line contains point, is not continued,
13827 and ends at same distance from eob as before, we win. */
13828 if (w->cursor.vpos >= 0
13829 /* Line is not continued, otherwise this_line_start_pos
13830 would have been set to 0 in display_line. */
13831 && CHARPOS (this_line_start_pos)
13832 /* Line ends as before. */
13833 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13834 /* Line has same height as before. Otherwise other lines
13835 would have to be shifted up or down. */
13836 && this_line_pixel_height == line_height_before)
13837 {
13838 /* If this is not the window's last line, we must adjust
13839 the charstarts of the lines below. */
13840 if (it.current_y < it.last_visible_y)
13841 {
13842 struct glyph_row *row
13843 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13844 ptrdiff_t delta, delta_bytes;
13845
13846 /* We used to distinguish between two cases here,
13847 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13848 when the line ends in a newline or the end of the
13849 buffer's accessible portion. But both cases did
13850 the same, so they were collapsed. */
13851 delta = (Z
13852 - CHARPOS (tlendpos)
13853 - MATRIX_ROW_START_CHARPOS (row));
13854 delta_bytes = (Z_BYTE
13855 - BYTEPOS (tlendpos)
13856 - MATRIX_ROW_START_BYTEPOS (row));
13857
13858 increment_matrix_positions (w->current_matrix,
13859 this_line_vpos + 1,
13860 w->current_matrix->nrows,
13861 delta, delta_bytes);
13862 }
13863
13864 /* If this row displays text now but previously didn't,
13865 or vice versa, w->window_end_vpos may have to be
13866 adjusted. */
13867 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13868 {
13869 if (w->window_end_vpos < this_line_vpos)
13870 w->window_end_vpos = this_line_vpos;
13871 }
13872 else if (w->window_end_vpos == this_line_vpos
13873 && this_line_vpos > 0)
13874 w->window_end_vpos = this_line_vpos - 1;
13875 w->window_end_valid = false;
13876
13877 /* Update hint: No need to try to scroll in update_window. */
13878 w->desired_matrix->no_scrolling_p = true;
13879
13880 #ifdef GLYPH_DEBUG
13881 *w->desired_matrix->method = 0;
13882 debug_method_add (w, "optimization 1");
13883 #endif
13884 #ifdef HAVE_WINDOW_SYSTEM
13885 update_window_fringes (w, false);
13886 #endif
13887 goto update;
13888 }
13889 else
13890 goto cancel;
13891 }
13892 else if (/* Cursor position hasn't changed. */
13893 PT == w->last_point
13894 /* Make sure the cursor was last displayed
13895 in this window. Otherwise we have to reposition it. */
13896
13897 /* PXW: Must be converted to pixels, probably. */
13898 && 0 <= w->cursor.vpos
13899 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13900 {
13901 if (!must_finish)
13902 {
13903 do_pending_window_change (true);
13904 /* If selected_window changed, redisplay again. */
13905 if (WINDOWP (selected_window)
13906 && (w = XWINDOW (selected_window)) != sw)
13907 goto retry;
13908
13909 /* We used to always goto end_of_redisplay here, but this
13910 isn't enough if we have a blinking cursor. */
13911 if (w->cursor_off_p == w->last_cursor_off_p)
13912 goto end_of_redisplay;
13913 }
13914 goto update;
13915 }
13916 /* If highlighting the region, or if the cursor is in the echo area,
13917 then we can't just move the cursor. */
13918 else if (NILP (Vshow_trailing_whitespace)
13919 && !cursor_in_echo_area)
13920 {
13921 struct it it;
13922 struct glyph_row *row;
13923
13924 /* Skip from tlbufpos to PT and see where it is. Note that
13925 PT may be in invisible text. If so, we will end at the
13926 next visible position. */
13927 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13928 NULL, DEFAULT_FACE_ID);
13929 it.current_x = this_line_start_x;
13930 it.current_y = this_line_y;
13931 it.vpos = this_line_vpos;
13932
13933 /* The call to move_it_to stops in front of PT, but
13934 moves over before-strings. */
13935 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13936
13937 if (it.vpos == this_line_vpos
13938 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13939 row->enabled_p))
13940 {
13941 eassert (this_line_vpos == it.vpos);
13942 eassert (this_line_y == it.current_y);
13943 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13944 if (cursor_row_fully_visible_p (w, false, true))
13945 {
13946 #ifdef GLYPH_DEBUG
13947 *w->desired_matrix->method = 0;
13948 debug_method_add (w, "optimization 3");
13949 #endif
13950 goto update;
13951 }
13952 else
13953 goto cancel;
13954 }
13955 else
13956 goto cancel;
13957 }
13958
13959 cancel:
13960 /* Text changed drastically or point moved off of line. */
13961 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13962 }
13963
13964 CHARPOS (this_line_start_pos) = 0;
13965 ++clear_face_cache_count;
13966 #ifdef HAVE_WINDOW_SYSTEM
13967 ++clear_image_cache_count;
13968 #endif
13969
13970 /* Build desired matrices, and update the display. If
13971 consider_all_windows_p, do it for all windows on all frames that
13972 require redisplay, as specified by their 'redisplay' flag.
13973 Otherwise do it for selected_window, only. */
13974
13975 if (consider_all_windows_p)
13976 {
13977 FOR_EACH_FRAME (tail, frame)
13978 XFRAME (frame)->updated_p = false;
13979
13980 propagate_buffer_redisplay ();
13981
13982 FOR_EACH_FRAME (tail, frame)
13983 {
13984 struct frame *f = XFRAME (frame);
13985
13986 /* We don't have to do anything for unselected terminal
13987 frames. */
13988 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13989 && !EQ (FRAME_TTY (f)->top_frame, frame))
13990 continue;
13991
13992 retry_frame:
13993 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13994 {
13995 bool gcscrollbars
13996 /* Only GC scrollbars when we redisplay the whole frame. */
13997 = f->redisplay || !REDISPLAY_SOME_P ();
13998 bool f_redisplay_flag = f->redisplay;
13999 /* Mark all the scroll bars to be removed; we'll redeem
14000 the ones we want when we redisplay their windows. */
14001 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14002 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14003
14004 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14005 redisplay_windows (FRAME_ROOT_WINDOW (f));
14006 /* Remember that the invisible frames need to be redisplayed next
14007 time they're visible. */
14008 else if (!REDISPLAY_SOME_P ())
14009 f->redisplay = true;
14010
14011 /* The X error handler may have deleted that frame. */
14012 if (!FRAME_LIVE_P (f))
14013 continue;
14014
14015 /* Any scroll bars which redisplay_windows should have
14016 nuked should now go away. */
14017 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14018 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14019
14020 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14021 {
14022 /* If fonts changed on visible frame, display again. */
14023 if (f->fonts_changed)
14024 {
14025 adjust_frame_glyphs (f);
14026 /* Disable all redisplay optimizations for this
14027 frame. For the reasons, see the comment near
14028 the previous call to adjust_frame_glyphs above. */
14029 SET_FRAME_GARBAGED (f);
14030 f->fonts_changed = false;
14031 goto retry_frame;
14032 }
14033
14034 /* See if we have to hscroll. */
14035 if (!f->already_hscrolled_p)
14036 {
14037 f->already_hscrolled_p = true;
14038 if (hscroll_windows (f->root_window))
14039 goto retry_frame;
14040 }
14041
14042 /* If the frame's redisplay flag was not set before
14043 we went about redisplaying its windows, but it is
14044 set now, that means we employed some redisplay
14045 optimizations inside redisplay_windows, and
14046 bypassed producing some screen lines. But if
14047 f->redisplay is now set, it might mean the old
14048 faces are no longer valid (e.g., if redisplaying
14049 some window called some Lisp which defined a new
14050 face or redefined an existing face), so trying to
14051 use them in update_frame will segfault.
14052 Therefore, we must redisplay this frame. */
14053 if (!f_redisplay_flag && f->redisplay)
14054 goto retry_frame;
14055
14056 /* Prevent various kinds of signals during display
14057 update. stdio is not robust about handling
14058 signals, which can cause an apparent I/O error. */
14059 if (interrupt_input)
14060 unrequest_sigio ();
14061 STOP_POLLING;
14062
14063 pending |= update_frame (f, false, false);
14064 f->cursor_type_changed = false;
14065 f->updated_p = true;
14066 }
14067 }
14068 }
14069
14070 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14071
14072 if (!pending)
14073 {
14074 /* Do the mark_window_display_accurate after all windows have
14075 been redisplayed because this call resets flags in buffers
14076 which are needed for proper redisplay. */
14077 FOR_EACH_FRAME (tail, frame)
14078 {
14079 struct frame *f = XFRAME (frame);
14080 if (f->updated_p)
14081 {
14082 f->redisplay = false;
14083 mark_window_display_accurate (f->root_window, true);
14084 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14085 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14086 }
14087 }
14088 }
14089 }
14090 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14091 {
14092 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14093 /* Use list_of_error, not Qerror, so that
14094 we catch only errors and don't run the debugger. */
14095 internal_condition_case_1 (redisplay_window_1, selected_window,
14096 list_of_error,
14097 redisplay_window_error);
14098 if (update_miniwindow_p)
14099 internal_condition_case_1 (redisplay_window_1,
14100 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14101 redisplay_window_error);
14102
14103 /* Compare desired and current matrices, perform output. */
14104
14105 update:
14106 /* If fonts changed, display again. Likewise if redisplay_window_1
14107 above caused some change (e.g., a change in faces) that requires
14108 considering the entire frame again. */
14109 if (sf->fonts_changed || sf->redisplay)
14110 {
14111 if (sf->redisplay)
14112 {
14113 /* Set this to force a more thorough redisplay.
14114 Otherwise, we might immediately loop back to the
14115 above "else-if" clause (since all the conditions that
14116 led here might still be true), and we will then
14117 infloop, because the selected-frame's redisplay flag
14118 is not (and cannot be) reset. */
14119 windows_or_buffers_changed = 50;
14120 }
14121 goto retry;
14122 }
14123
14124 /* Prevent freeing of realized faces, since desired matrices are
14125 pending that reference the faces we computed and cached. */
14126 inhibit_free_realized_faces = true;
14127
14128 /* Prevent various kinds of signals during display update.
14129 stdio is not robust about handling signals,
14130 which can cause an apparent I/O error. */
14131 if (interrupt_input)
14132 unrequest_sigio ();
14133 STOP_POLLING;
14134
14135 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14136 {
14137 if (hscroll_windows (selected_window))
14138 goto retry;
14139
14140 XWINDOW (selected_window)->must_be_updated_p = true;
14141 pending = update_frame (sf, false, false);
14142 sf->cursor_type_changed = false;
14143 }
14144
14145 /* We may have called echo_area_display at the top of this
14146 function. If the echo area is on another frame, that may
14147 have put text on a frame other than the selected one, so the
14148 above call to update_frame would not have caught it. Catch
14149 it here. */
14150 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14151 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14152
14153 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14154 {
14155 XWINDOW (mini_window)->must_be_updated_p = true;
14156 pending |= update_frame (mini_frame, false, false);
14157 mini_frame->cursor_type_changed = false;
14158 if (!pending && hscroll_windows (mini_window))
14159 goto retry;
14160 }
14161 }
14162
14163 /* If display was paused because of pending input, make sure we do a
14164 thorough update the next time. */
14165 if (pending)
14166 {
14167 /* Prevent the optimization at the beginning of
14168 redisplay_internal that tries a single-line update of the
14169 line containing the cursor in the selected window. */
14170 CHARPOS (this_line_start_pos) = 0;
14171
14172 /* Let the overlay arrow be updated the next time. */
14173 update_overlay_arrows (0);
14174
14175 /* If we pause after scrolling, some rows in the current
14176 matrices of some windows are not valid. */
14177 if (!WINDOW_FULL_WIDTH_P (w)
14178 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14179 update_mode_lines = 36;
14180 }
14181 else
14182 {
14183 if (!consider_all_windows_p)
14184 {
14185 /* This has already been done above if
14186 consider_all_windows_p is set. */
14187 if (XBUFFER (w->contents)->text->redisplay
14188 && buffer_window_count (XBUFFER (w->contents)) > 1)
14189 /* This can happen if b->text->redisplay was set during
14190 jit-lock. */
14191 propagate_buffer_redisplay ();
14192 mark_window_display_accurate_1 (w, true);
14193
14194 /* Say overlay arrows are up to date. */
14195 update_overlay_arrows (1);
14196
14197 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14198 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14199 }
14200
14201 update_mode_lines = 0;
14202 windows_or_buffers_changed = 0;
14203 }
14204
14205 /* Start SIGIO interrupts coming again. Having them off during the
14206 code above makes it less likely one will discard output, but not
14207 impossible, since there might be stuff in the system buffer here.
14208 But it is much hairier to try to do anything about that. */
14209 if (interrupt_input)
14210 request_sigio ();
14211 RESUME_POLLING;
14212
14213 /* If a frame has become visible which was not before, redisplay
14214 again, so that we display it. Expose events for such a frame
14215 (which it gets when becoming visible) don't call the parts of
14216 redisplay constructing glyphs, so simply exposing a frame won't
14217 display anything in this case. So, we have to display these
14218 frames here explicitly. */
14219 if (!pending)
14220 {
14221 int new_count = 0;
14222
14223 FOR_EACH_FRAME (tail, frame)
14224 {
14225 if (XFRAME (frame)->visible)
14226 new_count++;
14227 }
14228
14229 if (new_count != number_of_visible_frames)
14230 windows_or_buffers_changed = 52;
14231 }
14232
14233 /* Change frame size now if a change is pending. */
14234 do_pending_window_change (true);
14235
14236 /* If we just did a pending size change, or have additional
14237 visible frames, or selected_window changed, redisplay again. */
14238 if ((windows_or_buffers_changed && !pending)
14239 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14240 goto retry;
14241
14242 /* Clear the face and image caches.
14243
14244 We used to do this only if consider_all_windows_p. But the cache
14245 needs to be cleared if a timer creates images in the current
14246 buffer (e.g. the test case in Bug#6230). */
14247
14248 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14249 {
14250 clear_face_cache (false);
14251 clear_face_cache_count = 0;
14252 }
14253
14254 #ifdef HAVE_WINDOW_SYSTEM
14255 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14256 {
14257 clear_image_caches (Qnil);
14258 clear_image_cache_count = 0;
14259 }
14260 #endif /* HAVE_WINDOW_SYSTEM */
14261
14262 end_of_redisplay:
14263 #ifdef HAVE_NS
14264 ns_set_doc_edited ();
14265 #endif
14266 if (interrupt_input && interrupts_deferred)
14267 request_sigio ();
14268
14269 unbind_to (count, Qnil);
14270 RESUME_POLLING;
14271 }
14272
14273
14274 /* Redisplay, but leave alone any recent echo area message unless
14275 another message has been requested in its place.
14276
14277 This is useful in situations where you need to redisplay but no
14278 user action has occurred, making it inappropriate for the message
14279 area to be cleared. See tracking_off and
14280 wait_reading_process_output for examples of these situations.
14281
14282 FROM_WHERE is an integer saying from where this function was
14283 called. This is useful for debugging. */
14284
14285 void
14286 redisplay_preserve_echo_area (int from_where)
14287 {
14288 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14289
14290 if (!NILP (echo_area_buffer[1]))
14291 {
14292 /* We have a previously displayed message, but no current
14293 message. Redisplay the previous message. */
14294 display_last_displayed_message_p = true;
14295 redisplay_internal ();
14296 display_last_displayed_message_p = false;
14297 }
14298 else
14299 redisplay_internal ();
14300
14301 flush_frame (SELECTED_FRAME ());
14302 }
14303
14304
14305 /* Function registered with record_unwind_protect in redisplay_internal. */
14306
14307 static void
14308 unwind_redisplay (void)
14309 {
14310 redisplaying_p = false;
14311 }
14312
14313
14314 /* Mark the display of leaf window W as accurate or inaccurate.
14315 If ACCURATE_P, mark display of W as accurate.
14316 If !ACCURATE_P, arrange for W to be redisplayed the next
14317 time redisplay_internal is called. */
14318
14319 static void
14320 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14321 {
14322 struct buffer *b = XBUFFER (w->contents);
14323
14324 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14325 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14326 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14327
14328 if (accurate_p)
14329 {
14330 b->clip_changed = false;
14331 b->prevent_redisplay_optimizations_p = false;
14332 eassert (buffer_window_count (b) > 0);
14333 /* Resetting b->text->redisplay is problematic!
14334 In order to make it safer to do it here, redisplay_internal must
14335 have copied all b->text->redisplay to their respective windows. */
14336 b->text->redisplay = false;
14337
14338 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14339 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14340 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14341 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14342
14343 w->current_matrix->buffer = b;
14344 w->current_matrix->begv = BUF_BEGV (b);
14345 w->current_matrix->zv = BUF_ZV (b);
14346
14347 w->last_cursor_vpos = w->cursor.vpos;
14348 w->last_cursor_off_p = w->cursor_off_p;
14349
14350 if (w == XWINDOW (selected_window))
14351 w->last_point = BUF_PT (b);
14352 else
14353 w->last_point = marker_position (w->pointm);
14354
14355 w->window_end_valid = true;
14356 w->update_mode_line = false;
14357 }
14358
14359 w->redisplay = !accurate_p;
14360 }
14361
14362
14363 /* Mark the display of windows in the window tree rooted at WINDOW as
14364 accurate or inaccurate. If ACCURATE_P, mark display of
14365 windows as accurate. If !ACCURATE_P, arrange for windows to
14366 be redisplayed the next time redisplay_internal is called. */
14367
14368 void
14369 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14370 {
14371 struct window *w;
14372
14373 for (; !NILP (window); window = w->next)
14374 {
14375 w = XWINDOW (window);
14376 if (WINDOWP (w->contents))
14377 mark_window_display_accurate (w->contents, accurate_p);
14378 else
14379 mark_window_display_accurate_1 (w, accurate_p);
14380 }
14381
14382 if (accurate_p)
14383 update_overlay_arrows (1);
14384 else
14385 /* Force a thorough redisplay the next time by setting
14386 last_arrow_position and last_arrow_string to t, which is
14387 unequal to any useful value of Voverlay_arrow_... */
14388 update_overlay_arrows (-1);
14389 }
14390
14391
14392 /* Return value in display table DP (Lisp_Char_Table *) for character
14393 C. Since a display table doesn't have any parent, we don't have to
14394 follow parent. Do not call this function directly but use the
14395 macro DISP_CHAR_VECTOR. */
14396
14397 Lisp_Object
14398 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14399 {
14400 Lisp_Object val;
14401
14402 if (ASCII_CHAR_P (c))
14403 {
14404 val = dp->ascii;
14405 if (SUB_CHAR_TABLE_P (val))
14406 val = XSUB_CHAR_TABLE (val)->contents[c];
14407 }
14408 else
14409 {
14410 Lisp_Object table;
14411
14412 XSETCHAR_TABLE (table, dp);
14413 val = char_table_ref (table, c);
14414 }
14415 if (NILP (val))
14416 val = dp->defalt;
14417 return val;
14418 }
14419
14420
14421 \f
14422 /***********************************************************************
14423 Window Redisplay
14424 ***********************************************************************/
14425
14426 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14427
14428 static void
14429 redisplay_windows (Lisp_Object window)
14430 {
14431 while (!NILP (window))
14432 {
14433 struct window *w = XWINDOW (window);
14434
14435 if (WINDOWP (w->contents))
14436 redisplay_windows (w->contents);
14437 else if (BUFFERP (w->contents))
14438 {
14439 displayed_buffer = XBUFFER (w->contents);
14440 /* Use list_of_error, not Qerror, so that
14441 we catch only errors and don't run the debugger. */
14442 internal_condition_case_1 (redisplay_window_0, window,
14443 list_of_error,
14444 redisplay_window_error);
14445 }
14446
14447 window = w->next;
14448 }
14449 }
14450
14451 static Lisp_Object
14452 redisplay_window_error (Lisp_Object ignore)
14453 {
14454 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14455 return Qnil;
14456 }
14457
14458 static Lisp_Object
14459 redisplay_window_0 (Lisp_Object window)
14460 {
14461 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14462 redisplay_window (window, false);
14463 return Qnil;
14464 }
14465
14466 static Lisp_Object
14467 redisplay_window_1 (Lisp_Object window)
14468 {
14469 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14470 redisplay_window (window, true);
14471 return Qnil;
14472 }
14473 \f
14474
14475 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14476 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14477 which positions recorded in ROW differ from current buffer
14478 positions.
14479
14480 Return true iff cursor is on this row. */
14481
14482 static bool
14483 set_cursor_from_row (struct window *w, struct glyph_row *row,
14484 struct glyph_matrix *matrix,
14485 ptrdiff_t delta, ptrdiff_t delta_bytes,
14486 int dy, int dvpos)
14487 {
14488 struct glyph *glyph = row->glyphs[TEXT_AREA];
14489 struct glyph *end = glyph + row->used[TEXT_AREA];
14490 struct glyph *cursor = NULL;
14491 /* The last known character position in row. */
14492 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14493 int x = row->x;
14494 ptrdiff_t pt_old = PT - delta;
14495 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14496 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14497 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14498 /* A glyph beyond the edge of TEXT_AREA which we should never
14499 touch. */
14500 struct glyph *glyphs_end = end;
14501 /* True means we've found a match for cursor position, but that
14502 glyph has the avoid_cursor_p flag set. */
14503 bool match_with_avoid_cursor = false;
14504 /* True means we've seen at least one glyph that came from a
14505 display string. */
14506 bool string_seen = false;
14507 /* Largest and smallest buffer positions seen so far during scan of
14508 glyph row. */
14509 ptrdiff_t bpos_max = pos_before;
14510 ptrdiff_t bpos_min = pos_after;
14511 /* Last buffer position covered by an overlay string with an integer
14512 `cursor' property. */
14513 ptrdiff_t bpos_covered = 0;
14514 /* True means the display string on which to display the cursor
14515 comes from a text property, not from an overlay. */
14516 bool string_from_text_prop = false;
14517
14518 /* Don't even try doing anything if called for a mode-line or
14519 header-line row, since the rest of the code isn't prepared to
14520 deal with such calamities. */
14521 eassert (!row->mode_line_p);
14522 if (row->mode_line_p)
14523 return false;
14524
14525 /* Skip over glyphs not having an object at the start and the end of
14526 the row. These are special glyphs like truncation marks on
14527 terminal frames. */
14528 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14529 {
14530 if (!row->reversed_p)
14531 {
14532 while (glyph < end
14533 && NILP (glyph->object)
14534 && glyph->charpos < 0)
14535 {
14536 x += glyph->pixel_width;
14537 ++glyph;
14538 }
14539 while (end > glyph
14540 && NILP ((end - 1)->object)
14541 /* CHARPOS is zero for blanks and stretch glyphs
14542 inserted by extend_face_to_end_of_line. */
14543 && (end - 1)->charpos <= 0)
14544 --end;
14545 glyph_before = glyph - 1;
14546 glyph_after = end;
14547 }
14548 else
14549 {
14550 struct glyph *g;
14551
14552 /* If the glyph row is reversed, we need to process it from back
14553 to front, so swap the edge pointers. */
14554 glyphs_end = end = glyph - 1;
14555 glyph += row->used[TEXT_AREA] - 1;
14556
14557 while (glyph > end + 1
14558 && NILP (glyph->object)
14559 && glyph->charpos < 0)
14560 {
14561 --glyph;
14562 x -= glyph->pixel_width;
14563 }
14564 if (NILP (glyph->object) && glyph->charpos < 0)
14565 --glyph;
14566 /* By default, in reversed rows we put the cursor on the
14567 rightmost (first in the reading order) glyph. */
14568 for (g = end + 1; g < glyph; g++)
14569 x += g->pixel_width;
14570 while (end < glyph
14571 && NILP ((end + 1)->object)
14572 && (end + 1)->charpos <= 0)
14573 ++end;
14574 glyph_before = glyph + 1;
14575 glyph_after = end;
14576 }
14577 }
14578 else if (row->reversed_p)
14579 {
14580 /* In R2L rows that don't display text, put the cursor on the
14581 rightmost glyph. Case in point: an empty last line that is
14582 part of an R2L paragraph. */
14583 cursor = end - 1;
14584 /* Avoid placing the cursor on the last glyph of the row, where
14585 on terminal frames we hold the vertical border between
14586 adjacent windows. */
14587 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14588 && !WINDOW_RIGHTMOST_P (w)
14589 && cursor == row->glyphs[LAST_AREA] - 1)
14590 cursor--;
14591 x = -1; /* will be computed below, at label compute_x */
14592 }
14593
14594 /* Step 1: Try to find the glyph whose character position
14595 corresponds to point. If that's not possible, find 2 glyphs
14596 whose character positions are the closest to point, one before
14597 point, the other after it. */
14598 if (!row->reversed_p)
14599 while (/* not marched to end of glyph row */
14600 glyph < end
14601 /* glyph was not inserted by redisplay for internal purposes */
14602 && !NILP (glyph->object))
14603 {
14604 if (BUFFERP (glyph->object))
14605 {
14606 ptrdiff_t dpos = glyph->charpos - pt_old;
14607
14608 if (glyph->charpos > bpos_max)
14609 bpos_max = glyph->charpos;
14610 if (glyph->charpos < bpos_min)
14611 bpos_min = glyph->charpos;
14612 if (!glyph->avoid_cursor_p)
14613 {
14614 /* If we hit point, we've found the glyph on which to
14615 display the cursor. */
14616 if (dpos == 0)
14617 {
14618 match_with_avoid_cursor = false;
14619 break;
14620 }
14621 /* See if we've found a better approximation to
14622 POS_BEFORE or to POS_AFTER. */
14623 if (0 > dpos && dpos > pos_before - pt_old)
14624 {
14625 pos_before = glyph->charpos;
14626 glyph_before = glyph;
14627 }
14628 else if (0 < dpos && dpos < pos_after - pt_old)
14629 {
14630 pos_after = glyph->charpos;
14631 glyph_after = glyph;
14632 }
14633 }
14634 else if (dpos == 0)
14635 match_with_avoid_cursor = true;
14636 }
14637 else if (STRINGP (glyph->object))
14638 {
14639 Lisp_Object chprop;
14640 ptrdiff_t glyph_pos = glyph->charpos;
14641
14642 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14643 glyph->object);
14644 if (!NILP (chprop))
14645 {
14646 /* If the string came from a `display' text property,
14647 look up the buffer position of that property and
14648 use that position to update bpos_max, as if we
14649 actually saw such a position in one of the row's
14650 glyphs. This helps with supporting integer values
14651 of `cursor' property on the display string in
14652 situations where most or all of the row's buffer
14653 text is completely covered by display properties,
14654 so that no glyph with valid buffer positions is
14655 ever seen in the row. */
14656 ptrdiff_t prop_pos =
14657 string_buffer_position_lim (glyph->object, pos_before,
14658 pos_after, false);
14659
14660 if (prop_pos >= pos_before)
14661 bpos_max = prop_pos;
14662 }
14663 if (INTEGERP (chprop))
14664 {
14665 bpos_covered = bpos_max + XINT (chprop);
14666 /* If the `cursor' property covers buffer positions up
14667 to and including point, we should display cursor on
14668 this glyph. Note that, if a `cursor' property on one
14669 of the string's characters has an integer value, we
14670 will break out of the loop below _before_ we get to
14671 the position match above. IOW, integer values of
14672 the `cursor' property override the "exact match for
14673 point" strategy of positioning the cursor. */
14674 /* Implementation note: bpos_max == pt_old when, e.g.,
14675 we are in an empty line, where bpos_max is set to
14676 MATRIX_ROW_START_CHARPOS, see above. */
14677 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14678 {
14679 cursor = glyph;
14680 break;
14681 }
14682 }
14683
14684 string_seen = true;
14685 }
14686 x += glyph->pixel_width;
14687 ++glyph;
14688 }
14689 else if (glyph > end) /* row is reversed */
14690 while (!NILP (glyph->object))
14691 {
14692 if (BUFFERP (glyph->object))
14693 {
14694 ptrdiff_t dpos = glyph->charpos - pt_old;
14695
14696 if (glyph->charpos > bpos_max)
14697 bpos_max = glyph->charpos;
14698 if (glyph->charpos < bpos_min)
14699 bpos_min = glyph->charpos;
14700 if (!glyph->avoid_cursor_p)
14701 {
14702 if (dpos == 0)
14703 {
14704 match_with_avoid_cursor = false;
14705 break;
14706 }
14707 if (0 > dpos && dpos > pos_before - pt_old)
14708 {
14709 pos_before = glyph->charpos;
14710 glyph_before = glyph;
14711 }
14712 else if (0 < dpos && dpos < pos_after - pt_old)
14713 {
14714 pos_after = glyph->charpos;
14715 glyph_after = glyph;
14716 }
14717 }
14718 else if (dpos == 0)
14719 match_with_avoid_cursor = true;
14720 }
14721 else if (STRINGP (glyph->object))
14722 {
14723 Lisp_Object chprop;
14724 ptrdiff_t glyph_pos = glyph->charpos;
14725
14726 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14727 glyph->object);
14728 if (!NILP (chprop))
14729 {
14730 ptrdiff_t prop_pos =
14731 string_buffer_position_lim (glyph->object, pos_before,
14732 pos_after, false);
14733
14734 if (prop_pos >= pos_before)
14735 bpos_max = prop_pos;
14736 }
14737 if (INTEGERP (chprop))
14738 {
14739 bpos_covered = bpos_max + XINT (chprop);
14740 /* If the `cursor' property covers buffer positions up
14741 to and including point, we should display cursor on
14742 this glyph. */
14743 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14744 {
14745 cursor = glyph;
14746 break;
14747 }
14748 }
14749 string_seen = true;
14750 }
14751 --glyph;
14752 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14753 {
14754 x--; /* can't use any pixel_width */
14755 break;
14756 }
14757 x -= glyph->pixel_width;
14758 }
14759
14760 /* Step 2: If we didn't find an exact match for point, we need to
14761 look for a proper place to put the cursor among glyphs between
14762 GLYPH_BEFORE and GLYPH_AFTER. */
14763 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14764 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14765 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14766 {
14767 /* An empty line has a single glyph whose OBJECT is nil and
14768 whose CHARPOS is the position of a newline on that line.
14769 Note that on a TTY, there are more glyphs after that, which
14770 were produced by extend_face_to_end_of_line, but their
14771 CHARPOS is zero or negative. */
14772 bool empty_line_p =
14773 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14774 && NILP (glyph->object) && glyph->charpos > 0
14775 /* On a TTY, continued and truncated rows also have a glyph at
14776 their end whose OBJECT is nil and whose CHARPOS is
14777 positive (the continuation and truncation glyphs), but such
14778 rows are obviously not "empty". */
14779 && !(row->continued_p || row->truncated_on_right_p));
14780
14781 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14782 {
14783 ptrdiff_t ellipsis_pos;
14784
14785 /* Scan back over the ellipsis glyphs. */
14786 if (!row->reversed_p)
14787 {
14788 ellipsis_pos = (glyph - 1)->charpos;
14789 while (glyph > row->glyphs[TEXT_AREA]
14790 && (glyph - 1)->charpos == ellipsis_pos)
14791 glyph--, x -= glyph->pixel_width;
14792 /* That loop always goes one position too far, including
14793 the glyph before the ellipsis. So scan forward over
14794 that one. */
14795 x += glyph->pixel_width;
14796 glyph++;
14797 }
14798 else /* row is reversed */
14799 {
14800 ellipsis_pos = (glyph + 1)->charpos;
14801 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14802 && (glyph + 1)->charpos == ellipsis_pos)
14803 glyph++, x += glyph->pixel_width;
14804 x -= glyph->pixel_width;
14805 glyph--;
14806 }
14807 }
14808 else if (match_with_avoid_cursor)
14809 {
14810 cursor = glyph_after;
14811 x = -1;
14812 }
14813 else if (string_seen)
14814 {
14815 int incr = row->reversed_p ? -1 : +1;
14816
14817 /* Need to find the glyph that came out of a string which is
14818 present at point. That glyph is somewhere between
14819 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14820 positioned between POS_BEFORE and POS_AFTER in the
14821 buffer. */
14822 struct glyph *start, *stop;
14823 ptrdiff_t pos = pos_before;
14824
14825 x = -1;
14826
14827 /* If the row ends in a newline from a display string,
14828 reordering could have moved the glyphs belonging to the
14829 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14830 in this case we extend the search to the last glyph in
14831 the row that was not inserted by redisplay. */
14832 if (row->ends_in_newline_from_string_p)
14833 {
14834 glyph_after = end;
14835 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14836 }
14837
14838 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14839 correspond to POS_BEFORE and POS_AFTER, respectively. We
14840 need START and STOP in the order that corresponds to the
14841 row's direction as given by its reversed_p flag. If the
14842 directionality of characters between POS_BEFORE and
14843 POS_AFTER is the opposite of the row's base direction,
14844 these characters will have been reordered for display,
14845 and we need to reverse START and STOP. */
14846 if (!row->reversed_p)
14847 {
14848 start = min (glyph_before, glyph_after);
14849 stop = max (glyph_before, glyph_after);
14850 }
14851 else
14852 {
14853 start = max (glyph_before, glyph_after);
14854 stop = min (glyph_before, glyph_after);
14855 }
14856 for (glyph = start + incr;
14857 row->reversed_p ? glyph > stop : glyph < stop; )
14858 {
14859
14860 /* Any glyphs that come from the buffer are here because
14861 of bidi reordering. Skip them, and only pay
14862 attention to glyphs that came from some string. */
14863 if (STRINGP (glyph->object))
14864 {
14865 Lisp_Object str;
14866 ptrdiff_t tem;
14867 /* If the display property covers the newline, we
14868 need to search for it one position farther. */
14869 ptrdiff_t lim = pos_after
14870 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14871
14872 string_from_text_prop = false;
14873 str = glyph->object;
14874 tem = string_buffer_position_lim (str, pos, lim, false);
14875 if (tem == 0 /* from overlay */
14876 || pos <= tem)
14877 {
14878 /* If the string from which this glyph came is
14879 found in the buffer at point, or at position
14880 that is closer to point than pos_after, then
14881 we've found the glyph we've been looking for.
14882 If it comes from an overlay (tem == 0), and
14883 it has the `cursor' property on one of its
14884 glyphs, record that glyph as a candidate for
14885 displaying the cursor. (As in the
14886 unidirectional version, we will display the
14887 cursor on the last candidate we find.) */
14888 if (tem == 0
14889 || tem == pt_old
14890 || (tem - pt_old > 0 && tem < pos_after))
14891 {
14892 /* The glyphs from this string could have
14893 been reordered. Find the one with the
14894 smallest string position. Or there could
14895 be a character in the string with the
14896 `cursor' property, which means display
14897 cursor on that character's glyph. */
14898 ptrdiff_t strpos = glyph->charpos;
14899
14900 if (tem)
14901 {
14902 cursor = glyph;
14903 string_from_text_prop = true;
14904 }
14905 for ( ;
14906 (row->reversed_p ? glyph > stop : glyph < stop)
14907 && EQ (glyph->object, str);
14908 glyph += incr)
14909 {
14910 Lisp_Object cprop;
14911 ptrdiff_t gpos = glyph->charpos;
14912
14913 cprop = Fget_char_property (make_number (gpos),
14914 Qcursor,
14915 glyph->object);
14916 if (!NILP (cprop))
14917 {
14918 cursor = glyph;
14919 break;
14920 }
14921 if (tem && glyph->charpos < strpos)
14922 {
14923 strpos = glyph->charpos;
14924 cursor = glyph;
14925 }
14926 }
14927
14928 if (tem == pt_old
14929 || (tem - pt_old > 0 && tem < pos_after))
14930 goto compute_x;
14931 }
14932 if (tem)
14933 pos = tem + 1; /* don't find previous instances */
14934 }
14935 /* This string is not what we want; skip all of the
14936 glyphs that came from it. */
14937 while ((row->reversed_p ? glyph > stop : glyph < stop)
14938 && EQ (glyph->object, str))
14939 glyph += incr;
14940 }
14941 else
14942 glyph += incr;
14943 }
14944
14945 /* If we reached the end of the line, and END was from a string,
14946 the cursor is not on this line. */
14947 if (cursor == NULL
14948 && (row->reversed_p ? glyph <= end : glyph >= end)
14949 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14950 && STRINGP (end->object)
14951 && row->continued_p)
14952 return false;
14953 }
14954 /* A truncated row may not include PT among its character positions.
14955 Setting the cursor inside the scroll margin will trigger
14956 recalculation of hscroll in hscroll_window_tree. But if a
14957 display string covers point, defer to the string-handling
14958 code below to figure this out. */
14959 else if (row->truncated_on_left_p && pt_old < bpos_min)
14960 {
14961 cursor = glyph_before;
14962 x = -1;
14963 }
14964 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14965 /* Zero-width characters produce no glyphs. */
14966 || (!empty_line_p
14967 && (row->reversed_p
14968 ? glyph_after > glyphs_end
14969 : glyph_after < glyphs_end)))
14970 {
14971 cursor = glyph_after;
14972 x = -1;
14973 }
14974 }
14975
14976 compute_x:
14977 if (cursor != NULL)
14978 glyph = cursor;
14979 else if (glyph == glyphs_end
14980 && pos_before == pos_after
14981 && STRINGP ((row->reversed_p
14982 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14983 : row->glyphs[TEXT_AREA])->object))
14984 {
14985 /* If all the glyphs of this row came from strings, put the
14986 cursor on the first glyph of the row. This avoids having the
14987 cursor outside of the text area in this very rare and hard
14988 use case. */
14989 glyph =
14990 row->reversed_p
14991 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14992 : row->glyphs[TEXT_AREA];
14993 }
14994 if (x < 0)
14995 {
14996 struct glyph *g;
14997
14998 /* Need to compute x that corresponds to GLYPH. */
14999 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15000 {
15001 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15002 emacs_abort ();
15003 x += g->pixel_width;
15004 }
15005 }
15006
15007 /* ROW could be part of a continued line, which, under bidi
15008 reordering, might have other rows whose start and end charpos
15009 occlude point. Only set w->cursor if we found a better
15010 approximation to the cursor position than we have from previously
15011 examined candidate rows belonging to the same continued line. */
15012 if (/* We already have a candidate row. */
15013 w->cursor.vpos >= 0
15014 /* That candidate is not the row we are processing. */
15015 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15016 /* Make sure cursor.vpos specifies a row whose start and end
15017 charpos occlude point, and it is valid candidate for being a
15018 cursor-row. This is because some callers of this function
15019 leave cursor.vpos at the row where the cursor was displayed
15020 during the last redisplay cycle. */
15021 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15022 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15023 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15024 {
15025 struct glyph *g1
15026 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15027
15028 /* Don't consider glyphs that are outside TEXT_AREA. */
15029 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15030 return false;
15031 /* Keep the candidate whose buffer position is the closest to
15032 point or has the `cursor' property. */
15033 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15034 w->cursor.hpos >= 0
15035 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15036 && ((BUFFERP (g1->object)
15037 && (g1->charpos == pt_old /* An exact match always wins. */
15038 || (BUFFERP (glyph->object)
15039 && eabs (g1->charpos - pt_old)
15040 < eabs (glyph->charpos - pt_old))))
15041 /* Previous candidate is a glyph from a string that has
15042 a non-nil `cursor' property. */
15043 || (STRINGP (g1->object)
15044 && (!NILP (Fget_char_property (make_number (g1->charpos),
15045 Qcursor, g1->object))
15046 /* Previous candidate is from the same display
15047 string as this one, and the display string
15048 came from a text property. */
15049 || (EQ (g1->object, glyph->object)
15050 && string_from_text_prop)
15051 /* this candidate is from newline and its
15052 position is not an exact match */
15053 || (NILP (glyph->object)
15054 && glyph->charpos != pt_old)))))
15055 return false;
15056 /* If this candidate gives an exact match, use that. */
15057 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15058 /* If this candidate is a glyph created for the
15059 terminating newline of a line, and point is on that
15060 newline, it wins because it's an exact match. */
15061 || (!row->continued_p
15062 && NILP (glyph->object)
15063 && glyph->charpos == 0
15064 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15065 /* Otherwise, keep the candidate that comes from a row
15066 spanning less buffer positions. This may win when one or
15067 both candidate positions are on glyphs that came from
15068 display strings, for which we cannot compare buffer
15069 positions. */
15070 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15071 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15072 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15073 return false;
15074 }
15075 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15076 w->cursor.x = x;
15077 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15078 w->cursor.y = row->y + dy;
15079
15080 if (w == XWINDOW (selected_window))
15081 {
15082 if (!row->continued_p
15083 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15084 && row->x == 0)
15085 {
15086 this_line_buffer = XBUFFER (w->contents);
15087
15088 CHARPOS (this_line_start_pos)
15089 = MATRIX_ROW_START_CHARPOS (row) + delta;
15090 BYTEPOS (this_line_start_pos)
15091 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15092
15093 CHARPOS (this_line_end_pos)
15094 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15095 BYTEPOS (this_line_end_pos)
15096 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15097
15098 this_line_y = w->cursor.y;
15099 this_line_pixel_height = row->height;
15100 this_line_vpos = w->cursor.vpos;
15101 this_line_start_x = row->x;
15102 }
15103 else
15104 CHARPOS (this_line_start_pos) = 0;
15105 }
15106
15107 return true;
15108 }
15109
15110
15111 /* Run window scroll functions, if any, for WINDOW with new window
15112 start STARTP. Sets the window start of WINDOW to that position.
15113
15114 We assume that the window's buffer is really current. */
15115
15116 static struct text_pos
15117 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15118 {
15119 struct window *w = XWINDOW (window);
15120 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15121
15122 eassert (current_buffer == XBUFFER (w->contents));
15123
15124 if (!NILP (Vwindow_scroll_functions))
15125 {
15126 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15127 make_number (CHARPOS (startp)));
15128 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15129 /* In case the hook functions switch buffers. */
15130 set_buffer_internal (XBUFFER (w->contents));
15131 }
15132
15133 return startp;
15134 }
15135
15136
15137 /* Make sure the line containing the cursor is fully visible.
15138 A value of true means there is nothing to be done.
15139 (Either the line is fully visible, or it cannot be made so,
15140 or we cannot tell.)
15141
15142 If FORCE_P, return false even if partial visible cursor row
15143 is higher than window.
15144
15145 If CURRENT_MATRIX_P, use the information from the
15146 window's current glyph matrix; otherwise use the desired glyph
15147 matrix.
15148
15149 A value of false means the caller should do scrolling
15150 as if point had gone off the screen. */
15151
15152 static bool
15153 cursor_row_fully_visible_p (struct window *w, bool force_p,
15154 bool current_matrix_p)
15155 {
15156 struct glyph_matrix *matrix;
15157 struct glyph_row *row;
15158 int window_height;
15159
15160 if (!make_cursor_line_fully_visible_p)
15161 return true;
15162
15163 /* It's not always possible to find the cursor, e.g, when a window
15164 is full of overlay strings. Don't do anything in that case. */
15165 if (w->cursor.vpos < 0)
15166 return true;
15167
15168 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15169 row = MATRIX_ROW (matrix, w->cursor.vpos);
15170
15171 /* If the cursor row is not partially visible, there's nothing to do. */
15172 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15173 return true;
15174
15175 /* If the row the cursor is in is taller than the window's height,
15176 it's not clear what to do, so do nothing. */
15177 window_height = window_box_height (w);
15178 if (row->height >= window_height)
15179 {
15180 if (!force_p || MINI_WINDOW_P (w)
15181 || w->vscroll || w->cursor.vpos == 0)
15182 return true;
15183 }
15184 return false;
15185 }
15186
15187
15188 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15189 means only WINDOW is redisplayed in redisplay_internal.
15190 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15191 in redisplay_window to bring a partially visible line into view in
15192 the case that only the cursor has moved.
15193
15194 LAST_LINE_MISFIT should be true if we're scrolling because the
15195 last screen line's vertical height extends past the end of the screen.
15196
15197 Value is
15198
15199 1 if scrolling succeeded
15200
15201 0 if scrolling didn't find point.
15202
15203 -1 if new fonts have been loaded so that we must interrupt
15204 redisplay, adjust glyph matrices, and try again. */
15205
15206 enum
15207 {
15208 SCROLLING_SUCCESS,
15209 SCROLLING_FAILED,
15210 SCROLLING_NEED_LARGER_MATRICES
15211 };
15212
15213 /* If scroll-conservatively is more than this, never recenter.
15214
15215 If you change this, don't forget to update the doc string of
15216 `scroll-conservatively' and the Emacs manual. */
15217 #define SCROLL_LIMIT 100
15218
15219 static int
15220 try_scrolling (Lisp_Object window, bool just_this_one_p,
15221 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15222 bool temp_scroll_step, bool last_line_misfit)
15223 {
15224 struct window *w = XWINDOW (window);
15225 struct frame *f = XFRAME (w->frame);
15226 struct text_pos pos, startp;
15227 struct it it;
15228 int this_scroll_margin, scroll_max, rc, height;
15229 int dy = 0, amount_to_scroll = 0;
15230 bool scroll_down_p = false;
15231 int extra_scroll_margin_lines = last_line_misfit;
15232 Lisp_Object aggressive;
15233 /* We will never try scrolling more than this number of lines. */
15234 int scroll_limit = SCROLL_LIMIT;
15235 int frame_line_height = default_line_pixel_height (w);
15236 int window_total_lines
15237 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15238
15239 #ifdef GLYPH_DEBUG
15240 debug_method_add (w, "try_scrolling");
15241 #endif
15242
15243 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15244
15245 /* Compute scroll margin height in pixels. We scroll when point is
15246 within this distance from the top or bottom of the window. */
15247 if (scroll_margin > 0)
15248 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15249 * frame_line_height;
15250 else
15251 this_scroll_margin = 0;
15252
15253 /* Force arg_scroll_conservatively to have a reasonable value, to
15254 avoid scrolling too far away with slow move_it_* functions. Note
15255 that the user can supply scroll-conservatively equal to
15256 `most-positive-fixnum', which can be larger than INT_MAX. */
15257 if (arg_scroll_conservatively > scroll_limit)
15258 {
15259 arg_scroll_conservatively = scroll_limit + 1;
15260 scroll_max = scroll_limit * frame_line_height;
15261 }
15262 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15263 /* Compute how much we should try to scroll maximally to bring
15264 point into view. */
15265 scroll_max = (max (scroll_step,
15266 max (arg_scroll_conservatively, temp_scroll_step))
15267 * frame_line_height);
15268 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15269 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15270 /* We're trying to scroll because of aggressive scrolling but no
15271 scroll_step is set. Choose an arbitrary one. */
15272 scroll_max = 10 * frame_line_height;
15273 else
15274 scroll_max = 0;
15275
15276 too_near_end:
15277
15278 /* Decide whether to scroll down. */
15279 if (PT > CHARPOS (startp))
15280 {
15281 int scroll_margin_y;
15282
15283 /* Compute the pixel ypos of the scroll margin, then move IT to
15284 either that ypos or PT, whichever comes first. */
15285 start_display (&it, w, startp);
15286 scroll_margin_y = it.last_visible_y - this_scroll_margin
15287 - frame_line_height * extra_scroll_margin_lines;
15288 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15289 (MOVE_TO_POS | MOVE_TO_Y));
15290
15291 if (PT > CHARPOS (it.current.pos))
15292 {
15293 int y0 = line_bottom_y (&it);
15294 /* Compute how many pixels below window bottom to stop searching
15295 for PT. This avoids costly search for PT that is far away if
15296 the user limited scrolling by a small number of lines, but
15297 always finds PT if scroll_conservatively is set to a large
15298 number, such as most-positive-fixnum. */
15299 int slack = max (scroll_max, 10 * frame_line_height);
15300 int y_to_move = it.last_visible_y + slack;
15301
15302 /* Compute the distance from the scroll margin to PT or to
15303 the scroll limit, whichever comes first. This should
15304 include the height of the cursor line, to make that line
15305 fully visible. */
15306 move_it_to (&it, PT, -1, y_to_move,
15307 -1, MOVE_TO_POS | MOVE_TO_Y);
15308 dy = line_bottom_y (&it) - y0;
15309
15310 if (dy > scroll_max)
15311 return SCROLLING_FAILED;
15312
15313 if (dy > 0)
15314 scroll_down_p = true;
15315 }
15316 }
15317
15318 if (scroll_down_p)
15319 {
15320 /* Point is in or below the bottom scroll margin, so move the
15321 window start down. If scrolling conservatively, move it just
15322 enough down to make point visible. If scroll_step is set,
15323 move it down by scroll_step. */
15324 if (arg_scroll_conservatively)
15325 amount_to_scroll
15326 = min (max (dy, frame_line_height),
15327 frame_line_height * arg_scroll_conservatively);
15328 else if (scroll_step || temp_scroll_step)
15329 amount_to_scroll = scroll_max;
15330 else
15331 {
15332 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15333 height = WINDOW_BOX_TEXT_HEIGHT (w);
15334 if (NUMBERP (aggressive))
15335 {
15336 double float_amount = XFLOATINT (aggressive) * height;
15337 int aggressive_scroll = float_amount;
15338 if (aggressive_scroll == 0 && float_amount > 0)
15339 aggressive_scroll = 1;
15340 /* Don't let point enter the scroll margin near top of
15341 the window. This could happen if the value of
15342 scroll_up_aggressively is too large and there are
15343 non-zero margins, because scroll_up_aggressively
15344 means put point that fraction of window height
15345 _from_the_bottom_margin_. */
15346 if (aggressive_scroll + 2 * this_scroll_margin > height)
15347 aggressive_scroll = height - 2 * this_scroll_margin;
15348 amount_to_scroll = dy + aggressive_scroll;
15349 }
15350 }
15351
15352 if (amount_to_scroll <= 0)
15353 return SCROLLING_FAILED;
15354
15355 start_display (&it, w, startp);
15356 if (arg_scroll_conservatively <= scroll_limit)
15357 move_it_vertically (&it, amount_to_scroll);
15358 else
15359 {
15360 /* Extra precision for users who set scroll-conservatively
15361 to a large number: make sure the amount we scroll
15362 the window start is never less than amount_to_scroll,
15363 which was computed as distance from window bottom to
15364 point. This matters when lines at window top and lines
15365 below window bottom have different height. */
15366 struct it it1;
15367 void *it1data = NULL;
15368 /* We use a temporary it1 because line_bottom_y can modify
15369 its argument, if it moves one line down; see there. */
15370 int start_y;
15371
15372 SAVE_IT (it1, it, it1data);
15373 start_y = line_bottom_y (&it1);
15374 do {
15375 RESTORE_IT (&it, &it, it1data);
15376 move_it_by_lines (&it, 1);
15377 SAVE_IT (it1, it, it1data);
15378 } while (IT_CHARPOS (it) < ZV
15379 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15380 bidi_unshelve_cache (it1data, true);
15381 }
15382
15383 /* If STARTP is unchanged, move it down another screen line. */
15384 if (IT_CHARPOS (it) == CHARPOS (startp))
15385 move_it_by_lines (&it, 1);
15386 startp = it.current.pos;
15387 }
15388 else
15389 {
15390 struct text_pos scroll_margin_pos = startp;
15391 int y_offset = 0;
15392
15393 /* See if point is inside the scroll margin at the top of the
15394 window. */
15395 if (this_scroll_margin)
15396 {
15397 int y_start;
15398
15399 start_display (&it, w, startp);
15400 y_start = it.current_y;
15401 move_it_vertically (&it, this_scroll_margin);
15402 scroll_margin_pos = it.current.pos;
15403 /* If we didn't move enough before hitting ZV, request
15404 additional amount of scroll, to move point out of the
15405 scroll margin. */
15406 if (IT_CHARPOS (it) == ZV
15407 && it.current_y - y_start < this_scroll_margin)
15408 y_offset = this_scroll_margin - (it.current_y - y_start);
15409 }
15410
15411 if (PT < CHARPOS (scroll_margin_pos))
15412 {
15413 /* Point is in the scroll margin at the top of the window or
15414 above what is displayed in the window. */
15415 int y0, y_to_move;
15416
15417 /* Compute the vertical distance from PT to the scroll
15418 margin position. Move as far as scroll_max allows, or
15419 one screenful, or 10 screen lines, whichever is largest.
15420 Give up if distance is greater than scroll_max or if we
15421 didn't reach the scroll margin position. */
15422 SET_TEXT_POS (pos, PT, PT_BYTE);
15423 start_display (&it, w, pos);
15424 y0 = it.current_y;
15425 y_to_move = max (it.last_visible_y,
15426 max (scroll_max, 10 * frame_line_height));
15427 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15428 y_to_move, -1,
15429 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15430 dy = it.current_y - y0;
15431 if (dy > scroll_max
15432 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15433 return SCROLLING_FAILED;
15434
15435 /* Additional scroll for when ZV was too close to point. */
15436 dy += y_offset;
15437
15438 /* Compute new window start. */
15439 start_display (&it, w, startp);
15440
15441 if (arg_scroll_conservatively)
15442 amount_to_scroll = max (dy, frame_line_height
15443 * max (scroll_step, temp_scroll_step));
15444 else if (scroll_step || temp_scroll_step)
15445 amount_to_scroll = scroll_max;
15446 else
15447 {
15448 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15449 height = WINDOW_BOX_TEXT_HEIGHT (w);
15450 if (NUMBERP (aggressive))
15451 {
15452 double float_amount = XFLOATINT (aggressive) * height;
15453 int aggressive_scroll = float_amount;
15454 if (aggressive_scroll == 0 && float_amount > 0)
15455 aggressive_scroll = 1;
15456 /* Don't let point enter the scroll margin near
15457 bottom of the window, if the value of
15458 scroll_down_aggressively happens to be too
15459 large. */
15460 if (aggressive_scroll + 2 * this_scroll_margin > height)
15461 aggressive_scroll = height - 2 * this_scroll_margin;
15462 amount_to_scroll = dy + aggressive_scroll;
15463 }
15464 }
15465
15466 if (amount_to_scroll <= 0)
15467 return SCROLLING_FAILED;
15468
15469 move_it_vertically_backward (&it, amount_to_scroll);
15470 startp = it.current.pos;
15471 }
15472 }
15473
15474 /* Run window scroll functions. */
15475 startp = run_window_scroll_functions (window, startp);
15476
15477 /* Display the window. Give up if new fonts are loaded, or if point
15478 doesn't appear. */
15479 if (!try_window (window, startp, 0))
15480 rc = SCROLLING_NEED_LARGER_MATRICES;
15481 else if (w->cursor.vpos < 0)
15482 {
15483 clear_glyph_matrix (w->desired_matrix);
15484 rc = SCROLLING_FAILED;
15485 }
15486 else
15487 {
15488 /* Maybe forget recorded base line for line number display. */
15489 if (!just_this_one_p
15490 || current_buffer->clip_changed
15491 || BEG_UNCHANGED < CHARPOS (startp))
15492 w->base_line_number = 0;
15493
15494 /* If cursor ends up on a partially visible line,
15495 treat that as being off the bottom of the screen. */
15496 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15497 false)
15498 /* It's possible that the cursor is on the first line of the
15499 buffer, which is partially obscured due to a vscroll
15500 (Bug#7537). In that case, avoid looping forever. */
15501 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15502 {
15503 clear_glyph_matrix (w->desired_matrix);
15504 ++extra_scroll_margin_lines;
15505 goto too_near_end;
15506 }
15507 rc = SCROLLING_SUCCESS;
15508 }
15509
15510 return rc;
15511 }
15512
15513
15514 /* Compute a suitable window start for window W if display of W starts
15515 on a continuation line. Value is true if a new window start
15516 was computed.
15517
15518 The new window start will be computed, based on W's width, starting
15519 from the start of the continued line. It is the start of the
15520 screen line with the minimum distance from the old start W->start,
15521 which is still before point (otherwise point will definitely not
15522 be visible in the window). */
15523
15524 static bool
15525 compute_window_start_on_continuation_line (struct window *w)
15526 {
15527 struct text_pos pos, start_pos, pos_before_pt;
15528 bool window_start_changed_p = false;
15529
15530 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15531
15532 /* If window start is on a continuation line... Window start may be
15533 < BEGV in case there's invisible text at the start of the
15534 buffer (M-x rmail, for example). */
15535 if (CHARPOS (start_pos) > BEGV
15536 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15537 {
15538 struct it it;
15539 struct glyph_row *row;
15540
15541 /* Handle the case that the window start is out of range. */
15542 if (CHARPOS (start_pos) < BEGV)
15543 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15544 else if (CHARPOS (start_pos) > ZV)
15545 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15546
15547 /* Find the start of the continued line. This should be fast
15548 because find_newline is fast (newline cache). */
15549 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15550 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15551 row, DEFAULT_FACE_ID);
15552 reseat_at_previous_visible_line_start (&it);
15553
15554 /* If the line start is "too far" away from the window start,
15555 say it takes too much time to compute a new window start.
15556 Also, give up if the line start is after point, as in that
15557 case point will not be visible with any window start we
15558 compute. */
15559 if (IT_CHARPOS (it) <= PT
15560 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15561 /* PXW: Do we need upper bounds here? */
15562 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
15563 {
15564 int min_distance, distance;
15565
15566 /* Move forward by display lines to find the new window
15567 start. If window width was enlarged, the new start can
15568 be expected to be > the old start. If window width was
15569 decreased, the new window start will be < the old start.
15570 So, we're looking for the display line start with the
15571 minimum distance from the old window start. */
15572 pos_before_pt = pos = it.current.pos;
15573 min_distance = INFINITY;
15574 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15575 distance < min_distance)
15576 {
15577 min_distance = distance;
15578 if (CHARPOS (pos) <= PT)
15579 pos_before_pt = pos;
15580 pos = it.current.pos;
15581 if (it.line_wrap == WORD_WRAP)
15582 {
15583 /* Under WORD_WRAP, move_it_by_lines is likely to
15584 overshoot and stop not at the first, but the
15585 second character from the left margin. So in
15586 that case, we need a more tight control on the X
15587 coordinate of the iterator than move_it_by_lines
15588 promises in its contract. The method is to first
15589 go to the last (rightmost) visible character of a
15590 line, then move to the leftmost character on the
15591 next line in a separate call. */
15592 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15593 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15594 move_it_to (&it, ZV, 0,
15595 it.current_y + it.max_ascent + it.max_descent, -1,
15596 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15597 }
15598 else
15599 move_it_by_lines (&it, 1);
15600 }
15601
15602 /* It makes very little sense to make the new window start
15603 after point, as point won't be visible. If that's what
15604 the loop above finds, fall back on the candidate before
15605 or at point that is closest to the old window start. */
15606 if (CHARPOS (pos) > PT)
15607 pos = pos_before_pt;
15608
15609 /* Set the window start there. */
15610 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15611 window_start_changed_p = true;
15612 }
15613 }
15614
15615 return window_start_changed_p;
15616 }
15617
15618
15619 /* Try cursor movement in case text has not changed in window WINDOW,
15620 with window start STARTP. Value is
15621
15622 CURSOR_MOVEMENT_SUCCESS if successful
15623
15624 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15625
15626 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15627 display. *SCROLL_STEP is set to true, under certain circumstances, if
15628 we want to scroll as if scroll-step were set to 1. See the code.
15629
15630 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15631 which case we have to abort this redisplay, and adjust matrices
15632 first. */
15633
15634 enum
15635 {
15636 CURSOR_MOVEMENT_SUCCESS,
15637 CURSOR_MOVEMENT_CANNOT_BE_USED,
15638 CURSOR_MOVEMENT_MUST_SCROLL,
15639 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15640 };
15641
15642 static int
15643 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15644 bool *scroll_step)
15645 {
15646 struct window *w = XWINDOW (window);
15647 struct frame *f = XFRAME (w->frame);
15648 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15649
15650 #ifdef GLYPH_DEBUG
15651 if (inhibit_try_cursor_movement)
15652 return rc;
15653 #endif
15654
15655 /* Previously, there was a check for Lisp integer in the
15656 if-statement below. Now, this field is converted to
15657 ptrdiff_t, thus zero means invalid position in a buffer. */
15658 eassert (w->last_point > 0);
15659 /* Likewise there was a check whether window_end_vpos is nil or larger
15660 than the window. Now window_end_vpos is int and so never nil, but
15661 let's leave eassert to check whether it fits in the window. */
15662 eassert (!w->window_end_valid
15663 || w->window_end_vpos < w->current_matrix->nrows);
15664
15665 /* Handle case where text has not changed, only point, and it has
15666 not moved off the frame. */
15667 if (/* Point may be in this window. */
15668 PT >= CHARPOS (startp)
15669 /* Selective display hasn't changed. */
15670 && !current_buffer->clip_changed
15671 /* Function force-mode-line-update is used to force a thorough
15672 redisplay. It sets either windows_or_buffers_changed or
15673 update_mode_lines. So don't take a shortcut here for these
15674 cases. */
15675 && !update_mode_lines
15676 && !windows_or_buffers_changed
15677 && !f->cursor_type_changed
15678 && NILP (Vshow_trailing_whitespace)
15679 /* This code is not used for mini-buffer for the sake of the case
15680 of redisplaying to replace an echo area message; since in
15681 that case the mini-buffer contents per se are usually
15682 unchanged. This code is of no real use in the mini-buffer
15683 since the handling of this_line_start_pos, etc., in redisplay
15684 handles the same cases. */
15685 && !EQ (window, minibuf_window)
15686 && (FRAME_WINDOW_P (f)
15687 || !overlay_arrow_in_current_buffer_p ()))
15688 {
15689 int this_scroll_margin, top_scroll_margin;
15690 struct glyph_row *row = NULL;
15691 int frame_line_height = default_line_pixel_height (w);
15692 int window_total_lines
15693 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15694
15695 #ifdef GLYPH_DEBUG
15696 debug_method_add (w, "cursor movement");
15697 #endif
15698
15699 /* Scroll if point within this distance from the top or bottom
15700 of the window. This is a pixel value. */
15701 if (scroll_margin > 0)
15702 {
15703 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15704 this_scroll_margin *= frame_line_height;
15705 }
15706 else
15707 this_scroll_margin = 0;
15708
15709 top_scroll_margin = this_scroll_margin;
15710 if (WINDOW_WANTS_HEADER_LINE_P (w))
15711 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15712
15713 /* Start with the row the cursor was displayed during the last
15714 not paused redisplay. Give up if that row is not valid. */
15715 if (w->last_cursor_vpos < 0
15716 || w->last_cursor_vpos >= w->current_matrix->nrows)
15717 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15718 else
15719 {
15720 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15721 if (row->mode_line_p)
15722 ++row;
15723 if (!row->enabled_p)
15724 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15725 }
15726
15727 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15728 {
15729 bool scroll_p = false, must_scroll = false;
15730 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15731
15732 if (PT > w->last_point)
15733 {
15734 /* Point has moved forward. */
15735 while (MATRIX_ROW_END_CHARPOS (row) < PT
15736 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15737 {
15738 eassert (row->enabled_p);
15739 ++row;
15740 }
15741
15742 /* If the end position of a row equals the start
15743 position of the next row, and PT is at that position,
15744 we would rather display cursor in the next line. */
15745 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15746 && MATRIX_ROW_END_CHARPOS (row) == PT
15747 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15748 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15749 && !cursor_row_p (row))
15750 ++row;
15751
15752 /* If within the scroll margin, scroll. Note that
15753 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15754 the next line would be drawn, and that
15755 this_scroll_margin can be zero. */
15756 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15757 || PT > MATRIX_ROW_END_CHARPOS (row)
15758 /* Line is completely visible last line in window
15759 and PT is to be set in the next line. */
15760 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15761 && PT == MATRIX_ROW_END_CHARPOS (row)
15762 && !row->ends_at_zv_p
15763 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15764 scroll_p = true;
15765 }
15766 else if (PT < w->last_point)
15767 {
15768 /* Cursor has to be moved backward. Note that PT >=
15769 CHARPOS (startp) because of the outer if-statement. */
15770 while (!row->mode_line_p
15771 && (MATRIX_ROW_START_CHARPOS (row) > PT
15772 || (MATRIX_ROW_START_CHARPOS (row) == PT
15773 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15774 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15775 row > w->current_matrix->rows
15776 && (row-1)->ends_in_newline_from_string_p))))
15777 && (row->y > top_scroll_margin
15778 || CHARPOS (startp) == BEGV))
15779 {
15780 eassert (row->enabled_p);
15781 --row;
15782 }
15783
15784 /* Consider the following case: Window starts at BEGV,
15785 there is invisible, intangible text at BEGV, so that
15786 display starts at some point START > BEGV. It can
15787 happen that we are called with PT somewhere between
15788 BEGV and START. Try to handle that case. */
15789 if (row < w->current_matrix->rows
15790 || row->mode_line_p)
15791 {
15792 row = w->current_matrix->rows;
15793 if (row->mode_line_p)
15794 ++row;
15795 }
15796
15797 /* Due to newlines in overlay strings, we may have to
15798 skip forward over overlay strings. */
15799 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15800 && MATRIX_ROW_END_CHARPOS (row) == PT
15801 && !cursor_row_p (row))
15802 ++row;
15803
15804 /* If within the scroll margin, scroll. */
15805 if (row->y < top_scroll_margin
15806 && CHARPOS (startp) != BEGV)
15807 scroll_p = true;
15808 }
15809 else
15810 {
15811 /* Cursor did not move. So don't scroll even if cursor line
15812 is partially visible, as it was so before. */
15813 rc = CURSOR_MOVEMENT_SUCCESS;
15814 }
15815
15816 if (PT < MATRIX_ROW_START_CHARPOS (row)
15817 || PT > MATRIX_ROW_END_CHARPOS (row))
15818 {
15819 /* if PT is not in the glyph row, give up. */
15820 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15821 must_scroll = true;
15822 }
15823 else if (rc != CURSOR_MOVEMENT_SUCCESS
15824 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15825 {
15826 struct glyph_row *row1;
15827
15828 /* If rows are bidi-reordered and point moved, back up
15829 until we find a row that does not belong to a
15830 continuation line. This is because we must consider
15831 all rows of a continued line as candidates for the
15832 new cursor positioning, since row start and end
15833 positions change non-linearly with vertical position
15834 in such rows. */
15835 /* FIXME: Revisit this when glyph ``spilling'' in
15836 continuation lines' rows is implemented for
15837 bidi-reordered rows. */
15838 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15839 MATRIX_ROW_CONTINUATION_LINE_P (row);
15840 --row)
15841 {
15842 /* If we hit the beginning of the displayed portion
15843 without finding the first row of a continued
15844 line, give up. */
15845 if (row <= row1)
15846 {
15847 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15848 break;
15849 }
15850 eassert (row->enabled_p);
15851 }
15852 }
15853 if (must_scroll)
15854 ;
15855 else if (rc != CURSOR_MOVEMENT_SUCCESS
15856 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15857 /* Make sure this isn't a header line by any chance, since
15858 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15859 && !row->mode_line_p
15860 && make_cursor_line_fully_visible_p)
15861 {
15862 if (PT == MATRIX_ROW_END_CHARPOS (row)
15863 && !row->ends_at_zv_p
15864 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15865 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15866 else if (row->height > window_box_height (w))
15867 {
15868 /* If we end up in a partially visible line, let's
15869 make it fully visible, except when it's taller
15870 than the window, in which case we can't do much
15871 about it. */
15872 *scroll_step = true;
15873 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15874 }
15875 else
15876 {
15877 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15878 if (!cursor_row_fully_visible_p (w, false, true))
15879 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15880 else
15881 rc = CURSOR_MOVEMENT_SUCCESS;
15882 }
15883 }
15884 else if (scroll_p)
15885 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15886 else if (rc != CURSOR_MOVEMENT_SUCCESS
15887 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15888 {
15889 /* With bidi-reordered rows, there could be more than
15890 one candidate row whose start and end positions
15891 occlude point. We need to let set_cursor_from_row
15892 find the best candidate. */
15893 /* FIXME: Revisit this when glyph ``spilling'' in
15894 continuation lines' rows is implemented for
15895 bidi-reordered rows. */
15896 bool rv = false;
15897
15898 do
15899 {
15900 bool at_zv_p = false, exact_match_p = false;
15901
15902 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15903 && PT <= MATRIX_ROW_END_CHARPOS (row)
15904 && cursor_row_p (row))
15905 rv |= set_cursor_from_row (w, row, w->current_matrix,
15906 0, 0, 0, 0);
15907 /* As soon as we've found the exact match for point,
15908 or the first suitable row whose ends_at_zv_p flag
15909 is set, we are done. */
15910 if (rv)
15911 {
15912 at_zv_p = MATRIX_ROW (w->current_matrix,
15913 w->cursor.vpos)->ends_at_zv_p;
15914 if (!at_zv_p
15915 && w->cursor.hpos >= 0
15916 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15917 w->cursor.vpos))
15918 {
15919 struct glyph_row *candidate =
15920 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15921 struct glyph *g =
15922 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15923 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15924
15925 exact_match_p =
15926 (BUFFERP (g->object) && g->charpos == PT)
15927 || (NILP (g->object)
15928 && (g->charpos == PT
15929 || (g->charpos == 0 && endpos - 1 == PT)));
15930 }
15931 if (at_zv_p || exact_match_p)
15932 {
15933 rc = CURSOR_MOVEMENT_SUCCESS;
15934 break;
15935 }
15936 }
15937 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15938 break;
15939 ++row;
15940 }
15941 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15942 || row->continued_p)
15943 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15944 || (MATRIX_ROW_START_CHARPOS (row) == PT
15945 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15946 /* If we didn't find any candidate rows, or exited the
15947 loop before all the candidates were examined, signal
15948 to the caller that this method failed. */
15949 if (rc != CURSOR_MOVEMENT_SUCCESS
15950 && !(rv
15951 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15952 && !row->continued_p))
15953 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15954 else if (rv)
15955 rc = CURSOR_MOVEMENT_SUCCESS;
15956 }
15957 else
15958 {
15959 do
15960 {
15961 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15962 {
15963 rc = CURSOR_MOVEMENT_SUCCESS;
15964 break;
15965 }
15966 ++row;
15967 }
15968 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15969 && MATRIX_ROW_START_CHARPOS (row) == PT
15970 && cursor_row_p (row));
15971 }
15972 }
15973 }
15974
15975 return rc;
15976 }
15977
15978
15979 void
15980 set_vertical_scroll_bar (struct window *w)
15981 {
15982 ptrdiff_t start, end, whole;
15983
15984 /* Calculate the start and end positions for the current window.
15985 At some point, it would be nice to choose between scrollbars
15986 which reflect the whole buffer size, with special markers
15987 indicating narrowing, and scrollbars which reflect only the
15988 visible region.
15989
15990 Note that mini-buffers sometimes aren't displaying any text. */
15991 if (!MINI_WINDOW_P (w)
15992 || (w == XWINDOW (minibuf_window)
15993 && NILP (echo_area_buffer[0])))
15994 {
15995 struct buffer *buf = XBUFFER (w->contents);
15996 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15997 start = marker_position (w->start) - BUF_BEGV (buf);
15998 /* I don't think this is guaranteed to be right. For the
15999 moment, we'll pretend it is. */
16000 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16001
16002 if (end < start)
16003 end = start;
16004 if (whole < (end - start))
16005 whole = end - start;
16006 }
16007 else
16008 start = end = whole = 0;
16009
16010 /* Indicate what this scroll bar ought to be displaying now. */
16011 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16012 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16013 (w, end - start, whole, start);
16014 }
16015
16016
16017 void
16018 set_horizontal_scroll_bar (struct window *w)
16019 {
16020 int start, end, whole, portion;
16021
16022 if (!MINI_WINDOW_P (w)
16023 || (w == XWINDOW (minibuf_window)
16024 && NILP (echo_area_buffer[0])))
16025 {
16026 struct buffer *b = XBUFFER (w->contents);
16027 struct buffer *old_buffer = NULL;
16028 struct it it;
16029 struct text_pos startp;
16030
16031 if (b != current_buffer)
16032 {
16033 old_buffer = current_buffer;
16034 set_buffer_internal (b);
16035 }
16036
16037 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16038 start_display (&it, w, startp);
16039 it.last_visible_x = INT_MAX;
16040 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16041 MOVE_TO_X | MOVE_TO_Y);
16042 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16043 window_box_height (w), -1,
16044 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16045
16046 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16047 end = start + window_box_width (w, TEXT_AREA);
16048 portion = end - start;
16049 /* After enlarging a horizontally scrolled window such that it
16050 gets at least as wide as the text it contains, make sure that
16051 the thumb doesn't fill the entire scroll bar so we can still
16052 drag it back to see the entire text. */
16053 whole = max (whole, end);
16054
16055 if (it.bidi_p)
16056 {
16057 Lisp_Object pdir;
16058
16059 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16060 if (EQ (pdir, Qright_to_left))
16061 {
16062 start = whole - end;
16063 end = start + portion;
16064 }
16065 }
16066
16067 if (old_buffer)
16068 set_buffer_internal (old_buffer);
16069 }
16070 else
16071 start = end = whole = portion = 0;
16072
16073 w->hscroll_whole = whole;
16074
16075 /* Indicate what this scroll bar ought to be displaying now. */
16076 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16077 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16078 (w, portion, whole, start);
16079 }
16080
16081
16082 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16083 selected_window is redisplayed.
16084
16085 We can return without actually redisplaying the window if fonts has been
16086 changed on window's frame. In that case, redisplay_internal will retry.
16087
16088 As one of the important parts of redisplaying a window, we need to
16089 decide whether the previous window-start position (stored in the
16090 window's w->start marker position) is still valid, and if it isn't,
16091 recompute it. Some details about that:
16092
16093 . The previous window-start could be in a continuation line, in
16094 which case we need to recompute it when the window width
16095 changes. See compute_window_start_on_continuation_line and its
16096 call below.
16097
16098 . The text that changed since last redisplay could include the
16099 previous window-start position. In that case, we try to salvage
16100 what we can from the current glyph matrix by calling
16101 try_scrolling, which see.
16102
16103 . Some Emacs command could force us to use a specific window-start
16104 position by setting the window's force_start flag, or gently
16105 propose doing that by setting the window's optional_new_start
16106 flag. In these cases, we try using the specified start point if
16107 that succeeds (i.e. the window desired matrix is successfully
16108 recomputed, and point location is within the window). In case
16109 of optional_new_start, we first check if the specified start
16110 position is feasible, i.e. if it will allow point to be
16111 displayed in the window. If using the specified start point
16112 fails, e.g., if new fonts are needed to be loaded, we abort the
16113 redisplay cycle and leave it up to the next cycle to figure out
16114 things.
16115
16116 . Note that the window's force_start flag is sometimes set by
16117 redisplay itself, when it decides that the previous window start
16118 point is fine and should be kept. Search for "goto force_start"
16119 below to see the details. Like the values of window-start
16120 specified outside of redisplay, these internally-deduced values
16121 are tested for feasibility, and ignored if found to be
16122 unfeasible.
16123
16124 . Note that the function try_window, used to completely redisplay
16125 a window, accepts the window's start point as its argument.
16126 This is used several times in the redisplay code to control
16127 where the window start will be, according to user options such
16128 as scroll-conservatively, and also to ensure the screen line
16129 showing point will be fully (as opposed to partially) visible on
16130 display. */
16131
16132 static void
16133 redisplay_window (Lisp_Object window, bool just_this_one_p)
16134 {
16135 struct window *w = XWINDOW (window);
16136 struct frame *f = XFRAME (w->frame);
16137 struct buffer *buffer = XBUFFER (w->contents);
16138 struct buffer *old = current_buffer;
16139 struct text_pos lpoint, opoint, startp;
16140 bool update_mode_line;
16141 int tem;
16142 struct it it;
16143 /* Record it now because it's overwritten. */
16144 bool current_matrix_up_to_date_p = false;
16145 bool used_current_matrix_p = false;
16146 /* This is less strict than current_matrix_up_to_date_p.
16147 It indicates that the buffer contents and narrowing are unchanged. */
16148 bool buffer_unchanged_p = false;
16149 bool temp_scroll_step = false;
16150 ptrdiff_t count = SPECPDL_INDEX ();
16151 int rc;
16152 int centering_position = -1;
16153 bool last_line_misfit = false;
16154 ptrdiff_t beg_unchanged, end_unchanged;
16155 int frame_line_height;
16156 bool use_desired_matrix;
16157
16158 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16159 opoint = lpoint;
16160
16161 #ifdef GLYPH_DEBUG
16162 *w->desired_matrix->method = 0;
16163 #endif
16164
16165 if (!just_this_one_p
16166 && REDISPLAY_SOME_P ()
16167 && !w->redisplay
16168 && !w->update_mode_line
16169 && !f->face_change
16170 && !f->redisplay
16171 && !buffer->text->redisplay
16172 && BUF_PT (buffer) == w->last_point)
16173 return;
16174
16175 /* Make sure that both W's markers are valid. */
16176 eassert (XMARKER (w->start)->buffer == buffer);
16177 eassert (XMARKER (w->pointm)->buffer == buffer);
16178
16179 /* We come here again if we need to run window-text-change-functions
16180 below. */
16181 restart:
16182 reconsider_clip_changes (w);
16183 frame_line_height = default_line_pixel_height (w);
16184
16185 /* Has the mode line to be updated? */
16186 update_mode_line = (w->update_mode_line
16187 || update_mode_lines
16188 || buffer->clip_changed
16189 || buffer->prevent_redisplay_optimizations_p);
16190
16191 if (!just_this_one_p)
16192 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16193 cleverly elsewhere. */
16194 w->must_be_updated_p = true;
16195
16196 if (MINI_WINDOW_P (w))
16197 {
16198 if (w == XWINDOW (echo_area_window)
16199 && !NILP (echo_area_buffer[0]))
16200 {
16201 if (update_mode_line)
16202 /* We may have to update a tty frame's menu bar or a
16203 tool-bar. Example `M-x C-h C-h C-g'. */
16204 goto finish_menu_bars;
16205 else
16206 /* We've already displayed the echo area glyphs in this window. */
16207 goto finish_scroll_bars;
16208 }
16209 else if ((w != XWINDOW (minibuf_window)
16210 || minibuf_level == 0)
16211 /* When buffer is nonempty, redisplay window normally. */
16212 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16213 /* Quail displays non-mini buffers in minibuffer window.
16214 In that case, redisplay the window normally. */
16215 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16216 {
16217 /* W is a mini-buffer window, but it's not active, so clear
16218 it. */
16219 int yb = window_text_bottom_y (w);
16220 struct glyph_row *row;
16221 int y;
16222
16223 for (y = 0, row = w->desired_matrix->rows;
16224 y < yb;
16225 y += row->height, ++row)
16226 blank_row (w, row, y);
16227 goto finish_scroll_bars;
16228 }
16229
16230 clear_glyph_matrix (w->desired_matrix);
16231 }
16232
16233 /* Otherwise set up data on this window; select its buffer and point
16234 value. */
16235 /* Really select the buffer, for the sake of buffer-local
16236 variables. */
16237 set_buffer_internal_1 (XBUFFER (w->contents));
16238
16239 current_matrix_up_to_date_p
16240 = (w->window_end_valid
16241 && !current_buffer->clip_changed
16242 && !current_buffer->prevent_redisplay_optimizations_p
16243 && !window_outdated (w));
16244
16245 /* Run the window-text-change-functions
16246 if it is possible that the text on the screen has changed
16247 (either due to modification of the text, or any other reason). */
16248 if (!current_matrix_up_to_date_p
16249 && !NILP (Vwindow_text_change_functions))
16250 {
16251 safe_run_hooks (Qwindow_text_change_functions);
16252 goto restart;
16253 }
16254
16255 beg_unchanged = BEG_UNCHANGED;
16256 end_unchanged = END_UNCHANGED;
16257
16258 SET_TEXT_POS (opoint, PT, PT_BYTE);
16259
16260 specbind (Qinhibit_point_motion_hooks, Qt);
16261
16262 buffer_unchanged_p
16263 = (w->window_end_valid
16264 && !current_buffer->clip_changed
16265 && !window_outdated (w));
16266
16267 /* When windows_or_buffers_changed is non-zero, we can't rely
16268 on the window end being valid, so set it to zero there. */
16269 if (windows_or_buffers_changed)
16270 {
16271 /* If window starts on a continuation line, maybe adjust the
16272 window start in case the window's width changed. */
16273 if (XMARKER (w->start)->buffer == current_buffer)
16274 compute_window_start_on_continuation_line (w);
16275
16276 w->window_end_valid = false;
16277 /* If so, we also can't rely on current matrix
16278 and should not fool try_cursor_movement below. */
16279 current_matrix_up_to_date_p = false;
16280 }
16281
16282 /* Some sanity checks. */
16283 CHECK_WINDOW_END (w);
16284 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16285 emacs_abort ();
16286 if (BYTEPOS (opoint) < CHARPOS (opoint))
16287 emacs_abort ();
16288
16289 if (mode_line_update_needed (w))
16290 update_mode_line = true;
16291
16292 /* Point refers normally to the selected window. For any other
16293 window, set up appropriate value. */
16294 if (!EQ (window, selected_window))
16295 {
16296 ptrdiff_t new_pt = marker_position (w->pointm);
16297 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16298
16299 if (new_pt < BEGV)
16300 {
16301 new_pt = BEGV;
16302 new_pt_byte = BEGV_BYTE;
16303 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16304 }
16305 else if (new_pt > (ZV - 1))
16306 {
16307 new_pt = ZV;
16308 new_pt_byte = ZV_BYTE;
16309 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16310 }
16311
16312 /* We don't use SET_PT so that the point-motion hooks don't run. */
16313 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16314 }
16315
16316 /* If any of the character widths specified in the display table
16317 have changed, invalidate the width run cache. It's true that
16318 this may be a bit late to catch such changes, but the rest of
16319 redisplay goes (non-fatally) haywire when the display table is
16320 changed, so why should we worry about doing any better? */
16321 if (current_buffer->width_run_cache
16322 || (current_buffer->base_buffer
16323 && current_buffer->base_buffer->width_run_cache))
16324 {
16325 struct Lisp_Char_Table *disptab = buffer_display_table ();
16326
16327 if (! disptab_matches_widthtab
16328 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16329 {
16330 struct buffer *buf = current_buffer;
16331
16332 if (buf->base_buffer)
16333 buf = buf->base_buffer;
16334 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16335 recompute_width_table (current_buffer, disptab);
16336 }
16337 }
16338
16339 /* If window-start is screwed up, choose a new one. */
16340 if (XMARKER (w->start)->buffer != current_buffer)
16341 goto recenter;
16342
16343 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16344
16345 /* If someone specified a new starting point but did not insist,
16346 check whether it can be used. */
16347 if ((w->optional_new_start || window_frozen_p (w))
16348 && CHARPOS (startp) >= BEGV
16349 && CHARPOS (startp) <= ZV)
16350 {
16351 ptrdiff_t it_charpos;
16352
16353 w->optional_new_start = false;
16354 start_display (&it, w, startp);
16355 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16356 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16357 /* Record IT's position now, since line_bottom_y might change
16358 that. */
16359 it_charpos = IT_CHARPOS (it);
16360 /* Make sure we set the force_start flag only if the cursor row
16361 will be fully visible. Otherwise, the code under force_start
16362 label below will try to move point back into view, which is
16363 not what the code which sets optional_new_start wants. */
16364 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16365 && !w->force_start)
16366 {
16367 if (it_charpos == PT)
16368 w->force_start = true;
16369 /* IT may overshoot PT if text at PT is invisible. */
16370 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16371 w->force_start = true;
16372 #ifdef GLYPH_DEBUG
16373 if (w->force_start)
16374 {
16375 if (window_frozen_p (w))
16376 debug_method_add (w, "set force_start from frozen window start");
16377 else
16378 debug_method_add (w, "set force_start from optional_new_start");
16379 }
16380 #endif
16381 }
16382 }
16383
16384 force_start:
16385
16386 /* Handle case where place to start displaying has been specified,
16387 unless the specified location is outside the accessible range. */
16388 if (w->force_start)
16389 {
16390 /* We set this later on if we have to adjust point. */
16391 int new_vpos = -1;
16392
16393 w->force_start = false;
16394 w->vscroll = 0;
16395 w->window_end_valid = false;
16396
16397 /* Forget any recorded base line for line number display. */
16398 if (!buffer_unchanged_p)
16399 w->base_line_number = 0;
16400
16401 /* Redisplay the mode line. Select the buffer properly for that.
16402 Also, run the hook window-scroll-functions
16403 because we have scrolled. */
16404 /* Note, we do this after clearing force_start because
16405 if there's an error, it is better to forget about force_start
16406 than to get into an infinite loop calling the hook functions
16407 and having them get more errors. */
16408 if (!update_mode_line
16409 || ! NILP (Vwindow_scroll_functions))
16410 {
16411 update_mode_line = true;
16412 w->update_mode_line = true;
16413 startp = run_window_scroll_functions (window, startp);
16414 }
16415
16416 if (CHARPOS (startp) < BEGV)
16417 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16418 else if (CHARPOS (startp) > ZV)
16419 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16420
16421 /* Redisplay, then check if cursor has been set during the
16422 redisplay. Give up if new fonts were loaded. */
16423 /* We used to issue a CHECK_MARGINS argument to try_window here,
16424 but this causes scrolling to fail when point begins inside
16425 the scroll margin (bug#148) -- cyd */
16426 if (!try_window (window, startp, 0))
16427 {
16428 w->force_start = true;
16429 clear_glyph_matrix (w->desired_matrix);
16430 goto need_larger_matrices;
16431 }
16432
16433 if (w->cursor.vpos < 0)
16434 {
16435 /* If point does not appear, try to move point so it does
16436 appear. The desired matrix has been built above, so we
16437 can use it here. First see if point is in invisible
16438 text, and if so, move it to the first visible buffer
16439 position past that. */
16440 struct glyph_row *r = NULL;
16441 Lisp_Object invprop =
16442 get_char_property_and_overlay (make_number (PT), Qinvisible,
16443 Qnil, NULL);
16444
16445 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16446 {
16447 ptrdiff_t alt_pt;
16448 Lisp_Object invprop_end =
16449 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16450 Qnil, Qnil);
16451
16452 if (NATNUMP (invprop_end))
16453 alt_pt = XFASTINT (invprop_end);
16454 else
16455 alt_pt = ZV;
16456 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16457 NULL, 0);
16458 }
16459 if (r)
16460 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16461 else /* Give up and just move to the middle of the window. */
16462 new_vpos = window_box_height (w) / 2;
16463 }
16464
16465 if (!cursor_row_fully_visible_p (w, false, false))
16466 {
16467 /* Point does appear, but on a line partly visible at end of window.
16468 Move it back to a fully-visible line. */
16469 new_vpos = window_box_height (w);
16470 /* But if window_box_height suggests a Y coordinate that is
16471 not less than we already have, that line will clearly not
16472 be fully visible, so give up and scroll the display.
16473 This can happen when the default face uses a font whose
16474 dimensions are different from the frame's default
16475 font. */
16476 if (new_vpos >= w->cursor.y)
16477 {
16478 w->cursor.vpos = -1;
16479 clear_glyph_matrix (w->desired_matrix);
16480 goto try_to_scroll;
16481 }
16482 }
16483 else if (w->cursor.vpos >= 0)
16484 {
16485 /* Some people insist on not letting point enter the scroll
16486 margin, even though this part handles windows that didn't
16487 scroll at all. */
16488 int window_total_lines
16489 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16490 int margin = min (scroll_margin, window_total_lines / 4);
16491 int pixel_margin = margin * frame_line_height;
16492 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16493
16494 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16495 below, which finds the row to move point to, advances by
16496 the Y coordinate of the _next_ row, see the definition of
16497 MATRIX_ROW_BOTTOM_Y. */
16498 if (w->cursor.vpos < margin + header_line)
16499 {
16500 w->cursor.vpos = -1;
16501 clear_glyph_matrix (w->desired_matrix);
16502 goto try_to_scroll;
16503 }
16504 else
16505 {
16506 int window_height = window_box_height (w);
16507
16508 if (header_line)
16509 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16510 if (w->cursor.y >= window_height - pixel_margin)
16511 {
16512 w->cursor.vpos = -1;
16513 clear_glyph_matrix (w->desired_matrix);
16514 goto try_to_scroll;
16515 }
16516 }
16517 }
16518
16519 /* If we need to move point for either of the above reasons,
16520 now actually do it. */
16521 if (new_vpos >= 0)
16522 {
16523 struct glyph_row *row;
16524
16525 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16526 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16527 ++row;
16528
16529 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16530 MATRIX_ROW_START_BYTEPOS (row));
16531
16532 if (w != XWINDOW (selected_window))
16533 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16534 else if (current_buffer == old)
16535 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16536
16537 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16538
16539 /* Re-run pre-redisplay-function so it can update the region
16540 according to the new position of point. */
16541 /* Other than the cursor, w's redisplay is done so we can set its
16542 redisplay to false. Also the buffer's redisplay can be set to
16543 false, since propagate_buffer_redisplay should have already
16544 propagated its info to `w' anyway. */
16545 w->redisplay = false;
16546 XBUFFER (w->contents)->text->redisplay = false;
16547 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16548
16549 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16550 {
16551 /* pre-redisplay-function made changes (e.g. move the region)
16552 that require another round of redisplay. */
16553 clear_glyph_matrix (w->desired_matrix);
16554 if (!try_window (window, startp, 0))
16555 goto need_larger_matrices;
16556 }
16557 }
16558 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16559 {
16560 clear_glyph_matrix (w->desired_matrix);
16561 goto try_to_scroll;
16562 }
16563
16564 #ifdef GLYPH_DEBUG
16565 debug_method_add (w, "forced window start");
16566 #endif
16567 goto done;
16568 }
16569
16570 /* Handle case where text has not changed, only point, and it has
16571 not moved off the frame, and we are not retrying after hscroll.
16572 (current_matrix_up_to_date_p is true when retrying.) */
16573 if (current_matrix_up_to_date_p
16574 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16575 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16576 {
16577 switch (rc)
16578 {
16579 case CURSOR_MOVEMENT_SUCCESS:
16580 used_current_matrix_p = true;
16581 goto done;
16582
16583 case CURSOR_MOVEMENT_MUST_SCROLL:
16584 goto try_to_scroll;
16585
16586 default:
16587 emacs_abort ();
16588 }
16589 }
16590 /* If current starting point was originally the beginning of a line
16591 but no longer is, find a new starting point. */
16592 else if (w->start_at_line_beg
16593 && !(CHARPOS (startp) <= BEGV
16594 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16595 {
16596 #ifdef GLYPH_DEBUG
16597 debug_method_add (w, "recenter 1");
16598 #endif
16599 goto recenter;
16600 }
16601
16602 /* Try scrolling with try_window_id. Value is > 0 if update has
16603 been done, it is -1 if we know that the same window start will
16604 not work. It is 0 if unsuccessful for some other reason. */
16605 else if ((tem = try_window_id (w)) != 0)
16606 {
16607 #ifdef GLYPH_DEBUG
16608 debug_method_add (w, "try_window_id %d", tem);
16609 #endif
16610
16611 if (f->fonts_changed)
16612 goto need_larger_matrices;
16613 if (tem > 0)
16614 goto done;
16615
16616 /* Otherwise try_window_id has returned -1 which means that we
16617 don't want the alternative below this comment to execute. */
16618 }
16619 else if (CHARPOS (startp) >= BEGV
16620 && CHARPOS (startp) <= ZV
16621 && PT >= CHARPOS (startp)
16622 && (CHARPOS (startp) < ZV
16623 /* Avoid starting at end of buffer. */
16624 || CHARPOS (startp) == BEGV
16625 || !window_outdated (w)))
16626 {
16627 int d1, d2, d5, d6;
16628 int rtop, rbot;
16629
16630 /* If first window line is a continuation line, and window start
16631 is inside the modified region, but the first change is before
16632 current window start, we must select a new window start.
16633
16634 However, if this is the result of a down-mouse event (e.g. by
16635 extending the mouse-drag-overlay), we don't want to select a
16636 new window start, since that would change the position under
16637 the mouse, resulting in an unwanted mouse-movement rather
16638 than a simple mouse-click. */
16639 if (!w->start_at_line_beg
16640 && NILP (do_mouse_tracking)
16641 && CHARPOS (startp) > BEGV
16642 && CHARPOS (startp) > BEG + beg_unchanged
16643 && CHARPOS (startp) <= Z - end_unchanged
16644 /* Even if w->start_at_line_beg is nil, a new window may
16645 start at a line_beg, since that's how set_buffer_window
16646 sets it. So, we need to check the return value of
16647 compute_window_start_on_continuation_line. (See also
16648 bug#197). */
16649 && XMARKER (w->start)->buffer == current_buffer
16650 && compute_window_start_on_continuation_line (w)
16651 /* It doesn't make sense to force the window start like we
16652 do at label force_start if it is already known that point
16653 will not be fully visible in the resulting window, because
16654 doing so will move point from its correct position
16655 instead of scrolling the window to bring point into view.
16656 See bug#9324. */
16657 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16658 /* A very tall row could need more than the window height,
16659 in which case we accept that it is partially visible. */
16660 && (rtop != 0) == (rbot != 0))
16661 {
16662 w->force_start = true;
16663 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16664 #ifdef GLYPH_DEBUG
16665 debug_method_add (w, "recomputed window start in continuation line");
16666 #endif
16667 goto force_start;
16668 }
16669
16670 #ifdef GLYPH_DEBUG
16671 debug_method_add (w, "same window start");
16672 #endif
16673
16674 /* Try to redisplay starting at same place as before.
16675 If point has not moved off frame, accept the results. */
16676 if (!current_matrix_up_to_date_p
16677 /* Don't use try_window_reusing_current_matrix in this case
16678 because a window scroll function can have changed the
16679 buffer. */
16680 || !NILP (Vwindow_scroll_functions)
16681 || MINI_WINDOW_P (w)
16682 || !(used_current_matrix_p
16683 = try_window_reusing_current_matrix (w)))
16684 {
16685 IF_DEBUG (debug_method_add (w, "1"));
16686 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16687 /* -1 means we need to scroll.
16688 0 means we need new matrices, but fonts_changed
16689 is set in that case, so we will detect it below. */
16690 goto try_to_scroll;
16691 }
16692
16693 if (f->fonts_changed)
16694 goto need_larger_matrices;
16695
16696 if (w->cursor.vpos >= 0)
16697 {
16698 if (!just_this_one_p
16699 || current_buffer->clip_changed
16700 || BEG_UNCHANGED < CHARPOS (startp))
16701 /* Forget any recorded base line for line number display. */
16702 w->base_line_number = 0;
16703
16704 if (!cursor_row_fully_visible_p (w, true, false))
16705 {
16706 clear_glyph_matrix (w->desired_matrix);
16707 last_line_misfit = true;
16708 }
16709 /* Drop through and scroll. */
16710 else
16711 goto done;
16712 }
16713 else
16714 clear_glyph_matrix (w->desired_matrix);
16715 }
16716
16717 try_to_scroll:
16718
16719 /* Redisplay the mode line. Select the buffer properly for that. */
16720 if (!update_mode_line)
16721 {
16722 update_mode_line = true;
16723 w->update_mode_line = true;
16724 }
16725
16726 /* Try to scroll by specified few lines. */
16727 if ((scroll_conservatively
16728 || emacs_scroll_step
16729 || temp_scroll_step
16730 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16731 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16732 && CHARPOS (startp) >= BEGV
16733 && CHARPOS (startp) <= ZV)
16734 {
16735 /* The function returns -1 if new fonts were loaded, 1 if
16736 successful, 0 if not successful. */
16737 int ss = try_scrolling (window, just_this_one_p,
16738 scroll_conservatively,
16739 emacs_scroll_step,
16740 temp_scroll_step, last_line_misfit);
16741 switch (ss)
16742 {
16743 case SCROLLING_SUCCESS:
16744 goto done;
16745
16746 case SCROLLING_NEED_LARGER_MATRICES:
16747 goto need_larger_matrices;
16748
16749 case SCROLLING_FAILED:
16750 break;
16751
16752 default:
16753 emacs_abort ();
16754 }
16755 }
16756
16757 /* Finally, just choose a place to start which positions point
16758 according to user preferences. */
16759
16760 recenter:
16761
16762 #ifdef GLYPH_DEBUG
16763 debug_method_add (w, "recenter");
16764 #endif
16765
16766 /* Forget any previously recorded base line for line number display. */
16767 if (!buffer_unchanged_p)
16768 w->base_line_number = 0;
16769
16770 /* Determine the window start relative to point. */
16771 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16772 it.current_y = it.last_visible_y;
16773 if (centering_position < 0)
16774 {
16775 int window_total_lines
16776 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16777 int margin
16778 = scroll_margin > 0
16779 ? min (scroll_margin, window_total_lines / 4)
16780 : 0;
16781 ptrdiff_t margin_pos = CHARPOS (startp);
16782 Lisp_Object aggressive;
16783 bool scrolling_up;
16784
16785 /* If there is a scroll margin at the top of the window, find
16786 its character position. */
16787 if (margin
16788 /* Cannot call start_display if startp is not in the
16789 accessible region of the buffer. This can happen when we
16790 have just switched to a different buffer and/or changed
16791 its restriction. In that case, startp is initialized to
16792 the character position 1 (BEGV) because we did not yet
16793 have chance to display the buffer even once. */
16794 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16795 {
16796 struct it it1;
16797 void *it1data = NULL;
16798
16799 SAVE_IT (it1, it, it1data);
16800 start_display (&it1, w, startp);
16801 move_it_vertically (&it1, margin * frame_line_height);
16802 margin_pos = IT_CHARPOS (it1);
16803 RESTORE_IT (&it, &it, it1data);
16804 }
16805 scrolling_up = PT > margin_pos;
16806 aggressive =
16807 scrolling_up
16808 ? BVAR (current_buffer, scroll_up_aggressively)
16809 : BVAR (current_buffer, scroll_down_aggressively);
16810
16811 if (!MINI_WINDOW_P (w)
16812 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16813 {
16814 int pt_offset = 0;
16815
16816 /* Setting scroll-conservatively overrides
16817 scroll-*-aggressively. */
16818 if (!scroll_conservatively && NUMBERP (aggressive))
16819 {
16820 double float_amount = XFLOATINT (aggressive);
16821
16822 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16823 if (pt_offset == 0 && float_amount > 0)
16824 pt_offset = 1;
16825 if (pt_offset && margin > 0)
16826 margin -= 1;
16827 }
16828 /* Compute how much to move the window start backward from
16829 point so that point will be displayed where the user
16830 wants it. */
16831 if (scrolling_up)
16832 {
16833 centering_position = it.last_visible_y;
16834 if (pt_offset)
16835 centering_position -= pt_offset;
16836 centering_position -=
16837 (frame_line_height * (1 + margin + last_line_misfit)
16838 + WINDOW_HEADER_LINE_HEIGHT (w));
16839 /* Don't let point enter the scroll margin near top of
16840 the window. */
16841 if (centering_position < margin * frame_line_height)
16842 centering_position = margin * frame_line_height;
16843 }
16844 else
16845 centering_position = margin * frame_line_height + pt_offset;
16846 }
16847 else
16848 /* Set the window start half the height of the window backward
16849 from point. */
16850 centering_position = window_box_height (w) / 2;
16851 }
16852 move_it_vertically_backward (&it, centering_position);
16853
16854 eassert (IT_CHARPOS (it) >= BEGV);
16855
16856 /* The function move_it_vertically_backward may move over more
16857 than the specified y-distance. If it->w is small, e.g. a
16858 mini-buffer window, we may end up in front of the window's
16859 display area. Start displaying at the start of the line
16860 containing PT in this case. */
16861 if (it.current_y <= 0)
16862 {
16863 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16864 move_it_vertically_backward (&it, 0);
16865 it.current_y = 0;
16866 }
16867
16868 it.current_x = it.hpos = 0;
16869
16870 /* Set the window start position here explicitly, to avoid an
16871 infinite loop in case the functions in window-scroll-functions
16872 get errors. */
16873 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16874
16875 /* Run scroll hooks. */
16876 startp = run_window_scroll_functions (window, it.current.pos);
16877
16878 /* Redisplay the window. */
16879 use_desired_matrix = false;
16880 if (!current_matrix_up_to_date_p
16881 || windows_or_buffers_changed
16882 || f->cursor_type_changed
16883 /* Don't use try_window_reusing_current_matrix in this case
16884 because it can have changed the buffer. */
16885 || !NILP (Vwindow_scroll_functions)
16886 || !just_this_one_p
16887 || MINI_WINDOW_P (w)
16888 || !(used_current_matrix_p
16889 = try_window_reusing_current_matrix (w)))
16890 use_desired_matrix = (try_window (window, startp, 0) == 1);
16891
16892 /* If new fonts have been loaded (due to fontsets), give up. We
16893 have to start a new redisplay since we need to re-adjust glyph
16894 matrices. */
16895 if (f->fonts_changed)
16896 goto need_larger_matrices;
16897
16898 /* If cursor did not appear assume that the middle of the window is
16899 in the first line of the window. Do it again with the next line.
16900 (Imagine a window of height 100, displaying two lines of height
16901 60. Moving back 50 from it->last_visible_y will end in the first
16902 line.) */
16903 if (w->cursor.vpos < 0)
16904 {
16905 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16906 {
16907 clear_glyph_matrix (w->desired_matrix);
16908 move_it_by_lines (&it, 1);
16909 try_window (window, it.current.pos, 0);
16910 }
16911 else if (PT < IT_CHARPOS (it))
16912 {
16913 clear_glyph_matrix (w->desired_matrix);
16914 move_it_by_lines (&it, -1);
16915 try_window (window, it.current.pos, 0);
16916 }
16917 else
16918 {
16919 /* Not much we can do about it. */
16920 }
16921 }
16922
16923 /* Consider the following case: Window starts at BEGV, there is
16924 invisible, intangible text at BEGV, so that display starts at
16925 some point START > BEGV. It can happen that we are called with
16926 PT somewhere between BEGV and START. Try to handle that case,
16927 and similar ones. */
16928 if (w->cursor.vpos < 0)
16929 {
16930 /* Prefer the desired matrix to the current matrix, if possible,
16931 in the fallback calculations below. This is because using
16932 the current matrix might completely goof, e.g. if its first
16933 row is after point. */
16934 struct glyph_matrix *matrix =
16935 use_desired_matrix ? w->desired_matrix : w->current_matrix;
16936 /* First, try locating the proper glyph row for PT. */
16937 struct glyph_row *row =
16938 row_containing_pos (w, PT, matrix->rows, NULL, 0);
16939
16940 /* Sometimes point is at the beginning of invisible text that is
16941 before the 1st character displayed in the row. In that case,
16942 row_containing_pos fails to find the row, because no glyphs
16943 with appropriate buffer positions are present in the row.
16944 Therefore, we next try to find the row which shows the 1st
16945 position after the invisible text. */
16946 if (!row)
16947 {
16948 Lisp_Object val =
16949 get_char_property_and_overlay (make_number (PT), Qinvisible,
16950 Qnil, NULL);
16951
16952 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
16953 {
16954 ptrdiff_t alt_pos;
16955 Lisp_Object invis_end =
16956 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16957 Qnil, Qnil);
16958
16959 if (NATNUMP (invis_end))
16960 alt_pos = XFASTINT (invis_end);
16961 else
16962 alt_pos = ZV;
16963 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
16964 }
16965 }
16966 /* Finally, fall back on the first row of the window after the
16967 header line (if any). This is slightly better than not
16968 displaying the cursor at all. */
16969 if (!row)
16970 {
16971 row = matrix->rows;
16972 if (row->mode_line_p)
16973 ++row;
16974 }
16975 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
16976 }
16977
16978 if (!cursor_row_fully_visible_p (w, false, false))
16979 {
16980 /* If vscroll is enabled, disable it and try again. */
16981 if (w->vscroll)
16982 {
16983 w->vscroll = 0;
16984 clear_glyph_matrix (w->desired_matrix);
16985 goto recenter;
16986 }
16987
16988 /* Users who set scroll-conservatively to a large number want
16989 point just above/below the scroll margin. If we ended up
16990 with point's row partially visible, move the window start to
16991 make that row fully visible and out of the margin. */
16992 if (scroll_conservatively > SCROLL_LIMIT)
16993 {
16994 int window_total_lines
16995 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16996 int margin =
16997 scroll_margin > 0
16998 ? min (scroll_margin, window_total_lines / 4)
16999 : 0;
17000 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17001
17002 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17003 clear_glyph_matrix (w->desired_matrix);
17004 if (1 == try_window (window, it.current.pos,
17005 TRY_WINDOW_CHECK_MARGINS))
17006 goto done;
17007 }
17008
17009 /* If centering point failed to make the whole line visible,
17010 put point at the top instead. That has to make the whole line
17011 visible, if it can be done. */
17012 if (centering_position == 0)
17013 goto done;
17014
17015 clear_glyph_matrix (w->desired_matrix);
17016 centering_position = 0;
17017 goto recenter;
17018 }
17019
17020 done:
17021
17022 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17023 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17024 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17025
17026 /* Display the mode line, if we must. */
17027 if ((update_mode_line
17028 /* If window not full width, must redo its mode line
17029 if (a) the window to its side is being redone and
17030 (b) we do a frame-based redisplay. This is a consequence
17031 of how inverted lines are drawn in frame-based redisplay. */
17032 || (!just_this_one_p
17033 && !FRAME_WINDOW_P (f)
17034 && !WINDOW_FULL_WIDTH_P (w))
17035 /* Line number to display. */
17036 || w->base_line_pos > 0
17037 /* Column number is displayed and different from the one displayed. */
17038 || (w->column_number_displayed != -1
17039 && (w->column_number_displayed != current_column ())))
17040 /* This means that the window has a mode line. */
17041 && (WINDOW_WANTS_MODELINE_P (w)
17042 || WINDOW_WANTS_HEADER_LINE_P (w)))
17043 {
17044
17045 display_mode_lines (w);
17046
17047 /* If mode line height has changed, arrange for a thorough
17048 immediate redisplay using the correct mode line height. */
17049 if (WINDOW_WANTS_MODELINE_P (w)
17050 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17051 {
17052 f->fonts_changed = true;
17053 w->mode_line_height = -1;
17054 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17055 = DESIRED_MODE_LINE_HEIGHT (w);
17056 }
17057
17058 /* If header line height has changed, arrange for a thorough
17059 immediate redisplay using the correct header line height. */
17060 if (WINDOW_WANTS_HEADER_LINE_P (w)
17061 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17062 {
17063 f->fonts_changed = true;
17064 w->header_line_height = -1;
17065 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17066 = DESIRED_HEADER_LINE_HEIGHT (w);
17067 }
17068
17069 if (f->fonts_changed)
17070 goto need_larger_matrices;
17071 }
17072
17073 if (!line_number_displayed && w->base_line_pos != -1)
17074 {
17075 w->base_line_pos = 0;
17076 w->base_line_number = 0;
17077 }
17078
17079 finish_menu_bars:
17080
17081 /* When we reach a frame's selected window, redo the frame's menu
17082 bar and the frame's title. */
17083 if (update_mode_line
17084 && EQ (FRAME_SELECTED_WINDOW (f), window))
17085 {
17086 bool redisplay_menu_p;
17087
17088 if (FRAME_WINDOW_P (f))
17089 {
17090 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17091 || defined (HAVE_NS) || defined (USE_GTK)
17092 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17093 #else
17094 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17095 #endif
17096 }
17097 else
17098 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17099
17100 if (redisplay_menu_p)
17101 display_menu_bar (w);
17102
17103 #ifdef HAVE_WINDOW_SYSTEM
17104 if (FRAME_WINDOW_P (f))
17105 {
17106 #if defined (USE_GTK) || defined (HAVE_NS)
17107 if (FRAME_EXTERNAL_TOOL_BAR (f))
17108 redisplay_tool_bar (f);
17109 #else
17110 if (WINDOWP (f->tool_bar_window)
17111 && (FRAME_TOOL_BAR_LINES (f) > 0
17112 || !NILP (Vauto_resize_tool_bars))
17113 && redisplay_tool_bar (f))
17114 ignore_mouse_drag_p = true;
17115 #endif
17116 }
17117 ptrdiff_t count1 = SPECPDL_INDEX ();
17118 /* x_consider_frame_title calls select-frame, which calls
17119 resize_mini_window, which could resize the mini-window and by
17120 that undo the effect of this redisplay cycle wrt minibuffer
17121 and echo-area display. Binding inhibit-redisplay to t makes
17122 the call to resize_mini_window a no-op, thus avoiding the
17123 adverse side effects. */
17124 specbind (Qinhibit_redisplay, Qt);
17125 x_consider_frame_title (w->frame);
17126 unbind_to (count1, Qnil);
17127 #endif
17128 }
17129
17130 #ifdef HAVE_WINDOW_SYSTEM
17131 if (FRAME_WINDOW_P (f)
17132 && update_window_fringes (w, (just_this_one_p
17133 || (!used_current_matrix_p && !overlay_arrow_seen)
17134 || w->pseudo_window_p)))
17135 {
17136 update_begin (f);
17137 block_input ();
17138 if (draw_window_fringes (w, true))
17139 {
17140 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17141 x_draw_right_divider (w);
17142 else
17143 x_draw_vertical_border (w);
17144 }
17145 unblock_input ();
17146 update_end (f);
17147 }
17148
17149 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17150 x_draw_bottom_divider (w);
17151 #endif /* HAVE_WINDOW_SYSTEM */
17152
17153 /* We go to this label, with fonts_changed set, if it is
17154 necessary to try again using larger glyph matrices.
17155 We have to redeem the scroll bar even in this case,
17156 because the loop in redisplay_internal expects that. */
17157 need_larger_matrices:
17158 ;
17159 finish_scroll_bars:
17160
17161 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17162 {
17163 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17164 /* Set the thumb's position and size. */
17165 set_vertical_scroll_bar (w);
17166
17167 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17168 /* Set the thumb's position and size. */
17169 set_horizontal_scroll_bar (w);
17170
17171 /* Note that we actually used the scroll bar attached to this
17172 window, so it shouldn't be deleted at the end of redisplay. */
17173 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17174 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17175 }
17176
17177 /* Restore current_buffer and value of point in it. The window
17178 update may have changed the buffer, so first make sure `opoint'
17179 is still valid (Bug#6177). */
17180 if (CHARPOS (opoint) < BEGV)
17181 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17182 else if (CHARPOS (opoint) > ZV)
17183 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17184 else
17185 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17186
17187 set_buffer_internal_1 (old);
17188 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17189 shorter. This can be caused by log truncation in *Messages*. */
17190 if (CHARPOS (lpoint) <= ZV)
17191 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17192
17193 unbind_to (count, Qnil);
17194 }
17195
17196
17197 /* Build the complete desired matrix of WINDOW with a window start
17198 buffer position POS.
17199
17200 Value is 1 if successful. It is zero if fonts were loaded during
17201 redisplay which makes re-adjusting glyph matrices necessary, and -1
17202 if point would appear in the scroll margins.
17203 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17204 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17205 set in FLAGS.) */
17206
17207 int
17208 try_window (Lisp_Object window, struct text_pos pos, int flags)
17209 {
17210 struct window *w = XWINDOW (window);
17211 struct it it;
17212 struct glyph_row *last_text_row = NULL;
17213 struct frame *f = XFRAME (w->frame);
17214 int frame_line_height = default_line_pixel_height (w);
17215
17216 /* Make POS the new window start. */
17217 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17218
17219 /* Mark cursor position as unknown. No overlay arrow seen. */
17220 w->cursor.vpos = -1;
17221 overlay_arrow_seen = false;
17222
17223 /* Initialize iterator and info to start at POS. */
17224 start_display (&it, w, pos);
17225 it.glyph_row->reversed_p = false;
17226
17227 /* Display all lines of W. */
17228 while (it.current_y < it.last_visible_y)
17229 {
17230 if (display_line (&it))
17231 last_text_row = it.glyph_row - 1;
17232 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17233 return 0;
17234 }
17235
17236 /* Don't let the cursor end in the scroll margins. */
17237 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17238 && !MINI_WINDOW_P (w))
17239 {
17240 int this_scroll_margin;
17241 int window_total_lines
17242 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17243
17244 if (scroll_margin > 0)
17245 {
17246 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
17247 this_scroll_margin *= frame_line_height;
17248 }
17249 else
17250 this_scroll_margin = 0;
17251
17252 if ((w->cursor.y >= 0 /* not vscrolled */
17253 && w->cursor.y < this_scroll_margin
17254 && CHARPOS (pos) > BEGV
17255 && IT_CHARPOS (it) < ZV)
17256 /* rms: considering make_cursor_line_fully_visible_p here
17257 seems to give wrong results. We don't want to recenter
17258 when the last line is partly visible, we want to allow
17259 that case to be handled in the usual way. */
17260 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
17261 {
17262 w->cursor.vpos = -1;
17263 clear_glyph_matrix (w->desired_matrix);
17264 return -1;
17265 }
17266 }
17267
17268 /* If bottom moved off end of frame, change mode line percentage. */
17269 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
17270 w->update_mode_line = true;
17271
17272 /* Set window_end_pos to the offset of the last character displayed
17273 on the window from the end of current_buffer. Set
17274 window_end_vpos to its row number. */
17275 if (last_text_row)
17276 {
17277 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17278 adjust_window_ends (w, last_text_row, false);
17279 eassert
17280 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17281 w->window_end_vpos)));
17282 }
17283 else
17284 {
17285 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17286 w->window_end_pos = Z - ZV;
17287 w->window_end_vpos = 0;
17288 }
17289
17290 /* But that is not valid info until redisplay finishes. */
17291 w->window_end_valid = false;
17292 return 1;
17293 }
17294
17295
17296 \f
17297 /************************************************************************
17298 Window redisplay reusing current matrix when buffer has not changed
17299 ************************************************************************/
17300
17301 /* Try redisplay of window W showing an unchanged buffer with a
17302 different window start than the last time it was displayed by
17303 reusing its current matrix. Value is true if successful.
17304 W->start is the new window start. */
17305
17306 static bool
17307 try_window_reusing_current_matrix (struct window *w)
17308 {
17309 struct frame *f = XFRAME (w->frame);
17310 struct glyph_row *bottom_row;
17311 struct it it;
17312 struct run run;
17313 struct text_pos start, new_start;
17314 int nrows_scrolled, i;
17315 struct glyph_row *last_text_row;
17316 struct glyph_row *last_reused_text_row;
17317 struct glyph_row *start_row;
17318 int start_vpos, min_y, max_y;
17319
17320 #ifdef GLYPH_DEBUG
17321 if (inhibit_try_window_reusing)
17322 return false;
17323 #endif
17324
17325 if (/* This function doesn't handle terminal frames. */
17326 !FRAME_WINDOW_P (f)
17327 /* Don't try to reuse the display if windows have been split
17328 or such. */
17329 || windows_or_buffers_changed
17330 || f->cursor_type_changed)
17331 return false;
17332
17333 /* Can't do this if showing trailing whitespace. */
17334 if (!NILP (Vshow_trailing_whitespace))
17335 return false;
17336
17337 /* If top-line visibility has changed, give up. */
17338 if (WINDOW_WANTS_HEADER_LINE_P (w)
17339 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17340 return false;
17341
17342 /* Give up if old or new display is scrolled vertically. We could
17343 make this function handle this, but right now it doesn't. */
17344 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17345 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17346 return false;
17347
17348 /* The variable new_start now holds the new window start. The old
17349 start `start' can be determined from the current matrix. */
17350 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17351 start = start_row->minpos;
17352 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17353
17354 /* Clear the desired matrix for the display below. */
17355 clear_glyph_matrix (w->desired_matrix);
17356
17357 if (CHARPOS (new_start) <= CHARPOS (start))
17358 {
17359 /* Don't use this method if the display starts with an ellipsis
17360 displayed for invisible text. It's not easy to handle that case
17361 below, and it's certainly not worth the effort since this is
17362 not a frequent case. */
17363 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17364 return false;
17365
17366 IF_DEBUG (debug_method_add (w, "twu1"));
17367
17368 /* Display up to a row that can be reused. The variable
17369 last_text_row is set to the last row displayed that displays
17370 text. Note that it.vpos == 0 if or if not there is a
17371 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17372 start_display (&it, w, new_start);
17373 w->cursor.vpos = -1;
17374 last_text_row = last_reused_text_row = NULL;
17375
17376 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17377 {
17378 /* If we have reached into the characters in the START row,
17379 that means the line boundaries have changed. So we
17380 can't start copying with the row START. Maybe it will
17381 work to start copying with the following row. */
17382 while (IT_CHARPOS (it) > CHARPOS (start))
17383 {
17384 /* Advance to the next row as the "start". */
17385 start_row++;
17386 start = start_row->minpos;
17387 /* If there are no more rows to try, or just one, give up. */
17388 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17389 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17390 || CHARPOS (start) == ZV)
17391 {
17392 clear_glyph_matrix (w->desired_matrix);
17393 return false;
17394 }
17395
17396 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17397 }
17398 /* If we have reached alignment, we can copy the rest of the
17399 rows. */
17400 if (IT_CHARPOS (it) == CHARPOS (start)
17401 /* Don't accept "alignment" inside a display vector,
17402 since start_row could have started in the middle of
17403 that same display vector (thus their character
17404 positions match), and we have no way of telling if
17405 that is the case. */
17406 && it.current.dpvec_index < 0)
17407 break;
17408
17409 it.glyph_row->reversed_p = false;
17410 if (display_line (&it))
17411 last_text_row = it.glyph_row - 1;
17412
17413 }
17414
17415 /* A value of current_y < last_visible_y means that we stopped
17416 at the previous window start, which in turn means that we
17417 have at least one reusable row. */
17418 if (it.current_y < it.last_visible_y)
17419 {
17420 struct glyph_row *row;
17421
17422 /* IT.vpos always starts from 0; it counts text lines. */
17423 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17424
17425 /* Find PT if not already found in the lines displayed. */
17426 if (w->cursor.vpos < 0)
17427 {
17428 int dy = it.current_y - start_row->y;
17429
17430 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17431 row = row_containing_pos (w, PT, row, NULL, dy);
17432 if (row)
17433 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17434 dy, nrows_scrolled);
17435 else
17436 {
17437 clear_glyph_matrix (w->desired_matrix);
17438 return false;
17439 }
17440 }
17441
17442 /* Scroll the display. Do it before the current matrix is
17443 changed. The problem here is that update has not yet
17444 run, i.e. part of the current matrix is not up to date.
17445 scroll_run_hook will clear the cursor, and use the
17446 current matrix to get the height of the row the cursor is
17447 in. */
17448 run.current_y = start_row->y;
17449 run.desired_y = it.current_y;
17450 run.height = it.last_visible_y - it.current_y;
17451
17452 if (run.height > 0 && run.current_y != run.desired_y)
17453 {
17454 update_begin (f);
17455 FRAME_RIF (f)->update_window_begin_hook (w);
17456 FRAME_RIF (f)->clear_window_mouse_face (w);
17457 FRAME_RIF (f)->scroll_run_hook (w, &run);
17458 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17459 update_end (f);
17460 }
17461
17462 /* Shift current matrix down by nrows_scrolled lines. */
17463 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17464 rotate_matrix (w->current_matrix,
17465 start_vpos,
17466 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17467 nrows_scrolled);
17468
17469 /* Disable lines that must be updated. */
17470 for (i = 0; i < nrows_scrolled; ++i)
17471 (start_row + i)->enabled_p = false;
17472
17473 /* Re-compute Y positions. */
17474 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17475 max_y = it.last_visible_y;
17476 for (row = start_row + nrows_scrolled;
17477 row < bottom_row;
17478 ++row)
17479 {
17480 row->y = it.current_y;
17481 row->visible_height = row->height;
17482
17483 if (row->y < min_y)
17484 row->visible_height -= min_y - row->y;
17485 if (row->y + row->height > max_y)
17486 row->visible_height -= row->y + row->height - max_y;
17487 if (row->fringe_bitmap_periodic_p)
17488 row->redraw_fringe_bitmaps_p = true;
17489
17490 it.current_y += row->height;
17491
17492 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17493 last_reused_text_row = row;
17494 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17495 break;
17496 }
17497
17498 /* Disable lines in the current matrix which are now
17499 below the window. */
17500 for (++row; row < bottom_row; ++row)
17501 row->enabled_p = row->mode_line_p = false;
17502 }
17503
17504 /* Update window_end_pos etc.; last_reused_text_row is the last
17505 reused row from the current matrix containing text, if any.
17506 The value of last_text_row is the last displayed line
17507 containing text. */
17508 if (last_reused_text_row)
17509 adjust_window_ends (w, last_reused_text_row, true);
17510 else if (last_text_row)
17511 adjust_window_ends (w, last_text_row, false);
17512 else
17513 {
17514 /* This window must be completely empty. */
17515 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17516 w->window_end_pos = Z - ZV;
17517 w->window_end_vpos = 0;
17518 }
17519 w->window_end_valid = false;
17520
17521 /* Update hint: don't try scrolling again in update_window. */
17522 w->desired_matrix->no_scrolling_p = true;
17523
17524 #ifdef GLYPH_DEBUG
17525 debug_method_add (w, "try_window_reusing_current_matrix 1");
17526 #endif
17527 return true;
17528 }
17529 else if (CHARPOS (new_start) > CHARPOS (start))
17530 {
17531 struct glyph_row *pt_row, *row;
17532 struct glyph_row *first_reusable_row;
17533 struct glyph_row *first_row_to_display;
17534 int dy;
17535 int yb = window_text_bottom_y (w);
17536
17537 /* Find the row starting at new_start, if there is one. Don't
17538 reuse a partially visible line at the end. */
17539 first_reusable_row = start_row;
17540 while (first_reusable_row->enabled_p
17541 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17542 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17543 < CHARPOS (new_start)))
17544 ++first_reusable_row;
17545
17546 /* Give up if there is no row to reuse. */
17547 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17548 || !first_reusable_row->enabled_p
17549 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17550 != CHARPOS (new_start)))
17551 return false;
17552
17553 /* We can reuse fully visible rows beginning with
17554 first_reusable_row to the end of the window. Set
17555 first_row_to_display to the first row that cannot be reused.
17556 Set pt_row to the row containing point, if there is any. */
17557 pt_row = NULL;
17558 for (first_row_to_display = first_reusable_row;
17559 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17560 ++first_row_to_display)
17561 {
17562 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17563 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17564 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17565 && first_row_to_display->ends_at_zv_p
17566 && pt_row == NULL)))
17567 pt_row = first_row_to_display;
17568 }
17569
17570 /* Start displaying at the start of first_row_to_display. */
17571 eassert (first_row_to_display->y < yb);
17572 init_to_row_start (&it, w, first_row_to_display);
17573
17574 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17575 - start_vpos);
17576 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17577 - nrows_scrolled);
17578 it.current_y = (first_row_to_display->y - first_reusable_row->y
17579 + WINDOW_HEADER_LINE_HEIGHT (w));
17580
17581 /* Display lines beginning with first_row_to_display in the
17582 desired matrix. Set last_text_row to the last row displayed
17583 that displays text. */
17584 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17585 if (pt_row == NULL)
17586 w->cursor.vpos = -1;
17587 last_text_row = NULL;
17588 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17589 if (display_line (&it))
17590 last_text_row = it.glyph_row - 1;
17591
17592 /* If point is in a reused row, adjust y and vpos of the cursor
17593 position. */
17594 if (pt_row)
17595 {
17596 w->cursor.vpos -= nrows_scrolled;
17597 w->cursor.y -= first_reusable_row->y - start_row->y;
17598 }
17599
17600 /* Give up if point isn't in a row displayed or reused. (This
17601 also handles the case where w->cursor.vpos < nrows_scrolled
17602 after the calls to display_line, which can happen with scroll
17603 margins. See bug#1295.) */
17604 if (w->cursor.vpos < 0)
17605 {
17606 clear_glyph_matrix (w->desired_matrix);
17607 return false;
17608 }
17609
17610 /* Scroll the display. */
17611 run.current_y = first_reusable_row->y;
17612 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17613 run.height = it.last_visible_y - run.current_y;
17614 dy = run.current_y - run.desired_y;
17615
17616 if (run.height)
17617 {
17618 update_begin (f);
17619 FRAME_RIF (f)->update_window_begin_hook (w);
17620 FRAME_RIF (f)->clear_window_mouse_face (w);
17621 FRAME_RIF (f)->scroll_run_hook (w, &run);
17622 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17623 update_end (f);
17624 }
17625
17626 /* Adjust Y positions of reused rows. */
17627 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17628 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17629 max_y = it.last_visible_y;
17630 for (row = first_reusable_row; row < first_row_to_display; ++row)
17631 {
17632 row->y -= dy;
17633 row->visible_height = row->height;
17634 if (row->y < min_y)
17635 row->visible_height -= min_y - row->y;
17636 if (row->y + row->height > max_y)
17637 row->visible_height -= row->y + row->height - max_y;
17638 if (row->fringe_bitmap_periodic_p)
17639 row->redraw_fringe_bitmaps_p = true;
17640 }
17641
17642 /* Scroll the current matrix. */
17643 eassert (nrows_scrolled > 0);
17644 rotate_matrix (w->current_matrix,
17645 start_vpos,
17646 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17647 -nrows_scrolled);
17648
17649 /* Disable rows not reused. */
17650 for (row -= nrows_scrolled; row < bottom_row; ++row)
17651 row->enabled_p = false;
17652
17653 /* Point may have moved to a different line, so we cannot assume that
17654 the previous cursor position is valid; locate the correct row. */
17655 if (pt_row)
17656 {
17657 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17658 row < bottom_row
17659 && PT >= MATRIX_ROW_END_CHARPOS (row)
17660 && !row->ends_at_zv_p;
17661 row++)
17662 {
17663 w->cursor.vpos++;
17664 w->cursor.y = row->y;
17665 }
17666 if (row < bottom_row)
17667 {
17668 /* Can't simply scan the row for point with
17669 bidi-reordered glyph rows. Let set_cursor_from_row
17670 figure out where to put the cursor, and if it fails,
17671 give up. */
17672 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17673 {
17674 if (!set_cursor_from_row (w, row, w->current_matrix,
17675 0, 0, 0, 0))
17676 {
17677 clear_glyph_matrix (w->desired_matrix);
17678 return false;
17679 }
17680 }
17681 else
17682 {
17683 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17684 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17685
17686 for (; glyph < end
17687 && (!BUFFERP (glyph->object)
17688 || glyph->charpos < PT);
17689 glyph++)
17690 {
17691 w->cursor.hpos++;
17692 w->cursor.x += glyph->pixel_width;
17693 }
17694 }
17695 }
17696 }
17697
17698 /* Adjust window end. A null value of last_text_row means that
17699 the window end is in reused rows which in turn means that
17700 only its vpos can have changed. */
17701 if (last_text_row)
17702 adjust_window_ends (w, last_text_row, false);
17703 else
17704 w->window_end_vpos -= nrows_scrolled;
17705
17706 w->window_end_valid = false;
17707 w->desired_matrix->no_scrolling_p = true;
17708
17709 #ifdef GLYPH_DEBUG
17710 debug_method_add (w, "try_window_reusing_current_matrix 2");
17711 #endif
17712 return true;
17713 }
17714
17715 return false;
17716 }
17717
17718
17719 \f
17720 /************************************************************************
17721 Window redisplay reusing current matrix when buffer has changed
17722 ************************************************************************/
17723
17724 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17725 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17726 ptrdiff_t *, ptrdiff_t *);
17727 static struct glyph_row *
17728 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17729 struct glyph_row *);
17730
17731
17732 /* Return the last row in MATRIX displaying text. If row START is
17733 non-null, start searching with that row. IT gives the dimensions
17734 of the display. Value is null if matrix is empty; otherwise it is
17735 a pointer to the row found. */
17736
17737 static struct glyph_row *
17738 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17739 struct glyph_row *start)
17740 {
17741 struct glyph_row *row, *row_found;
17742
17743 /* Set row_found to the last row in IT->w's current matrix
17744 displaying text. The loop looks funny but think of partially
17745 visible lines. */
17746 row_found = NULL;
17747 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17748 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17749 {
17750 eassert (row->enabled_p);
17751 row_found = row;
17752 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17753 break;
17754 ++row;
17755 }
17756
17757 return row_found;
17758 }
17759
17760
17761 /* Return the last row in the current matrix of W that is not affected
17762 by changes at the start of current_buffer that occurred since W's
17763 current matrix was built. Value is null if no such row exists.
17764
17765 BEG_UNCHANGED us the number of characters unchanged at the start of
17766 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17767 first changed character in current_buffer. Characters at positions <
17768 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17769 when the current matrix was built. */
17770
17771 static struct glyph_row *
17772 find_last_unchanged_at_beg_row (struct window *w)
17773 {
17774 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17775 struct glyph_row *row;
17776 struct glyph_row *row_found = NULL;
17777 int yb = window_text_bottom_y (w);
17778
17779 /* Find the last row displaying unchanged text. */
17780 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17781 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17782 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17783 ++row)
17784 {
17785 if (/* If row ends before first_changed_pos, it is unchanged,
17786 except in some case. */
17787 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17788 /* When row ends in ZV and we write at ZV it is not
17789 unchanged. */
17790 && !row->ends_at_zv_p
17791 /* When first_changed_pos is the end of a continued line,
17792 row is not unchanged because it may be no longer
17793 continued. */
17794 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17795 && (row->continued_p
17796 || row->exact_window_width_line_p))
17797 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17798 needs to be recomputed, so don't consider this row as
17799 unchanged. This happens when the last line was
17800 bidi-reordered and was killed immediately before this
17801 redisplay cycle. In that case, ROW->end stores the
17802 buffer position of the first visual-order character of
17803 the killed text, which is now beyond ZV. */
17804 && CHARPOS (row->end.pos) <= ZV)
17805 row_found = row;
17806
17807 /* Stop if last visible row. */
17808 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17809 break;
17810 }
17811
17812 return row_found;
17813 }
17814
17815
17816 /* Find the first glyph row in the current matrix of W that is not
17817 affected by changes at the end of current_buffer since the
17818 time W's current matrix was built.
17819
17820 Return in *DELTA the number of chars by which buffer positions in
17821 unchanged text at the end of current_buffer must be adjusted.
17822
17823 Return in *DELTA_BYTES the corresponding number of bytes.
17824
17825 Value is null if no such row exists, i.e. all rows are affected by
17826 changes. */
17827
17828 static struct glyph_row *
17829 find_first_unchanged_at_end_row (struct window *w,
17830 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17831 {
17832 struct glyph_row *row;
17833 struct glyph_row *row_found = NULL;
17834
17835 *delta = *delta_bytes = 0;
17836
17837 /* Display must not have been paused, otherwise the current matrix
17838 is not up to date. */
17839 eassert (w->window_end_valid);
17840
17841 /* A value of window_end_pos >= END_UNCHANGED means that the window
17842 end is in the range of changed text. If so, there is no
17843 unchanged row at the end of W's current matrix. */
17844 if (w->window_end_pos >= END_UNCHANGED)
17845 return NULL;
17846
17847 /* Set row to the last row in W's current matrix displaying text. */
17848 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17849
17850 /* If matrix is entirely empty, no unchanged row exists. */
17851 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17852 {
17853 /* The value of row is the last glyph row in the matrix having a
17854 meaningful buffer position in it. The end position of row
17855 corresponds to window_end_pos. This allows us to translate
17856 buffer positions in the current matrix to current buffer
17857 positions for characters not in changed text. */
17858 ptrdiff_t Z_old =
17859 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17860 ptrdiff_t Z_BYTE_old =
17861 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17862 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17863 struct glyph_row *first_text_row
17864 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17865
17866 *delta = Z - Z_old;
17867 *delta_bytes = Z_BYTE - Z_BYTE_old;
17868
17869 /* Set last_unchanged_pos to the buffer position of the last
17870 character in the buffer that has not been changed. Z is the
17871 index + 1 of the last character in current_buffer, i.e. by
17872 subtracting END_UNCHANGED we get the index of the last
17873 unchanged character, and we have to add BEG to get its buffer
17874 position. */
17875 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17876 last_unchanged_pos_old = last_unchanged_pos - *delta;
17877
17878 /* Search backward from ROW for a row displaying a line that
17879 starts at a minimum position >= last_unchanged_pos_old. */
17880 for (; row > first_text_row; --row)
17881 {
17882 /* This used to abort, but it can happen.
17883 It is ok to just stop the search instead here. KFS. */
17884 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17885 break;
17886
17887 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17888 row_found = row;
17889 }
17890 }
17891
17892 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17893
17894 return row_found;
17895 }
17896
17897
17898 /* Make sure that glyph rows in the current matrix of window W
17899 reference the same glyph memory as corresponding rows in the
17900 frame's frame matrix. This function is called after scrolling W's
17901 current matrix on a terminal frame in try_window_id and
17902 try_window_reusing_current_matrix. */
17903
17904 static void
17905 sync_frame_with_window_matrix_rows (struct window *w)
17906 {
17907 struct frame *f = XFRAME (w->frame);
17908 struct glyph_row *window_row, *window_row_end, *frame_row;
17909
17910 /* Preconditions: W must be a leaf window and full-width. Its frame
17911 must have a frame matrix. */
17912 eassert (BUFFERP (w->contents));
17913 eassert (WINDOW_FULL_WIDTH_P (w));
17914 eassert (!FRAME_WINDOW_P (f));
17915
17916 /* If W is a full-width window, glyph pointers in W's current matrix
17917 have, by definition, to be the same as glyph pointers in the
17918 corresponding frame matrix. Note that frame matrices have no
17919 marginal areas (see build_frame_matrix). */
17920 window_row = w->current_matrix->rows;
17921 window_row_end = window_row + w->current_matrix->nrows;
17922 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17923 while (window_row < window_row_end)
17924 {
17925 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17926 struct glyph *end = window_row->glyphs[LAST_AREA];
17927
17928 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17929 frame_row->glyphs[TEXT_AREA] = start;
17930 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17931 frame_row->glyphs[LAST_AREA] = end;
17932
17933 /* Disable frame rows whose corresponding window rows have
17934 been disabled in try_window_id. */
17935 if (!window_row->enabled_p)
17936 frame_row->enabled_p = false;
17937
17938 ++window_row, ++frame_row;
17939 }
17940 }
17941
17942
17943 /* Find the glyph row in window W containing CHARPOS. Consider all
17944 rows between START and END (not inclusive). END null means search
17945 all rows to the end of the display area of W. Value is the row
17946 containing CHARPOS or null. */
17947
17948 struct glyph_row *
17949 row_containing_pos (struct window *w, ptrdiff_t charpos,
17950 struct glyph_row *start, struct glyph_row *end, int dy)
17951 {
17952 struct glyph_row *row = start;
17953 struct glyph_row *best_row = NULL;
17954 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17955 int last_y;
17956
17957 /* If we happen to start on a header-line, skip that. */
17958 if (row->mode_line_p)
17959 ++row;
17960
17961 if ((end && row >= end) || !row->enabled_p)
17962 return NULL;
17963
17964 last_y = window_text_bottom_y (w) - dy;
17965
17966 while (true)
17967 {
17968 /* Give up if we have gone too far. */
17969 if ((end && row >= end) || !row->enabled_p)
17970 return NULL;
17971 /* This formerly returned if they were equal.
17972 I think that both quantities are of a "last plus one" type;
17973 if so, when they are equal, the row is within the screen. -- rms. */
17974 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17975 return NULL;
17976
17977 /* If it is in this row, return this row. */
17978 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17979 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17980 /* The end position of a row equals the start
17981 position of the next row. If CHARPOS is there, we
17982 would rather consider it displayed in the next
17983 line, except when this line ends in ZV. */
17984 && !row_for_charpos_p (row, charpos)))
17985 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17986 {
17987 struct glyph *g;
17988
17989 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17990 || (!best_row && !row->continued_p))
17991 return row;
17992 /* In bidi-reordered rows, there could be several rows whose
17993 edges surround CHARPOS, all of these rows belonging to
17994 the same continued line. We need to find the row which
17995 fits CHARPOS the best. */
17996 for (g = row->glyphs[TEXT_AREA];
17997 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17998 g++)
17999 {
18000 if (!STRINGP (g->object))
18001 {
18002 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18003 {
18004 mindif = eabs (g->charpos - charpos);
18005 best_row = row;
18006 /* Exact match always wins. */
18007 if (mindif == 0)
18008 return best_row;
18009 }
18010 }
18011 }
18012 }
18013 else if (best_row && !row->continued_p)
18014 return best_row;
18015 ++row;
18016 }
18017 }
18018
18019
18020 /* Try to redisplay window W by reusing its existing display. W's
18021 current matrix must be up to date when this function is called,
18022 i.e., window_end_valid must be true.
18023
18024 Value is
18025
18026 >= 1 if successful, i.e. display has been updated
18027 specifically:
18028 1 means the changes were in front of a newline that precedes
18029 the window start, and the whole current matrix was reused
18030 2 means the changes were after the last position displayed
18031 in the window, and the whole current matrix was reused
18032 3 means portions of the current matrix were reused, while
18033 some of the screen lines were redrawn
18034 -1 if redisplay with same window start is known not to succeed
18035 0 if otherwise unsuccessful
18036
18037 The following steps are performed:
18038
18039 1. Find the last row in the current matrix of W that is not
18040 affected by changes at the start of current_buffer. If no such row
18041 is found, give up.
18042
18043 2. Find the first row in W's current matrix that is not affected by
18044 changes at the end of current_buffer. Maybe there is no such row.
18045
18046 3. Display lines beginning with the row + 1 found in step 1 to the
18047 row found in step 2 or, if step 2 didn't find a row, to the end of
18048 the window.
18049
18050 4. If cursor is not known to appear on the window, give up.
18051
18052 5. If display stopped at the row found in step 2, scroll the
18053 display and current matrix as needed.
18054
18055 6. Maybe display some lines at the end of W, if we must. This can
18056 happen under various circumstances, like a partially visible line
18057 becoming fully visible, or because newly displayed lines are displayed
18058 in smaller font sizes.
18059
18060 7. Update W's window end information. */
18061
18062 static int
18063 try_window_id (struct window *w)
18064 {
18065 struct frame *f = XFRAME (w->frame);
18066 struct glyph_matrix *current_matrix = w->current_matrix;
18067 struct glyph_matrix *desired_matrix = w->desired_matrix;
18068 struct glyph_row *last_unchanged_at_beg_row;
18069 struct glyph_row *first_unchanged_at_end_row;
18070 struct glyph_row *row;
18071 struct glyph_row *bottom_row;
18072 int bottom_vpos;
18073 struct it it;
18074 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18075 int dvpos, dy;
18076 struct text_pos start_pos;
18077 struct run run;
18078 int first_unchanged_at_end_vpos = 0;
18079 struct glyph_row *last_text_row, *last_text_row_at_end;
18080 struct text_pos start;
18081 ptrdiff_t first_changed_charpos, last_changed_charpos;
18082
18083 #ifdef GLYPH_DEBUG
18084 if (inhibit_try_window_id)
18085 return 0;
18086 #endif
18087
18088 /* This is handy for debugging. */
18089 #if false
18090 #define GIVE_UP(X) \
18091 do { \
18092 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18093 return 0; \
18094 } while (false)
18095 #else
18096 #define GIVE_UP(X) return 0
18097 #endif
18098
18099 SET_TEXT_POS_FROM_MARKER (start, w->start);
18100
18101 /* Don't use this for mini-windows because these can show
18102 messages and mini-buffers, and we don't handle that here. */
18103 if (MINI_WINDOW_P (w))
18104 GIVE_UP (1);
18105
18106 /* This flag is used to prevent redisplay optimizations. */
18107 if (windows_or_buffers_changed || f->cursor_type_changed)
18108 GIVE_UP (2);
18109
18110 /* This function's optimizations cannot be used if overlays have
18111 changed in the buffer displayed by the window, so give up if they
18112 have. */
18113 if (w->last_overlay_modified != OVERLAY_MODIFF)
18114 GIVE_UP (200);
18115
18116 /* Verify that narrowing has not changed.
18117 Also verify that we were not told to prevent redisplay optimizations.
18118 It would be nice to further
18119 reduce the number of cases where this prevents try_window_id. */
18120 if (current_buffer->clip_changed
18121 || current_buffer->prevent_redisplay_optimizations_p)
18122 GIVE_UP (3);
18123
18124 /* Window must either use window-based redisplay or be full width. */
18125 if (!FRAME_WINDOW_P (f)
18126 && (!FRAME_LINE_INS_DEL_OK (f)
18127 || !WINDOW_FULL_WIDTH_P (w)))
18128 GIVE_UP (4);
18129
18130 /* Give up if point is known NOT to appear in W. */
18131 if (PT < CHARPOS (start))
18132 GIVE_UP (5);
18133
18134 /* Another way to prevent redisplay optimizations. */
18135 if (w->last_modified == 0)
18136 GIVE_UP (6);
18137
18138 /* Verify that window is not hscrolled. */
18139 if (w->hscroll != 0)
18140 GIVE_UP (7);
18141
18142 /* Verify that display wasn't paused. */
18143 if (!w->window_end_valid)
18144 GIVE_UP (8);
18145
18146 /* Likewise if highlighting trailing whitespace. */
18147 if (!NILP (Vshow_trailing_whitespace))
18148 GIVE_UP (11);
18149
18150 /* Can't use this if overlay arrow position and/or string have
18151 changed. */
18152 if (overlay_arrows_changed_p ())
18153 GIVE_UP (12);
18154
18155 /* When word-wrap is on, adding a space to the first word of a
18156 wrapped line can change the wrap position, altering the line
18157 above it. It might be worthwhile to handle this more
18158 intelligently, but for now just redisplay from scratch. */
18159 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18160 GIVE_UP (21);
18161
18162 /* Under bidi reordering, adding or deleting a character in the
18163 beginning of a paragraph, before the first strong directional
18164 character, can change the base direction of the paragraph (unless
18165 the buffer specifies a fixed paragraph direction), which will
18166 require redisplaying the whole paragraph. It might be worthwhile
18167 to find the paragraph limits and widen the range of redisplayed
18168 lines to that, but for now just give up this optimization and
18169 redisplay from scratch. */
18170 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18171 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18172 GIVE_UP (22);
18173
18174 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18175 to that variable require thorough redisplay. */
18176 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18177 GIVE_UP (23);
18178
18179 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18180 only if buffer has really changed. The reason is that the gap is
18181 initially at Z for freshly visited files. The code below would
18182 set end_unchanged to 0 in that case. */
18183 if (MODIFF > SAVE_MODIFF
18184 /* This seems to happen sometimes after saving a buffer. */
18185 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18186 {
18187 if (GPT - BEG < BEG_UNCHANGED)
18188 BEG_UNCHANGED = GPT - BEG;
18189 if (Z - GPT < END_UNCHANGED)
18190 END_UNCHANGED = Z - GPT;
18191 }
18192
18193 /* The position of the first and last character that has been changed. */
18194 first_changed_charpos = BEG + BEG_UNCHANGED;
18195 last_changed_charpos = Z - END_UNCHANGED;
18196
18197 /* If window starts after a line end, and the last change is in
18198 front of that newline, then changes don't affect the display.
18199 This case happens with stealth-fontification. Note that although
18200 the display is unchanged, glyph positions in the matrix have to
18201 be adjusted, of course. */
18202 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18203 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18204 && ((last_changed_charpos < CHARPOS (start)
18205 && CHARPOS (start) == BEGV)
18206 || (last_changed_charpos < CHARPOS (start) - 1
18207 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18208 {
18209 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18210 struct glyph_row *r0;
18211
18212 /* Compute how many chars/bytes have been added to or removed
18213 from the buffer. */
18214 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18215 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18216 Z_delta = Z - Z_old;
18217 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18218
18219 /* Give up if PT is not in the window. Note that it already has
18220 been checked at the start of try_window_id that PT is not in
18221 front of the window start. */
18222 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18223 GIVE_UP (13);
18224
18225 /* If window start is unchanged, we can reuse the whole matrix
18226 as is, after adjusting glyph positions. No need to compute
18227 the window end again, since its offset from Z hasn't changed. */
18228 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18229 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18230 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18231 /* PT must not be in a partially visible line. */
18232 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18233 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18234 {
18235 /* Adjust positions in the glyph matrix. */
18236 if (Z_delta || Z_delta_bytes)
18237 {
18238 struct glyph_row *r1
18239 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18240 increment_matrix_positions (w->current_matrix,
18241 MATRIX_ROW_VPOS (r0, current_matrix),
18242 MATRIX_ROW_VPOS (r1, current_matrix),
18243 Z_delta, Z_delta_bytes);
18244 }
18245
18246 /* Set the cursor. */
18247 row = row_containing_pos (w, PT, r0, NULL, 0);
18248 if (row)
18249 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18250 return 1;
18251 }
18252 }
18253
18254 /* Handle the case that changes are all below what is displayed in
18255 the window, and that PT is in the window. This shortcut cannot
18256 be taken if ZV is visible in the window, and text has been added
18257 there that is visible in the window. */
18258 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18259 /* ZV is not visible in the window, or there are no
18260 changes at ZV, actually. */
18261 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18262 || first_changed_charpos == last_changed_charpos))
18263 {
18264 struct glyph_row *r0;
18265
18266 /* Give up if PT is not in the window. Note that it already has
18267 been checked at the start of try_window_id that PT is not in
18268 front of the window start. */
18269 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18270 GIVE_UP (14);
18271
18272 /* If window start is unchanged, we can reuse the whole matrix
18273 as is, without changing glyph positions since no text has
18274 been added/removed in front of the window end. */
18275 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18276 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18277 /* PT must not be in a partially visible line. */
18278 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18279 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18280 {
18281 /* We have to compute the window end anew since text
18282 could have been added/removed after it. */
18283 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18284 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18285
18286 /* Set the cursor. */
18287 row = row_containing_pos (w, PT, r0, NULL, 0);
18288 if (row)
18289 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18290 return 2;
18291 }
18292 }
18293
18294 /* Give up if window start is in the changed area.
18295
18296 The condition used to read
18297
18298 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18299
18300 but why that was tested escapes me at the moment. */
18301 if (CHARPOS (start) >= first_changed_charpos
18302 && CHARPOS (start) <= last_changed_charpos)
18303 GIVE_UP (15);
18304
18305 /* Check that window start agrees with the start of the first glyph
18306 row in its current matrix. Check this after we know the window
18307 start is not in changed text, otherwise positions would not be
18308 comparable. */
18309 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18310 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18311 GIVE_UP (16);
18312
18313 /* Give up if the window ends in strings. Overlay strings
18314 at the end are difficult to handle, so don't try. */
18315 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18316 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18317 GIVE_UP (20);
18318
18319 /* Compute the position at which we have to start displaying new
18320 lines. Some of the lines at the top of the window might be
18321 reusable because they are not displaying changed text. Find the
18322 last row in W's current matrix not affected by changes at the
18323 start of current_buffer. Value is null if changes start in the
18324 first line of window. */
18325 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18326 if (last_unchanged_at_beg_row)
18327 {
18328 /* Avoid starting to display in the middle of a character, a TAB
18329 for instance. This is easier than to set up the iterator
18330 exactly, and it's not a frequent case, so the additional
18331 effort wouldn't really pay off. */
18332 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18333 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18334 && last_unchanged_at_beg_row > w->current_matrix->rows)
18335 --last_unchanged_at_beg_row;
18336
18337 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18338 GIVE_UP (17);
18339
18340 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18341 GIVE_UP (18);
18342 start_pos = it.current.pos;
18343
18344 /* Start displaying new lines in the desired matrix at the same
18345 vpos we would use in the current matrix, i.e. below
18346 last_unchanged_at_beg_row. */
18347 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18348 current_matrix);
18349 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18350 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18351
18352 eassert (it.hpos == 0 && it.current_x == 0);
18353 }
18354 else
18355 {
18356 /* There are no reusable lines at the start of the window.
18357 Start displaying in the first text line. */
18358 start_display (&it, w, start);
18359 it.vpos = it.first_vpos;
18360 start_pos = it.current.pos;
18361 }
18362
18363 /* Find the first row that is not affected by changes at the end of
18364 the buffer. Value will be null if there is no unchanged row, in
18365 which case we must redisplay to the end of the window. delta
18366 will be set to the value by which buffer positions beginning with
18367 first_unchanged_at_end_row have to be adjusted due to text
18368 changes. */
18369 first_unchanged_at_end_row
18370 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18371 IF_DEBUG (debug_delta = delta);
18372 IF_DEBUG (debug_delta_bytes = delta_bytes);
18373
18374 /* Set stop_pos to the buffer position up to which we will have to
18375 display new lines. If first_unchanged_at_end_row != NULL, this
18376 is the buffer position of the start of the line displayed in that
18377 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18378 that we don't stop at a buffer position. */
18379 stop_pos = 0;
18380 if (first_unchanged_at_end_row)
18381 {
18382 eassert (last_unchanged_at_beg_row == NULL
18383 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18384
18385 /* If this is a continuation line, move forward to the next one
18386 that isn't. Changes in lines above affect this line.
18387 Caution: this may move first_unchanged_at_end_row to a row
18388 not displaying text. */
18389 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18390 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18391 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18392 < it.last_visible_y))
18393 ++first_unchanged_at_end_row;
18394
18395 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18396 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18397 >= it.last_visible_y))
18398 first_unchanged_at_end_row = NULL;
18399 else
18400 {
18401 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18402 + delta);
18403 first_unchanged_at_end_vpos
18404 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18405 eassert (stop_pos >= Z - END_UNCHANGED);
18406 }
18407 }
18408 else if (last_unchanged_at_beg_row == NULL)
18409 GIVE_UP (19);
18410
18411
18412 #ifdef GLYPH_DEBUG
18413
18414 /* Either there is no unchanged row at the end, or the one we have
18415 now displays text. This is a necessary condition for the window
18416 end pos calculation at the end of this function. */
18417 eassert (first_unchanged_at_end_row == NULL
18418 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18419
18420 debug_last_unchanged_at_beg_vpos
18421 = (last_unchanged_at_beg_row
18422 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18423 : -1);
18424 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18425
18426 #endif /* GLYPH_DEBUG */
18427
18428
18429 /* Display new lines. Set last_text_row to the last new line
18430 displayed which has text on it, i.e. might end up as being the
18431 line where the window_end_vpos is. */
18432 w->cursor.vpos = -1;
18433 last_text_row = NULL;
18434 overlay_arrow_seen = false;
18435 if (it.current_y < it.last_visible_y
18436 && !f->fonts_changed
18437 && (first_unchanged_at_end_row == NULL
18438 || IT_CHARPOS (it) < stop_pos))
18439 it.glyph_row->reversed_p = false;
18440 while (it.current_y < it.last_visible_y
18441 && !f->fonts_changed
18442 && (first_unchanged_at_end_row == NULL
18443 || IT_CHARPOS (it) < stop_pos))
18444 {
18445 if (display_line (&it))
18446 last_text_row = it.glyph_row - 1;
18447 }
18448
18449 if (f->fonts_changed)
18450 return -1;
18451
18452 /* The redisplay iterations in display_line above could have
18453 triggered font-lock, which could have done something that
18454 invalidates IT->w window's end-point information, on which we
18455 rely below. E.g., one package, which will remain unnamed, used
18456 to install a font-lock-fontify-region-function that called
18457 bury-buffer, whose side effect is to switch the buffer displayed
18458 by IT->w, and that predictably resets IT->w's window_end_valid
18459 flag, which we already tested at the entry to this function.
18460 Amply punish such packages/modes by giving up on this
18461 optimization in those cases. */
18462 if (!w->window_end_valid)
18463 {
18464 clear_glyph_matrix (w->desired_matrix);
18465 return -1;
18466 }
18467
18468 /* Compute differences in buffer positions, y-positions etc. for
18469 lines reused at the bottom of the window. Compute what we can
18470 scroll. */
18471 if (first_unchanged_at_end_row
18472 /* No lines reused because we displayed everything up to the
18473 bottom of the window. */
18474 && it.current_y < it.last_visible_y)
18475 {
18476 dvpos = (it.vpos
18477 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18478 current_matrix));
18479 dy = it.current_y - first_unchanged_at_end_row->y;
18480 run.current_y = first_unchanged_at_end_row->y;
18481 run.desired_y = run.current_y + dy;
18482 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18483 }
18484 else
18485 {
18486 delta = delta_bytes = dvpos = dy
18487 = run.current_y = run.desired_y = run.height = 0;
18488 first_unchanged_at_end_row = NULL;
18489 }
18490 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18491
18492
18493 /* Find the cursor if not already found. We have to decide whether
18494 PT will appear on this window (it sometimes doesn't, but this is
18495 not a very frequent case.) This decision has to be made before
18496 the current matrix is altered. A value of cursor.vpos < 0 means
18497 that PT is either in one of the lines beginning at
18498 first_unchanged_at_end_row or below the window. Don't care for
18499 lines that might be displayed later at the window end; as
18500 mentioned, this is not a frequent case. */
18501 if (w->cursor.vpos < 0)
18502 {
18503 /* Cursor in unchanged rows at the top? */
18504 if (PT < CHARPOS (start_pos)
18505 && last_unchanged_at_beg_row)
18506 {
18507 row = row_containing_pos (w, PT,
18508 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18509 last_unchanged_at_beg_row + 1, 0);
18510 if (row)
18511 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18512 }
18513
18514 /* Start from first_unchanged_at_end_row looking for PT. */
18515 else if (first_unchanged_at_end_row)
18516 {
18517 row = row_containing_pos (w, PT - delta,
18518 first_unchanged_at_end_row, NULL, 0);
18519 if (row)
18520 set_cursor_from_row (w, row, w->current_matrix, delta,
18521 delta_bytes, dy, dvpos);
18522 }
18523
18524 /* Give up if cursor was not found. */
18525 if (w->cursor.vpos < 0)
18526 {
18527 clear_glyph_matrix (w->desired_matrix);
18528 return -1;
18529 }
18530 }
18531
18532 /* Don't let the cursor end in the scroll margins. */
18533 {
18534 int this_scroll_margin, cursor_height;
18535 int frame_line_height = default_line_pixel_height (w);
18536 int window_total_lines
18537 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18538
18539 this_scroll_margin =
18540 max (0, min (scroll_margin, window_total_lines / 4));
18541 this_scroll_margin *= frame_line_height;
18542 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18543
18544 if ((w->cursor.y < this_scroll_margin
18545 && CHARPOS (start) > BEGV)
18546 /* Old redisplay didn't take scroll margin into account at the bottom,
18547 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18548 || (w->cursor.y + (make_cursor_line_fully_visible_p
18549 ? cursor_height + this_scroll_margin
18550 : 1)) > it.last_visible_y)
18551 {
18552 w->cursor.vpos = -1;
18553 clear_glyph_matrix (w->desired_matrix);
18554 return -1;
18555 }
18556 }
18557
18558 /* Scroll the display. Do it before changing the current matrix so
18559 that xterm.c doesn't get confused about where the cursor glyph is
18560 found. */
18561 if (dy && run.height)
18562 {
18563 update_begin (f);
18564
18565 if (FRAME_WINDOW_P (f))
18566 {
18567 FRAME_RIF (f)->update_window_begin_hook (w);
18568 FRAME_RIF (f)->clear_window_mouse_face (w);
18569 FRAME_RIF (f)->scroll_run_hook (w, &run);
18570 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18571 }
18572 else
18573 {
18574 /* Terminal frame. In this case, dvpos gives the number of
18575 lines to scroll by; dvpos < 0 means scroll up. */
18576 int from_vpos
18577 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18578 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18579 int end = (WINDOW_TOP_EDGE_LINE (w)
18580 + WINDOW_WANTS_HEADER_LINE_P (w)
18581 + window_internal_height (w));
18582
18583 #if defined (HAVE_GPM) || defined (MSDOS)
18584 x_clear_window_mouse_face (w);
18585 #endif
18586 /* Perform the operation on the screen. */
18587 if (dvpos > 0)
18588 {
18589 /* Scroll last_unchanged_at_beg_row to the end of the
18590 window down dvpos lines. */
18591 set_terminal_window (f, end);
18592
18593 /* On dumb terminals delete dvpos lines at the end
18594 before inserting dvpos empty lines. */
18595 if (!FRAME_SCROLL_REGION_OK (f))
18596 ins_del_lines (f, end - dvpos, -dvpos);
18597
18598 /* Insert dvpos empty lines in front of
18599 last_unchanged_at_beg_row. */
18600 ins_del_lines (f, from, dvpos);
18601 }
18602 else if (dvpos < 0)
18603 {
18604 /* Scroll up last_unchanged_at_beg_vpos to the end of
18605 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18606 set_terminal_window (f, end);
18607
18608 /* Delete dvpos lines in front of
18609 last_unchanged_at_beg_vpos. ins_del_lines will set
18610 the cursor to the given vpos and emit |dvpos| delete
18611 line sequences. */
18612 ins_del_lines (f, from + dvpos, dvpos);
18613
18614 /* On a dumb terminal insert dvpos empty lines at the
18615 end. */
18616 if (!FRAME_SCROLL_REGION_OK (f))
18617 ins_del_lines (f, end + dvpos, -dvpos);
18618 }
18619
18620 set_terminal_window (f, 0);
18621 }
18622
18623 update_end (f);
18624 }
18625
18626 /* Shift reused rows of the current matrix to the right position.
18627 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18628 text. */
18629 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18630 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18631 if (dvpos < 0)
18632 {
18633 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18634 bottom_vpos, dvpos);
18635 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18636 bottom_vpos);
18637 }
18638 else if (dvpos > 0)
18639 {
18640 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18641 bottom_vpos, dvpos);
18642 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18643 first_unchanged_at_end_vpos + dvpos);
18644 }
18645
18646 /* For frame-based redisplay, make sure that current frame and window
18647 matrix are in sync with respect to glyph memory. */
18648 if (!FRAME_WINDOW_P (f))
18649 sync_frame_with_window_matrix_rows (w);
18650
18651 /* Adjust buffer positions in reused rows. */
18652 if (delta || delta_bytes)
18653 increment_matrix_positions (current_matrix,
18654 first_unchanged_at_end_vpos + dvpos,
18655 bottom_vpos, delta, delta_bytes);
18656
18657 /* Adjust Y positions. */
18658 if (dy)
18659 shift_glyph_matrix (w, current_matrix,
18660 first_unchanged_at_end_vpos + dvpos,
18661 bottom_vpos, dy);
18662
18663 if (first_unchanged_at_end_row)
18664 {
18665 first_unchanged_at_end_row += dvpos;
18666 if (first_unchanged_at_end_row->y >= it.last_visible_y
18667 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18668 first_unchanged_at_end_row = NULL;
18669 }
18670
18671 /* If scrolling up, there may be some lines to display at the end of
18672 the window. */
18673 last_text_row_at_end = NULL;
18674 if (dy < 0)
18675 {
18676 /* Scrolling up can leave for example a partially visible line
18677 at the end of the window to be redisplayed. */
18678 /* Set last_row to the glyph row in the current matrix where the
18679 window end line is found. It has been moved up or down in
18680 the matrix by dvpos. */
18681 int last_vpos = w->window_end_vpos + dvpos;
18682 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18683
18684 /* If last_row is the window end line, it should display text. */
18685 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18686
18687 /* If window end line was partially visible before, begin
18688 displaying at that line. Otherwise begin displaying with the
18689 line following it. */
18690 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18691 {
18692 init_to_row_start (&it, w, last_row);
18693 it.vpos = last_vpos;
18694 it.current_y = last_row->y;
18695 }
18696 else
18697 {
18698 init_to_row_end (&it, w, last_row);
18699 it.vpos = 1 + last_vpos;
18700 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18701 ++last_row;
18702 }
18703
18704 /* We may start in a continuation line. If so, we have to
18705 get the right continuation_lines_width and current_x. */
18706 it.continuation_lines_width = last_row->continuation_lines_width;
18707 it.hpos = it.current_x = 0;
18708
18709 /* Display the rest of the lines at the window end. */
18710 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18711 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18712 {
18713 /* Is it always sure that the display agrees with lines in
18714 the current matrix? I don't think so, so we mark rows
18715 displayed invalid in the current matrix by setting their
18716 enabled_p flag to false. */
18717 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18718 if (display_line (&it))
18719 last_text_row_at_end = it.glyph_row - 1;
18720 }
18721 }
18722
18723 /* Update window_end_pos and window_end_vpos. */
18724 if (first_unchanged_at_end_row && !last_text_row_at_end)
18725 {
18726 /* Window end line if one of the preserved rows from the current
18727 matrix. Set row to the last row displaying text in current
18728 matrix starting at first_unchanged_at_end_row, after
18729 scrolling. */
18730 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18731 row = find_last_row_displaying_text (w->current_matrix, &it,
18732 first_unchanged_at_end_row);
18733 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18734 adjust_window_ends (w, row, true);
18735 eassert (w->window_end_bytepos >= 0);
18736 IF_DEBUG (debug_method_add (w, "A"));
18737 }
18738 else if (last_text_row_at_end)
18739 {
18740 adjust_window_ends (w, last_text_row_at_end, false);
18741 eassert (w->window_end_bytepos >= 0);
18742 IF_DEBUG (debug_method_add (w, "B"));
18743 }
18744 else if (last_text_row)
18745 {
18746 /* We have displayed either to the end of the window or at the
18747 end of the window, i.e. the last row with text is to be found
18748 in the desired matrix. */
18749 adjust_window_ends (w, last_text_row, false);
18750 eassert (w->window_end_bytepos >= 0);
18751 }
18752 else if (first_unchanged_at_end_row == NULL
18753 && last_text_row == NULL
18754 && last_text_row_at_end == NULL)
18755 {
18756 /* Displayed to end of window, but no line containing text was
18757 displayed. Lines were deleted at the end of the window. */
18758 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18759 int vpos = w->window_end_vpos;
18760 struct glyph_row *current_row = current_matrix->rows + vpos;
18761 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18762
18763 for (row = NULL; !row; --vpos, --current_row, --desired_row)
18764 {
18765 eassert (first_vpos <= vpos);
18766 if (desired_row->enabled_p)
18767 {
18768 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18769 row = desired_row;
18770 }
18771 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18772 row = current_row;
18773 }
18774
18775 w->window_end_vpos = vpos + 1;
18776 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18777 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18778 eassert (w->window_end_bytepos >= 0);
18779 IF_DEBUG (debug_method_add (w, "C"));
18780 }
18781 else
18782 emacs_abort ();
18783
18784 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18785 debug_end_vpos = w->window_end_vpos));
18786
18787 /* Record that display has not been completed. */
18788 w->window_end_valid = false;
18789 w->desired_matrix->no_scrolling_p = true;
18790 return 3;
18791
18792 #undef GIVE_UP
18793 }
18794
18795
18796 \f
18797 /***********************************************************************
18798 More debugging support
18799 ***********************************************************************/
18800
18801 #ifdef GLYPH_DEBUG
18802
18803 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18804 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18805 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18806
18807
18808 /* Dump the contents of glyph matrix MATRIX on stderr.
18809
18810 GLYPHS 0 means don't show glyph contents.
18811 GLYPHS 1 means show glyphs in short form
18812 GLYPHS > 1 means show glyphs in long form. */
18813
18814 void
18815 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18816 {
18817 int i;
18818 for (i = 0; i < matrix->nrows; ++i)
18819 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18820 }
18821
18822
18823 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18824 the glyph row and area where the glyph comes from. */
18825
18826 void
18827 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18828 {
18829 if (glyph->type == CHAR_GLYPH
18830 || glyph->type == GLYPHLESS_GLYPH)
18831 {
18832 fprintf (stderr,
18833 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18834 glyph - row->glyphs[TEXT_AREA],
18835 (glyph->type == CHAR_GLYPH
18836 ? 'C'
18837 : 'G'),
18838 glyph->charpos,
18839 (BUFFERP (glyph->object)
18840 ? 'B'
18841 : (STRINGP (glyph->object)
18842 ? 'S'
18843 : (NILP (glyph->object)
18844 ? '0'
18845 : '-'))),
18846 glyph->pixel_width,
18847 glyph->u.ch,
18848 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18849 ? glyph->u.ch
18850 : '.'),
18851 glyph->face_id,
18852 glyph->left_box_line_p,
18853 glyph->right_box_line_p);
18854 }
18855 else if (glyph->type == STRETCH_GLYPH)
18856 {
18857 fprintf (stderr,
18858 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18859 glyph - row->glyphs[TEXT_AREA],
18860 'S',
18861 glyph->charpos,
18862 (BUFFERP (glyph->object)
18863 ? 'B'
18864 : (STRINGP (glyph->object)
18865 ? 'S'
18866 : (NILP (glyph->object)
18867 ? '0'
18868 : '-'))),
18869 glyph->pixel_width,
18870 0,
18871 ' ',
18872 glyph->face_id,
18873 glyph->left_box_line_p,
18874 glyph->right_box_line_p);
18875 }
18876 else if (glyph->type == IMAGE_GLYPH)
18877 {
18878 fprintf (stderr,
18879 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18880 glyph - row->glyphs[TEXT_AREA],
18881 'I',
18882 glyph->charpos,
18883 (BUFFERP (glyph->object)
18884 ? 'B'
18885 : (STRINGP (glyph->object)
18886 ? 'S'
18887 : (NILP (glyph->object)
18888 ? '0'
18889 : '-'))),
18890 glyph->pixel_width,
18891 glyph->u.img_id,
18892 '.',
18893 glyph->face_id,
18894 glyph->left_box_line_p,
18895 glyph->right_box_line_p);
18896 }
18897 else if (glyph->type == COMPOSITE_GLYPH)
18898 {
18899 fprintf (stderr,
18900 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18901 glyph - row->glyphs[TEXT_AREA],
18902 '+',
18903 glyph->charpos,
18904 (BUFFERP (glyph->object)
18905 ? 'B'
18906 : (STRINGP (glyph->object)
18907 ? 'S'
18908 : (NILP (glyph->object)
18909 ? '0'
18910 : '-'))),
18911 glyph->pixel_width,
18912 glyph->u.cmp.id);
18913 if (glyph->u.cmp.automatic)
18914 fprintf (stderr,
18915 "[%d-%d]",
18916 glyph->slice.cmp.from, glyph->slice.cmp.to);
18917 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18918 glyph->face_id,
18919 glyph->left_box_line_p,
18920 glyph->right_box_line_p);
18921 }
18922 else if (glyph->type == XWIDGET_GLYPH)
18923 {
18924 #ifndef HAVE_XWIDGETS
18925 eassume (false);
18926 #else
18927 fprintf (stderr,
18928 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18929 glyph - row->glyphs[TEXT_AREA],
18930 'X',
18931 glyph->charpos,
18932 (BUFFERP (glyph->object)
18933 ? 'B'
18934 : (STRINGP (glyph->object)
18935 ? 'S'
18936 : '-')),
18937 glyph->pixel_width,
18938 glyph->u.xwidget,
18939 '.',
18940 glyph->face_id,
18941 glyph->left_box_line_p,
18942 glyph->right_box_line_p);
18943 #endif
18944 }
18945 }
18946
18947
18948 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18949 GLYPHS 0 means don't show glyph contents.
18950 GLYPHS 1 means show glyphs in short form
18951 GLYPHS > 1 means show glyphs in long form. */
18952
18953 void
18954 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18955 {
18956 if (glyphs != 1)
18957 {
18958 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18959 fprintf (stderr, "==============================================================================\n");
18960
18961 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18962 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18963 vpos,
18964 MATRIX_ROW_START_CHARPOS (row),
18965 MATRIX_ROW_END_CHARPOS (row),
18966 row->used[TEXT_AREA],
18967 row->contains_overlapping_glyphs_p,
18968 row->enabled_p,
18969 row->truncated_on_left_p,
18970 row->truncated_on_right_p,
18971 row->continued_p,
18972 MATRIX_ROW_CONTINUATION_LINE_P (row),
18973 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18974 row->ends_at_zv_p,
18975 row->fill_line_p,
18976 row->ends_in_middle_of_char_p,
18977 row->starts_in_middle_of_char_p,
18978 row->mouse_face_p,
18979 row->x,
18980 row->y,
18981 row->pixel_width,
18982 row->height,
18983 row->visible_height,
18984 row->ascent,
18985 row->phys_ascent);
18986 /* The next 3 lines should align to "Start" in the header. */
18987 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18988 row->end.overlay_string_index,
18989 row->continuation_lines_width);
18990 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18991 CHARPOS (row->start.string_pos),
18992 CHARPOS (row->end.string_pos));
18993 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18994 row->end.dpvec_index);
18995 }
18996
18997 if (glyphs > 1)
18998 {
18999 int area;
19000
19001 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19002 {
19003 struct glyph *glyph = row->glyphs[area];
19004 struct glyph *glyph_end = glyph + row->used[area];
19005
19006 /* Glyph for a line end in text. */
19007 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19008 ++glyph_end;
19009
19010 if (glyph < glyph_end)
19011 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19012
19013 for (; glyph < glyph_end; ++glyph)
19014 dump_glyph (row, glyph, area);
19015 }
19016 }
19017 else if (glyphs == 1)
19018 {
19019 int area;
19020 char s[SHRT_MAX + 4];
19021
19022 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19023 {
19024 int i;
19025
19026 for (i = 0; i < row->used[area]; ++i)
19027 {
19028 struct glyph *glyph = row->glyphs[area] + i;
19029 if (i == row->used[area] - 1
19030 && area == TEXT_AREA
19031 && NILP (glyph->object)
19032 && glyph->type == CHAR_GLYPH
19033 && glyph->u.ch == ' ')
19034 {
19035 strcpy (&s[i], "[\\n]");
19036 i += 4;
19037 }
19038 else if (glyph->type == CHAR_GLYPH
19039 && glyph->u.ch < 0x80
19040 && glyph->u.ch >= ' ')
19041 s[i] = glyph->u.ch;
19042 else
19043 s[i] = '.';
19044 }
19045
19046 s[i] = '\0';
19047 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19048 }
19049 }
19050 }
19051
19052
19053 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19054 Sdump_glyph_matrix, 0, 1, "p",
19055 doc: /* Dump the current matrix of the selected window to stderr.
19056 Shows contents of glyph row structures. With non-nil
19057 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19058 glyphs in short form, otherwise show glyphs in long form.
19059
19060 Interactively, no argument means show glyphs in short form;
19061 with numeric argument, its value is passed as the GLYPHS flag. */)
19062 (Lisp_Object glyphs)
19063 {
19064 struct window *w = XWINDOW (selected_window);
19065 struct buffer *buffer = XBUFFER (w->contents);
19066
19067 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
19068 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19069 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19070 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19071 fprintf (stderr, "=============================================\n");
19072 dump_glyph_matrix (w->current_matrix,
19073 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19074 return Qnil;
19075 }
19076
19077
19078 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19079 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19080 Only text-mode frames have frame glyph matrices. */)
19081 (void)
19082 {
19083 struct frame *f = XFRAME (selected_frame);
19084
19085 if (f->current_matrix)
19086 dump_glyph_matrix (f->current_matrix, 1);
19087 else
19088 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19089 return Qnil;
19090 }
19091
19092
19093 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
19094 doc: /* Dump glyph row ROW to stderr.
19095 GLYPH 0 means don't dump glyphs.
19096 GLYPH 1 means dump glyphs in short form.
19097 GLYPH > 1 or omitted means dump glyphs in long form. */)
19098 (Lisp_Object row, Lisp_Object glyphs)
19099 {
19100 struct glyph_matrix *matrix;
19101 EMACS_INT vpos;
19102
19103 CHECK_NUMBER (row);
19104 matrix = XWINDOW (selected_window)->current_matrix;
19105 vpos = XINT (row);
19106 if (vpos >= 0 && vpos < matrix->nrows)
19107 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19108 vpos,
19109 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19110 return Qnil;
19111 }
19112
19113
19114 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
19115 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19116 GLYPH 0 means don't dump glyphs.
19117 GLYPH 1 means dump glyphs in short form.
19118 GLYPH > 1 or omitted means dump glyphs in long form.
19119
19120 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19121 do nothing. */)
19122 (Lisp_Object row, Lisp_Object glyphs)
19123 {
19124 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19125 struct frame *sf = SELECTED_FRAME ();
19126 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19127 EMACS_INT vpos;
19128
19129 CHECK_NUMBER (row);
19130 vpos = XINT (row);
19131 if (vpos >= 0 && vpos < m->nrows)
19132 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19133 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19134 #endif
19135 return Qnil;
19136 }
19137
19138
19139 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19140 doc: /* Toggle tracing of redisplay.
19141 With ARG, turn tracing on if and only if ARG is positive. */)
19142 (Lisp_Object arg)
19143 {
19144 if (NILP (arg))
19145 trace_redisplay_p = !trace_redisplay_p;
19146 else
19147 {
19148 arg = Fprefix_numeric_value (arg);
19149 trace_redisplay_p = XINT (arg) > 0;
19150 }
19151
19152 return Qnil;
19153 }
19154
19155
19156 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19157 doc: /* Like `format', but print result to stderr.
19158 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19159 (ptrdiff_t nargs, Lisp_Object *args)
19160 {
19161 Lisp_Object s = Fformat (nargs, args);
19162 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19163 return Qnil;
19164 }
19165
19166 #endif /* GLYPH_DEBUG */
19167
19168
19169 \f
19170 /***********************************************************************
19171 Building Desired Matrix Rows
19172 ***********************************************************************/
19173
19174 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19175 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19176
19177 static struct glyph_row *
19178 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19179 {
19180 struct frame *f = XFRAME (WINDOW_FRAME (w));
19181 struct buffer *buffer = XBUFFER (w->contents);
19182 struct buffer *old = current_buffer;
19183 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19184 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19185 const unsigned char *arrow_end = arrow_string + arrow_len;
19186 const unsigned char *p;
19187 struct it it;
19188 bool multibyte_p;
19189 int n_glyphs_before;
19190
19191 set_buffer_temp (buffer);
19192 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19193 scratch_glyph_row.reversed_p = false;
19194 it.glyph_row->used[TEXT_AREA] = 0;
19195 SET_TEXT_POS (it.position, 0, 0);
19196
19197 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19198 p = arrow_string;
19199 while (p < arrow_end)
19200 {
19201 Lisp_Object face, ilisp;
19202
19203 /* Get the next character. */
19204 if (multibyte_p)
19205 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19206 else
19207 {
19208 it.c = it.char_to_display = *p, it.len = 1;
19209 if (! ASCII_CHAR_P (it.c))
19210 it.char_to_display = BYTE8_TO_CHAR (it.c);
19211 }
19212 p += it.len;
19213
19214 /* Get its face. */
19215 ilisp = make_number (p - arrow_string);
19216 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19217 it.face_id = compute_char_face (f, it.char_to_display, face);
19218
19219 /* Compute its width, get its glyphs. */
19220 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19221 SET_TEXT_POS (it.position, -1, -1);
19222 PRODUCE_GLYPHS (&it);
19223
19224 /* If this character doesn't fit any more in the line, we have
19225 to remove some glyphs. */
19226 if (it.current_x > it.last_visible_x)
19227 {
19228 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19229 break;
19230 }
19231 }
19232
19233 set_buffer_temp (old);
19234 return it.glyph_row;
19235 }
19236
19237
19238 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19239 glyphs to insert is determined by produce_special_glyphs. */
19240
19241 static void
19242 insert_left_trunc_glyphs (struct it *it)
19243 {
19244 struct it truncate_it;
19245 struct glyph *from, *end, *to, *toend;
19246
19247 eassert (!FRAME_WINDOW_P (it->f)
19248 || (!it->glyph_row->reversed_p
19249 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19250 || (it->glyph_row->reversed_p
19251 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19252
19253 /* Get the truncation glyphs. */
19254 truncate_it = *it;
19255 truncate_it.current_x = 0;
19256 truncate_it.face_id = DEFAULT_FACE_ID;
19257 truncate_it.glyph_row = &scratch_glyph_row;
19258 truncate_it.area = TEXT_AREA;
19259 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19260 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19261 truncate_it.object = Qnil;
19262 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19263
19264 /* Overwrite glyphs from IT with truncation glyphs. */
19265 if (!it->glyph_row->reversed_p)
19266 {
19267 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19268
19269 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19270 end = from + tused;
19271 to = it->glyph_row->glyphs[TEXT_AREA];
19272 toend = to + it->glyph_row->used[TEXT_AREA];
19273 if (FRAME_WINDOW_P (it->f))
19274 {
19275 /* On GUI frames, when variable-size fonts are displayed,
19276 the truncation glyphs may need more pixels than the row's
19277 glyphs they overwrite. We overwrite more glyphs to free
19278 enough screen real estate, and enlarge the stretch glyph
19279 on the right (see display_line), if there is one, to
19280 preserve the screen position of the truncation glyphs on
19281 the right. */
19282 int w = 0;
19283 struct glyph *g = to;
19284 short used;
19285
19286 /* The first glyph could be partially visible, in which case
19287 it->glyph_row->x will be negative. But we want the left
19288 truncation glyphs to be aligned at the left margin of the
19289 window, so we override the x coordinate at which the row
19290 will begin. */
19291 it->glyph_row->x = 0;
19292 while (g < toend && w < it->truncation_pixel_width)
19293 {
19294 w += g->pixel_width;
19295 ++g;
19296 }
19297 if (g - to - tused > 0)
19298 {
19299 memmove (to + tused, g, (toend - g) * sizeof(*g));
19300 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19301 }
19302 used = it->glyph_row->used[TEXT_AREA];
19303 if (it->glyph_row->truncated_on_right_p
19304 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19305 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19306 == STRETCH_GLYPH)
19307 {
19308 int extra = w - it->truncation_pixel_width;
19309
19310 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19311 }
19312 }
19313
19314 while (from < end)
19315 *to++ = *from++;
19316
19317 /* There may be padding glyphs left over. Overwrite them too. */
19318 if (!FRAME_WINDOW_P (it->f))
19319 {
19320 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19321 {
19322 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19323 while (from < end)
19324 *to++ = *from++;
19325 }
19326 }
19327
19328 if (to > toend)
19329 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19330 }
19331 else
19332 {
19333 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19334
19335 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19336 that back to front. */
19337 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19338 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19339 toend = it->glyph_row->glyphs[TEXT_AREA];
19340 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19341 if (FRAME_WINDOW_P (it->f))
19342 {
19343 int w = 0;
19344 struct glyph *g = to;
19345
19346 while (g >= toend && w < it->truncation_pixel_width)
19347 {
19348 w += g->pixel_width;
19349 --g;
19350 }
19351 if (to - g - tused > 0)
19352 to = g + tused;
19353 if (it->glyph_row->truncated_on_right_p
19354 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19355 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19356 {
19357 int extra = w - it->truncation_pixel_width;
19358
19359 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19360 }
19361 }
19362
19363 while (from >= end && to >= toend)
19364 *to-- = *from--;
19365 if (!FRAME_WINDOW_P (it->f))
19366 {
19367 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19368 {
19369 from =
19370 truncate_it.glyph_row->glyphs[TEXT_AREA]
19371 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19372 while (from >= end && to >= toend)
19373 *to-- = *from--;
19374 }
19375 }
19376 if (from >= end)
19377 {
19378 /* Need to free some room before prepending additional
19379 glyphs. */
19380 int move_by = from - end + 1;
19381 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19382 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19383
19384 for ( ; g >= g0; g--)
19385 g[move_by] = *g;
19386 while (from >= end)
19387 *to-- = *from--;
19388 it->glyph_row->used[TEXT_AREA] += move_by;
19389 }
19390 }
19391 }
19392
19393 /* Compute the hash code for ROW. */
19394 unsigned
19395 row_hash (struct glyph_row *row)
19396 {
19397 int area, k;
19398 unsigned hashval = 0;
19399
19400 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19401 for (k = 0; k < row->used[area]; ++k)
19402 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19403 + row->glyphs[area][k].u.val
19404 + row->glyphs[area][k].face_id
19405 + row->glyphs[area][k].padding_p
19406 + (row->glyphs[area][k].type << 2));
19407
19408 return hashval;
19409 }
19410
19411 /* Compute the pixel height and width of IT->glyph_row.
19412
19413 Most of the time, ascent and height of a display line will be equal
19414 to the max_ascent and max_height values of the display iterator
19415 structure. This is not the case if
19416
19417 1. We hit ZV without displaying anything. In this case, max_ascent
19418 and max_height will be zero.
19419
19420 2. We have some glyphs that don't contribute to the line height.
19421 (The glyph row flag contributes_to_line_height_p is for future
19422 pixmap extensions).
19423
19424 The first case is easily covered by using default values because in
19425 these cases, the line height does not really matter, except that it
19426 must not be zero. */
19427
19428 static void
19429 compute_line_metrics (struct it *it)
19430 {
19431 struct glyph_row *row = it->glyph_row;
19432
19433 if (FRAME_WINDOW_P (it->f))
19434 {
19435 int i, min_y, max_y;
19436
19437 /* The line may consist of one space only, that was added to
19438 place the cursor on it. If so, the row's height hasn't been
19439 computed yet. */
19440 if (row->height == 0)
19441 {
19442 if (it->max_ascent + it->max_descent == 0)
19443 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19444 row->ascent = it->max_ascent;
19445 row->height = it->max_ascent + it->max_descent;
19446 row->phys_ascent = it->max_phys_ascent;
19447 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19448 row->extra_line_spacing = it->max_extra_line_spacing;
19449 }
19450
19451 /* Compute the width of this line. */
19452 row->pixel_width = row->x;
19453 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19454 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19455
19456 eassert (row->pixel_width >= 0);
19457 eassert (row->ascent >= 0 && row->height > 0);
19458
19459 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19460 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19461
19462 /* If first line's physical ascent is larger than its logical
19463 ascent, use the physical ascent, and make the row taller.
19464 This makes accented characters fully visible. */
19465 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19466 && row->phys_ascent > row->ascent)
19467 {
19468 row->height += row->phys_ascent - row->ascent;
19469 row->ascent = row->phys_ascent;
19470 }
19471
19472 /* Compute how much of the line is visible. */
19473 row->visible_height = row->height;
19474
19475 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19476 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19477
19478 if (row->y < min_y)
19479 row->visible_height -= min_y - row->y;
19480 if (row->y + row->height > max_y)
19481 row->visible_height -= row->y + row->height - max_y;
19482 }
19483 else
19484 {
19485 row->pixel_width = row->used[TEXT_AREA];
19486 if (row->continued_p)
19487 row->pixel_width -= it->continuation_pixel_width;
19488 else if (row->truncated_on_right_p)
19489 row->pixel_width -= it->truncation_pixel_width;
19490 row->ascent = row->phys_ascent = 0;
19491 row->height = row->phys_height = row->visible_height = 1;
19492 row->extra_line_spacing = 0;
19493 }
19494
19495 /* Compute a hash code for this row. */
19496 row->hash = row_hash (row);
19497
19498 it->max_ascent = it->max_descent = 0;
19499 it->max_phys_ascent = it->max_phys_descent = 0;
19500 }
19501
19502
19503 /* Append one space to the glyph row of iterator IT if doing a
19504 window-based redisplay. The space has the same face as
19505 IT->face_id. Value is true if a space was added.
19506
19507 This function is called to make sure that there is always one glyph
19508 at the end of a glyph row that the cursor can be set on under
19509 window-systems. (If there weren't such a glyph we would not know
19510 how wide and tall a box cursor should be displayed).
19511
19512 At the same time this space let's a nicely handle clearing to the
19513 end of the line if the row ends in italic text. */
19514
19515 static bool
19516 append_space_for_newline (struct it *it, bool default_face_p)
19517 {
19518 if (FRAME_WINDOW_P (it->f))
19519 {
19520 int n = it->glyph_row->used[TEXT_AREA];
19521
19522 if (it->glyph_row->glyphs[TEXT_AREA] + n
19523 < it->glyph_row->glyphs[1 + TEXT_AREA])
19524 {
19525 /* Save some values that must not be changed.
19526 Must save IT->c and IT->len because otherwise
19527 ITERATOR_AT_END_P wouldn't work anymore after
19528 append_space_for_newline has been called. */
19529 enum display_element_type saved_what = it->what;
19530 int saved_c = it->c, saved_len = it->len;
19531 int saved_char_to_display = it->char_to_display;
19532 int saved_x = it->current_x;
19533 int saved_face_id = it->face_id;
19534 bool saved_box_end = it->end_of_box_run_p;
19535 struct text_pos saved_pos;
19536 Lisp_Object saved_object;
19537 struct face *face;
19538
19539 saved_object = it->object;
19540 saved_pos = it->position;
19541
19542 it->what = IT_CHARACTER;
19543 memset (&it->position, 0, sizeof it->position);
19544 it->object = Qnil;
19545 it->c = it->char_to_display = ' ';
19546 it->len = 1;
19547
19548 /* If the default face was remapped, be sure to use the
19549 remapped face for the appended newline. */
19550 if (default_face_p)
19551 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19552 else if (it->face_before_selective_p)
19553 it->face_id = it->saved_face_id;
19554 face = FACE_FROM_ID (it->f, it->face_id);
19555 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19556 /* In R2L rows, we will prepend a stretch glyph that will
19557 have the end_of_box_run_p flag set for it, so there's no
19558 need for the appended newline glyph to have that flag
19559 set. */
19560 if (it->glyph_row->reversed_p
19561 /* But if the appended newline glyph goes all the way to
19562 the end of the row, there will be no stretch glyph,
19563 so leave the box flag set. */
19564 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19565 it->end_of_box_run_p = false;
19566
19567 PRODUCE_GLYPHS (it);
19568
19569 #ifdef HAVE_WINDOW_SYSTEM
19570 /* Make sure this space glyph has the right ascent and
19571 descent values, or else cursor at end of line will look
19572 funny, and height of empty lines will be incorrect. */
19573 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
19574 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19575 if (n == 0)
19576 {
19577 Lisp_Object height, total_height;
19578 int extra_line_spacing = it->extra_line_spacing;
19579 int boff = font->baseline_offset;
19580
19581 if (font->vertical_centering)
19582 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19583
19584 it->object = saved_object; /* get_it_property needs this */
19585 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19586 /* Must do a subset of line height processing from
19587 x_produce_glyph for newline characters. */
19588 height = get_it_property (it, Qline_height);
19589 if (CONSP (height)
19590 && CONSP (XCDR (height))
19591 && NILP (XCDR (XCDR (height))))
19592 {
19593 total_height = XCAR (XCDR (height));
19594 height = XCAR (height);
19595 }
19596 else
19597 total_height = Qnil;
19598 height = calc_line_height_property (it, height, font, boff, true);
19599
19600 if (it->override_ascent >= 0)
19601 {
19602 it->ascent = it->override_ascent;
19603 it->descent = it->override_descent;
19604 boff = it->override_boff;
19605 }
19606 if (EQ (height, Qt))
19607 extra_line_spacing = 0;
19608 else
19609 {
19610 Lisp_Object spacing;
19611
19612 it->phys_ascent = it->ascent;
19613 it->phys_descent = it->descent;
19614 if (!NILP (height)
19615 && XINT (height) > it->ascent + it->descent)
19616 it->ascent = XINT (height) - it->descent;
19617
19618 if (!NILP (total_height))
19619 spacing = calc_line_height_property (it, total_height, font,
19620 boff, false);
19621 else
19622 {
19623 spacing = get_it_property (it, Qline_spacing);
19624 spacing = calc_line_height_property (it, spacing, font,
19625 boff, false);
19626 }
19627 if (INTEGERP (spacing))
19628 {
19629 extra_line_spacing = XINT (spacing);
19630 if (!NILP (total_height))
19631 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19632 }
19633 }
19634 if (extra_line_spacing > 0)
19635 {
19636 it->descent += extra_line_spacing;
19637 if (extra_line_spacing > it->max_extra_line_spacing)
19638 it->max_extra_line_spacing = extra_line_spacing;
19639 }
19640 it->max_ascent = it->ascent;
19641 it->max_descent = it->descent;
19642 /* Make sure compute_line_metrics recomputes the row height. */
19643 it->glyph_row->height = 0;
19644 }
19645
19646 g->ascent = it->max_ascent;
19647 g->descent = it->max_descent;
19648 #endif
19649
19650 it->override_ascent = -1;
19651 it->constrain_row_ascent_descent_p = false;
19652 it->current_x = saved_x;
19653 it->object = saved_object;
19654 it->position = saved_pos;
19655 it->what = saved_what;
19656 it->face_id = saved_face_id;
19657 it->len = saved_len;
19658 it->c = saved_c;
19659 it->char_to_display = saved_char_to_display;
19660 it->end_of_box_run_p = saved_box_end;
19661 return true;
19662 }
19663 }
19664
19665 return false;
19666 }
19667
19668
19669 /* Extend the face of the last glyph in the text area of IT->glyph_row
19670 to the end of the display line. Called from display_line. If the
19671 glyph row is empty, add a space glyph to it so that we know the
19672 face to draw. Set the glyph row flag fill_line_p. If the glyph
19673 row is R2L, prepend a stretch glyph to cover the empty space to the
19674 left of the leftmost glyph. */
19675
19676 static void
19677 extend_face_to_end_of_line (struct it *it)
19678 {
19679 struct face *face, *default_face;
19680 struct frame *f = it->f;
19681
19682 /* If line is already filled, do nothing. Non window-system frames
19683 get a grace of one more ``pixel'' because their characters are
19684 1-``pixel'' wide, so they hit the equality too early. This grace
19685 is needed only for R2L rows that are not continued, to produce
19686 one extra blank where we could display the cursor. */
19687 if ((it->current_x >= it->last_visible_x
19688 + (!FRAME_WINDOW_P (f)
19689 && it->glyph_row->reversed_p
19690 && !it->glyph_row->continued_p))
19691 /* If the window has display margins, we will need to extend
19692 their face even if the text area is filled. */
19693 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19694 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19695 return;
19696
19697 /* The default face, possibly remapped. */
19698 default_face = FACE_FROM_ID_OR_NULL (f,
19699 lookup_basic_face (f, DEFAULT_FACE_ID));
19700
19701 /* Face extension extends the background and box of IT->face_id
19702 to the end of the line. If the background equals the background
19703 of the frame, we don't have to do anything. */
19704 face = FACE_FROM_ID (f, (it->face_before_selective_p
19705 ? it->saved_face_id
19706 : it->face_id));
19707
19708 if (FRAME_WINDOW_P (f)
19709 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19710 && face->box == FACE_NO_BOX
19711 && face->background == FRAME_BACKGROUND_PIXEL (f)
19712 #ifdef HAVE_WINDOW_SYSTEM
19713 && !face->stipple
19714 #endif
19715 && !it->glyph_row->reversed_p)
19716 return;
19717
19718 /* Set the glyph row flag indicating that the face of the last glyph
19719 in the text area has to be drawn to the end of the text area. */
19720 it->glyph_row->fill_line_p = true;
19721
19722 /* If current character of IT is not ASCII, make sure we have the
19723 ASCII face. This will be automatically undone the next time
19724 get_next_display_element returns a multibyte character. Note
19725 that the character will always be single byte in unibyte
19726 text. */
19727 if (!ASCII_CHAR_P (it->c))
19728 {
19729 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19730 }
19731
19732 if (FRAME_WINDOW_P (f))
19733 {
19734 /* If the row is empty, add a space with the current face of IT,
19735 so that we know which face to draw. */
19736 if (it->glyph_row->used[TEXT_AREA] == 0)
19737 {
19738 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19739 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19740 it->glyph_row->used[TEXT_AREA] = 1;
19741 }
19742 /* Mode line and the header line don't have margins, and
19743 likewise the frame's tool-bar window, if there is any. */
19744 if (!(it->glyph_row->mode_line_p
19745 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19746 || (WINDOWP (f->tool_bar_window)
19747 && it->w == XWINDOW (f->tool_bar_window))
19748 #endif
19749 ))
19750 {
19751 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19752 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19753 {
19754 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19755 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19756 default_face->id;
19757 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19758 }
19759 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19760 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19761 {
19762 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19763 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19764 default_face->id;
19765 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19766 }
19767 }
19768 #ifdef HAVE_WINDOW_SYSTEM
19769 if (it->glyph_row->reversed_p)
19770 {
19771 /* Prepend a stretch glyph to the row, such that the
19772 rightmost glyph will be drawn flushed all the way to the
19773 right margin of the window. The stretch glyph that will
19774 occupy the empty space, if any, to the left of the
19775 glyphs. */
19776 struct font *font = face->font ? face->font : FRAME_FONT (f);
19777 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19778 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19779 struct glyph *g;
19780 int row_width, stretch_ascent, stretch_width;
19781 struct text_pos saved_pos;
19782 int saved_face_id;
19783 bool saved_avoid_cursor, saved_box_start;
19784
19785 for (row_width = 0, g = row_start; g < row_end; g++)
19786 row_width += g->pixel_width;
19787
19788 /* FIXME: There are various minor display glitches in R2L
19789 rows when only one of the fringes is missing. The
19790 strange condition below produces the least bad effect. */
19791 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19792 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19793 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19794 stretch_width = window_box_width (it->w, TEXT_AREA);
19795 else
19796 stretch_width = it->last_visible_x - it->first_visible_x;
19797 stretch_width -= row_width;
19798
19799 if (stretch_width > 0)
19800 {
19801 stretch_ascent =
19802 (((it->ascent + it->descent)
19803 * FONT_BASE (font)) / FONT_HEIGHT (font));
19804 saved_pos = it->position;
19805 memset (&it->position, 0, sizeof it->position);
19806 saved_avoid_cursor = it->avoid_cursor_p;
19807 it->avoid_cursor_p = true;
19808 saved_face_id = it->face_id;
19809 saved_box_start = it->start_of_box_run_p;
19810 /* The last row's stretch glyph should get the default
19811 face, to avoid painting the rest of the window with
19812 the region face, if the region ends at ZV. */
19813 if (it->glyph_row->ends_at_zv_p)
19814 it->face_id = default_face->id;
19815 else
19816 it->face_id = face->id;
19817 it->start_of_box_run_p = false;
19818 append_stretch_glyph (it, Qnil, stretch_width,
19819 it->ascent + it->descent, stretch_ascent);
19820 it->position = saved_pos;
19821 it->avoid_cursor_p = saved_avoid_cursor;
19822 it->face_id = saved_face_id;
19823 it->start_of_box_run_p = saved_box_start;
19824 }
19825 /* If stretch_width comes out negative, it means that the
19826 last glyph is only partially visible. In R2L rows, we
19827 want the leftmost glyph to be partially visible, so we
19828 need to give the row the corresponding left offset. */
19829 if (stretch_width < 0)
19830 it->glyph_row->x = stretch_width;
19831 }
19832 #endif /* HAVE_WINDOW_SYSTEM */
19833 }
19834 else
19835 {
19836 /* Save some values that must not be changed. */
19837 int saved_x = it->current_x;
19838 struct text_pos saved_pos;
19839 Lisp_Object saved_object;
19840 enum display_element_type saved_what = it->what;
19841 int saved_face_id = it->face_id;
19842
19843 saved_object = it->object;
19844 saved_pos = it->position;
19845
19846 it->what = IT_CHARACTER;
19847 memset (&it->position, 0, sizeof it->position);
19848 it->object = Qnil;
19849 it->c = it->char_to_display = ' ';
19850 it->len = 1;
19851
19852 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19853 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19854 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19855 && !it->glyph_row->mode_line_p
19856 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19857 {
19858 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19859 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19860
19861 for (it->current_x = 0; g < e; g++)
19862 it->current_x += g->pixel_width;
19863
19864 it->area = LEFT_MARGIN_AREA;
19865 it->face_id = default_face->id;
19866 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19867 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19868 {
19869 PRODUCE_GLYPHS (it);
19870 /* term.c:produce_glyphs advances it->current_x only for
19871 TEXT_AREA. */
19872 it->current_x += it->pixel_width;
19873 }
19874
19875 it->current_x = saved_x;
19876 it->area = TEXT_AREA;
19877 }
19878
19879 /* The last row's blank glyphs should get the default face, to
19880 avoid painting the rest of the window with the region face,
19881 if the region ends at ZV. */
19882 if (it->glyph_row->ends_at_zv_p)
19883 it->face_id = default_face->id;
19884 else
19885 it->face_id = face->id;
19886 PRODUCE_GLYPHS (it);
19887
19888 while (it->current_x <= it->last_visible_x)
19889 PRODUCE_GLYPHS (it);
19890
19891 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19892 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19893 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19894 && !it->glyph_row->mode_line_p
19895 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19896 {
19897 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19898 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19899
19900 for ( ; g < e; g++)
19901 it->current_x += g->pixel_width;
19902
19903 it->area = RIGHT_MARGIN_AREA;
19904 it->face_id = default_face->id;
19905 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19906 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19907 {
19908 PRODUCE_GLYPHS (it);
19909 it->current_x += it->pixel_width;
19910 }
19911
19912 it->area = TEXT_AREA;
19913 }
19914
19915 /* Don't count these blanks really. It would let us insert a left
19916 truncation glyph below and make us set the cursor on them, maybe. */
19917 it->current_x = saved_x;
19918 it->object = saved_object;
19919 it->position = saved_pos;
19920 it->what = saved_what;
19921 it->face_id = saved_face_id;
19922 }
19923 }
19924
19925
19926 /* Value is true if text starting at CHARPOS in current_buffer is
19927 trailing whitespace. */
19928
19929 static bool
19930 trailing_whitespace_p (ptrdiff_t charpos)
19931 {
19932 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19933 int c = 0;
19934
19935 while (bytepos < ZV_BYTE
19936 && (c = FETCH_CHAR (bytepos),
19937 c == ' ' || c == '\t'))
19938 ++bytepos;
19939
19940 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19941 {
19942 if (bytepos != PT_BYTE)
19943 return true;
19944 }
19945 return false;
19946 }
19947
19948
19949 /* Highlight trailing whitespace, if any, in ROW. */
19950
19951 static void
19952 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19953 {
19954 int used = row->used[TEXT_AREA];
19955
19956 if (used)
19957 {
19958 struct glyph *start = row->glyphs[TEXT_AREA];
19959 struct glyph *glyph = start + used - 1;
19960
19961 if (row->reversed_p)
19962 {
19963 /* Right-to-left rows need to be processed in the opposite
19964 direction, so swap the edge pointers. */
19965 glyph = start;
19966 start = row->glyphs[TEXT_AREA] + used - 1;
19967 }
19968
19969 /* Skip over glyphs inserted to display the cursor at the
19970 end of a line, for extending the face of the last glyph
19971 to the end of the line on terminals, and for truncation
19972 and continuation glyphs. */
19973 if (!row->reversed_p)
19974 {
19975 while (glyph >= start
19976 && glyph->type == CHAR_GLYPH
19977 && NILP (glyph->object))
19978 --glyph;
19979 }
19980 else
19981 {
19982 while (glyph <= start
19983 && glyph->type == CHAR_GLYPH
19984 && NILP (glyph->object))
19985 ++glyph;
19986 }
19987
19988 /* If last glyph is a space or stretch, and it's trailing
19989 whitespace, set the face of all trailing whitespace glyphs in
19990 IT->glyph_row to `trailing-whitespace'. */
19991 if ((row->reversed_p ? glyph <= start : glyph >= start)
19992 && BUFFERP (glyph->object)
19993 && (glyph->type == STRETCH_GLYPH
19994 || (glyph->type == CHAR_GLYPH
19995 && glyph->u.ch == ' '))
19996 && trailing_whitespace_p (glyph->charpos))
19997 {
19998 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
19999 if (face_id < 0)
20000 return;
20001
20002 if (!row->reversed_p)
20003 {
20004 while (glyph >= start
20005 && BUFFERP (glyph->object)
20006 && (glyph->type == STRETCH_GLYPH
20007 || (glyph->type == CHAR_GLYPH
20008 && glyph->u.ch == ' ')))
20009 (glyph--)->face_id = face_id;
20010 }
20011 else
20012 {
20013 while (glyph <= start
20014 && BUFFERP (glyph->object)
20015 && (glyph->type == STRETCH_GLYPH
20016 || (glyph->type == CHAR_GLYPH
20017 && glyph->u.ch == ' ')))
20018 (glyph++)->face_id = face_id;
20019 }
20020 }
20021 }
20022 }
20023
20024
20025 /* Value is true if glyph row ROW should be
20026 considered to hold the buffer position CHARPOS. */
20027
20028 static bool
20029 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20030 {
20031 bool result = true;
20032
20033 if (charpos == CHARPOS (row->end.pos)
20034 || charpos == MATRIX_ROW_END_CHARPOS (row))
20035 {
20036 /* Suppose the row ends on a string.
20037 Unless the row is continued, that means it ends on a newline
20038 in the string. If it's anything other than a display string
20039 (e.g., a before-string from an overlay), we don't want the
20040 cursor there. (This heuristic seems to give the optimal
20041 behavior for the various types of multi-line strings.)
20042 One exception: if the string has `cursor' property on one of
20043 its characters, we _do_ want the cursor there. */
20044 if (CHARPOS (row->end.string_pos) >= 0)
20045 {
20046 if (row->continued_p)
20047 result = true;
20048 else
20049 {
20050 /* Check for `display' property. */
20051 struct glyph *beg = row->glyphs[TEXT_AREA];
20052 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20053 struct glyph *glyph;
20054
20055 result = false;
20056 for (glyph = end; glyph >= beg; --glyph)
20057 if (STRINGP (glyph->object))
20058 {
20059 Lisp_Object prop
20060 = Fget_char_property (make_number (charpos),
20061 Qdisplay, Qnil);
20062 result =
20063 (!NILP (prop)
20064 && display_prop_string_p (prop, glyph->object));
20065 /* If there's a `cursor' property on one of the
20066 string's characters, this row is a cursor row,
20067 even though this is not a display string. */
20068 if (!result)
20069 {
20070 Lisp_Object s = glyph->object;
20071
20072 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20073 {
20074 ptrdiff_t gpos = glyph->charpos;
20075
20076 if (!NILP (Fget_char_property (make_number (gpos),
20077 Qcursor, s)))
20078 {
20079 result = true;
20080 break;
20081 }
20082 }
20083 }
20084 break;
20085 }
20086 }
20087 }
20088 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20089 {
20090 /* If the row ends in middle of a real character,
20091 and the line is continued, we want the cursor here.
20092 That's because CHARPOS (ROW->end.pos) would equal
20093 PT if PT is before the character. */
20094 if (!row->ends_in_ellipsis_p)
20095 result = row->continued_p;
20096 else
20097 /* If the row ends in an ellipsis, then
20098 CHARPOS (ROW->end.pos) will equal point after the
20099 invisible text. We want that position to be displayed
20100 after the ellipsis. */
20101 result = false;
20102 }
20103 /* If the row ends at ZV, display the cursor at the end of that
20104 row instead of at the start of the row below. */
20105 else
20106 result = row->ends_at_zv_p;
20107 }
20108
20109 return result;
20110 }
20111
20112 /* Value is true if glyph row ROW should be
20113 used to hold the cursor. */
20114
20115 static bool
20116 cursor_row_p (struct glyph_row *row)
20117 {
20118 return row_for_charpos_p (row, PT);
20119 }
20120
20121 \f
20122
20123 /* Push the property PROP so that it will be rendered at the current
20124 position in IT. Return true if PROP was successfully pushed, false
20125 otherwise. Called from handle_line_prefix to handle the
20126 `line-prefix' and `wrap-prefix' properties. */
20127
20128 static bool
20129 push_prefix_prop (struct it *it, Lisp_Object prop)
20130 {
20131 struct text_pos pos =
20132 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20133
20134 eassert (it->method == GET_FROM_BUFFER
20135 || it->method == GET_FROM_DISPLAY_VECTOR
20136 || it->method == GET_FROM_STRING
20137 || it->method == GET_FROM_IMAGE);
20138
20139 /* We need to save the current buffer/string position, so it will be
20140 restored by pop_it, because iterate_out_of_display_property
20141 depends on that being set correctly, but some situations leave
20142 it->position not yet set when this function is called. */
20143 push_it (it, &pos);
20144
20145 if (STRINGP (prop))
20146 {
20147 if (SCHARS (prop) == 0)
20148 {
20149 pop_it (it);
20150 return false;
20151 }
20152
20153 it->string = prop;
20154 it->string_from_prefix_prop_p = true;
20155 it->multibyte_p = STRING_MULTIBYTE (it->string);
20156 it->current.overlay_string_index = -1;
20157 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20158 it->end_charpos = it->string_nchars = SCHARS (it->string);
20159 it->method = GET_FROM_STRING;
20160 it->stop_charpos = 0;
20161 it->prev_stop = 0;
20162 it->base_level_stop = 0;
20163
20164 /* Force paragraph direction to be that of the parent
20165 buffer/string. */
20166 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20167 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20168 else
20169 it->paragraph_embedding = L2R;
20170
20171 /* Set up the bidi iterator for this display string. */
20172 if (it->bidi_p)
20173 {
20174 it->bidi_it.string.lstring = it->string;
20175 it->bidi_it.string.s = NULL;
20176 it->bidi_it.string.schars = it->end_charpos;
20177 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20178 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20179 it->bidi_it.string.unibyte = !it->multibyte_p;
20180 it->bidi_it.w = it->w;
20181 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20182 }
20183 }
20184 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20185 {
20186 it->method = GET_FROM_STRETCH;
20187 it->object = prop;
20188 }
20189 #ifdef HAVE_WINDOW_SYSTEM
20190 else if (IMAGEP (prop))
20191 {
20192 it->what = IT_IMAGE;
20193 it->image_id = lookup_image (it->f, prop);
20194 it->method = GET_FROM_IMAGE;
20195 }
20196 #endif /* HAVE_WINDOW_SYSTEM */
20197 else
20198 {
20199 pop_it (it); /* bogus display property, give up */
20200 return false;
20201 }
20202
20203 return true;
20204 }
20205
20206 /* Return the character-property PROP at the current position in IT. */
20207
20208 static Lisp_Object
20209 get_it_property (struct it *it, Lisp_Object prop)
20210 {
20211 Lisp_Object position, object = it->object;
20212
20213 if (STRINGP (object))
20214 position = make_number (IT_STRING_CHARPOS (*it));
20215 else if (BUFFERP (object))
20216 {
20217 position = make_number (IT_CHARPOS (*it));
20218 object = it->window;
20219 }
20220 else
20221 return Qnil;
20222
20223 return Fget_char_property (position, prop, object);
20224 }
20225
20226 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20227
20228 static void
20229 handle_line_prefix (struct it *it)
20230 {
20231 Lisp_Object prefix;
20232
20233 if (it->continuation_lines_width > 0)
20234 {
20235 prefix = get_it_property (it, Qwrap_prefix);
20236 if (NILP (prefix))
20237 prefix = Vwrap_prefix;
20238 }
20239 else
20240 {
20241 prefix = get_it_property (it, Qline_prefix);
20242 if (NILP (prefix))
20243 prefix = Vline_prefix;
20244 }
20245 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20246 {
20247 /* If the prefix is wider than the window, and we try to wrap
20248 it, it would acquire its own wrap prefix, and so on till the
20249 iterator stack overflows. So, don't wrap the prefix. */
20250 it->line_wrap = TRUNCATE;
20251 it->avoid_cursor_p = true;
20252 }
20253 }
20254
20255 \f
20256
20257 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20258 only for R2L lines from display_line and display_string, when they
20259 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20260 the line/string needs to be continued on the next glyph row. */
20261 static void
20262 unproduce_glyphs (struct it *it, int n)
20263 {
20264 struct glyph *glyph, *end;
20265
20266 eassert (it->glyph_row);
20267 eassert (it->glyph_row->reversed_p);
20268 eassert (it->area == TEXT_AREA);
20269 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20270
20271 if (n > it->glyph_row->used[TEXT_AREA])
20272 n = it->glyph_row->used[TEXT_AREA];
20273 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20274 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20275 for ( ; glyph < end; glyph++)
20276 glyph[-n] = *glyph;
20277 }
20278
20279 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20280 and ROW->maxpos. */
20281 static void
20282 find_row_edges (struct it *it, struct glyph_row *row,
20283 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20284 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20285 {
20286 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20287 lines' rows is implemented for bidi-reordered rows. */
20288
20289 /* ROW->minpos is the value of min_pos, the minimal buffer position
20290 we have in ROW, or ROW->start.pos if that is smaller. */
20291 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20292 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20293 else
20294 /* We didn't find buffer positions smaller than ROW->start, or
20295 didn't find _any_ valid buffer positions in any of the glyphs,
20296 so we must trust the iterator's computed positions. */
20297 row->minpos = row->start.pos;
20298 if (max_pos <= 0)
20299 {
20300 max_pos = CHARPOS (it->current.pos);
20301 max_bpos = BYTEPOS (it->current.pos);
20302 }
20303
20304 /* Here are the various use-cases for ending the row, and the
20305 corresponding values for ROW->maxpos:
20306
20307 Line ends in a newline from buffer eol_pos + 1
20308 Line is continued from buffer max_pos + 1
20309 Line is truncated on right it->current.pos
20310 Line ends in a newline from string max_pos + 1(*)
20311 (*) + 1 only when line ends in a forward scan
20312 Line is continued from string max_pos
20313 Line is continued from display vector max_pos
20314 Line is entirely from a string min_pos == max_pos
20315 Line is entirely from a display vector min_pos == max_pos
20316 Line that ends at ZV ZV
20317
20318 If you discover other use-cases, please add them here as
20319 appropriate. */
20320 if (row->ends_at_zv_p)
20321 row->maxpos = it->current.pos;
20322 else if (row->used[TEXT_AREA])
20323 {
20324 bool seen_this_string = false;
20325 struct glyph_row *r1 = row - 1;
20326
20327 /* Did we see the same display string on the previous row? */
20328 if (STRINGP (it->object)
20329 /* this is not the first row */
20330 && row > it->w->desired_matrix->rows
20331 /* previous row is not the header line */
20332 && !r1->mode_line_p
20333 /* previous row also ends in a newline from a string */
20334 && r1->ends_in_newline_from_string_p)
20335 {
20336 struct glyph *start, *end;
20337
20338 /* Search for the last glyph of the previous row that came
20339 from buffer or string. Depending on whether the row is
20340 L2R or R2L, we need to process it front to back or the
20341 other way round. */
20342 if (!r1->reversed_p)
20343 {
20344 start = r1->glyphs[TEXT_AREA];
20345 end = start + r1->used[TEXT_AREA];
20346 /* Glyphs inserted by redisplay have nil as their object. */
20347 while (end > start
20348 && NILP ((end - 1)->object)
20349 && (end - 1)->charpos <= 0)
20350 --end;
20351 if (end > start)
20352 {
20353 if (EQ ((end - 1)->object, it->object))
20354 seen_this_string = true;
20355 }
20356 else
20357 /* If all the glyphs of the previous row were inserted
20358 by redisplay, it means the previous row was
20359 produced from a single newline, which is only
20360 possible if that newline came from the same string
20361 as the one which produced this ROW. */
20362 seen_this_string = true;
20363 }
20364 else
20365 {
20366 end = r1->glyphs[TEXT_AREA] - 1;
20367 start = end + r1->used[TEXT_AREA];
20368 while (end < start
20369 && NILP ((end + 1)->object)
20370 && (end + 1)->charpos <= 0)
20371 ++end;
20372 if (end < start)
20373 {
20374 if (EQ ((end + 1)->object, it->object))
20375 seen_this_string = true;
20376 }
20377 else
20378 seen_this_string = true;
20379 }
20380 }
20381 /* Take note of each display string that covers a newline only
20382 once, the first time we see it. This is for when a display
20383 string includes more than one newline in it. */
20384 if (row->ends_in_newline_from_string_p && !seen_this_string)
20385 {
20386 /* If we were scanning the buffer forward when we displayed
20387 the string, we want to account for at least one buffer
20388 position that belongs to this row (position covered by
20389 the display string), so that cursor positioning will
20390 consider this row as a candidate when point is at the end
20391 of the visual line represented by this row. This is not
20392 required when scanning back, because max_pos will already
20393 have a much larger value. */
20394 if (CHARPOS (row->end.pos) > max_pos)
20395 INC_BOTH (max_pos, max_bpos);
20396 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20397 }
20398 else if (CHARPOS (it->eol_pos) > 0)
20399 SET_TEXT_POS (row->maxpos,
20400 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20401 else if (row->continued_p)
20402 {
20403 /* If max_pos is different from IT's current position, it
20404 means IT->method does not belong to the display element
20405 at max_pos. However, it also means that the display
20406 element at max_pos was displayed in its entirety on this
20407 line, which is equivalent to saying that the next line
20408 starts at the next buffer position. */
20409 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20410 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20411 else
20412 {
20413 INC_BOTH (max_pos, max_bpos);
20414 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20415 }
20416 }
20417 else if (row->truncated_on_right_p)
20418 /* display_line already called reseat_at_next_visible_line_start,
20419 which puts the iterator at the beginning of the next line, in
20420 the logical order. */
20421 row->maxpos = it->current.pos;
20422 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20423 /* A line that is entirely from a string/image/stretch... */
20424 row->maxpos = row->minpos;
20425 else
20426 emacs_abort ();
20427 }
20428 else
20429 row->maxpos = it->current.pos;
20430 }
20431
20432 /* Construct the glyph row IT->glyph_row in the desired matrix of
20433 IT->w from text at the current position of IT. See dispextern.h
20434 for an overview of struct it. Value is true if
20435 IT->glyph_row displays text, as opposed to a line displaying ZV
20436 only. */
20437
20438 static bool
20439 display_line (struct it *it)
20440 {
20441 struct glyph_row *row = it->glyph_row;
20442 Lisp_Object overlay_arrow_string;
20443 struct it wrap_it;
20444 void *wrap_data = NULL;
20445 bool may_wrap = false;
20446 int wrap_x UNINIT;
20447 int wrap_row_used = -1;
20448 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
20449 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
20450 int wrap_row_extra_line_spacing UNINIT;
20451 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
20452 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
20453 int cvpos;
20454 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20455 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
20456 bool pending_handle_line_prefix = false;
20457
20458 /* We always start displaying at hpos zero even if hscrolled. */
20459 eassert (it->hpos == 0 && it->current_x == 0);
20460
20461 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20462 >= it->w->desired_matrix->nrows)
20463 {
20464 it->w->nrows_scale_factor++;
20465 it->f->fonts_changed = true;
20466 return false;
20467 }
20468
20469 /* Clear the result glyph row and enable it. */
20470 prepare_desired_row (it->w, row, false);
20471
20472 row->y = it->current_y;
20473 row->start = it->start;
20474 row->continuation_lines_width = it->continuation_lines_width;
20475 row->displays_text_p = true;
20476 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20477 it->starts_in_middle_of_char_p = false;
20478
20479 /* Arrange the overlays nicely for our purposes. Usually, we call
20480 display_line on only one line at a time, in which case this
20481 can't really hurt too much, or we call it on lines which appear
20482 one after another in the buffer, in which case all calls to
20483 recenter_overlay_lists but the first will be pretty cheap. */
20484 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20485
20486 /* Move over display elements that are not visible because we are
20487 hscrolled. This may stop at an x-position < IT->first_visible_x
20488 if the first glyph is partially visible or if we hit a line end. */
20489 if (it->current_x < it->first_visible_x)
20490 {
20491 enum move_it_result move_result;
20492
20493 this_line_min_pos = row->start.pos;
20494 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20495 MOVE_TO_POS | MOVE_TO_X);
20496 /* If we are under a large hscroll, move_it_in_display_line_to
20497 could hit the end of the line without reaching
20498 it->first_visible_x. Pretend that we did reach it. This is
20499 especially important on a TTY, where we will call
20500 extend_face_to_end_of_line, which needs to know how many
20501 blank glyphs to produce. */
20502 if (it->current_x < it->first_visible_x
20503 && (move_result == MOVE_NEWLINE_OR_CR
20504 || move_result == MOVE_POS_MATCH_OR_ZV))
20505 it->current_x = it->first_visible_x;
20506
20507 /* Record the smallest positions seen while we moved over
20508 display elements that are not visible. This is needed by
20509 redisplay_internal for optimizing the case where the cursor
20510 stays inside the same line. The rest of this function only
20511 considers positions that are actually displayed, so
20512 RECORD_MAX_MIN_POS will not otherwise record positions that
20513 are hscrolled to the left of the left edge of the window. */
20514 min_pos = CHARPOS (this_line_min_pos);
20515 min_bpos = BYTEPOS (this_line_min_pos);
20516 }
20517 else if (it->area == TEXT_AREA)
20518 {
20519 /* We only do this when not calling move_it_in_display_line_to
20520 above, because that function calls itself handle_line_prefix. */
20521 handle_line_prefix (it);
20522 }
20523 else
20524 {
20525 /* Line-prefix and wrap-prefix are always displayed in the text
20526 area. But if this is the first call to display_line after
20527 init_iterator, the iterator might have been set up to write
20528 into a marginal area, e.g. if the line begins with some
20529 display property that writes to the margins. So we need to
20530 wait with the call to handle_line_prefix until whatever
20531 writes to the margin has done its job. */
20532 pending_handle_line_prefix = true;
20533 }
20534
20535 /* Get the initial row height. This is either the height of the
20536 text hscrolled, if there is any, or zero. */
20537 row->ascent = it->max_ascent;
20538 row->height = it->max_ascent + it->max_descent;
20539 row->phys_ascent = it->max_phys_ascent;
20540 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20541 row->extra_line_spacing = it->max_extra_line_spacing;
20542
20543 /* Utility macro to record max and min buffer positions seen until now. */
20544 #define RECORD_MAX_MIN_POS(IT) \
20545 do \
20546 { \
20547 bool composition_p \
20548 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20549 ptrdiff_t current_pos = \
20550 composition_p ? (IT)->cmp_it.charpos \
20551 : IT_CHARPOS (*(IT)); \
20552 ptrdiff_t current_bpos = \
20553 composition_p ? CHAR_TO_BYTE (current_pos) \
20554 : IT_BYTEPOS (*(IT)); \
20555 if (current_pos < min_pos) \
20556 { \
20557 min_pos = current_pos; \
20558 min_bpos = current_bpos; \
20559 } \
20560 if (IT_CHARPOS (*it) > max_pos) \
20561 { \
20562 max_pos = IT_CHARPOS (*it); \
20563 max_bpos = IT_BYTEPOS (*it); \
20564 } \
20565 } \
20566 while (false)
20567
20568 /* Loop generating characters. The loop is left with IT on the next
20569 character to display. */
20570 while (true)
20571 {
20572 int n_glyphs_before, hpos_before, x_before;
20573 int x, nglyphs;
20574 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20575
20576 /* Retrieve the next thing to display. Value is false if end of
20577 buffer reached. */
20578 if (!get_next_display_element (it))
20579 {
20580 /* Maybe add a space at the end of this line that is used to
20581 display the cursor there under X. Set the charpos of the
20582 first glyph of blank lines not corresponding to any text
20583 to -1. */
20584 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20585 row->exact_window_width_line_p = true;
20586 else if ((append_space_for_newline (it, true)
20587 && row->used[TEXT_AREA] == 1)
20588 || row->used[TEXT_AREA] == 0)
20589 {
20590 row->glyphs[TEXT_AREA]->charpos = -1;
20591 row->displays_text_p = false;
20592
20593 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20594 && (!MINI_WINDOW_P (it->w)
20595 || (minibuf_level && EQ (it->window, minibuf_window))))
20596 row->indicate_empty_line_p = true;
20597 }
20598
20599 it->continuation_lines_width = 0;
20600 row->ends_at_zv_p = true;
20601 /* A row that displays right-to-left text must always have
20602 its last face extended all the way to the end of line,
20603 even if this row ends in ZV, because we still write to
20604 the screen left to right. We also need to extend the
20605 last face if the default face is remapped to some
20606 different face, otherwise the functions that clear
20607 portions of the screen will clear with the default face's
20608 background color. */
20609 if (row->reversed_p
20610 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20611 extend_face_to_end_of_line (it);
20612 break;
20613 }
20614
20615 /* Now, get the metrics of what we want to display. This also
20616 generates glyphs in `row' (which is IT->glyph_row). */
20617 n_glyphs_before = row->used[TEXT_AREA];
20618 x = it->current_x;
20619
20620 /* Remember the line height so far in case the next element doesn't
20621 fit on the line. */
20622 if (it->line_wrap != TRUNCATE)
20623 {
20624 ascent = it->max_ascent;
20625 descent = it->max_descent;
20626 phys_ascent = it->max_phys_ascent;
20627 phys_descent = it->max_phys_descent;
20628
20629 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20630 {
20631 if (IT_DISPLAYING_WHITESPACE (it))
20632 may_wrap = true;
20633 else if (may_wrap)
20634 {
20635 SAVE_IT (wrap_it, *it, wrap_data);
20636 wrap_x = x;
20637 wrap_row_used = row->used[TEXT_AREA];
20638 wrap_row_ascent = row->ascent;
20639 wrap_row_height = row->height;
20640 wrap_row_phys_ascent = row->phys_ascent;
20641 wrap_row_phys_height = row->phys_height;
20642 wrap_row_extra_line_spacing = row->extra_line_spacing;
20643 wrap_row_min_pos = min_pos;
20644 wrap_row_min_bpos = min_bpos;
20645 wrap_row_max_pos = max_pos;
20646 wrap_row_max_bpos = max_bpos;
20647 may_wrap = false;
20648 }
20649 }
20650 }
20651
20652 PRODUCE_GLYPHS (it);
20653
20654 /* If this display element was in marginal areas, continue with
20655 the next one. */
20656 if (it->area != TEXT_AREA)
20657 {
20658 row->ascent = max (row->ascent, it->max_ascent);
20659 row->height = max (row->height, it->max_ascent + it->max_descent);
20660 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20661 row->phys_height = max (row->phys_height,
20662 it->max_phys_ascent + it->max_phys_descent);
20663 row->extra_line_spacing = max (row->extra_line_spacing,
20664 it->max_extra_line_spacing);
20665 set_iterator_to_next (it, true);
20666 /* If we didn't handle the line/wrap prefix above, and the
20667 call to set_iterator_to_next just switched to TEXT_AREA,
20668 process the prefix now. */
20669 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20670 {
20671 pending_handle_line_prefix = false;
20672 handle_line_prefix (it);
20673 }
20674 continue;
20675 }
20676
20677 /* Does the display element fit on the line? If we truncate
20678 lines, we should draw past the right edge of the window. If
20679 we don't truncate, we want to stop so that we can display the
20680 continuation glyph before the right margin. If lines are
20681 continued, there are two possible strategies for characters
20682 resulting in more than 1 glyph (e.g. tabs): Display as many
20683 glyphs as possible in this line and leave the rest for the
20684 continuation line, or display the whole element in the next
20685 line. Original redisplay did the former, so we do it also. */
20686 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20687 hpos_before = it->hpos;
20688 x_before = x;
20689
20690 if (/* Not a newline. */
20691 nglyphs > 0
20692 /* Glyphs produced fit entirely in the line. */
20693 && it->current_x < it->last_visible_x)
20694 {
20695 it->hpos += nglyphs;
20696 row->ascent = max (row->ascent, it->max_ascent);
20697 row->height = max (row->height, it->max_ascent + it->max_descent);
20698 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20699 row->phys_height = max (row->phys_height,
20700 it->max_phys_ascent + it->max_phys_descent);
20701 row->extra_line_spacing = max (row->extra_line_spacing,
20702 it->max_extra_line_spacing);
20703 if (it->current_x - it->pixel_width < it->first_visible_x
20704 /* In R2L rows, we arrange in extend_face_to_end_of_line
20705 to add a right offset to the line, by a suitable
20706 change to the stretch glyph that is the leftmost
20707 glyph of the line. */
20708 && !row->reversed_p)
20709 row->x = x - it->first_visible_x;
20710 /* Record the maximum and minimum buffer positions seen so
20711 far in glyphs that will be displayed by this row. */
20712 if (it->bidi_p)
20713 RECORD_MAX_MIN_POS (it);
20714 }
20715 else
20716 {
20717 int i, new_x;
20718 struct glyph *glyph;
20719
20720 for (i = 0; i < nglyphs; ++i, x = new_x)
20721 {
20722 /* Identify the glyphs added by the last call to
20723 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20724 the previous glyphs. */
20725 if (!row->reversed_p)
20726 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20727 else
20728 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20729 new_x = x + glyph->pixel_width;
20730
20731 if (/* Lines are continued. */
20732 it->line_wrap != TRUNCATE
20733 && (/* Glyph doesn't fit on the line. */
20734 new_x > it->last_visible_x
20735 /* Or it fits exactly on a window system frame. */
20736 || (new_x == it->last_visible_x
20737 && FRAME_WINDOW_P (it->f)
20738 && (row->reversed_p
20739 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20740 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20741 {
20742 /* End of a continued line. */
20743
20744 if (it->hpos == 0
20745 || (new_x == it->last_visible_x
20746 && FRAME_WINDOW_P (it->f)
20747 && (row->reversed_p
20748 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20749 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20750 {
20751 /* Current glyph is the only one on the line or
20752 fits exactly on the line. We must continue
20753 the line because we can't draw the cursor
20754 after the glyph. */
20755 row->continued_p = true;
20756 it->current_x = new_x;
20757 it->continuation_lines_width += new_x;
20758 ++it->hpos;
20759 if (i == nglyphs - 1)
20760 {
20761 /* If line-wrap is on, check if a previous
20762 wrap point was found. */
20763 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20764 && wrap_row_used > 0
20765 /* Even if there is a previous wrap
20766 point, continue the line here as
20767 usual, if (i) the previous character
20768 was a space or tab AND (ii) the
20769 current character is not. */
20770 && (!may_wrap
20771 || IT_DISPLAYING_WHITESPACE (it)))
20772 goto back_to_wrap;
20773
20774 /* Record the maximum and minimum buffer
20775 positions seen so far in glyphs that will be
20776 displayed by this row. */
20777 if (it->bidi_p)
20778 RECORD_MAX_MIN_POS (it);
20779 set_iterator_to_next (it, true);
20780 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20781 {
20782 if (!get_next_display_element (it))
20783 {
20784 row->exact_window_width_line_p = true;
20785 it->continuation_lines_width = 0;
20786 row->continued_p = false;
20787 row->ends_at_zv_p = true;
20788 }
20789 else if (ITERATOR_AT_END_OF_LINE_P (it))
20790 {
20791 row->continued_p = false;
20792 row->exact_window_width_line_p = true;
20793 }
20794 /* If line-wrap is on, check if a
20795 previous wrap point was found. */
20796 else if (wrap_row_used > 0
20797 /* Even if there is a previous wrap
20798 point, continue the line here as
20799 usual, if (i) the previous character
20800 was a space or tab AND (ii) the
20801 current character is not. */
20802 && (!may_wrap
20803 || IT_DISPLAYING_WHITESPACE (it)))
20804 goto back_to_wrap;
20805
20806 }
20807 }
20808 else if (it->bidi_p)
20809 RECORD_MAX_MIN_POS (it);
20810 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20811 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20812 extend_face_to_end_of_line (it);
20813 }
20814 else if (CHAR_GLYPH_PADDING_P (*glyph)
20815 && !FRAME_WINDOW_P (it->f))
20816 {
20817 /* A padding glyph that doesn't fit on this line.
20818 This means the whole character doesn't fit
20819 on the line. */
20820 if (row->reversed_p)
20821 unproduce_glyphs (it, row->used[TEXT_AREA]
20822 - n_glyphs_before);
20823 row->used[TEXT_AREA] = n_glyphs_before;
20824
20825 /* Fill the rest of the row with continuation
20826 glyphs like in 20.x. */
20827 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20828 < row->glyphs[1 + TEXT_AREA])
20829 produce_special_glyphs (it, IT_CONTINUATION);
20830
20831 row->continued_p = true;
20832 it->current_x = x_before;
20833 it->continuation_lines_width += x_before;
20834
20835 /* Restore the height to what it was before the
20836 element not fitting on the line. */
20837 it->max_ascent = ascent;
20838 it->max_descent = descent;
20839 it->max_phys_ascent = phys_ascent;
20840 it->max_phys_descent = phys_descent;
20841 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20842 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20843 extend_face_to_end_of_line (it);
20844 }
20845 else if (wrap_row_used > 0)
20846 {
20847 back_to_wrap:
20848 if (row->reversed_p)
20849 unproduce_glyphs (it,
20850 row->used[TEXT_AREA] - wrap_row_used);
20851 RESTORE_IT (it, &wrap_it, wrap_data);
20852 it->continuation_lines_width += wrap_x;
20853 row->used[TEXT_AREA] = wrap_row_used;
20854 row->ascent = wrap_row_ascent;
20855 row->height = wrap_row_height;
20856 row->phys_ascent = wrap_row_phys_ascent;
20857 row->phys_height = wrap_row_phys_height;
20858 row->extra_line_spacing = wrap_row_extra_line_spacing;
20859 min_pos = wrap_row_min_pos;
20860 min_bpos = wrap_row_min_bpos;
20861 max_pos = wrap_row_max_pos;
20862 max_bpos = wrap_row_max_bpos;
20863 row->continued_p = true;
20864 row->ends_at_zv_p = false;
20865 row->exact_window_width_line_p = false;
20866 it->continuation_lines_width += x;
20867
20868 /* Make sure that a non-default face is extended
20869 up to the right margin of the window. */
20870 extend_face_to_end_of_line (it);
20871 }
20872 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20873 {
20874 /* A TAB that extends past the right edge of the
20875 window. This produces a single glyph on
20876 window system frames. We leave the glyph in
20877 this row and let it fill the row, but don't
20878 consume the TAB. */
20879 if ((row->reversed_p
20880 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20881 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20882 produce_special_glyphs (it, IT_CONTINUATION);
20883 it->continuation_lines_width += it->last_visible_x;
20884 row->ends_in_middle_of_char_p = true;
20885 row->continued_p = true;
20886 glyph->pixel_width = it->last_visible_x - x;
20887 it->starts_in_middle_of_char_p = true;
20888 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20889 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20890 extend_face_to_end_of_line (it);
20891 }
20892 else
20893 {
20894 /* Something other than a TAB that draws past
20895 the right edge of the window. Restore
20896 positions to values before the element. */
20897 if (row->reversed_p)
20898 unproduce_glyphs (it, row->used[TEXT_AREA]
20899 - (n_glyphs_before + i));
20900 row->used[TEXT_AREA] = n_glyphs_before + i;
20901
20902 /* Display continuation glyphs. */
20903 it->current_x = x_before;
20904 it->continuation_lines_width += x;
20905 if (!FRAME_WINDOW_P (it->f)
20906 || (row->reversed_p
20907 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20908 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20909 produce_special_glyphs (it, IT_CONTINUATION);
20910 row->continued_p = true;
20911
20912 extend_face_to_end_of_line (it);
20913
20914 if (nglyphs > 1 && i > 0)
20915 {
20916 row->ends_in_middle_of_char_p = true;
20917 it->starts_in_middle_of_char_p = true;
20918 }
20919
20920 /* Restore the height to what it was before the
20921 element not fitting on the line. */
20922 it->max_ascent = ascent;
20923 it->max_descent = descent;
20924 it->max_phys_ascent = phys_ascent;
20925 it->max_phys_descent = phys_descent;
20926 }
20927
20928 break;
20929 }
20930 else if (new_x > it->first_visible_x)
20931 {
20932 /* Increment number of glyphs actually displayed. */
20933 ++it->hpos;
20934
20935 /* Record the maximum and minimum buffer positions
20936 seen so far in glyphs that will be displayed by
20937 this row. */
20938 if (it->bidi_p)
20939 RECORD_MAX_MIN_POS (it);
20940
20941 if (x < it->first_visible_x && !row->reversed_p)
20942 /* Glyph is partially visible, i.e. row starts at
20943 negative X position. Don't do that in R2L
20944 rows, where we arrange to add a right offset to
20945 the line in extend_face_to_end_of_line, by a
20946 suitable change to the stretch glyph that is
20947 the leftmost glyph of the line. */
20948 row->x = x - it->first_visible_x;
20949 /* When the last glyph of an R2L row only fits
20950 partially on the line, we need to set row->x to a
20951 negative offset, so that the leftmost glyph is
20952 the one that is partially visible. But if we are
20953 going to produce the truncation glyph, this will
20954 be taken care of in produce_special_glyphs. */
20955 if (row->reversed_p
20956 && new_x > it->last_visible_x
20957 && !(it->line_wrap == TRUNCATE
20958 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20959 {
20960 eassert (FRAME_WINDOW_P (it->f));
20961 row->x = it->last_visible_x - new_x;
20962 }
20963 }
20964 else
20965 {
20966 /* Glyph is completely off the left margin of the
20967 window. This should not happen because of the
20968 move_it_in_display_line at the start of this
20969 function, unless the text display area of the
20970 window is empty. */
20971 eassert (it->first_visible_x <= it->last_visible_x);
20972 }
20973 }
20974 /* Even if this display element produced no glyphs at all,
20975 we want to record its position. */
20976 if (it->bidi_p && nglyphs == 0)
20977 RECORD_MAX_MIN_POS (it);
20978
20979 row->ascent = max (row->ascent, it->max_ascent);
20980 row->height = max (row->height, it->max_ascent + it->max_descent);
20981 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20982 row->phys_height = max (row->phys_height,
20983 it->max_phys_ascent + it->max_phys_descent);
20984 row->extra_line_spacing = max (row->extra_line_spacing,
20985 it->max_extra_line_spacing);
20986
20987 /* End of this display line if row is continued. */
20988 if (row->continued_p || row->ends_at_zv_p)
20989 break;
20990 }
20991
20992 at_end_of_line:
20993 /* Is this a line end? If yes, we're also done, after making
20994 sure that a non-default face is extended up to the right
20995 margin of the window. */
20996 if (ITERATOR_AT_END_OF_LINE_P (it))
20997 {
20998 int used_before = row->used[TEXT_AREA];
20999
21000 row->ends_in_newline_from_string_p = STRINGP (it->object);
21001
21002 /* Add a space at the end of the line that is used to
21003 display the cursor there. */
21004 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21005 append_space_for_newline (it, false);
21006
21007 /* Extend the face to the end of the line. */
21008 extend_face_to_end_of_line (it);
21009
21010 /* Make sure we have the position. */
21011 if (used_before == 0)
21012 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21013
21014 /* Record the position of the newline, for use in
21015 find_row_edges. */
21016 it->eol_pos = it->current.pos;
21017
21018 /* Consume the line end. This skips over invisible lines. */
21019 set_iterator_to_next (it, true);
21020 it->continuation_lines_width = 0;
21021 break;
21022 }
21023
21024 /* Proceed with next display element. Note that this skips
21025 over lines invisible because of selective display. */
21026 set_iterator_to_next (it, true);
21027
21028 /* If we truncate lines, we are done when the last displayed
21029 glyphs reach past the right margin of the window. */
21030 if (it->line_wrap == TRUNCATE
21031 && ((FRAME_WINDOW_P (it->f)
21032 /* Images are preprocessed in produce_image_glyph such
21033 that they are cropped at the right edge of the
21034 window, so an image glyph will always end exactly at
21035 last_visible_x, even if there's no right fringe. */
21036 && ((row->reversed_p
21037 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21038 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21039 || it->what == IT_IMAGE))
21040 ? (it->current_x >= it->last_visible_x)
21041 : (it->current_x > it->last_visible_x)))
21042 {
21043 /* Maybe add truncation glyphs. */
21044 if (!FRAME_WINDOW_P (it->f)
21045 || (row->reversed_p
21046 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21047 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21048 {
21049 int i, n;
21050
21051 if (!row->reversed_p)
21052 {
21053 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21054 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21055 break;
21056 }
21057 else
21058 {
21059 for (i = 0; i < row->used[TEXT_AREA]; i++)
21060 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21061 break;
21062 /* Remove any padding glyphs at the front of ROW, to
21063 make room for the truncation glyphs we will be
21064 adding below. The loop below always inserts at
21065 least one truncation glyph, so also remove the
21066 last glyph added to ROW. */
21067 unproduce_glyphs (it, i + 1);
21068 /* Adjust i for the loop below. */
21069 i = row->used[TEXT_AREA] - (i + 1);
21070 }
21071
21072 /* produce_special_glyphs overwrites the last glyph, so
21073 we don't want that if we want to keep that last
21074 glyph, which means it's an image. */
21075 if (it->current_x > it->last_visible_x)
21076 {
21077 it->current_x = x_before;
21078 if (!FRAME_WINDOW_P (it->f))
21079 {
21080 for (n = row->used[TEXT_AREA]; i < n; ++i)
21081 {
21082 row->used[TEXT_AREA] = i;
21083 produce_special_glyphs (it, IT_TRUNCATION);
21084 }
21085 }
21086 else
21087 {
21088 row->used[TEXT_AREA] = i;
21089 produce_special_glyphs (it, IT_TRUNCATION);
21090 }
21091 it->hpos = hpos_before;
21092 }
21093 }
21094 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21095 {
21096 /* Don't truncate if we can overflow newline into fringe. */
21097 if (!get_next_display_element (it))
21098 {
21099 it->continuation_lines_width = 0;
21100 row->ends_at_zv_p = true;
21101 row->exact_window_width_line_p = true;
21102 break;
21103 }
21104 if (ITERATOR_AT_END_OF_LINE_P (it))
21105 {
21106 row->exact_window_width_line_p = true;
21107 goto at_end_of_line;
21108 }
21109 it->current_x = x_before;
21110 it->hpos = hpos_before;
21111 }
21112
21113 row->truncated_on_right_p = true;
21114 it->continuation_lines_width = 0;
21115 reseat_at_next_visible_line_start (it, false);
21116 /* We insist below that IT's position be at ZV because in
21117 bidi-reordered lines the character at visible line start
21118 might not be the character that follows the newline in
21119 the logical order. */
21120 if (IT_BYTEPOS (*it) > BEG_BYTE)
21121 row->ends_at_zv_p =
21122 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21123 else
21124 row->ends_at_zv_p = false;
21125 break;
21126 }
21127 }
21128
21129 if (wrap_data)
21130 bidi_unshelve_cache (wrap_data, true);
21131
21132 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21133 at the left window margin. */
21134 if (it->first_visible_x
21135 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21136 {
21137 if (!FRAME_WINDOW_P (it->f)
21138 || (((row->reversed_p
21139 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21140 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21141 /* Don't let insert_left_trunc_glyphs overwrite the
21142 first glyph of the row if it is an image. */
21143 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21144 insert_left_trunc_glyphs (it);
21145 row->truncated_on_left_p = true;
21146 }
21147
21148 /* Remember the position at which this line ends.
21149
21150 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21151 cannot be before the call to find_row_edges below, since that is
21152 where these positions are determined. */
21153 row->end = it->current;
21154 if (!it->bidi_p)
21155 {
21156 row->minpos = row->start.pos;
21157 row->maxpos = row->end.pos;
21158 }
21159 else
21160 {
21161 /* ROW->minpos and ROW->maxpos must be the smallest and
21162 `1 + the largest' buffer positions in ROW. But if ROW was
21163 bidi-reordered, these two positions can be anywhere in the
21164 row, so we must determine them now. */
21165 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21166 }
21167
21168 /* If the start of this line is the overlay arrow-position, then
21169 mark this glyph row as the one containing the overlay arrow.
21170 This is clearly a mess with variable size fonts. It would be
21171 better to let it be displayed like cursors under X. */
21172 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21173 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21174 !NILP (overlay_arrow_string)))
21175 {
21176 /* Overlay arrow in window redisplay is a fringe bitmap. */
21177 if (STRINGP (overlay_arrow_string))
21178 {
21179 struct glyph_row *arrow_row
21180 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21181 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21182 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21183 struct glyph *p = row->glyphs[TEXT_AREA];
21184 struct glyph *p2, *end;
21185
21186 /* Copy the arrow glyphs. */
21187 while (glyph < arrow_end)
21188 *p++ = *glyph++;
21189
21190 /* Throw away padding glyphs. */
21191 p2 = p;
21192 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21193 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21194 ++p2;
21195 if (p2 > p)
21196 {
21197 while (p2 < end)
21198 *p++ = *p2++;
21199 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21200 }
21201 }
21202 else
21203 {
21204 eassert (INTEGERP (overlay_arrow_string));
21205 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21206 }
21207 overlay_arrow_seen = true;
21208 }
21209
21210 /* Highlight trailing whitespace. */
21211 if (!NILP (Vshow_trailing_whitespace))
21212 highlight_trailing_whitespace (it->f, it->glyph_row);
21213
21214 /* Compute pixel dimensions of this line. */
21215 compute_line_metrics (it);
21216
21217 /* Implementation note: No changes in the glyphs of ROW or in their
21218 faces can be done past this point, because compute_line_metrics
21219 computes ROW's hash value and stores it within the glyph_row
21220 structure. */
21221
21222 /* Record whether this row ends inside an ellipsis. */
21223 row->ends_in_ellipsis_p
21224 = (it->method == GET_FROM_DISPLAY_VECTOR
21225 && it->ellipsis_p);
21226
21227 /* Save fringe bitmaps in this row. */
21228 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
21229 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
21230 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
21231 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
21232
21233 it->left_user_fringe_bitmap = 0;
21234 it->left_user_fringe_face_id = 0;
21235 it->right_user_fringe_bitmap = 0;
21236 it->right_user_fringe_face_id = 0;
21237
21238 /* Maybe set the cursor. */
21239 cvpos = it->w->cursor.vpos;
21240 if ((cvpos < 0
21241 /* In bidi-reordered rows, keep checking for proper cursor
21242 position even if one has been found already, because buffer
21243 positions in such rows change non-linearly with ROW->VPOS,
21244 when a line is continued. One exception: when we are at ZV,
21245 display cursor on the first suitable glyph row, since all
21246 the empty rows after that also have their position set to ZV. */
21247 /* FIXME: Revisit this when glyph ``spilling'' in continuation
21248 lines' rows is implemented for bidi-reordered rows. */
21249 || (it->bidi_p
21250 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
21251 && PT >= MATRIX_ROW_START_CHARPOS (row)
21252 && PT <= MATRIX_ROW_END_CHARPOS (row)
21253 && cursor_row_p (row))
21254 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21255
21256 /* Prepare for the next line. This line starts horizontally at (X
21257 HPOS) = (0 0). Vertical positions are incremented. As a
21258 convenience for the caller, IT->glyph_row is set to the next
21259 row to be used. */
21260 it->current_x = it->hpos = 0;
21261 it->current_y += row->height;
21262 SET_TEXT_POS (it->eol_pos, 0, 0);
21263 ++it->vpos;
21264 ++it->glyph_row;
21265 /* The next row should by default use the same value of the
21266 reversed_p flag as this one. set_iterator_to_next decides when
21267 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21268 the flag accordingly. */
21269 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21270 it->glyph_row->reversed_p = row->reversed_p;
21271 it->start = row->end;
21272 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21273
21274 #undef RECORD_MAX_MIN_POS
21275 }
21276
21277 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21278 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21279 doc: /* Return paragraph direction at point in BUFFER.
21280 Value is either `left-to-right' or `right-to-left'.
21281 If BUFFER is omitted or nil, it defaults to the current buffer.
21282
21283 Paragraph direction determines how the text in the paragraph is displayed.
21284 In left-to-right paragraphs, text begins at the left margin of the window
21285 and the reading direction is generally left to right. In right-to-left
21286 paragraphs, text begins at the right margin and is read from right to left.
21287
21288 See also `bidi-paragraph-direction'. */)
21289 (Lisp_Object buffer)
21290 {
21291 struct buffer *buf = current_buffer;
21292 struct buffer *old = buf;
21293
21294 if (! NILP (buffer))
21295 {
21296 CHECK_BUFFER (buffer);
21297 buf = XBUFFER (buffer);
21298 }
21299
21300 if (NILP (BVAR (buf, bidi_display_reordering))
21301 || NILP (BVAR (buf, enable_multibyte_characters))
21302 /* When we are loading loadup.el, the character property tables
21303 needed for bidi iteration are not yet available. */
21304 || redisplay__inhibit_bidi)
21305 return Qleft_to_right;
21306 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21307 return BVAR (buf, bidi_paragraph_direction);
21308 else
21309 {
21310 /* Determine the direction from buffer text. We could try to
21311 use current_matrix if it is up to date, but this seems fast
21312 enough as it is. */
21313 struct bidi_it itb;
21314 ptrdiff_t pos = BUF_PT (buf);
21315 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21316 int c;
21317 void *itb_data = bidi_shelve_cache ();
21318
21319 set_buffer_temp (buf);
21320 /* bidi_paragraph_init finds the base direction of the paragraph
21321 by searching forward from paragraph start. We need the base
21322 direction of the current or _previous_ paragraph, so we need
21323 to make sure we are within that paragraph. To that end, find
21324 the previous non-empty line. */
21325 if (pos >= ZV && pos > BEGV)
21326 DEC_BOTH (pos, bytepos);
21327 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21328 if (fast_looking_at (trailing_white_space,
21329 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21330 {
21331 while ((c = FETCH_BYTE (bytepos)) == '\n'
21332 || c == ' ' || c == '\t' || c == '\f')
21333 {
21334 if (bytepos <= BEGV_BYTE)
21335 break;
21336 bytepos--;
21337 pos--;
21338 }
21339 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21340 bytepos--;
21341 }
21342 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21343 itb.paragraph_dir = NEUTRAL_DIR;
21344 itb.string.s = NULL;
21345 itb.string.lstring = Qnil;
21346 itb.string.bufpos = 0;
21347 itb.string.from_disp_str = false;
21348 itb.string.unibyte = false;
21349 /* We have no window to use here for ignoring window-specific
21350 overlays. Using NULL for window pointer will cause
21351 compute_display_string_pos to use the current buffer. */
21352 itb.w = NULL;
21353 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21354 bidi_unshelve_cache (itb_data, false);
21355 set_buffer_temp (old);
21356 switch (itb.paragraph_dir)
21357 {
21358 case L2R:
21359 return Qleft_to_right;
21360 break;
21361 case R2L:
21362 return Qright_to_left;
21363 break;
21364 default:
21365 emacs_abort ();
21366 }
21367 }
21368 }
21369
21370 DEFUN ("bidi-find-overridden-directionality",
21371 Fbidi_find_overridden_directionality,
21372 Sbidi_find_overridden_directionality, 2, 3, 0,
21373 doc: /* Return position between FROM and TO where directionality was overridden.
21374
21375 This function returns the first character position in the specified
21376 region of OBJECT where there is a character whose `bidi-class' property
21377 is `L', but which was forced to display as `R' by a directional
21378 override, and likewise with characters whose `bidi-class' is `R'
21379 or `AL' that were forced to display as `L'.
21380
21381 If no such character is found, the function returns nil.
21382
21383 OBJECT is a Lisp string or buffer to search for overridden
21384 directionality, and defaults to the current buffer if nil or omitted.
21385 OBJECT can also be a window, in which case the function will search
21386 the buffer displayed in that window. Passing the window instead of
21387 a buffer is preferable when the buffer is displayed in some window,
21388 because this function will then be able to correctly account for
21389 window-specific overlays, which can affect the results.
21390
21391 Strong directional characters `L', `R', and `AL' can have their
21392 intrinsic directionality overridden by directional override
21393 control characters RLO (u+202e) and LRO (u+202d). See the
21394 function `get-char-code-property' for a way to inquire about
21395 the `bidi-class' property of a character. */)
21396 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21397 {
21398 struct buffer *buf = current_buffer;
21399 struct buffer *old = buf;
21400 struct window *w = NULL;
21401 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21402 struct bidi_it itb;
21403 ptrdiff_t from_pos, to_pos, from_bpos;
21404 void *itb_data;
21405
21406 if (!NILP (object))
21407 {
21408 if (BUFFERP (object))
21409 buf = XBUFFER (object);
21410 else if (WINDOWP (object))
21411 {
21412 w = decode_live_window (object);
21413 buf = XBUFFER (w->contents);
21414 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21415 }
21416 else
21417 CHECK_STRING (object);
21418 }
21419
21420 if (STRINGP (object))
21421 {
21422 /* Characters in unibyte strings are always treated by bidi.c as
21423 strong LTR. */
21424 if (!STRING_MULTIBYTE (object)
21425 /* When we are loading loadup.el, the character property
21426 tables needed for bidi iteration are not yet
21427 available. */
21428 || redisplay__inhibit_bidi)
21429 return Qnil;
21430
21431 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21432 if (from_pos >= SCHARS (object))
21433 return Qnil;
21434
21435 /* Set up the bidi iterator. */
21436 itb_data = bidi_shelve_cache ();
21437 itb.paragraph_dir = NEUTRAL_DIR;
21438 itb.string.lstring = object;
21439 itb.string.s = NULL;
21440 itb.string.schars = SCHARS (object);
21441 itb.string.bufpos = 0;
21442 itb.string.from_disp_str = false;
21443 itb.string.unibyte = false;
21444 itb.w = w;
21445 bidi_init_it (0, 0, frame_window_p, &itb);
21446 }
21447 else
21448 {
21449 /* Nothing this fancy can happen in unibyte buffers, or in a
21450 buffer that disabled reordering, or if FROM is at EOB. */
21451 if (NILP (BVAR (buf, bidi_display_reordering))
21452 || NILP (BVAR (buf, enable_multibyte_characters))
21453 /* When we are loading loadup.el, the character property
21454 tables needed for bidi iteration are not yet
21455 available. */
21456 || redisplay__inhibit_bidi)
21457 return Qnil;
21458
21459 set_buffer_temp (buf);
21460 validate_region (&from, &to);
21461 from_pos = XINT (from);
21462 to_pos = XINT (to);
21463 if (from_pos >= ZV)
21464 return Qnil;
21465
21466 /* Set up the bidi iterator. */
21467 itb_data = bidi_shelve_cache ();
21468 from_bpos = CHAR_TO_BYTE (from_pos);
21469 if (from_pos == BEGV)
21470 {
21471 itb.charpos = BEGV;
21472 itb.bytepos = BEGV_BYTE;
21473 }
21474 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21475 {
21476 itb.charpos = from_pos;
21477 itb.bytepos = from_bpos;
21478 }
21479 else
21480 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21481 -1, &itb.bytepos);
21482 itb.paragraph_dir = NEUTRAL_DIR;
21483 itb.string.s = NULL;
21484 itb.string.lstring = Qnil;
21485 itb.string.bufpos = 0;
21486 itb.string.from_disp_str = false;
21487 itb.string.unibyte = false;
21488 itb.w = w;
21489 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21490 }
21491
21492 ptrdiff_t found;
21493 do {
21494 /* For the purposes of this function, the actual base direction of
21495 the paragraph doesn't matter, so just set it to L2R. */
21496 bidi_paragraph_init (L2R, &itb, false);
21497 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21498 ;
21499 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21500
21501 bidi_unshelve_cache (itb_data, false);
21502 set_buffer_temp (old);
21503
21504 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21505 }
21506
21507 DEFUN ("move-point-visually", Fmove_point_visually,
21508 Smove_point_visually, 1, 1, 0,
21509 doc: /* Move point in the visual order in the specified DIRECTION.
21510 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21511 left.
21512
21513 Value is the new character position of point. */)
21514 (Lisp_Object direction)
21515 {
21516 struct window *w = XWINDOW (selected_window);
21517 struct buffer *b = XBUFFER (w->contents);
21518 struct glyph_row *row;
21519 int dir;
21520 Lisp_Object paragraph_dir;
21521
21522 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21523 (!(ROW)->continued_p \
21524 && NILP ((GLYPH)->object) \
21525 && (GLYPH)->type == CHAR_GLYPH \
21526 && (GLYPH)->u.ch == ' ' \
21527 && (GLYPH)->charpos >= 0 \
21528 && !(GLYPH)->avoid_cursor_p)
21529
21530 CHECK_NUMBER (direction);
21531 dir = XINT (direction);
21532 if (dir > 0)
21533 dir = 1;
21534 else
21535 dir = -1;
21536
21537 /* If current matrix is up-to-date, we can use the information
21538 recorded in the glyphs, at least as long as the goal is on the
21539 screen. */
21540 if (w->window_end_valid
21541 && !windows_or_buffers_changed
21542 && b
21543 && !b->clip_changed
21544 && !b->prevent_redisplay_optimizations_p
21545 && !window_outdated (w)
21546 /* We rely below on the cursor coordinates to be up to date, but
21547 we cannot trust them if some command moved point since the
21548 last complete redisplay. */
21549 && w->last_point == BUF_PT (b)
21550 && w->cursor.vpos >= 0
21551 && w->cursor.vpos < w->current_matrix->nrows
21552 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21553 {
21554 struct glyph *g = row->glyphs[TEXT_AREA];
21555 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21556 struct glyph *gpt = g + w->cursor.hpos;
21557
21558 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21559 {
21560 if (BUFFERP (g->object) && g->charpos != PT)
21561 {
21562 SET_PT (g->charpos);
21563 w->cursor.vpos = -1;
21564 return make_number (PT);
21565 }
21566 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21567 {
21568 ptrdiff_t new_pos;
21569
21570 if (BUFFERP (gpt->object))
21571 {
21572 new_pos = PT;
21573 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21574 new_pos += (row->reversed_p ? -dir : dir);
21575 else
21576 new_pos -= (row->reversed_p ? -dir : dir);
21577 }
21578 else if (BUFFERP (g->object))
21579 new_pos = g->charpos;
21580 else
21581 break;
21582 SET_PT (new_pos);
21583 w->cursor.vpos = -1;
21584 return make_number (PT);
21585 }
21586 else if (ROW_GLYPH_NEWLINE_P (row, g))
21587 {
21588 /* Glyphs inserted at the end of a non-empty line for
21589 positioning the cursor have zero charpos, so we must
21590 deduce the value of point by other means. */
21591 if (g->charpos > 0)
21592 SET_PT (g->charpos);
21593 else if (row->ends_at_zv_p && PT != ZV)
21594 SET_PT (ZV);
21595 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21596 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21597 else
21598 break;
21599 w->cursor.vpos = -1;
21600 return make_number (PT);
21601 }
21602 }
21603 if (g == e || NILP (g->object))
21604 {
21605 if (row->truncated_on_left_p || row->truncated_on_right_p)
21606 goto simulate_display;
21607 if (!row->reversed_p)
21608 row += dir;
21609 else
21610 row -= dir;
21611 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21612 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21613 goto simulate_display;
21614
21615 if (dir > 0)
21616 {
21617 if (row->reversed_p && !row->continued_p)
21618 {
21619 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21620 w->cursor.vpos = -1;
21621 return make_number (PT);
21622 }
21623 g = row->glyphs[TEXT_AREA];
21624 e = g + row->used[TEXT_AREA];
21625 for ( ; g < e; g++)
21626 {
21627 if (BUFFERP (g->object)
21628 /* Empty lines have only one glyph, which stands
21629 for the newline, and whose charpos is the
21630 buffer position of the newline. */
21631 || ROW_GLYPH_NEWLINE_P (row, g)
21632 /* When the buffer ends in a newline, the line at
21633 EOB also has one glyph, but its charpos is -1. */
21634 || (row->ends_at_zv_p
21635 && !row->reversed_p
21636 && NILP (g->object)
21637 && g->type == CHAR_GLYPH
21638 && g->u.ch == ' '))
21639 {
21640 if (g->charpos > 0)
21641 SET_PT (g->charpos);
21642 else if (!row->reversed_p
21643 && row->ends_at_zv_p
21644 && PT != ZV)
21645 SET_PT (ZV);
21646 else
21647 continue;
21648 w->cursor.vpos = -1;
21649 return make_number (PT);
21650 }
21651 }
21652 }
21653 else
21654 {
21655 if (!row->reversed_p && !row->continued_p)
21656 {
21657 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21658 w->cursor.vpos = -1;
21659 return make_number (PT);
21660 }
21661 e = row->glyphs[TEXT_AREA];
21662 g = e + row->used[TEXT_AREA] - 1;
21663 for ( ; g >= e; g--)
21664 {
21665 if (BUFFERP (g->object)
21666 || (ROW_GLYPH_NEWLINE_P (row, g)
21667 && g->charpos > 0)
21668 /* Empty R2L lines on GUI frames have the buffer
21669 position of the newline stored in the stretch
21670 glyph. */
21671 || g->type == STRETCH_GLYPH
21672 || (row->ends_at_zv_p
21673 && row->reversed_p
21674 && NILP (g->object)
21675 && g->type == CHAR_GLYPH
21676 && g->u.ch == ' '))
21677 {
21678 if (g->charpos > 0)
21679 SET_PT (g->charpos);
21680 else if (row->reversed_p
21681 && row->ends_at_zv_p
21682 && PT != ZV)
21683 SET_PT (ZV);
21684 else
21685 continue;
21686 w->cursor.vpos = -1;
21687 return make_number (PT);
21688 }
21689 }
21690 }
21691 }
21692 }
21693
21694 simulate_display:
21695
21696 /* If we wind up here, we failed to move by using the glyphs, so we
21697 need to simulate display instead. */
21698
21699 if (b)
21700 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21701 else
21702 paragraph_dir = Qleft_to_right;
21703 if (EQ (paragraph_dir, Qright_to_left))
21704 dir = -dir;
21705 if (PT <= BEGV && dir < 0)
21706 xsignal0 (Qbeginning_of_buffer);
21707 else if (PT >= ZV && dir > 0)
21708 xsignal0 (Qend_of_buffer);
21709 else
21710 {
21711 struct text_pos pt;
21712 struct it it;
21713 int pt_x, target_x, pixel_width, pt_vpos;
21714 bool at_eol_p;
21715 bool overshoot_expected = false;
21716 bool target_is_eol_p = false;
21717
21718 /* Setup the arena. */
21719 SET_TEXT_POS (pt, PT, PT_BYTE);
21720 start_display (&it, w, pt);
21721 /* When lines are truncated, we could be called with point
21722 outside of the windows edges, in which case move_it_*
21723 functions either prematurely stop at window's edge or jump to
21724 the next screen line, whereas we rely below on our ability to
21725 reach point, in order to start from its X coordinate. So we
21726 need to disregard the window's horizontal extent in that case. */
21727 if (it.line_wrap == TRUNCATE)
21728 it.last_visible_x = INFINITY;
21729
21730 if (it.cmp_it.id < 0
21731 && it.method == GET_FROM_STRING
21732 && it.area == TEXT_AREA
21733 && it.string_from_display_prop_p
21734 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21735 overshoot_expected = true;
21736
21737 /* Find the X coordinate of point. We start from the beginning
21738 of this or previous line to make sure we are before point in
21739 the logical order (since the move_it_* functions can only
21740 move forward). */
21741 reseat:
21742 reseat_at_previous_visible_line_start (&it);
21743 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21744 if (IT_CHARPOS (it) != PT)
21745 {
21746 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21747 -1, -1, -1, MOVE_TO_POS);
21748 /* If we missed point because the character there is
21749 displayed out of a display vector that has more than one
21750 glyph, retry expecting overshoot. */
21751 if (it.method == GET_FROM_DISPLAY_VECTOR
21752 && it.current.dpvec_index > 0
21753 && !overshoot_expected)
21754 {
21755 overshoot_expected = true;
21756 goto reseat;
21757 }
21758 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21759 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21760 }
21761 pt_x = it.current_x;
21762 pt_vpos = it.vpos;
21763 if (dir > 0 || overshoot_expected)
21764 {
21765 struct glyph_row *row = it.glyph_row;
21766
21767 /* When point is at beginning of line, we don't have
21768 information about the glyph there loaded into struct
21769 it. Calling get_next_display_element fixes that. */
21770 if (pt_x == 0)
21771 get_next_display_element (&it);
21772 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21773 it.glyph_row = NULL;
21774 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21775 it.glyph_row = row;
21776 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21777 it, lest it will become out of sync with it's buffer
21778 position. */
21779 it.current_x = pt_x;
21780 }
21781 else
21782 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21783 pixel_width = it.pixel_width;
21784 if (overshoot_expected && at_eol_p)
21785 pixel_width = 0;
21786 else if (pixel_width <= 0)
21787 pixel_width = 1;
21788
21789 /* If there's a display string (or something similar) at point,
21790 we are actually at the glyph to the left of point, so we need
21791 to correct the X coordinate. */
21792 if (overshoot_expected)
21793 {
21794 if (it.bidi_p)
21795 pt_x += pixel_width * it.bidi_it.scan_dir;
21796 else
21797 pt_x += pixel_width;
21798 }
21799
21800 /* Compute target X coordinate, either to the left or to the
21801 right of point. On TTY frames, all characters have the same
21802 pixel width of 1, so we can use that. On GUI frames we don't
21803 have an easy way of getting at the pixel width of the
21804 character to the left of point, so we use a different method
21805 of getting to that place. */
21806 if (dir > 0)
21807 target_x = pt_x + pixel_width;
21808 else
21809 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21810
21811 /* Target X coordinate could be one line above or below the line
21812 of point, in which case we need to adjust the target X
21813 coordinate. Also, if moving to the left, we need to begin at
21814 the left edge of the point's screen line. */
21815 if (dir < 0)
21816 {
21817 if (pt_x > 0)
21818 {
21819 start_display (&it, w, pt);
21820 if (it.line_wrap == TRUNCATE)
21821 it.last_visible_x = INFINITY;
21822 reseat_at_previous_visible_line_start (&it);
21823 it.current_x = it.current_y = it.hpos = 0;
21824 if (pt_vpos != 0)
21825 move_it_by_lines (&it, pt_vpos);
21826 }
21827 else
21828 {
21829 move_it_by_lines (&it, -1);
21830 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21831 target_is_eol_p = true;
21832 /* Under word-wrap, we don't know the x coordinate of
21833 the last character displayed on the previous line,
21834 which immediately precedes the wrap point. To find
21835 out its x coordinate, we try moving to the right
21836 margin of the window, which will stop at the wrap
21837 point, and then reset target_x to point at the
21838 character that precedes the wrap point. This is not
21839 needed on GUI frames, because (see below) there we
21840 move from the left margin one grapheme cluster at a
21841 time, and stop when we hit the wrap point. */
21842 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21843 {
21844 void *it_data = NULL;
21845 struct it it2;
21846
21847 SAVE_IT (it2, it, it_data);
21848 move_it_in_display_line_to (&it, ZV, target_x,
21849 MOVE_TO_POS | MOVE_TO_X);
21850 /* If we arrived at target_x, that _is_ the last
21851 character on the previous line. */
21852 if (it.current_x != target_x)
21853 target_x = it.current_x - 1;
21854 RESTORE_IT (&it, &it2, it_data);
21855 }
21856 }
21857 }
21858 else
21859 {
21860 if (at_eol_p
21861 || (target_x >= it.last_visible_x
21862 && it.line_wrap != TRUNCATE))
21863 {
21864 if (pt_x > 0)
21865 move_it_by_lines (&it, 0);
21866 move_it_by_lines (&it, 1);
21867 target_x = 0;
21868 }
21869 }
21870
21871 /* Move to the target X coordinate. */
21872 /* On GUI frames, as we don't know the X coordinate of the
21873 character to the left of point, moving point to the left
21874 requires walking, one grapheme cluster at a time, until we
21875 find ourself at a place immediately to the left of the
21876 character at point. */
21877 if (FRAME_WINDOW_P (it.f) && dir < 0)
21878 {
21879 struct text_pos new_pos;
21880 enum move_it_result rc = MOVE_X_REACHED;
21881
21882 if (it.current_x == 0)
21883 get_next_display_element (&it);
21884 if (it.what == IT_COMPOSITION)
21885 {
21886 new_pos.charpos = it.cmp_it.charpos;
21887 new_pos.bytepos = -1;
21888 }
21889 else
21890 new_pos = it.current.pos;
21891
21892 while (it.current_x + it.pixel_width <= target_x
21893 && (rc == MOVE_X_REACHED
21894 /* Under word-wrap, move_it_in_display_line_to
21895 stops at correct coordinates, but sometimes
21896 returns MOVE_POS_MATCH_OR_ZV. */
21897 || (it.line_wrap == WORD_WRAP
21898 && rc == MOVE_POS_MATCH_OR_ZV)))
21899 {
21900 int new_x = it.current_x + it.pixel_width;
21901
21902 /* For composed characters, we want the position of the
21903 first character in the grapheme cluster (usually, the
21904 composition's base character), whereas it.current
21905 might give us the position of the _last_ one, e.g. if
21906 the composition is rendered in reverse due to bidi
21907 reordering. */
21908 if (it.what == IT_COMPOSITION)
21909 {
21910 new_pos.charpos = it.cmp_it.charpos;
21911 new_pos.bytepos = -1;
21912 }
21913 else
21914 new_pos = it.current.pos;
21915 if (new_x == it.current_x)
21916 new_x++;
21917 rc = move_it_in_display_line_to (&it, ZV, new_x,
21918 MOVE_TO_POS | MOVE_TO_X);
21919 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21920 break;
21921 }
21922 /* The previous position we saw in the loop is the one we
21923 want. */
21924 if (new_pos.bytepos == -1)
21925 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21926 it.current.pos = new_pos;
21927 }
21928 else if (it.current_x != target_x)
21929 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21930
21931 /* If we ended up in a display string that covers point, move to
21932 buffer position to the right in the visual order. */
21933 if (dir > 0)
21934 {
21935 while (IT_CHARPOS (it) == PT)
21936 {
21937 set_iterator_to_next (&it, false);
21938 if (!get_next_display_element (&it))
21939 break;
21940 }
21941 }
21942
21943 /* Move point to that position. */
21944 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21945 }
21946
21947 return make_number (PT);
21948
21949 #undef ROW_GLYPH_NEWLINE_P
21950 }
21951
21952 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21953 Sbidi_resolved_levels, 0, 1, 0,
21954 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21955
21956 The resolved levels are produced by the Emacs bidi reordering engine
21957 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21958 read the Unicode Standard Annex 9 (UAX#9) for background information
21959 about these levels.
21960
21961 VPOS is the zero-based number of the current window's screen line
21962 for which to produce the resolved levels. If VPOS is nil or omitted,
21963 it defaults to the screen line of point. If the window displays a
21964 header line, VPOS of zero will report on the header line, and first
21965 line of text in the window will have VPOS of 1.
21966
21967 Value is an array of resolved levels, indexed by glyph number.
21968 Glyphs are numbered from zero starting from the beginning of the
21969 screen line, i.e. the left edge of the window for left-to-right lines
21970 and from the right edge for right-to-left lines. The resolved levels
21971 are produced only for the window's text area; text in display margins
21972 is not included.
21973
21974 If the selected window's display is not up-to-date, or if the specified
21975 screen line does not display text, this function returns nil. It is
21976 highly recommended to bind this function to some simple key, like F8,
21977 in order to avoid these problems.
21978
21979 This function exists mainly for testing the correctness of the
21980 Emacs UBA implementation, in particular with the test suite. */)
21981 (Lisp_Object vpos)
21982 {
21983 struct window *w = XWINDOW (selected_window);
21984 struct buffer *b = XBUFFER (w->contents);
21985 int nrow;
21986 struct glyph_row *row;
21987
21988 if (NILP (vpos))
21989 {
21990 int d1, d2, d3, d4, d5;
21991
21992 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21993 }
21994 else
21995 {
21996 CHECK_NUMBER_COERCE_MARKER (vpos);
21997 nrow = XINT (vpos);
21998 }
21999
22000 /* We require up-to-date glyph matrix for this window. */
22001 if (w->window_end_valid
22002 && !windows_or_buffers_changed
22003 && b
22004 && !b->clip_changed
22005 && !b->prevent_redisplay_optimizations_p
22006 && !window_outdated (w)
22007 && nrow >= 0
22008 && nrow < w->current_matrix->nrows
22009 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22010 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22011 {
22012 struct glyph *g, *e, *g1;
22013 int nglyphs, i;
22014 Lisp_Object levels;
22015
22016 if (!row->reversed_p) /* Left-to-right glyph row. */
22017 {
22018 g = g1 = row->glyphs[TEXT_AREA];
22019 e = g + row->used[TEXT_AREA];
22020
22021 /* Skip over glyphs at the start of the row that was
22022 generated by redisplay for its own needs. */
22023 while (g < e
22024 && NILP (g->object)
22025 && g->charpos < 0)
22026 g++;
22027 g1 = g;
22028
22029 /* Count the "interesting" glyphs in this row. */
22030 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22031 nglyphs++;
22032
22033 /* Create and fill the array. */
22034 levels = make_uninit_vector (nglyphs);
22035 for (i = 0; g1 < g; i++, g1++)
22036 ASET (levels, i, make_number (g1->resolved_level));
22037 }
22038 else /* Right-to-left glyph row. */
22039 {
22040 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22041 e = row->glyphs[TEXT_AREA] - 1;
22042 while (g > e
22043 && NILP (g->object)
22044 && g->charpos < 0)
22045 g--;
22046 g1 = g;
22047 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22048 nglyphs++;
22049 levels = make_uninit_vector (nglyphs);
22050 for (i = 0; g1 > g; i++, g1--)
22051 ASET (levels, i, make_number (g1->resolved_level));
22052 }
22053 return levels;
22054 }
22055 else
22056 return Qnil;
22057 }
22058
22059
22060 \f
22061 /***********************************************************************
22062 Menu Bar
22063 ***********************************************************************/
22064
22065 /* Redisplay the menu bar in the frame for window W.
22066
22067 The menu bar of X frames that don't have X toolkit support is
22068 displayed in a special window W->frame->menu_bar_window.
22069
22070 The menu bar of terminal frames is treated specially as far as
22071 glyph matrices are concerned. Menu bar lines are not part of
22072 windows, so the update is done directly on the frame matrix rows
22073 for the menu bar. */
22074
22075 static void
22076 display_menu_bar (struct window *w)
22077 {
22078 struct frame *f = XFRAME (WINDOW_FRAME (w));
22079 struct it it;
22080 Lisp_Object items;
22081 int i;
22082
22083 /* Don't do all this for graphical frames. */
22084 #ifdef HAVE_NTGUI
22085 if (FRAME_W32_P (f))
22086 return;
22087 #endif
22088 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22089 if (FRAME_X_P (f))
22090 return;
22091 #endif
22092
22093 #ifdef HAVE_NS
22094 if (FRAME_NS_P (f))
22095 return;
22096 #endif /* HAVE_NS */
22097
22098 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22099 eassert (!FRAME_WINDOW_P (f));
22100 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22101 it.first_visible_x = 0;
22102 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22103 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22104 if (FRAME_WINDOW_P (f))
22105 {
22106 /* Menu bar lines are displayed in the desired matrix of the
22107 dummy window menu_bar_window. */
22108 struct window *menu_w;
22109 menu_w = XWINDOW (f->menu_bar_window);
22110 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22111 MENU_FACE_ID);
22112 it.first_visible_x = 0;
22113 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22114 }
22115 else
22116 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22117 {
22118 /* This is a TTY frame, i.e. character hpos/vpos are used as
22119 pixel x/y. */
22120 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22121 MENU_FACE_ID);
22122 it.first_visible_x = 0;
22123 it.last_visible_x = FRAME_COLS (f);
22124 }
22125
22126 /* FIXME: This should be controlled by a user option. See the
22127 comments in redisplay_tool_bar and display_mode_line about
22128 this. */
22129 it.paragraph_embedding = L2R;
22130
22131 /* Clear all rows of the menu bar. */
22132 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22133 {
22134 struct glyph_row *row = it.glyph_row + i;
22135 clear_glyph_row (row);
22136 row->enabled_p = true;
22137 row->full_width_p = true;
22138 row->reversed_p = false;
22139 }
22140
22141 /* Display all items of the menu bar. */
22142 items = FRAME_MENU_BAR_ITEMS (it.f);
22143 for (i = 0; i < ASIZE (items); i += 4)
22144 {
22145 Lisp_Object string;
22146
22147 /* Stop at nil string. */
22148 string = AREF (items, i + 1);
22149 if (NILP (string))
22150 break;
22151
22152 /* Remember where item was displayed. */
22153 ASET (items, i + 3, make_number (it.hpos));
22154
22155 /* Display the item, pad with one space. */
22156 if (it.current_x < it.last_visible_x)
22157 display_string (NULL, string, Qnil, 0, 0, &it,
22158 SCHARS (string) + 1, 0, 0, -1);
22159 }
22160
22161 /* Fill out the line with spaces. */
22162 if (it.current_x < it.last_visible_x)
22163 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22164
22165 /* Compute the total height of the lines. */
22166 compute_line_metrics (&it);
22167 }
22168
22169 /* Deep copy of a glyph row, including the glyphs. */
22170 static void
22171 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22172 {
22173 struct glyph *pointers[1 + LAST_AREA];
22174 int to_used = to->used[TEXT_AREA];
22175
22176 /* Save glyph pointers of TO. */
22177 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22178
22179 /* Do a structure assignment. */
22180 *to = *from;
22181
22182 /* Restore original glyph pointers of TO. */
22183 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22184
22185 /* Copy the glyphs. */
22186 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22187 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22188
22189 /* If we filled only part of the TO row, fill the rest with
22190 space_glyph (which will display as empty space). */
22191 if (to_used > from->used[TEXT_AREA])
22192 fill_up_frame_row_with_spaces (to, to_used);
22193 }
22194
22195 /* Display one menu item on a TTY, by overwriting the glyphs in the
22196 frame F's desired glyph matrix with glyphs produced from the menu
22197 item text. Called from term.c to display TTY drop-down menus one
22198 item at a time.
22199
22200 ITEM_TEXT is the menu item text as a C string.
22201
22202 FACE_ID is the face ID to be used for this menu item. FACE_ID
22203 could specify one of 3 faces: a face for an enabled item, a face
22204 for a disabled item, or a face for a selected item.
22205
22206 X and Y are coordinates of the first glyph in the frame's desired
22207 matrix to be overwritten by the menu item. Since this is a TTY, Y
22208 is the zero-based number of the glyph row and X is the zero-based
22209 glyph number in the row, starting from left, where to start
22210 displaying the item.
22211
22212 SUBMENU means this menu item drops down a submenu, which
22213 should be indicated by displaying a proper visual cue after the
22214 item text. */
22215
22216 void
22217 display_tty_menu_item (const char *item_text, int width, int face_id,
22218 int x, int y, bool submenu)
22219 {
22220 struct it it;
22221 struct frame *f = SELECTED_FRAME ();
22222 struct window *w = XWINDOW (f->selected_window);
22223 struct glyph_row *row;
22224 size_t item_len = strlen (item_text);
22225
22226 eassert (FRAME_TERMCAP_P (f));
22227
22228 /* Don't write beyond the matrix's last row. This can happen for
22229 TTY screens that are not high enough to show the entire menu.
22230 (This is actually a bit of defensive programming, as
22231 tty_menu_display already limits the number of menu items to one
22232 less than the number of screen lines.) */
22233 if (y >= f->desired_matrix->nrows)
22234 return;
22235
22236 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
22237 it.first_visible_x = 0;
22238 it.last_visible_x = FRAME_COLS (f) - 1;
22239 row = it.glyph_row;
22240 /* Start with the row contents from the current matrix. */
22241 deep_copy_glyph_row (row, f->current_matrix->rows + y);
22242 bool saved_width = row->full_width_p;
22243 row->full_width_p = true;
22244 bool saved_reversed = row->reversed_p;
22245 row->reversed_p = false;
22246 row->enabled_p = true;
22247
22248 /* Arrange for the menu item glyphs to start at (X,Y) and have the
22249 desired face. */
22250 eassert (x < f->desired_matrix->matrix_w);
22251 it.current_x = it.hpos = x;
22252 it.current_y = it.vpos = y;
22253 int saved_used = row->used[TEXT_AREA];
22254 bool saved_truncated = row->truncated_on_right_p;
22255 row->used[TEXT_AREA] = x;
22256 it.face_id = face_id;
22257 it.line_wrap = TRUNCATE;
22258
22259 /* FIXME: This should be controlled by a user option. See the
22260 comments in redisplay_tool_bar and display_mode_line about this.
22261 Also, if paragraph_embedding could ever be R2L, changes will be
22262 needed to avoid shifting to the right the row characters in
22263 term.c:append_glyph. */
22264 it.paragraph_embedding = L2R;
22265
22266 /* Pad with a space on the left. */
22267 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22268 width--;
22269 /* Display the menu item, pad with spaces to WIDTH. */
22270 if (submenu)
22271 {
22272 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22273 item_len, 0, FRAME_COLS (f) - 1, -1);
22274 width -= item_len;
22275 /* Indicate with " >" that there's a submenu. */
22276 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22277 FRAME_COLS (f) - 1, -1);
22278 }
22279 else
22280 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22281 width, 0, FRAME_COLS (f) - 1, -1);
22282
22283 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22284 row->truncated_on_right_p = saved_truncated;
22285 row->hash = row_hash (row);
22286 row->full_width_p = saved_width;
22287 row->reversed_p = saved_reversed;
22288 }
22289 \f
22290 /***********************************************************************
22291 Mode Line
22292 ***********************************************************************/
22293
22294 /* Redisplay mode lines in the window tree whose root is WINDOW.
22295 If FORCE, redisplay mode lines unconditionally.
22296 Otherwise, redisplay only mode lines that are garbaged. Value is
22297 the number of windows whose mode lines were redisplayed. */
22298
22299 static int
22300 redisplay_mode_lines (Lisp_Object window, bool force)
22301 {
22302 int nwindows = 0;
22303
22304 while (!NILP (window))
22305 {
22306 struct window *w = XWINDOW (window);
22307
22308 if (WINDOWP (w->contents))
22309 nwindows += redisplay_mode_lines (w->contents, force);
22310 else if (force
22311 || FRAME_GARBAGED_P (XFRAME (w->frame))
22312 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22313 {
22314 struct text_pos lpoint;
22315 struct buffer *old = current_buffer;
22316
22317 /* Set the window's buffer for the mode line display. */
22318 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22319 set_buffer_internal_1 (XBUFFER (w->contents));
22320
22321 /* Point refers normally to the selected window. For any
22322 other window, set up appropriate value. */
22323 if (!EQ (window, selected_window))
22324 {
22325 struct text_pos pt;
22326
22327 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22328 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22329 }
22330
22331 /* Display mode lines. */
22332 clear_glyph_matrix (w->desired_matrix);
22333 if (display_mode_lines (w))
22334 ++nwindows;
22335
22336 /* Restore old settings. */
22337 set_buffer_internal_1 (old);
22338 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22339 }
22340
22341 window = w->next;
22342 }
22343
22344 return nwindows;
22345 }
22346
22347
22348 /* Display the mode and/or header line of window W. Value is the
22349 sum number of mode lines and header lines displayed. */
22350
22351 static int
22352 display_mode_lines (struct window *w)
22353 {
22354 Lisp_Object old_selected_window = selected_window;
22355 Lisp_Object old_selected_frame = selected_frame;
22356 Lisp_Object new_frame = w->frame;
22357 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22358 int n = 0;
22359
22360 selected_frame = new_frame;
22361 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22362 or window's point, then we'd need select_window_1 here as well. */
22363 XSETWINDOW (selected_window, w);
22364 XFRAME (new_frame)->selected_window = selected_window;
22365
22366 /* These will be set while the mode line specs are processed. */
22367 line_number_displayed = false;
22368 w->column_number_displayed = -1;
22369
22370 if (WINDOW_WANTS_MODELINE_P (w))
22371 {
22372 struct window *sel_w = XWINDOW (old_selected_window);
22373
22374 /* Select mode line face based on the real selected window. */
22375 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22376 BVAR (current_buffer, mode_line_format));
22377 ++n;
22378 }
22379
22380 if (WINDOW_WANTS_HEADER_LINE_P (w))
22381 {
22382 display_mode_line (w, HEADER_LINE_FACE_ID,
22383 BVAR (current_buffer, header_line_format));
22384 ++n;
22385 }
22386
22387 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22388 selected_frame = old_selected_frame;
22389 selected_window = old_selected_window;
22390 if (n > 0)
22391 w->must_be_updated_p = true;
22392 return n;
22393 }
22394
22395
22396 /* Display mode or header line of window W. FACE_ID specifies which
22397 line to display; it is either MODE_LINE_FACE_ID or
22398 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22399 display. Value is the pixel height of the mode/header line
22400 displayed. */
22401
22402 static int
22403 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22404 {
22405 struct it it;
22406 struct face *face;
22407 ptrdiff_t count = SPECPDL_INDEX ();
22408
22409 init_iterator (&it, w, -1, -1, NULL, face_id);
22410 /* Don't extend on a previously drawn mode-line.
22411 This may happen if called from pos_visible_p. */
22412 it.glyph_row->enabled_p = false;
22413 prepare_desired_row (w, it.glyph_row, true);
22414
22415 it.glyph_row->mode_line_p = true;
22416
22417 /* FIXME: This should be controlled by a user option. But
22418 supporting such an option is not trivial, since the mode line is
22419 made up of many separate strings. */
22420 it.paragraph_embedding = L2R;
22421
22422 record_unwind_protect (unwind_format_mode_line,
22423 format_mode_line_unwind_data (NULL, NULL,
22424 Qnil, false));
22425
22426 mode_line_target = MODE_LINE_DISPLAY;
22427
22428 /* Temporarily make frame's keyboard the current kboard so that
22429 kboard-local variables in the mode_line_format will get the right
22430 values. */
22431 push_kboard (FRAME_KBOARD (it.f));
22432 record_unwind_save_match_data ();
22433 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22434 pop_kboard ();
22435
22436 unbind_to (count, Qnil);
22437
22438 /* Fill up with spaces. */
22439 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22440
22441 compute_line_metrics (&it);
22442 it.glyph_row->full_width_p = true;
22443 it.glyph_row->continued_p = false;
22444 it.glyph_row->truncated_on_left_p = false;
22445 it.glyph_row->truncated_on_right_p = false;
22446
22447 /* Make a 3D mode-line have a shadow at its right end. */
22448 face = FACE_FROM_ID (it.f, face_id);
22449 extend_face_to_end_of_line (&it);
22450 if (face->box != FACE_NO_BOX)
22451 {
22452 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22453 + it.glyph_row->used[TEXT_AREA] - 1);
22454 last->right_box_line_p = true;
22455 }
22456
22457 return it.glyph_row->height;
22458 }
22459
22460 /* Move element ELT in LIST to the front of LIST.
22461 Return the updated list. */
22462
22463 static Lisp_Object
22464 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22465 {
22466 register Lisp_Object tail, prev;
22467 register Lisp_Object tem;
22468
22469 tail = list;
22470 prev = Qnil;
22471 while (CONSP (tail))
22472 {
22473 tem = XCAR (tail);
22474
22475 if (EQ (elt, tem))
22476 {
22477 /* Splice out the link TAIL. */
22478 if (NILP (prev))
22479 list = XCDR (tail);
22480 else
22481 Fsetcdr (prev, XCDR (tail));
22482
22483 /* Now make it the first. */
22484 Fsetcdr (tail, list);
22485 return tail;
22486 }
22487 else
22488 prev = tail;
22489 tail = XCDR (tail);
22490 QUIT;
22491 }
22492
22493 /* Not found--return unchanged LIST. */
22494 return list;
22495 }
22496
22497 /* Contribute ELT to the mode line for window IT->w. How it
22498 translates into text depends on its data type.
22499
22500 IT describes the display environment in which we display, as usual.
22501
22502 DEPTH is the depth in recursion. It is used to prevent
22503 infinite recursion here.
22504
22505 FIELD_WIDTH is the number of characters the display of ELT should
22506 occupy in the mode line, and PRECISION is the maximum number of
22507 characters to display from ELT's representation. See
22508 display_string for details.
22509
22510 Returns the hpos of the end of the text generated by ELT.
22511
22512 PROPS is a property list to add to any string we encounter.
22513
22514 If RISKY, remove (disregard) any properties in any string
22515 we encounter, and ignore :eval and :propertize.
22516
22517 The global variable `mode_line_target' determines whether the
22518 output is passed to `store_mode_line_noprop',
22519 `store_mode_line_string', or `display_string'. */
22520
22521 static int
22522 display_mode_element (struct it *it, int depth, int field_width, int precision,
22523 Lisp_Object elt, Lisp_Object props, bool risky)
22524 {
22525 int n = 0, field, prec;
22526 bool literal = false;
22527
22528 tail_recurse:
22529 if (depth > 100)
22530 elt = build_string ("*too-deep*");
22531
22532 depth++;
22533
22534 switch (XTYPE (elt))
22535 {
22536 case Lisp_String:
22537 {
22538 /* A string: output it and check for %-constructs within it. */
22539 unsigned char c;
22540 ptrdiff_t offset = 0;
22541
22542 if (SCHARS (elt) > 0
22543 && (!NILP (props) || risky))
22544 {
22545 Lisp_Object oprops, aelt;
22546 oprops = Ftext_properties_at (make_number (0), elt);
22547
22548 /* If the starting string's properties are not what
22549 we want, translate the string. Also, if the string
22550 is risky, do that anyway. */
22551
22552 if (NILP (Fequal (props, oprops)) || risky)
22553 {
22554 /* If the starting string has properties,
22555 merge the specified ones onto the existing ones. */
22556 if (! NILP (oprops) && !risky)
22557 {
22558 Lisp_Object tem;
22559
22560 oprops = Fcopy_sequence (oprops);
22561 tem = props;
22562 while (CONSP (tem))
22563 {
22564 oprops = Fplist_put (oprops, XCAR (tem),
22565 XCAR (XCDR (tem)));
22566 tem = XCDR (XCDR (tem));
22567 }
22568 props = oprops;
22569 }
22570
22571 aelt = Fassoc (elt, mode_line_proptrans_alist);
22572 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22573 {
22574 /* AELT is what we want. Move it to the front
22575 without consing. */
22576 elt = XCAR (aelt);
22577 mode_line_proptrans_alist
22578 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22579 }
22580 else
22581 {
22582 Lisp_Object tem;
22583
22584 /* If AELT has the wrong props, it is useless.
22585 so get rid of it. */
22586 if (! NILP (aelt))
22587 mode_line_proptrans_alist
22588 = Fdelq (aelt, mode_line_proptrans_alist);
22589
22590 elt = Fcopy_sequence (elt);
22591 Fset_text_properties (make_number (0), Flength (elt),
22592 props, elt);
22593 /* Add this item to mode_line_proptrans_alist. */
22594 mode_line_proptrans_alist
22595 = Fcons (Fcons (elt, props),
22596 mode_line_proptrans_alist);
22597 /* Truncate mode_line_proptrans_alist
22598 to at most 50 elements. */
22599 tem = Fnthcdr (make_number (50),
22600 mode_line_proptrans_alist);
22601 if (! NILP (tem))
22602 XSETCDR (tem, Qnil);
22603 }
22604 }
22605 }
22606
22607 offset = 0;
22608
22609 if (literal)
22610 {
22611 prec = precision - n;
22612 switch (mode_line_target)
22613 {
22614 case MODE_LINE_NOPROP:
22615 case MODE_LINE_TITLE:
22616 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22617 break;
22618 case MODE_LINE_STRING:
22619 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22620 break;
22621 case MODE_LINE_DISPLAY:
22622 n += display_string (NULL, elt, Qnil, 0, 0, it,
22623 0, prec, 0, STRING_MULTIBYTE (elt));
22624 break;
22625 }
22626
22627 break;
22628 }
22629
22630 /* Handle the non-literal case. */
22631
22632 while ((precision <= 0 || n < precision)
22633 && SREF (elt, offset) != 0
22634 && (mode_line_target != MODE_LINE_DISPLAY
22635 || it->current_x < it->last_visible_x))
22636 {
22637 ptrdiff_t last_offset = offset;
22638
22639 /* Advance to end of string or next format specifier. */
22640 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22641 ;
22642
22643 if (offset - 1 != last_offset)
22644 {
22645 ptrdiff_t nchars, nbytes;
22646
22647 /* Output to end of string or up to '%'. Field width
22648 is length of string. Don't output more than
22649 PRECISION allows us. */
22650 offset--;
22651
22652 prec = c_string_width (SDATA (elt) + last_offset,
22653 offset - last_offset, precision - n,
22654 &nchars, &nbytes);
22655
22656 switch (mode_line_target)
22657 {
22658 case MODE_LINE_NOPROP:
22659 case MODE_LINE_TITLE:
22660 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22661 break;
22662 case MODE_LINE_STRING:
22663 {
22664 ptrdiff_t bytepos = last_offset;
22665 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22666 ptrdiff_t endpos = (precision <= 0
22667 ? string_byte_to_char (elt, offset)
22668 : charpos + nchars);
22669 Lisp_Object mode_string
22670 = Fsubstring (elt, make_number (charpos),
22671 make_number (endpos));
22672 n += store_mode_line_string (NULL, mode_string, false,
22673 0, 0, Qnil);
22674 }
22675 break;
22676 case MODE_LINE_DISPLAY:
22677 {
22678 ptrdiff_t bytepos = last_offset;
22679 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22680
22681 if (precision <= 0)
22682 nchars = string_byte_to_char (elt, offset) - charpos;
22683 n += display_string (NULL, elt, Qnil, 0, charpos,
22684 it, 0, nchars, 0,
22685 STRING_MULTIBYTE (elt));
22686 }
22687 break;
22688 }
22689 }
22690 else /* c == '%' */
22691 {
22692 ptrdiff_t percent_position = offset;
22693
22694 /* Get the specified minimum width. Zero means
22695 don't pad. */
22696 field = 0;
22697 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22698 field = field * 10 + c - '0';
22699
22700 /* Don't pad beyond the total padding allowed. */
22701 if (field_width - n > 0 && field > field_width - n)
22702 field = field_width - n;
22703
22704 /* Note that either PRECISION <= 0 or N < PRECISION. */
22705 prec = precision - n;
22706
22707 if (c == 'M')
22708 n += display_mode_element (it, depth, field, prec,
22709 Vglobal_mode_string, props,
22710 risky);
22711 else if (c != 0)
22712 {
22713 bool multibyte;
22714 ptrdiff_t bytepos, charpos;
22715 const char *spec;
22716 Lisp_Object string;
22717
22718 bytepos = percent_position;
22719 charpos = (STRING_MULTIBYTE (elt)
22720 ? string_byte_to_char (elt, bytepos)
22721 : bytepos);
22722 spec = decode_mode_spec (it->w, c, field, &string);
22723 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22724
22725 switch (mode_line_target)
22726 {
22727 case MODE_LINE_NOPROP:
22728 case MODE_LINE_TITLE:
22729 n += store_mode_line_noprop (spec, field, prec);
22730 break;
22731 case MODE_LINE_STRING:
22732 {
22733 Lisp_Object tem = build_string (spec);
22734 props = Ftext_properties_at (make_number (charpos), elt);
22735 /* Should only keep face property in props */
22736 n += store_mode_line_string (NULL, tem, false,
22737 field, prec, props);
22738 }
22739 break;
22740 case MODE_LINE_DISPLAY:
22741 {
22742 int nglyphs_before, nwritten;
22743
22744 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22745 nwritten = display_string (spec, string, elt,
22746 charpos, 0, it,
22747 field, prec, 0,
22748 multibyte);
22749
22750 /* Assign to the glyphs written above the
22751 string where the `%x' came from, position
22752 of the `%'. */
22753 if (nwritten > 0)
22754 {
22755 struct glyph *glyph
22756 = (it->glyph_row->glyphs[TEXT_AREA]
22757 + nglyphs_before);
22758 int i;
22759
22760 for (i = 0; i < nwritten; ++i)
22761 {
22762 glyph[i].object = elt;
22763 glyph[i].charpos = charpos;
22764 }
22765
22766 n += nwritten;
22767 }
22768 }
22769 break;
22770 }
22771 }
22772 else /* c == 0 */
22773 break;
22774 }
22775 }
22776 }
22777 break;
22778
22779 case Lisp_Symbol:
22780 /* A symbol: process the value of the symbol recursively
22781 as if it appeared here directly. Avoid error if symbol void.
22782 Special case: if value of symbol is a string, output the string
22783 literally. */
22784 {
22785 register Lisp_Object tem;
22786
22787 /* If the variable is not marked as risky to set
22788 then its contents are risky to use. */
22789 if (NILP (Fget (elt, Qrisky_local_variable)))
22790 risky = true;
22791
22792 tem = Fboundp (elt);
22793 if (!NILP (tem))
22794 {
22795 tem = Fsymbol_value (elt);
22796 /* If value is a string, output that string literally:
22797 don't check for % within it. */
22798 if (STRINGP (tem))
22799 literal = true;
22800
22801 if (!EQ (tem, elt))
22802 {
22803 /* Give up right away for nil or t. */
22804 elt = tem;
22805 goto tail_recurse;
22806 }
22807 }
22808 }
22809 break;
22810
22811 case Lisp_Cons:
22812 {
22813 register Lisp_Object car, tem;
22814
22815 /* A cons cell: five distinct cases.
22816 If first element is :eval or :propertize, do something special.
22817 If first element is a string or a cons, process all the elements
22818 and effectively concatenate them.
22819 If first element is a negative number, truncate displaying cdr to
22820 at most that many characters. If positive, pad (with spaces)
22821 to at least that many characters.
22822 If first element is a symbol, process the cadr or caddr recursively
22823 according to whether the symbol's value is non-nil or nil. */
22824 car = XCAR (elt);
22825 if (EQ (car, QCeval))
22826 {
22827 /* An element of the form (:eval FORM) means evaluate FORM
22828 and use the result as mode line elements. */
22829
22830 if (risky)
22831 break;
22832
22833 if (CONSP (XCDR (elt)))
22834 {
22835 Lisp_Object spec;
22836 spec = safe__eval (true, XCAR (XCDR (elt)));
22837 n += display_mode_element (it, depth, field_width - n,
22838 precision - n, spec, props,
22839 risky);
22840 }
22841 }
22842 else if (EQ (car, QCpropertize))
22843 {
22844 /* An element of the form (:propertize ELT PROPS...)
22845 means display ELT but applying properties PROPS. */
22846
22847 if (risky)
22848 break;
22849
22850 if (CONSP (XCDR (elt)))
22851 n += display_mode_element (it, depth, field_width - n,
22852 precision - n, XCAR (XCDR (elt)),
22853 XCDR (XCDR (elt)), risky);
22854 }
22855 else if (SYMBOLP (car))
22856 {
22857 tem = Fboundp (car);
22858 elt = XCDR (elt);
22859 if (!CONSP (elt))
22860 goto invalid;
22861 /* elt is now the cdr, and we know it is a cons cell.
22862 Use its car if CAR has a non-nil value. */
22863 if (!NILP (tem))
22864 {
22865 tem = Fsymbol_value (car);
22866 if (!NILP (tem))
22867 {
22868 elt = XCAR (elt);
22869 goto tail_recurse;
22870 }
22871 }
22872 /* Symbol's value is nil (or symbol is unbound)
22873 Get the cddr of the original list
22874 and if possible find the caddr and use that. */
22875 elt = XCDR (elt);
22876 if (NILP (elt))
22877 break;
22878 else if (!CONSP (elt))
22879 goto invalid;
22880 elt = XCAR (elt);
22881 goto tail_recurse;
22882 }
22883 else if (INTEGERP (car))
22884 {
22885 register int lim = XINT (car);
22886 elt = XCDR (elt);
22887 if (lim < 0)
22888 {
22889 /* Negative int means reduce maximum width. */
22890 if (precision <= 0)
22891 precision = -lim;
22892 else
22893 precision = min (precision, -lim);
22894 }
22895 else if (lim > 0)
22896 {
22897 /* Padding specified. Don't let it be more than
22898 current maximum. */
22899 if (precision > 0)
22900 lim = min (precision, lim);
22901
22902 /* If that's more padding than already wanted, queue it.
22903 But don't reduce padding already specified even if
22904 that is beyond the current truncation point. */
22905 field_width = max (lim, field_width);
22906 }
22907 goto tail_recurse;
22908 }
22909 else if (STRINGP (car) || CONSP (car))
22910 {
22911 Lisp_Object halftail = elt;
22912 int len = 0;
22913
22914 while (CONSP (elt)
22915 && (precision <= 0 || n < precision))
22916 {
22917 n += display_mode_element (it, depth,
22918 /* Do padding only after the last
22919 element in the list. */
22920 (! CONSP (XCDR (elt))
22921 ? field_width - n
22922 : 0),
22923 precision - n, XCAR (elt),
22924 props, risky);
22925 elt = XCDR (elt);
22926 len++;
22927 if ((len & 1) == 0)
22928 halftail = XCDR (halftail);
22929 /* Check for cycle. */
22930 if (EQ (halftail, elt))
22931 break;
22932 }
22933 }
22934 }
22935 break;
22936
22937 default:
22938 invalid:
22939 elt = build_string ("*invalid*");
22940 goto tail_recurse;
22941 }
22942
22943 /* Pad to FIELD_WIDTH. */
22944 if (field_width > 0 && n < field_width)
22945 {
22946 switch (mode_line_target)
22947 {
22948 case MODE_LINE_NOPROP:
22949 case MODE_LINE_TITLE:
22950 n += store_mode_line_noprop ("", field_width - n, 0);
22951 break;
22952 case MODE_LINE_STRING:
22953 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
22954 Qnil);
22955 break;
22956 case MODE_LINE_DISPLAY:
22957 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22958 0, 0, 0);
22959 break;
22960 }
22961 }
22962
22963 return n;
22964 }
22965
22966 /* Store a mode-line string element in mode_line_string_list.
22967
22968 If STRING is non-null, display that C string. Otherwise, the Lisp
22969 string LISP_STRING is displayed.
22970
22971 FIELD_WIDTH is the minimum number of output glyphs to produce.
22972 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22973 with spaces. FIELD_WIDTH <= 0 means don't pad.
22974
22975 PRECISION is the maximum number of characters to output from
22976 STRING. PRECISION <= 0 means don't truncate the string.
22977
22978 If COPY_STRING, make a copy of LISP_STRING before adding
22979 properties to the string.
22980
22981 PROPS are the properties to add to the string.
22982 The mode_line_string_face face property is always added to the string.
22983 */
22984
22985 static int
22986 store_mode_line_string (const char *string, Lisp_Object lisp_string,
22987 bool copy_string,
22988 int field_width, int precision, Lisp_Object props)
22989 {
22990 ptrdiff_t len;
22991 int n = 0;
22992
22993 if (string != NULL)
22994 {
22995 len = strlen (string);
22996 if (precision > 0 && len > precision)
22997 len = precision;
22998 lisp_string = make_string (string, len);
22999 if (NILP (props))
23000 props = mode_line_string_face_prop;
23001 else if (!NILP (mode_line_string_face))
23002 {
23003 Lisp_Object face = Fplist_get (props, Qface);
23004 props = Fcopy_sequence (props);
23005 if (NILP (face))
23006 face = mode_line_string_face;
23007 else
23008 face = list2 (face, mode_line_string_face);
23009 props = Fplist_put (props, Qface, face);
23010 }
23011 Fadd_text_properties (make_number (0), make_number (len),
23012 props, lisp_string);
23013 }
23014 else
23015 {
23016 len = XFASTINT (Flength (lisp_string));
23017 if (precision > 0 && len > precision)
23018 {
23019 len = precision;
23020 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23021 precision = -1;
23022 }
23023 if (!NILP (mode_line_string_face))
23024 {
23025 Lisp_Object face;
23026 if (NILP (props))
23027 props = Ftext_properties_at (make_number (0), lisp_string);
23028 face = Fplist_get (props, Qface);
23029 if (NILP (face))
23030 face = mode_line_string_face;
23031 else
23032 face = list2 (face, mode_line_string_face);
23033 props = list2 (Qface, face);
23034 if (copy_string)
23035 lisp_string = Fcopy_sequence (lisp_string);
23036 }
23037 if (!NILP (props))
23038 Fadd_text_properties (make_number (0), make_number (len),
23039 props, lisp_string);
23040 }
23041
23042 if (len > 0)
23043 {
23044 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23045 n += len;
23046 }
23047
23048 if (field_width > len)
23049 {
23050 field_width -= len;
23051 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
23052 if (!NILP (props))
23053 Fadd_text_properties (make_number (0), make_number (field_width),
23054 props, lisp_string);
23055 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23056 n += field_width;
23057 }
23058
23059 return n;
23060 }
23061
23062
23063 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23064 1, 4, 0,
23065 doc: /* Format a string out of a mode line format specification.
23066 First arg FORMAT specifies the mode line format (see `mode-line-format'
23067 for details) to use.
23068
23069 By default, the format is evaluated for the currently selected window.
23070
23071 Optional second arg FACE specifies the face property to put on all
23072 characters for which no face is specified. The value nil means the
23073 default face. The value t means whatever face the window's mode line
23074 currently uses (either `mode-line' or `mode-line-inactive',
23075 depending on whether the window is the selected window or not).
23076 An integer value means the value string has no text
23077 properties.
23078
23079 Optional third and fourth args WINDOW and BUFFER specify the window
23080 and buffer to use as the context for the formatting (defaults
23081 are the selected window and the WINDOW's buffer). */)
23082 (Lisp_Object format, Lisp_Object face,
23083 Lisp_Object window, Lisp_Object buffer)
23084 {
23085 struct it it;
23086 int len;
23087 struct window *w;
23088 struct buffer *old_buffer = NULL;
23089 int face_id;
23090 bool no_props = INTEGERP (face);
23091 ptrdiff_t count = SPECPDL_INDEX ();
23092 Lisp_Object str;
23093 int string_start = 0;
23094
23095 w = decode_any_window (window);
23096 XSETWINDOW (window, w);
23097
23098 if (NILP (buffer))
23099 buffer = w->contents;
23100 CHECK_BUFFER (buffer);
23101
23102 /* Make formatting the modeline a non-op when noninteractive, otherwise
23103 there will be problems later caused by a partially initialized frame. */
23104 if (NILP (format) || noninteractive)
23105 return empty_unibyte_string;
23106
23107 if (no_props)
23108 face = Qnil;
23109
23110 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23111 : EQ (face, Qt) ? (EQ (window, selected_window)
23112 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23113 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23114 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23115 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23116 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23117 : DEFAULT_FACE_ID;
23118
23119 old_buffer = current_buffer;
23120
23121 /* Save things including mode_line_proptrans_alist,
23122 and set that to nil so that we don't alter the outer value. */
23123 record_unwind_protect (unwind_format_mode_line,
23124 format_mode_line_unwind_data
23125 (XFRAME (WINDOW_FRAME (w)),
23126 old_buffer, selected_window, true));
23127 mode_line_proptrans_alist = Qnil;
23128
23129 Fselect_window (window, Qt);
23130 set_buffer_internal_1 (XBUFFER (buffer));
23131
23132 init_iterator (&it, w, -1, -1, NULL, face_id);
23133
23134 if (no_props)
23135 {
23136 mode_line_target = MODE_LINE_NOPROP;
23137 mode_line_string_face_prop = Qnil;
23138 mode_line_string_list = Qnil;
23139 string_start = MODE_LINE_NOPROP_LEN (0);
23140 }
23141 else
23142 {
23143 mode_line_target = MODE_LINE_STRING;
23144 mode_line_string_list = Qnil;
23145 mode_line_string_face = face;
23146 mode_line_string_face_prop
23147 = NILP (face) ? Qnil : list2 (Qface, face);
23148 }
23149
23150 push_kboard (FRAME_KBOARD (it.f));
23151 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23152 pop_kboard ();
23153
23154 if (no_props)
23155 {
23156 len = MODE_LINE_NOPROP_LEN (string_start);
23157 str = make_string (mode_line_noprop_buf + string_start, len);
23158 }
23159 else
23160 {
23161 mode_line_string_list = Fnreverse (mode_line_string_list);
23162 str = Fmapconcat (Qidentity, mode_line_string_list,
23163 empty_unibyte_string);
23164 }
23165
23166 unbind_to (count, Qnil);
23167 return str;
23168 }
23169
23170 /* Write a null-terminated, right justified decimal representation of
23171 the positive integer D to BUF using a minimal field width WIDTH. */
23172
23173 static void
23174 pint2str (register char *buf, register int width, register ptrdiff_t d)
23175 {
23176 register char *p = buf;
23177
23178 if (d <= 0)
23179 *p++ = '0';
23180 else
23181 {
23182 while (d > 0)
23183 {
23184 *p++ = d % 10 + '0';
23185 d /= 10;
23186 }
23187 }
23188
23189 for (width -= (int) (p - buf); width > 0; --width)
23190 *p++ = ' ';
23191 *p-- = '\0';
23192 while (p > buf)
23193 {
23194 d = *buf;
23195 *buf++ = *p;
23196 *p-- = d;
23197 }
23198 }
23199
23200 /* Write a null-terminated, right justified decimal and "human
23201 readable" representation of the nonnegative integer D to BUF using
23202 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23203
23204 static const char power_letter[] =
23205 {
23206 0, /* no letter */
23207 'k', /* kilo */
23208 'M', /* mega */
23209 'G', /* giga */
23210 'T', /* tera */
23211 'P', /* peta */
23212 'E', /* exa */
23213 'Z', /* zetta */
23214 'Y' /* yotta */
23215 };
23216
23217 static void
23218 pint2hrstr (char *buf, int width, ptrdiff_t d)
23219 {
23220 /* We aim to represent the nonnegative integer D as
23221 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
23222 ptrdiff_t quotient = d;
23223 int remainder = 0;
23224 /* -1 means: do not use TENTHS. */
23225 int tenths = -1;
23226 int exponent = 0;
23227
23228 /* Length of QUOTIENT.TENTHS as a string. */
23229 int length;
23230
23231 char * psuffix;
23232 char * p;
23233
23234 if (quotient >= 1000)
23235 {
23236 /* Scale to the appropriate EXPONENT. */
23237 do
23238 {
23239 remainder = quotient % 1000;
23240 quotient /= 1000;
23241 exponent++;
23242 }
23243 while (quotient >= 1000);
23244
23245 /* Round to nearest and decide whether to use TENTHS or not. */
23246 if (quotient <= 9)
23247 {
23248 tenths = remainder / 100;
23249 if (remainder % 100 >= 50)
23250 {
23251 if (tenths < 9)
23252 tenths++;
23253 else
23254 {
23255 quotient++;
23256 if (quotient == 10)
23257 tenths = -1;
23258 else
23259 tenths = 0;
23260 }
23261 }
23262 }
23263 else
23264 if (remainder >= 500)
23265 {
23266 if (quotient < 999)
23267 quotient++;
23268 else
23269 {
23270 quotient = 1;
23271 exponent++;
23272 tenths = 0;
23273 }
23274 }
23275 }
23276
23277 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23278 if (tenths == -1 && quotient <= 99)
23279 if (quotient <= 9)
23280 length = 1;
23281 else
23282 length = 2;
23283 else
23284 length = 3;
23285 p = psuffix = buf + max (width, length);
23286
23287 /* Print EXPONENT. */
23288 *psuffix++ = power_letter[exponent];
23289 *psuffix = '\0';
23290
23291 /* Print TENTHS. */
23292 if (tenths >= 0)
23293 {
23294 *--p = '0' + tenths;
23295 *--p = '.';
23296 }
23297
23298 /* Print QUOTIENT. */
23299 do
23300 {
23301 int digit = quotient % 10;
23302 *--p = '0' + digit;
23303 }
23304 while ((quotient /= 10) != 0);
23305
23306 /* Print leading spaces. */
23307 while (buf < p)
23308 *--p = ' ';
23309 }
23310
23311 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23312 If EOL_FLAG, set also a mnemonic character for end-of-line
23313 type of CODING_SYSTEM. Return updated pointer into BUF. */
23314
23315 static unsigned char invalid_eol_type[] = "(*invalid*)";
23316
23317 static char *
23318 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23319 {
23320 Lisp_Object val;
23321 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23322 const unsigned char *eol_str;
23323 int eol_str_len;
23324 /* The EOL conversion we are using. */
23325 Lisp_Object eoltype;
23326
23327 val = CODING_SYSTEM_SPEC (coding_system);
23328 eoltype = Qnil;
23329
23330 if (!VECTORP (val)) /* Not yet decided. */
23331 {
23332 *buf++ = multibyte ? '-' : ' ';
23333 if (eol_flag)
23334 eoltype = eol_mnemonic_undecided;
23335 /* Don't mention EOL conversion if it isn't decided. */
23336 }
23337 else
23338 {
23339 Lisp_Object attrs;
23340 Lisp_Object eolvalue;
23341
23342 attrs = AREF (val, 0);
23343 eolvalue = AREF (val, 2);
23344
23345 *buf++ = multibyte
23346 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23347 : ' ';
23348
23349 if (eol_flag)
23350 {
23351 /* The EOL conversion that is normal on this system. */
23352
23353 if (NILP (eolvalue)) /* Not yet decided. */
23354 eoltype = eol_mnemonic_undecided;
23355 else if (VECTORP (eolvalue)) /* Not yet decided. */
23356 eoltype = eol_mnemonic_undecided;
23357 else /* eolvalue is Qunix, Qdos, or Qmac. */
23358 eoltype = (EQ (eolvalue, Qunix)
23359 ? eol_mnemonic_unix
23360 : EQ (eolvalue, Qdos)
23361 ? eol_mnemonic_dos : eol_mnemonic_mac);
23362 }
23363 }
23364
23365 if (eol_flag)
23366 {
23367 /* Mention the EOL conversion if it is not the usual one. */
23368 if (STRINGP (eoltype))
23369 {
23370 eol_str = SDATA (eoltype);
23371 eol_str_len = SBYTES (eoltype);
23372 }
23373 else if (CHARACTERP (eoltype))
23374 {
23375 int c = XFASTINT (eoltype);
23376 return buf + CHAR_STRING (c, (unsigned char *) buf);
23377 }
23378 else
23379 {
23380 eol_str = invalid_eol_type;
23381 eol_str_len = sizeof (invalid_eol_type) - 1;
23382 }
23383 memcpy (buf, eol_str, eol_str_len);
23384 buf += eol_str_len;
23385 }
23386
23387 return buf;
23388 }
23389
23390 /* Return a string for the output of a mode line %-spec for window W,
23391 generated by character C. FIELD_WIDTH > 0 means pad the string
23392 returned with spaces to that value. Return a Lisp string in
23393 *STRING if the resulting string is taken from that Lisp string.
23394
23395 Note we operate on the current buffer for most purposes. */
23396
23397 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23398
23399 static const char *
23400 decode_mode_spec (struct window *w, register int c, int field_width,
23401 Lisp_Object *string)
23402 {
23403 Lisp_Object obj;
23404 struct frame *f = XFRAME (WINDOW_FRAME (w));
23405 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23406 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23407 produce strings from numerical values, so limit preposterously
23408 large values of FIELD_WIDTH to avoid overrunning the buffer's
23409 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23410 bytes plus the terminating null. */
23411 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23412 struct buffer *b = current_buffer;
23413
23414 obj = Qnil;
23415 *string = Qnil;
23416
23417 switch (c)
23418 {
23419 case '*':
23420 if (!NILP (BVAR (b, read_only)))
23421 return "%";
23422 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23423 return "*";
23424 return "-";
23425
23426 case '+':
23427 /* This differs from %* only for a modified read-only buffer. */
23428 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23429 return "*";
23430 if (!NILP (BVAR (b, read_only)))
23431 return "%";
23432 return "-";
23433
23434 case '&':
23435 /* This differs from %* in ignoring read-only-ness. */
23436 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23437 return "*";
23438 return "-";
23439
23440 case '%':
23441 return "%";
23442
23443 case '[':
23444 {
23445 int i;
23446 char *p;
23447
23448 if (command_loop_level > 5)
23449 return "[[[... ";
23450 p = decode_mode_spec_buf;
23451 for (i = 0; i < command_loop_level; i++)
23452 *p++ = '[';
23453 *p = 0;
23454 return decode_mode_spec_buf;
23455 }
23456
23457 case ']':
23458 {
23459 int i;
23460 char *p;
23461
23462 if (command_loop_level > 5)
23463 return " ...]]]";
23464 p = decode_mode_spec_buf;
23465 for (i = 0; i < command_loop_level; i++)
23466 *p++ = ']';
23467 *p = 0;
23468 return decode_mode_spec_buf;
23469 }
23470
23471 case '-':
23472 {
23473 register int i;
23474
23475 /* Let lots_of_dashes be a string of infinite length. */
23476 if (mode_line_target == MODE_LINE_NOPROP
23477 || mode_line_target == MODE_LINE_STRING)
23478 return "--";
23479 if (field_width <= 0
23480 || field_width > sizeof (lots_of_dashes))
23481 {
23482 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23483 decode_mode_spec_buf[i] = '-';
23484 decode_mode_spec_buf[i] = '\0';
23485 return decode_mode_spec_buf;
23486 }
23487 else
23488 return lots_of_dashes;
23489 }
23490
23491 case 'b':
23492 obj = BVAR (b, name);
23493 break;
23494
23495 case 'c':
23496 /* %c and %l are ignored in `frame-title-format'.
23497 (In redisplay_internal, the frame title is drawn _before_ the
23498 windows are updated, so the stuff which depends on actual
23499 window contents (such as %l) may fail to render properly, or
23500 even crash emacs.) */
23501 if (mode_line_target == MODE_LINE_TITLE)
23502 return "";
23503 else
23504 {
23505 ptrdiff_t col = current_column ();
23506 w->column_number_displayed = col;
23507 pint2str (decode_mode_spec_buf, width, col);
23508 return decode_mode_spec_buf;
23509 }
23510
23511 case 'e':
23512 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23513 {
23514 if (NILP (Vmemory_full))
23515 return "";
23516 else
23517 return "!MEM FULL! ";
23518 }
23519 #else
23520 return "";
23521 #endif
23522
23523 case 'F':
23524 /* %F displays the frame name. */
23525 if (!NILP (f->title))
23526 return SSDATA (f->title);
23527 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23528 return SSDATA (f->name);
23529 return "Emacs";
23530
23531 case 'f':
23532 obj = BVAR (b, filename);
23533 break;
23534
23535 case 'i':
23536 {
23537 ptrdiff_t size = ZV - BEGV;
23538 pint2str (decode_mode_spec_buf, width, size);
23539 return decode_mode_spec_buf;
23540 }
23541
23542 case 'I':
23543 {
23544 ptrdiff_t size = ZV - BEGV;
23545 pint2hrstr (decode_mode_spec_buf, width, size);
23546 return decode_mode_spec_buf;
23547 }
23548
23549 case 'l':
23550 {
23551 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23552 ptrdiff_t topline, nlines, height;
23553 ptrdiff_t junk;
23554
23555 /* %c and %l are ignored in `frame-title-format'. */
23556 if (mode_line_target == MODE_LINE_TITLE)
23557 return "";
23558
23559 startpos = marker_position (w->start);
23560 startpos_byte = marker_byte_position (w->start);
23561 height = WINDOW_TOTAL_LINES (w);
23562
23563 /* If we decided that this buffer isn't suitable for line numbers,
23564 don't forget that too fast. */
23565 if (w->base_line_pos == -1)
23566 goto no_value;
23567
23568 /* If the buffer is very big, don't waste time. */
23569 if (INTEGERP (Vline_number_display_limit)
23570 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23571 {
23572 w->base_line_pos = 0;
23573 w->base_line_number = 0;
23574 goto no_value;
23575 }
23576
23577 if (w->base_line_number > 0
23578 && w->base_line_pos > 0
23579 && w->base_line_pos <= startpos)
23580 {
23581 line = w->base_line_number;
23582 linepos = w->base_line_pos;
23583 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23584 }
23585 else
23586 {
23587 line = 1;
23588 linepos = BUF_BEGV (b);
23589 linepos_byte = BUF_BEGV_BYTE (b);
23590 }
23591
23592 /* Count lines from base line to window start position. */
23593 nlines = display_count_lines (linepos_byte,
23594 startpos_byte,
23595 startpos, &junk);
23596
23597 topline = nlines + line;
23598
23599 /* Determine a new base line, if the old one is too close
23600 or too far away, or if we did not have one.
23601 "Too close" means it's plausible a scroll-down would
23602 go back past it. */
23603 if (startpos == BUF_BEGV (b))
23604 {
23605 w->base_line_number = topline;
23606 w->base_line_pos = BUF_BEGV (b);
23607 }
23608 else if (nlines < height + 25 || nlines > height * 3 + 50
23609 || linepos == BUF_BEGV (b))
23610 {
23611 ptrdiff_t limit = BUF_BEGV (b);
23612 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23613 ptrdiff_t position;
23614 ptrdiff_t distance =
23615 (height * 2 + 30) * line_number_display_limit_width;
23616
23617 if (startpos - distance > limit)
23618 {
23619 limit = startpos - distance;
23620 limit_byte = CHAR_TO_BYTE (limit);
23621 }
23622
23623 nlines = display_count_lines (startpos_byte,
23624 limit_byte,
23625 - (height * 2 + 30),
23626 &position);
23627 /* If we couldn't find the lines we wanted within
23628 line_number_display_limit_width chars per line,
23629 give up on line numbers for this window. */
23630 if (position == limit_byte && limit == startpos - distance)
23631 {
23632 w->base_line_pos = -1;
23633 w->base_line_number = 0;
23634 goto no_value;
23635 }
23636
23637 w->base_line_number = topline - nlines;
23638 w->base_line_pos = BYTE_TO_CHAR (position);
23639 }
23640
23641 /* Now count lines from the start pos to point. */
23642 nlines = display_count_lines (startpos_byte,
23643 PT_BYTE, PT, &junk);
23644
23645 /* Record that we did display the line number. */
23646 line_number_displayed = true;
23647
23648 /* Make the string to show. */
23649 pint2str (decode_mode_spec_buf, width, topline + nlines);
23650 return decode_mode_spec_buf;
23651 no_value:
23652 {
23653 char *p = decode_mode_spec_buf;
23654 int pad = width - 2;
23655 while (pad-- > 0)
23656 *p++ = ' ';
23657 *p++ = '?';
23658 *p++ = '?';
23659 *p = '\0';
23660 return decode_mode_spec_buf;
23661 }
23662 }
23663 break;
23664
23665 case 'm':
23666 obj = BVAR (b, mode_name);
23667 break;
23668
23669 case 'n':
23670 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23671 return " Narrow";
23672 break;
23673
23674 case 'p':
23675 {
23676 ptrdiff_t pos = marker_position (w->start);
23677 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23678
23679 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23680 {
23681 if (pos <= BUF_BEGV (b))
23682 return "All";
23683 else
23684 return "Bottom";
23685 }
23686 else if (pos <= BUF_BEGV (b))
23687 return "Top";
23688 else
23689 {
23690 if (total > 1000000)
23691 /* Do it differently for a large value, to avoid overflow. */
23692 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23693 else
23694 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23695 /* We can't normally display a 3-digit number,
23696 so get us a 2-digit number that is close. */
23697 if (total == 100)
23698 total = 99;
23699 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23700 return decode_mode_spec_buf;
23701 }
23702 }
23703
23704 /* Display percentage of size above the bottom of the screen. */
23705 case 'P':
23706 {
23707 ptrdiff_t toppos = marker_position (w->start);
23708 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23709 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23710
23711 if (botpos >= BUF_ZV (b))
23712 {
23713 if (toppos <= BUF_BEGV (b))
23714 return "All";
23715 else
23716 return "Bottom";
23717 }
23718 else
23719 {
23720 if (total > 1000000)
23721 /* Do it differently for a large value, to avoid overflow. */
23722 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23723 else
23724 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23725 /* We can't normally display a 3-digit number,
23726 so get us a 2-digit number that is close. */
23727 if (total == 100)
23728 total = 99;
23729 if (toppos <= BUF_BEGV (b))
23730 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23731 else
23732 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23733 return decode_mode_spec_buf;
23734 }
23735 }
23736
23737 case 's':
23738 /* status of process */
23739 obj = Fget_buffer_process (Fcurrent_buffer ());
23740 if (NILP (obj))
23741 return "no process";
23742 #ifndef MSDOS
23743 obj = Fsymbol_name (Fprocess_status (obj));
23744 #endif
23745 break;
23746
23747 case '@':
23748 {
23749 ptrdiff_t count = inhibit_garbage_collection ();
23750 Lisp_Object curdir = BVAR (current_buffer, directory);
23751 Lisp_Object val = Qnil;
23752
23753 if (STRINGP (curdir))
23754 val = call1 (intern ("file-remote-p"), curdir);
23755
23756 unbind_to (count, Qnil);
23757
23758 if (NILP (val))
23759 return "-";
23760 else
23761 return "@";
23762 }
23763
23764 case 'z':
23765 /* coding-system (not including end-of-line format) */
23766 case 'Z':
23767 /* coding-system (including end-of-line type) */
23768 {
23769 bool eol_flag = (c == 'Z');
23770 char *p = decode_mode_spec_buf;
23771
23772 if (! FRAME_WINDOW_P (f))
23773 {
23774 /* No need to mention EOL here--the terminal never needs
23775 to do EOL conversion. */
23776 p = decode_mode_spec_coding (CODING_ID_NAME
23777 (FRAME_KEYBOARD_CODING (f)->id),
23778 p, false);
23779 p = decode_mode_spec_coding (CODING_ID_NAME
23780 (FRAME_TERMINAL_CODING (f)->id),
23781 p, false);
23782 }
23783 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23784 p, eol_flag);
23785
23786 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23787 #ifdef subprocesses
23788 obj = Fget_buffer_process (Fcurrent_buffer ());
23789 if (PROCESSP (obj))
23790 {
23791 p = decode_mode_spec_coding
23792 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23793 p = decode_mode_spec_coding
23794 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23795 }
23796 #endif /* subprocesses */
23797 #endif /* false */
23798 *p = 0;
23799 return decode_mode_spec_buf;
23800 }
23801 }
23802
23803 if (STRINGP (obj))
23804 {
23805 *string = obj;
23806 return SSDATA (obj);
23807 }
23808 else
23809 return "";
23810 }
23811
23812
23813 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23814 means count lines back from START_BYTE. But don't go beyond
23815 LIMIT_BYTE. Return the number of lines thus found (always
23816 nonnegative).
23817
23818 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23819 either the position COUNT lines after/before START_BYTE, if we
23820 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23821 COUNT lines. */
23822
23823 static ptrdiff_t
23824 display_count_lines (ptrdiff_t start_byte,
23825 ptrdiff_t limit_byte, ptrdiff_t count,
23826 ptrdiff_t *byte_pos_ptr)
23827 {
23828 register unsigned char *cursor;
23829 unsigned char *base;
23830
23831 register ptrdiff_t ceiling;
23832 register unsigned char *ceiling_addr;
23833 ptrdiff_t orig_count = count;
23834
23835 /* If we are not in selective display mode,
23836 check only for newlines. */
23837 bool selective_display
23838 = (!NILP (BVAR (current_buffer, selective_display))
23839 && !INTEGERP (BVAR (current_buffer, selective_display)));
23840
23841 if (count > 0)
23842 {
23843 while (start_byte < limit_byte)
23844 {
23845 ceiling = BUFFER_CEILING_OF (start_byte);
23846 ceiling = min (limit_byte - 1, ceiling);
23847 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23848 base = (cursor = BYTE_POS_ADDR (start_byte));
23849
23850 do
23851 {
23852 if (selective_display)
23853 {
23854 while (*cursor != '\n' && *cursor != 015
23855 && ++cursor != ceiling_addr)
23856 continue;
23857 if (cursor == ceiling_addr)
23858 break;
23859 }
23860 else
23861 {
23862 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23863 if (! cursor)
23864 break;
23865 }
23866
23867 cursor++;
23868
23869 if (--count == 0)
23870 {
23871 start_byte += cursor - base;
23872 *byte_pos_ptr = start_byte;
23873 return orig_count;
23874 }
23875 }
23876 while (cursor < ceiling_addr);
23877
23878 start_byte += ceiling_addr - base;
23879 }
23880 }
23881 else
23882 {
23883 while (start_byte > limit_byte)
23884 {
23885 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23886 ceiling = max (limit_byte, ceiling);
23887 ceiling_addr = BYTE_POS_ADDR (ceiling);
23888 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23889 while (true)
23890 {
23891 if (selective_display)
23892 {
23893 while (--cursor >= ceiling_addr
23894 && *cursor != '\n' && *cursor != 015)
23895 continue;
23896 if (cursor < ceiling_addr)
23897 break;
23898 }
23899 else
23900 {
23901 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23902 if (! cursor)
23903 break;
23904 }
23905
23906 if (++count == 0)
23907 {
23908 start_byte += cursor - base + 1;
23909 *byte_pos_ptr = start_byte;
23910 /* When scanning backwards, we should
23911 not count the newline posterior to which we stop. */
23912 return - orig_count - 1;
23913 }
23914 }
23915 start_byte += ceiling_addr - base;
23916 }
23917 }
23918
23919 *byte_pos_ptr = limit_byte;
23920
23921 if (count < 0)
23922 return - orig_count + count;
23923 return orig_count - count;
23924
23925 }
23926
23927
23928 \f
23929 /***********************************************************************
23930 Displaying strings
23931 ***********************************************************************/
23932
23933 /* Display a NUL-terminated string, starting with index START.
23934
23935 If STRING is non-null, display that C string. Otherwise, the Lisp
23936 string LISP_STRING is displayed. There's a case that STRING is
23937 non-null and LISP_STRING is not nil. It means STRING is a string
23938 data of LISP_STRING. In that case, we display LISP_STRING while
23939 ignoring its text properties.
23940
23941 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23942 FACE_STRING. Display STRING or LISP_STRING with the face at
23943 FACE_STRING_POS in FACE_STRING:
23944
23945 Display the string in the environment given by IT, but use the
23946 standard display table, temporarily.
23947
23948 FIELD_WIDTH is the minimum number of output glyphs to produce.
23949 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23950 with spaces. If STRING has more characters, more than FIELD_WIDTH
23951 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23952
23953 PRECISION is the maximum number of characters to output from
23954 STRING. PRECISION < 0 means don't truncate the string.
23955
23956 This is roughly equivalent to printf format specifiers:
23957
23958 FIELD_WIDTH PRECISION PRINTF
23959 ----------------------------------------
23960 -1 -1 %s
23961 -1 10 %.10s
23962 10 -1 %10s
23963 20 10 %20.10s
23964
23965 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23966 display them, and < 0 means obey the current buffer's value of
23967 enable_multibyte_characters.
23968
23969 Value is the number of columns displayed. */
23970
23971 static int
23972 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23973 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23974 int field_width, int precision, int max_x, int multibyte)
23975 {
23976 int hpos_at_start = it->hpos;
23977 int saved_face_id = it->face_id;
23978 struct glyph_row *row = it->glyph_row;
23979 ptrdiff_t it_charpos;
23980
23981 /* Initialize the iterator IT for iteration over STRING beginning
23982 with index START. */
23983 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23984 precision, field_width, multibyte);
23985 if (string && STRINGP (lisp_string))
23986 /* LISP_STRING is the one returned by decode_mode_spec. We should
23987 ignore its text properties. */
23988 it->stop_charpos = it->end_charpos;
23989
23990 /* If displaying STRING, set up the face of the iterator from
23991 FACE_STRING, if that's given. */
23992 if (STRINGP (face_string))
23993 {
23994 ptrdiff_t endptr;
23995 struct face *face;
23996
23997 it->face_id
23998 = face_at_string_position (it->w, face_string, face_string_pos,
23999 0, &endptr, it->base_face_id, false);
24000 face = FACE_FROM_ID (it->f, it->face_id);
24001 it->face_box_p = face->box != FACE_NO_BOX;
24002 }
24003
24004 /* Set max_x to the maximum allowed X position. Don't let it go
24005 beyond the right edge of the window. */
24006 if (max_x <= 0)
24007 max_x = it->last_visible_x;
24008 else
24009 max_x = min (max_x, it->last_visible_x);
24010
24011 /* Skip over display elements that are not visible. because IT->w is
24012 hscrolled. */
24013 if (it->current_x < it->first_visible_x)
24014 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24015 MOVE_TO_POS | MOVE_TO_X);
24016
24017 row->ascent = it->max_ascent;
24018 row->height = it->max_ascent + it->max_descent;
24019 row->phys_ascent = it->max_phys_ascent;
24020 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24021 row->extra_line_spacing = it->max_extra_line_spacing;
24022
24023 if (STRINGP (it->string))
24024 it_charpos = IT_STRING_CHARPOS (*it);
24025 else
24026 it_charpos = IT_CHARPOS (*it);
24027
24028 /* This condition is for the case that we are called with current_x
24029 past last_visible_x. */
24030 while (it->current_x < max_x)
24031 {
24032 int x_before, x, n_glyphs_before, i, nglyphs;
24033
24034 /* Get the next display element. */
24035 if (!get_next_display_element (it))
24036 break;
24037
24038 /* Produce glyphs. */
24039 x_before = it->current_x;
24040 n_glyphs_before = row->used[TEXT_AREA];
24041 PRODUCE_GLYPHS (it);
24042
24043 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24044 i = 0;
24045 x = x_before;
24046 while (i < nglyphs)
24047 {
24048 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24049
24050 if (it->line_wrap != TRUNCATE
24051 && x + glyph->pixel_width > max_x)
24052 {
24053 /* End of continued line or max_x reached. */
24054 if (CHAR_GLYPH_PADDING_P (*glyph))
24055 {
24056 /* A wide character is unbreakable. */
24057 if (row->reversed_p)
24058 unproduce_glyphs (it, row->used[TEXT_AREA]
24059 - n_glyphs_before);
24060 row->used[TEXT_AREA] = n_glyphs_before;
24061 it->current_x = x_before;
24062 }
24063 else
24064 {
24065 if (row->reversed_p)
24066 unproduce_glyphs (it, row->used[TEXT_AREA]
24067 - (n_glyphs_before + i));
24068 row->used[TEXT_AREA] = n_glyphs_before + i;
24069 it->current_x = x;
24070 }
24071 break;
24072 }
24073 else if (x + glyph->pixel_width >= it->first_visible_x)
24074 {
24075 /* Glyph is at least partially visible. */
24076 ++it->hpos;
24077 if (x < it->first_visible_x)
24078 row->x = x - it->first_visible_x;
24079 }
24080 else
24081 {
24082 /* Glyph is off the left margin of the display area.
24083 Should not happen. */
24084 emacs_abort ();
24085 }
24086
24087 row->ascent = max (row->ascent, it->max_ascent);
24088 row->height = max (row->height, it->max_ascent + it->max_descent);
24089 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24090 row->phys_height = max (row->phys_height,
24091 it->max_phys_ascent + it->max_phys_descent);
24092 row->extra_line_spacing = max (row->extra_line_spacing,
24093 it->max_extra_line_spacing);
24094 x += glyph->pixel_width;
24095 ++i;
24096 }
24097
24098 /* Stop if max_x reached. */
24099 if (i < nglyphs)
24100 break;
24101
24102 /* Stop at line ends. */
24103 if (ITERATOR_AT_END_OF_LINE_P (it))
24104 {
24105 it->continuation_lines_width = 0;
24106 break;
24107 }
24108
24109 set_iterator_to_next (it, true);
24110 if (STRINGP (it->string))
24111 it_charpos = IT_STRING_CHARPOS (*it);
24112 else
24113 it_charpos = IT_CHARPOS (*it);
24114
24115 /* Stop if truncating at the right edge. */
24116 if (it->line_wrap == TRUNCATE
24117 && it->current_x >= it->last_visible_x)
24118 {
24119 /* Add truncation mark, but don't do it if the line is
24120 truncated at a padding space. */
24121 if (it_charpos < it->string_nchars)
24122 {
24123 if (!FRAME_WINDOW_P (it->f))
24124 {
24125 int ii, n;
24126
24127 if (it->current_x > it->last_visible_x)
24128 {
24129 if (!row->reversed_p)
24130 {
24131 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24132 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24133 break;
24134 }
24135 else
24136 {
24137 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24138 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24139 break;
24140 unproduce_glyphs (it, ii + 1);
24141 ii = row->used[TEXT_AREA] - (ii + 1);
24142 }
24143 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24144 {
24145 row->used[TEXT_AREA] = ii;
24146 produce_special_glyphs (it, IT_TRUNCATION);
24147 }
24148 }
24149 produce_special_glyphs (it, IT_TRUNCATION);
24150 }
24151 row->truncated_on_right_p = true;
24152 }
24153 break;
24154 }
24155 }
24156
24157 /* Maybe insert a truncation at the left. */
24158 if (it->first_visible_x
24159 && it_charpos > 0)
24160 {
24161 if (!FRAME_WINDOW_P (it->f)
24162 || (row->reversed_p
24163 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24164 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24165 insert_left_trunc_glyphs (it);
24166 row->truncated_on_left_p = true;
24167 }
24168
24169 it->face_id = saved_face_id;
24170
24171 /* Value is number of columns displayed. */
24172 return it->hpos - hpos_at_start;
24173 }
24174
24175
24176 \f
24177 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24178 appears as an element of LIST or as the car of an element of LIST.
24179 If PROPVAL is a list, compare each element against LIST in that
24180 way, and return 1/2 if any element of PROPVAL is found in LIST.
24181 Otherwise return 0. This function cannot quit.
24182 The return value is 2 if the text is invisible but with an ellipsis
24183 and 1 if it's invisible and without an ellipsis. */
24184
24185 int
24186 invisible_prop (Lisp_Object propval, Lisp_Object list)
24187 {
24188 Lisp_Object tail, proptail;
24189
24190 for (tail = list; CONSP (tail); tail = XCDR (tail))
24191 {
24192 register Lisp_Object tem;
24193 tem = XCAR (tail);
24194 if (EQ (propval, tem))
24195 return 1;
24196 if (CONSP (tem) && EQ (propval, XCAR (tem)))
24197 return NILP (XCDR (tem)) ? 1 : 2;
24198 }
24199
24200 if (CONSP (propval))
24201 {
24202 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
24203 {
24204 Lisp_Object propelt;
24205 propelt = XCAR (proptail);
24206 for (tail = list; CONSP (tail); tail = XCDR (tail))
24207 {
24208 register Lisp_Object tem;
24209 tem = XCAR (tail);
24210 if (EQ (propelt, tem))
24211 return 1;
24212 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
24213 return NILP (XCDR (tem)) ? 1 : 2;
24214 }
24215 }
24216 }
24217
24218 return 0;
24219 }
24220
24221 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
24222 doc: /* Non-nil if the property makes the text invisible.
24223 POS-OR-PROP can be a marker or number, in which case it is taken to be
24224 a position in the current buffer and the value of the `invisible' property
24225 is checked; or it can be some other value, which is then presumed to be the
24226 value of the `invisible' property of the text of interest.
24227 The non-nil value returned can be t for truly invisible text or something
24228 else if the text is replaced by an ellipsis. */)
24229 (Lisp_Object pos_or_prop)
24230 {
24231 Lisp_Object prop
24232 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
24233 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
24234 : pos_or_prop);
24235 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
24236 return (invis == 0 ? Qnil
24237 : invis == 1 ? Qt
24238 : make_number (invis));
24239 }
24240
24241 /* Calculate a width or height in pixels from a specification using
24242 the following elements:
24243
24244 SPEC ::=
24245 NUM - a (fractional) multiple of the default font width/height
24246 (NUM) - specifies exactly NUM pixels
24247 UNIT - a fixed number of pixels, see below.
24248 ELEMENT - size of a display element in pixels, see below.
24249 (NUM . SPEC) - equals NUM * SPEC
24250 (+ SPEC SPEC ...) - add pixel values
24251 (- SPEC SPEC ...) - subtract pixel values
24252 (- SPEC) - negate pixel value
24253
24254 NUM ::=
24255 INT or FLOAT - a number constant
24256 SYMBOL - use symbol's (buffer local) variable binding.
24257
24258 UNIT ::=
24259 in - pixels per inch *)
24260 mm - pixels per 1/1000 meter *)
24261 cm - pixels per 1/100 meter *)
24262 width - width of current font in pixels.
24263 height - height of current font in pixels.
24264
24265 *) using the ratio(s) defined in display-pixels-per-inch.
24266
24267 ELEMENT ::=
24268
24269 left-fringe - left fringe width in pixels
24270 right-fringe - right fringe width in pixels
24271
24272 left-margin - left margin width in pixels
24273 right-margin - right margin width in pixels
24274
24275 scroll-bar - scroll-bar area width in pixels
24276
24277 Examples:
24278
24279 Pixels corresponding to 5 inches:
24280 (5 . in)
24281
24282 Total width of non-text areas on left side of window (if scroll-bar is on left):
24283 '(space :width (+ left-fringe left-margin scroll-bar))
24284
24285 Align to first text column (in header line):
24286 '(space :align-to 0)
24287
24288 Align to middle of text area minus half the width of variable `my-image'
24289 containing a loaded image:
24290 '(space :align-to (0.5 . (- text my-image)))
24291
24292 Width of left margin minus width of 1 character in the default font:
24293 '(space :width (- left-margin 1))
24294
24295 Width of left margin minus width of 2 characters in the current font:
24296 '(space :width (- left-margin (2 . width)))
24297
24298 Center 1 character over left-margin (in header line):
24299 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24300
24301 Different ways to express width of left fringe plus left margin minus one pixel:
24302 '(space :width (- (+ left-fringe left-margin) (1)))
24303 '(space :width (+ left-fringe left-margin (- (1))))
24304 '(space :width (+ left-fringe left-margin (-1)))
24305
24306 */
24307
24308 static bool
24309 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24310 struct font *font, bool width_p, int *align_to)
24311 {
24312 double pixels;
24313
24314 # define OK_PIXELS(val) (*res = (val), true)
24315 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24316
24317 if (NILP (prop))
24318 return OK_PIXELS (0);
24319
24320 eassert (FRAME_LIVE_P (it->f));
24321
24322 if (SYMBOLP (prop))
24323 {
24324 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24325 {
24326 char *unit = SSDATA (SYMBOL_NAME (prop));
24327
24328 if (unit[0] == 'i' && unit[1] == 'n')
24329 pixels = 1.0;
24330 else if (unit[0] == 'm' && unit[1] == 'm')
24331 pixels = 25.4;
24332 else if (unit[0] == 'c' && unit[1] == 'm')
24333 pixels = 2.54;
24334 else
24335 pixels = 0;
24336 if (pixels > 0)
24337 {
24338 double ppi = (width_p ? FRAME_RES_X (it->f)
24339 : FRAME_RES_Y (it->f));
24340
24341 if (ppi > 0)
24342 return OK_PIXELS (ppi / pixels);
24343 return false;
24344 }
24345 }
24346
24347 #ifdef HAVE_WINDOW_SYSTEM
24348 if (EQ (prop, Qheight))
24349 return OK_PIXELS (font
24350 ? normal_char_height (font, -1)
24351 : FRAME_LINE_HEIGHT (it->f));
24352 if (EQ (prop, Qwidth))
24353 return OK_PIXELS (font
24354 ? FONT_WIDTH (font)
24355 : FRAME_COLUMN_WIDTH (it->f));
24356 #else
24357 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24358 return OK_PIXELS (1);
24359 #endif
24360
24361 if (EQ (prop, Qtext))
24362 return OK_PIXELS (width_p
24363 ? window_box_width (it->w, TEXT_AREA)
24364 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24365
24366 if (align_to && *align_to < 0)
24367 {
24368 *res = 0;
24369 if (EQ (prop, Qleft))
24370 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24371 if (EQ (prop, Qright))
24372 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24373 if (EQ (prop, Qcenter))
24374 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24375 + window_box_width (it->w, TEXT_AREA) / 2);
24376 if (EQ (prop, Qleft_fringe))
24377 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24378 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24379 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24380 if (EQ (prop, Qright_fringe))
24381 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24382 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24383 : window_box_right_offset (it->w, TEXT_AREA));
24384 if (EQ (prop, Qleft_margin))
24385 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24386 if (EQ (prop, Qright_margin))
24387 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24388 if (EQ (prop, Qscroll_bar))
24389 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24390 ? 0
24391 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24392 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24393 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24394 : 0)));
24395 }
24396 else
24397 {
24398 if (EQ (prop, Qleft_fringe))
24399 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24400 if (EQ (prop, Qright_fringe))
24401 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24402 if (EQ (prop, Qleft_margin))
24403 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24404 if (EQ (prop, Qright_margin))
24405 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24406 if (EQ (prop, Qscroll_bar))
24407 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24408 }
24409
24410 prop = buffer_local_value (prop, it->w->contents);
24411 if (EQ (prop, Qunbound))
24412 prop = Qnil;
24413 }
24414
24415 if (NUMBERP (prop))
24416 {
24417 int base_unit = (width_p
24418 ? FRAME_COLUMN_WIDTH (it->f)
24419 : FRAME_LINE_HEIGHT (it->f));
24420 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24421 }
24422
24423 if (CONSP (prop))
24424 {
24425 Lisp_Object car = XCAR (prop);
24426 Lisp_Object cdr = XCDR (prop);
24427
24428 if (SYMBOLP (car))
24429 {
24430 #ifdef HAVE_WINDOW_SYSTEM
24431 if (FRAME_WINDOW_P (it->f)
24432 && valid_image_p (prop))
24433 {
24434 ptrdiff_t id = lookup_image (it->f, prop);
24435 struct image *img = IMAGE_FROM_ID (it->f, id);
24436
24437 return OK_PIXELS (width_p ? img->width : img->height);
24438 }
24439 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
24440 {
24441 // TODO: Don't return dummy size.
24442 return OK_PIXELS (100);
24443 }
24444 #endif
24445 if (EQ (car, Qplus) || EQ (car, Qminus))
24446 {
24447 bool first = true;
24448 double px;
24449
24450 pixels = 0;
24451 while (CONSP (cdr))
24452 {
24453 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24454 font, width_p, align_to))
24455 return false;
24456 if (first)
24457 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24458 else
24459 pixels += px;
24460 cdr = XCDR (cdr);
24461 }
24462 if (EQ (car, Qminus))
24463 pixels = -pixels;
24464 return OK_PIXELS (pixels);
24465 }
24466
24467 car = buffer_local_value (car, it->w->contents);
24468 if (EQ (car, Qunbound))
24469 car = Qnil;
24470 }
24471
24472 if (NUMBERP (car))
24473 {
24474 double fact;
24475 pixels = XFLOATINT (car);
24476 if (NILP (cdr))
24477 return OK_PIXELS (pixels);
24478 if (calc_pixel_width_or_height (&fact, it, cdr,
24479 font, width_p, align_to))
24480 return OK_PIXELS (pixels * fact);
24481 return false;
24482 }
24483
24484 return false;
24485 }
24486
24487 return false;
24488 }
24489
24490 void
24491 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24492 {
24493 #ifdef HAVE_WINDOW_SYSTEM
24494 normal_char_ascent_descent (font, -1, ascent, descent);
24495 #else
24496 *ascent = 1;
24497 *descent = 0;
24498 #endif
24499 }
24500
24501 \f
24502 /***********************************************************************
24503 Glyph Display
24504 ***********************************************************************/
24505
24506 #ifdef HAVE_WINDOW_SYSTEM
24507
24508 #ifdef GLYPH_DEBUG
24509
24510 void
24511 dump_glyph_string (struct glyph_string *s)
24512 {
24513 fprintf (stderr, "glyph string\n");
24514 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24515 s->x, s->y, s->width, s->height);
24516 fprintf (stderr, " ybase = %d\n", s->ybase);
24517 fprintf (stderr, " hl = %d\n", s->hl);
24518 fprintf (stderr, " left overhang = %d, right = %d\n",
24519 s->left_overhang, s->right_overhang);
24520 fprintf (stderr, " nchars = %d\n", s->nchars);
24521 fprintf (stderr, " extends to end of line = %d\n",
24522 s->extends_to_end_of_line_p);
24523 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24524 fprintf (stderr, " bg width = %d\n", s->background_width);
24525 }
24526
24527 #endif /* GLYPH_DEBUG */
24528
24529 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24530 of XChar2b structures for S; it can't be allocated in
24531 init_glyph_string because it must be allocated via `alloca'. W
24532 is the window on which S is drawn. ROW and AREA are the glyph row
24533 and area within the row from which S is constructed. START is the
24534 index of the first glyph structure covered by S. HL is a
24535 face-override for drawing S. */
24536
24537 #ifdef HAVE_NTGUI
24538 #define OPTIONAL_HDC(hdc) HDC hdc,
24539 #define DECLARE_HDC(hdc) HDC hdc;
24540 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24541 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24542 #endif
24543
24544 #ifndef OPTIONAL_HDC
24545 #define OPTIONAL_HDC(hdc)
24546 #define DECLARE_HDC(hdc)
24547 #define ALLOCATE_HDC(hdc, f)
24548 #define RELEASE_HDC(hdc, f)
24549 #endif
24550
24551 static void
24552 init_glyph_string (struct glyph_string *s,
24553 OPTIONAL_HDC (hdc)
24554 XChar2b *char2b, struct window *w, struct glyph_row *row,
24555 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24556 {
24557 memset (s, 0, sizeof *s);
24558 s->w = w;
24559 s->f = XFRAME (w->frame);
24560 #ifdef HAVE_NTGUI
24561 s->hdc = hdc;
24562 #endif
24563 s->display = FRAME_X_DISPLAY (s->f);
24564 s->window = FRAME_X_WINDOW (s->f);
24565 s->char2b = char2b;
24566 s->hl = hl;
24567 s->row = row;
24568 s->area = area;
24569 s->first_glyph = row->glyphs[area] + start;
24570 s->height = row->height;
24571 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24572 s->ybase = s->y + row->ascent;
24573 }
24574
24575
24576 /* Append the list of glyph strings with head H and tail T to the list
24577 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24578
24579 static void
24580 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24581 struct glyph_string *h, struct glyph_string *t)
24582 {
24583 if (h)
24584 {
24585 if (*head)
24586 (*tail)->next = h;
24587 else
24588 *head = h;
24589 h->prev = *tail;
24590 *tail = t;
24591 }
24592 }
24593
24594
24595 /* Prepend the list of glyph strings with head H and tail T to the
24596 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24597 result. */
24598
24599 static void
24600 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24601 struct glyph_string *h, struct glyph_string *t)
24602 {
24603 if (h)
24604 {
24605 if (*head)
24606 (*head)->prev = t;
24607 else
24608 *tail = t;
24609 t->next = *head;
24610 *head = h;
24611 }
24612 }
24613
24614
24615 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24616 Set *HEAD and *TAIL to the resulting list. */
24617
24618 static void
24619 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24620 struct glyph_string *s)
24621 {
24622 s->next = s->prev = NULL;
24623 append_glyph_string_lists (head, tail, s, s);
24624 }
24625
24626
24627 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24628 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24629 make sure that X resources for the face returned are allocated.
24630 Value is a pointer to a realized face that is ready for display if
24631 DISPLAY_P. */
24632
24633 static struct face *
24634 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24635 XChar2b *char2b, bool display_p)
24636 {
24637 struct face *face = FACE_FROM_ID (f, face_id);
24638 unsigned code = 0;
24639
24640 if (face->font)
24641 {
24642 code = face->font->driver->encode_char (face->font, c);
24643
24644 if (code == FONT_INVALID_CODE)
24645 code = 0;
24646 }
24647 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24648
24649 /* Make sure X resources of the face are allocated. */
24650 #ifdef HAVE_X_WINDOWS
24651 if (display_p)
24652 #endif
24653 {
24654 eassert (face != NULL);
24655 prepare_face_for_display (f, face);
24656 }
24657
24658 return face;
24659 }
24660
24661
24662 /* Get face and two-byte form of character glyph GLYPH on frame F.
24663 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24664 a pointer to a realized face that is ready for display. */
24665
24666 static struct face *
24667 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24668 XChar2b *char2b)
24669 {
24670 struct face *face;
24671 unsigned code = 0;
24672
24673 eassert (glyph->type == CHAR_GLYPH);
24674 face = FACE_FROM_ID (f, glyph->face_id);
24675
24676 /* Make sure X resources of the face are allocated. */
24677 prepare_face_for_display (f, face);
24678
24679 if (face->font)
24680 {
24681 if (CHAR_BYTE8_P (glyph->u.ch))
24682 code = CHAR_TO_BYTE8 (glyph->u.ch);
24683 else
24684 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24685
24686 if (code == FONT_INVALID_CODE)
24687 code = 0;
24688 }
24689
24690 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24691 return face;
24692 }
24693
24694
24695 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24696 Return true iff FONT has a glyph for C. */
24697
24698 static bool
24699 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24700 {
24701 unsigned code;
24702
24703 if (CHAR_BYTE8_P (c))
24704 code = CHAR_TO_BYTE8 (c);
24705 else
24706 code = font->driver->encode_char (font, c);
24707
24708 if (code == FONT_INVALID_CODE)
24709 return false;
24710 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24711 return true;
24712 }
24713
24714
24715 /* Fill glyph string S with composition components specified by S->cmp.
24716
24717 BASE_FACE is the base face of the composition.
24718 S->cmp_from is the index of the first component for S.
24719
24720 OVERLAPS non-zero means S should draw the foreground only, and use
24721 its physical height for clipping. See also draw_glyphs.
24722
24723 Value is the index of a component not in S. */
24724
24725 static int
24726 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24727 int overlaps)
24728 {
24729 int i;
24730 /* For all glyphs of this composition, starting at the offset
24731 S->cmp_from, until we reach the end of the definition or encounter a
24732 glyph that requires the different face, add it to S. */
24733 struct face *face;
24734
24735 eassert (s);
24736
24737 s->for_overlaps = overlaps;
24738 s->face = NULL;
24739 s->font = NULL;
24740 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24741 {
24742 int c = COMPOSITION_GLYPH (s->cmp, i);
24743
24744 /* TAB in a composition means display glyphs with padding space
24745 on the left or right. */
24746 if (c != '\t')
24747 {
24748 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24749 -1, Qnil);
24750
24751 face = get_char_face_and_encoding (s->f, c, face_id,
24752 s->char2b + i, true);
24753 if (face)
24754 {
24755 if (! s->face)
24756 {
24757 s->face = face;
24758 s->font = s->face->font;
24759 }
24760 else if (s->face != face)
24761 break;
24762 }
24763 }
24764 ++s->nchars;
24765 }
24766 s->cmp_to = i;
24767
24768 if (s->face == NULL)
24769 {
24770 s->face = base_face->ascii_face;
24771 s->font = s->face->font;
24772 }
24773
24774 /* All glyph strings for the same composition has the same width,
24775 i.e. the width set for the first component of the composition. */
24776 s->width = s->first_glyph->pixel_width;
24777
24778 /* If the specified font could not be loaded, use the frame's
24779 default font, but record the fact that we couldn't load it in
24780 the glyph string so that we can draw rectangles for the
24781 characters of the glyph string. */
24782 if (s->font == NULL)
24783 {
24784 s->font_not_found_p = true;
24785 s->font = FRAME_FONT (s->f);
24786 }
24787
24788 /* Adjust base line for subscript/superscript text. */
24789 s->ybase += s->first_glyph->voffset;
24790
24791 return s->cmp_to;
24792 }
24793
24794 static int
24795 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24796 int start, int end, int overlaps)
24797 {
24798 struct glyph *glyph, *last;
24799 Lisp_Object lgstring;
24800 int i;
24801
24802 s->for_overlaps = overlaps;
24803 glyph = s->row->glyphs[s->area] + start;
24804 last = s->row->glyphs[s->area] + end;
24805 s->cmp_id = glyph->u.cmp.id;
24806 s->cmp_from = glyph->slice.cmp.from;
24807 s->cmp_to = glyph->slice.cmp.to + 1;
24808 s->face = FACE_FROM_ID (s->f, face_id);
24809 lgstring = composition_gstring_from_id (s->cmp_id);
24810 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24811 glyph++;
24812 while (glyph < last
24813 && glyph->u.cmp.automatic
24814 && glyph->u.cmp.id == s->cmp_id
24815 && s->cmp_to == glyph->slice.cmp.from)
24816 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24817
24818 for (i = s->cmp_from; i < s->cmp_to; i++)
24819 {
24820 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24821 unsigned code = LGLYPH_CODE (lglyph);
24822
24823 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24824 }
24825 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24826 return glyph - s->row->glyphs[s->area];
24827 }
24828
24829
24830 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24831 See the comment of fill_glyph_string for arguments.
24832 Value is the index of the first glyph not in S. */
24833
24834
24835 static int
24836 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24837 int start, int end, int overlaps)
24838 {
24839 struct glyph *glyph, *last;
24840 int voffset;
24841
24842 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24843 s->for_overlaps = overlaps;
24844 glyph = s->row->glyphs[s->area] + start;
24845 last = s->row->glyphs[s->area] + end;
24846 voffset = glyph->voffset;
24847 s->face = FACE_FROM_ID (s->f, face_id);
24848 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24849 s->nchars = 1;
24850 s->width = glyph->pixel_width;
24851 glyph++;
24852 while (glyph < last
24853 && glyph->type == GLYPHLESS_GLYPH
24854 && glyph->voffset == voffset
24855 && glyph->face_id == face_id)
24856 {
24857 s->nchars++;
24858 s->width += glyph->pixel_width;
24859 glyph++;
24860 }
24861 s->ybase += voffset;
24862 return glyph - s->row->glyphs[s->area];
24863 }
24864
24865
24866 /* Fill glyph string S from a sequence of character glyphs.
24867
24868 FACE_ID is the face id of the string. START is the index of the
24869 first glyph to consider, END is the index of the last + 1.
24870 OVERLAPS non-zero means S should draw the foreground only, and use
24871 its physical height for clipping. See also draw_glyphs.
24872
24873 Value is the index of the first glyph not in S. */
24874
24875 static int
24876 fill_glyph_string (struct glyph_string *s, int face_id,
24877 int start, int end, int overlaps)
24878 {
24879 struct glyph *glyph, *last;
24880 int voffset;
24881 bool glyph_not_available_p;
24882
24883 eassert (s->f == XFRAME (s->w->frame));
24884 eassert (s->nchars == 0);
24885 eassert (start >= 0 && end > start);
24886
24887 s->for_overlaps = overlaps;
24888 glyph = s->row->glyphs[s->area] + start;
24889 last = s->row->glyphs[s->area] + end;
24890 voffset = glyph->voffset;
24891 s->padding_p = glyph->padding_p;
24892 glyph_not_available_p = glyph->glyph_not_available_p;
24893
24894 while (glyph < last
24895 && glyph->type == CHAR_GLYPH
24896 && glyph->voffset == voffset
24897 /* Same face id implies same font, nowadays. */
24898 && glyph->face_id == face_id
24899 && glyph->glyph_not_available_p == glyph_not_available_p)
24900 {
24901 s->face = get_glyph_face_and_encoding (s->f, glyph,
24902 s->char2b + s->nchars);
24903 ++s->nchars;
24904 eassert (s->nchars <= end - start);
24905 s->width += glyph->pixel_width;
24906 if (glyph++->padding_p != s->padding_p)
24907 break;
24908 }
24909
24910 s->font = s->face->font;
24911
24912 /* If the specified font could not be loaded, use the frame's font,
24913 but record the fact that we couldn't load it in
24914 S->font_not_found_p so that we can draw rectangles for the
24915 characters of the glyph string. */
24916 if (s->font == NULL || glyph_not_available_p)
24917 {
24918 s->font_not_found_p = true;
24919 s->font = FRAME_FONT (s->f);
24920 }
24921
24922 /* Adjust base line for subscript/superscript text. */
24923 s->ybase += voffset;
24924
24925 eassert (s->face && s->face->gc);
24926 return glyph - s->row->glyphs[s->area];
24927 }
24928
24929
24930 /* Fill glyph string S from image glyph S->first_glyph. */
24931
24932 static void
24933 fill_image_glyph_string (struct glyph_string *s)
24934 {
24935 eassert (s->first_glyph->type == IMAGE_GLYPH);
24936 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24937 eassert (s->img);
24938 s->slice = s->first_glyph->slice.img;
24939 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24940 s->font = s->face->font;
24941 s->width = s->first_glyph->pixel_width;
24942
24943 /* Adjust base line for subscript/superscript text. */
24944 s->ybase += s->first_glyph->voffset;
24945 }
24946
24947
24948 #ifdef HAVE_XWIDGETS
24949 static void
24950 fill_xwidget_glyph_string (struct glyph_string *s)
24951 {
24952 eassert (s->first_glyph->type == XWIDGET_GLYPH);
24953 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24954 s->font = s->face->font;
24955 s->width = s->first_glyph->pixel_width;
24956 s->ybase += s->first_glyph->voffset;
24957 s->xwidget = s->first_glyph->u.xwidget;
24958 }
24959 #endif
24960 /* Fill glyph string S from a sequence of stretch glyphs.
24961
24962 START is the index of the first glyph to consider,
24963 END is the index of the last + 1.
24964
24965 Value is the index of the first glyph not in S. */
24966
24967 static int
24968 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24969 {
24970 struct glyph *glyph, *last;
24971 int voffset, face_id;
24972
24973 eassert (s->first_glyph->type == STRETCH_GLYPH);
24974
24975 glyph = s->row->glyphs[s->area] + start;
24976 last = s->row->glyphs[s->area] + end;
24977 face_id = glyph->face_id;
24978 s->face = FACE_FROM_ID (s->f, face_id);
24979 s->font = s->face->font;
24980 s->width = glyph->pixel_width;
24981 s->nchars = 1;
24982 voffset = glyph->voffset;
24983
24984 for (++glyph;
24985 (glyph < last
24986 && glyph->type == STRETCH_GLYPH
24987 && glyph->voffset == voffset
24988 && glyph->face_id == face_id);
24989 ++glyph)
24990 s->width += glyph->pixel_width;
24991
24992 /* Adjust base line for subscript/superscript text. */
24993 s->ybase += voffset;
24994
24995 /* The case that face->gc == 0 is handled when drawing the glyph
24996 string by calling prepare_face_for_display. */
24997 eassert (s->face);
24998 return glyph - s->row->glyphs[s->area];
24999 }
25000
25001 static struct font_metrics *
25002 get_per_char_metric (struct font *font, XChar2b *char2b)
25003 {
25004 static struct font_metrics metrics;
25005 unsigned code;
25006
25007 if (! font)
25008 return NULL;
25009 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25010 if (code == FONT_INVALID_CODE)
25011 return NULL;
25012 font->driver->text_extents (font, &code, 1, &metrics);
25013 return &metrics;
25014 }
25015
25016 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25017 for FONT. Values are taken from font-global ones, except for fonts
25018 that claim preposterously large values, but whose glyphs actually
25019 have reasonable dimensions. C is the character to use for metrics
25020 if the font-global values are too large; if C is negative, the
25021 function selects a default character. */
25022 static void
25023 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25024 {
25025 *ascent = FONT_BASE (font);
25026 *descent = FONT_DESCENT (font);
25027
25028 if (FONT_TOO_HIGH (font))
25029 {
25030 XChar2b char2b;
25031
25032 /* Get metrics of C, defaulting to a reasonably sized ASCII
25033 character. */
25034 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25035 {
25036 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25037
25038 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25039 {
25040 /* We add 1 pixel to character dimensions as heuristics
25041 that produces nicer display, e.g. when the face has
25042 the box attribute. */
25043 *ascent = pcm->ascent + 1;
25044 *descent = pcm->descent + 1;
25045 }
25046 }
25047 }
25048 }
25049
25050 /* A subroutine that computes a reasonable "normal character height"
25051 for fonts that claim preposterously large vertical dimensions, but
25052 whose glyphs are actually reasonably sized. C is the character
25053 whose metrics to use for those fonts, or -1 for default
25054 character. */
25055 static int
25056 normal_char_height (struct font *font, int c)
25057 {
25058 int ascent, descent;
25059
25060 normal_char_ascent_descent (font, c, &ascent, &descent);
25061
25062 return ascent + descent;
25063 }
25064
25065 /* EXPORT for RIF:
25066 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
25067 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
25068 assumed to be zero. */
25069
25070 void
25071 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
25072 {
25073 *left = *right = 0;
25074
25075 if (glyph->type == CHAR_GLYPH)
25076 {
25077 XChar2b char2b;
25078 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
25079 if (face->font)
25080 {
25081 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
25082 if (pcm)
25083 {
25084 if (pcm->rbearing > pcm->width)
25085 *right = pcm->rbearing - pcm->width;
25086 if (pcm->lbearing < 0)
25087 *left = -pcm->lbearing;
25088 }
25089 }
25090 }
25091 else if (glyph->type == COMPOSITE_GLYPH)
25092 {
25093 if (! glyph->u.cmp.automatic)
25094 {
25095 struct composition *cmp = composition_table[glyph->u.cmp.id];
25096
25097 if (cmp->rbearing > cmp->pixel_width)
25098 *right = cmp->rbearing - cmp->pixel_width;
25099 if (cmp->lbearing < 0)
25100 *left = - cmp->lbearing;
25101 }
25102 else
25103 {
25104 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25105 struct font_metrics metrics;
25106
25107 composition_gstring_width (gstring, glyph->slice.cmp.from,
25108 glyph->slice.cmp.to + 1, &metrics);
25109 if (metrics.rbearing > metrics.width)
25110 *right = metrics.rbearing - metrics.width;
25111 if (metrics.lbearing < 0)
25112 *left = - metrics.lbearing;
25113 }
25114 }
25115 }
25116
25117
25118 /* Return the index of the first glyph preceding glyph string S that
25119 is overwritten by S because of S's left overhang. Value is -1
25120 if no glyphs are overwritten. */
25121
25122 static int
25123 left_overwritten (struct glyph_string *s)
25124 {
25125 int k;
25126
25127 if (s->left_overhang)
25128 {
25129 int x = 0, i;
25130 struct glyph *glyphs = s->row->glyphs[s->area];
25131 int first = s->first_glyph - glyphs;
25132
25133 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25134 x -= glyphs[i].pixel_width;
25135
25136 k = i + 1;
25137 }
25138 else
25139 k = -1;
25140
25141 return k;
25142 }
25143
25144
25145 /* Return the index of the first glyph preceding glyph string S that
25146 is overwriting S because of its right overhang. Value is -1 if no
25147 glyph in front of S overwrites S. */
25148
25149 static int
25150 left_overwriting (struct glyph_string *s)
25151 {
25152 int i, k, x;
25153 struct glyph *glyphs = s->row->glyphs[s->area];
25154 int first = s->first_glyph - glyphs;
25155
25156 k = -1;
25157 x = 0;
25158 for (i = first - 1; i >= 0; --i)
25159 {
25160 int left, right;
25161 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25162 if (x + right > 0)
25163 k = i;
25164 x -= glyphs[i].pixel_width;
25165 }
25166
25167 return k;
25168 }
25169
25170
25171 /* Return the index of the last glyph following glyph string S that is
25172 overwritten by S because of S's right overhang. Value is -1 if
25173 no such glyph is found. */
25174
25175 static int
25176 right_overwritten (struct glyph_string *s)
25177 {
25178 int k = -1;
25179
25180 if (s->right_overhang)
25181 {
25182 int x = 0, i;
25183 struct glyph *glyphs = s->row->glyphs[s->area];
25184 int first = (s->first_glyph - glyphs
25185 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25186 int end = s->row->used[s->area];
25187
25188 for (i = first; i < end && s->right_overhang > x; ++i)
25189 x += glyphs[i].pixel_width;
25190
25191 k = i;
25192 }
25193
25194 return k;
25195 }
25196
25197
25198 /* Return the index of the last glyph following glyph string S that
25199 overwrites S because of its left overhang. Value is negative
25200 if no such glyph is found. */
25201
25202 static int
25203 right_overwriting (struct glyph_string *s)
25204 {
25205 int i, k, x;
25206 int end = s->row->used[s->area];
25207 struct glyph *glyphs = s->row->glyphs[s->area];
25208 int first = (s->first_glyph - glyphs
25209 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25210
25211 k = -1;
25212 x = 0;
25213 for (i = first; i < end; ++i)
25214 {
25215 int left, right;
25216 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25217 if (x - left < 0)
25218 k = i;
25219 x += glyphs[i].pixel_width;
25220 }
25221
25222 return k;
25223 }
25224
25225
25226 /* Set background width of glyph string S. START is the index of the
25227 first glyph following S. LAST_X is the right-most x-position + 1
25228 in the drawing area. */
25229
25230 static void
25231 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
25232 {
25233 /* If the face of this glyph string has to be drawn to the end of
25234 the drawing area, set S->extends_to_end_of_line_p. */
25235
25236 if (start == s->row->used[s->area]
25237 && ((s->row->fill_line_p
25238 && (s->hl == DRAW_NORMAL_TEXT
25239 || s->hl == DRAW_IMAGE_RAISED
25240 || s->hl == DRAW_IMAGE_SUNKEN))
25241 || s->hl == DRAW_MOUSE_FACE))
25242 s->extends_to_end_of_line_p = true;
25243
25244 /* If S extends its face to the end of the line, set its
25245 background_width to the distance to the right edge of the drawing
25246 area. */
25247 if (s->extends_to_end_of_line_p)
25248 s->background_width = last_x - s->x + 1;
25249 else
25250 s->background_width = s->width;
25251 }
25252
25253
25254 /* Compute overhangs and x-positions for glyph string S and its
25255 predecessors, or successors. X is the starting x-position for S.
25256 BACKWARD_P means process predecessors. */
25257
25258 static void
25259 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
25260 {
25261 if (backward_p)
25262 {
25263 while (s)
25264 {
25265 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25266 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25267 x -= s->width;
25268 s->x = x;
25269 s = s->prev;
25270 }
25271 }
25272 else
25273 {
25274 while (s)
25275 {
25276 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25277 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25278 s->x = x;
25279 x += s->width;
25280 s = s->next;
25281 }
25282 }
25283 }
25284
25285
25286
25287 /* The following macros are only called from draw_glyphs below.
25288 They reference the following parameters of that function directly:
25289 `w', `row', `area', and `overlap_p'
25290 as well as the following local variables:
25291 `s', `f', and `hdc' (in W32) */
25292
25293 #ifdef HAVE_NTGUI
25294 /* On W32, silently add local `hdc' variable to argument list of
25295 init_glyph_string. */
25296 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25297 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25298 #else
25299 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25300 init_glyph_string (s, char2b, w, row, area, start, hl)
25301 #endif
25302
25303 /* Add a glyph string for a stretch glyph to the list of strings
25304 between HEAD and TAIL. START is the index of the stretch glyph in
25305 row area AREA of glyph row ROW. END is the index of the last glyph
25306 in that glyph row area. X is the current output position assigned
25307 to the new glyph string constructed. HL overrides that face of the
25308 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25309 is the right-most x-position of the drawing area. */
25310
25311 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25312 and below -- keep them on one line. */
25313 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25314 do \
25315 { \
25316 s = alloca (sizeof *s); \
25317 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25318 START = fill_stretch_glyph_string (s, START, END); \
25319 append_glyph_string (&HEAD, &TAIL, s); \
25320 s->x = (X); \
25321 } \
25322 while (false)
25323
25324
25325 /* Add a glyph string for an image glyph to the list of strings
25326 between HEAD and TAIL. START is the index of the image glyph in
25327 row area AREA of glyph row ROW. END is the index of the last glyph
25328 in that glyph row area. X is the current output position assigned
25329 to the new glyph string constructed. HL overrides that face of the
25330 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25331 is the right-most x-position of the drawing area. */
25332
25333 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25334 do \
25335 { \
25336 s = alloca (sizeof *s); \
25337 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25338 fill_image_glyph_string (s); \
25339 append_glyph_string (&HEAD, &TAIL, s); \
25340 ++START; \
25341 s->x = (X); \
25342 } \
25343 while (false)
25344
25345 #ifndef HAVE_XWIDGETS
25346 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25347 eassume (false)
25348 #else
25349 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25350 do \
25351 { \
25352 s = alloca (sizeof *s); \
25353 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25354 fill_xwidget_glyph_string (s); \
25355 append_glyph_string (&(HEAD), &(TAIL), s); \
25356 ++(START); \
25357 s->x = (X); \
25358 } \
25359 while (false)
25360 #endif
25361
25362 /* Add a glyph string for a sequence of character glyphs to the list
25363 of strings between HEAD and TAIL. START is the index of the first
25364 glyph in row area AREA of glyph row ROW that is part of the new
25365 glyph string. END is the index of the last glyph in that glyph row
25366 area. X is the current output position assigned to the new glyph
25367 string constructed. HL overrides that face of the glyph; e.g. it
25368 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25369 right-most x-position of the drawing area. */
25370
25371 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25372 do \
25373 { \
25374 int face_id; \
25375 XChar2b *char2b; \
25376 \
25377 face_id = (row)->glyphs[area][START].face_id; \
25378 \
25379 s = alloca (sizeof *s); \
25380 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25381 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25382 append_glyph_string (&HEAD, &TAIL, s); \
25383 s->x = (X); \
25384 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25385 } \
25386 while (false)
25387
25388
25389 /* Add a glyph string for a composite sequence to the list of strings
25390 between HEAD and TAIL. START is the index of the first glyph in
25391 row area AREA of glyph row ROW that is part of the new glyph
25392 string. END is the index of the last glyph in that glyph row area.
25393 X is the current output position assigned to the new glyph string
25394 constructed. HL overrides that face of the glyph; e.g. it is
25395 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25396 x-position of the drawing area. */
25397
25398 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25399 do { \
25400 int face_id = (row)->glyphs[area][START].face_id; \
25401 struct face *base_face = FACE_FROM_ID (f, face_id); \
25402 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25403 struct composition *cmp = composition_table[cmp_id]; \
25404 XChar2b *char2b; \
25405 struct glyph_string *first_s = NULL; \
25406 int n; \
25407 \
25408 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25409 \
25410 /* Make glyph_strings for each glyph sequence that is drawable by \
25411 the same face, and append them to HEAD/TAIL. */ \
25412 for (n = 0; n < cmp->glyph_len;) \
25413 { \
25414 s = alloca (sizeof *s); \
25415 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25416 append_glyph_string (&(HEAD), &(TAIL), s); \
25417 s->cmp = cmp; \
25418 s->cmp_from = n; \
25419 s->x = (X); \
25420 if (n == 0) \
25421 first_s = s; \
25422 n = fill_composite_glyph_string (s, base_face, overlaps); \
25423 } \
25424 \
25425 ++START; \
25426 s = first_s; \
25427 } while (false)
25428
25429
25430 /* Add a glyph string for a glyph-string sequence to the list of strings
25431 between HEAD and TAIL. */
25432
25433 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25434 do { \
25435 int face_id; \
25436 XChar2b *char2b; \
25437 Lisp_Object gstring; \
25438 \
25439 face_id = (row)->glyphs[area][START].face_id; \
25440 gstring = (composition_gstring_from_id \
25441 ((row)->glyphs[area][START].u.cmp.id)); \
25442 s = alloca (sizeof *s); \
25443 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25444 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25445 append_glyph_string (&(HEAD), &(TAIL), s); \
25446 s->x = (X); \
25447 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25448 } while (false)
25449
25450
25451 /* Add a glyph string for a sequence of glyphless character's glyphs
25452 to the list of strings between HEAD and TAIL. The meanings of
25453 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25454
25455 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25456 do \
25457 { \
25458 int face_id; \
25459 \
25460 face_id = (row)->glyphs[area][START].face_id; \
25461 \
25462 s = alloca (sizeof *s); \
25463 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25464 append_glyph_string (&HEAD, &TAIL, s); \
25465 s->x = (X); \
25466 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25467 overlaps); \
25468 } \
25469 while (false)
25470
25471
25472 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25473 of AREA of glyph row ROW on window W between indices START and END.
25474 HL overrides the face for drawing glyph strings, e.g. it is
25475 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25476 x-positions of the drawing area.
25477
25478 This is an ugly monster macro construct because we must use alloca
25479 to allocate glyph strings (because draw_glyphs can be called
25480 asynchronously). */
25481
25482 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25483 do \
25484 { \
25485 HEAD = TAIL = NULL; \
25486 while (START < END) \
25487 { \
25488 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25489 switch (first_glyph->type) \
25490 { \
25491 case CHAR_GLYPH: \
25492 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25493 HL, X, LAST_X); \
25494 break; \
25495 \
25496 case COMPOSITE_GLYPH: \
25497 if (first_glyph->u.cmp.automatic) \
25498 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25499 HL, X, LAST_X); \
25500 else \
25501 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25502 HL, X, LAST_X); \
25503 break; \
25504 \
25505 case STRETCH_GLYPH: \
25506 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25507 HL, X, LAST_X); \
25508 break; \
25509 \
25510 case IMAGE_GLYPH: \
25511 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25512 HL, X, LAST_X); \
25513 break;
25514
25515 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25516 case XWIDGET_GLYPH: \
25517 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
25518 HL, X, LAST_X); \
25519 break;
25520
25521 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
25522 case GLYPHLESS_GLYPH: \
25523 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25524 HL, X, LAST_X); \
25525 break; \
25526 \
25527 default: \
25528 emacs_abort (); \
25529 } \
25530 \
25531 if (s) \
25532 { \
25533 set_glyph_string_background_width (s, START, LAST_X); \
25534 (X) += s->width; \
25535 } \
25536 } \
25537 } while (false)
25538
25539
25540 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25541 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25542 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25543 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
25544
25545
25546 /* Draw glyphs between START and END in AREA of ROW on window W,
25547 starting at x-position X. X is relative to AREA in W. HL is a
25548 face-override with the following meaning:
25549
25550 DRAW_NORMAL_TEXT draw normally
25551 DRAW_CURSOR draw in cursor face
25552 DRAW_MOUSE_FACE draw in mouse face.
25553 DRAW_INVERSE_VIDEO draw in mode line face
25554 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25555 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25556
25557 If OVERLAPS is non-zero, draw only the foreground of characters and
25558 clip to the physical height of ROW. Non-zero value also defines
25559 the overlapping part to be drawn:
25560
25561 OVERLAPS_PRED overlap with preceding rows
25562 OVERLAPS_SUCC overlap with succeeding rows
25563 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25564 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25565
25566 Value is the x-position reached, relative to AREA of W. */
25567
25568 static int
25569 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25570 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25571 enum draw_glyphs_face hl, int overlaps)
25572 {
25573 struct glyph_string *head, *tail;
25574 struct glyph_string *s;
25575 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25576 int i, j, x_reached, last_x, area_left = 0;
25577 struct frame *f = XFRAME (WINDOW_FRAME (w));
25578 DECLARE_HDC (hdc);
25579
25580 ALLOCATE_HDC (hdc, f);
25581
25582 /* Let's rather be paranoid than getting a SEGV. */
25583 end = min (end, row->used[area]);
25584 start = clip_to_bounds (0, start, end);
25585
25586 /* Translate X to frame coordinates. Set last_x to the right
25587 end of the drawing area. */
25588 if (row->full_width_p)
25589 {
25590 /* X is relative to the left edge of W, without scroll bars
25591 or fringes. */
25592 area_left = WINDOW_LEFT_EDGE_X (w);
25593 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25594 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25595 }
25596 else
25597 {
25598 area_left = window_box_left (w, area);
25599 last_x = area_left + window_box_width (w, area);
25600 }
25601 x += area_left;
25602
25603 /* Build a doubly-linked list of glyph_string structures between
25604 head and tail from what we have to draw. Note that the macro
25605 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25606 the reason we use a separate variable `i'. */
25607 i = start;
25608 USE_SAFE_ALLOCA;
25609 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25610 if (tail)
25611 x_reached = tail->x + tail->background_width;
25612 else
25613 x_reached = x;
25614
25615 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25616 the row, redraw some glyphs in front or following the glyph
25617 strings built above. */
25618 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25619 {
25620 struct glyph_string *h, *t;
25621 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25622 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
25623 bool check_mouse_face = false;
25624 int dummy_x = 0;
25625
25626 /* If mouse highlighting is on, we may need to draw adjacent
25627 glyphs using mouse-face highlighting. */
25628 if (area == TEXT_AREA && row->mouse_face_p
25629 && hlinfo->mouse_face_beg_row >= 0
25630 && hlinfo->mouse_face_end_row >= 0)
25631 {
25632 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25633
25634 if (row_vpos >= hlinfo->mouse_face_beg_row
25635 && row_vpos <= hlinfo->mouse_face_end_row)
25636 {
25637 check_mouse_face = true;
25638 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25639 ? hlinfo->mouse_face_beg_col : 0;
25640 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25641 ? hlinfo->mouse_face_end_col
25642 : row->used[TEXT_AREA];
25643 }
25644 }
25645
25646 /* Compute overhangs for all glyph strings. */
25647 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25648 for (s = head; s; s = s->next)
25649 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25650
25651 /* Prepend glyph strings for glyphs in front of the first glyph
25652 string that are overwritten because of the first glyph
25653 string's left overhang. The background of all strings
25654 prepended must be drawn because the first glyph string
25655 draws over it. */
25656 i = left_overwritten (head);
25657 if (i >= 0)
25658 {
25659 enum draw_glyphs_face overlap_hl;
25660
25661 /* If this row contains mouse highlighting, attempt to draw
25662 the overlapped glyphs with the correct highlight. This
25663 code fails if the overlap encompasses more than one glyph
25664 and mouse-highlight spans only some of these glyphs.
25665 However, making it work perfectly involves a lot more
25666 code, and I don't know if the pathological case occurs in
25667 practice, so we'll stick to this for now. --- cyd */
25668 if (check_mouse_face
25669 && mouse_beg_col < start && mouse_end_col > i)
25670 overlap_hl = DRAW_MOUSE_FACE;
25671 else
25672 overlap_hl = DRAW_NORMAL_TEXT;
25673
25674 if (hl != overlap_hl)
25675 clip_head = head;
25676 j = i;
25677 BUILD_GLYPH_STRINGS (j, start, h, t,
25678 overlap_hl, dummy_x, last_x);
25679 start = i;
25680 compute_overhangs_and_x (t, head->x, true);
25681 prepend_glyph_string_lists (&head, &tail, h, t);
25682 if (clip_head == NULL)
25683 clip_head = head;
25684 }
25685
25686 /* Prepend glyph strings for glyphs in front of the first glyph
25687 string that overwrite that glyph string because of their
25688 right overhang. For these strings, only the foreground must
25689 be drawn, because it draws over the glyph string at `head'.
25690 The background must not be drawn because this would overwrite
25691 right overhangs of preceding glyphs for which no glyph
25692 strings exist. */
25693 i = left_overwriting (head);
25694 if (i >= 0)
25695 {
25696 enum draw_glyphs_face overlap_hl;
25697
25698 if (check_mouse_face
25699 && mouse_beg_col < start && mouse_end_col > i)
25700 overlap_hl = DRAW_MOUSE_FACE;
25701 else
25702 overlap_hl = DRAW_NORMAL_TEXT;
25703
25704 if (hl == overlap_hl || clip_head == NULL)
25705 clip_head = head;
25706 BUILD_GLYPH_STRINGS (i, start, h, t,
25707 overlap_hl, dummy_x, last_x);
25708 for (s = h; s; s = s->next)
25709 s->background_filled_p = true;
25710 compute_overhangs_and_x (t, head->x, true);
25711 prepend_glyph_string_lists (&head, &tail, h, t);
25712 }
25713
25714 /* Append glyphs strings for glyphs following the last glyph
25715 string tail that are overwritten by tail. The background of
25716 these strings has to be drawn because tail's foreground draws
25717 over it. */
25718 i = right_overwritten (tail);
25719 if (i >= 0)
25720 {
25721 enum draw_glyphs_face overlap_hl;
25722
25723 if (check_mouse_face
25724 && mouse_beg_col < i && mouse_end_col > end)
25725 overlap_hl = DRAW_MOUSE_FACE;
25726 else
25727 overlap_hl = DRAW_NORMAL_TEXT;
25728
25729 if (hl != overlap_hl)
25730 clip_tail = tail;
25731 BUILD_GLYPH_STRINGS (end, i, h, t,
25732 overlap_hl, x, last_x);
25733 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25734 we don't have `end = i;' here. */
25735 compute_overhangs_and_x (h, tail->x + tail->width, false);
25736 append_glyph_string_lists (&head, &tail, h, t);
25737 if (clip_tail == NULL)
25738 clip_tail = tail;
25739 }
25740
25741 /* Append glyph strings for glyphs following the last glyph
25742 string tail that overwrite tail. The foreground of such
25743 glyphs has to be drawn because it writes into the background
25744 of tail. The background must not be drawn because it could
25745 paint over the foreground of following glyphs. */
25746 i = right_overwriting (tail);
25747 if (i >= 0)
25748 {
25749 enum draw_glyphs_face overlap_hl;
25750 if (check_mouse_face
25751 && mouse_beg_col < i && mouse_end_col > end)
25752 overlap_hl = DRAW_MOUSE_FACE;
25753 else
25754 overlap_hl = DRAW_NORMAL_TEXT;
25755
25756 if (hl == overlap_hl || clip_tail == NULL)
25757 clip_tail = tail;
25758 i++; /* We must include the Ith glyph. */
25759 BUILD_GLYPH_STRINGS (end, i, h, t,
25760 overlap_hl, x, last_x);
25761 for (s = h; s; s = s->next)
25762 s->background_filled_p = true;
25763 compute_overhangs_and_x (h, tail->x + tail->width, false);
25764 append_glyph_string_lists (&head, &tail, h, t);
25765 }
25766 if (clip_head || clip_tail)
25767 for (s = head; s; s = s->next)
25768 {
25769 s->clip_head = clip_head;
25770 s->clip_tail = clip_tail;
25771 }
25772 }
25773
25774 /* Draw all strings. */
25775 for (s = head; s; s = s->next)
25776 FRAME_RIF (f)->draw_glyph_string (s);
25777
25778 #ifndef HAVE_NS
25779 /* When focus a sole frame and move horizontally, this clears on_p
25780 causing a failure to erase prev cursor position. */
25781 if (area == TEXT_AREA
25782 && !row->full_width_p
25783 /* When drawing overlapping rows, only the glyph strings'
25784 foreground is drawn, which doesn't erase a cursor
25785 completely. */
25786 && !overlaps)
25787 {
25788 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25789 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25790 : (tail ? tail->x + tail->background_width : x));
25791 x0 -= area_left;
25792 x1 -= area_left;
25793
25794 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25795 row->y, MATRIX_ROW_BOTTOM_Y (row));
25796 }
25797 #endif
25798
25799 /* Value is the x-position up to which drawn, relative to AREA of W.
25800 This doesn't include parts drawn because of overhangs. */
25801 if (row->full_width_p)
25802 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25803 else
25804 x_reached -= area_left;
25805
25806 RELEASE_HDC (hdc, f);
25807
25808 SAFE_FREE ();
25809 return x_reached;
25810 }
25811
25812 /* Expand row matrix if too narrow. Don't expand if area
25813 is not present. */
25814
25815 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25816 { \
25817 if (!it->f->fonts_changed \
25818 && (it->glyph_row->glyphs[area] \
25819 < it->glyph_row->glyphs[area + 1])) \
25820 { \
25821 it->w->ncols_scale_factor++; \
25822 it->f->fonts_changed = true; \
25823 } \
25824 }
25825
25826 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25827 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25828
25829 static void
25830 append_glyph (struct it *it)
25831 {
25832 struct glyph *glyph;
25833 enum glyph_row_area area = it->area;
25834
25835 eassert (it->glyph_row);
25836 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25837
25838 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25839 if (glyph < it->glyph_row->glyphs[area + 1])
25840 {
25841 /* If the glyph row is reversed, we need to prepend the glyph
25842 rather than append it. */
25843 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25844 {
25845 struct glyph *g;
25846
25847 /* Make room for the additional glyph. */
25848 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25849 g[1] = *g;
25850 glyph = it->glyph_row->glyphs[area];
25851 }
25852 glyph->charpos = CHARPOS (it->position);
25853 glyph->object = it->object;
25854 if (it->pixel_width > 0)
25855 {
25856 eassert (it->pixel_width <= SHRT_MAX);
25857 glyph->pixel_width = it->pixel_width;
25858 glyph->padding_p = false;
25859 }
25860 else
25861 {
25862 /* Assure at least 1-pixel width. Otherwise, cursor can't
25863 be displayed correctly. */
25864 glyph->pixel_width = 1;
25865 glyph->padding_p = true;
25866 }
25867 glyph->ascent = it->ascent;
25868 glyph->descent = it->descent;
25869 glyph->voffset = it->voffset;
25870 glyph->type = CHAR_GLYPH;
25871 glyph->avoid_cursor_p = it->avoid_cursor_p;
25872 glyph->multibyte_p = it->multibyte_p;
25873 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25874 {
25875 /* In R2L rows, the left and the right box edges need to be
25876 drawn in reverse direction. */
25877 glyph->right_box_line_p = it->start_of_box_run_p;
25878 glyph->left_box_line_p = it->end_of_box_run_p;
25879 }
25880 else
25881 {
25882 glyph->left_box_line_p = it->start_of_box_run_p;
25883 glyph->right_box_line_p = it->end_of_box_run_p;
25884 }
25885 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25886 || it->phys_descent > it->descent);
25887 glyph->glyph_not_available_p = it->glyph_not_available_p;
25888 glyph->face_id = it->face_id;
25889 glyph->u.ch = it->char_to_display;
25890 glyph->slice.img = null_glyph_slice;
25891 glyph->font_type = FONT_TYPE_UNKNOWN;
25892 if (it->bidi_p)
25893 {
25894 glyph->resolved_level = it->bidi_it.resolved_level;
25895 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25896 glyph->bidi_type = it->bidi_it.type;
25897 }
25898 else
25899 {
25900 glyph->resolved_level = 0;
25901 glyph->bidi_type = UNKNOWN_BT;
25902 }
25903 ++it->glyph_row->used[area];
25904 }
25905 else
25906 IT_EXPAND_MATRIX_WIDTH (it, area);
25907 }
25908
25909 /* Store one glyph for the composition IT->cmp_it.id in
25910 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25911 non-null. */
25912
25913 static void
25914 append_composite_glyph (struct it *it)
25915 {
25916 struct glyph *glyph;
25917 enum glyph_row_area area = it->area;
25918
25919 eassert (it->glyph_row);
25920
25921 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25922 if (glyph < it->glyph_row->glyphs[area + 1])
25923 {
25924 /* If the glyph row is reversed, we need to prepend the glyph
25925 rather than append it. */
25926 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25927 {
25928 struct glyph *g;
25929
25930 /* Make room for the new glyph. */
25931 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25932 g[1] = *g;
25933 glyph = it->glyph_row->glyphs[it->area];
25934 }
25935 glyph->charpos = it->cmp_it.charpos;
25936 glyph->object = it->object;
25937 eassert (it->pixel_width <= SHRT_MAX);
25938 glyph->pixel_width = it->pixel_width;
25939 glyph->ascent = it->ascent;
25940 glyph->descent = it->descent;
25941 glyph->voffset = it->voffset;
25942 glyph->type = COMPOSITE_GLYPH;
25943 if (it->cmp_it.ch < 0)
25944 {
25945 glyph->u.cmp.automatic = false;
25946 glyph->u.cmp.id = it->cmp_it.id;
25947 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25948 }
25949 else
25950 {
25951 glyph->u.cmp.automatic = true;
25952 glyph->u.cmp.id = it->cmp_it.id;
25953 glyph->slice.cmp.from = it->cmp_it.from;
25954 glyph->slice.cmp.to = it->cmp_it.to - 1;
25955 }
25956 glyph->avoid_cursor_p = it->avoid_cursor_p;
25957 glyph->multibyte_p = it->multibyte_p;
25958 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25959 {
25960 /* In R2L rows, the left and the right box edges need to be
25961 drawn in reverse direction. */
25962 glyph->right_box_line_p = it->start_of_box_run_p;
25963 glyph->left_box_line_p = it->end_of_box_run_p;
25964 }
25965 else
25966 {
25967 glyph->left_box_line_p = it->start_of_box_run_p;
25968 glyph->right_box_line_p = it->end_of_box_run_p;
25969 }
25970 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25971 || it->phys_descent > it->descent);
25972 glyph->padding_p = false;
25973 glyph->glyph_not_available_p = false;
25974 glyph->face_id = it->face_id;
25975 glyph->font_type = FONT_TYPE_UNKNOWN;
25976 if (it->bidi_p)
25977 {
25978 glyph->resolved_level = it->bidi_it.resolved_level;
25979 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25980 glyph->bidi_type = it->bidi_it.type;
25981 }
25982 ++it->glyph_row->used[area];
25983 }
25984 else
25985 IT_EXPAND_MATRIX_WIDTH (it, area);
25986 }
25987
25988
25989 /* Change IT->ascent and IT->height according to the setting of
25990 IT->voffset. */
25991
25992 static void
25993 take_vertical_position_into_account (struct it *it)
25994 {
25995 if (it->voffset)
25996 {
25997 if (it->voffset < 0)
25998 /* Increase the ascent so that we can display the text higher
25999 in the line. */
26000 it->ascent -= it->voffset;
26001 else
26002 /* Increase the descent so that we can display the text lower
26003 in the line. */
26004 it->descent += it->voffset;
26005 }
26006 }
26007
26008
26009 /* Produce glyphs/get display metrics for the image IT is loaded with.
26010 See the description of struct display_iterator in dispextern.h for
26011 an overview of struct display_iterator. */
26012
26013 static void
26014 produce_image_glyph (struct it *it)
26015 {
26016 struct image *img;
26017 struct face *face;
26018 int glyph_ascent, crop;
26019 struct glyph_slice slice;
26020
26021 eassert (it->what == IT_IMAGE);
26022
26023 face = FACE_FROM_ID (it->f, it->face_id);
26024 /* Make sure X resources of the face is loaded. */
26025 prepare_face_for_display (it->f, face);
26026
26027 if (it->image_id < 0)
26028 {
26029 /* Fringe bitmap. */
26030 it->ascent = it->phys_ascent = 0;
26031 it->descent = it->phys_descent = 0;
26032 it->pixel_width = 0;
26033 it->nglyphs = 0;
26034 return;
26035 }
26036
26037 img = IMAGE_FROM_ID (it->f, it->image_id);
26038 /* Make sure X resources of the image is loaded. */
26039 prepare_image_for_display (it->f, img);
26040
26041 slice.x = slice.y = 0;
26042 slice.width = img->width;
26043 slice.height = img->height;
26044
26045 if (INTEGERP (it->slice.x))
26046 slice.x = XINT (it->slice.x);
26047 else if (FLOATP (it->slice.x))
26048 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
26049
26050 if (INTEGERP (it->slice.y))
26051 slice.y = XINT (it->slice.y);
26052 else if (FLOATP (it->slice.y))
26053 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
26054
26055 if (INTEGERP (it->slice.width))
26056 slice.width = XINT (it->slice.width);
26057 else if (FLOATP (it->slice.width))
26058 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
26059
26060 if (INTEGERP (it->slice.height))
26061 slice.height = XINT (it->slice.height);
26062 else if (FLOATP (it->slice.height))
26063 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
26064
26065 if (slice.x >= img->width)
26066 slice.x = img->width;
26067 if (slice.y >= img->height)
26068 slice.y = img->height;
26069 if (slice.x + slice.width >= img->width)
26070 slice.width = img->width - slice.x;
26071 if (slice.y + slice.height > img->height)
26072 slice.height = img->height - slice.y;
26073
26074 if (slice.width == 0 || slice.height == 0)
26075 return;
26076
26077 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
26078
26079 it->descent = slice.height - glyph_ascent;
26080 if (slice.y == 0)
26081 it->descent += img->vmargin;
26082 if (slice.y + slice.height == img->height)
26083 it->descent += img->vmargin;
26084 it->phys_descent = it->descent;
26085
26086 it->pixel_width = slice.width;
26087 if (slice.x == 0)
26088 it->pixel_width += img->hmargin;
26089 if (slice.x + slice.width == img->width)
26090 it->pixel_width += img->hmargin;
26091
26092 /* It's quite possible for images to have an ascent greater than
26093 their height, so don't get confused in that case. */
26094 if (it->descent < 0)
26095 it->descent = 0;
26096
26097 it->nglyphs = 1;
26098
26099 if (face->box != FACE_NO_BOX)
26100 {
26101 if (face->box_line_width > 0)
26102 {
26103 if (slice.y == 0)
26104 it->ascent += face->box_line_width;
26105 if (slice.y + slice.height == img->height)
26106 it->descent += face->box_line_width;
26107 }
26108
26109 if (it->start_of_box_run_p && slice.x == 0)
26110 it->pixel_width += eabs (face->box_line_width);
26111 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
26112 it->pixel_width += eabs (face->box_line_width);
26113 }
26114
26115 take_vertical_position_into_account (it);
26116
26117 /* Automatically crop wide image glyphs at right edge so we can
26118 draw the cursor on same display row. */
26119 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
26120 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26121 {
26122 it->pixel_width -= crop;
26123 slice.width -= crop;
26124 }
26125
26126 if (it->glyph_row)
26127 {
26128 struct glyph *glyph;
26129 enum glyph_row_area area = it->area;
26130
26131 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26132 if (it->glyph_row->reversed_p)
26133 {
26134 struct glyph *g;
26135
26136 /* Make room for the new glyph. */
26137 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26138 g[1] = *g;
26139 glyph = it->glyph_row->glyphs[it->area];
26140 }
26141 if (glyph < it->glyph_row->glyphs[area + 1])
26142 {
26143 glyph->charpos = CHARPOS (it->position);
26144 glyph->object = it->object;
26145 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26146 glyph->ascent = glyph_ascent;
26147 glyph->descent = it->descent;
26148 glyph->voffset = it->voffset;
26149 glyph->type = IMAGE_GLYPH;
26150 glyph->avoid_cursor_p = it->avoid_cursor_p;
26151 glyph->multibyte_p = it->multibyte_p;
26152 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26153 {
26154 /* In R2L rows, the left and the right box edges need to be
26155 drawn in reverse direction. */
26156 glyph->right_box_line_p = it->start_of_box_run_p;
26157 glyph->left_box_line_p = it->end_of_box_run_p;
26158 }
26159 else
26160 {
26161 glyph->left_box_line_p = it->start_of_box_run_p;
26162 glyph->right_box_line_p = it->end_of_box_run_p;
26163 }
26164 glyph->overlaps_vertically_p = false;
26165 glyph->padding_p = false;
26166 glyph->glyph_not_available_p = false;
26167 glyph->face_id = it->face_id;
26168 glyph->u.img_id = img->id;
26169 glyph->slice.img = slice;
26170 glyph->font_type = FONT_TYPE_UNKNOWN;
26171 if (it->bidi_p)
26172 {
26173 glyph->resolved_level = it->bidi_it.resolved_level;
26174 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26175 glyph->bidi_type = it->bidi_it.type;
26176 }
26177 ++it->glyph_row->used[area];
26178 }
26179 else
26180 IT_EXPAND_MATRIX_WIDTH (it, area);
26181 }
26182 }
26183
26184 static void
26185 produce_xwidget_glyph (struct it *it)
26186 {
26187 #ifdef HAVE_XWIDGETS
26188 struct xwidget *xw;
26189 int glyph_ascent, crop;
26190 eassert (it->what == IT_XWIDGET);
26191
26192 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26193 /* Make sure X resources of the face is loaded. */
26194 prepare_face_for_display (it->f, face);
26195
26196 xw = it->xwidget;
26197 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
26198 it->descent = xw->height/2;
26199 it->phys_descent = it->descent;
26200 it->pixel_width = xw->width;
26201 /* It's quite possible for images to have an ascent greater than
26202 their height, so don't get confused in that case. */
26203 if (it->descent < 0)
26204 it->descent = 0;
26205
26206 it->nglyphs = 1;
26207
26208 if (face->box != FACE_NO_BOX)
26209 {
26210 if (face->box_line_width > 0)
26211 {
26212 it->ascent += face->box_line_width;
26213 it->descent += face->box_line_width;
26214 }
26215
26216 if (it->start_of_box_run_p)
26217 it->pixel_width += eabs (face->box_line_width);
26218 it->pixel_width += eabs (face->box_line_width);
26219 }
26220
26221 take_vertical_position_into_account (it);
26222
26223 /* Automatically crop wide image glyphs at right edge so we can
26224 draw the cursor on same display row. */
26225 crop = it->pixel_width - (it->last_visible_x - it->current_x);
26226 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26227 it->pixel_width -= crop;
26228
26229 if (it->glyph_row)
26230 {
26231 enum glyph_row_area area = it->area;
26232 struct glyph *glyph
26233 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26234
26235 if (it->glyph_row->reversed_p)
26236 {
26237 struct glyph *g;
26238
26239 /* Make room for the new glyph. */
26240 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26241 g[1] = *g;
26242 glyph = it->glyph_row->glyphs[it->area];
26243 }
26244 if (glyph < it->glyph_row->glyphs[area + 1])
26245 {
26246 glyph->charpos = CHARPOS (it->position);
26247 glyph->object = it->object;
26248 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26249 glyph->ascent = glyph_ascent;
26250 glyph->descent = it->descent;
26251 glyph->voffset = it->voffset;
26252 glyph->type = XWIDGET_GLYPH;
26253 glyph->avoid_cursor_p = it->avoid_cursor_p;
26254 glyph->multibyte_p = it->multibyte_p;
26255 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26256 {
26257 /* In R2L rows, the left and the right box edges need to be
26258 drawn in reverse direction. */
26259 glyph->right_box_line_p = it->start_of_box_run_p;
26260 glyph->left_box_line_p = it->end_of_box_run_p;
26261 }
26262 else
26263 {
26264 glyph->left_box_line_p = it->start_of_box_run_p;
26265 glyph->right_box_line_p = it->end_of_box_run_p;
26266 }
26267 glyph->overlaps_vertically_p = 0;
26268 glyph->padding_p = 0;
26269 glyph->glyph_not_available_p = 0;
26270 glyph->face_id = it->face_id;
26271 glyph->u.xwidget = it->xwidget;
26272 glyph->font_type = FONT_TYPE_UNKNOWN;
26273 if (it->bidi_p)
26274 {
26275 glyph->resolved_level = it->bidi_it.resolved_level;
26276 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26277 glyph->bidi_type = it->bidi_it.type;
26278 }
26279 ++it->glyph_row->used[area];
26280 }
26281 else
26282 IT_EXPAND_MATRIX_WIDTH (it, area);
26283 }
26284 #endif
26285 }
26286
26287 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
26288 of the glyph, WIDTH and HEIGHT are the width and height of the
26289 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
26290
26291 static void
26292 append_stretch_glyph (struct it *it, Lisp_Object object,
26293 int width, int height, int ascent)
26294 {
26295 struct glyph *glyph;
26296 enum glyph_row_area area = it->area;
26297
26298 eassert (ascent >= 0 && ascent <= height);
26299
26300 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26301 if (glyph < it->glyph_row->glyphs[area + 1])
26302 {
26303 /* If the glyph row is reversed, we need to prepend the glyph
26304 rather than append it. */
26305 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26306 {
26307 struct glyph *g;
26308
26309 /* Make room for the additional glyph. */
26310 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26311 g[1] = *g;
26312 glyph = it->glyph_row->glyphs[area];
26313
26314 /* Decrease the width of the first glyph of the row that
26315 begins before first_visible_x (e.g., due to hscroll).
26316 This is so the overall width of the row becomes smaller
26317 by the scroll amount, and the stretch glyph appended by
26318 extend_face_to_end_of_line will be wider, to shift the
26319 row glyphs to the right. (In L2R rows, the corresponding
26320 left-shift effect is accomplished by setting row->x to a
26321 negative value, which won't work with R2L rows.)
26322
26323 This must leave us with a positive value of WIDTH, since
26324 otherwise the call to move_it_in_display_line_to at the
26325 beginning of display_line would have got past the entire
26326 first glyph, and then it->current_x would have been
26327 greater or equal to it->first_visible_x. */
26328 if (it->current_x < it->first_visible_x)
26329 width -= it->first_visible_x - it->current_x;
26330 eassert (width > 0);
26331 }
26332 glyph->charpos = CHARPOS (it->position);
26333 glyph->object = object;
26334 /* FIXME: It would be better to use TYPE_MAX here, but
26335 __typeof__ is not portable enough... */
26336 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
26337 glyph->ascent = ascent;
26338 glyph->descent = height - ascent;
26339 glyph->voffset = it->voffset;
26340 glyph->type = STRETCH_GLYPH;
26341 glyph->avoid_cursor_p = it->avoid_cursor_p;
26342 glyph->multibyte_p = it->multibyte_p;
26343 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26344 {
26345 /* In R2L rows, the left and the right box edges need to be
26346 drawn in reverse direction. */
26347 glyph->right_box_line_p = it->start_of_box_run_p;
26348 glyph->left_box_line_p = it->end_of_box_run_p;
26349 }
26350 else
26351 {
26352 glyph->left_box_line_p = it->start_of_box_run_p;
26353 glyph->right_box_line_p = it->end_of_box_run_p;
26354 }
26355 glyph->overlaps_vertically_p = false;
26356 glyph->padding_p = false;
26357 glyph->glyph_not_available_p = false;
26358 glyph->face_id = it->face_id;
26359 glyph->u.stretch.ascent = ascent;
26360 glyph->u.stretch.height = height;
26361 glyph->slice.img = null_glyph_slice;
26362 glyph->font_type = FONT_TYPE_UNKNOWN;
26363 if (it->bidi_p)
26364 {
26365 glyph->resolved_level = it->bidi_it.resolved_level;
26366 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26367 glyph->bidi_type = it->bidi_it.type;
26368 }
26369 else
26370 {
26371 glyph->resolved_level = 0;
26372 glyph->bidi_type = UNKNOWN_BT;
26373 }
26374 ++it->glyph_row->used[area];
26375 }
26376 else
26377 IT_EXPAND_MATRIX_WIDTH (it, area);
26378 }
26379
26380 #endif /* HAVE_WINDOW_SYSTEM */
26381
26382 /* Produce a stretch glyph for iterator IT. IT->object is the value
26383 of the glyph property displayed. The value must be a list
26384 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
26385 being recognized:
26386
26387 1. `:width WIDTH' specifies that the space should be WIDTH *
26388 canonical char width wide. WIDTH may be an integer or floating
26389 point number.
26390
26391 2. `:relative-width FACTOR' specifies that the width of the stretch
26392 should be computed from the width of the first character having the
26393 `glyph' property, and should be FACTOR times that width.
26394
26395 3. `:align-to HPOS' specifies that the space should be wide enough
26396 to reach HPOS, a value in canonical character units.
26397
26398 Exactly one of the above pairs must be present.
26399
26400 4. `:height HEIGHT' specifies that the height of the stretch produced
26401 should be HEIGHT, measured in canonical character units.
26402
26403 5. `:relative-height FACTOR' specifies that the height of the
26404 stretch should be FACTOR times the height of the characters having
26405 the glyph property.
26406
26407 Either none or exactly one of 4 or 5 must be present.
26408
26409 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26410 of the stretch should be used for the ascent of the stretch.
26411 ASCENT must be in the range 0 <= ASCENT <= 100. */
26412
26413 void
26414 produce_stretch_glyph (struct it *it)
26415 {
26416 /* (space :width WIDTH :height HEIGHT ...) */
26417 Lisp_Object prop, plist;
26418 int width = 0, height = 0, align_to = -1;
26419 bool zero_width_ok_p = false;
26420 double tem;
26421 struct font *font = NULL;
26422
26423 #ifdef HAVE_WINDOW_SYSTEM
26424 int ascent = 0;
26425 bool zero_height_ok_p = false;
26426
26427 if (FRAME_WINDOW_P (it->f))
26428 {
26429 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26430 font = face->font ? face->font : FRAME_FONT (it->f);
26431 prepare_face_for_display (it->f, face);
26432 }
26433 #endif
26434
26435 /* List should start with `space'. */
26436 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26437 plist = XCDR (it->object);
26438
26439 /* Compute the width of the stretch. */
26440 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26441 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26442 {
26443 /* Absolute width `:width WIDTH' specified and valid. */
26444 zero_width_ok_p = true;
26445 width = (int)tem;
26446 }
26447 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26448 {
26449 /* Relative width `:relative-width FACTOR' specified and valid.
26450 Compute the width of the characters having the `glyph'
26451 property. */
26452 struct it it2;
26453 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26454
26455 it2 = *it;
26456 if (it->multibyte_p)
26457 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26458 else
26459 {
26460 it2.c = it2.char_to_display = *p, it2.len = 1;
26461 if (! ASCII_CHAR_P (it2.c))
26462 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26463 }
26464
26465 it2.glyph_row = NULL;
26466 it2.what = IT_CHARACTER;
26467 PRODUCE_GLYPHS (&it2);
26468 width = NUMVAL (prop) * it2.pixel_width;
26469 }
26470 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26471 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26472 &align_to))
26473 {
26474 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26475 align_to = (align_to < 0
26476 ? 0
26477 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26478 else if (align_to < 0)
26479 align_to = window_box_left_offset (it->w, TEXT_AREA);
26480 width = max (0, (int)tem + align_to - it->current_x);
26481 zero_width_ok_p = true;
26482 }
26483 else
26484 /* Nothing specified -> width defaults to canonical char width. */
26485 width = FRAME_COLUMN_WIDTH (it->f);
26486
26487 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26488 width = 1;
26489
26490 #ifdef HAVE_WINDOW_SYSTEM
26491 /* Compute height. */
26492 if (FRAME_WINDOW_P (it->f))
26493 {
26494 int default_height = normal_char_height (font, ' ');
26495
26496 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26497 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26498 {
26499 height = (int)tem;
26500 zero_height_ok_p = true;
26501 }
26502 else if (prop = Fplist_get (plist, QCrelative_height),
26503 NUMVAL (prop) > 0)
26504 height = default_height * NUMVAL (prop);
26505 else
26506 height = default_height;
26507
26508 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26509 height = 1;
26510
26511 /* Compute percentage of height used for ascent. If
26512 `:ascent ASCENT' is present and valid, use that. Otherwise,
26513 derive the ascent from the font in use. */
26514 if (prop = Fplist_get (plist, QCascent),
26515 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26516 ascent = height * NUMVAL (prop) / 100.0;
26517 else if (!NILP (prop)
26518 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26519 ascent = min (max (0, (int)tem), height);
26520 else
26521 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26522 }
26523 else
26524 #endif /* HAVE_WINDOW_SYSTEM */
26525 height = 1;
26526
26527 if (width > 0 && it->line_wrap != TRUNCATE
26528 && it->current_x + width > it->last_visible_x)
26529 {
26530 width = it->last_visible_x - it->current_x;
26531 #ifdef HAVE_WINDOW_SYSTEM
26532 /* Subtract one more pixel from the stretch width, but only on
26533 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26534 width -= FRAME_WINDOW_P (it->f);
26535 #endif
26536 }
26537
26538 if (width > 0 && height > 0 && it->glyph_row)
26539 {
26540 Lisp_Object o_object = it->object;
26541 Lisp_Object object = it->stack[it->sp - 1].string;
26542 int n = width;
26543
26544 if (!STRINGP (object))
26545 object = it->w->contents;
26546 #ifdef HAVE_WINDOW_SYSTEM
26547 if (FRAME_WINDOW_P (it->f))
26548 append_stretch_glyph (it, object, width, height, ascent);
26549 else
26550 #endif
26551 {
26552 it->object = object;
26553 it->char_to_display = ' ';
26554 it->pixel_width = it->len = 1;
26555 while (n--)
26556 tty_append_glyph (it);
26557 it->object = o_object;
26558 }
26559 }
26560
26561 it->pixel_width = width;
26562 #ifdef HAVE_WINDOW_SYSTEM
26563 if (FRAME_WINDOW_P (it->f))
26564 {
26565 it->ascent = it->phys_ascent = ascent;
26566 it->descent = it->phys_descent = height - it->ascent;
26567 it->nglyphs = width > 0 && height > 0;
26568 take_vertical_position_into_account (it);
26569 }
26570 else
26571 #endif
26572 it->nglyphs = width;
26573 }
26574
26575 /* Get information about special display element WHAT in an
26576 environment described by IT. WHAT is one of IT_TRUNCATION or
26577 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26578 non-null glyph_row member. This function ensures that fields like
26579 face_id, c, len of IT are left untouched. */
26580
26581 static void
26582 produce_special_glyphs (struct it *it, enum display_element_type what)
26583 {
26584 struct it temp_it;
26585 Lisp_Object gc;
26586 GLYPH glyph;
26587
26588 temp_it = *it;
26589 temp_it.object = Qnil;
26590 memset (&temp_it.current, 0, sizeof temp_it.current);
26591
26592 if (what == IT_CONTINUATION)
26593 {
26594 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26595 if (it->bidi_it.paragraph_dir == R2L)
26596 SET_GLYPH_FROM_CHAR (glyph, '/');
26597 else
26598 SET_GLYPH_FROM_CHAR (glyph, '\\');
26599 if (it->dp
26600 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26601 {
26602 /* FIXME: Should we mirror GC for R2L lines? */
26603 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26604 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26605 }
26606 }
26607 else if (what == IT_TRUNCATION)
26608 {
26609 /* Truncation glyph. */
26610 SET_GLYPH_FROM_CHAR (glyph, '$');
26611 if (it->dp
26612 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26613 {
26614 /* FIXME: Should we mirror GC for R2L lines? */
26615 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26616 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26617 }
26618 }
26619 else
26620 emacs_abort ();
26621
26622 #ifdef HAVE_WINDOW_SYSTEM
26623 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26624 is turned off, we precede the truncation/continuation glyphs by a
26625 stretch glyph whose width is computed such that these special
26626 glyphs are aligned at the window margin, even when very different
26627 fonts are used in different glyph rows. */
26628 if (FRAME_WINDOW_P (temp_it.f)
26629 /* init_iterator calls this with it->glyph_row == NULL, and it
26630 wants only the pixel width of the truncation/continuation
26631 glyphs. */
26632 && temp_it.glyph_row
26633 /* insert_left_trunc_glyphs calls us at the beginning of the
26634 row, and it has its own calculation of the stretch glyph
26635 width. */
26636 && temp_it.glyph_row->used[TEXT_AREA] > 0
26637 && (temp_it.glyph_row->reversed_p
26638 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26639 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26640 {
26641 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26642
26643 if (stretch_width > 0)
26644 {
26645 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26646 struct font *font =
26647 face->font ? face->font : FRAME_FONT (temp_it.f);
26648 int stretch_ascent =
26649 (((temp_it.ascent + temp_it.descent)
26650 * FONT_BASE (font)) / FONT_HEIGHT (font));
26651
26652 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26653 temp_it.ascent + temp_it.descent,
26654 stretch_ascent);
26655 }
26656 }
26657 #endif
26658
26659 temp_it.dp = NULL;
26660 temp_it.what = IT_CHARACTER;
26661 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26662 temp_it.face_id = GLYPH_FACE (glyph);
26663 temp_it.len = CHAR_BYTES (temp_it.c);
26664
26665 PRODUCE_GLYPHS (&temp_it);
26666 it->pixel_width = temp_it.pixel_width;
26667 it->nglyphs = temp_it.nglyphs;
26668 }
26669
26670 #ifdef HAVE_WINDOW_SYSTEM
26671
26672 /* Calculate line-height and line-spacing properties.
26673 An integer value specifies explicit pixel value.
26674 A float value specifies relative value to current face height.
26675 A cons (float . face-name) specifies relative value to
26676 height of specified face font.
26677
26678 Returns height in pixels, or nil. */
26679
26680 static Lisp_Object
26681 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26682 int boff, bool override)
26683 {
26684 Lisp_Object face_name = Qnil;
26685 int ascent, descent, height;
26686
26687 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26688 return val;
26689
26690 if (CONSP (val))
26691 {
26692 face_name = XCAR (val);
26693 val = XCDR (val);
26694 if (!NUMBERP (val))
26695 val = make_number (1);
26696 if (NILP (face_name))
26697 {
26698 height = it->ascent + it->descent;
26699 goto scale;
26700 }
26701 }
26702
26703 if (NILP (face_name))
26704 {
26705 font = FRAME_FONT (it->f);
26706 boff = FRAME_BASELINE_OFFSET (it->f);
26707 }
26708 else if (EQ (face_name, Qt))
26709 {
26710 override = false;
26711 }
26712 else
26713 {
26714 int face_id;
26715 struct face *face;
26716
26717 face_id = lookup_named_face (it->f, face_name, false);
26718 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
26719 if (face == NULL || ((font = face->font) == NULL))
26720 return make_number (-1);
26721 boff = font->baseline_offset;
26722 if (font->vertical_centering)
26723 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26724 }
26725
26726 normal_char_ascent_descent (font, -1, &ascent, &descent);
26727
26728 if (override)
26729 {
26730 it->override_ascent = ascent;
26731 it->override_descent = descent;
26732 it->override_boff = boff;
26733 }
26734
26735 height = ascent + descent;
26736
26737 scale:
26738 if (FLOATP (val))
26739 height = (int)(XFLOAT_DATA (val) * height);
26740 else if (INTEGERP (val))
26741 height *= XINT (val);
26742
26743 return make_number (height);
26744 }
26745
26746
26747 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26748 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26749 and only if this is for a character for which no font was found.
26750
26751 If the display method (it->glyphless_method) is
26752 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26753 length of the acronym or the hexadecimal string, UPPER_XOFF and
26754 UPPER_YOFF are pixel offsets for the upper part of the string,
26755 LOWER_XOFF and LOWER_YOFF are for the lower part.
26756
26757 For the other display methods, LEN through LOWER_YOFF are zero. */
26758
26759 static void
26760 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26761 short upper_xoff, short upper_yoff,
26762 short lower_xoff, short lower_yoff)
26763 {
26764 struct glyph *glyph;
26765 enum glyph_row_area area = it->area;
26766
26767 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26768 if (glyph < it->glyph_row->glyphs[area + 1])
26769 {
26770 /* If the glyph row is reversed, we need to prepend the glyph
26771 rather than append it. */
26772 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26773 {
26774 struct glyph *g;
26775
26776 /* Make room for the additional glyph. */
26777 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26778 g[1] = *g;
26779 glyph = it->glyph_row->glyphs[area];
26780 }
26781 glyph->charpos = CHARPOS (it->position);
26782 glyph->object = it->object;
26783 eassert (it->pixel_width <= SHRT_MAX);
26784 glyph->pixel_width = it->pixel_width;
26785 glyph->ascent = it->ascent;
26786 glyph->descent = it->descent;
26787 glyph->voffset = it->voffset;
26788 glyph->type = GLYPHLESS_GLYPH;
26789 glyph->u.glyphless.method = it->glyphless_method;
26790 glyph->u.glyphless.for_no_font = for_no_font;
26791 glyph->u.glyphless.len = len;
26792 glyph->u.glyphless.ch = it->c;
26793 glyph->slice.glyphless.upper_xoff = upper_xoff;
26794 glyph->slice.glyphless.upper_yoff = upper_yoff;
26795 glyph->slice.glyphless.lower_xoff = lower_xoff;
26796 glyph->slice.glyphless.lower_yoff = lower_yoff;
26797 glyph->avoid_cursor_p = it->avoid_cursor_p;
26798 glyph->multibyte_p = it->multibyte_p;
26799 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26800 {
26801 /* In R2L rows, the left and the right box edges need to be
26802 drawn in reverse direction. */
26803 glyph->right_box_line_p = it->start_of_box_run_p;
26804 glyph->left_box_line_p = it->end_of_box_run_p;
26805 }
26806 else
26807 {
26808 glyph->left_box_line_p = it->start_of_box_run_p;
26809 glyph->right_box_line_p = it->end_of_box_run_p;
26810 }
26811 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26812 || it->phys_descent > it->descent);
26813 glyph->padding_p = false;
26814 glyph->glyph_not_available_p = false;
26815 glyph->face_id = face_id;
26816 glyph->font_type = FONT_TYPE_UNKNOWN;
26817 if (it->bidi_p)
26818 {
26819 glyph->resolved_level = it->bidi_it.resolved_level;
26820 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26821 glyph->bidi_type = it->bidi_it.type;
26822 }
26823 ++it->glyph_row->used[area];
26824 }
26825 else
26826 IT_EXPAND_MATRIX_WIDTH (it, area);
26827 }
26828
26829
26830 /* Produce a glyph for a glyphless character for iterator IT.
26831 IT->glyphless_method specifies which method to use for displaying
26832 the character. See the description of enum
26833 glyphless_display_method in dispextern.h for the detail.
26834
26835 FOR_NO_FONT is true if and only if this is for a character for
26836 which no font was found. ACRONYM, if non-nil, is an acronym string
26837 for the character. */
26838
26839 static void
26840 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26841 {
26842 int face_id;
26843 struct face *face;
26844 struct font *font;
26845 int base_width, base_height, width, height;
26846 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26847 int len;
26848
26849 /* Get the metrics of the base font. We always refer to the current
26850 ASCII face. */
26851 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26852 font = face->font ? face->font : FRAME_FONT (it->f);
26853 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
26854 it->ascent += font->baseline_offset;
26855 it->descent -= font->baseline_offset;
26856 base_height = it->ascent + it->descent;
26857 base_width = font->average_width;
26858
26859 face_id = merge_glyphless_glyph_face (it);
26860
26861 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26862 {
26863 it->pixel_width = THIN_SPACE_WIDTH;
26864 len = 0;
26865 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26866 }
26867 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26868 {
26869 width = CHAR_WIDTH (it->c);
26870 if (width == 0)
26871 width = 1;
26872 else if (width > 4)
26873 width = 4;
26874 it->pixel_width = base_width * width;
26875 len = 0;
26876 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26877 }
26878 else
26879 {
26880 char buf[7];
26881 const char *str;
26882 unsigned int code[6];
26883 int upper_len;
26884 int ascent, descent;
26885 struct font_metrics metrics_upper, metrics_lower;
26886
26887 face = FACE_FROM_ID (it->f, face_id);
26888 font = face->font ? face->font : FRAME_FONT (it->f);
26889 prepare_face_for_display (it->f, face);
26890
26891 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26892 {
26893 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26894 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26895 if (CONSP (acronym))
26896 acronym = XCAR (acronym);
26897 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26898 }
26899 else
26900 {
26901 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26902 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
26903 str = buf;
26904 }
26905 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26906 code[len] = font->driver->encode_char (font, str[len]);
26907 upper_len = (len + 1) / 2;
26908 font->driver->text_extents (font, code, upper_len,
26909 &metrics_upper);
26910 font->driver->text_extents (font, code + upper_len, len - upper_len,
26911 &metrics_lower);
26912
26913
26914
26915 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26916 width = max (metrics_upper.width, metrics_lower.width) + 4;
26917 upper_xoff = upper_yoff = 2; /* the typical case */
26918 if (base_width >= width)
26919 {
26920 /* Align the upper to the left, the lower to the right. */
26921 it->pixel_width = base_width;
26922 lower_xoff = base_width - 2 - metrics_lower.width;
26923 }
26924 else
26925 {
26926 /* Center the shorter one. */
26927 it->pixel_width = width;
26928 if (metrics_upper.width >= metrics_lower.width)
26929 lower_xoff = (width - metrics_lower.width) / 2;
26930 else
26931 {
26932 /* FIXME: This code doesn't look right. It formerly was
26933 missing the "lower_xoff = 0;", which couldn't have
26934 been right since it left lower_xoff uninitialized. */
26935 lower_xoff = 0;
26936 upper_xoff = (width - metrics_upper.width) / 2;
26937 }
26938 }
26939
26940 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26941 top, bottom, and between upper and lower strings. */
26942 height = (metrics_upper.ascent + metrics_upper.descent
26943 + metrics_lower.ascent + metrics_lower.descent) + 5;
26944 /* Center vertically.
26945 H:base_height, D:base_descent
26946 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26947
26948 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26949 descent = D - H/2 + h/2;
26950 lower_yoff = descent - 2 - ld;
26951 upper_yoff = lower_yoff - la - 1 - ud; */
26952 ascent = - (it->descent - (base_height + height + 1) / 2);
26953 descent = it->descent - (base_height - height) / 2;
26954 lower_yoff = descent - 2 - metrics_lower.descent;
26955 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26956 - metrics_upper.descent);
26957 /* Don't make the height shorter than the base height. */
26958 if (height > base_height)
26959 {
26960 it->ascent = ascent;
26961 it->descent = descent;
26962 }
26963 }
26964
26965 it->phys_ascent = it->ascent;
26966 it->phys_descent = it->descent;
26967 if (it->glyph_row)
26968 append_glyphless_glyph (it, face_id, for_no_font, len,
26969 upper_xoff, upper_yoff,
26970 lower_xoff, lower_yoff);
26971 it->nglyphs = 1;
26972 take_vertical_position_into_account (it);
26973 }
26974
26975
26976 /* RIF:
26977 Produce glyphs/get display metrics for the display element IT is
26978 loaded with. See the description of struct it in dispextern.h
26979 for an overview of struct it. */
26980
26981 void
26982 x_produce_glyphs (struct it *it)
26983 {
26984 int extra_line_spacing = it->extra_line_spacing;
26985
26986 it->glyph_not_available_p = false;
26987
26988 if (it->what == IT_CHARACTER)
26989 {
26990 XChar2b char2b;
26991 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26992 struct font *font = face->font;
26993 struct font_metrics *pcm = NULL;
26994 int boff; /* Baseline offset. */
26995
26996 if (font == NULL)
26997 {
26998 /* When no suitable font is found, display this character by
26999 the method specified in the first extra slot of
27000 Vglyphless_char_display. */
27001 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
27002
27003 eassert (it->what == IT_GLYPHLESS);
27004 produce_glyphless_glyph (it, true,
27005 STRINGP (acronym) ? acronym : Qnil);
27006 goto done;
27007 }
27008
27009 boff = font->baseline_offset;
27010 if (font->vertical_centering)
27011 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27012
27013 if (it->char_to_display != '\n' && it->char_to_display != '\t')
27014 {
27015 it->nglyphs = 1;
27016
27017 if (it->override_ascent >= 0)
27018 {
27019 it->ascent = it->override_ascent;
27020 it->descent = it->override_descent;
27021 boff = it->override_boff;
27022 }
27023 else
27024 {
27025 it->ascent = FONT_BASE (font) + boff;
27026 it->descent = FONT_DESCENT (font) - boff;
27027 }
27028
27029 if (get_char_glyph_code (it->char_to_display, font, &char2b))
27030 {
27031 pcm = get_per_char_metric (font, &char2b);
27032 if (pcm->width == 0
27033 && pcm->rbearing == 0 && pcm->lbearing == 0)
27034 pcm = NULL;
27035 }
27036
27037 if (pcm)
27038 {
27039 it->phys_ascent = pcm->ascent + boff;
27040 it->phys_descent = pcm->descent - boff;
27041 it->pixel_width = pcm->width;
27042 /* Don't use font-global values for ascent and descent
27043 if they result in an exceedingly large line height. */
27044 if (it->override_ascent < 0)
27045 {
27046 if (FONT_TOO_HIGH (font))
27047 {
27048 it->ascent = it->phys_ascent;
27049 it->descent = it->phys_descent;
27050 /* These limitations are enforced by an
27051 assertion near the end of this function. */
27052 if (it->ascent < 0)
27053 it->ascent = 0;
27054 if (it->descent < 0)
27055 it->descent = 0;
27056 }
27057 }
27058 }
27059 else
27060 {
27061 it->glyph_not_available_p = true;
27062 it->phys_ascent = it->ascent;
27063 it->phys_descent = it->descent;
27064 it->pixel_width = font->space_width;
27065 }
27066
27067 if (it->constrain_row_ascent_descent_p)
27068 {
27069 if (it->descent > it->max_descent)
27070 {
27071 it->ascent += it->descent - it->max_descent;
27072 it->descent = it->max_descent;
27073 }
27074 if (it->ascent > it->max_ascent)
27075 {
27076 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27077 it->ascent = it->max_ascent;
27078 }
27079 it->phys_ascent = min (it->phys_ascent, it->ascent);
27080 it->phys_descent = min (it->phys_descent, it->descent);
27081 extra_line_spacing = 0;
27082 }
27083
27084 /* If this is a space inside a region of text with
27085 `space-width' property, change its width. */
27086 bool stretched_p
27087 = it->char_to_display == ' ' && !NILP (it->space_width);
27088 if (stretched_p)
27089 it->pixel_width *= XFLOATINT (it->space_width);
27090
27091 /* If face has a box, add the box thickness to the character
27092 height. If character has a box line to the left and/or
27093 right, add the box line width to the character's width. */
27094 if (face->box != FACE_NO_BOX)
27095 {
27096 int thick = face->box_line_width;
27097
27098 if (thick > 0)
27099 {
27100 it->ascent += thick;
27101 it->descent += thick;
27102 }
27103 else
27104 thick = -thick;
27105
27106 if (it->start_of_box_run_p)
27107 it->pixel_width += thick;
27108 if (it->end_of_box_run_p)
27109 it->pixel_width += thick;
27110 }
27111
27112 /* If face has an overline, add the height of the overline
27113 (1 pixel) and a 1 pixel margin to the character height. */
27114 if (face->overline_p)
27115 it->ascent += overline_margin;
27116
27117 if (it->constrain_row_ascent_descent_p)
27118 {
27119 if (it->ascent > it->max_ascent)
27120 it->ascent = it->max_ascent;
27121 if (it->descent > it->max_descent)
27122 it->descent = it->max_descent;
27123 }
27124
27125 take_vertical_position_into_account (it);
27126
27127 /* If we have to actually produce glyphs, do it. */
27128 if (it->glyph_row)
27129 {
27130 if (stretched_p)
27131 {
27132 /* Translate a space with a `space-width' property
27133 into a stretch glyph. */
27134 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
27135 / FONT_HEIGHT (font));
27136 append_stretch_glyph (it, it->object, it->pixel_width,
27137 it->ascent + it->descent, ascent);
27138 }
27139 else
27140 append_glyph (it);
27141
27142 /* If characters with lbearing or rbearing are displayed
27143 in this line, record that fact in a flag of the
27144 glyph row. This is used to optimize X output code. */
27145 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
27146 it->glyph_row->contains_overlapping_glyphs_p = true;
27147 }
27148 if (! stretched_p && it->pixel_width == 0)
27149 /* We assure that all visible glyphs have at least 1-pixel
27150 width. */
27151 it->pixel_width = 1;
27152 }
27153 else if (it->char_to_display == '\n')
27154 {
27155 /* A newline has no width, but we need the height of the
27156 line. But if previous part of the line sets a height,
27157 don't increase that height. */
27158
27159 Lisp_Object height;
27160 Lisp_Object total_height = Qnil;
27161
27162 it->override_ascent = -1;
27163 it->pixel_width = 0;
27164 it->nglyphs = 0;
27165
27166 height = get_it_property (it, Qline_height);
27167 /* Split (line-height total-height) list. */
27168 if (CONSP (height)
27169 && CONSP (XCDR (height))
27170 && NILP (XCDR (XCDR (height))))
27171 {
27172 total_height = XCAR (XCDR (height));
27173 height = XCAR (height);
27174 }
27175 height = calc_line_height_property (it, height, font, boff, true);
27176
27177 if (it->override_ascent >= 0)
27178 {
27179 it->ascent = it->override_ascent;
27180 it->descent = it->override_descent;
27181 boff = it->override_boff;
27182 }
27183 else
27184 {
27185 if (FONT_TOO_HIGH (font))
27186 {
27187 it->ascent = font->pixel_size + boff - 1;
27188 it->descent = -boff + 1;
27189 if (it->descent < 0)
27190 it->descent = 0;
27191 }
27192 else
27193 {
27194 it->ascent = FONT_BASE (font) + boff;
27195 it->descent = FONT_DESCENT (font) - boff;
27196 }
27197 }
27198
27199 if (EQ (height, Qt))
27200 {
27201 if (it->descent > it->max_descent)
27202 {
27203 it->ascent += it->descent - it->max_descent;
27204 it->descent = it->max_descent;
27205 }
27206 if (it->ascent > it->max_ascent)
27207 {
27208 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27209 it->ascent = it->max_ascent;
27210 }
27211 it->phys_ascent = min (it->phys_ascent, it->ascent);
27212 it->phys_descent = min (it->phys_descent, it->descent);
27213 it->constrain_row_ascent_descent_p = true;
27214 extra_line_spacing = 0;
27215 }
27216 else
27217 {
27218 Lisp_Object spacing;
27219
27220 it->phys_ascent = it->ascent;
27221 it->phys_descent = it->descent;
27222
27223 if ((it->max_ascent > 0 || it->max_descent > 0)
27224 && face->box != FACE_NO_BOX
27225 && face->box_line_width > 0)
27226 {
27227 it->ascent += face->box_line_width;
27228 it->descent += face->box_line_width;
27229 }
27230 if (!NILP (height)
27231 && XINT (height) > it->ascent + it->descent)
27232 it->ascent = XINT (height) - it->descent;
27233
27234 if (!NILP (total_height))
27235 spacing = calc_line_height_property (it, total_height, font,
27236 boff, false);
27237 else
27238 {
27239 spacing = get_it_property (it, Qline_spacing);
27240 spacing = calc_line_height_property (it, spacing, font,
27241 boff, false);
27242 }
27243 if (INTEGERP (spacing))
27244 {
27245 extra_line_spacing = XINT (spacing);
27246 if (!NILP (total_height))
27247 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
27248 }
27249 }
27250 }
27251 else /* i.e. (it->char_to_display == '\t') */
27252 {
27253 if (font->space_width > 0)
27254 {
27255 int tab_width = it->tab_width * font->space_width;
27256 int x = it->current_x + it->continuation_lines_width;
27257 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
27258
27259 /* If the distance from the current position to the next tab
27260 stop is less than a space character width, use the
27261 tab stop after that. */
27262 if (next_tab_x - x < font->space_width)
27263 next_tab_x += tab_width;
27264
27265 it->pixel_width = next_tab_x - x;
27266 it->nglyphs = 1;
27267 if (FONT_TOO_HIGH (font))
27268 {
27269 if (get_char_glyph_code (' ', font, &char2b))
27270 {
27271 pcm = get_per_char_metric (font, &char2b);
27272 if (pcm->width == 0
27273 && pcm->rbearing == 0 && pcm->lbearing == 0)
27274 pcm = NULL;
27275 }
27276
27277 if (pcm)
27278 {
27279 it->ascent = pcm->ascent + boff;
27280 it->descent = pcm->descent - boff;
27281 }
27282 else
27283 {
27284 it->ascent = font->pixel_size + boff - 1;
27285 it->descent = -boff + 1;
27286 }
27287 if (it->ascent < 0)
27288 it->ascent = 0;
27289 if (it->descent < 0)
27290 it->descent = 0;
27291 }
27292 else
27293 {
27294 it->ascent = FONT_BASE (font) + boff;
27295 it->descent = FONT_DESCENT (font) - boff;
27296 }
27297 it->phys_ascent = it->ascent;
27298 it->phys_descent = it->descent;
27299
27300 if (it->glyph_row)
27301 {
27302 append_stretch_glyph (it, it->object, it->pixel_width,
27303 it->ascent + it->descent, it->ascent);
27304 }
27305 }
27306 else
27307 {
27308 it->pixel_width = 0;
27309 it->nglyphs = 1;
27310 }
27311 }
27312
27313 if (FONT_TOO_HIGH (font))
27314 {
27315 int font_ascent, font_descent;
27316
27317 /* For very large fonts, where we ignore the declared font
27318 dimensions, and go by per-character metrics instead,
27319 don't let the row ascent and descent values (and the row
27320 height computed from them) be smaller than the "normal"
27321 character metrics. This avoids unpleasant effects
27322 whereby lines on display would change their height
27323 depending on which characters are shown. */
27324 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27325 it->max_ascent = max (it->max_ascent, font_ascent);
27326 it->max_descent = max (it->max_descent, font_descent);
27327 }
27328 }
27329 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
27330 {
27331 /* A static composition.
27332
27333 Note: A composition is represented as one glyph in the
27334 glyph matrix. There are no padding glyphs.
27335
27336 Important note: pixel_width, ascent, and descent are the
27337 values of what is drawn by draw_glyphs (i.e. the values of
27338 the overall glyphs composed). */
27339 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27340 int boff; /* baseline offset */
27341 struct composition *cmp = composition_table[it->cmp_it.id];
27342 int glyph_len = cmp->glyph_len;
27343 struct font *font = face->font;
27344
27345 it->nglyphs = 1;
27346
27347 /* If we have not yet calculated pixel size data of glyphs of
27348 the composition for the current face font, calculate them
27349 now. Theoretically, we have to check all fonts for the
27350 glyphs, but that requires much time and memory space. So,
27351 here we check only the font of the first glyph. This may
27352 lead to incorrect display, but it's very rare, and C-l
27353 (recenter-top-bottom) can correct the display anyway. */
27354 if (! cmp->font || cmp->font != font)
27355 {
27356 /* Ascent and descent of the font of the first character
27357 of this composition (adjusted by baseline offset).
27358 Ascent and descent of overall glyphs should not be less
27359 than these, respectively. */
27360 int font_ascent, font_descent, font_height;
27361 /* Bounding box of the overall glyphs. */
27362 int leftmost, rightmost, lowest, highest;
27363 int lbearing, rbearing;
27364 int i, width, ascent, descent;
27365 int c;
27366 XChar2b char2b;
27367 struct font_metrics *pcm;
27368 ptrdiff_t pos;
27369
27370 eassume (0 < glyph_len); /* See Bug#8512. */
27371 do
27372 c = COMPOSITION_GLYPH (cmp, --glyph_len);
27373 while (c == '\t' && 0 < glyph_len);
27374
27375 bool right_padded = glyph_len < cmp->glyph_len;
27376 for (i = 0; i < glyph_len; i++)
27377 {
27378 c = COMPOSITION_GLYPH (cmp, i);
27379 if (c != '\t')
27380 break;
27381 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27382 }
27383 bool left_padded = i > 0;
27384
27385 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
27386 : IT_CHARPOS (*it));
27387 /* If no suitable font is found, use the default font. */
27388 bool font_not_found_p = font == NULL;
27389 if (font_not_found_p)
27390 {
27391 face = face->ascii_face;
27392 font = face->font;
27393 }
27394 boff = font->baseline_offset;
27395 if (font->vertical_centering)
27396 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27397 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27398 font_ascent += boff;
27399 font_descent -= boff;
27400 font_height = font_ascent + font_descent;
27401
27402 cmp->font = font;
27403
27404 pcm = NULL;
27405 if (! font_not_found_p)
27406 {
27407 get_char_face_and_encoding (it->f, c, it->face_id,
27408 &char2b, false);
27409 pcm = get_per_char_metric (font, &char2b);
27410 }
27411
27412 /* Initialize the bounding box. */
27413 if (pcm)
27414 {
27415 width = cmp->glyph_len > 0 ? pcm->width : 0;
27416 ascent = pcm->ascent;
27417 descent = pcm->descent;
27418 lbearing = pcm->lbearing;
27419 rbearing = pcm->rbearing;
27420 }
27421 else
27422 {
27423 width = cmp->glyph_len > 0 ? font->space_width : 0;
27424 ascent = FONT_BASE (font);
27425 descent = FONT_DESCENT (font);
27426 lbearing = 0;
27427 rbearing = width;
27428 }
27429
27430 rightmost = width;
27431 leftmost = 0;
27432 lowest = - descent + boff;
27433 highest = ascent + boff;
27434
27435 if (! font_not_found_p
27436 && font->default_ascent
27437 && CHAR_TABLE_P (Vuse_default_ascent)
27438 && !NILP (Faref (Vuse_default_ascent,
27439 make_number (it->char_to_display))))
27440 highest = font->default_ascent + boff;
27441
27442 /* Draw the first glyph at the normal position. It may be
27443 shifted to right later if some other glyphs are drawn
27444 at the left. */
27445 cmp->offsets[i * 2] = 0;
27446 cmp->offsets[i * 2 + 1] = boff;
27447 cmp->lbearing = lbearing;
27448 cmp->rbearing = rbearing;
27449
27450 /* Set cmp->offsets for the remaining glyphs. */
27451 for (i++; i < glyph_len; i++)
27452 {
27453 int left, right, btm, top;
27454 int ch = COMPOSITION_GLYPH (cmp, i);
27455 int face_id;
27456 struct face *this_face;
27457
27458 if (ch == '\t')
27459 ch = ' ';
27460 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27461 this_face = FACE_FROM_ID (it->f, face_id);
27462 font = this_face->font;
27463
27464 if (font == NULL)
27465 pcm = NULL;
27466 else
27467 {
27468 get_char_face_and_encoding (it->f, ch, face_id,
27469 &char2b, false);
27470 pcm = get_per_char_metric (font, &char2b);
27471 }
27472 if (! pcm)
27473 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27474 else
27475 {
27476 width = pcm->width;
27477 ascent = pcm->ascent;
27478 descent = pcm->descent;
27479 lbearing = pcm->lbearing;
27480 rbearing = pcm->rbearing;
27481 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27482 {
27483 /* Relative composition with or without
27484 alternate chars. */
27485 left = (leftmost + rightmost - width) / 2;
27486 btm = - descent + boff;
27487 if (font->relative_compose
27488 && (! CHAR_TABLE_P (Vignore_relative_composition)
27489 || NILP (Faref (Vignore_relative_composition,
27490 make_number (ch)))))
27491 {
27492
27493 if (- descent >= font->relative_compose)
27494 /* One extra pixel between two glyphs. */
27495 btm = highest + 1;
27496 else if (ascent <= 0)
27497 /* One extra pixel between two glyphs. */
27498 btm = lowest - 1 - ascent - descent;
27499 }
27500 }
27501 else
27502 {
27503 /* A composition rule is specified by an integer
27504 value that encodes global and new reference
27505 points (GREF and NREF). GREF and NREF are
27506 specified by numbers as below:
27507
27508 0---1---2 -- ascent
27509 | |
27510 | |
27511 | |
27512 9--10--11 -- center
27513 | |
27514 ---3---4---5--- baseline
27515 | |
27516 6---7---8 -- descent
27517 */
27518 int rule = COMPOSITION_RULE (cmp, i);
27519 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27520
27521 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27522 grefx = gref % 3, nrefx = nref % 3;
27523 grefy = gref / 3, nrefy = nref / 3;
27524 if (xoff)
27525 xoff = font_height * (xoff - 128) / 256;
27526 if (yoff)
27527 yoff = font_height * (yoff - 128) / 256;
27528
27529 left = (leftmost
27530 + grefx * (rightmost - leftmost) / 2
27531 - nrefx * width / 2
27532 + xoff);
27533
27534 btm = ((grefy == 0 ? highest
27535 : grefy == 1 ? 0
27536 : grefy == 2 ? lowest
27537 : (highest + lowest) / 2)
27538 - (nrefy == 0 ? ascent + descent
27539 : nrefy == 1 ? descent - boff
27540 : nrefy == 2 ? 0
27541 : (ascent + descent) / 2)
27542 + yoff);
27543 }
27544
27545 cmp->offsets[i * 2] = left;
27546 cmp->offsets[i * 2 + 1] = btm + descent;
27547
27548 /* Update the bounding box of the overall glyphs. */
27549 if (width > 0)
27550 {
27551 right = left + width;
27552 if (left < leftmost)
27553 leftmost = left;
27554 if (right > rightmost)
27555 rightmost = right;
27556 }
27557 top = btm + descent + ascent;
27558 if (top > highest)
27559 highest = top;
27560 if (btm < lowest)
27561 lowest = btm;
27562
27563 if (cmp->lbearing > left + lbearing)
27564 cmp->lbearing = left + lbearing;
27565 if (cmp->rbearing < left + rbearing)
27566 cmp->rbearing = left + rbearing;
27567 }
27568 }
27569
27570 /* If there are glyphs whose x-offsets are negative,
27571 shift all glyphs to the right and make all x-offsets
27572 non-negative. */
27573 if (leftmost < 0)
27574 {
27575 for (i = 0; i < cmp->glyph_len; i++)
27576 cmp->offsets[i * 2] -= leftmost;
27577 rightmost -= leftmost;
27578 cmp->lbearing -= leftmost;
27579 cmp->rbearing -= leftmost;
27580 }
27581
27582 if (left_padded && cmp->lbearing < 0)
27583 {
27584 for (i = 0; i < cmp->glyph_len; i++)
27585 cmp->offsets[i * 2] -= cmp->lbearing;
27586 rightmost -= cmp->lbearing;
27587 cmp->rbearing -= cmp->lbearing;
27588 cmp->lbearing = 0;
27589 }
27590 if (right_padded && rightmost < cmp->rbearing)
27591 {
27592 rightmost = cmp->rbearing;
27593 }
27594
27595 cmp->pixel_width = rightmost;
27596 cmp->ascent = highest;
27597 cmp->descent = - lowest;
27598 if (cmp->ascent < font_ascent)
27599 cmp->ascent = font_ascent;
27600 if (cmp->descent < font_descent)
27601 cmp->descent = font_descent;
27602 }
27603
27604 if (it->glyph_row
27605 && (cmp->lbearing < 0
27606 || cmp->rbearing > cmp->pixel_width))
27607 it->glyph_row->contains_overlapping_glyphs_p = true;
27608
27609 it->pixel_width = cmp->pixel_width;
27610 it->ascent = it->phys_ascent = cmp->ascent;
27611 it->descent = it->phys_descent = cmp->descent;
27612 if (face->box != FACE_NO_BOX)
27613 {
27614 int thick = face->box_line_width;
27615
27616 if (thick > 0)
27617 {
27618 it->ascent += thick;
27619 it->descent += thick;
27620 }
27621 else
27622 thick = - thick;
27623
27624 if (it->start_of_box_run_p)
27625 it->pixel_width += thick;
27626 if (it->end_of_box_run_p)
27627 it->pixel_width += thick;
27628 }
27629
27630 /* If face has an overline, add the height of the overline
27631 (1 pixel) and a 1 pixel margin to the character height. */
27632 if (face->overline_p)
27633 it->ascent += overline_margin;
27634
27635 take_vertical_position_into_account (it);
27636 if (it->ascent < 0)
27637 it->ascent = 0;
27638 if (it->descent < 0)
27639 it->descent = 0;
27640
27641 if (it->glyph_row && cmp->glyph_len > 0)
27642 append_composite_glyph (it);
27643 }
27644 else if (it->what == IT_COMPOSITION)
27645 {
27646 /* A dynamic (automatic) composition. */
27647 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27648 Lisp_Object gstring;
27649 struct font_metrics metrics;
27650
27651 it->nglyphs = 1;
27652
27653 gstring = composition_gstring_from_id (it->cmp_it.id);
27654 it->pixel_width
27655 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27656 &metrics);
27657 if (it->glyph_row
27658 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27659 it->glyph_row->contains_overlapping_glyphs_p = true;
27660 it->ascent = it->phys_ascent = metrics.ascent;
27661 it->descent = it->phys_descent = metrics.descent;
27662 if (face->box != FACE_NO_BOX)
27663 {
27664 int thick = face->box_line_width;
27665
27666 if (thick > 0)
27667 {
27668 it->ascent += thick;
27669 it->descent += thick;
27670 }
27671 else
27672 thick = - thick;
27673
27674 if (it->start_of_box_run_p)
27675 it->pixel_width += thick;
27676 if (it->end_of_box_run_p)
27677 it->pixel_width += thick;
27678 }
27679 /* If face has an overline, add the height of the overline
27680 (1 pixel) and a 1 pixel margin to the character height. */
27681 if (face->overline_p)
27682 it->ascent += overline_margin;
27683 take_vertical_position_into_account (it);
27684 if (it->ascent < 0)
27685 it->ascent = 0;
27686 if (it->descent < 0)
27687 it->descent = 0;
27688
27689 if (it->glyph_row)
27690 append_composite_glyph (it);
27691 }
27692 else if (it->what == IT_GLYPHLESS)
27693 produce_glyphless_glyph (it, false, Qnil);
27694 else if (it->what == IT_IMAGE)
27695 produce_image_glyph (it);
27696 else if (it->what == IT_STRETCH)
27697 produce_stretch_glyph (it);
27698 else if (it->what == IT_XWIDGET)
27699 produce_xwidget_glyph (it);
27700
27701 done:
27702 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27703 because this isn't true for images with `:ascent 100'. */
27704 eassert (it->ascent >= 0 && it->descent >= 0);
27705 if (it->area == TEXT_AREA)
27706 it->current_x += it->pixel_width;
27707
27708 if (extra_line_spacing > 0)
27709 {
27710 it->descent += extra_line_spacing;
27711 if (extra_line_spacing > it->max_extra_line_spacing)
27712 it->max_extra_line_spacing = extra_line_spacing;
27713 }
27714
27715 it->max_ascent = max (it->max_ascent, it->ascent);
27716 it->max_descent = max (it->max_descent, it->descent);
27717 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27718 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27719 }
27720
27721 /* EXPORT for RIF:
27722 Output LEN glyphs starting at START at the nominal cursor position.
27723 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27724 being updated, and UPDATED_AREA is the area of that row being updated. */
27725
27726 void
27727 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27728 struct glyph *start, enum glyph_row_area updated_area, int len)
27729 {
27730 int x, hpos, chpos = w->phys_cursor.hpos;
27731
27732 eassert (updated_row);
27733 /* When the window is hscrolled, cursor hpos can legitimately be out
27734 of bounds, but we draw the cursor at the corresponding window
27735 margin in that case. */
27736 if (!updated_row->reversed_p && chpos < 0)
27737 chpos = 0;
27738 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27739 chpos = updated_row->used[TEXT_AREA] - 1;
27740
27741 block_input ();
27742
27743 /* Write glyphs. */
27744
27745 hpos = start - updated_row->glyphs[updated_area];
27746 x = draw_glyphs (w, w->output_cursor.x,
27747 updated_row, updated_area,
27748 hpos, hpos + len,
27749 DRAW_NORMAL_TEXT, 0);
27750
27751 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27752 if (updated_area == TEXT_AREA
27753 && w->phys_cursor_on_p
27754 && w->phys_cursor.vpos == w->output_cursor.vpos
27755 && chpos >= hpos
27756 && chpos < hpos + len)
27757 w->phys_cursor_on_p = false;
27758
27759 unblock_input ();
27760
27761 /* Advance the output cursor. */
27762 w->output_cursor.hpos += len;
27763 w->output_cursor.x = x;
27764 }
27765
27766
27767 /* EXPORT for RIF:
27768 Insert LEN glyphs from START at the nominal cursor position. */
27769
27770 void
27771 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27772 struct glyph *start, enum glyph_row_area updated_area, int len)
27773 {
27774 struct frame *f;
27775 int line_height, shift_by_width, shifted_region_width;
27776 struct glyph_row *row;
27777 struct glyph *glyph;
27778 int frame_x, frame_y;
27779 ptrdiff_t hpos;
27780
27781 eassert (updated_row);
27782 block_input ();
27783 f = XFRAME (WINDOW_FRAME (w));
27784
27785 /* Get the height of the line we are in. */
27786 row = updated_row;
27787 line_height = row->height;
27788
27789 /* Get the width of the glyphs to insert. */
27790 shift_by_width = 0;
27791 for (glyph = start; glyph < start + len; ++glyph)
27792 shift_by_width += glyph->pixel_width;
27793
27794 /* Get the width of the region to shift right. */
27795 shifted_region_width = (window_box_width (w, updated_area)
27796 - w->output_cursor.x
27797 - shift_by_width);
27798
27799 /* Shift right. */
27800 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27801 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27802
27803 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27804 line_height, shift_by_width);
27805
27806 /* Write the glyphs. */
27807 hpos = start - row->glyphs[updated_area];
27808 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27809 hpos, hpos + len,
27810 DRAW_NORMAL_TEXT, 0);
27811
27812 /* Advance the output cursor. */
27813 w->output_cursor.hpos += len;
27814 w->output_cursor.x += shift_by_width;
27815 unblock_input ();
27816 }
27817
27818
27819 /* EXPORT for RIF:
27820 Erase the current text line from the nominal cursor position
27821 (inclusive) to pixel column TO_X (exclusive). The idea is that
27822 everything from TO_X onward is already erased.
27823
27824 TO_X is a pixel position relative to UPDATED_AREA of currently
27825 updated window W. TO_X == -1 means clear to the end of this area. */
27826
27827 void
27828 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27829 enum glyph_row_area updated_area, int to_x)
27830 {
27831 struct frame *f;
27832 int max_x, min_y, max_y;
27833 int from_x, from_y, to_y;
27834
27835 eassert (updated_row);
27836 f = XFRAME (w->frame);
27837
27838 if (updated_row->full_width_p)
27839 max_x = (WINDOW_PIXEL_WIDTH (w)
27840 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27841 else
27842 max_x = window_box_width (w, updated_area);
27843 max_y = window_text_bottom_y (w);
27844
27845 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27846 of window. For TO_X > 0, truncate to end of drawing area. */
27847 if (to_x == 0)
27848 return;
27849 else if (to_x < 0)
27850 to_x = max_x;
27851 else
27852 to_x = min (to_x, max_x);
27853
27854 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27855
27856 /* Notice if the cursor will be cleared by this operation. */
27857 if (!updated_row->full_width_p)
27858 notice_overwritten_cursor (w, updated_area,
27859 w->output_cursor.x, -1,
27860 updated_row->y,
27861 MATRIX_ROW_BOTTOM_Y (updated_row));
27862
27863 from_x = w->output_cursor.x;
27864
27865 /* Translate to frame coordinates. */
27866 if (updated_row->full_width_p)
27867 {
27868 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27869 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27870 }
27871 else
27872 {
27873 int area_left = window_box_left (w, updated_area);
27874 from_x += area_left;
27875 to_x += area_left;
27876 }
27877
27878 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27879 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27880 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27881
27882 /* Prevent inadvertently clearing to end of the X window. */
27883 if (to_x > from_x && to_y > from_y)
27884 {
27885 block_input ();
27886 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27887 to_x - from_x, to_y - from_y);
27888 unblock_input ();
27889 }
27890 }
27891
27892 #endif /* HAVE_WINDOW_SYSTEM */
27893
27894
27895 \f
27896 /***********************************************************************
27897 Cursor types
27898 ***********************************************************************/
27899
27900 /* Value is the internal representation of the specified cursor type
27901 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27902 of the bar cursor. */
27903
27904 static enum text_cursor_kinds
27905 get_specified_cursor_type (Lisp_Object arg, int *width)
27906 {
27907 enum text_cursor_kinds type;
27908
27909 if (NILP (arg))
27910 return NO_CURSOR;
27911
27912 if (EQ (arg, Qbox))
27913 return FILLED_BOX_CURSOR;
27914
27915 if (EQ (arg, Qhollow))
27916 return HOLLOW_BOX_CURSOR;
27917
27918 if (EQ (arg, Qbar))
27919 {
27920 *width = 2;
27921 return BAR_CURSOR;
27922 }
27923
27924 if (CONSP (arg)
27925 && EQ (XCAR (arg), Qbar)
27926 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27927 {
27928 *width = XINT (XCDR (arg));
27929 return BAR_CURSOR;
27930 }
27931
27932 if (EQ (arg, Qhbar))
27933 {
27934 *width = 2;
27935 return HBAR_CURSOR;
27936 }
27937
27938 if (CONSP (arg)
27939 && EQ (XCAR (arg), Qhbar)
27940 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27941 {
27942 *width = XINT (XCDR (arg));
27943 return HBAR_CURSOR;
27944 }
27945
27946 /* Treat anything unknown as "hollow box cursor".
27947 It was bad to signal an error; people have trouble fixing
27948 .Xdefaults with Emacs, when it has something bad in it. */
27949 type = HOLLOW_BOX_CURSOR;
27950
27951 return type;
27952 }
27953
27954 /* Set the default cursor types for specified frame. */
27955 void
27956 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27957 {
27958 int width = 1;
27959 Lisp_Object tem;
27960
27961 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27962 FRAME_CURSOR_WIDTH (f) = width;
27963
27964 /* By default, set up the blink-off state depending on the on-state. */
27965
27966 tem = Fassoc (arg, Vblink_cursor_alist);
27967 if (!NILP (tem))
27968 {
27969 FRAME_BLINK_OFF_CURSOR (f)
27970 = get_specified_cursor_type (XCDR (tem), &width);
27971 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27972 }
27973 else
27974 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27975
27976 /* Make sure the cursor gets redrawn. */
27977 f->cursor_type_changed = true;
27978 }
27979
27980
27981 #ifdef HAVE_WINDOW_SYSTEM
27982
27983 /* Return the cursor we want to be displayed in window W. Return
27984 width of bar/hbar cursor through WIDTH arg. Return with
27985 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
27986 (i.e. if the `system caret' should track this cursor).
27987
27988 In a mini-buffer window, we want the cursor only to appear if we
27989 are reading input from this window. For the selected window, we
27990 want the cursor type given by the frame parameter or buffer local
27991 setting of cursor-type. If explicitly marked off, draw no cursor.
27992 In all other cases, we want a hollow box cursor. */
27993
27994 static enum text_cursor_kinds
27995 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27996 bool *active_cursor)
27997 {
27998 struct frame *f = XFRAME (w->frame);
27999 struct buffer *b = XBUFFER (w->contents);
28000 int cursor_type = DEFAULT_CURSOR;
28001 Lisp_Object alt_cursor;
28002 bool non_selected = false;
28003
28004 *active_cursor = true;
28005
28006 /* Echo area */
28007 if (cursor_in_echo_area
28008 && FRAME_HAS_MINIBUF_P (f)
28009 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
28010 {
28011 if (w == XWINDOW (echo_area_window))
28012 {
28013 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
28014 {
28015 *width = FRAME_CURSOR_WIDTH (f);
28016 return FRAME_DESIRED_CURSOR (f);
28017 }
28018 else
28019 return get_specified_cursor_type (BVAR (b, cursor_type), width);
28020 }
28021
28022 *active_cursor = false;
28023 non_selected = true;
28024 }
28025
28026 /* Detect a nonselected window or nonselected frame. */
28027 else if (w != XWINDOW (f->selected_window)
28028 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
28029 {
28030 *active_cursor = false;
28031
28032 if (MINI_WINDOW_P (w) && minibuf_level == 0)
28033 return NO_CURSOR;
28034
28035 non_selected = true;
28036 }
28037
28038 /* Never display a cursor in a window in which cursor-type is nil. */
28039 if (NILP (BVAR (b, cursor_type)))
28040 return NO_CURSOR;
28041
28042 /* Get the normal cursor type for this window. */
28043 if (EQ (BVAR (b, cursor_type), Qt))
28044 {
28045 cursor_type = FRAME_DESIRED_CURSOR (f);
28046 *width = FRAME_CURSOR_WIDTH (f);
28047 }
28048 else
28049 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
28050
28051 /* Use cursor-in-non-selected-windows instead
28052 for non-selected window or frame. */
28053 if (non_selected)
28054 {
28055 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
28056 if (!EQ (Qt, alt_cursor))
28057 return get_specified_cursor_type (alt_cursor, width);
28058 /* t means modify the normal cursor type. */
28059 if (cursor_type == FILLED_BOX_CURSOR)
28060 cursor_type = HOLLOW_BOX_CURSOR;
28061 else if (cursor_type == BAR_CURSOR && *width > 1)
28062 --*width;
28063 return cursor_type;
28064 }
28065
28066 /* Use normal cursor if not blinked off. */
28067 if (!w->cursor_off_p)
28068 {
28069 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
28070 return NO_CURSOR;
28071 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28072 {
28073 if (cursor_type == FILLED_BOX_CURSOR)
28074 {
28075 /* Using a block cursor on large images can be very annoying.
28076 So use a hollow cursor for "large" images.
28077 If image is not transparent (no mask), also use hollow cursor. */
28078 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
28079 if (img != NULL && IMAGEP (img->spec))
28080 {
28081 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
28082 where N = size of default frame font size.
28083 This should cover most of the "tiny" icons people may use. */
28084 if (!img->mask
28085 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
28086 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
28087 cursor_type = HOLLOW_BOX_CURSOR;
28088 }
28089 }
28090 else if (cursor_type != NO_CURSOR)
28091 {
28092 /* Display current only supports BOX and HOLLOW cursors for images.
28093 So for now, unconditionally use a HOLLOW cursor when cursor is
28094 not a solid box cursor. */
28095 cursor_type = HOLLOW_BOX_CURSOR;
28096 }
28097 }
28098 return cursor_type;
28099 }
28100
28101 /* Cursor is blinked off, so determine how to "toggle" it. */
28102
28103 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
28104 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
28105 return get_specified_cursor_type (XCDR (alt_cursor), width);
28106
28107 /* Then see if frame has specified a specific blink off cursor type. */
28108 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
28109 {
28110 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
28111 return FRAME_BLINK_OFF_CURSOR (f);
28112 }
28113
28114 #if false
28115 /* Some people liked having a permanently visible blinking cursor,
28116 while others had very strong opinions against it. So it was
28117 decided to remove it. KFS 2003-09-03 */
28118
28119 /* Finally perform built-in cursor blinking:
28120 filled box <-> hollow box
28121 wide [h]bar <-> narrow [h]bar
28122 narrow [h]bar <-> no cursor
28123 other type <-> no cursor */
28124
28125 if (cursor_type == FILLED_BOX_CURSOR)
28126 return HOLLOW_BOX_CURSOR;
28127
28128 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
28129 {
28130 *width = 1;
28131 return cursor_type;
28132 }
28133 #endif
28134
28135 return NO_CURSOR;
28136 }
28137
28138
28139 /* Notice when the text cursor of window W has been completely
28140 overwritten by a drawing operation that outputs glyphs in AREA
28141 starting at X0 and ending at X1 in the line starting at Y0 and
28142 ending at Y1. X coordinates are area-relative. X1 < 0 means all
28143 the rest of the line after X0 has been written. Y coordinates
28144 are window-relative. */
28145
28146 static void
28147 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
28148 int x0, int x1, int y0, int y1)
28149 {
28150 int cx0, cx1, cy0, cy1;
28151 struct glyph_row *row;
28152
28153 if (!w->phys_cursor_on_p)
28154 return;
28155 if (area != TEXT_AREA)
28156 return;
28157
28158 if (w->phys_cursor.vpos < 0
28159 || w->phys_cursor.vpos >= w->current_matrix->nrows
28160 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
28161 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
28162 return;
28163
28164 if (row->cursor_in_fringe_p)
28165 {
28166 row->cursor_in_fringe_p = false;
28167 draw_fringe_bitmap (w, row, row->reversed_p);
28168 w->phys_cursor_on_p = false;
28169 return;
28170 }
28171
28172 cx0 = w->phys_cursor.x;
28173 cx1 = cx0 + w->phys_cursor_width;
28174 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
28175 return;
28176
28177 /* The cursor image will be completely removed from the
28178 screen if the output area intersects the cursor area in
28179 y-direction. When we draw in [y0 y1[, and some part of
28180 the cursor is at y < y0, that part must have been drawn
28181 before. When scrolling, the cursor is erased before
28182 actually scrolling, so we don't come here. When not
28183 scrolling, the rows above the old cursor row must have
28184 changed, and in this case these rows must have written
28185 over the cursor image.
28186
28187 Likewise if part of the cursor is below y1, with the
28188 exception of the cursor being in the first blank row at
28189 the buffer and window end because update_text_area
28190 doesn't draw that row. (Except when it does, but
28191 that's handled in update_text_area.) */
28192
28193 cy0 = w->phys_cursor.y;
28194 cy1 = cy0 + w->phys_cursor_height;
28195 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
28196 return;
28197
28198 w->phys_cursor_on_p = false;
28199 }
28200
28201 #endif /* HAVE_WINDOW_SYSTEM */
28202
28203 \f
28204 /************************************************************************
28205 Mouse Face
28206 ************************************************************************/
28207
28208 #ifdef HAVE_WINDOW_SYSTEM
28209
28210 /* EXPORT for RIF:
28211 Fix the display of area AREA of overlapping row ROW in window W
28212 with respect to the overlapping part OVERLAPS. */
28213
28214 void
28215 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
28216 enum glyph_row_area area, int overlaps)
28217 {
28218 int i, x;
28219
28220 block_input ();
28221
28222 x = 0;
28223 for (i = 0; i < row->used[area];)
28224 {
28225 if (row->glyphs[area][i].overlaps_vertically_p)
28226 {
28227 int start = i, start_x = x;
28228
28229 do
28230 {
28231 x += row->glyphs[area][i].pixel_width;
28232 ++i;
28233 }
28234 while (i < row->used[area]
28235 && row->glyphs[area][i].overlaps_vertically_p);
28236
28237 draw_glyphs (w, start_x, row, area,
28238 start, i,
28239 DRAW_NORMAL_TEXT, overlaps);
28240 }
28241 else
28242 {
28243 x += row->glyphs[area][i].pixel_width;
28244 ++i;
28245 }
28246 }
28247
28248 unblock_input ();
28249 }
28250
28251
28252 /* EXPORT:
28253 Draw the cursor glyph of window W in glyph row ROW. See the
28254 comment of draw_glyphs for the meaning of HL. */
28255
28256 void
28257 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
28258 enum draw_glyphs_face hl)
28259 {
28260 /* If cursor hpos is out of bounds, don't draw garbage. This can
28261 happen in mini-buffer windows when switching between echo area
28262 glyphs and mini-buffer. */
28263 if ((row->reversed_p
28264 ? (w->phys_cursor.hpos >= 0)
28265 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
28266 {
28267 bool on_p = w->phys_cursor_on_p;
28268 int x1;
28269 int hpos = w->phys_cursor.hpos;
28270
28271 /* When the window is hscrolled, cursor hpos can legitimately be
28272 out of bounds, but we draw the cursor at the corresponding
28273 window margin in that case. */
28274 if (!row->reversed_p && hpos < 0)
28275 hpos = 0;
28276 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28277 hpos = row->used[TEXT_AREA] - 1;
28278
28279 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
28280 hl, 0);
28281 w->phys_cursor_on_p = on_p;
28282
28283 if (hl == DRAW_CURSOR)
28284 w->phys_cursor_width = x1 - w->phys_cursor.x;
28285 /* When we erase the cursor, and ROW is overlapped by other
28286 rows, make sure that these overlapping parts of other rows
28287 are redrawn. */
28288 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
28289 {
28290 w->phys_cursor_width = x1 - w->phys_cursor.x;
28291
28292 if (row > w->current_matrix->rows
28293 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
28294 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
28295 OVERLAPS_ERASED_CURSOR);
28296
28297 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
28298 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
28299 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
28300 OVERLAPS_ERASED_CURSOR);
28301 }
28302 }
28303 }
28304
28305
28306 /* Erase the image of a cursor of window W from the screen. */
28307
28308 void
28309 erase_phys_cursor (struct window *w)
28310 {
28311 struct frame *f = XFRAME (w->frame);
28312 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28313 int hpos = w->phys_cursor.hpos;
28314 int vpos = w->phys_cursor.vpos;
28315 bool mouse_face_here_p = false;
28316 struct glyph_matrix *active_glyphs = w->current_matrix;
28317 struct glyph_row *cursor_row;
28318 struct glyph *cursor_glyph;
28319 enum draw_glyphs_face hl;
28320
28321 /* No cursor displayed or row invalidated => nothing to do on the
28322 screen. */
28323 if (w->phys_cursor_type == NO_CURSOR)
28324 goto mark_cursor_off;
28325
28326 /* VPOS >= active_glyphs->nrows means that window has been resized.
28327 Don't bother to erase the cursor. */
28328 if (vpos >= active_glyphs->nrows)
28329 goto mark_cursor_off;
28330
28331 /* If row containing cursor is marked invalid, there is nothing we
28332 can do. */
28333 cursor_row = MATRIX_ROW (active_glyphs, vpos);
28334 if (!cursor_row->enabled_p)
28335 goto mark_cursor_off;
28336
28337 /* If line spacing is > 0, old cursor may only be partially visible in
28338 window after split-window. So adjust visible height. */
28339 cursor_row->visible_height = min (cursor_row->visible_height,
28340 window_text_bottom_y (w) - cursor_row->y);
28341
28342 /* If row is completely invisible, don't attempt to delete a cursor which
28343 isn't there. This can happen if cursor is at top of a window, and
28344 we switch to a buffer with a header line in that window. */
28345 if (cursor_row->visible_height <= 0)
28346 goto mark_cursor_off;
28347
28348 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
28349 if (cursor_row->cursor_in_fringe_p)
28350 {
28351 cursor_row->cursor_in_fringe_p = false;
28352 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
28353 goto mark_cursor_off;
28354 }
28355
28356 /* This can happen when the new row is shorter than the old one.
28357 In this case, either draw_glyphs or clear_end_of_line
28358 should have cleared the cursor. Note that we wouldn't be
28359 able to erase the cursor in this case because we don't have a
28360 cursor glyph at hand. */
28361 if ((cursor_row->reversed_p
28362 ? (w->phys_cursor.hpos < 0)
28363 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
28364 goto mark_cursor_off;
28365
28366 /* When the window is hscrolled, cursor hpos can legitimately be out
28367 of bounds, but we draw the cursor at the corresponding window
28368 margin in that case. */
28369 if (!cursor_row->reversed_p && hpos < 0)
28370 hpos = 0;
28371 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
28372 hpos = cursor_row->used[TEXT_AREA] - 1;
28373
28374 /* If the cursor is in the mouse face area, redisplay that when
28375 we clear the cursor. */
28376 if (! NILP (hlinfo->mouse_face_window)
28377 && coords_in_mouse_face_p (w, hpos, vpos)
28378 /* Don't redraw the cursor's spot in mouse face if it is at the
28379 end of a line (on a newline). The cursor appears there, but
28380 mouse highlighting does not. */
28381 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
28382 mouse_face_here_p = true;
28383
28384 /* Maybe clear the display under the cursor. */
28385 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
28386 {
28387 int x, y;
28388 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
28389 int width;
28390
28391 cursor_glyph = get_phys_cursor_glyph (w);
28392 if (cursor_glyph == NULL)
28393 goto mark_cursor_off;
28394
28395 width = cursor_glyph->pixel_width;
28396 x = w->phys_cursor.x;
28397 if (x < 0)
28398 {
28399 width += x;
28400 x = 0;
28401 }
28402 width = min (width, window_box_width (w, TEXT_AREA) - x);
28403 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
28404 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28405
28406 if (width > 0)
28407 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28408 }
28409
28410 /* Erase the cursor by redrawing the character underneath it. */
28411 if (mouse_face_here_p)
28412 hl = DRAW_MOUSE_FACE;
28413 else
28414 hl = DRAW_NORMAL_TEXT;
28415 draw_phys_cursor_glyph (w, cursor_row, hl);
28416
28417 mark_cursor_off:
28418 w->phys_cursor_on_p = false;
28419 w->phys_cursor_type = NO_CURSOR;
28420 }
28421
28422
28423 /* Display or clear cursor of window W. If !ON, clear the cursor.
28424 If ON, display the cursor; where to put the cursor is specified by
28425 HPOS, VPOS, X and Y. */
28426
28427 void
28428 display_and_set_cursor (struct window *w, bool on,
28429 int hpos, int vpos, int x, int y)
28430 {
28431 struct frame *f = XFRAME (w->frame);
28432 int new_cursor_type;
28433 int new_cursor_width;
28434 bool active_cursor;
28435 struct glyph_row *glyph_row;
28436 struct glyph *glyph;
28437
28438 /* This is pointless on invisible frames, and dangerous on garbaged
28439 windows and frames; in the latter case, the frame or window may
28440 be in the midst of changing its size, and x and y may be off the
28441 window. */
28442 if (! FRAME_VISIBLE_P (f)
28443 || FRAME_GARBAGED_P (f)
28444 || vpos >= w->current_matrix->nrows
28445 || hpos >= w->current_matrix->matrix_w)
28446 return;
28447
28448 /* If cursor is off and we want it off, return quickly. */
28449 if (!on && !w->phys_cursor_on_p)
28450 return;
28451
28452 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28453 /* If cursor row is not enabled, we don't really know where to
28454 display the cursor. */
28455 if (!glyph_row->enabled_p)
28456 {
28457 w->phys_cursor_on_p = false;
28458 return;
28459 }
28460
28461 glyph = NULL;
28462 if (!glyph_row->exact_window_width_line_p
28463 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
28464 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28465
28466 eassert (input_blocked_p ());
28467
28468 /* Set new_cursor_type to the cursor we want to be displayed. */
28469 new_cursor_type = get_window_cursor_type (w, glyph,
28470 &new_cursor_width, &active_cursor);
28471
28472 /* If cursor is currently being shown and we don't want it to be or
28473 it is in the wrong place, or the cursor type is not what we want,
28474 erase it. */
28475 if (w->phys_cursor_on_p
28476 && (!on
28477 || w->phys_cursor.x != x
28478 || w->phys_cursor.y != y
28479 /* HPOS can be negative in R2L rows whose
28480 exact_window_width_line_p flag is set (i.e. their newline
28481 would "overflow into the fringe"). */
28482 || hpos < 0
28483 || new_cursor_type != w->phys_cursor_type
28484 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28485 && new_cursor_width != w->phys_cursor_width)))
28486 erase_phys_cursor (w);
28487
28488 /* Don't check phys_cursor_on_p here because that flag is only set
28489 to false in some cases where we know that the cursor has been
28490 completely erased, to avoid the extra work of erasing the cursor
28491 twice. In other words, phys_cursor_on_p can be true and the cursor
28492 still not be visible, or it has only been partly erased. */
28493 if (on)
28494 {
28495 w->phys_cursor_ascent = glyph_row->ascent;
28496 w->phys_cursor_height = glyph_row->height;
28497
28498 /* Set phys_cursor_.* before x_draw_.* is called because some
28499 of them may need the information. */
28500 w->phys_cursor.x = x;
28501 w->phys_cursor.y = glyph_row->y;
28502 w->phys_cursor.hpos = hpos;
28503 w->phys_cursor.vpos = vpos;
28504 }
28505
28506 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28507 new_cursor_type, new_cursor_width,
28508 on, active_cursor);
28509 }
28510
28511
28512 /* Switch the display of W's cursor on or off, according to the value
28513 of ON. */
28514
28515 static void
28516 update_window_cursor (struct window *w, bool on)
28517 {
28518 /* Don't update cursor in windows whose frame is in the process
28519 of being deleted. */
28520 if (w->current_matrix)
28521 {
28522 int hpos = w->phys_cursor.hpos;
28523 int vpos = w->phys_cursor.vpos;
28524 struct glyph_row *row;
28525
28526 if (vpos >= w->current_matrix->nrows
28527 || hpos >= w->current_matrix->matrix_w)
28528 return;
28529
28530 row = MATRIX_ROW (w->current_matrix, vpos);
28531
28532 /* When the window is hscrolled, cursor hpos can legitimately be
28533 out of bounds, but we draw the cursor at the corresponding
28534 window margin in that case. */
28535 if (!row->reversed_p && hpos < 0)
28536 hpos = 0;
28537 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28538 hpos = row->used[TEXT_AREA] - 1;
28539
28540 block_input ();
28541 display_and_set_cursor (w, on, hpos, vpos,
28542 w->phys_cursor.x, w->phys_cursor.y);
28543 unblock_input ();
28544 }
28545 }
28546
28547
28548 /* Call update_window_cursor with parameter ON_P on all leaf windows
28549 in the window tree rooted at W. */
28550
28551 static void
28552 update_cursor_in_window_tree (struct window *w, bool on_p)
28553 {
28554 while (w)
28555 {
28556 if (WINDOWP (w->contents))
28557 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28558 else
28559 update_window_cursor (w, on_p);
28560
28561 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28562 }
28563 }
28564
28565
28566 /* EXPORT:
28567 Display the cursor on window W, or clear it, according to ON_P.
28568 Don't change the cursor's position. */
28569
28570 void
28571 x_update_cursor (struct frame *f, bool on_p)
28572 {
28573 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28574 }
28575
28576
28577 /* EXPORT:
28578 Clear the cursor of window W to background color, and mark the
28579 cursor as not shown. This is used when the text where the cursor
28580 is about to be rewritten. */
28581
28582 void
28583 x_clear_cursor (struct window *w)
28584 {
28585 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28586 update_window_cursor (w, false);
28587 }
28588
28589 #endif /* HAVE_WINDOW_SYSTEM */
28590
28591 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28592 and MSDOS. */
28593 static void
28594 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28595 int start_hpos, int end_hpos,
28596 enum draw_glyphs_face draw)
28597 {
28598 #ifdef HAVE_WINDOW_SYSTEM
28599 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28600 {
28601 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28602 return;
28603 }
28604 #endif
28605 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28606 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28607 #endif
28608 }
28609
28610 /* Display the active region described by mouse_face_* according to DRAW. */
28611
28612 static void
28613 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28614 {
28615 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28616 struct frame *f = XFRAME (WINDOW_FRAME (w));
28617
28618 if (/* If window is in the process of being destroyed, don't bother
28619 to do anything. */
28620 w->current_matrix != NULL
28621 /* Don't update mouse highlight if hidden. */
28622 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28623 /* Recognize when we are called to operate on rows that don't exist
28624 anymore. This can happen when a window is split. */
28625 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28626 {
28627 bool phys_cursor_on_p = w->phys_cursor_on_p;
28628 struct glyph_row *row, *first, *last;
28629
28630 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28631 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28632
28633 for (row = first; row <= last && row->enabled_p; ++row)
28634 {
28635 int start_hpos, end_hpos, start_x;
28636
28637 /* For all but the first row, the highlight starts at column 0. */
28638 if (row == first)
28639 {
28640 /* R2L rows have BEG and END in reversed order, but the
28641 screen drawing geometry is always left to right. So
28642 we need to mirror the beginning and end of the
28643 highlighted area in R2L rows. */
28644 if (!row->reversed_p)
28645 {
28646 start_hpos = hlinfo->mouse_face_beg_col;
28647 start_x = hlinfo->mouse_face_beg_x;
28648 }
28649 else if (row == last)
28650 {
28651 start_hpos = hlinfo->mouse_face_end_col;
28652 start_x = hlinfo->mouse_face_end_x;
28653 }
28654 else
28655 {
28656 start_hpos = 0;
28657 start_x = 0;
28658 }
28659 }
28660 else if (row->reversed_p && row == last)
28661 {
28662 start_hpos = hlinfo->mouse_face_end_col;
28663 start_x = hlinfo->mouse_face_end_x;
28664 }
28665 else
28666 {
28667 start_hpos = 0;
28668 start_x = 0;
28669 }
28670
28671 if (row == last)
28672 {
28673 if (!row->reversed_p)
28674 end_hpos = hlinfo->mouse_face_end_col;
28675 else if (row == first)
28676 end_hpos = hlinfo->mouse_face_beg_col;
28677 else
28678 {
28679 end_hpos = row->used[TEXT_AREA];
28680 if (draw == DRAW_NORMAL_TEXT)
28681 row->fill_line_p = true; /* Clear to end of line. */
28682 }
28683 }
28684 else if (row->reversed_p && row == first)
28685 end_hpos = hlinfo->mouse_face_beg_col;
28686 else
28687 {
28688 end_hpos = row->used[TEXT_AREA];
28689 if (draw == DRAW_NORMAL_TEXT)
28690 row->fill_line_p = true; /* Clear to end of line. */
28691 }
28692
28693 if (end_hpos > start_hpos)
28694 {
28695 draw_row_with_mouse_face (w, start_x, row,
28696 start_hpos, end_hpos, draw);
28697
28698 row->mouse_face_p
28699 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28700 }
28701 }
28702
28703 /* When we've written over the cursor, arrange for it to
28704 be displayed again. */
28705 if (FRAME_WINDOW_P (f)
28706 && phys_cursor_on_p && !w->phys_cursor_on_p)
28707 {
28708 #ifdef HAVE_WINDOW_SYSTEM
28709 int hpos = w->phys_cursor.hpos;
28710
28711 /* When the window is hscrolled, cursor hpos can legitimately be
28712 out of bounds, but we draw the cursor at the corresponding
28713 window margin in that case. */
28714 if (!row->reversed_p && hpos < 0)
28715 hpos = 0;
28716 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28717 hpos = row->used[TEXT_AREA] - 1;
28718
28719 block_input ();
28720 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28721 w->phys_cursor.x, w->phys_cursor.y);
28722 unblock_input ();
28723 #endif /* HAVE_WINDOW_SYSTEM */
28724 }
28725 }
28726
28727 #ifdef HAVE_WINDOW_SYSTEM
28728 /* Change the mouse cursor. */
28729 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28730 {
28731 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28732 if (draw == DRAW_NORMAL_TEXT
28733 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28734 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28735 else
28736 #endif
28737 if (draw == DRAW_MOUSE_FACE)
28738 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28739 else
28740 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28741 }
28742 #endif /* HAVE_WINDOW_SYSTEM */
28743 }
28744
28745 /* EXPORT:
28746 Clear out the mouse-highlighted active region.
28747 Redraw it un-highlighted first. Value is true if mouse
28748 face was actually drawn unhighlighted. */
28749
28750 bool
28751 clear_mouse_face (Mouse_HLInfo *hlinfo)
28752 {
28753 bool cleared
28754 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28755 if (cleared)
28756 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28757 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28758 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28759 hlinfo->mouse_face_window = Qnil;
28760 hlinfo->mouse_face_overlay = Qnil;
28761 return cleared;
28762 }
28763
28764 /* Return true if the coordinates HPOS and VPOS on windows W are
28765 within the mouse face on that window. */
28766 static bool
28767 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28768 {
28769 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28770
28771 /* Quickly resolve the easy cases. */
28772 if (!(WINDOWP (hlinfo->mouse_face_window)
28773 && XWINDOW (hlinfo->mouse_face_window) == w))
28774 return false;
28775 if (vpos < hlinfo->mouse_face_beg_row
28776 || vpos > hlinfo->mouse_face_end_row)
28777 return false;
28778 if (vpos > hlinfo->mouse_face_beg_row
28779 && vpos < hlinfo->mouse_face_end_row)
28780 return true;
28781
28782 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28783 {
28784 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28785 {
28786 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28787 return true;
28788 }
28789 else if ((vpos == hlinfo->mouse_face_beg_row
28790 && hpos >= hlinfo->mouse_face_beg_col)
28791 || (vpos == hlinfo->mouse_face_end_row
28792 && hpos < hlinfo->mouse_face_end_col))
28793 return true;
28794 }
28795 else
28796 {
28797 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28798 {
28799 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28800 return true;
28801 }
28802 else if ((vpos == hlinfo->mouse_face_beg_row
28803 && hpos <= hlinfo->mouse_face_beg_col)
28804 || (vpos == hlinfo->mouse_face_end_row
28805 && hpos > hlinfo->mouse_face_end_col))
28806 return true;
28807 }
28808 return false;
28809 }
28810
28811
28812 /* EXPORT:
28813 True if physical cursor of window W is within mouse face. */
28814
28815 bool
28816 cursor_in_mouse_face_p (struct window *w)
28817 {
28818 int hpos = w->phys_cursor.hpos;
28819 int vpos = w->phys_cursor.vpos;
28820 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28821
28822 /* When the window is hscrolled, cursor hpos can legitimately be out
28823 of bounds, but we draw the cursor at the corresponding window
28824 margin in that case. */
28825 if (!row->reversed_p && hpos < 0)
28826 hpos = 0;
28827 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28828 hpos = row->used[TEXT_AREA] - 1;
28829
28830 return coords_in_mouse_face_p (w, hpos, vpos);
28831 }
28832
28833
28834 \f
28835 /* Find the glyph rows START_ROW and END_ROW of window W that display
28836 characters between buffer positions START_CHARPOS and END_CHARPOS
28837 (excluding END_CHARPOS). DISP_STRING is a display string that
28838 covers these buffer positions. This is similar to
28839 row_containing_pos, but is more accurate when bidi reordering makes
28840 buffer positions change non-linearly with glyph rows. */
28841 static void
28842 rows_from_pos_range (struct window *w,
28843 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28844 Lisp_Object disp_string,
28845 struct glyph_row **start, struct glyph_row **end)
28846 {
28847 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28848 int last_y = window_text_bottom_y (w);
28849 struct glyph_row *row;
28850
28851 *start = NULL;
28852 *end = NULL;
28853
28854 while (!first->enabled_p
28855 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28856 first++;
28857
28858 /* Find the START row. */
28859 for (row = first;
28860 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28861 row++)
28862 {
28863 /* A row can potentially be the START row if the range of the
28864 characters it displays intersects the range
28865 [START_CHARPOS..END_CHARPOS). */
28866 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28867 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28868 /* See the commentary in row_containing_pos, for the
28869 explanation of the complicated way to check whether
28870 some position is beyond the end of the characters
28871 displayed by a row. */
28872 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28873 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28874 && !row->ends_at_zv_p
28875 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28876 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28877 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28878 && !row->ends_at_zv_p
28879 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28880 {
28881 /* Found a candidate row. Now make sure at least one of the
28882 glyphs it displays has a charpos from the range
28883 [START_CHARPOS..END_CHARPOS).
28884
28885 This is not obvious because bidi reordering could make
28886 buffer positions of a row be 1,2,3,102,101,100, and if we
28887 want to highlight characters in [50..60), we don't want
28888 this row, even though [50..60) does intersect [1..103),
28889 the range of character positions given by the row's start
28890 and end positions. */
28891 struct glyph *g = row->glyphs[TEXT_AREA];
28892 struct glyph *e = g + row->used[TEXT_AREA];
28893
28894 while (g < e)
28895 {
28896 if (((BUFFERP (g->object) || NILP (g->object))
28897 && start_charpos <= g->charpos && g->charpos < end_charpos)
28898 /* A glyph that comes from DISP_STRING is by
28899 definition to be highlighted. */
28900 || EQ (g->object, disp_string))
28901 *start = row;
28902 g++;
28903 }
28904 if (*start)
28905 break;
28906 }
28907 }
28908
28909 /* Find the END row. */
28910 if (!*start
28911 /* If the last row is partially visible, start looking for END
28912 from that row, instead of starting from FIRST. */
28913 && !(row->enabled_p
28914 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28915 row = first;
28916 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28917 {
28918 struct glyph_row *next = row + 1;
28919 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28920
28921 if (!next->enabled_p
28922 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28923 /* The first row >= START whose range of displayed characters
28924 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28925 is the row END + 1. */
28926 || (start_charpos < next_start
28927 && end_charpos < next_start)
28928 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28929 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28930 && !next->ends_at_zv_p
28931 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28932 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28933 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28934 && !next->ends_at_zv_p
28935 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28936 {
28937 *end = row;
28938 break;
28939 }
28940 else
28941 {
28942 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28943 but none of the characters it displays are in the range, it is
28944 also END + 1. */
28945 struct glyph *g = next->glyphs[TEXT_AREA];
28946 struct glyph *s = g;
28947 struct glyph *e = g + next->used[TEXT_AREA];
28948
28949 while (g < e)
28950 {
28951 if (((BUFFERP (g->object) || NILP (g->object))
28952 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28953 /* If the buffer position of the first glyph in
28954 the row is equal to END_CHARPOS, it means
28955 the last character to be highlighted is the
28956 newline of ROW, and we must consider NEXT as
28957 END, not END+1. */
28958 || (((!next->reversed_p && g == s)
28959 || (next->reversed_p && g == e - 1))
28960 && (g->charpos == end_charpos
28961 /* Special case for when NEXT is an
28962 empty line at ZV. */
28963 || (g->charpos == -1
28964 && !row->ends_at_zv_p
28965 && next_start == end_charpos)))))
28966 /* A glyph that comes from DISP_STRING is by
28967 definition to be highlighted. */
28968 || EQ (g->object, disp_string))
28969 break;
28970 g++;
28971 }
28972 if (g == e)
28973 {
28974 *end = row;
28975 break;
28976 }
28977 /* The first row that ends at ZV must be the last to be
28978 highlighted. */
28979 else if (next->ends_at_zv_p)
28980 {
28981 *end = next;
28982 break;
28983 }
28984 }
28985 }
28986 }
28987
28988 /* This function sets the mouse_face_* elements of HLINFO, assuming
28989 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28990 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28991 for the overlay or run of text properties specifying the mouse
28992 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28993 before-string and after-string that must also be highlighted.
28994 DISP_STRING, if non-nil, is a display string that may cover some
28995 or all of the highlighted text. */
28996
28997 static void
28998 mouse_face_from_buffer_pos (Lisp_Object window,
28999 Mouse_HLInfo *hlinfo,
29000 ptrdiff_t mouse_charpos,
29001 ptrdiff_t start_charpos,
29002 ptrdiff_t end_charpos,
29003 Lisp_Object before_string,
29004 Lisp_Object after_string,
29005 Lisp_Object disp_string)
29006 {
29007 struct window *w = XWINDOW (window);
29008 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29009 struct glyph_row *r1, *r2;
29010 struct glyph *glyph, *end;
29011 ptrdiff_t ignore, pos;
29012 int x;
29013
29014 eassert (NILP (disp_string) || STRINGP (disp_string));
29015 eassert (NILP (before_string) || STRINGP (before_string));
29016 eassert (NILP (after_string) || STRINGP (after_string));
29017
29018 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
29019 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
29020 if (r1 == NULL)
29021 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29022 /* If the before-string or display-string contains newlines,
29023 rows_from_pos_range skips to its last row. Move back. */
29024 if (!NILP (before_string) || !NILP (disp_string))
29025 {
29026 struct glyph_row *prev;
29027 while ((prev = r1 - 1, prev >= first)
29028 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
29029 && prev->used[TEXT_AREA] > 0)
29030 {
29031 struct glyph *beg = prev->glyphs[TEXT_AREA];
29032 glyph = beg + prev->used[TEXT_AREA];
29033 while (--glyph >= beg && NILP (glyph->object));
29034 if (glyph < beg
29035 || !(EQ (glyph->object, before_string)
29036 || EQ (glyph->object, disp_string)))
29037 break;
29038 r1 = prev;
29039 }
29040 }
29041 if (r2 == NULL)
29042 {
29043 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29044 hlinfo->mouse_face_past_end = true;
29045 }
29046 else if (!NILP (after_string))
29047 {
29048 /* If the after-string has newlines, advance to its last row. */
29049 struct glyph_row *next;
29050 struct glyph_row *last
29051 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29052
29053 for (next = r2 + 1;
29054 next <= last
29055 && next->used[TEXT_AREA] > 0
29056 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
29057 ++next)
29058 r2 = next;
29059 }
29060 /* The rest of the display engine assumes that mouse_face_beg_row is
29061 either above mouse_face_end_row or identical to it. But with
29062 bidi-reordered continued lines, the row for START_CHARPOS could
29063 be below the row for END_CHARPOS. If so, swap the rows and store
29064 them in correct order. */
29065 if (r1->y > r2->y)
29066 {
29067 struct glyph_row *tem = r2;
29068
29069 r2 = r1;
29070 r1 = tem;
29071 }
29072
29073 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
29074 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
29075
29076 /* For a bidi-reordered row, the positions of BEFORE_STRING,
29077 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
29078 could be anywhere in the row and in any order. The strategy
29079 below is to find the leftmost and the rightmost glyph that
29080 belongs to either of these 3 strings, or whose position is
29081 between START_CHARPOS and END_CHARPOS, and highlight all the
29082 glyphs between those two. This may cover more than just the text
29083 between START_CHARPOS and END_CHARPOS if the range of characters
29084 strides the bidi level boundary, e.g. if the beginning is in R2L
29085 text while the end is in L2R text or vice versa. */
29086 if (!r1->reversed_p)
29087 {
29088 /* This row is in a left to right paragraph. Scan it left to
29089 right. */
29090 glyph = r1->glyphs[TEXT_AREA];
29091 end = glyph + r1->used[TEXT_AREA];
29092 x = r1->x;
29093
29094 /* Skip truncation glyphs at the start of the glyph row. */
29095 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29096 for (; glyph < end
29097 && NILP (glyph->object)
29098 && glyph->charpos < 0;
29099 ++glyph)
29100 x += glyph->pixel_width;
29101
29102 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29103 or DISP_STRING, and the first glyph from buffer whose
29104 position is between START_CHARPOS and END_CHARPOS. */
29105 for (; glyph < end
29106 && !NILP (glyph->object)
29107 && !EQ (glyph->object, disp_string)
29108 && !(BUFFERP (glyph->object)
29109 && (glyph->charpos >= start_charpos
29110 && glyph->charpos < end_charpos));
29111 ++glyph)
29112 {
29113 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29114 are present at buffer positions between START_CHARPOS and
29115 END_CHARPOS, or if they come from an overlay. */
29116 if (EQ (glyph->object, before_string))
29117 {
29118 pos = string_buffer_position (before_string,
29119 start_charpos);
29120 /* If pos == 0, it means before_string came from an
29121 overlay, not from a buffer position. */
29122 if (!pos || (pos >= start_charpos && pos < end_charpos))
29123 break;
29124 }
29125 else if (EQ (glyph->object, after_string))
29126 {
29127 pos = string_buffer_position (after_string, end_charpos);
29128 if (!pos || (pos >= start_charpos && pos < end_charpos))
29129 break;
29130 }
29131 x += glyph->pixel_width;
29132 }
29133 hlinfo->mouse_face_beg_x = x;
29134 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29135 }
29136 else
29137 {
29138 /* This row is in a right to left paragraph. Scan it right to
29139 left. */
29140 struct glyph *g;
29141
29142 end = r1->glyphs[TEXT_AREA] - 1;
29143 glyph = end + r1->used[TEXT_AREA];
29144
29145 /* Skip truncation glyphs at the start of the glyph row. */
29146 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29147 for (; glyph > end
29148 && NILP (glyph->object)
29149 && glyph->charpos < 0;
29150 --glyph)
29151 ;
29152
29153 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29154 or DISP_STRING, and the first glyph from buffer whose
29155 position is between START_CHARPOS and END_CHARPOS. */
29156 for (; glyph > end
29157 && !NILP (glyph->object)
29158 && !EQ (glyph->object, disp_string)
29159 && !(BUFFERP (glyph->object)
29160 && (glyph->charpos >= start_charpos
29161 && glyph->charpos < end_charpos));
29162 --glyph)
29163 {
29164 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29165 are present at buffer positions between START_CHARPOS and
29166 END_CHARPOS, or if they come from an overlay. */
29167 if (EQ (glyph->object, before_string))
29168 {
29169 pos = string_buffer_position (before_string, start_charpos);
29170 /* If pos == 0, it means before_string came from an
29171 overlay, not from a buffer position. */
29172 if (!pos || (pos >= start_charpos && pos < end_charpos))
29173 break;
29174 }
29175 else if (EQ (glyph->object, after_string))
29176 {
29177 pos = string_buffer_position (after_string, end_charpos);
29178 if (!pos || (pos >= start_charpos && pos < end_charpos))
29179 break;
29180 }
29181 }
29182
29183 glyph++; /* first glyph to the right of the highlighted area */
29184 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
29185 x += g->pixel_width;
29186 hlinfo->mouse_face_beg_x = x;
29187 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29188 }
29189
29190 /* If the highlight ends in a different row, compute GLYPH and END
29191 for the end row. Otherwise, reuse the values computed above for
29192 the row where the highlight begins. */
29193 if (r2 != r1)
29194 {
29195 if (!r2->reversed_p)
29196 {
29197 glyph = r2->glyphs[TEXT_AREA];
29198 end = glyph + r2->used[TEXT_AREA];
29199 x = r2->x;
29200 }
29201 else
29202 {
29203 end = r2->glyphs[TEXT_AREA] - 1;
29204 glyph = end + r2->used[TEXT_AREA];
29205 }
29206 }
29207
29208 if (!r2->reversed_p)
29209 {
29210 /* Skip truncation and continuation glyphs near the end of the
29211 row, and also blanks and stretch glyphs inserted by
29212 extend_face_to_end_of_line. */
29213 while (end > glyph
29214 && NILP ((end - 1)->object))
29215 --end;
29216 /* Scan the rest of the glyph row from the end, looking for the
29217 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29218 DISP_STRING, or whose position is between START_CHARPOS
29219 and END_CHARPOS */
29220 for (--end;
29221 end > glyph
29222 && !NILP (end->object)
29223 && !EQ (end->object, disp_string)
29224 && !(BUFFERP (end->object)
29225 && (end->charpos >= start_charpos
29226 && end->charpos < end_charpos));
29227 --end)
29228 {
29229 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29230 are present at buffer positions between START_CHARPOS and
29231 END_CHARPOS, or if they come from an overlay. */
29232 if (EQ (end->object, before_string))
29233 {
29234 pos = string_buffer_position (before_string, start_charpos);
29235 if (!pos || (pos >= start_charpos && pos < end_charpos))
29236 break;
29237 }
29238 else if (EQ (end->object, after_string))
29239 {
29240 pos = string_buffer_position (after_string, end_charpos);
29241 if (!pos || (pos >= start_charpos && pos < end_charpos))
29242 break;
29243 }
29244 }
29245 /* Find the X coordinate of the last glyph to be highlighted. */
29246 for (; glyph <= end; ++glyph)
29247 x += glyph->pixel_width;
29248
29249 hlinfo->mouse_face_end_x = x;
29250 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
29251 }
29252 else
29253 {
29254 /* Skip truncation and continuation glyphs near the end of the
29255 row, and also blanks and stretch glyphs inserted by
29256 extend_face_to_end_of_line. */
29257 x = r2->x;
29258 end++;
29259 while (end < glyph
29260 && NILP (end->object))
29261 {
29262 x += end->pixel_width;
29263 ++end;
29264 }
29265 /* Scan the rest of the glyph row from the end, looking for the
29266 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29267 DISP_STRING, or whose position is between START_CHARPOS
29268 and END_CHARPOS */
29269 for ( ;
29270 end < glyph
29271 && !NILP (end->object)
29272 && !EQ (end->object, disp_string)
29273 && !(BUFFERP (end->object)
29274 && (end->charpos >= start_charpos
29275 && end->charpos < end_charpos));
29276 ++end)
29277 {
29278 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29279 are present at buffer positions between START_CHARPOS and
29280 END_CHARPOS, or if they come from an overlay. */
29281 if (EQ (end->object, before_string))
29282 {
29283 pos = string_buffer_position (before_string, start_charpos);
29284 if (!pos || (pos >= start_charpos && pos < end_charpos))
29285 break;
29286 }
29287 else if (EQ (end->object, after_string))
29288 {
29289 pos = string_buffer_position (after_string, end_charpos);
29290 if (!pos || (pos >= start_charpos && pos < end_charpos))
29291 break;
29292 }
29293 x += end->pixel_width;
29294 }
29295 /* If we exited the above loop because we arrived at the last
29296 glyph of the row, and its buffer position is still not in
29297 range, it means the last character in range is the preceding
29298 newline. Bump the end column and x values to get past the
29299 last glyph. */
29300 if (end == glyph
29301 && BUFFERP (end->object)
29302 && (end->charpos < start_charpos
29303 || end->charpos >= end_charpos))
29304 {
29305 x += end->pixel_width;
29306 ++end;
29307 }
29308 hlinfo->mouse_face_end_x = x;
29309 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
29310 }
29311
29312 hlinfo->mouse_face_window = window;
29313 hlinfo->mouse_face_face_id
29314 = face_at_buffer_position (w, mouse_charpos, &ignore,
29315 mouse_charpos + 1,
29316 !hlinfo->mouse_face_hidden, -1);
29317 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29318 }
29319
29320 /* The following function is not used anymore (replaced with
29321 mouse_face_from_string_pos), but I leave it here for the time
29322 being, in case someone would. */
29323
29324 #if false /* not used */
29325
29326 /* Find the position of the glyph for position POS in OBJECT in
29327 window W's current matrix, and return in *X, *Y the pixel
29328 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
29329
29330 RIGHT_P means return the position of the right edge of the glyph.
29331 !RIGHT_P means return the left edge position.
29332
29333 If no glyph for POS exists in the matrix, return the position of
29334 the glyph with the next smaller position that is in the matrix, if
29335 RIGHT_P is false. If RIGHT_P, and no glyph for POS
29336 exists in the matrix, return the position of the glyph with the
29337 next larger position in OBJECT.
29338
29339 Value is true if a glyph was found. */
29340
29341 static bool
29342 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
29343 int *hpos, int *vpos, int *x, int *y, bool right_p)
29344 {
29345 int yb = window_text_bottom_y (w);
29346 struct glyph_row *r;
29347 struct glyph *best_glyph = NULL;
29348 struct glyph_row *best_row = NULL;
29349 int best_x = 0;
29350
29351 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29352 r->enabled_p && r->y < yb;
29353 ++r)
29354 {
29355 struct glyph *g = r->glyphs[TEXT_AREA];
29356 struct glyph *e = g + r->used[TEXT_AREA];
29357 int gx;
29358
29359 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29360 if (EQ (g->object, object))
29361 {
29362 if (g->charpos == pos)
29363 {
29364 best_glyph = g;
29365 best_x = gx;
29366 best_row = r;
29367 goto found;
29368 }
29369 else if (best_glyph == NULL
29370 || ((eabs (g->charpos - pos)
29371 < eabs (best_glyph->charpos - pos))
29372 && (right_p
29373 ? g->charpos < pos
29374 : g->charpos > pos)))
29375 {
29376 best_glyph = g;
29377 best_x = gx;
29378 best_row = r;
29379 }
29380 }
29381 }
29382
29383 found:
29384
29385 if (best_glyph)
29386 {
29387 *x = best_x;
29388 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
29389
29390 if (right_p)
29391 {
29392 *x += best_glyph->pixel_width;
29393 ++*hpos;
29394 }
29395
29396 *y = best_row->y;
29397 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
29398 }
29399
29400 return best_glyph != NULL;
29401 }
29402 #endif /* not used */
29403
29404 /* Find the positions of the first and the last glyphs in window W's
29405 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29406 (assumed to be a string), and return in HLINFO's mouse_face_*
29407 members the pixel and column/row coordinates of those glyphs. */
29408
29409 static void
29410 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29411 Lisp_Object object,
29412 ptrdiff_t startpos, ptrdiff_t endpos)
29413 {
29414 int yb = window_text_bottom_y (w);
29415 struct glyph_row *r;
29416 struct glyph *g, *e;
29417 int gx;
29418 bool found = false;
29419
29420 /* Find the glyph row with at least one position in the range
29421 [STARTPOS..ENDPOS), and the first glyph in that row whose
29422 position belongs to that range. */
29423 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29424 r->enabled_p && r->y < yb;
29425 ++r)
29426 {
29427 if (!r->reversed_p)
29428 {
29429 g = r->glyphs[TEXT_AREA];
29430 e = g + r->used[TEXT_AREA];
29431 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29432 if (EQ (g->object, object)
29433 && startpos <= g->charpos && g->charpos < endpos)
29434 {
29435 hlinfo->mouse_face_beg_row
29436 = MATRIX_ROW_VPOS (r, w->current_matrix);
29437 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29438 hlinfo->mouse_face_beg_x = gx;
29439 found = true;
29440 break;
29441 }
29442 }
29443 else
29444 {
29445 struct glyph *g1;
29446
29447 e = r->glyphs[TEXT_AREA];
29448 g = e + r->used[TEXT_AREA];
29449 for ( ; g > e; --g)
29450 if (EQ ((g-1)->object, object)
29451 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29452 {
29453 hlinfo->mouse_face_beg_row
29454 = MATRIX_ROW_VPOS (r, w->current_matrix);
29455 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29456 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29457 gx += g1->pixel_width;
29458 hlinfo->mouse_face_beg_x = gx;
29459 found = true;
29460 break;
29461 }
29462 }
29463 if (found)
29464 break;
29465 }
29466
29467 if (!found)
29468 return;
29469
29470 /* Starting with the next row, look for the first row which does NOT
29471 include any glyphs whose positions are in the range. */
29472 for (++r; r->enabled_p && r->y < yb; ++r)
29473 {
29474 g = r->glyphs[TEXT_AREA];
29475 e = g + r->used[TEXT_AREA];
29476 found = false;
29477 for ( ; g < e; ++g)
29478 if (EQ (g->object, object)
29479 && startpos <= g->charpos && g->charpos < endpos)
29480 {
29481 found = true;
29482 break;
29483 }
29484 if (!found)
29485 break;
29486 }
29487
29488 /* The highlighted region ends on the previous row. */
29489 r--;
29490
29491 /* Set the end row. */
29492 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29493
29494 /* Compute and set the end column and the end column's horizontal
29495 pixel coordinate. */
29496 if (!r->reversed_p)
29497 {
29498 g = r->glyphs[TEXT_AREA];
29499 e = g + r->used[TEXT_AREA];
29500 for ( ; e > g; --e)
29501 if (EQ ((e-1)->object, object)
29502 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29503 break;
29504 hlinfo->mouse_face_end_col = e - g;
29505
29506 for (gx = r->x; g < e; ++g)
29507 gx += g->pixel_width;
29508 hlinfo->mouse_face_end_x = gx;
29509 }
29510 else
29511 {
29512 e = r->glyphs[TEXT_AREA];
29513 g = e + r->used[TEXT_AREA];
29514 for (gx = r->x ; e < g; ++e)
29515 {
29516 if (EQ (e->object, object)
29517 && startpos <= e->charpos && e->charpos < endpos)
29518 break;
29519 gx += e->pixel_width;
29520 }
29521 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29522 hlinfo->mouse_face_end_x = gx;
29523 }
29524 }
29525
29526 #ifdef HAVE_WINDOW_SYSTEM
29527
29528 /* See if position X, Y is within a hot-spot of an image. */
29529
29530 static bool
29531 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29532 {
29533 if (!CONSP (hot_spot))
29534 return false;
29535
29536 if (EQ (XCAR (hot_spot), Qrect))
29537 {
29538 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29539 Lisp_Object rect = XCDR (hot_spot);
29540 Lisp_Object tem;
29541 if (!CONSP (rect))
29542 return false;
29543 if (!CONSP (XCAR (rect)))
29544 return false;
29545 if (!CONSP (XCDR (rect)))
29546 return false;
29547 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29548 return false;
29549 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29550 return false;
29551 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29552 return false;
29553 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29554 return false;
29555 return true;
29556 }
29557 else if (EQ (XCAR (hot_spot), Qcircle))
29558 {
29559 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29560 Lisp_Object circ = XCDR (hot_spot);
29561 Lisp_Object lr, lx0, ly0;
29562 if (CONSP (circ)
29563 && CONSP (XCAR (circ))
29564 && (lr = XCDR (circ), NUMBERP (lr))
29565 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29566 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29567 {
29568 double r = XFLOATINT (lr);
29569 double dx = XINT (lx0) - x;
29570 double dy = XINT (ly0) - y;
29571 return (dx * dx + dy * dy <= r * r);
29572 }
29573 }
29574 else if (EQ (XCAR (hot_spot), Qpoly))
29575 {
29576 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29577 if (VECTORP (XCDR (hot_spot)))
29578 {
29579 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29580 Lisp_Object *poly = v->contents;
29581 ptrdiff_t n = v->header.size;
29582 ptrdiff_t i;
29583 bool inside = false;
29584 Lisp_Object lx, ly;
29585 int x0, y0;
29586
29587 /* Need an even number of coordinates, and at least 3 edges. */
29588 if (n < 6 || n & 1)
29589 return false;
29590
29591 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29592 If count is odd, we are inside polygon. Pixels on edges
29593 may or may not be included depending on actual geometry of the
29594 polygon. */
29595 if ((lx = poly[n-2], !INTEGERP (lx))
29596 || (ly = poly[n-1], !INTEGERP (lx)))
29597 return false;
29598 x0 = XINT (lx), y0 = XINT (ly);
29599 for (i = 0; i < n; i += 2)
29600 {
29601 int x1 = x0, y1 = y0;
29602 if ((lx = poly[i], !INTEGERP (lx))
29603 || (ly = poly[i+1], !INTEGERP (ly)))
29604 return false;
29605 x0 = XINT (lx), y0 = XINT (ly);
29606
29607 /* Does this segment cross the X line? */
29608 if (x0 >= x)
29609 {
29610 if (x1 >= x)
29611 continue;
29612 }
29613 else if (x1 < x)
29614 continue;
29615 if (y > y0 && y > y1)
29616 continue;
29617 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29618 inside = !inside;
29619 }
29620 return inside;
29621 }
29622 }
29623 return false;
29624 }
29625
29626 Lisp_Object
29627 find_hot_spot (Lisp_Object map, int x, int y)
29628 {
29629 while (CONSP (map))
29630 {
29631 if (CONSP (XCAR (map))
29632 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29633 return XCAR (map);
29634 map = XCDR (map);
29635 }
29636
29637 return Qnil;
29638 }
29639
29640 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29641 3, 3, 0,
29642 doc: /* Lookup in image map MAP coordinates X and Y.
29643 An image map is an alist where each element has the format (AREA ID PLIST).
29644 An AREA is specified as either a rectangle, a circle, or a polygon:
29645 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29646 pixel coordinates of the upper left and bottom right corners.
29647 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29648 and the radius of the circle; r may be a float or integer.
29649 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29650 vector describes one corner in the polygon.
29651 Returns the alist element for the first matching AREA in MAP. */)
29652 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29653 {
29654 if (NILP (map))
29655 return Qnil;
29656
29657 CHECK_NUMBER (x);
29658 CHECK_NUMBER (y);
29659
29660 return find_hot_spot (map,
29661 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29662 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29663 }
29664 #endif /* HAVE_WINDOW_SYSTEM */
29665
29666
29667 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29668 static void
29669 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29670 {
29671 #ifdef HAVE_WINDOW_SYSTEM
29672 if (!FRAME_WINDOW_P (f))
29673 return;
29674
29675 /* Do not change cursor shape while dragging mouse. */
29676 if (EQ (do_mouse_tracking, Qdragging))
29677 return;
29678
29679 if (!NILP (pointer))
29680 {
29681 if (EQ (pointer, Qarrow))
29682 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29683 else if (EQ (pointer, Qhand))
29684 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29685 else if (EQ (pointer, Qtext))
29686 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29687 else if (EQ (pointer, intern ("hdrag")))
29688 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29689 else if (EQ (pointer, intern ("nhdrag")))
29690 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29691 # ifdef HAVE_X_WINDOWS
29692 else if (EQ (pointer, intern ("vdrag")))
29693 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29694 # endif
29695 else if (EQ (pointer, intern ("hourglass")))
29696 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29697 else if (EQ (pointer, Qmodeline))
29698 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29699 else
29700 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29701 }
29702
29703 if (cursor != No_Cursor)
29704 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29705 #endif
29706 }
29707
29708 /* Take proper action when mouse has moved to the mode or header line
29709 or marginal area AREA of window W, x-position X and y-position Y.
29710 X is relative to the start of the text display area of W, so the
29711 width of bitmap areas and scroll bars must be subtracted to get a
29712 position relative to the start of the mode line. */
29713
29714 static void
29715 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29716 enum window_part area)
29717 {
29718 struct window *w = XWINDOW (window);
29719 struct frame *f = XFRAME (w->frame);
29720 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29721 #ifdef HAVE_WINDOW_SYSTEM
29722 Display_Info *dpyinfo;
29723 #endif
29724 Cursor cursor = No_Cursor;
29725 Lisp_Object pointer = Qnil;
29726 int dx, dy, width, height;
29727 ptrdiff_t charpos;
29728 Lisp_Object string, object = Qnil;
29729 Lisp_Object pos UNINIT;
29730 Lisp_Object mouse_face;
29731 int original_x_pixel = x;
29732 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29733 struct glyph_row *row UNINIT;
29734
29735 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29736 {
29737 int x0;
29738 struct glyph *end;
29739
29740 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29741 returns them in row/column units! */
29742 string = mode_line_string (w, area, &x, &y, &charpos,
29743 &object, &dx, &dy, &width, &height);
29744
29745 row = (area == ON_MODE_LINE
29746 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29747 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29748
29749 /* Find the glyph under the mouse pointer. */
29750 if (row->mode_line_p && row->enabled_p)
29751 {
29752 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29753 end = glyph + row->used[TEXT_AREA];
29754
29755 for (x0 = original_x_pixel;
29756 glyph < end && x0 >= glyph->pixel_width;
29757 ++glyph)
29758 x0 -= glyph->pixel_width;
29759
29760 if (glyph >= end)
29761 glyph = NULL;
29762 }
29763 }
29764 else
29765 {
29766 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29767 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29768 returns them in row/column units! */
29769 string = marginal_area_string (w, area, &x, &y, &charpos,
29770 &object, &dx, &dy, &width, &height);
29771 }
29772
29773 Lisp_Object help = Qnil;
29774
29775 #ifdef HAVE_WINDOW_SYSTEM
29776 if (IMAGEP (object))
29777 {
29778 Lisp_Object image_map, hotspot;
29779 if ((image_map = Fplist_get (XCDR (object), QCmap),
29780 !NILP (image_map))
29781 && (hotspot = find_hot_spot (image_map, dx, dy),
29782 CONSP (hotspot))
29783 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29784 {
29785 Lisp_Object plist;
29786
29787 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29788 If so, we could look for mouse-enter, mouse-leave
29789 properties in PLIST (and do something...). */
29790 hotspot = XCDR (hotspot);
29791 if (CONSP (hotspot)
29792 && (plist = XCAR (hotspot), CONSP (plist)))
29793 {
29794 pointer = Fplist_get (plist, Qpointer);
29795 if (NILP (pointer))
29796 pointer = Qhand;
29797 help = Fplist_get (plist, Qhelp_echo);
29798 if (!NILP (help))
29799 {
29800 help_echo_string = help;
29801 XSETWINDOW (help_echo_window, w);
29802 help_echo_object = w->contents;
29803 help_echo_pos = charpos;
29804 }
29805 }
29806 }
29807 if (NILP (pointer))
29808 pointer = Fplist_get (XCDR (object), QCpointer);
29809 }
29810 #endif /* HAVE_WINDOW_SYSTEM */
29811
29812 if (STRINGP (string))
29813 pos = make_number (charpos);
29814
29815 /* Set the help text and mouse pointer. If the mouse is on a part
29816 of the mode line without any text (e.g. past the right edge of
29817 the mode line text), use the default help text and pointer. */
29818 if (STRINGP (string) || area == ON_MODE_LINE)
29819 {
29820 /* Arrange to display the help by setting the global variables
29821 help_echo_string, help_echo_object, and help_echo_pos. */
29822 if (NILP (help))
29823 {
29824 if (STRINGP (string))
29825 help = Fget_text_property (pos, Qhelp_echo, string);
29826
29827 if (!NILP (help))
29828 {
29829 help_echo_string = help;
29830 XSETWINDOW (help_echo_window, w);
29831 help_echo_object = string;
29832 help_echo_pos = charpos;
29833 }
29834 else if (area == ON_MODE_LINE)
29835 {
29836 Lisp_Object default_help
29837 = buffer_local_value (Qmode_line_default_help_echo,
29838 w->contents);
29839
29840 if (STRINGP (default_help))
29841 {
29842 help_echo_string = default_help;
29843 XSETWINDOW (help_echo_window, w);
29844 help_echo_object = Qnil;
29845 help_echo_pos = -1;
29846 }
29847 }
29848 }
29849
29850 #ifdef HAVE_WINDOW_SYSTEM
29851 /* Change the mouse pointer according to what is under it. */
29852 if (FRAME_WINDOW_P (f))
29853 {
29854 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29855 || minibuf_level
29856 || NILP (Vresize_mini_windows));
29857
29858 dpyinfo = FRAME_DISPLAY_INFO (f);
29859 if (STRINGP (string))
29860 {
29861 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29862
29863 if (NILP (pointer))
29864 pointer = Fget_text_property (pos, Qpointer, string);
29865
29866 /* Change the mouse pointer according to what is under X/Y. */
29867 if (NILP (pointer)
29868 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29869 {
29870 Lisp_Object map;
29871 map = Fget_text_property (pos, Qlocal_map, string);
29872 if (!KEYMAPP (map))
29873 map = Fget_text_property (pos, Qkeymap, string);
29874 if (!KEYMAPP (map) && draggable)
29875 cursor = dpyinfo->vertical_scroll_bar_cursor;
29876 }
29877 }
29878 else if (draggable)
29879 /* Default mode-line pointer. */
29880 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29881 }
29882 #endif
29883 }
29884
29885 /* Change the mouse face according to what is under X/Y. */
29886 bool mouse_face_shown = false;
29887 if (STRINGP (string))
29888 {
29889 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29890 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29891 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29892 && glyph)
29893 {
29894 Lisp_Object b, e;
29895
29896 struct glyph * tmp_glyph;
29897
29898 int gpos;
29899 int gseq_length;
29900 int total_pixel_width;
29901 ptrdiff_t begpos, endpos, ignore;
29902
29903 int vpos, hpos;
29904
29905 b = Fprevious_single_property_change (make_number (charpos + 1),
29906 Qmouse_face, string, Qnil);
29907 if (NILP (b))
29908 begpos = 0;
29909 else
29910 begpos = XINT (b);
29911
29912 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29913 if (NILP (e))
29914 endpos = SCHARS (string);
29915 else
29916 endpos = XINT (e);
29917
29918 /* Calculate the glyph position GPOS of GLYPH in the
29919 displayed string, relative to the beginning of the
29920 highlighted part of the string.
29921
29922 Note: GPOS is different from CHARPOS. CHARPOS is the
29923 position of GLYPH in the internal string object. A mode
29924 line string format has structures which are converted to
29925 a flattened string by the Emacs Lisp interpreter. The
29926 internal string is an element of those structures. The
29927 displayed string is the flattened string. */
29928 tmp_glyph = row_start_glyph;
29929 while (tmp_glyph < glyph
29930 && (!(EQ (tmp_glyph->object, glyph->object)
29931 && begpos <= tmp_glyph->charpos
29932 && tmp_glyph->charpos < endpos)))
29933 tmp_glyph++;
29934 gpos = glyph - tmp_glyph;
29935
29936 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29937 the highlighted part of the displayed string to which
29938 GLYPH belongs. Note: GSEQ_LENGTH is different from
29939 SCHARS (STRING), because the latter returns the length of
29940 the internal string. */
29941 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29942 tmp_glyph > glyph
29943 && (!(EQ (tmp_glyph->object, glyph->object)
29944 && begpos <= tmp_glyph->charpos
29945 && tmp_glyph->charpos < endpos));
29946 tmp_glyph--)
29947 ;
29948 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29949
29950 /* Calculate the total pixel width of all the glyphs between
29951 the beginning of the highlighted area and GLYPH. */
29952 total_pixel_width = 0;
29953 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29954 total_pixel_width += tmp_glyph->pixel_width;
29955
29956 /* Pre calculation of re-rendering position. Note: X is in
29957 column units here, after the call to mode_line_string or
29958 marginal_area_string. */
29959 hpos = x - gpos;
29960 vpos = (area == ON_MODE_LINE
29961 ? (w->current_matrix)->nrows - 1
29962 : 0);
29963
29964 /* If GLYPH's position is included in the region that is
29965 already drawn in mouse face, we have nothing to do. */
29966 if ( EQ (window, hlinfo->mouse_face_window)
29967 && (!row->reversed_p
29968 ? (hlinfo->mouse_face_beg_col <= hpos
29969 && hpos < hlinfo->mouse_face_end_col)
29970 /* In R2L rows we swap BEG and END, see below. */
29971 : (hlinfo->mouse_face_end_col <= hpos
29972 && hpos < hlinfo->mouse_face_beg_col))
29973 && hlinfo->mouse_face_beg_row == vpos )
29974 return;
29975
29976 if (clear_mouse_face (hlinfo))
29977 cursor = No_Cursor;
29978
29979 if (!row->reversed_p)
29980 {
29981 hlinfo->mouse_face_beg_col = hpos;
29982 hlinfo->mouse_face_beg_x = original_x_pixel
29983 - (total_pixel_width + dx);
29984 hlinfo->mouse_face_end_col = hpos + gseq_length;
29985 hlinfo->mouse_face_end_x = 0;
29986 }
29987 else
29988 {
29989 /* In R2L rows, show_mouse_face expects BEG and END
29990 coordinates to be swapped. */
29991 hlinfo->mouse_face_end_col = hpos;
29992 hlinfo->mouse_face_end_x = original_x_pixel
29993 - (total_pixel_width + dx);
29994 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29995 hlinfo->mouse_face_beg_x = 0;
29996 }
29997
29998 hlinfo->mouse_face_beg_row = vpos;
29999 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
30000 hlinfo->mouse_face_past_end = false;
30001 hlinfo->mouse_face_window = window;
30002
30003 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
30004 charpos,
30005 0, &ignore,
30006 glyph->face_id,
30007 true);
30008 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30009 mouse_face_shown = true;
30010
30011 if (NILP (pointer))
30012 pointer = Qhand;
30013 }
30014 }
30015
30016 /* If mouse-face doesn't need to be shown, clear any existing
30017 mouse-face. */
30018 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
30019 clear_mouse_face (hlinfo);
30020
30021 define_frame_cursor1 (f, cursor, pointer);
30022 }
30023
30024
30025 /* EXPORT:
30026 Take proper action when the mouse has moved to position X, Y on
30027 frame F with regards to highlighting portions of display that have
30028 mouse-face properties. Also de-highlight portions of display where
30029 the mouse was before, set the mouse pointer shape as appropriate
30030 for the mouse coordinates, and activate help echo (tooltips).
30031 X and Y can be negative or out of range. */
30032
30033 void
30034 note_mouse_highlight (struct frame *f, int x, int y)
30035 {
30036 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30037 enum window_part part = ON_NOTHING;
30038 Lisp_Object window;
30039 struct window *w;
30040 Cursor cursor = No_Cursor;
30041 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
30042 struct buffer *b;
30043
30044 /* When a menu is active, don't highlight because this looks odd. */
30045 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
30046 if (popup_activated ())
30047 return;
30048 #endif
30049
30050 if (!f->glyphs_initialized_p
30051 || f->pointer_invisible)
30052 return;
30053
30054 hlinfo->mouse_face_mouse_x = x;
30055 hlinfo->mouse_face_mouse_y = y;
30056 hlinfo->mouse_face_mouse_frame = f;
30057
30058 if (hlinfo->mouse_face_defer)
30059 return;
30060
30061 /* Which window is that in? */
30062 window = window_from_coordinates (f, x, y, &part, true);
30063
30064 /* If displaying active text in another window, clear that. */
30065 if (! EQ (window, hlinfo->mouse_face_window)
30066 /* Also clear if we move out of text area in same window. */
30067 || (!NILP (hlinfo->mouse_face_window)
30068 && !NILP (window)
30069 && part != ON_TEXT
30070 && part != ON_MODE_LINE
30071 && part != ON_HEADER_LINE))
30072 clear_mouse_face (hlinfo);
30073
30074 /* Not on a window -> return. */
30075 if (!WINDOWP (window))
30076 return;
30077
30078 /* Reset help_echo_string. It will get recomputed below. */
30079 help_echo_string = Qnil;
30080
30081 /* Convert to window-relative pixel coordinates. */
30082 w = XWINDOW (window);
30083 frame_to_window_pixel_xy (w, &x, &y);
30084
30085 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
30086 /* Handle tool-bar window differently since it doesn't display a
30087 buffer. */
30088 if (EQ (window, f->tool_bar_window))
30089 {
30090 note_tool_bar_highlight (f, x, y);
30091 return;
30092 }
30093 #endif
30094
30095 /* Mouse is on the mode, header line or margin? */
30096 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
30097 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30098 {
30099 note_mode_line_or_margin_highlight (window, x, y, part);
30100
30101 #ifdef HAVE_WINDOW_SYSTEM
30102 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30103 {
30104 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30105 /* Show non-text cursor (Bug#16647). */
30106 goto set_cursor;
30107 }
30108 else
30109 #endif
30110 return;
30111 }
30112
30113 #ifdef HAVE_WINDOW_SYSTEM
30114 if (part == ON_VERTICAL_BORDER)
30115 {
30116 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30117 help_echo_string = build_string ("drag-mouse-1: resize");
30118 }
30119 else if (part == ON_RIGHT_DIVIDER)
30120 {
30121 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30122 help_echo_string = build_string ("drag-mouse-1: resize");
30123 }
30124 else if (part == ON_BOTTOM_DIVIDER)
30125 if (! WINDOW_BOTTOMMOST_P (w)
30126 || minibuf_level
30127 || NILP (Vresize_mini_windows))
30128 {
30129 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30130 help_echo_string = build_string ("drag-mouse-1: resize");
30131 }
30132 else
30133 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30134 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
30135 || part == ON_VERTICAL_SCROLL_BAR
30136 || part == ON_HORIZONTAL_SCROLL_BAR)
30137 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30138 else
30139 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30140 #endif
30141
30142 /* Are we in a window whose display is up to date?
30143 And verify the buffer's text has not changed. */
30144 b = XBUFFER (w->contents);
30145 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
30146 {
30147 int hpos, vpos, dx, dy, area = LAST_AREA;
30148 ptrdiff_t pos;
30149 struct glyph *glyph;
30150 Lisp_Object object;
30151 Lisp_Object mouse_face = Qnil, position;
30152 Lisp_Object *overlay_vec = NULL;
30153 ptrdiff_t i, noverlays;
30154 struct buffer *obuf;
30155 ptrdiff_t obegv, ozv;
30156 bool same_region;
30157
30158 /* Find the glyph under X/Y. */
30159 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
30160
30161 #ifdef HAVE_WINDOW_SYSTEM
30162 /* Look for :pointer property on image. */
30163 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
30164 {
30165 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
30166 if (img != NULL && IMAGEP (img->spec))
30167 {
30168 Lisp_Object image_map, hotspot;
30169 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
30170 !NILP (image_map))
30171 && (hotspot = find_hot_spot (image_map,
30172 glyph->slice.img.x + dx,
30173 glyph->slice.img.y + dy),
30174 CONSP (hotspot))
30175 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30176 {
30177 Lisp_Object plist;
30178
30179 /* Could check XCAR (hotspot) to see if we enter/leave
30180 this hot-spot.
30181 If so, we could look for mouse-enter, mouse-leave
30182 properties in PLIST (and do something...). */
30183 hotspot = XCDR (hotspot);
30184 if (CONSP (hotspot)
30185 && (plist = XCAR (hotspot), CONSP (plist)))
30186 {
30187 pointer = Fplist_get (plist, Qpointer);
30188 if (NILP (pointer))
30189 pointer = Qhand;
30190 help_echo_string = Fplist_get (plist, Qhelp_echo);
30191 if (!NILP (help_echo_string))
30192 {
30193 help_echo_window = window;
30194 help_echo_object = glyph->object;
30195 help_echo_pos = glyph->charpos;
30196 }
30197 }
30198 }
30199 if (NILP (pointer))
30200 pointer = Fplist_get (XCDR (img->spec), QCpointer);
30201 }
30202 }
30203 #endif /* HAVE_WINDOW_SYSTEM */
30204
30205 /* Clear mouse face if X/Y not over text. */
30206 if (glyph == NULL
30207 || area != TEXT_AREA
30208 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
30209 /* Glyph's OBJECT is nil for glyphs inserted by the
30210 display engine for its internal purposes, like truncation
30211 and continuation glyphs and blanks beyond the end of
30212 line's text on text terminals. If we are over such a
30213 glyph, we are not over any text. */
30214 || NILP (glyph->object)
30215 /* R2L rows have a stretch glyph at their front, which
30216 stands for no text, whereas L2R rows have no glyphs at
30217 all beyond the end of text. Treat such stretch glyphs
30218 like we do with NULL glyphs in L2R rows. */
30219 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
30220 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
30221 && glyph->type == STRETCH_GLYPH
30222 && glyph->avoid_cursor_p))
30223 {
30224 if (clear_mouse_face (hlinfo))
30225 cursor = No_Cursor;
30226 if (FRAME_WINDOW_P (f) && NILP (pointer))
30227 {
30228 #ifdef HAVE_WINDOW_SYSTEM
30229 if (area != TEXT_AREA)
30230 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30231 else
30232 pointer = Vvoid_text_area_pointer;
30233 #endif
30234 }
30235 goto set_cursor;
30236 }
30237
30238 pos = glyph->charpos;
30239 object = glyph->object;
30240 if (!STRINGP (object) && !BUFFERP (object))
30241 goto set_cursor;
30242
30243 /* If we get an out-of-range value, return now; avoid an error. */
30244 if (BUFFERP (object) && pos > BUF_Z (b))
30245 goto set_cursor;
30246
30247 /* Make the window's buffer temporarily current for
30248 overlays_at and compute_char_face. */
30249 obuf = current_buffer;
30250 current_buffer = b;
30251 obegv = BEGV;
30252 ozv = ZV;
30253 BEGV = BEG;
30254 ZV = Z;
30255
30256 /* Is this char mouse-active or does it have help-echo? */
30257 position = make_number (pos);
30258
30259 USE_SAFE_ALLOCA;
30260
30261 if (BUFFERP (object))
30262 {
30263 /* Put all the overlays we want in a vector in overlay_vec. */
30264 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
30265 /* Sort overlays into increasing priority order. */
30266 noverlays = sort_overlays (overlay_vec, noverlays, w);
30267 }
30268 else
30269 noverlays = 0;
30270
30271 if (NILP (Vmouse_highlight))
30272 {
30273 clear_mouse_face (hlinfo);
30274 goto check_help_echo;
30275 }
30276
30277 same_region = coords_in_mouse_face_p (w, hpos, vpos);
30278
30279 if (same_region)
30280 cursor = No_Cursor;
30281
30282 /* Check mouse-face highlighting. */
30283 if (! same_region
30284 /* If there exists an overlay with mouse-face overlapping
30285 the one we are currently highlighting, we have to
30286 check if we enter the overlapping overlay, and then
30287 highlight only that. */
30288 || (OVERLAYP (hlinfo->mouse_face_overlay)
30289 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
30290 {
30291 /* Find the highest priority overlay with a mouse-face. */
30292 Lisp_Object overlay = Qnil;
30293 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
30294 {
30295 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
30296 if (!NILP (mouse_face))
30297 overlay = overlay_vec[i];
30298 }
30299
30300 /* If we're highlighting the same overlay as before, there's
30301 no need to do that again. */
30302 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
30303 goto check_help_echo;
30304 hlinfo->mouse_face_overlay = overlay;
30305
30306 /* Clear the display of the old active region, if any. */
30307 if (clear_mouse_face (hlinfo))
30308 cursor = No_Cursor;
30309
30310 /* If no overlay applies, get a text property. */
30311 if (NILP (overlay))
30312 mouse_face = Fget_text_property (position, Qmouse_face, object);
30313
30314 /* Next, compute the bounds of the mouse highlighting and
30315 display it. */
30316 if (!NILP (mouse_face) && STRINGP (object))
30317 {
30318 /* The mouse-highlighting comes from a display string
30319 with a mouse-face. */
30320 Lisp_Object s, e;
30321 ptrdiff_t ignore;
30322
30323 s = Fprevious_single_property_change
30324 (make_number (pos + 1), Qmouse_face, object, Qnil);
30325 e = Fnext_single_property_change
30326 (position, Qmouse_face, object, Qnil);
30327 if (NILP (s))
30328 s = make_number (0);
30329 if (NILP (e))
30330 e = make_number (SCHARS (object));
30331 mouse_face_from_string_pos (w, hlinfo, object,
30332 XINT (s), XINT (e));
30333 hlinfo->mouse_face_past_end = false;
30334 hlinfo->mouse_face_window = window;
30335 hlinfo->mouse_face_face_id
30336 = face_at_string_position (w, object, pos, 0, &ignore,
30337 glyph->face_id, true);
30338 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30339 cursor = No_Cursor;
30340 }
30341 else
30342 {
30343 /* The mouse-highlighting, if any, comes from an overlay
30344 or text property in the buffer. */
30345 Lisp_Object buffer UNINIT;
30346 Lisp_Object disp_string UNINIT;
30347
30348 if (STRINGP (object))
30349 {
30350 /* If we are on a display string with no mouse-face,
30351 check if the text under it has one. */
30352 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
30353 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30354 pos = string_buffer_position (object, start);
30355 if (pos > 0)
30356 {
30357 mouse_face = get_char_property_and_overlay
30358 (make_number (pos), Qmouse_face, w->contents, &overlay);
30359 buffer = w->contents;
30360 disp_string = object;
30361 }
30362 }
30363 else
30364 {
30365 buffer = object;
30366 disp_string = Qnil;
30367 }
30368
30369 if (!NILP (mouse_face))
30370 {
30371 Lisp_Object before, after;
30372 Lisp_Object before_string, after_string;
30373 /* To correctly find the limits of mouse highlight
30374 in a bidi-reordered buffer, we must not use the
30375 optimization of limiting the search in
30376 previous-single-property-change and
30377 next-single-property-change, because
30378 rows_from_pos_range needs the real start and end
30379 positions to DTRT in this case. That's because
30380 the first row visible in a window does not
30381 necessarily display the character whose position
30382 is the smallest. */
30383 Lisp_Object lim1
30384 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30385 ? Fmarker_position (w->start)
30386 : Qnil;
30387 Lisp_Object lim2
30388 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30389 ? make_number (BUF_Z (XBUFFER (buffer))
30390 - w->window_end_pos)
30391 : Qnil;
30392
30393 if (NILP (overlay))
30394 {
30395 /* Handle the text property case. */
30396 before = Fprevious_single_property_change
30397 (make_number (pos + 1), Qmouse_face, buffer, lim1);
30398 after = Fnext_single_property_change
30399 (make_number (pos), Qmouse_face, buffer, lim2);
30400 before_string = after_string = Qnil;
30401 }
30402 else
30403 {
30404 /* Handle the overlay case. */
30405 before = Foverlay_start (overlay);
30406 after = Foverlay_end (overlay);
30407 before_string = Foverlay_get (overlay, Qbefore_string);
30408 after_string = Foverlay_get (overlay, Qafter_string);
30409
30410 if (!STRINGP (before_string)) before_string = Qnil;
30411 if (!STRINGP (after_string)) after_string = Qnil;
30412 }
30413
30414 mouse_face_from_buffer_pos (window, hlinfo, pos,
30415 NILP (before)
30416 ? 1
30417 : XFASTINT (before),
30418 NILP (after)
30419 ? BUF_Z (XBUFFER (buffer))
30420 : XFASTINT (after),
30421 before_string, after_string,
30422 disp_string);
30423 cursor = No_Cursor;
30424 }
30425 }
30426 }
30427
30428 check_help_echo:
30429
30430 /* Look for a `help-echo' property. */
30431 if (NILP (help_echo_string)) {
30432 Lisp_Object help, overlay;
30433
30434 /* Check overlays first. */
30435 help = overlay = Qnil;
30436 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30437 {
30438 overlay = overlay_vec[i];
30439 help = Foverlay_get (overlay, Qhelp_echo);
30440 }
30441
30442 if (!NILP (help))
30443 {
30444 help_echo_string = help;
30445 help_echo_window = window;
30446 help_echo_object = overlay;
30447 help_echo_pos = pos;
30448 }
30449 else
30450 {
30451 Lisp_Object obj = glyph->object;
30452 ptrdiff_t charpos = glyph->charpos;
30453
30454 /* Try text properties. */
30455 if (STRINGP (obj)
30456 && charpos >= 0
30457 && charpos < SCHARS (obj))
30458 {
30459 help = Fget_text_property (make_number (charpos),
30460 Qhelp_echo, obj);
30461 if (NILP (help))
30462 {
30463 /* If the string itself doesn't specify a help-echo,
30464 see if the buffer text ``under'' it does. */
30465 struct glyph_row *r
30466 = MATRIX_ROW (w->current_matrix, vpos);
30467 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30468 ptrdiff_t p = string_buffer_position (obj, start);
30469 if (p > 0)
30470 {
30471 help = Fget_char_property (make_number (p),
30472 Qhelp_echo, w->contents);
30473 if (!NILP (help))
30474 {
30475 charpos = p;
30476 obj = w->contents;
30477 }
30478 }
30479 }
30480 }
30481 else if (BUFFERP (obj)
30482 && charpos >= BEGV
30483 && charpos < ZV)
30484 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30485 obj);
30486
30487 if (!NILP (help))
30488 {
30489 help_echo_string = help;
30490 help_echo_window = window;
30491 help_echo_object = obj;
30492 help_echo_pos = charpos;
30493 }
30494 }
30495 }
30496
30497 #ifdef HAVE_WINDOW_SYSTEM
30498 /* Look for a `pointer' property. */
30499 if (FRAME_WINDOW_P (f) && NILP (pointer))
30500 {
30501 /* Check overlays first. */
30502 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30503 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30504
30505 if (NILP (pointer))
30506 {
30507 Lisp_Object obj = glyph->object;
30508 ptrdiff_t charpos = glyph->charpos;
30509
30510 /* Try text properties. */
30511 if (STRINGP (obj)
30512 && charpos >= 0
30513 && charpos < SCHARS (obj))
30514 {
30515 pointer = Fget_text_property (make_number (charpos),
30516 Qpointer, obj);
30517 if (NILP (pointer))
30518 {
30519 /* If the string itself doesn't specify a pointer,
30520 see if the buffer text ``under'' it does. */
30521 struct glyph_row *r
30522 = MATRIX_ROW (w->current_matrix, vpos);
30523 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30524 ptrdiff_t p = string_buffer_position (obj, start);
30525 if (p > 0)
30526 pointer = Fget_char_property (make_number (p),
30527 Qpointer, w->contents);
30528 }
30529 }
30530 else if (BUFFERP (obj)
30531 && charpos >= BEGV
30532 && charpos < ZV)
30533 pointer = Fget_text_property (make_number (charpos),
30534 Qpointer, obj);
30535 }
30536 }
30537 #endif /* HAVE_WINDOW_SYSTEM */
30538
30539 BEGV = obegv;
30540 ZV = ozv;
30541 current_buffer = obuf;
30542 SAFE_FREE ();
30543 }
30544
30545 set_cursor:
30546 define_frame_cursor1 (f, cursor, pointer);
30547 }
30548
30549
30550 /* EXPORT for RIF:
30551 Clear any mouse-face on window W. This function is part of the
30552 redisplay interface, and is called from try_window_id and similar
30553 functions to ensure the mouse-highlight is off. */
30554
30555 void
30556 x_clear_window_mouse_face (struct window *w)
30557 {
30558 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30559 Lisp_Object window;
30560
30561 block_input ();
30562 XSETWINDOW (window, w);
30563 if (EQ (window, hlinfo->mouse_face_window))
30564 clear_mouse_face (hlinfo);
30565 unblock_input ();
30566 }
30567
30568
30569 /* EXPORT:
30570 Just discard the mouse face information for frame F, if any.
30571 This is used when the size of F is changed. */
30572
30573 void
30574 cancel_mouse_face (struct frame *f)
30575 {
30576 Lisp_Object window;
30577 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30578
30579 window = hlinfo->mouse_face_window;
30580 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30581 reset_mouse_highlight (hlinfo);
30582 }
30583
30584
30585 \f
30586 /***********************************************************************
30587 Exposure Events
30588 ***********************************************************************/
30589
30590 #ifdef HAVE_WINDOW_SYSTEM
30591
30592 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30593 which intersects rectangle R. R is in window-relative coordinates. */
30594
30595 static void
30596 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30597 enum glyph_row_area area)
30598 {
30599 struct glyph *first = row->glyphs[area];
30600 struct glyph *end = row->glyphs[area] + row->used[area];
30601 struct glyph *last;
30602 int first_x, start_x, x;
30603
30604 if (area == TEXT_AREA && row->fill_line_p)
30605 /* If row extends face to end of line write the whole line. */
30606 draw_glyphs (w, 0, row, area,
30607 0, row->used[area],
30608 DRAW_NORMAL_TEXT, 0);
30609 else
30610 {
30611 /* Set START_X to the window-relative start position for drawing glyphs of
30612 AREA. The first glyph of the text area can be partially visible.
30613 The first glyphs of other areas cannot. */
30614 start_x = window_box_left_offset (w, area);
30615 x = start_x;
30616 if (area == TEXT_AREA)
30617 x += row->x;
30618
30619 /* Find the first glyph that must be redrawn. */
30620 while (first < end
30621 && x + first->pixel_width < r->x)
30622 {
30623 x += first->pixel_width;
30624 ++first;
30625 }
30626
30627 /* Find the last one. */
30628 last = first;
30629 first_x = x;
30630 /* Use a signed int intermediate value to avoid catastrophic
30631 failures due to comparison between signed and unsigned, when
30632 x is negative (can happen for wide images that are hscrolled). */
30633 int r_end = r->x + r->width;
30634 while (last < end && x < r_end)
30635 {
30636 x += last->pixel_width;
30637 ++last;
30638 }
30639
30640 /* Repaint. */
30641 if (last > first)
30642 draw_glyphs (w, first_x - start_x, row, area,
30643 first - row->glyphs[area], last - row->glyphs[area],
30644 DRAW_NORMAL_TEXT, 0);
30645 }
30646 }
30647
30648
30649 /* Redraw the parts of the glyph row ROW on window W intersecting
30650 rectangle R. R is in window-relative coordinates. Value is
30651 true if mouse-face was overwritten. */
30652
30653 static bool
30654 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30655 {
30656 eassert (row->enabled_p);
30657
30658 if (row->mode_line_p || w->pseudo_window_p)
30659 draw_glyphs (w, 0, row, TEXT_AREA,
30660 0, row->used[TEXT_AREA],
30661 DRAW_NORMAL_TEXT, 0);
30662 else
30663 {
30664 if (row->used[LEFT_MARGIN_AREA])
30665 expose_area (w, row, r, LEFT_MARGIN_AREA);
30666 if (row->used[TEXT_AREA])
30667 expose_area (w, row, r, TEXT_AREA);
30668 if (row->used[RIGHT_MARGIN_AREA])
30669 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30670 draw_row_fringe_bitmaps (w, row);
30671 }
30672
30673 return row->mouse_face_p;
30674 }
30675
30676
30677 /* Redraw those parts of glyphs rows during expose event handling that
30678 overlap other rows. Redrawing of an exposed line writes over parts
30679 of lines overlapping that exposed line; this function fixes that.
30680
30681 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30682 row in W's current matrix that is exposed and overlaps other rows.
30683 LAST_OVERLAPPING_ROW is the last such row. */
30684
30685 static void
30686 expose_overlaps (struct window *w,
30687 struct glyph_row *first_overlapping_row,
30688 struct glyph_row *last_overlapping_row,
30689 XRectangle *r)
30690 {
30691 struct glyph_row *row;
30692
30693 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30694 if (row->overlapping_p)
30695 {
30696 eassert (row->enabled_p && !row->mode_line_p);
30697
30698 row->clip = r;
30699 if (row->used[LEFT_MARGIN_AREA])
30700 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30701
30702 if (row->used[TEXT_AREA])
30703 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30704
30705 if (row->used[RIGHT_MARGIN_AREA])
30706 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30707 row->clip = NULL;
30708 }
30709 }
30710
30711
30712 /* Return true if W's cursor intersects rectangle R. */
30713
30714 static bool
30715 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30716 {
30717 XRectangle cr, result;
30718 struct glyph *cursor_glyph;
30719 struct glyph_row *row;
30720
30721 if (w->phys_cursor.vpos >= 0
30722 && w->phys_cursor.vpos < w->current_matrix->nrows
30723 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30724 row->enabled_p)
30725 && row->cursor_in_fringe_p)
30726 {
30727 /* Cursor is in the fringe. */
30728 cr.x = window_box_right_offset (w,
30729 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30730 ? RIGHT_MARGIN_AREA
30731 : TEXT_AREA));
30732 cr.y = row->y;
30733 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30734 cr.height = row->height;
30735 return x_intersect_rectangles (&cr, r, &result);
30736 }
30737
30738 cursor_glyph = get_phys_cursor_glyph (w);
30739 if (cursor_glyph)
30740 {
30741 /* r is relative to W's box, but w->phys_cursor.x is relative
30742 to left edge of W's TEXT area. Adjust it. */
30743 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30744 cr.y = w->phys_cursor.y;
30745 cr.width = cursor_glyph->pixel_width;
30746 cr.height = w->phys_cursor_height;
30747 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30748 I assume the effect is the same -- and this is portable. */
30749 return x_intersect_rectangles (&cr, r, &result);
30750 }
30751 /* If we don't understand the format, pretend we're not in the hot-spot. */
30752 return false;
30753 }
30754
30755
30756 /* EXPORT:
30757 Draw a vertical window border to the right of window W if W doesn't
30758 have vertical scroll bars. */
30759
30760 void
30761 x_draw_vertical_border (struct window *w)
30762 {
30763 struct frame *f = XFRAME (WINDOW_FRAME (w));
30764
30765 /* We could do better, if we knew what type of scroll-bar the adjacent
30766 windows (on either side) have... But we don't :-(
30767 However, I think this works ok. ++KFS 2003-04-25 */
30768
30769 /* Redraw borders between horizontally adjacent windows. Don't
30770 do it for frames with vertical scroll bars because either the
30771 right scroll bar of a window, or the left scroll bar of its
30772 neighbor will suffice as a border. */
30773 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30774 return;
30775
30776 /* Note: It is necessary to redraw both the left and the right
30777 borders, for when only this single window W is being
30778 redisplayed. */
30779 if (!WINDOW_RIGHTMOST_P (w)
30780 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30781 {
30782 int x0, x1, y0, y1;
30783
30784 window_box_edges (w, &x0, &y0, &x1, &y1);
30785 y1 -= 1;
30786
30787 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30788 x1 -= 1;
30789
30790 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30791 }
30792
30793 if (!WINDOW_LEFTMOST_P (w)
30794 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30795 {
30796 int x0, x1, y0, y1;
30797
30798 window_box_edges (w, &x0, &y0, &x1, &y1);
30799 y1 -= 1;
30800
30801 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30802 x0 -= 1;
30803
30804 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30805 }
30806 }
30807
30808
30809 /* Draw window dividers for window W. */
30810
30811 void
30812 x_draw_right_divider (struct window *w)
30813 {
30814 struct frame *f = WINDOW_XFRAME (w);
30815
30816 if (w->mini || w->pseudo_window_p)
30817 return;
30818 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30819 {
30820 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30821 int x1 = WINDOW_RIGHT_EDGE_X (w);
30822 int y0 = WINDOW_TOP_EDGE_Y (w);
30823 /* The bottom divider prevails. */
30824 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30825
30826 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30827 }
30828 }
30829
30830 static void
30831 x_draw_bottom_divider (struct window *w)
30832 {
30833 struct frame *f = XFRAME (WINDOW_FRAME (w));
30834
30835 if (w->mini || w->pseudo_window_p)
30836 return;
30837 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30838 {
30839 int x0 = WINDOW_LEFT_EDGE_X (w);
30840 int x1 = WINDOW_RIGHT_EDGE_X (w);
30841 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30842 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30843
30844 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30845 }
30846 }
30847
30848 /* Redraw the part of window W intersection rectangle FR. Pixel
30849 coordinates in FR are frame-relative. Call this function with
30850 input blocked. Value is true if the exposure overwrites
30851 mouse-face. */
30852
30853 static bool
30854 expose_window (struct window *w, XRectangle *fr)
30855 {
30856 struct frame *f = XFRAME (w->frame);
30857 XRectangle wr, r;
30858 bool mouse_face_overwritten_p = false;
30859
30860 /* If window is not yet fully initialized, do nothing. This can
30861 happen when toolkit scroll bars are used and a window is split.
30862 Reconfiguring the scroll bar will generate an expose for a newly
30863 created window. */
30864 if (w->current_matrix == NULL)
30865 return false;
30866
30867 /* When we're currently updating the window, display and current
30868 matrix usually don't agree. Arrange for a thorough display
30869 later. */
30870 if (w->must_be_updated_p)
30871 {
30872 SET_FRAME_GARBAGED (f);
30873 return false;
30874 }
30875
30876 /* Frame-relative pixel rectangle of W. */
30877 wr.x = WINDOW_LEFT_EDGE_X (w);
30878 wr.y = WINDOW_TOP_EDGE_Y (w);
30879 wr.width = WINDOW_PIXEL_WIDTH (w);
30880 wr.height = WINDOW_PIXEL_HEIGHT (w);
30881
30882 if (x_intersect_rectangles (fr, &wr, &r))
30883 {
30884 int yb = window_text_bottom_y (w);
30885 struct glyph_row *row;
30886 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30887
30888 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30889 r.x, r.y, r.width, r.height));
30890
30891 /* Convert to window coordinates. */
30892 r.x -= WINDOW_LEFT_EDGE_X (w);
30893 r.y -= WINDOW_TOP_EDGE_Y (w);
30894
30895 /* Turn off the cursor. */
30896 bool cursor_cleared_p = (!w->pseudo_window_p
30897 && phys_cursor_in_rect_p (w, &r));
30898 if (cursor_cleared_p)
30899 x_clear_cursor (w);
30900
30901 /* If the row containing the cursor extends face to end of line,
30902 then expose_area might overwrite the cursor outside the
30903 rectangle and thus notice_overwritten_cursor might clear
30904 w->phys_cursor_on_p. We remember the original value and
30905 check later if it is changed. */
30906 bool phys_cursor_on_p = w->phys_cursor_on_p;
30907
30908 /* Use a signed int intermediate value to avoid catastrophic
30909 failures due to comparison between signed and unsigned, when
30910 y0 or y1 is negative (can happen for tall images). */
30911 int r_bottom = r.y + r.height;
30912
30913 /* Update lines intersecting rectangle R. */
30914 first_overlapping_row = last_overlapping_row = NULL;
30915 for (row = w->current_matrix->rows;
30916 row->enabled_p;
30917 ++row)
30918 {
30919 int y0 = row->y;
30920 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30921
30922 if ((y0 >= r.y && y0 < r_bottom)
30923 || (y1 > r.y && y1 < r_bottom)
30924 || (r.y >= y0 && r.y < y1)
30925 || (r_bottom > y0 && r_bottom < y1))
30926 {
30927 /* A header line may be overlapping, but there is no need
30928 to fix overlapping areas for them. KFS 2005-02-12 */
30929 if (row->overlapping_p && !row->mode_line_p)
30930 {
30931 if (first_overlapping_row == NULL)
30932 first_overlapping_row = row;
30933 last_overlapping_row = row;
30934 }
30935
30936 row->clip = fr;
30937 if (expose_line (w, row, &r))
30938 mouse_face_overwritten_p = true;
30939 row->clip = NULL;
30940 }
30941 else if (row->overlapping_p)
30942 {
30943 /* We must redraw a row overlapping the exposed area. */
30944 if (y0 < r.y
30945 ? y0 + row->phys_height > r.y
30946 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30947 {
30948 if (first_overlapping_row == NULL)
30949 first_overlapping_row = row;
30950 last_overlapping_row = row;
30951 }
30952 }
30953
30954 if (y1 >= yb)
30955 break;
30956 }
30957
30958 /* Display the mode line if there is one. */
30959 if (WINDOW_WANTS_MODELINE_P (w)
30960 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30961 row->enabled_p)
30962 && row->y < r_bottom)
30963 {
30964 if (expose_line (w, row, &r))
30965 mouse_face_overwritten_p = true;
30966 }
30967
30968 if (!w->pseudo_window_p)
30969 {
30970 /* Fix the display of overlapping rows. */
30971 if (first_overlapping_row)
30972 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30973 fr);
30974
30975 /* Draw border between windows. */
30976 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30977 x_draw_right_divider (w);
30978 else
30979 x_draw_vertical_border (w);
30980
30981 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30982 x_draw_bottom_divider (w);
30983
30984 /* Turn the cursor on again. */
30985 if (cursor_cleared_p
30986 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30987 update_window_cursor (w, true);
30988 }
30989 }
30990
30991 return mouse_face_overwritten_p;
30992 }
30993
30994
30995
30996 /* Redraw (parts) of all windows in the window tree rooted at W that
30997 intersect R. R contains frame pixel coordinates. Value is
30998 true if the exposure overwrites mouse-face. */
30999
31000 static bool
31001 expose_window_tree (struct window *w, XRectangle *r)
31002 {
31003 struct frame *f = XFRAME (w->frame);
31004 bool mouse_face_overwritten_p = false;
31005
31006 while (w && !FRAME_GARBAGED_P (f))
31007 {
31008 mouse_face_overwritten_p
31009 |= (WINDOWP (w->contents)
31010 ? expose_window_tree (XWINDOW (w->contents), r)
31011 : expose_window (w, r));
31012
31013 w = NILP (w->next) ? NULL : XWINDOW (w->next);
31014 }
31015
31016 return mouse_face_overwritten_p;
31017 }
31018
31019
31020 /* EXPORT:
31021 Redisplay an exposed area of frame F. X and Y are the upper-left
31022 corner of the exposed rectangle. W and H are width and height of
31023 the exposed area. All are pixel values. W or H zero means redraw
31024 the entire frame. */
31025
31026 void
31027 expose_frame (struct frame *f, int x, int y, int w, int h)
31028 {
31029 XRectangle r;
31030 bool mouse_face_overwritten_p = false;
31031
31032 TRACE ((stderr, "expose_frame "));
31033
31034 /* No need to redraw if frame will be redrawn soon. */
31035 if (FRAME_GARBAGED_P (f))
31036 {
31037 TRACE ((stderr, " garbaged\n"));
31038 return;
31039 }
31040
31041 /* If basic faces haven't been realized yet, there is no point in
31042 trying to redraw anything. This can happen when we get an expose
31043 event while Emacs is starting, e.g. by moving another window. */
31044 if (FRAME_FACE_CACHE (f) == NULL
31045 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
31046 {
31047 TRACE ((stderr, " no faces\n"));
31048 return;
31049 }
31050
31051 if (w == 0 || h == 0)
31052 {
31053 r.x = r.y = 0;
31054 r.width = FRAME_TEXT_WIDTH (f);
31055 r.height = FRAME_TEXT_HEIGHT (f);
31056 }
31057 else
31058 {
31059 r.x = x;
31060 r.y = y;
31061 r.width = w;
31062 r.height = h;
31063 }
31064
31065 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
31066 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
31067
31068 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
31069 if (WINDOWP (f->tool_bar_window))
31070 mouse_face_overwritten_p
31071 |= expose_window (XWINDOW (f->tool_bar_window), &r);
31072 #endif
31073
31074 #ifdef HAVE_X_WINDOWS
31075 #ifndef MSDOS
31076 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
31077 if (WINDOWP (f->menu_bar_window))
31078 mouse_face_overwritten_p
31079 |= expose_window (XWINDOW (f->menu_bar_window), &r);
31080 #endif /* not USE_X_TOOLKIT and not USE_GTK */
31081 #endif
31082 #endif
31083
31084 /* Some window managers support a focus-follows-mouse style with
31085 delayed raising of frames. Imagine a partially obscured frame,
31086 and moving the mouse into partially obscured mouse-face on that
31087 frame. The visible part of the mouse-face will be highlighted,
31088 then the WM raises the obscured frame. With at least one WM, KDE
31089 2.1, Emacs is not getting any event for the raising of the frame
31090 (even tried with SubstructureRedirectMask), only Expose events.
31091 These expose events will draw text normally, i.e. not
31092 highlighted. Which means we must redo the highlight here.
31093 Subsume it under ``we love X''. --gerd 2001-08-15 */
31094 /* Included in Windows version because Windows most likely does not
31095 do the right thing if any third party tool offers
31096 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
31097 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
31098 {
31099 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31100 if (f == hlinfo->mouse_face_mouse_frame)
31101 {
31102 int mouse_x = hlinfo->mouse_face_mouse_x;
31103 int mouse_y = hlinfo->mouse_face_mouse_y;
31104 clear_mouse_face (hlinfo);
31105 note_mouse_highlight (f, mouse_x, mouse_y);
31106 }
31107 }
31108 }
31109
31110
31111 /* EXPORT:
31112 Determine the intersection of two rectangles R1 and R2. Return
31113 the intersection in *RESULT. Value is true if RESULT is not
31114 empty. */
31115
31116 bool
31117 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
31118 {
31119 XRectangle *left, *right;
31120 XRectangle *upper, *lower;
31121 bool intersection_p = false;
31122
31123 /* Rearrange so that R1 is the left-most rectangle. */
31124 if (r1->x < r2->x)
31125 left = r1, right = r2;
31126 else
31127 left = r2, right = r1;
31128
31129 /* X0 of the intersection is right.x0, if this is inside R1,
31130 otherwise there is no intersection. */
31131 if (right->x <= left->x + left->width)
31132 {
31133 result->x = right->x;
31134
31135 /* The right end of the intersection is the minimum of
31136 the right ends of left and right. */
31137 result->width = (min (left->x + left->width, right->x + right->width)
31138 - result->x);
31139
31140 /* Same game for Y. */
31141 if (r1->y < r2->y)
31142 upper = r1, lower = r2;
31143 else
31144 upper = r2, lower = r1;
31145
31146 /* The upper end of the intersection is lower.y0, if this is inside
31147 of upper. Otherwise, there is no intersection. */
31148 if (lower->y <= upper->y + upper->height)
31149 {
31150 result->y = lower->y;
31151
31152 /* The lower end of the intersection is the minimum of the lower
31153 ends of upper and lower. */
31154 result->height = (min (lower->y + lower->height,
31155 upper->y + upper->height)
31156 - result->y);
31157 intersection_p = true;
31158 }
31159 }
31160
31161 return intersection_p;
31162 }
31163
31164 #endif /* HAVE_WINDOW_SYSTEM */
31165
31166 \f
31167 /***********************************************************************
31168 Initialization
31169 ***********************************************************************/
31170
31171 void
31172 syms_of_xdisp (void)
31173 {
31174 Vwith_echo_area_save_vector = Qnil;
31175 staticpro (&Vwith_echo_area_save_vector);
31176
31177 Vmessage_stack = Qnil;
31178 staticpro (&Vmessage_stack);
31179
31180 /* Non-nil means don't actually do any redisplay. */
31181 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
31182
31183 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
31184
31185 DEFVAR_BOOL("inhibit-message", inhibit_message,
31186 doc: /* Non-nil means calls to `message' are not displayed.
31187 They are still logged to the *Messages* buffer. */);
31188 inhibit_message = 0;
31189
31190 message_dolog_marker1 = Fmake_marker ();
31191 staticpro (&message_dolog_marker1);
31192 message_dolog_marker2 = Fmake_marker ();
31193 staticpro (&message_dolog_marker2);
31194 message_dolog_marker3 = Fmake_marker ();
31195 staticpro (&message_dolog_marker3);
31196
31197 #ifdef GLYPH_DEBUG
31198 defsubr (&Sdump_frame_glyph_matrix);
31199 defsubr (&Sdump_glyph_matrix);
31200 defsubr (&Sdump_glyph_row);
31201 defsubr (&Sdump_tool_bar_row);
31202 defsubr (&Strace_redisplay);
31203 defsubr (&Strace_to_stderr);
31204 #endif
31205 #ifdef HAVE_WINDOW_SYSTEM
31206 defsubr (&Stool_bar_height);
31207 defsubr (&Slookup_image_map);
31208 #endif
31209 defsubr (&Sline_pixel_height);
31210 defsubr (&Sformat_mode_line);
31211 defsubr (&Sinvisible_p);
31212 defsubr (&Scurrent_bidi_paragraph_direction);
31213 defsubr (&Swindow_text_pixel_size);
31214 defsubr (&Smove_point_visually);
31215 defsubr (&Sbidi_find_overridden_directionality);
31216
31217 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
31218 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
31219 DEFSYM (Qoverriding_local_map, "overriding-local-map");
31220 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
31221 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
31222 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
31223 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
31224 DEFSYM (Qeval, "eval");
31225 DEFSYM (QCdata, ":data");
31226
31227 /* Names of text properties relevant for redisplay. */
31228 DEFSYM (Qdisplay, "display");
31229 DEFSYM (Qspace_width, "space-width");
31230 DEFSYM (Qraise, "raise");
31231 DEFSYM (Qslice, "slice");
31232 DEFSYM (Qspace, "space");
31233 DEFSYM (Qmargin, "margin");
31234 DEFSYM (Qpointer, "pointer");
31235 DEFSYM (Qleft_margin, "left-margin");
31236 DEFSYM (Qright_margin, "right-margin");
31237 DEFSYM (Qcenter, "center");
31238 DEFSYM (Qline_height, "line-height");
31239 DEFSYM (QCalign_to, ":align-to");
31240 DEFSYM (QCrelative_width, ":relative-width");
31241 DEFSYM (QCrelative_height, ":relative-height");
31242 DEFSYM (QCeval, ":eval");
31243 DEFSYM (QCpropertize, ":propertize");
31244 DEFSYM (QCfile, ":file");
31245 DEFSYM (Qfontified, "fontified");
31246 DEFSYM (Qfontification_functions, "fontification-functions");
31247
31248 /* Name of the face used to highlight trailing whitespace. */
31249 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
31250
31251 /* Name and number of the face used to highlight escape glyphs. */
31252 DEFSYM (Qescape_glyph, "escape-glyph");
31253
31254 /* Name and number of the face used to highlight non-breaking
31255 spaces/hyphens. */
31256 DEFSYM (Qnobreak_space, "nobreak-space");
31257 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
31258
31259 /* The symbol 'image' which is the car of the lists used to represent
31260 images in Lisp. Also a tool bar style. */
31261 DEFSYM (Qimage, "image");
31262
31263 /* Tool bar styles. */
31264 DEFSYM (Qtext, "text");
31265 DEFSYM (Qboth, "both");
31266 DEFSYM (Qboth_horiz, "both-horiz");
31267 DEFSYM (Qtext_image_horiz, "text-image-horiz");
31268
31269 /* The image map types. */
31270 DEFSYM (QCmap, ":map");
31271 DEFSYM (QCpointer, ":pointer");
31272 DEFSYM (Qrect, "rect");
31273 DEFSYM (Qcircle, "circle");
31274 DEFSYM (Qpoly, "poly");
31275
31276 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
31277
31278 DEFSYM (Qgrow_only, "grow-only");
31279 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
31280 DEFSYM (Qposition, "position");
31281 DEFSYM (Qbuffer_position, "buffer-position");
31282 DEFSYM (Qobject, "object");
31283
31284 /* Cursor shapes. */
31285 DEFSYM (Qbar, "bar");
31286 DEFSYM (Qhbar, "hbar");
31287 DEFSYM (Qbox, "box");
31288 DEFSYM (Qhollow, "hollow");
31289
31290 /* Pointer shapes. */
31291 DEFSYM (Qhand, "hand");
31292 DEFSYM (Qarrow, "arrow");
31293 /* also Qtext */
31294
31295 DEFSYM (Qdragging, "dragging");
31296
31297 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
31298
31299 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
31300 staticpro (&list_of_error);
31301
31302 /* Values of those variables at last redisplay are stored as
31303 properties on 'overlay-arrow-position' symbol. However, if
31304 Voverlay_arrow_position is a marker, last-arrow-position is its
31305 numerical position. */
31306 DEFSYM (Qlast_arrow_position, "last-arrow-position");
31307 DEFSYM (Qlast_arrow_string, "last-arrow-string");
31308
31309 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
31310 properties on a symbol in overlay-arrow-variable-list. */
31311 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
31312 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
31313
31314 echo_buffer[0] = echo_buffer[1] = Qnil;
31315 staticpro (&echo_buffer[0]);
31316 staticpro (&echo_buffer[1]);
31317
31318 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
31319 staticpro (&echo_area_buffer[0]);
31320 staticpro (&echo_area_buffer[1]);
31321
31322 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
31323 staticpro (&Vmessages_buffer_name);
31324
31325 mode_line_proptrans_alist = Qnil;
31326 staticpro (&mode_line_proptrans_alist);
31327 mode_line_string_list = Qnil;
31328 staticpro (&mode_line_string_list);
31329 mode_line_string_face = Qnil;
31330 staticpro (&mode_line_string_face);
31331 mode_line_string_face_prop = Qnil;
31332 staticpro (&mode_line_string_face_prop);
31333 Vmode_line_unwind_vector = Qnil;
31334 staticpro (&Vmode_line_unwind_vector);
31335
31336 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
31337
31338 help_echo_string = Qnil;
31339 staticpro (&help_echo_string);
31340 help_echo_object = Qnil;
31341 staticpro (&help_echo_object);
31342 help_echo_window = Qnil;
31343 staticpro (&help_echo_window);
31344 previous_help_echo_string = Qnil;
31345 staticpro (&previous_help_echo_string);
31346 help_echo_pos = -1;
31347
31348 DEFSYM (Qright_to_left, "right-to-left");
31349 DEFSYM (Qleft_to_right, "left-to-right");
31350 defsubr (&Sbidi_resolved_levels);
31351
31352 #ifdef HAVE_WINDOW_SYSTEM
31353 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
31354 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
31355 For example, if a block cursor is over a tab, it will be drawn as
31356 wide as that tab on the display. */);
31357 x_stretch_cursor_p = 0;
31358 #endif
31359
31360 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
31361 doc: /* Non-nil means highlight trailing whitespace.
31362 The face used for trailing whitespace is `trailing-whitespace'. */);
31363 Vshow_trailing_whitespace = Qnil;
31364
31365 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
31366 doc: /* Control highlighting of non-ASCII space and hyphen chars.
31367 If the value is t, Emacs highlights non-ASCII chars which have the
31368 same appearance as an ASCII space or hyphen, using the `nobreak-space'
31369 or `nobreak-hyphen' face respectively.
31370
31371 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
31372 U+2011 (non-breaking hyphen) are affected.
31373
31374 Any other non-nil value means to display these characters as a escape
31375 glyph followed by an ordinary space or hyphen.
31376
31377 A value of nil means no special handling of these characters. */);
31378 Vnobreak_char_display = Qt;
31379
31380 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
31381 doc: /* The pointer shape to show in void text areas.
31382 A value of nil means to show the text pointer. Other options are
31383 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
31384 `hourglass'. */);
31385 Vvoid_text_area_pointer = Qarrow;
31386
31387 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
31388 doc: /* Non-nil means don't actually do any redisplay.
31389 This is used for internal purposes. */);
31390 Vinhibit_redisplay = Qnil;
31391
31392 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
31393 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
31394 Vglobal_mode_string = Qnil;
31395
31396 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
31397 doc: /* Marker for where to display an arrow on top of the buffer text.
31398 This must be the beginning of a line in order to work.
31399 See also `overlay-arrow-string'. */);
31400 Voverlay_arrow_position = Qnil;
31401
31402 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
31403 doc: /* String to display as an arrow in non-window frames.
31404 See also `overlay-arrow-position'. */);
31405 Voverlay_arrow_string = build_pure_c_string ("=>");
31406
31407 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31408 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31409 The symbols on this list are examined during redisplay to determine
31410 where to display overlay arrows. */);
31411 Voverlay_arrow_variable_list
31412 = list1 (intern_c_string ("overlay-arrow-position"));
31413
31414 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31415 doc: /* The number of lines to try scrolling a window by when point moves out.
31416 If that fails to bring point back on frame, point is centered instead.
31417 If this is zero, point is always centered after it moves off frame.
31418 If you want scrolling to always be a line at a time, you should set
31419 `scroll-conservatively' to a large value rather than set this to 1. */);
31420
31421 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31422 doc: /* Scroll up to this many lines, to bring point back on screen.
31423 If point moves off-screen, redisplay will scroll by up to
31424 `scroll-conservatively' lines in order to bring point just barely
31425 onto the screen again. If that cannot be done, then redisplay
31426 recenters point as usual.
31427
31428 If the value is greater than 100, redisplay will never recenter point,
31429 but will always scroll just enough text to bring point into view, even
31430 if you move far away.
31431
31432 A value of zero means always recenter point if it moves off screen. */);
31433 scroll_conservatively = 0;
31434
31435 DEFVAR_INT ("scroll-margin", scroll_margin,
31436 doc: /* Number of lines of margin at the top and bottom of a window.
31437 Recenter the window whenever point gets within this many lines
31438 of the top or bottom of the window. */);
31439 scroll_margin = 0;
31440
31441 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31442 doc: /* Pixels per inch value for non-window system displays.
31443 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31444 Vdisplay_pixels_per_inch = make_float (72.0);
31445
31446 #ifdef GLYPH_DEBUG
31447 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31448 #endif
31449
31450 DEFVAR_LISP ("truncate-partial-width-windows",
31451 Vtruncate_partial_width_windows,
31452 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31453 For an integer value, truncate lines in each window narrower than the
31454 full frame width, provided the total window width in column units is less
31455 than that integer; otherwise, respect the value of `truncate-lines'.
31456 The total width of the window is as returned by `window-total-width', it
31457 includes the fringes, the continuation and truncation glyphs, the
31458 display margins (if any), and the scroll bar
31459
31460 For any other non-nil value, truncate lines in all windows that do
31461 not span the full frame width.
31462
31463 A value of nil means to respect the value of `truncate-lines'.
31464
31465 If `word-wrap' is enabled, you might want to reduce this. */);
31466 Vtruncate_partial_width_windows = make_number (50);
31467
31468 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31469 doc: /* Maximum buffer size for which line number should be displayed.
31470 If the buffer is bigger than this, the line number does not appear
31471 in the mode line. A value of nil means no limit. */);
31472 Vline_number_display_limit = Qnil;
31473
31474 DEFVAR_INT ("line-number-display-limit-width",
31475 line_number_display_limit_width,
31476 doc: /* Maximum line width (in characters) for line number display.
31477 If the average length of the lines near point is bigger than this, then the
31478 line number may be omitted from the mode line. */);
31479 line_number_display_limit_width = 200;
31480
31481 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31482 doc: /* Non-nil means highlight region even in nonselected windows. */);
31483 highlight_nonselected_windows = false;
31484
31485 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31486 doc: /* Non-nil if more than one frame is visible on this display.
31487 Minibuffer-only frames don't count, but iconified frames do.
31488 This variable is not guaranteed to be accurate except while processing
31489 `frame-title-format' and `icon-title-format'. */);
31490
31491 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31492 doc: /* Template for displaying the title bar of visible frames.
31493 \(Assuming the window manager supports this feature.)
31494
31495 This variable has the same structure as `mode-line-format', except that
31496 the %c and %l constructs are ignored. It is used only on frames for
31497 which no explicit name has been set (see `modify-frame-parameters'). */);
31498
31499 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31500 doc: /* Template for displaying the title bar of an iconified frame.
31501 \(Assuming the window manager supports this feature.)
31502 This variable has the same structure as `mode-line-format' (which see),
31503 and is used only on frames for which no explicit name has been set
31504 \(see `modify-frame-parameters'). */);
31505 Vicon_title_format
31506 = Vframe_title_format
31507 = listn (CONSTYPE_PURE, 3,
31508 intern_c_string ("multiple-frames"),
31509 build_pure_c_string ("%b"),
31510 listn (CONSTYPE_PURE, 4,
31511 empty_unibyte_string,
31512 intern_c_string ("invocation-name"),
31513 build_pure_c_string ("@"),
31514 intern_c_string ("system-name")));
31515
31516 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31517 doc: /* Maximum number of lines to keep in the message log buffer.
31518 If nil, disable message logging. If t, log messages but don't truncate
31519 the buffer when it becomes large. */);
31520 Vmessage_log_max = make_number (1000);
31521
31522 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31523 doc: /* List of functions to call before redisplaying a window with scrolling.
31524 Each function is called with two arguments, the window and its new
31525 display-start position.
31526 These functions are called whenever the `window-start' marker is modified,
31527 either to point into another buffer (e.g. via `set-window-buffer') or another
31528 place in the same buffer.
31529 Note that the value of `window-end' is not valid when these functions are
31530 called.
31531
31532 Warning: Do not use this feature to alter the way the window
31533 is scrolled. It is not designed for that, and such use probably won't
31534 work. */);
31535 Vwindow_scroll_functions = Qnil;
31536
31537 DEFVAR_LISP ("window-text-change-functions",
31538 Vwindow_text_change_functions,
31539 doc: /* Functions to call in redisplay when text in the window might change. */);
31540 Vwindow_text_change_functions = Qnil;
31541
31542 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31543 doc: /* Functions called when redisplay of a window reaches the end trigger.
31544 Each function is called with two arguments, the window and the end trigger value.
31545 See `set-window-redisplay-end-trigger'. */);
31546 Vredisplay_end_trigger_functions = Qnil;
31547
31548 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31549 doc: /* Non-nil means autoselect window with mouse pointer.
31550 If nil, do not autoselect windows.
31551 A positive number means delay autoselection by that many seconds: a
31552 window is autoselected only after the mouse has remained in that
31553 window for the duration of the delay.
31554 A negative number has a similar effect, but causes windows to be
31555 autoselected only after the mouse has stopped moving. (Because of
31556 the way Emacs compares mouse events, you will occasionally wait twice
31557 that time before the window gets selected.)
31558 Any other value means to autoselect window instantaneously when the
31559 mouse pointer enters it.
31560
31561 Autoselection selects the minibuffer only if it is active, and never
31562 unselects the minibuffer if it is active.
31563
31564 When customizing this variable make sure that the actual value of
31565 `focus-follows-mouse' matches the behavior of your window manager. */);
31566 Vmouse_autoselect_window = Qnil;
31567
31568 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31569 doc: /* Non-nil means automatically resize tool-bars.
31570 This dynamically changes the tool-bar's height to the minimum height
31571 that is needed to make all tool-bar items visible.
31572 If value is `grow-only', the tool-bar's height is only increased
31573 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31574 Vauto_resize_tool_bars = Qt;
31575
31576 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31577 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31578 auto_raise_tool_bar_buttons_p = true;
31579
31580 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31581 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31582 make_cursor_line_fully_visible_p = true;
31583
31584 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31585 doc: /* Border below tool-bar in pixels.
31586 If an integer, use it as the height of the border.
31587 If it is one of `internal-border-width' or `border-width', use the
31588 value of the corresponding frame parameter.
31589 Otherwise, no border is added below the tool-bar. */);
31590 Vtool_bar_border = Qinternal_border_width;
31591
31592 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31593 doc: /* Margin around tool-bar buttons in pixels.
31594 If an integer, use that for both horizontal and vertical margins.
31595 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31596 HORZ specifying the horizontal margin, and VERT specifying the
31597 vertical margin. */);
31598 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31599
31600 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31601 doc: /* Relief thickness of tool-bar buttons. */);
31602 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31603
31604 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31605 doc: /* Tool bar style to use.
31606 It can be one of
31607 image - show images only
31608 text - show text only
31609 both - show both, text below image
31610 both-horiz - show text to the right of the image
31611 text-image-horiz - show text to the left of the image
31612 any other - use system default or image if no system default.
31613
31614 This variable only affects the GTK+ toolkit version of Emacs. */);
31615 Vtool_bar_style = Qnil;
31616
31617 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31618 doc: /* Maximum number of characters a label can have to be shown.
31619 The tool bar style must also show labels for this to have any effect, see
31620 `tool-bar-style'. */);
31621 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31622
31623 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31624 doc: /* List of functions to call to fontify regions of text.
31625 Each function is called with one argument POS. Functions must
31626 fontify a region starting at POS in the current buffer, and give
31627 fontified regions the property `fontified'. */);
31628 Vfontification_functions = Qnil;
31629 Fmake_variable_buffer_local (Qfontification_functions);
31630
31631 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31632 unibyte_display_via_language_environment,
31633 doc: /* Non-nil means display unibyte text according to language environment.
31634 Specifically, this means that raw bytes in the range 160-255 decimal
31635 are displayed by converting them to the equivalent multibyte characters
31636 according to the current language environment. As a result, they are
31637 displayed according to the current fontset.
31638
31639 Note that this variable affects only how these bytes are displayed,
31640 but does not change the fact they are interpreted as raw bytes. */);
31641 unibyte_display_via_language_environment = false;
31642
31643 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31644 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31645 If a float, it specifies a fraction of the mini-window frame's height.
31646 If an integer, it specifies a number of lines. */);
31647 Vmax_mini_window_height = make_float (0.25);
31648
31649 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31650 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31651 A value of nil means don't automatically resize mini-windows.
31652 A value of t means resize them to fit the text displayed in them.
31653 A value of `grow-only', the default, means let mini-windows grow only;
31654 they return to their normal size when the minibuffer is closed, or the
31655 echo area becomes empty. */);
31656 /* Contrary to the doc string, we initialize this to nil, so that
31657 loading loadup.el won't try to resize windows before loading
31658 window.el, where some functions we need to call for this live.
31659 We assign the 'grow-only' value right after loading window.el
31660 during loadup. */
31661 Vresize_mini_windows = Qnil;
31662
31663 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31664 doc: /* Alist specifying how to blink the cursor off.
31665 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31666 `cursor-type' frame-parameter or variable equals ON-STATE,
31667 comparing using `equal', Emacs uses OFF-STATE to specify
31668 how to blink it off. ON-STATE and OFF-STATE are values for
31669 the `cursor-type' frame parameter.
31670
31671 If a frame's ON-STATE has no entry in this list,
31672 the frame's other specifications determine how to blink the cursor off. */);
31673 Vblink_cursor_alist = Qnil;
31674
31675 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31676 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31677 If non-nil, windows are automatically scrolled horizontally to make
31678 point visible. */);
31679 automatic_hscrolling_p = true;
31680 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31681
31682 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31683 doc: /* How many columns away from the window edge point is allowed to get
31684 before automatic hscrolling will horizontally scroll the window. */);
31685 hscroll_margin = 5;
31686
31687 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31688 doc: /* How many columns to scroll the window when point gets too close to the edge.
31689 When point is less than `hscroll-margin' columns from the window
31690 edge, automatic hscrolling will scroll the window by the amount of columns
31691 determined by this variable. If its value is a positive integer, scroll that
31692 many columns. If it's a positive floating-point number, it specifies the
31693 fraction of the window's width to scroll. If it's nil or zero, point will be
31694 centered horizontally after the scroll. Any other value, including negative
31695 numbers, are treated as if the value were zero.
31696
31697 Automatic hscrolling always moves point outside the scroll margin, so if
31698 point was more than scroll step columns inside the margin, the window will
31699 scroll more than the value given by the scroll step.
31700
31701 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31702 and `scroll-right' overrides this variable's effect. */);
31703 Vhscroll_step = make_number (0);
31704
31705 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31706 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31707 Bind this around calls to `message' to let it take effect. */);
31708 message_truncate_lines = false;
31709
31710 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31711 doc: /* Normal hook run to update the menu bar definitions.
31712 Redisplay runs this hook before it redisplays the menu bar.
31713 This is used to update menus such as Buffers, whose contents depend on
31714 various data. */);
31715 Vmenu_bar_update_hook = Qnil;
31716
31717 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31718 doc: /* Frame for which we are updating a menu.
31719 The enable predicate for a menu binding should check this variable. */);
31720 Vmenu_updating_frame = Qnil;
31721
31722 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31723 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31724 inhibit_menubar_update = false;
31725
31726 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31727 doc: /* Prefix prepended to all continuation lines at display time.
31728 The value may be a string, an image, or a stretch-glyph; it is
31729 interpreted in the same way as the value of a `display' text property.
31730
31731 This variable is overridden by any `wrap-prefix' text or overlay
31732 property.
31733
31734 To add a prefix to non-continuation lines, use `line-prefix'. */);
31735 Vwrap_prefix = Qnil;
31736 DEFSYM (Qwrap_prefix, "wrap-prefix");
31737 Fmake_variable_buffer_local (Qwrap_prefix);
31738
31739 DEFVAR_LISP ("line-prefix", Vline_prefix,
31740 doc: /* Prefix prepended to all non-continuation lines at display time.
31741 The value may be a string, an image, or a stretch-glyph; it is
31742 interpreted in the same way as the value of a `display' text property.
31743
31744 This variable is overridden by any `line-prefix' text or overlay
31745 property.
31746
31747 To add a prefix to continuation lines, use `wrap-prefix'. */);
31748 Vline_prefix = Qnil;
31749 DEFSYM (Qline_prefix, "line-prefix");
31750 Fmake_variable_buffer_local (Qline_prefix);
31751
31752 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31753 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31754 inhibit_eval_during_redisplay = false;
31755
31756 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31757 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31758 inhibit_free_realized_faces = false;
31759
31760 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31761 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31762 Intended for use during debugging and for testing bidi display;
31763 see biditest.el in the test suite. */);
31764 inhibit_bidi_mirroring = false;
31765
31766 #ifdef GLYPH_DEBUG
31767 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31768 doc: /* Inhibit try_window_id display optimization. */);
31769 inhibit_try_window_id = false;
31770
31771 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31772 doc: /* Inhibit try_window_reusing display optimization. */);
31773 inhibit_try_window_reusing = false;
31774
31775 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31776 doc: /* Inhibit try_cursor_movement display optimization. */);
31777 inhibit_try_cursor_movement = false;
31778 #endif /* GLYPH_DEBUG */
31779
31780 DEFVAR_INT ("overline-margin", overline_margin,
31781 doc: /* Space between overline and text, in pixels.
31782 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31783 margin to the character height. */);
31784 overline_margin = 2;
31785
31786 DEFVAR_INT ("underline-minimum-offset",
31787 underline_minimum_offset,
31788 doc: /* Minimum distance between baseline and underline.
31789 This can improve legibility of underlined text at small font sizes,
31790 particularly when using variable `x-use-underline-position-properties'
31791 with fonts that specify an UNDERLINE_POSITION relatively close to the
31792 baseline. The default value is 1. */);
31793 underline_minimum_offset = 1;
31794
31795 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31796 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31797 This feature only works when on a window system that can change
31798 cursor shapes. */);
31799 display_hourglass_p = true;
31800
31801 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31802 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31803 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31804
31805 #ifdef HAVE_WINDOW_SYSTEM
31806 hourglass_atimer = NULL;
31807 hourglass_shown_p = false;
31808 #endif /* HAVE_WINDOW_SYSTEM */
31809
31810 /* Name of the face used to display glyphless characters. */
31811 DEFSYM (Qglyphless_char, "glyphless-char");
31812
31813 /* Method symbols for Vglyphless_char_display. */
31814 DEFSYM (Qhex_code, "hex-code");
31815 DEFSYM (Qempty_box, "empty-box");
31816 DEFSYM (Qthin_space, "thin-space");
31817 DEFSYM (Qzero_width, "zero-width");
31818
31819 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31820 doc: /* Function run just before redisplay.
31821 It is called with one argument, which is the set of windows that are to
31822 be redisplayed. This set can be nil (meaning, only the selected window),
31823 or t (meaning all windows). */);
31824 Vpre_redisplay_function = intern ("ignore");
31825
31826 /* Symbol for the purpose of Vglyphless_char_display. */
31827 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31828 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31829
31830 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31831 doc: /* Char-table defining glyphless characters.
31832 Each element, if non-nil, should be one of the following:
31833 an ASCII acronym string: display this string in a box
31834 `hex-code': display the hexadecimal code of a character in a box
31835 `empty-box': display as an empty box
31836 `thin-space': display as 1-pixel width space
31837 `zero-width': don't display
31838 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31839 display method for graphical terminals and text terminals respectively.
31840 GRAPHICAL and TEXT should each have one of the values listed above.
31841
31842 The char-table has one extra slot to control the display of a character for
31843 which no font is found. This slot only takes effect on graphical terminals.
31844 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31845 `thin-space'. The default is `empty-box'.
31846
31847 If a character has a non-nil entry in an active display table, the
31848 display table takes effect; in this case, Emacs does not consult
31849 `glyphless-char-display' at all. */);
31850 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31851 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31852 Qempty_box);
31853
31854 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31855 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31856 Vdebug_on_message = Qnil;
31857
31858 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31859 doc: /* */);
31860 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
31861
31862 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31863 doc: /* */);
31864 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
31865
31866 DEFVAR_LISP ("redisplay--variables", Vredisplay__variables,
31867 doc: /* A hash-table of variables changing which triggers a thorough redisplay. */);
31868 Vredisplay__variables = Qnil;
31869
31870 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
31871 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
31872 /* Initialize to t, since we need to disable reordering until
31873 loadup.el successfully loads charprop.el. */
31874 redisplay__inhibit_bidi = true;
31875 }
31876
31877
31878 /* Initialize this module when Emacs starts. */
31879
31880 void
31881 init_xdisp (void)
31882 {
31883 CHARPOS (this_line_start_pos) = 0;
31884
31885 if (!noninteractive)
31886 {
31887 struct window *m = XWINDOW (minibuf_window);
31888 Lisp_Object frame = m->frame;
31889 struct frame *f = XFRAME (frame);
31890 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31891 struct window *r = XWINDOW (root);
31892 int i;
31893
31894 echo_area_window = minibuf_window;
31895
31896 r->top_line = FRAME_TOP_MARGIN (f);
31897 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31898 r->total_cols = FRAME_COLS (f);
31899 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31900 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31901 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31902
31903 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31904 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31905 m->total_cols = FRAME_COLS (f);
31906 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31907 m->total_lines = 1;
31908 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31909
31910 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31911 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31912 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31913
31914 /* The default ellipsis glyphs `...'. */
31915 for (i = 0; i < 3; ++i)
31916 default_invis_vector[i] = make_number ('.');
31917 }
31918
31919 {
31920 /* Allocate the buffer for frame titles.
31921 Also used for `format-mode-line'. */
31922 int size = 100;
31923 mode_line_noprop_buf = xmalloc (size);
31924 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31925 mode_line_noprop_ptr = mode_line_noprop_buf;
31926 mode_line_target = MODE_LINE_DISPLAY;
31927 }
31928
31929 help_echo_showing_p = false;
31930 }
31931
31932 #ifdef HAVE_WINDOW_SYSTEM
31933
31934 /* Platform-independent portion of hourglass implementation. */
31935
31936 /* Timer function of hourglass_atimer. */
31937
31938 static void
31939 show_hourglass (struct atimer *timer)
31940 {
31941 /* The timer implementation will cancel this timer automatically
31942 after this function has run. Set hourglass_atimer to null
31943 so that we know the timer doesn't have to be canceled. */
31944 hourglass_atimer = NULL;
31945
31946 if (!hourglass_shown_p)
31947 {
31948 Lisp_Object tail, frame;
31949
31950 block_input ();
31951
31952 FOR_EACH_FRAME (tail, frame)
31953 {
31954 struct frame *f = XFRAME (frame);
31955
31956 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31957 && FRAME_RIF (f)->show_hourglass)
31958 FRAME_RIF (f)->show_hourglass (f);
31959 }
31960
31961 hourglass_shown_p = true;
31962 unblock_input ();
31963 }
31964 }
31965
31966 /* Cancel a currently active hourglass timer, and start a new one. */
31967
31968 void
31969 start_hourglass (void)
31970 {
31971 struct timespec delay;
31972
31973 cancel_hourglass ();
31974
31975 if (INTEGERP (Vhourglass_delay)
31976 && XINT (Vhourglass_delay) > 0)
31977 delay = make_timespec (min (XINT (Vhourglass_delay),
31978 TYPE_MAXIMUM (time_t)),
31979 0);
31980 else if (FLOATP (Vhourglass_delay)
31981 && XFLOAT_DATA (Vhourglass_delay) > 0)
31982 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31983 else
31984 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31985
31986 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31987 show_hourglass, NULL);
31988 }
31989
31990 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31991 shown. */
31992
31993 void
31994 cancel_hourglass (void)
31995 {
31996 if (hourglass_atimer)
31997 {
31998 cancel_atimer (hourglass_atimer);
31999 hourglass_atimer = NULL;
32000 }
32001
32002 if (hourglass_shown_p)
32003 {
32004 Lisp_Object tail, frame;
32005
32006 block_input ();
32007
32008 FOR_EACH_FRAME (tail, frame)
32009 {
32010 struct frame *f = XFRAME (frame);
32011
32012 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32013 && FRAME_RIF (f)->hide_hourglass)
32014 FRAME_RIF (f)->hide_hourglass (f);
32015 #ifdef HAVE_NTGUI
32016 /* No cursors on non GUI frames - restore to stock arrow cursor. */
32017 else if (!FRAME_W32_P (f))
32018 w32_arrow_cursor ();
32019 #endif
32020 }
32021
32022 hourglass_shown_p = false;
32023 unblock_input ();
32024 }
32025 }
32026
32027 #endif /* HAVE_WINDOW_SYSTEM */