]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Support for the new Xwidget feature.
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2016 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
104
105 . try_window
106
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
112
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
117
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
130
131 Desired matrices.
132
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
139
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
146
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
156
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
163
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
175
176 Frame matrices.
177
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
184
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
196
197 Bidirectional display.
198
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
211
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
220
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
239
240 Bidirectional display and character compositions
241
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
246
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
266
267 Moving an iterator in bidirectional text
268 without producing glyphs
269
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
288
289 #include <config.h>
290 #include <stdio.h>
291 #include <limits.h>
292
293 #include "lisp.h"
294 #include "atimer.h"
295 #include "composite.h"
296 #include "keyboard.h"
297 #include "systime.h"
298 #include "frame.h"
299 #include "window.h"
300 #include "termchar.h"
301 #include "dispextern.h"
302 #include "character.h"
303 #include "buffer.h"
304 #include "charset.h"
305 #include "indent.h"
306 #include "commands.h"
307 #include "keymap.h"
308 #include "disptab.h"
309 #include "termhooks.h"
310 #include "termopts.h"
311 #include "intervals.h"
312 #include "coding.h"
313 #include "region-cache.h"
314 #include "font.h"
315 #include "fontset.h"
316 #include "blockinput.h"
317 #ifdef HAVE_WINDOW_SYSTEM
318 #include TERM_HEADER
319 #endif /* HAVE_WINDOW_SYSTEM */
320
321 #ifdef HAVE_XWIDGETS
322 # include "xwidget.h"
323 #endif
324 #ifndef FRAME_X_OUTPUT
325 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
326 #endif
327
328 #define INFINITY 10000000
329
330 /* Holds the list (error). */
331 static Lisp_Object list_of_error;
332
333 #ifdef HAVE_WINDOW_SYSTEM
334
335 /* Test if overflow newline into fringe. Called with iterator IT
336 at or past right window margin, and with IT->current_x set. */
337
338 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
339 (!NILP (Voverflow_newline_into_fringe) \
340 && FRAME_WINDOW_P ((IT)->f) \
341 && ((IT)->bidi_it.paragraph_dir == R2L \
342 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
343 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
344 && (IT)->current_x == (IT)->last_visible_x)
345
346 #else /* !HAVE_WINDOW_SYSTEM */
347 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
348 #endif /* HAVE_WINDOW_SYSTEM */
349
350 /* Test if the display element loaded in IT, or the underlying buffer
351 or string character, is a space or a TAB character. This is used
352 to determine where word wrapping can occur. */
353
354 #define IT_DISPLAYING_WHITESPACE(it) \
355 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
356 || ((STRINGP (it->string) \
357 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
358 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
359 || (it->s \
360 && (it->s[IT_BYTEPOS (*it)] == ' ' \
361 || it->s[IT_BYTEPOS (*it)] == '\t')) \
362 || (IT_BYTEPOS (*it) < ZV_BYTE \
363 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
364 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
365
366 /* True means print newline to stdout before next mini-buffer message. */
367
368 bool noninteractive_need_newline;
369
370 /* True means print newline to message log before next message. */
371
372 static bool message_log_need_newline;
373
374 /* Three markers that message_dolog uses.
375 It could allocate them itself, but that causes trouble
376 in handling memory-full errors. */
377 static Lisp_Object message_dolog_marker1;
378 static Lisp_Object message_dolog_marker2;
379 static Lisp_Object message_dolog_marker3;
380 \f
381 /* The buffer position of the first character appearing entirely or
382 partially on the line of the selected window which contains the
383 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
384 redisplay optimization in redisplay_internal. */
385
386 static struct text_pos this_line_start_pos;
387
388 /* Number of characters past the end of the line above, including the
389 terminating newline. */
390
391 static struct text_pos this_line_end_pos;
392
393 /* The vertical positions and the height of this line. */
394
395 static int this_line_vpos;
396 static int this_line_y;
397 static int this_line_pixel_height;
398
399 /* X position at which this display line starts. Usually zero;
400 negative if first character is partially visible. */
401
402 static int this_line_start_x;
403
404 /* The smallest character position seen by move_it_* functions as they
405 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
406 hscrolled lines, see display_line. */
407
408 static struct text_pos this_line_min_pos;
409
410 /* Buffer that this_line_.* variables are referring to. */
411
412 static struct buffer *this_line_buffer;
413
414 /* True if an overlay arrow has been displayed in this window. */
415
416 static bool overlay_arrow_seen;
417
418 /* Vector containing glyphs for an ellipsis `...'. */
419
420 static Lisp_Object default_invis_vector[3];
421
422 /* This is the window where the echo area message was displayed. It
423 is always a mini-buffer window, but it may not be the same window
424 currently active as a mini-buffer. */
425
426 Lisp_Object echo_area_window;
427
428 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
429 pushes the current message and the value of
430 message_enable_multibyte on the stack, the function restore_message
431 pops the stack and displays MESSAGE again. */
432
433 static Lisp_Object Vmessage_stack;
434
435 /* True means multibyte characters were enabled when the echo area
436 message was specified. */
437
438 static bool message_enable_multibyte;
439
440 /* At each redisplay cycle, we should refresh everything there is to refresh.
441 To do that efficiently, we use many optimizations that try to make sure we
442 don't waste too much time updating things that haven't changed.
443 The coarsest such optimization is that, in the most common cases, we only
444 look at the selected-window.
445
446 To know whether other windows should be considered for redisplay, we use the
447 variable windows_or_buffers_changed: as long as it is 0, it means that we
448 have not noticed anything that should require updating anything else than
449 the selected-window. If it is set to REDISPLAY_SOME, it means that since
450 last redisplay, some changes have been made which could impact other
451 windows. To know which ones need redisplay, every buffer, window, and frame
452 has a `redisplay' bit, which (if true) means that this object needs to be
453 redisplayed. If windows_or_buffers_changed is 0, we know there's no point
454 looking for those `redisplay' bits (actually, there might be some such bits
455 set, but then only on objects which aren't displayed anyway).
456
457 OTOH if it's non-zero we wil have to loop through all windows and then check
458 the `redisplay' bit of the corresponding window, frame, and buffer, in order
459 to decide whether that window needs attention or not. Note that we can't
460 just look at the frame's redisplay bit to decide that the whole frame can be
461 skipped, since even if the frame's redisplay bit is unset, some of its
462 windows's redisplay bits may be set.
463
464 Mostly for historical reasons, windows_or_buffers_changed can also take
465 other non-zero values. In that case, the precise value doesn't matter (it
466 encodes the cause of the setting but is only used for debugging purposes),
467 and what it means is that we shouldn't pay attention to any `redisplay' bits
468 and we should simply try and redisplay every window out there. */
469
470 int windows_or_buffers_changed;
471
472 /* Nonzero if we should redraw the mode lines on the next redisplay.
473 Similarly to `windows_or_buffers_changed', If it has value REDISPLAY_SOME,
474 then only redisplay the mode lines in those buffers/windows/frames where the
475 `redisplay' bit has been set.
476 For any other value, redisplay all mode lines (the number used is then only
477 used to track down the cause for this full-redisplay).
478
479 Since the frame title uses the same %-constructs as the mode line
480 (except %c and %l), if this variable is non-zero, we also consider
481 redisplaying the title of each frame, see x_consider_frame_title.
482
483 The `redisplay' bits are the same as those used for
484 windows_or_buffers_changed, and setting windows_or_buffers_changed also
485 causes recomputation of the mode lines of all those windows. IOW this
486 variable only has an effect if windows_or_buffers_changed is zero, in which
487 case we should only need to redisplay the mode-line of those objects with
488 a `redisplay' bit set but not the window's text content (tho we may still
489 need to refresh the text content of the selected-window). */
490
491 int update_mode_lines;
492
493 /* True after display_mode_line if %l was used and it displayed a
494 line number. */
495
496 static bool line_number_displayed;
497
498 /* The name of the *Messages* buffer, a string. */
499
500 static Lisp_Object Vmessages_buffer_name;
501
502 /* Current, index 0, and last displayed echo area message. Either
503 buffers from echo_buffers, or nil to indicate no message. */
504
505 Lisp_Object echo_area_buffer[2];
506
507 /* The buffers referenced from echo_area_buffer. */
508
509 static Lisp_Object echo_buffer[2];
510
511 /* A vector saved used in with_area_buffer to reduce consing. */
512
513 static Lisp_Object Vwith_echo_area_save_vector;
514
515 /* True means display_echo_area should display the last echo area
516 message again. Set by redisplay_preserve_echo_area. */
517
518 static bool display_last_displayed_message_p;
519
520 /* True if echo area is being used by print; false if being used by
521 message. */
522
523 static bool message_buf_print;
524
525 /* Set to true in clear_message to make redisplay_internal aware
526 of an emptied echo area. */
527
528 static bool message_cleared_p;
529
530 /* A scratch glyph row with contents used for generating truncation
531 glyphs. Also used in direct_output_for_insert. */
532
533 #define MAX_SCRATCH_GLYPHS 100
534 static struct glyph_row scratch_glyph_row;
535 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
536
537 /* Ascent and height of the last line processed by move_it_to. */
538
539 static int last_height;
540
541 /* True if there's a help-echo in the echo area. */
542
543 bool help_echo_showing_p;
544
545 /* The maximum distance to look ahead for text properties. Values
546 that are too small let us call compute_char_face and similar
547 functions too often which is expensive. Values that are too large
548 let us call compute_char_face and alike too often because we
549 might not be interested in text properties that far away. */
550
551 #define TEXT_PROP_DISTANCE_LIMIT 100
552
553 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
554 iterator state and later restore it. This is needed because the
555 bidi iterator on bidi.c keeps a stacked cache of its states, which
556 is really a singleton. When we use scratch iterator objects to
557 move around the buffer, we can cause the bidi cache to be pushed or
558 popped, and therefore we need to restore the cache state when we
559 return to the original iterator. */
560 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
561 do { \
562 if (CACHE) \
563 bidi_unshelve_cache (CACHE, true); \
564 ITCOPY = ITORIG; \
565 CACHE = bidi_shelve_cache (); \
566 } while (false)
567
568 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
569 do { \
570 if (pITORIG != pITCOPY) \
571 *(pITORIG) = *(pITCOPY); \
572 bidi_unshelve_cache (CACHE, false); \
573 CACHE = NULL; \
574 } while (false)
575
576 /* Functions to mark elements as needing redisplay. */
577 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
578
579 void
580 redisplay_other_windows (void)
581 {
582 if (!windows_or_buffers_changed)
583 windows_or_buffers_changed = REDISPLAY_SOME;
584 }
585
586 void
587 wset_redisplay (struct window *w)
588 {
589 /* Beware: selected_window can be nil during early stages. */
590 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
591 redisplay_other_windows ();
592 w->redisplay = true;
593 }
594
595 void
596 fset_redisplay (struct frame *f)
597 {
598 redisplay_other_windows ();
599 f->redisplay = true;
600 }
601
602 void
603 bset_redisplay (struct buffer *b)
604 {
605 int count = buffer_window_count (b);
606 if (count > 0)
607 {
608 /* ... it's visible in other window than selected, */
609 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
610 redisplay_other_windows ();
611 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
612 so that if we later set windows_or_buffers_changed, this buffer will
613 not be omitted. */
614 b->text->redisplay = true;
615 }
616 }
617
618 void
619 bset_update_mode_line (struct buffer *b)
620 {
621 if (!update_mode_lines)
622 update_mode_lines = REDISPLAY_SOME;
623 b->text->redisplay = true;
624 }
625
626 void
627 maybe_set_redisplay (Lisp_Object symbol)
628 {
629 if (HASH_TABLE_P (Vredisplay__variables)
630 && hash_lookup (XHASH_TABLE (Vredisplay__variables), symbol, NULL) >= 0)
631 {
632 bset_update_mode_line (current_buffer);
633 current_buffer->prevent_redisplay_optimizations_p = true;
634 }
635 }
636
637 #ifdef GLYPH_DEBUG
638
639 /* True means print traces of redisplay if compiled with
640 GLYPH_DEBUG defined. */
641
642 bool trace_redisplay_p;
643
644 #endif /* GLYPH_DEBUG */
645
646 #ifdef DEBUG_TRACE_MOVE
647 /* True means trace with TRACE_MOVE to stderr. */
648 static bool trace_move;
649
650 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
651 #else
652 #define TRACE_MOVE(x) (void) 0
653 #endif
654
655 /* Buffer being redisplayed -- for redisplay_window_error. */
656
657 static struct buffer *displayed_buffer;
658
659 /* Value returned from text property handlers (see below). */
660
661 enum prop_handled
662 {
663 HANDLED_NORMALLY,
664 HANDLED_RECOMPUTE_PROPS,
665 HANDLED_OVERLAY_STRING_CONSUMED,
666 HANDLED_RETURN
667 };
668
669 /* A description of text properties that redisplay is interested
670 in. */
671
672 struct props
673 {
674 /* The symbol index of the name of the property. */
675 short name;
676
677 /* A unique index for the property. */
678 enum prop_idx idx;
679
680 /* A handler function called to set up iterator IT from the property
681 at IT's current position. Value is used to steer handle_stop. */
682 enum prop_handled (*handler) (struct it *it);
683 };
684
685 static enum prop_handled handle_face_prop (struct it *);
686 static enum prop_handled handle_invisible_prop (struct it *);
687 static enum prop_handled handle_display_prop (struct it *);
688 static enum prop_handled handle_composition_prop (struct it *);
689 static enum prop_handled handle_overlay_change (struct it *);
690 static enum prop_handled handle_fontified_prop (struct it *);
691
692 /* Properties handled by iterators. */
693
694 static struct props it_props[] =
695 {
696 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
697 /* Handle `face' before `display' because some sub-properties of
698 `display' need to know the face. */
699 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
700 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
701 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
702 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
703 {0, 0, NULL}
704 };
705
706 /* Value is the position described by X. If X is a marker, value is
707 the marker_position of X. Otherwise, value is X. */
708
709 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
710
711 /* Enumeration returned by some move_it_.* functions internally. */
712
713 enum move_it_result
714 {
715 /* Not used. Undefined value. */
716 MOVE_UNDEFINED,
717
718 /* Move ended at the requested buffer position or ZV. */
719 MOVE_POS_MATCH_OR_ZV,
720
721 /* Move ended at the requested X pixel position. */
722 MOVE_X_REACHED,
723
724 /* Move within a line ended at the end of a line that must be
725 continued. */
726 MOVE_LINE_CONTINUED,
727
728 /* Move within a line ended at the end of a line that would
729 be displayed truncated. */
730 MOVE_LINE_TRUNCATED,
731
732 /* Move within a line ended at a line end. */
733 MOVE_NEWLINE_OR_CR
734 };
735
736 /* This counter is used to clear the face cache every once in a while
737 in redisplay_internal. It is incremented for each redisplay.
738 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
739 cleared. */
740
741 #define CLEAR_FACE_CACHE_COUNT 500
742 static int clear_face_cache_count;
743
744 /* Similarly for the image cache. */
745
746 #ifdef HAVE_WINDOW_SYSTEM
747 #define CLEAR_IMAGE_CACHE_COUNT 101
748 static int clear_image_cache_count;
749
750 /* Null glyph slice */
751 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
752 #endif
753
754 /* True while redisplay_internal is in progress. */
755
756 bool redisplaying_p;
757
758 /* If a string, XTread_socket generates an event to display that string.
759 (The display is done in read_char.) */
760
761 Lisp_Object help_echo_string;
762 Lisp_Object help_echo_window;
763 Lisp_Object help_echo_object;
764 ptrdiff_t help_echo_pos;
765
766 /* Temporary variable for XTread_socket. */
767
768 Lisp_Object previous_help_echo_string;
769
770 /* Platform-independent portion of hourglass implementation. */
771
772 #ifdef HAVE_WINDOW_SYSTEM
773
774 /* True means an hourglass cursor is currently shown. */
775 static bool hourglass_shown_p;
776
777 /* If non-null, an asynchronous timer that, when it expires, displays
778 an hourglass cursor on all frames. */
779 static struct atimer *hourglass_atimer;
780
781 #endif /* HAVE_WINDOW_SYSTEM */
782
783 /* Default number of seconds to wait before displaying an hourglass
784 cursor. */
785 #define DEFAULT_HOURGLASS_DELAY 1
786
787 #ifdef HAVE_WINDOW_SYSTEM
788
789 /* Default pixel width of `thin-space' display method. */
790 #define THIN_SPACE_WIDTH 1
791
792 #endif /* HAVE_WINDOW_SYSTEM */
793
794 /* Function prototypes. */
795
796 static void setup_for_ellipsis (struct it *, int);
797 static void set_iterator_to_next (struct it *, bool);
798 static void mark_window_display_accurate_1 (struct window *, bool);
799 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
800 static bool cursor_row_p (struct glyph_row *);
801 static int redisplay_mode_lines (Lisp_Object, bool);
802
803 static void handle_line_prefix (struct it *);
804
805 static void handle_stop_backwards (struct it *, ptrdiff_t);
806 static void unwind_with_echo_area_buffer (Lisp_Object);
807 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
808 static bool current_message_1 (ptrdiff_t, Lisp_Object);
809 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
810 static void set_message (Lisp_Object);
811 static bool set_message_1 (ptrdiff_t, Lisp_Object);
812 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
813 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
814 static void unwind_redisplay (void);
815 static void extend_face_to_end_of_line (struct it *);
816 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
817 static void push_it (struct it *, struct text_pos *);
818 static void iterate_out_of_display_property (struct it *);
819 static void pop_it (struct it *);
820 static void redisplay_internal (void);
821 static void echo_area_display (bool);
822 static void redisplay_windows (Lisp_Object);
823 static void redisplay_window (Lisp_Object, bool);
824 static Lisp_Object redisplay_window_error (Lisp_Object);
825 static Lisp_Object redisplay_window_0 (Lisp_Object);
826 static Lisp_Object redisplay_window_1 (Lisp_Object);
827 static bool set_cursor_from_row (struct window *, struct glyph_row *,
828 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
829 int, int);
830 static bool update_menu_bar (struct frame *, bool, bool);
831 static bool try_window_reusing_current_matrix (struct window *);
832 static int try_window_id (struct window *);
833 static bool display_line (struct it *);
834 static int display_mode_lines (struct window *);
835 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
836 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
837 Lisp_Object, bool);
838 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
839 Lisp_Object);
840 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
841 static void display_menu_bar (struct window *);
842 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
843 ptrdiff_t *);
844 static int display_string (const char *, Lisp_Object, Lisp_Object,
845 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
846 static void compute_line_metrics (struct it *);
847 static void run_redisplay_end_trigger_hook (struct it *);
848 static bool get_overlay_strings (struct it *, ptrdiff_t);
849 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
850 static void next_overlay_string (struct it *);
851 static void reseat (struct it *, struct text_pos, bool);
852 static void reseat_1 (struct it *, struct text_pos, bool);
853 static bool next_element_from_display_vector (struct it *);
854 static bool next_element_from_string (struct it *);
855 static bool next_element_from_c_string (struct it *);
856 static bool next_element_from_buffer (struct it *);
857 static bool next_element_from_composition (struct it *);
858 static bool next_element_from_image (struct it *);
859 static bool next_element_from_stretch (struct it *);
860 #ifdef HAVE_XWIDGETS
861 static bool next_element_from_xwidget (struct it *);
862 #endif
863 static void load_overlay_strings (struct it *, ptrdiff_t);
864 static bool get_next_display_element (struct it *);
865 static enum move_it_result
866 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
867 enum move_operation_enum);
868 static void get_visually_first_element (struct it *);
869 static void compute_stop_pos (struct it *);
870 static int face_before_or_after_it_pos (struct it *, bool);
871 static ptrdiff_t next_overlay_change (ptrdiff_t);
872 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
873 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
874 static int handle_single_display_spec (struct it *, Lisp_Object,
875 Lisp_Object, Lisp_Object,
876 struct text_pos *, ptrdiff_t, int, bool);
877 static int underlying_face_id (struct it *);
878
879 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
880 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
881
882 #ifdef HAVE_WINDOW_SYSTEM
883
884 static void update_tool_bar (struct frame *, bool);
885 static void x_draw_bottom_divider (struct window *w);
886 static void notice_overwritten_cursor (struct window *,
887 enum glyph_row_area,
888 int, int, int, int);
889 static int normal_char_height (struct font *, int);
890 static void normal_char_ascent_descent (struct font *, int, int *, int *);
891
892 static void append_stretch_glyph (struct it *, Lisp_Object,
893 int, int, int);
894
895 static Lisp_Object get_it_property (struct it *, Lisp_Object);
896 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
897 struct font *, int, bool);
898
899 #endif /* HAVE_WINDOW_SYSTEM */
900
901 static void produce_special_glyphs (struct it *, enum display_element_type);
902 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
903 static bool coords_in_mouse_face_p (struct window *, int, int);
904
905
906 \f
907 /***********************************************************************
908 Window display dimensions
909 ***********************************************************************/
910
911 /* Return the bottom boundary y-position for text lines in window W.
912 This is the first y position at which a line cannot start.
913 It is relative to the top of the window.
914
915 This is the height of W minus the height of a mode line, if any. */
916
917 int
918 window_text_bottom_y (struct window *w)
919 {
920 int height = WINDOW_PIXEL_HEIGHT (w);
921
922 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
923
924 if (WINDOW_WANTS_MODELINE_P (w))
925 height -= CURRENT_MODE_LINE_HEIGHT (w);
926
927 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
928
929 return height;
930 }
931
932 /* Return the pixel width of display area AREA of window W.
933 ANY_AREA means return the total width of W, not including
934 fringes to the left and right of the window. */
935
936 int
937 window_box_width (struct window *w, enum glyph_row_area area)
938 {
939 int width = w->pixel_width;
940
941 if (!w->pseudo_window_p)
942 {
943 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
944 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
945
946 if (area == TEXT_AREA)
947 width -= (WINDOW_MARGINS_WIDTH (w)
948 + WINDOW_FRINGES_WIDTH (w));
949 else if (area == LEFT_MARGIN_AREA)
950 width = WINDOW_LEFT_MARGIN_WIDTH (w);
951 else if (area == RIGHT_MARGIN_AREA)
952 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
953 }
954
955 /* With wide margins, fringes, etc. we might end up with a negative
956 width, correct that here. */
957 return max (0, width);
958 }
959
960
961 /* Return the pixel height of the display area of window W, not
962 including mode lines of W, if any. */
963
964 int
965 window_box_height (struct window *w)
966 {
967 struct frame *f = XFRAME (w->frame);
968 int height = WINDOW_PIXEL_HEIGHT (w);
969
970 eassert (height >= 0);
971
972 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
973 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
974
975 /* Note: the code below that determines the mode-line/header-line
976 height is essentially the same as that contained in the macro
977 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
978 the appropriate glyph row has its `mode_line_p' flag set,
979 and if it doesn't, uses estimate_mode_line_height instead. */
980
981 if (WINDOW_WANTS_MODELINE_P (w))
982 {
983 struct glyph_row *ml_row
984 = (w->current_matrix && w->current_matrix->rows
985 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
986 : 0);
987 if (ml_row && ml_row->mode_line_p)
988 height -= ml_row->height;
989 else
990 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
991 }
992
993 if (WINDOW_WANTS_HEADER_LINE_P (w))
994 {
995 struct glyph_row *hl_row
996 = (w->current_matrix && w->current_matrix->rows
997 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
998 : 0);
999 if (hl_row && hl_row->mode_line_p)
1000 height -= hl_row->height;
1001 else
1002 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1003 }
1004
1005 /* With a very small font and a mode-line that's taller than
1006 default, we might end up with a negative height. */
1007 return max (0, height);
1008 }
1009
1010 /* Return the window-relative coordinate of the left edge of display
1011 area AREA of window W. ANY_AREA means return the left edge of the
1012 whole window, to the right of the left fringe of W. */
1013
1014 int
1015 window_box_left_offset (struct window *w, enum glyph_row_area area)
1016 {
1017 int x;
1018
1019 if (w->pseudo_window_p)
1020 return 0;
1021
1022 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1023
1024 if (area == TEXT_AREA)
1025 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1026 + window_box_width (w, LEFT_MARGIN_AREA));
1027 else if (area == RIGHT_MARGIN_AREA)
1028 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1029 + window_box_width (w, LEFT_MARGIN_AREA)
1030 + window_box_width (w, TEXT_AREA)
1031 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1032 ? 0
1033 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1034 else if (area == LEFT_MARGIN_AREA
1035 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1036 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1037
1038 /* Don't return more than the window's pixel width. */
1039 return min (x, w->pixel_width);
1040 }
1041
1042
1043 /* Return the window-relative coordinate of the right edge of display
1044 area AREA of window W. ANY_AREA means return the right edge of the
1045 whole window, to the left of the right fringe of W. */
1046
1047 static int
1048 window_box_right_offset (struct window *w, enum glyph_row_area area)
1049 {
1050 /* Don't return more than the window's pixel width. */
1051 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1052 w->pixel_width);
1053 }
1054
1055 /* Return the frame-relative coordinate of the left edge of display
1056 area AREA of window W. ANY_AREA means return the left edge of the
1057 whole window, to the right of the left fringe of W. */
1058
1059 int
1060 window_box_left (struct window *w, enum glyph_row_area area)
1061 {
1062 struct frame *f = XFRAME (w->frame);
1063 int x;
1064
1065 if (w->pseudo_window_p)
1066 return FRAME_INTERNAL_BORDER_WIDTH (f);
1067
1068 x = (WINDOW_LEFT_EDGE_X (w)
1069 + window_box_left_offset (w, area));
1070
1071 return x;
1072 }
1073
1074
1075 /* Return the frame-relative coordinate of the right edge of display
1076 area AREA of window W. ANY_AREA means return the right edge of the
1077 whole window, to the left of the right fringe of W. */
1078
1079 int
1080 window_box_right (struct window *w, enum glyph_row_area area)
1081 {
1082 return window_box_left (w, area) + window_box_width (w, area);
1083 }
1084
1085 /* Get the bounding box of the display area AREA of window W, without
1086 mode lines, in frame-relative coordinates. ANY_AREA means the
1087 whole window, not including the left and right fringes of
1088 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1089 coordinates of the upper-left corner of the box. Return in
1090 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1091
1092 void
1093 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1094 int *box_y, int *box_width, int *box_height)
1095 {
1096 if (box_width)
1097 *box_width = window_box_width (w, area);
1098 if (box_height)
1099 *box_height = window_box_height (w);
1100 if (box_x)
1101 *box_x = window_box_left (w, area);
1102 if (box_y)
1103 {
1104 *box_y = WINDOW_TOP_EDGE_Y (w);
1105 if (WINDOW_WANTS_HEADER_LINE_P (w))
1106 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1107 }
1108 }
1109
1110 #ifdef HAVE_WINDOW_SYSTEM
1111
1112 /* Get the bounding box of the display area AREA of window W, without
1113 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1114 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1115 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1116 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1117 box. */
1118
1119 static void
1120 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1121 int *bottom_right_x, int *bottom_right_y)
1122 {
1123 window_box (w, ANY_AREA, top_left_x, top_left_y,
1124 bottom_right_x, bottom_right_y);
1125 *bottom_right_x += *top_left_x;
1126 *bottom_right_y += *top_left_y;
1127 }
1128
1129 #endif /* HAVE_WINDOW_SYSTEM */
1130
1131 /***********************************************************************
1132 Utilities
1133 ***********************************************************************/
1134
1135 /* Return the bottom y-position of the line the iterator IT is in.
1136 This can modify IT's settings. */
1137
1138 int
1139 line_bottom_y (struct it *it)
1140 {
1141 int line_height = it->max_ascent + it->max_descent;
1142 int line_top_y = it->current_y;
1143
1144 if (line_height == 0)
1145 {
1146 if (last_height)
1147 line_height = last_height;
1148 else if (IT_CHARPOS (*it) < ZV)
1149 {
1150 move_it_by_lines (it, 1);
1151 line_height = (it->max_ascent || it->max_descent
1152 ? it->max_ascent + it->max_descent
1153 : last_height);
1154 }
1155 else
1156 {
1157 struct glyph_row *row = it->glyph_row;
1158
1159 /* Use the default character height. */
1160 it->glyph_row = NULL;
1161 it->what = IT_CHARACTER;
1162 it->c = ' ';
1163 it->len = 1;
1164 PRODUCE_GLYPHS (it);
1165 line_height = it->ascent + it->descent;
1166 it->glyph_row = row;
1167 }
1168 }
1169
1170 return line_top_y + line_height;
1171 }
1172
1173 DEFUN ("line-pixel-height", Fline_pixel_height,
1174 Sline_pixel_height, 0, 0, 0,
1175 doc: /* Return height in pixels of text line in the selected window.
1176
1177 Value is the height in pixels of the line at point. */)
1178 (void)
1179 {
1180 struct it it;
1181 struct text_pos pt;
1182 struct window *w = XWINDOW (selected_window);
1183 struct buffer *old_buffer = NULL;
1184 Lisp_Object result;
1185
1186 if (XBUFFER (w->contents) != current_buffer)
1187 {
1188 old_buffer = current_buffer;
1189 set_buffer_internal_1 (XBUFFER (w->contents));
1190 }
1191 SET_TEXT_POS (pt, PT, PT_BYTE);
1192 start_display (&it, w, pt);
1193 it.vpos = it.current_y = 0;
1194 last_height = 0;
1195 result = make_number (line_bottom_y (&it));
1196 if (old_buffer)
1197 set_buffer_internal_1 (old_buffer);
1198
1199 return result;
1200 }
1201
1202 /* Return the default pixel height of text lines in window W. The
1203 value is the canonical height of the W frame's default font, plus
1204 any extra space required by the line-spacing variable or frame
1205 parameter.
1206
1207 Implementation note: this ignores any line-spacing text properties
1208 put on the newline characters. This is because those properties
1209 only affect the _screen_ line ending in the newline (i.e., in a
1210 continued line, only the last screen line will be affected), which
1211 means only a small number of lines in a buffer can ever use this
1212 feature. Since this function is used to compute the default pixel
1213 equivalent of text lines in a window, we can safely ignore those
1214 few lines. For the same reasons, we ignore the line-height
1215 properties. */
1216 int
1217 default_line_pixel_height (struct window *w)
1218 {
1219 struct frame *f = WINDOW_XFRAME (w);
1220 int height = FRAME_LINE_HEIGHT (f);
1221
1222 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1223 {
1224 struct buffer *b = XBUFFER (w->contents);
1225 Lisp_Object val = BVAR (b, extra_line_spacing);
1226
1227 if (NILP (val))
1228 val = BVAR (&buffer_defaults, extra_line_spacing);
1229 if (!NILP (val))
1230 {
1231 if (RANGED_INTEGERP (0, val, INT_MAX))
1232 height += XFASTINT (val);
1233 else if (FLOATP (val))
1234 {
1235 int addon = XFLOAT_DATA (val) * height + 0.5;
1236
1237 if (addon >= 0)
1238 height += addon;
1239 }
1240 }
1241 else
1242 height += f->extra_line_spacing;
1243 }
1244
1245 return height;
1246 }
1247
1248 /* Subroutine of pos_visible_p below. Extracts a display string, if
1249 any, from the display spec given as its argument. */
1250 static Lisp_Object
1251 string_from_display_spec (Lisp_Object spec)
1252 {
1253 if (CONSP (spec))
1254 {
1255 while (CONSP (spec))
1256 {
1257 if (STRINGP (XCAR (spec)))
1258 return XCAR (spec);
1259 spec = XCDR (spec);
1260 }
1261 }
1262 else if (VECTORP (spec))
1263 {
1264 ptrdiff_t i;
1265
1266 for (i = 0; i < ASIZE (spec); i++)
1267 {
1268 if (STRINGP (AREF (spec, i)))
1269 return AREF (spec, i);
1270 }
1271 return Qnil;
1272 }
1273
1274 return spec;
1275 }
1276
1277
1278 /* Limit insanely large values of W->hscroll on frame F to the largest
1279 value that will still prevent first_visible_x and last_visible_x of
1280 'struct it' from overflowing an int. */
1281 static int
1282 window_hscroll_limited (struct window *w, struct frame *f)
1283 {
1284 ptrdiff_t window_hscroll = w->hscroll;
1285 int window_text_width = window_box_width (w, TEXT_AREA);
1286 int colwidth = FRAME_COLUMN_WIDTH (f);
1287
1288 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1289 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1290
1291 return window_hscroll;
1292 }
1293
1294 /* Return true if position CHARPOS is visible in window W.
1295 CHARPOS < 0 means return info about WINDOW_END position.
1296 If visible, set *X and *Y to pixel coordinates of top left corner.
1297 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1298 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1299
1300 bool
1301 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1302 int *rtop, int *rbot, int *rowh, int *vpos)
1303 {
1304 struct it it;
1305 void *itdata = bidi_shelve_cache ();
1306 struct text_pos top;
1307 bool visible_p = false;
1308 struct buffer *old_buffer = NULL;
1309 bool r2l = false;
1310
1311 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1312 return visible_p;
1313
1314 if (XBUFFER (w->contents) != current_buffer)
1315 {
1316 old_buffer = current_buffer;
1317 set_buffer_internal_1 (XBUFFER (w->contents));
1318 }
1319
1320 SET_TEXT_POS_FROM_MARKER (top, w->start);
1321 /* Scrolling a minibuffer window via scroll bar when the echo area
1322 shows long text sometimes resets the minibuffer contents behind
1323 our backs. */
1324 if (CHARPOS (top) > ZV)
1325 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1326
1327 /* Compute exact mode line heights. */
1328 if (WINDOW_WANTS_MODELINE_P (w))
1329 w->mode_line_height
1330 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1331 BVAR (current_buffer, mode_line_format));
1332
1333 if (WINDOW_WANTS_HEADER_LINE_P (w))
1334 w->header_line_height
1335 = display_mode_line (w, HEADER_LINE_FACE_ID,
1336 BVAR (current_buffer, header_line_format));
1337
1338 start_display (&it, w, top);
1339 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1340 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1341
1342 if (charpos >= 0
1343 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1344 && IT_CHARPOS (it) >= charpos)
1345 /* When scanning backwards under bidi iteration, move_it_to
1346 stops at or _before_ CHARPOS, because it stops at or to
1347 the _right_ of the character at CHARPOS. */
1348 || (it.bidi_p && it.bidi_it.scan_dir == -1
1349 && IT_CHARPOS (it) <= charpos)))
1350 {
1351 /* We have reached CHARPOS, or passed it. How the call to
1352 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1353 or covered by a display property, move_it_to stops at the end
1354 of the invisible text, to the right of CHARPOS. (ii) If
1355 CHARPOS is in a display vector, move_it_to stops on its last
1356 glyph. */
1357 int top_x = it.current_x;
1358 int top_y = it.current_y;
1359 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1360 int bottom_y;
1361 struct it save_it;
1362 void *save_it_data = NULL;
1363
1364 /* Calling line_bottom_y may change it.method, it.position, etc. */
1365 SAVE_IT (save_it, it, save_it_data);
1366 last_height = 0;
1367 bottom_y = line_bottom_y (&it);
1368 if (top_y < window_top_y)
1369 visible_p = bottom_y > window_top_y;
1370 else if (top_y < it.last_visible_y)
1371 visible_p = true;
1372 if (bottom_y >= it.last_visible_y
1373 && it.bidi_p && it.bidi_it.scan_dir == -1
1374 && IT_CHARPOS (it) < charpos)
1375 {
1376 /* When the last line of the window is scanned backwards
1377 under bidi iteration, we could be duped into thinking
1378 that we have passed CHARPOS, when in fact move_it_to
1379 simply stopped short of CHARPOS because it reached
1380 last_visible_y. To see if that's what happened, we call
1381 move_it_to again with a slightly larger vertical limit,
1382 and see if it actually moved vertically; if it did, we
1383 didn't really reach CHARPOS, which is beyond window end. */
1384 /* Why 10? because we don't know how many canonical lines
1385 will the height of the next line(s) be. So we guess. */
1386 int ten_more_lines = 10 * default_line_pixel_height (w);
1387
1388 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1389 MOVE_TO_POS | MOVE_TO_Y);
1390 if (it.current_y > top_y)
1391 visible_p = false;
1392
1393 }
1394 RESTORE_IT (&it, &save_it, save_it_data);
1395 if (visible_p)
1396 {
1397 if (it.method == GET_FROM_DISPLAY_VECTOR)
1398 {
1399 /* We stopped on the last glyph of a display vector.
1400 Try and recompute. Hack alert! */
1401 if (charpos < 2 || top.charpos >= charpos)
1402 top_x = it.glyph_row->x;
1403 else
1404 {
1405 struct it it2, it2_prev;
1406 /* The idea is to get to the previous buffer
1407 position, consume the character there, and use
1408 the pixel coordinates we get after that. But if
1409 the previous buffer position is also displayed
1410 from a display vector, we need to consume all of
1411 the glyphs from that display vector. */
1412 start_display (&it2, w, top);
1413 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1414 /* If we didn't get to CHARPOS - 1, there's some
1415 replacing display property at that position, and
1416 we stopped after it. That is exactly the place
1417 whose coordinates we want. */
1418 if (IT_CHARPOS (it2) != charpos - 1)
1419 it2_prev = it2;
1420 else
1421 {
1422 /* Iterate until we get out of the display
1423 vector that displays the character at
1424 CHARPOS - 1. */
1425 do {
1426 get_next_display_element (&it2);
1427 PRODUCE_GLYPHS (&it2);
1428 it2_prev = it2;
1429 set_iterator_to_next (&it2, true);
1430 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1431 && IT_CHARPOS (it2) < charpos);
1432 }
1433 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1434 || it2_prev.current_x > it2_prev.last_visible_x)
1435 top_x = it.glyph_row->x;
1436 else
1437 {
1438 top_x = it2_prev.current_x;
1439 top_y = it2_prev.current_y;
1440 }
1441 }
1442 }
1443 else if (IT_CHARPOS (it) != charpos)
1444 {
1445 Lisp_Object cpos = make_number (charpos);
1446 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1447 Lisp_Object string = string_from_display_spec (spec);
1448 struct text_pos tpos;
1449 bool newline_in_string
1450 = (STRINGP (string)
1451 && memchr (SDATA (string), '\n', SBYTES (string)));
1452
1453 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1454 bool replacing_spec_p
1455 = (!NILP (spec)
1456 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1457 charpos, FRAME_WINDOW_P (it.f)));
1458 /* The tricky code below is needed because there's a
1459 discrepancy between move_it_to and how we set cursor
1460 when PT is at the beginning of a portion of text
1461 covered by a display property or an overlay with a
1462 display property, or the display line ends in a
1463 newline from a display string. move_it_to will stop
1464 _after_ such display strings, whereas
1465 set_cursor_from_row conspires with cursor_row_p to
1466 place the cursor on the first glyph produced from the
1467 display string. */
1468
1469 /* We have overshoot PT because it is covered by a
1470 display property that replaces the text it covers.
1471 If the string includes embedded newlines, we are also
1472 in the wrong display line. Backtrack to the correct
1473 line, where the display property begins. */
1474 if (replacing_spec_p)
1475 {
1476 Lisp_Object startpos, endpos;
1477 EMACS_INT start, end;
1478 struct it it3;
1479
1480 /* Find the first and the last buffer positions
1481 covered by the display string. */
1482 endpos =
1483 Fnext_single_char_property_change (cpos, Qdisplay,
1484 Qnil, Qnil);
1485 startpos =
1486 Fprevious_single_char_property_change (endpos, Qdisplay,
1487 Qnil, Qnil);
1488 start = XFASTINT (startpos);
1489 end = XFASTINT (endpos);
1490 /* Move to the last buffer position before the
1491 display property. */
1492 start_display (&it3, w, top);
1493 if (start > CHARPOS (top))
1494 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1495 /* Move forward one more line if the position before
1496 the display string is a newline or if it is the
1497 rightmost character on a line that is
1498 continued or word-wrapped. */
1499 if (it3.method == GET_FROM_BUFFER
1500 && (it3.c == '\n'
1501 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1502 move_it_by_lines (&it3, 1);
1503 else if (move_it_in_display_line_to (&it3, -1,
1504 it3.current_x
1505 + it3.pixel_width,
1506 MOVE_TO_X)
1507 == MOVE_LINE_CONTINUED)
1508 {
1509 move_it_by_lines (&it3, 1);
1510 /* When we are under word-wrap, the #$@%!
1511 move_it_by_lines moves 2 lines, so we need to
1512 fix that up. */
1513 if (it3.line_wrap == WORD_WRAP)
1514 move_it_by_lines (&it3, -1);
1515 }
1516
1517 /* Record the vertical coordinate of the display
1518 line where we wound up. */
1519 top_y = it3.current_y;
1520 if (it3.bidi_p)
1521 {
1522 /* When characters are reordered for display,
1523 the character displayed to the left of the
1524 display string could be _after_ the display
1525 property in the logical order. Use the
1526 smallest vertical position of these two. */
1527 start_display (&it3, w, top);
1528 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1529 if (it3.current_y < top_y)
1530 top_y = it3.current_y;
1531 }
1532 /* Move from the top of the window to the beginning
1533 of the display line where the display string
1534 begins. */
1535 start_display (&it3, w, top);
1536 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1537 /* If it3_moved stays false after the 'while' loop
1538 below, that means we already were at a newline
1539 before the loop (e.g., the display string begins
1540 with a newline), so we don't need to (and cannot)
1541 inspect the glyphs of it3.glyph_row, because
1542 PRODUCE_GLYPHS will not produce anything for a
1543 newline, and thus it3.glyph_row stays at its
1544 stale content it got at top of the window. */
1545 bool it3_moved = false;
1546 /* Finally, advance the iterator until we hit the
1547 first display element whose character position is
1548 CHARPOS, or until the first newline from the
1549 display string, which signals the end of the
1550 display line. */
1551 while (get_next_display_element (&it3))
1552 {
1553 PRODUCE_GLYPHS (&it3);
1554 if (IT_CHARPOS (it3) == charpos
1555 || ITERATOR_AT_END_OF_LINE_P (&it3))
1556 break;
1557 it3_moved = true;
1558 set_iterator_to_next (&it3, false);
1559 }
1560 top_x = it3.current_x - it3.pixel_width;
1561 /* Normally, we would exit the above loop because we
1562 found the display element whose character
1563 position is CHARPOS. For the contingency that we
1564 didn't, and stopped at the first newline from the
1565 display string, move back over the glyphs
1566 produced from the string, until we find the
1567 rightmost glyph not from the string. */
1568 if (it3_moved
1569 && newline_in_string
1570 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1571 {
1572 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1573 + it3.glyph_row->used[TEXT_AREA];
1574
1575 while (EQ ((g - 1)->object, string))
1576 {
1577 --g;
1578 top_x -= g->pixel_width;
1579 }
1580 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1581 + it3.glyph_row->used[TEXT_AREA]);
1582 }
1583 }
1584 }
1585
1586 *x = top_x;
1587 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1588 *rtop = max (0, window_top_y - top_y);
1589 *rbot = max (0, bottom_y - it.last_visible_y);
1590 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1591 - max (top_y, window_top_y)));
1592 *vpos = it.vpos;
1593 if (it.bidi_it.paragraph_dir == R2L)
1594 r2l = true;
1595 }
1596 }
1597 else
1598 {
1599 /* Either we were asked to provide info about WINDOW_END, or
1600 CHARPOS is in the partially visible glyph row at end of
1601 window. */
1602 struct it it2;
1603 void *it2data = NULL;
1604
1605 SAVE_IT (it2, it, it2data);
1606 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1607 move_it_by_lines (&it, 1);
1608 if (charpos < IT_CHARPOS (it)
1609 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1610 {
1611 visible_p = true;
1612 RESTORE_IT (&it2, &it2, it2data);
1613 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1614 *x = it2.current_x;
1615 *y = it2.current_y + it2.max_ascent - it2.ascent;
1616 *rtop = max (0, -it2.current_y);
1617 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1618 - it.last_visible_y));
1619 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1620 it.last_visible_y)
1621 - max (it2.current_y,
1622 WINDOW_HEADER_LINE_HEIGHT (w))));
1623 *vpos = it2.vpos;
1624 if (it2.bidi_it.paragraph_dir == R2L)
1625 r2l = true;
1626 }
1627 else
1628 bidi_unshelve_cache (it2data, true);
1629 }
1630 bidi_unshelve_cache (itdata, false);
1631
1632 if (old_buffer)
1633 set_buffer_internal_1 (old_buffer);
1634
1635 if (visible_p)
1636 {
1637 if (w->hscroll > 0)
1638 *x -=
1639 window_hscroll_limited (w, WINDOW_XFRAME (w))
1640 * WINDOW_FRAME_COLUMN_WIDTH (w);
1641 /* For lines in an R2L paragraph, we need to mirror the X pixel
1642 coordinate wrt the text area. For the reasons, see the
1643 commentary in buffer_posn_from_coords and the explanation of
1644 the geometry used by the move_it_* functions at the end of
1645 the large commentary near the beginning of this file. */
1646 if (r2l)
1647 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1648 }
1649
1650 #if false
1651 /* Debugging code. */
1652 if (visible_p)
1653 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1654 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1655 else
1656 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1657 #endif
1658
1659 return visible_p;
1660 }
1661
1662
1663 /* Return the next character from STR. Return in *LEN the length of
1664 the character. This is like STRING_CHAR_AND_LENGTH but never
1665 returns an invalid character. If we find one, we return a `?', but
1666 with the length of the invalid character. */
1667
1668 static int
1669 string_char_and_length (const unsigned char *str, int *len)
1670 {
1671 int c;
1672
1673 c = STRING_CHAR_AND_LENGTH (str, *len);
1674 if (!CHAR_VALID_P (c))
1675 /* We may not change the length here because other places in Emacs
1676 don't use this function, i.e. they silently accept invalid
1677 characters. */
1678 c = '?';
1679
1680 return c;
1681 }
1682
1683
1684
1685 /* Given a position POS containing a valid character and byte position
1686 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1687
1688 static struct text_pos
1689 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1690 {
1691 eassert (STRINGP (string) && nchars >= 0);
1692
1693 if (STRING_MULTIBYTE (string))
1694 {
1695 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1696 int len;
1697
1698 while (nchars--)
1699 {
1700 string_char_and_length (p, &len);
1701 p += len;
1702 CHARPOS (pos) += 1;
1703 BYTEPOS (pos) += len;
1704 }
1705 }
1706 else
1707 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1708
1709 return pos;
1710 }
1711
1712
1713 /* Value is the text position, i.e. character and byte position,
1714 for character position CHARPOS in STRING. */
1715
1716 static struct text_pos
1717 string_pos (ptrdiff_t charpos, Lisp_Object string)
1718 {
1719 struct text_pos pos;
1720 eassert (STRINGP (string));
1721 eassert (charpos >= 0);
1722 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1723 return pos;
1724 }
1725
1726
1727 /* Value is a text position, i.e. character and byte position, for
1728 character position CHARPOS in C string S. MULTIBYTE_P
1729 means recognize multibyte characters. */
1730
1731 static struct text_pos
1732 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1733 {
1734 struct text_pos pos;
1735
1736 eassert (s != NULL);
1737 eassert (charpos >= 0);
1738
1739 if (multibyte_p)
1740 {
1741 int len;
1742
1743 SET_TEXT_POS (pos, 0, 0);
1744 while (charpos--)
1745 {
1746 string_char_and_length ((const unsigned char *) s, &len);
1747 s += len;
1748 CHARPOS (pos) += 1;
1749 BYTEPOS (pos) += len;
1750 }
1751 }
1752 else
1753 SET_TEXT_POS (pos, charpos, charpos);
1754
1755 return pos;
1756 }
1757
1758
1759 /* Value is the number of characters in C string S. MULTIBYTE_P
1760 means recognize multibyte characters. */
1761
1762 static ptrdiff_t
1763 number_of_chars (const char *s, bool multibyte_p)
1764 {
1765 ptrdiff_t nchars;
1766
1767 if (multibyte_p)
1768 {
1769 ptrdiff_t rest = strlen (s);
1770 int len;
1771 const unsigned char *p = (const unsigned char *) s;
1772
1773 for (nchars = 0; rest > 0; ++nchars)
1774 {
1775 string_char_and_length (p, &len);
1776 rest -= len, p += len;
1777 }
1778 }
1779 else
1780 nchars = strlen (s);
1781
1782 return nchars;
1783 }
1784
1785
1786 /* Compute byte position NEWPOS->bytepos corresponding to
1787 NEWPOS->charpos. POS is a known position in string STRING.
1788 NEWPOS->charpos must be >= POS.charpos. */
1789
1790 static void
1791 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1792 {
1793 eassert (STRINGP (string));
1794 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1795
1796 if (STRING_MULTIBYTE (string))
1797 *newpos = string_pos_nchars_ahead (pos, string,
1798 CHARPOS (*newpos) - CHARPOS (pos));
1799 else
1800 BYTEPOS (*newpos) = CHARPOS (*newpos);
1801 }
1802
1803 /* EXPORT:
1804 Return an estimation of the pixel height of mode or header lines on
1805 frame F. FACE_ID specifies what line's height to estimate. */
1806
1807 int
1808 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1809 {
1810 #ifdef HAVE_WINDOW_SYSTEM
1811 if (FRAME_WINDOW_P (f))
1812 {
1813 int height = FONT_HEIGHT (FRAME_FONT (f));
1814
1815 /* This function is called so early when Emacs starts that the face
1816 cache and mode line face are not yet initialized. */
1817 if (FRAME_FACE_CACHE (f))
1818 {
1819 struct face *face = FACE_FROM_ID (f, face_id);
1820 if (face)
1821 {
1822 if (face->font)
1823 height = normal_char_height (face->font, -1);
1824 if (face->box_line_width > 0)
1825 height += 2 * face->box_line_width;
1826 }
1827 }
1828
1829 return height;
1830 }
1831 #endif
1832
1833 return 1;
1834 }
1835
1836 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1837 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1838 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1839 not force the value into range. */
1840
1841 void
1842 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1843 NativeRectangle *bounds, bool noclip)
1844 {
1845
1846 #ifdef HAVE_WINDOW_SYSTEM
1847 if (FRAME_WINDOW_P (f))
1848 {
1849 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1850 even for negative values. */
1851 if (pix_x < 0)
1852 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1853 if (pix_y < 0)
1854 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1855
1856 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1857 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1858
1859 if (bounds)
1860 STORE_NATIVE_RECT (*bounds,
1861 FRAME_COL_TO_PIXEL_X (f, pix_x),
1862 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1863 FRAME_COLUMN_WIDTH (f) - 1,
1864 FRAME_LINE_HEIGHT (f) - 1);
1865
1866 /* PXW: Should we clip pixels before converting to columns/lines? */
1867 if (!noclip)
1868 {
1869 if (pix_x < 0)
1870 pix_x = 0;
1871 else if (pix_x > FRAME_TOTAL_COLS (f))
1872 pix_x = FRAME_TOTAL_COLS (f);
1873
1874 if (pix_y < 0)
1875 pix_y = 0;
1876 else if (pix_y > FRAME_TOTAL_LINES (f))
1877 pix_y = FRAME_TOTAL_LINES (f);
1878 }
1879 }
1880 #endif
1881
1882 *x = pix_x;
1883 *y = pix_y;
1884 }
1885
1886
1887 /* Find the glyph under window-relative coordinates X/Y in window W.
1888 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1889 strings. Return in *HPOS and *VPOS the row and column number of
1890 the glyph found. Return in *AREA the glyph area containing X.
1891 Value is a pointer to the glyph found or null if X/Y is not on
1892 text, or we can't tell because W's current matrix is not up to
1893 date. */
1894
1895 static struct glyph *
1896 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1897 int *dx, int *dy, int *area)
1898 {
1899 struct glyph *glyph, *end;
1900 struct glyph_row *row = NULL;
1901 int x0, i;
1902
1903 /* Find row containing Y. Give up if some row is not enabled. */
1904 for (i = 0; i < w->current_matrix->nrows; ++i)
1905 {
1906 row = MATRIX_ROW (w->current_matrix, i);
1907 if (!row->enabled_p)
1908 return NULL;
1909 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1910 break;
1911 }
1912
1913 *vpos = i;
1914 *hpos = 0;
1915
1916 /* Give up if Y is not in the window. */
1917 if (i == w->current_matrix->nrows)
1918 return NULL;
1919
1920 /* Get the glyph area containing X. */
1921 if (w->pseudo_window_p)
1922 {
1923 *area = TEXT_AREA;
1924 x0 = 0;
1925 }
1926 else
1927 {
1928 if (x < window_box_left_offset (w, TEXT_AREA))
1929 {
1930 *area = LEFT_MARGIN_AREA;
1931 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1932 }
1933 else if (x < window_box_right_offset (w, TEXT_AREA))
1934 {
1935 *area = TEXT_AREA;
1936 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1937 }
1938 else
1939 {
1940 *area = RIGHT_MARGIN_AREA;
1941 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1942 }
1943 }
1944
1945 /* Find glyph containing X. */
1946 glyph = row->glyphs[*area];
1947 end = glyph + row->used[*area];
1948 x -= x0;
1949 while (glyph < end && x >= glyph->pixel_width)
1950 {
1951 x -= glyph->pixel_width;
1952 ++glyph;
1953 }
1954
1955 if (glyph == end)
1956 return NULL;
1957
1958 if (dx)
1959 {
1960 *dx = x;
1961 *dy = y - (row->y + row->ascent - glyph->ascent);
1962 }
1963
1964 *hpos = glyph - row->glyphs[*area];
1965 return glyph;
1966 }
1967
1968 /* Convert frame-relative x/y to coordinates relative to window W.
1969 Takes pseudo-windows into account. */
1970
1971 static void
1972 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1973 {
1974 if (w->pseudo_window_p)
1975 {
1976 /* A pseudo-window is always full-width, and starts at the
1977 left edge of the frame, plus a frame border. */
1978 struct frame *f = XFRAME (w->frame);
1979 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1980 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1981 }
1982 else
1983 {
1984 *x -= WINDOW_LEFT_EDGE_X (w);
1985 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1986 }
1987 }
1988
1989 #ifdef HAVE_WINDOW_SYSTEM
1990
1991 /* EXPORT:
1992 Return in RECTS[] at most N clipping rectangles for glyph string S.
1993 Return the number of stored rectangles. */
1994
1995 int
1996 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1997 {
1998 XRectangle r;
1999
2000 if (n <= 0)
2001 return 0;
2002
2003 if (s->row->full_width_p)
2004 {
2005 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2006 r.x = WINDOW_LEFT_EDGE_X (s->w);
2007 if (s->row->mode_line_p)
2008 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2009 else
2010 r.width = WINDOW_PIXEL_WIDTH (s->w);
2011
2012 /* Unless displaying a mode or menu bar line, which are always
2013 fully visible, clip to the visible part of the row. */
2014 if (s->w->pseudo_window_p)
2015 r.height = s->row->visible_height;
2016 else
2017 r.height = s->height;
2018 }
2019 else
2020 {
2021 /* This is a text line that may be partially visible. */
2022 r.x = window_box_left (s->w, s->area);
2023 r.width = window_box_width (s->w, s->area);
2024 r.height = s->row->visible_height;
2025 }
2026
2027 if (s->clip_head)
2028 if (r.x < s->clip_head->x)
2029 {
2030 if (r.width >= s->clip_head->x - r.x)
2031 r.width -= s->clip_head->x - r.x;
2032 else
2033 r.width = 0;
2034 r.x = s->clip_head->x;
2035 }
2036 if (s->clip_tail)
2037 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2038 {
2039 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2040 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2041 else
2042 r.width = 0;
2043 }
2044
2045 /* If S draws overlapping rows, it's sufficient to use the top and
2046 bottom of the window for clipping because this glyph string
2047 intentionally draws over other lines. */
2048 if (s->for_overlaps)
2049 {
2050 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2051 r.height = window_text_bottom_y (s->w) - r.y;
2052
2053 /* Alas, the above simple strategy does not work for the
2054 environments with anti-aliased text: if the same text is
2055 drawn onto the same place multiple times, it gets thicker.
2056 If the overlap we are processing is for the erased cursor, we
2057 take the intersection with the rectangle of the cursor. */
2058 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2059 {
2060 XRectangle rc, r_save = r;
2061
2062 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2063 rc.y = s->w->phys_cursor.y;
2064 rc.width = s->w->phys_cursor_width;
2065 rc.height = s->w->phys_cursor_height;
2066
2067 x_intersect_rectangles (&r_save, &rc, &r);
2068 }
2069 }
2070 else
2071 {
2072 /* Don't use S->y for clipping because it doesn't take partially
2073 visible lines into account. For example, it can be negative for
2074 partially visible lines at the top of a window. */
2075 if (!s->row->full_width_p
2076 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2077 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2078 else
2079 r.y = max (0, s->row->y);
2080 }
2081
2082 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2083
2084 /* If drawing the cursor, don't let glyph draw outside its
2085 advertised boundaries. Cleartype does this under some circumstances. */
2086 if (s->hl == DRAW_CURSOR)
2087 {
2088 struct glyph *glyph = s->first_glyph;
2089 int height, max_y;
2090
2091 if (s->x > r.x)
2092 {
2093 if (r.width >= s->x - r.x)
2094 r.width -= s->x - r.x;
2095 else /* R2L hscrolled row with cursor outside text area */
2096 r.width = 0;
2097 r.x = s->x;
2098 }
2099 r.width = min (r.width, glyph->pixel_width);
2100
2101 /* If r.y is below window bottom, ensure that we still see a cursor. */
2102 height = min (glyph->ascent + glyph->descent,
2103 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2104 max_y = window_text_bottom_y (s->w) - height;
2105 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2106 if (s->ybase - glyph->ascent > max_y)
2107 {
2108 r.y = max_y;
2109 r.height = height;
2110 }
2111 else
2112 {
2113 /* Don't draw cursor glyph taller than our actual glyph. */
2114 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2115 if (height < r.height)
2116 {
2117 max_y = r.y + r.height;
2118 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2119 r.height = min (max_y - r.y, height);
2120 }
2121 }
2122 }
2123
2124 if (s->row->clip)
2125 {
2126 XRectangle r_save = r;
2127
2128 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2129 r.width = 0;
2130 }
2131
2132 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2133 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2134 {
2135 #ifdef CONVERT_FROM_XRECT
2136 CONVERT_FROM_XRECT (r, *rects);
2137 #else
2138 *rects = r;
2139 #endif
2140 return 1;
2141 }
2142 else
2143 {
2144 /* If we are processing overlapping and allowed to return
2145 multiple clipping rectangles, we exclude the row of the glyph
2146 string from the clipping rectangle. This is to avoid drawing
2147 the same text on the environment with anti-aliasing. */
2148 #ifdef CONVERT_FROM_XRECT
2149 XRectangle rs[2];
2150 #else
2151 XRectangle *rs = rects;
2152 #endif
2153 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2154
2155 if (s->for_overlaps & OVERLAPS_PRED)
2156 {
2157 rs[i] = r;
2158 if (r.y + r.height > row_y)
2159 {
2160 if (r.y < row_y)
2161 rs[i].height = row_y - r.y;
2162 else
2163 rs[i].height = 0;
2164 }
2165 i++;
2166 }
2167 if (s->for_overlaps & OVERLAPS_SUCC)
2168 {
2169 rs[i] = r;
2170 if (r.y < row_y + s->row->visible_height)
2171 {
2172 if (r.y + r.height > row_y + s->row->visible_height)
2173 {
2174 rs[i].y = row_y + s->row->visible_height;
2175 rs[i].height = r.y + r.height - rs[i].y;
2176 }
2177 else
2178 rs[i].height = 0;
2179 }
2180 i++;
2181 }
2182
2183 n = i;
2184 #ifdef CONVERT_FROM_XRECT
2185 for (i = 0; i < n; i++)
2186 CONVERT_FROM_XRECT (rs[i], rects[i]);
2187 #endif
2188 return n;
2189 }
2190 }
2191
2192 /* EXPORT:
2193 Return in *NR the clipping rectangle for glyph string S. */
2194
2195 void
2196 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2197 {
2198 get_glyph_string_clip_rects (s, nr, 1);
2199 }
2200
2201
2202 /* EXPORT:
2203 Return the position and height of the phys cursor in window W.
2204 Set w->phys_cursor_width to width of phys cursor.
2205 */
2206
2207 void
2208 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2209 struct glyph *glyph, int *xp, int *yp, int *heightp)
2210 {
2211 struct frame *f = XFRAME (WINDOW_FRAME (w));
2212 int x, y, wd, h, h0, y0, ascent;
2213
2214 /* Compute the width of the rectangle to draw. If on a stretch
2215 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2216 rectangle as wide as the glyph, but use a canonical character
2217 width instead. */
2218 wd = glyph->pixel_width;
2219
2220 x = w->phys_cursor.x;
2221 if (x < 0)
2222 {
2223 wd += x;
2224 x = 0;
2225 }
2226
2227 if (glyph->type == STRETCH_GLYPH
2228 && !x_stretch_cursor_p)
2229 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2230 w->phys_cursor_width = wd;
2231
2232 /* Don't let the hollow cursor glyph descend below the glyph row's
2233 ascent value, lest the hollow cursor looks funny. */
2234 y = w->phys_cursor.y;
2235 ascent = row->ascent;
2236 if (row->ascent < glyph->ascent)
2237 {
2238 y =- glyph->ascent - row->ascent;
2239 ascent = glyph->ascent;
2240 }
2241
2242 /* If y is below window bottom, ensure that we still see a cursor. */
2243 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2244
2245 h = max (h0, ascent + glyph->descent);
2246 h0 = min (h0, ascent + glyph->descent);
2247
2248 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2249 if (y < y0)
2250 {
2251 h = max (h - (y0 - y) + 1, h0);
2252 y = y0 - 1;
2253 }
2254 else
2255 {
2256 y0 = window_text_bottom_y (w) - h0;
2257 if (y > y0)
2258 {
2259 h += y - y0;
2260 y = y0;
2261 }
2262 }
2263
2264 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2265 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2266 *heightp = h;
2267 }
2268
2269 /*
2270 * Remember which glyph the mouse is over.
2271 */
2272
2273 void
2274 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2275 {
2276 Lisp_Object window;
2277 struct window *w;
2278 struct glyph_row *r, *gr, *end_row;
2279 enum window_part part;
2280 enum glyph_row_area area;
2281 int x, y, width, height;
2282
2283 /* Try to determine frame pixel position and size of the glyph under
2284 frame pixel coordinates X/Y on frame F. */
2285
2286 if (window_resize_pixelwise)
2287 {
2288 width = height = 1;
2289 goto virtual_glyph;
2290 }
2291 else if (!f->glyphs_initialized_p
2292 || (window = window_from_coordinates (f, gx, gy, &part, false),
2293 NILP (window)))
2294 {
2295 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2296 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2297 goto virtual_glyph;
2298 }
2299
2300 w = XWINDOW (window);
2301 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2302 height = WINDOW_FRAME_LINE_HEIGHT (w);
2303
2304 x = window_relative_x_coord (w, part, gx);
2305 y = gy - WINDOW_TOP_EDGE_Y (w);
2306
2307 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2308 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2309
2310 if (w->pseudo_window_p)
2311 {
2312 area = TEXT_AREA;
2313 part = ON_MODE_LINE; /* Don't adjust margin. */
2314 goto text_glyph;
2315 }
2316
2317 switch (part)
2318 {
2319 case ON_LEFT_MARGIN:
2320 area = LEFT_MARGIN_AREA;
2321 goto text_glyph;
2322
2323 case ON_RIGHT_MARGIN:
2324 area = RIGHT_MARGIN_AREA;
2325 goto text_glyph;
2326
2327 case ON_HEADER_LINE:
2328 case ON_MODE_LINE:
2329 gr = (part == ON_HEADER_LINE
2330 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2331 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2332 gy = gr->y;
2333 area = TEXT_AREA;
2334 goto text_glyph_row_found;
2335
2336 case ON_TEXT:
2337 area = TEXT_AREA;
2338
2339 text_glyph:
2340 gr = 0; gy = 0;
2341 for (; r <= end_row && r->enabled_p; ++r)
2342 if (r->y + r->height > y)
2343 {
2344 gr = r; gy = r->y;
2345 break;
2346 }
2347
2348 text_glyph_row_found:
2349 if (gr && gy <= y)
2350 {
2351 struct glyph *g = gr->glyphs[area];
2352 struct glyph *end = g + gr->used[area];
2353
2354 height = gr->height;
2355 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2356 if (gx + g->pixel_width > x)
2357 break;
2358
2359 if (g < end)
2360 {
2361 if (g->type == IMAGE_GLYPH)
2362 {
2363 /* Don't remember when mouse is over image, as
2364 image may have hot-spots. */
2365 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2366 return;
2367 }
2368 width = g->pixel_width;
2369 }
2370 else
2371 {
2372 /* Use nominal char spacing at end of line. */
2373 x -= gx;
2374 gx += (x / width) * width;
2375 }
2376
2377 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2378 {
2379 gx += window_box_left_offset (w, area);
2380 /* Don't expand over the modeline to make sure the vertical
2381 drag cursor is shown early enough. */
2382 height = min (height,
2383 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2384 }
2385 }
2386 else
2387 {
2388 /* Use nominal line height at end of window. */
2389 gx = (x / width) * width;
2390 y -= gy;
2391 gy += (y / height) * height;
2392 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2393 /* See comment above. */
2394 height = min (height,
2395 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2396 }
2397 break;
2398
2399 case ON_LEFT_FRINGE:
2400 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2401 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2402 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2403 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2404 goto row_glyph;
2405
2406 case ON_RIGHT_FRINGE:
2407 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2408 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2409 : window_box_right_offset (w, TEXT_AREA));
2410 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2411 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2412 && !WINDOW_RIGHTMOST_P (w))
2413 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2414 /* Make sure the vertical border can get her own glyph to the
2415 right of the one we build here. */
2416 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2417 else
2418 width = WINDOW_PIXEL_WIDTH (w) - gx;
2419 else
2420 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2421
2422 goto row_glyph;
2423
2424 case ON_VERTICAL_BORDER:
2425 gx = WINDOW_PIXEL_WIDTH (w) - width;
2426 goto row_glyph;
2427
2428 case ON_VERTICAL_SCROLL_BAR:
2429 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2430 ? 0
2431 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2432 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2433 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2434 : 0)));
2435 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2436
2437 row_glyph:
2438 gr = 0, gy = 0;
2439 for (; r <= end_row && r->enabled_p; ++r)
2440 if (r->y + r->height > y)
2441 {
2442 gr = r; gy = r->y;
2443 break;
2444 }
2445
2446 if (gr && gy <= y)
2447 height = gr->height;
2448 else
2449 {
2450 /* Use nominal line height at end of window. */
2451 y -= gy;
2452 gy += (y / height) * height;
2453 }
2454 break;
2455
2456 case ON_RIGHT_DIVIDER:
2457 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2458 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2459 gy = 0;
2460 /* The bottom divider prevails. */
2461 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2462 goto add_edge;
2463
2464 case ON_BOTTOM_DIVIDER:
2465 gx = 0;
2466 width = WINDOW_PIXEL_WIDTH (w);
2467 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2468 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2469 goto add_edge;
2470
2471 default:
2472 ;
2473 virtual_glyph:
2474 /* If there is no glyph under the mouse, then we divide the screen
2475 into a grid of the smallest glyph in the frame, and use that
2476 as our "glyph". */
2477
2478 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2479 round down even for negative values. */
2480 if (gx < 0)
2481 gx -= width - 1;
2482 if (gy < 0)
2483 gy -= height - 1;
2484
2485 gx = (gx / width) * width;
2486 gy = (gy / height) * height;
2487
2488 goto store_rect;
2489 }
2490
2491 add_edge:
2492 gx += WINDOW_LEFT_EDGE_X (w);
2493 gy += WINDOW_TOP_EDGE_Y (w);
2494
2495 store_rect:
2496 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2497
2498 /* Visible feedback for debugging. */
2499 #if false && defined HAVE_X_WINDOWS
2500 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2501 f->output_data.x->normal_gc,
2502 gx, gy, width, height);
2503 #endif
2504 }
2505
2506
2507 #endif /* HAVE_WINDOW_SYSTEM */
2508
2509 static void
2510 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2511 {
2512 eassert (w);
2513 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2514 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2515 w->window_end_vpos
2516 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2517 }
2518
2519 /***********************************************************************
2520 Lisp form evaluation
2521 ***********************************************************************/
2522
2523 /* Error handler for safe_eval and safe_call. */
2524
2525 static Lisp_Object
2526 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2527 {
2528 add_to_log ("Error during redisplay: %S signaled %S",
2529 Flist (nargs, args), arg);
2530 return Qnil;
2531 }
2532
2533 /* Call function FUNC with the rest of NARGS - 1 arguments
2534 following. Return the result, or nil if something went
2535 wrong. Prevent redisplay during the evaluation. */
2536
2537 static Lisp_Object
2538 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2539 {
2540 Lisp_Object val;
2541
2542 if (inhibit_eval_during_redisplay)
2543 val = Qnil;
2544 else
2545 {
2546 ptrdiff_t i;
2547 ptrdiff_t count = SPECPDL_INDEX ();
2548 Lisp_Object *args;
2549 USE_SAFE_ALLOCA;
2550 SAFE_ALLOCA_LISP (args, nargs);
2551
2552 args[0] = func;
2553 for (i = 1; i < nargs; i++)
2554 args[i] = va_arg (ap, Lisp_Object);
2555
2556 specbind (Qinhibit_redisplay, Qt);
2557 if (inhibit_quit)
2558 specbind (Qinhibit_quit, Qt);
2559 /* Use Qt to ensure debugger does not run,
2560 so there is no possibility of wanting to redisplay. */
2561 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2562 safe_eval_handler);
2563 SAFE_FREE ();
2564 val = unbind_to (count, val);
2565 }
2566
2567 return val;
2568 }
2569
2570 Lisp_Object
2571 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2572 {
2573 Lisp_Object retval;
2574 va_list ap;
2575
2576 va_start (ap, func);
2577 retval = safe__call (false, nargs, func, ap);
2578 va_end (ap);
2579 return retval;
2580 }
2581
2582 /* Call function FN with one argument ARG.
2583 Return the result, or nil if something went wrong. */
2584
2585 Lisp_Object
2586 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2587 {
2588 return safe_call (2, fn, arg);
2589 }
2590
2591 static Lisp_Object
2592 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2593 {
2594 Lisp_Object retval;
2595 va_list ap;
2596
2597 va_start (ap, fn);
2598 retval = safe__call (inhibit_quit, 2, fn, ap);
2599 va_end (ap);
2600 return retval;
2601 }
2602
2603 Lisp_Object
2604 safe_eval (Lisp_Object sexpr)
2605 {
2606 return safe__call1 (false, Qeval, sexpr);
2607 }
2608
2609 static Lisp_Object
2610 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2611 {
2612 return safe__call1 (inhibit_quit, Qeval, sexpr);
2613 }
2614
2615 /* Call function FN with two arguments ARG1 and ARG2.
2616 Return the result, or nil if something went wrong. */
2617
2618 Lisp_Object
2619 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2620 {
2621 return safe_call (3, fn, arg1, arg2);
2622 }
2623
2624
2625 \f
2626 /***********************************************************************
2627 Debugging
2628 ***********************************************************************/
2629
2630 /* Define CHECK_IT to perform sanity checks on iterators.
2631 This is for debugging. It is too slow to do unconditionally. */
2632
2633 static void
2634 CHECK_IT (struct it *it)
2635 {
2636 #if false
2637 if (it->method == GET_FROM_STRING)
2638 {
2639 eassert (STRINGP (it->string));
2640 eassert (IT_STRING_CHARPOS (*it) >= 0);
2641 }
2642 else
2643 {
2644 eassert (IT_STRING_CHARPOS (*it) < 0);
2645 if (it->method == GET_FROM_BUFFER)
2646 {
2647 /* Check that character and byte positions agree. */
2648 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2649 }
2650 }
2651
2652 if (it->dpvec)
2653 eassert (it->current.dpvec_index >= 0);
2654 else
2655 eassert (it->current.dpvec_index < 0);
2656 #endif
2657 }
2658
2659
2660 /* Check that the window end of window W is what we expect it
2661 to be---the last row in the current matrix displaying text. */
2662
2663 static void
2664 CHECK_WINDOW_END (struct window *w)
2665 {
2666 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2667 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2668 {
2669 struct glyph_row *row;
2670 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2671 !row->enabled_p
2672 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2673 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2674 }
2675 #endif
2676 }
2677
2678 /***********************************************************************
2679 Iterator initialization
2680 ***********************************************************************/
2681
2682 /* Initialize IT for displaying current_buffer in window W, starting
2683 at character position CHARPOS. CHARPOS < 0 means that no buffer
2684 position is specified which is useful when the iterator is assigned
2685 a position later. BYTEPOS is the byte position corresponding to
2686 CHARPOS.
2687
2688 If ROW is not null, calls to produce_glyphs with IT as parameter
2689 will produce glyphs in that row.
2690
2691 BASE_FACE_ID is the id of a base face to use. It must be one of
2692 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2693 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2694 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2695
2696 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2697 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2698 will be initialized to use the corresponding mode line glyph row of
2699 the desired matrix of W. */
2700
2701 void
2702 init_iterator (struct it *it, struct window *w,
2703 ptrdiff_t charpos, ptrdiff_t bytepos,
2704 struct glyph_row *row, enum face_id base_face_id)
2705 {
2706 enum face_id remapped_base_face_id = base_face_id;
2707
2708 /* Some precondition checks. */
2709 eassert (w != NULL && it != NULL);
2710 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2711 && charpos <= ZV));
2712
2713 /* If face attributes have been changed since the last redisplay,
2714 free realized faces now because they depend on face definitions
2715 that might have changed. Don't free faces while there might be
2716 desired matrices pending which reference these faces. */
2717 if (!inhibit_free_realized_faces)
2718 {
2719 if (face_change)
2720 {
2721 face_change = false;
2722 free_all_realized_faces (Qnil);
2723 }
2724 else if (XFRAME (w->frame)->face_change)
2725 {
2726 XFRAME (w->frame)->face_change = 0;
2727 free_all_realized_faces (w->frame);
2728 }
2729 }
2730
2731 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2732 if (! NILP (Vface_remapping_alist))
2733 remapped_base_face_id
2734 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2735
2736 /* Use one of the mode line rows of W's desired matrix if
2737 appropriate. */
2738 if (row == NULL)
2739 {
2740 if (base_face_id == MODE_LINE_FACE_ID
2741 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2742 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2743 else if (base_face_id == HEADER_LINE_FACE_ID)
2744 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2745 }
2746
2747 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2748 Other parts of redisplay rely on that. */
2749 memclear (it, sizeof *it);
2750 it->current.overlay_string_index = -1;
2751 it->current.dpvec_index = -1;
2752 it->base_face_id = remapped_base_face_id;
2753 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2754 it->paragraph_embedding = L2R;
2755 it->bidi_it.w = w;
2756
2757 /* The window in which we iterate over current_buffer: */
2758 XSETWINDOW (it->window, w);
2759 it->w = w;
2760 it->f = XFRAME (w->frame);
2761
2762 it->cmp_it.id = -1;
2763
2764 /* Extra space between lines (on window systems only). */
2765 if (base_face_id == DEFAULT_FACE_ID
2766 && FRAME_WINDOW_P (it->f))
2767 {
2768 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2769 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2770 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2771 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2772 * FRAME_LINE_HEIGHT (it->f));
2773 else if (it->f->extra_line_spacing > 0)
2774 it->extra_line_spacing = it->f->extra_line_spacing;
2775 }
2776
2777 /* If realized faces have been removed, e.g. because of face
2778 attribute changes of named faces, recompute them. When running
2779 in batch mode, the face cache of the initial frame is null. If
2780 we happen to get called, make a dummy face cache. */
2781 if (FRAME_FACE_CACHE (it->f) == NULL)
2782 init_frame_faces (it->f);
2783 if (FRAME_FACE_CACHE (it->f)->used == 0)
2784 recompute_basic_faces (it->f);
2785
2786 it->override_ascent = -1;
2787
2788 /* Are control characters displayed as `^C'? */
2789 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2790
2791 /* -1 means everything between a CR and the following line end
2792 is invisible. >0 means lines indented more than this value are
2793 invisible. */
2794 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2795 ? (clip_to_bounds
2796 (-1, XINT (BVAR (current_buffer, selective_display)),
2797 PTRDIFF_MAX))
2798 : (!NILP (BVAR (current_buffer, selective_display))
2799 ? -1 : 0));
2800 it->selective_display_ellipsis_p
2801 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2802
2803 /* Display table to use. */
2804 it->dp = window_display_table (w);
2805
2806 /* Are multibyte characters enabled in current_buffer? */
2807 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2808
2809 /* Get the position at which the redisplay_end_trigger hook should
2810 be run, if it is to be run at all. */
2811 if (MARKERP (w->redisplay_end_trigger)
2812 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2813 it->redisplay_end_trigger_charpos
2814 = marker_position (w->redisplay_end_trigger);
2815 else if (INTEGERP (w->redisplay_end_trigger))
2816 it->redisplay_end_trigger_charpos
2817 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2818 PTRDIFF_MAX);
2819
2820 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2821
2822 /* Are lines in the display truncated? */
2823 if (TRUNCATE != 0)
2824 it->line_wrap = TRUNCATE;
2825 if (base_face_id == DEFAULT_FACE_ID
2826 && !it->w->hscroll
2827 && (WINDOW_FULL_WIDTH_P (it->w)
2828 || NILP (Vtruncate_partial_width_windows)
2829 || (INTEGERP (Vtruncate_partial_width_windows)
2830 /* PXW: Shall we do something about this? */
2831 && (XINT (Vtruncate_partial_width_windows)
2832 <= WINDOW_TOTAL_COLS (it->w))))
2833 && NILP (BVAR (current_buffer, truncate_lines)))
2834 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2835 ? WINDOW_WRAP : WORD_WRAP;
2836
2837 /* Get dimensions of truncation and continuation glyphs. These are
2838 displayed as fringe bitmaps under X, but we need them for such
2839 frames when the fringes are turned off. But leave the dimensions
2840 zero for tooltip frames, as these glyphs look ugly there and also
2841 sabotage calculations of tooltip dimensions in x-show-tip. */
2842 #ifdef HAVE_WINDOW_SYSTEM
2843 if (!(FRAME_WINDOW_P (it->f)
2844 && FRAMEP (tip_frame)
2845 && it->f == XFRAME (tip_frame)))
2846 #endif
2847 {
2848 if (it->line_wrap == TRUNCATE)
2849 {
2850 /* We will need the truncation glyph. */
2851 eassert (it->glyph_row == NULL);
2852 produce_special_glyphs (it, IT_TRUNCATION);
2853 it->truncation_pixel_width = it->pixel_width;
2854 }
2855 else
2856 {
2857 /* We will need the continuation glyph. */
2858 eassert (it->glyph_row == NULL);
2859 produce_special_glyphs (it, IT_CONTINUATION);
2860 it->continuation_pixel_width = it->pixel_width;
2861 }
2862 }
2863
2864 /* Reset these values to zero because the produce_special_glyphs
2865 above has changed them. */
2866 it->pixel_width = it->ascent = it->descent = 0;
2867 it->phys_ascent = it->phys_descent = 0;
2868
2869 /* Set this after getting the dimensions of truncation and
2870 continuation glyphs, so that we don't produce glyphs when calling
2871 produce_special_glyphs, above. */
2872 it->glyph_row = row;
2873 it->area = TEXT_AREA;
2874
2875 /* Get the dimensions of the display area. The display area
2876 consists of the visible window area plus a horizontally scrolled
2877 part to the left of the window. All x-values are relative to the
2878 start of this total display area. */
2879 if (base_face_id != DEFAULT_FACE_ID)
2880 {
2881 /* Mode lines, menu bar in terminal frames. */
2882 it->first_visible_x = 0;
2883 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2884 }
2885 else
2886 {
2887 it->first_visible_x
2888 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2889 it->last_visible_x = (it->first_visible_x
2890 + window_box_width (w, TEXT_AREA));
2891
2892 /* If we truncate lines, leave room for the truncation glyph(s) at
2893 the right margin. Otherwise, leave room for the continuation
2894 glyph(s). Done only if the window has no right fringe. */
2895 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2896 {
2897 if (it->line_wrap == TRUNCATE)
2898 it->last_visible_x -= it->truncation_pixel_width;
2899 else
2900 it->last_visible_x -= it->continuation_pixel_width;
2901 }
2902
2903 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2904 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2905 }
2906
2907 /* Leave room for a border glyph. */
2908 if (!FRAME_WINDOW_P (it->f)
2909 && !WINDOW_RIGHTMOST_P (it->w))
2910 it->last_visible_x -= 1;
2911
2912 it->last_visible_y = window_text_bottom_y (w);
2913
2914 /* For mode lines and alike, arrange for the first glyph having a
2915 left box line if the face specifies a box. */
2916 if (base_face_id != DEFAULT_FACE_ID)
2917 {
2918 struct face *face;
2919
2920 it->face_id = remapped_base_face_id;
2921
2922 /* If we have a boxed mode line, make the first character appear
2923 with a left box line. */
2924 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2925 if (face && face->box != FACE_NO_BOX)
2926 it->start_of_box_run_p = true;
2927 }
2928
2929 /* If a buffer position was specified, set the iterator there,
2930 getting overlays and face properties from that position. */
2931 if (charpos >= BUF_BEG (current_buffer))
2932 {
2933 it->stop_charpos = charpos;
2934 it->end_charpos = ZV;
2935 eassert (charpos == BYTE_TO_CHAR (bytepos));
2936 IT_CHARPOS (*it) = charpos;
2937 IT_BYTEPOS (*it) = bytepos;
2938
2939 /* We will rely on `reseat' to set this up properly, via
2940 handle_face_prop. */
2941 it->face_id = it->base_face_id;
2942
2943 it->start = it->current;
2944 /* Do we need to reorder bidirectional text? Not if this is a
2945 unibyte buffer: by definition, none of the single-byte
2946 characters are strong R2L, so no reordering is needed. And
2947 bidi.c doesn't support unibyte buffers anyway. Also, don't
2948 reorder while we are loading loadup.el, since the tables of
2949 character properties needed for reordering are not yet
2950 available. */
2951 it->bidi_p =
2952 NILP (Vpurify_flag)
2953 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2954 && it->multibyte_p;
2955
2956 /* If we are to reorder bidirectional text, init the bidi
2957 iterator. */
2958 if (it->bidi_p)
2959 {
2960 /* Since we don't know at this point whether there will be
2961 any R2L lines in the window, we reserve space for
2962 truncation/continuation glyphs even if only the left
2963 fringe is absent. */
2964 if (base_face_id == DEFAULT_FACE_ID
2965 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2966 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2967 {
2968 if (it->line_wrap == TRUNCATE)
2969 it->last_visible_x -= it->truncation_pixel_width;
2970 else
2971 it->last_visible_x -= it->continuation_pixel_width;
2972 }
2973 /* Note the paragraph direction that this buffer wants to
2974 use. */
2975 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2976 Qleft_to_right))
2977 it->paragraph_embedding = L2R;
2978 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2979 Qright_to_left))
2980 it->paragraph_embedding = R2L;
2981 else
2982 it->paragraph_embedding = NEUTRAL_DIR;
2983 bidi_unshelve_cache (NULL, false);
2984 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2985 &it->bidi_it);
2986 }
2987
2988 /* Compute faces etc. */
2989 reseat (it, it->current.pos, true);
2990 }
2991
2992 CHECK_IT (it);
2993 }
2994
2995
2996 /* Initialize IT for the display of window W with window start POS. */
2997
2998 void
2999 start_display (struct it *it, struct window *w, struct text_pos pos)
3000 {
3001 struct glyph_row *row;
3002 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
3003
3004 row = w->desired_matrix->rows + first_vpos;
3005 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3006 it->first_vpos = first_vpos;
3007
3008 /* Don't reseat to previous visible line start if current start
3009 position is in a string or image. */
3010 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3011 {
3012 int first_y = it->current_y;
3013
3014 /* If window start is not at a line start, skip forward to POS to
3015 get the correct continuation lines width. */
3016 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3017 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3018 if (!start_at_line_beg_p)
3019 {
3020 int new_x;
3021
3022 reseat_at_previous_visible_line_start (it);
3023 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3024
3025 new_x = it->current_x + it->pixel_width;
3026
3027 /* If lines are continued, this line may end in the middle
3028 of a multi-glyph character (e.g. a control character
3029 displayed as \003, or in the middle of an overlay
3030 string). In this case move_it_to above will not have
3031 taken us to the start of the continuation line but to the
3032 end of the continued line. */
3033 if (it->current_x > 0
3034 && it->line_wrap != TRUNCATE /* Lines are continued. */
3035 && (/* And glyph doesn't fit on the line. */
3036 new_x > it->last_visible_x
3037 /* Or it fits exactly and we're on a window
3038 system frame. */
3039 || (new_x == it->last_visible_x
3040 && FRAME_WINDOW_P (it->f)
3041 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3042 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3043 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3044 {
3045 if ((it->current.dpvec_index >= 0
3046 || it->current.overlay_string_index >= 0)
3047 /* If we are on a newline from a display vector or
3048 overlay string, then we are already at the end of
3049 a screen line; no need to go to the next line in
3050 that case, as this line is not really continued.
3051 (If we do go to the next line, C-e will not DTRT.) */
3052 && it->c != '\n')
3053 {
3054 set_iterator_to_next (it, true);
3055 move_it_in_display_line_to (it, -1, -1, 0);
3056 }
3057
3058 it->continuation_lines_width += it->current_x;
3059 }
3060 /* If the character at POS is displayed via a display
3061 vector, move_it_to above stops at the final glyph of
3062 IT->dpvec. To make the caller redisplay that character
3063 again (a.k.a. start at POS), we need to reset the
3064 dpvec_index to the beginning of IT->dpvec. */
3065 else if (it->current.dpvec_index >= 0)
3066 it->current.dpvec_index = 0;
3067
3068 /* We're starting a new display line, not affected by the
3069 height of the continued line, so clear the appropriate
3070 fields in the iterator structure. */
3071 it->max_ascent = it->max_descent = 0;
3072 it->max_phys_ascent = it->max_phys_descent = 0;
3073
3074 it->current_y = first_y;
3075 it->vpos = 0;
3076 it->current_x = it->hpos = 0;
3077 }
3078 }
3079 }
3080
3081
3082 /* Return true if POS is a position in ellipses displayed for invisible
3083 text. W is the window we display, for text property lookup. */
3084
3085 static bool
3086 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3087 {
3088 Lisp_Object prop, window;
3089 bool ellipses_p = false;
3090 ptrdiff_t charpos = CHARPOS (pos->pos);
3091
3092 /* If POS specifies a position in a display vector, this might
3093 be for an ellipsis displayed for invisible text. We won't
3094 get the iterator set up for delivering that ellipsis unless
3095 we make sure that it gets aware of the invisible text. */
3096 if (pos->dpvec_index >= 0
3097 && pos->overlay_string_index < 0
3098 && CHARPOS (pos->string_pos) < 0
3099 && charpos > BEGV
3100 && (XSETWINDOW (window, w),
3101 prop = Fget_char_property (make_number (charpos),
3102 Qinvisible, window),
3103 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3104 {
3105 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3106 window);
3107 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3108 }
3109
3110 return ellipses_p;
3111 }
3112
3113
3114 /* Initialize IT for stepping through current_buffer in window W,
3115 starting at position POS that includes overlay string and display
3116 vector/ control character translation position information. Value
3117 is false if there are overlay strings with newlines at POS. */
3118
3119 static bool
3120 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3121 {
3122 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3123 int i;
3124 bool overlay_strings_with_newlines = false;
3125
3126 /* If POS specifies a position in a display vector, this might
3127 be for an ellipsis displayed for invisible text. We won't
3128 get the iterator set up for delivering that ellipsis unless
3129 we make sure that it gets aware of the invisible text. */
3130 if (in_ellipses_for_invisible_text_p (pos, w))
3131 {
3132 --charpos;
3133 bytepos = 0;
3134 }
3135
3136 /* Keep in mind: the call to reseat in init_iterator skips invisible
3137 text, so we might end up at a position different from POS. This
3138 is only a problem when POS is a row start after a newline and an
3139 overlay starts there with an after-string, and the overlay has an
3140 invisible property. Since we don't skip invisible text in
3141 display_line and elsewhere immediately after consuming the
3142 newline before the row start, such a POS will not be in a string,
3143 but the call to init_iterator below will move us to the
3144 after-string. */
3145 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3146
3147 /* This only scans the current chunk -- it should scan all chunks.
3148 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3149 to 16 in 22.1 to make this a lesser problem. */
3150 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3151 {
3152 const char *s = SSDATA (it->overlay_strings[i]);
3153 const char *e = s + SBYTES (it->overlay_strings[i]);
3154
3155 while (s < e && *s != '\n')
3156 ++s;
3157
3158 if (s < e)
3159 {
3160 overlay_strings_with_newlines = true;
3161 break;
3162 }
3163 }
3164
3165 /* If position is within an overlay string, set up IT to the right
3166 overlay string. */
3167 if (pos->overlay_string_index >= 0)
3168 {
3169 int relative_index;
3170
3171 /* If the first overlay string happens to have a `display'
3172 property for an image, the iterator will be set up for that
3173 image, and we have to undo that setup first before we can
3174 correct the overlay string index. */
3175 if (it->method == GET_FROM_IMAGE)
3176 pop_it (it);
3177
3178 /* We already have the first chunk of overlay strings in
3179 IT->overlay_strings. Load more until the one for
3180 pos->overlay_string_index is in IT->overlay_strings. */
3181 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3182 {
3183 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3184 it->current.overlay_string_index = 0;
3185 while (n--)
3186 {
3187 load_overlay_strings (it, 0);
3188 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3189 }
3190 }
3191
3192 it->current.overlay_string_index = pos->overlay_string_index;
3193 relative_index = (it->current.overlay_string_index
3194 % OVERLAY_STRING_CHUNK_SIZE);
3195 it->string = it->overlay_strings[relative_index];
3196 eassert (STRINGP (it->string));
3197 it->current.string_pos = pos->string_pos;
3198 it->method = GET_FROM_STRING;
3199 it->end_charpos = SCHARS (it->string);
3200 /* Set up the bidi iterator for this overlay string. */
3201 if (it->bidi_p)
3202 {
3203 it->bidi_it.string.lstring = it->string;
3204 it->bidi_it.string.s = NULL;
3205 it->bidi_it.string.schars = SCHARS (it->string);
3206 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3207 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3208 it->bidi_it.string.unibyte = !it->multibyte_p;
3209 it->bidi_it.w = it->w;
3210 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3211 FRAME_WINDOW_P (it->f), &it->bidi_it);
3212
3213 /* Synchronize the state of the bidi iterator with
3214 pos->string_pos. For any string position other than
3215 zero, this will be done automagically when we resume
3216 iteration over the string and get_visually_first_element
3217 is called. But if string_pos is zero, and the string is
3218 to be reordered for display, we need to resync manually,
3219 since it could be that the iteration state recorded in
3220 pos ended at string_pos of 0 moving backwards in string. */
3221 if (CHARPOS (pos->string_pos) == 0)
3222 {
3223 get_visually_first_element (it);
3224 if (IT_STRING_CHARPOS (*it) != 0)
3225 do {
3226 /* Paranoia. */
3227 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3228 bidi_move_to_visually_next (&it->bidi_it);
3229 } while (it->bidi_it.charpos != 0);
3230 }
3231 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3232 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3233 }
3234 }
3235
3236 if (CHARPOS (pos->string_pos) >= 0)
3237 {
3238 /* Recorded position is not in an overlay string, but in another
3239 string. This can only be a string from a `display' property.
3240 IT should already be filled with that string. */
3241 it->current.string_pos = pos->string_pos;
3242 eassert (STRINGP (it->string));
3243 if (it->bidi_p)
3244 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3245 FRAME_WINDOW_P (it->f), &it->bidi_it);
3246 }
3247
3248 /* Restore position in display vector translations, control
3249 character translations or ellipses. */
3250 if (pos->dpvec_index >= 0)
3251 {
3252 if (it->dpvec == NULL)
3253 get_next_display_element (it);
3254 eassert (it->dpvec && it->current.dpvec_index == 0);
3255 it->current.dpvec_index = pos->dpvec_index;
3256 }
3257
3258 CHECK_IT (it);
3259 return !overlay_strings_with_newlines;
3260 }
3261
3262
3263 /* Initialize IT for stepping through current_buffer in window W
3264 starting at ROW->start. */
3265
3266 static void
3267 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3268 {
3269 init_from_display_pos (it, w, &row->start);
3270 it->start = row->start;
3271 it->continuation_lines_width = row->continuation_lines_width;
3272 CHECK_IT (it);
3273 }
3274
3275
3276 /* Initialize IT for stepping through current_buffer in window W
3277 starting in the line following ROW, i.e. starting at ROW->end.
3278 Value is false if there are overlay strings with newlines at ROW's
3279 end position. */
3280
3281 static bool
3282 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3283 {
3284 bool success = false;
3285
3286 if (init_from_display_pos (it, w, &row->end))
3287 {
3288 if (row->continued_p)
3289 it->continuation_lines_width
3290 = row->continuation_lines_width + row->pixel_width;
3291 CHECK_IT (it);
3292 success = true;
3293 }
3294
3295 return success;
3296 }
3297
3298
3299
3300 \f
3301 /***********************************************************************
3302 Text properties
3303 ***********************************************************************/
3304
3305 /* Called when IT reaches IT->stop_charpos. Handle text property and
3306 overlay changes. Set IT->stop_charpos to the next position where
3307 to stop. */
3308
3309 static void
3310 handle_stop (struct it *it)
3311 {
3312 enum prop_handled handled;
3313 bool handle_overlay_change_p;
3314 struct props *p;
3315
3316 it->dpvec = NULL;
3317 it->current.dpvec_index = -1;
3318 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3319 it->ellipsis_p = false;
3320
3321 /* Use face of preceding text for ellipsis (if invisible) */
3322 if (it->selective_display_ellipsis_p)
3323 it->saved_face_id = it->face_id;
3324
3325 /* Here's the description of the semantics of, and the logic behind,
3326 the various HANDLED_* statuses:
3327
3328 HANDLED_NORMALLY means the handler did its job, and the loop
3329 should proceed to calling the next handler in order.
3330
3331 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3332 change in the properties and overlays at current position, so the
3333 loop should be restarted, to re-invoke the handlers that were
3334 already called. This happens when fontification-functions were
3335 called by handle_fontified_prop, and actually fontified
3336 something. Another case where HANDLED_RECOMPUTE_PROPS is
3337 returned is when we discover overlay strings that need to be
3338 displayed right away. The loop below will continue for as long
3339 as the status is HANDLED_RECOMPUTE_PROPS.
3340
3341 HANDLED_RETURN means return immediately to the caller, to
3342 continue iteration without calling any further handlers. This is
3343 used when we need to act on some property right away, for example
3344 when we need to display the ellipsis or a replacing display
3345 property, such as display string or image.
3346
3347 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3348 consumed, and the handler switched to the next overlay string.
3349 This signals the loop below to refrain from looking for more
3350 overlays before all the overlay strings of the current overlay
3351 are processed.
3352
3353 Some of the handlers called by the loop push the iterator state
3354 onto the stack (see 'push_it'), and arrange for the iteration to
3355 continue with another object, such as an image, a display string,
3356 or an overlay string. In most such cases, it->stop_charpos is
3357 set to the first character of the string, so that when the
3358 iteration resumes, this function will immediately be called
3359 again, to examine the properties at the beginning of the string.
3360
3361 When a display or overlay string is exhausted, the iterator state
3362 is popped (see 'pop_it'), and iteration continues with the
3363 previous object. Again, in many such cases this function is
3364 called again to find the next position where properties might
3365 change. */
3366
3367 do
3368 {
3369 handled = HANDLED_NORMALLY;
3370
3371 /* Call text property handlers. */
3372 for (p = it_props; p->handler; ++p)
3373 {
3374 handled = p->handler (it);
3375
3376 if (handled == HANDLED_RECOMPUTE_PROPS)
3377 break;
3378 else if (handled == HANDLED_RETURN)
3379 {
3380 /* We still want to show before and after strings from
3381 overlays even if the actual buffer text is replaced. */
3382 if (!handle_overlay_change_p
3383 || it->sp > 1
3384 /* Don't call get_overlay_strings_1 if we already
3385 have overlay strings loaded, because doing so
3386 will load them again and push the iterator state
3387 onto the stack one more time, which is not
3388 expected by the rest of the code that processes
3389 overlay strings. */
3390 || (it->current.overlay_string_index < 0
3391 && !get_overlay_strings_1 (it, 0, false)))
3392 {
3393 if (it->ellipsis_p)
3394 setup_for_ellipsis (it, 0);
3395 /* When handling a display spec, we might load an
3396 empty string. In that case, discard it here. We
3397 used to discard it in handle_single_display_spec,
3398 but that causes get_overlay_strings_1, above, to
3399 ignore overlay strings that we must check. */
3400 if (STRINGP (it->string) && !SCHARS (it->string))
3401 pop_it (it);
3402 return;
3403 }
3404 else if (STRINGP (it->string) && !SCHARS (it->string))
3405 pop_it (it);
3406 else
3407 {
3408 it->string_from_display_prop_p = false;
3409 it->from_disp_prop_p = false;
3410 handle_overlay_change_p = false;
3411 }
3412 handled = HANDLED_RECOMPUTE_PROPS;
3413 break;
3414 }
3415 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3416 handle_overlay_change_p = false;
3417 }
3418
3419 if (handled != HANDLED_RECOMPUTE_PROPS)
3420 {
3421 /* Don't check for overlay strings below when set to deliver
3422 characters from a display vector. */
3423 if (it->method == GET_FROM_DISPLAY_VECTOR)
3424 handle_overlay_change_p = false;
3425
3426 /* Handle overlay changes.
3427 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3428 if it finds overlays. */
3429 if (handle_overlay_change_p)
3430 handled = handle_overlay_change (it);
3431 }
3432
3433 if (it->ellipsis_p)
3434 {
3435 setup_for_ellipsis (it, 0);
3436 break;
3437 }
3438 }
3439 while (handled == HANDLED_RECOMPUTE_PROPS);
3440
3441 /* Determine where to stop next. */
3442 if (handled == HANDLED_NORMALLY)
3443 compute_stop_pos (it);
3444 }
3445
3446
3447 /* Compute IT->stop_charpos from text property and overlay change
3448 information for IT's current position. */
3449
3450 static void
3451 compute_stop_pos (struct it *it)
3452 {
3453 register INTERVAL iv, next_iv;
3454 Lisp_Object object, limit, position;
3455 ptrdiff_t charpos, bytepos;
3456
3457 if (STRINGP (it->string))
3458 {
3459 /* Strings are usually short, so don't limit the search for
3460 properties. */
3461 it->stop_charpos = it->end_charpos;
3462 object = it->string;
3463 limit = Qnil;
3464 charpos = IT_STRING_CHARPOS (*it);
3465 bytepos = IT_STRING_BYTEPOS (*it);
3466 }
3467 else
3468 {
3469 ptrdiff_t pos;
3470
3471 /* If end_charpos is out of range for some reason, such as a
3472 misbehaving display function, rationalize it (Bug#5984). */
3473 if (it->end_charpos > ZV)
3474 it->end_charpos = ZV;
3475 it->stop_charpos = it->end_charpos;
3476
3477 /* If next overlay change is in front of the current stop pos
3478 (which is IT->end_charpos), stop there. Note: value of
3479 next_overlay_change is point-max if no overlay change
3480 follows. */
3481 charpos = IT_CHARPOS (*it);
3482 bytepos = IT_BYTEPOS (*it);
3483 pos = next_overlay_change (charpos);
3484 if (pos < it->stop_charpos)
3485 it->stop_charpos = pos;
3486
3487 /* Set up variables for computing the stop position from text
3488 property changes. */
3489 XSETBUFFER (object, current_buffer);
3490 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3491 }
3492
3493 /* Get the interval containing IT's position. Value is a null
3494 interval if there isn't such an interval. */
3495 position = make_number (charpos);
3496 iv = validate_interval_range (object, &position, &position, false);
3497 if (iv)
3498 {
3499 Lisp_Object values_here[LAST_PROP_IDX];
3500 struct props *p;
3501
3502 /* Get properties here. */
3503 for (p = it_props; p->handler; ++p)
3504 values_here[p->idx] = textget (iv->plist,
3505 builtin_lisp_symbol (p->name));
3506
3507 /* Look for an interval following iv that has different
3508 properties. */
3509 for (next_iv = next_interval (iv);
3510 (next_iv
3511 && (NILP (limit)
3512 || XFASTINT (limit) > next_iv->position));
3513 next_iv = next_interval (next_iv))
3514 {
3515 for (p = it_props; p->handler; ++p)
3516 {
3517 Lisp_Object new_value = textget (next_iv->plist,
3518 builtin_lisp_symbol (p->name));
3519 if (!EQ (values_here[p->idx], new_value))
3520 break;
3521 }
3522
3523 if (p->handler)
3524 break;
3525 }
3526
3527 if (next_iv)
3528 {
3529 if (INTEGERP (limit)
3530 && next_iv->position >= XFASTINT (limit))
3531 /* No text property change up to limit. */
3532 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3533 else
3534 /* Text properties change in next_iv. */
3535 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3536 }
3537 }
3538
3539 if (it->cmp_it.id < 0)
3540 {
3541 ptrdiff_t stoppos = it->end_charpos;
3542
3543 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3544 stoppos = -1;
3545 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3546 stoppos, it->string);
3547 }
3548
3549 eassert (STRINGP (it->string)
3550 || (it->stop_charpos >= BEGV
3551 && it->stop_charpos >= IT_CHARPOS (*it)));
3552 }
3553
3554
3555 /* Return the position of the next overlay change after POS in
3556 current_buffer. Value is point-max if no overlay change
3557 follows. This is like `next-overlay-change' but doesn't use
3558 xmalloc. */
3559
3560 static ptrdiff_t
3561 next_overlay_change (ptrdiff_t pos)
3562 {
3563 ptrdiff_t i, noverlays;
3564 ptrdiff_t endpos;
3565 Lisp_Object *overlays;
3566 USE_SAFE_ALLOCA;
3567
3568 /* Get all overlays at the given position. */
3569 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3570
3571 /* If any of these overlays ends before endpos,
3572 use its ending point instead. */
3573 for (i = 0; i < noverlays; ++i)
3574 {
3575 Lisp_Object oend;
3576 ptrdiff_t oendpos;
3577
3578 oend = OVERLAY_END (overlays[i]);
3579 oendpos = OVERLAY_POSITION (oend);
3580 endpos = min (endpos, oendpos);
3581 }
3582
3583 SAFE_FREE ();
3584 return endpos;
3585 }
3586
3587 /* How many characters forward to search for a display property or
3588 display string. Searching too far forward makes the bidi display
3589 sluggish, especially in small windows. */
3590 #define MAX_DISP_SCAN 250
3591
3592 /* Return the character position of a display string at or after
3593 position specified by POSITION. If no display string exists at or
3594 after POSITION, return ZV. A display string is either an overlay
3595 with `display' property whose value is a string, or a `display'
3596 text property whose value is a string. STRING is data about the
3597 string to iterate; if STRING->lstring is nil, we are iterating a
3598 buffer. FRAME_WINDOW_P is true when we are displaying a window
3599 on a GUI frame. DISP_PROP is set to zero if we searched
3600 MAX_DISP_SCAN characters forward without finding any display
3601 strings, non-zero otherwise. It is set to 2 if the display string
3602 uses any kind of `(space ...)' spec that will produce a stretch of
3603 white space in the text area. */
3604 ptrdiff_t
3605 compute_display_string_pos (struct text_pos *position,
3606 struct bidi_string_data *string,
3607 struct window *w,
3608 bool frame_window_p, int *disp_prop)
3609 {
3610 /* OBJECT = nil means current buffer. */
3611 Lisp_Object object, object1;
3612 Lisp_Object pos, spec, limpos;
3613 bool string_p = string && (STRINGP (string->lstring) || string->s);
3614 ptrdiff_t eob = string_p ? string->schars : ZV;
3615 ptrdiff_t begb = string_p ? 0 : BEGV;
3616 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3617 ptrdiff_t lim =
3618 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3619 struct text_pos tpos;
3620 int rv = 0;
3621
3622 if (string && STRINGP (string->lstring))
3623 object1 = object = string->lstring;
3624 else if (w && !string_p)
3625 {
3626 XSETWINDOW (object, w);
3627 object1 = Qnil;
3628 }
3629 else
3630 object1 = object = Qnil;
3631
3632 *disp_prop = 1;
3633
3634 if (charpos >= eob
3635 /* We don't support display properties whose values are strings
3636 that have display string properties. */
3637 || string->from_disp_str
3638 /* C strings cannot have display properties. */
3639 || (string->s && !STRINGP (object)))
3640 {
3641 *disp_prop = 0;
3642 return eob;
3643 }
3644
3645 /* If the character at CHARPOS is where the display string begins,
3646 return CHARPOS. */
3647 pos = make_number (charpos);
3648 if (STRINGP (object))
3649 bufpos = string->bufpos;
3650 else
3651 bufpos = charpos;
3652 tpos = *position;
3653 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3654 && (charpos <= begb
3655 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3656 object),
3657 spec))
3658 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3659 frame_window_p)))
3660 {
3661 if (rv == 2)
3662 *disp_prop = 2;
3663 return charpos;
3664 }
3665
3666 /* Look forward for the first character with a `display' property
3667 that will replace the underlying text when displayed. */
3668 limpos = make_number (lim);
3669 do {
3670 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3671 CHARPOS (tpos) = XFASTINT (pos);
3672 if (CHARPOS (tpos) >= lim)
3673 {
3674 *disp_prop = 0;
3675 break;
3676 }
3677 if (STRINGP (object))
3678 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3679 else
3680 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3681 spec = Fget_char_property (pos, Qdisplay, object);
3682 if (!STRINGP (object))
3683 bufpos = CHARPOS (tpos);
3684 } while (NILP (spec)
3685 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3686 bufpos, frame_window_p)));
3687 if (rv == 2)
3688 *disp_prop = 2;
3689
3690 return CHARPOS (tpos);
3691 }
3692
3693 /* Return the character position of the end of the display string that
3694 started at CHARPOS. If there's no display string at CHARPOS,
3695 return -1. A display string is either an overlay with `display'
3696 property whose value is a string or a `display' text property whose
3697 value is a string. */
3698 ptrdiff_t
3699 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3700 {
3701 /* OBJECT = nil means current buffer. */
3702 Lisp_Object object =
3703 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3704 Lisp_Object pos = make_number (charpos);
3705 ptrdiff_t eob =
3706 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3707
3708 if (charpos >= eob || (string->s && !STRINGP (object)))
3709 return eob;
3710
3711 /* It could happen that the display property or overlay was removed
3712 since we found it in compute_display_string_pos above. One way
3713 this can happen is if JIT font-lock was called (through
3714 handle_fontified_prop), and jit-lock-functions remove text
3715 properties or overlays from the portion of buffer that includes
3716 CHARPOS. Muse mode is known to do that, for example. In this
3717 case, we return -1 to the caller, to signal that no display
3718 string is actually present at CHARPOS. See bidi_fetch_char for
3719 how this is handled.
3720
3721 An alternative would be to never look for display properties past
3722 it->stop_charpos. But neither compute_display_string_pos nor
3723 bidi_fetch_char that calls it know or care where the next
3724 stop_charpos is. */
3725 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3726 return -1;
3727
3728 /* Look forward for the first character where the `display' property
3729 changes. */
3730 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3731
3732 return XFASTINT (pos);
3733 }
3734
3735
3736 \f
3737 /***********************************************************************
3738 Fontification
3739 ***********************************************************************/
3740
3741 /* Handle changes in the `fontified' property of the current buffer by
3742 calling hook functions from Qfontification_functions to fontify
3743 regions of text. */
3744
3745 static enum prop_handled
3746 handle_fontified_prop (struct it *it)
3747 {
3748 Lisp_Object prop, pos;
3749 enum prop_handled handled = HANDLED_NORMALLY;
3750
3751 if (!NILP (Vmemory_full))
3752 return handled;
3753
3754 /* Get the value of the `fontified' property at IT's current buffer
3755 position. (The `fontified' property doesn't have a special
3756 meaning in strings.) If the value is nil, call functions from
3757 Qfontification_functions. */
3758 if (!STRINGP (it->string)
3759 && it->s == NULL
3760 && !NILP (Vfontification_functions)
3761 && !NILP (Vrun_hooks)
3762 && (pos = make_number (IT_CHARPOS (*it)),
3763 prop = Fget_char_property (pos, Qfontified, Qnil),
3764 /* Ignore the special cased nil value always present at EOB since
3765 no amount of fontifying will be able to change it. */
3766 NILP (prop) && IT_CHARPOS (*it) < Z))
3767 {
3768 ptrdiff_t count = SPECPDL_INDEX ();
3769 Lisp_Object val;
3770 struct buffer *obuf = current_buffer;
3771 ptrdiff_t begv = BEGV, zv = ZV;
3772 bool old_clip_changed = current_buffer->clip_changed;
3773
3774 val = Vfontification_functions;
3775 specbind (Qfontification_functions, Qnil);
3776
3777 eassert (it->end_charpos == ZV);
3778
3779 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3780 safe_call1 (val, pos);
3781 else
3782 {
3783 Lisp_Object fns, fn;
3784
3785 fns = Qnil;
3786
3787 for (; CONSP (val); val = XCDR (val))
3788 {
3789 fn = XCAR (val);
3790
3791 if (EQ (fn, Qt))
3792 {
3793 /* A value of t indicates this hook has a local
3794 binding; it means to run the global binding too.
3795 In a global value, t should not occur. If it
3796 does, we must ignore it to avoid an endless
3797 loop. */
3798 for (fns = Fdefault_value (Qfontification_functions);
3799 CONSP (fns);
3800 fns = XCDR (fns))
3801 {
3802 fn = XCAR (fns);
3803 if (!EQ (fn, Qt))
3804 safe_call1 (fn, pos);
3805 }
3806 }
3807 else
3808 safe_call1 (fn, pos);
3809 }
3810 }
3811
3812 unbind_to (count, Qnil);
3813
3814 /* Fontification functions routinely call `save-restriction'.
3815 Normally, this tags clip_changed, which can confuse redisplay
3816 (see discussion in Bug#6671). Since we don't perform any
3817 special handling of fontification changes in the case where
3818 `save-restriction' isn't called, there's no point doing so in
3819 this case either. So, if the buffer's restrictions are
3820 actually left unchanged, reset clip_changed. */
3821 if (obuf == current_buffer)
3822 {
3823 if (begv == BEGV && zv == ZV)
3824 current_buffer->clip_changed = old_clip_changed;
3825 }
3826 /* There isn't much we can reasonably do to protect against
3827 misbehaving fontification, but here's a fig leaf. */
3828 else if (BUFFER_LIVE_P (obuf))
3829 set_buffer_internal_1 (obuf);
3830
3831 /* The fontification code may have added/removed text.
3832 It could do even a lot worse, but let's at least protect against
3833 the most obvious case where only the text past `pos' gets changed',
3834 as is/was done in grep.el where some escapes sequences are turned
3835 into face properties (bug#7876). */
3836 it->end_charpos = ZV;
3837
3838 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3839 something. This avoids an endless loop if they failed to
3840 fontify the text for which reason ever. */
3841 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3842 handled = HANDLED_RECOMPUTE_PROPS;
3843 }
3844
3845 return handled;
3846 }
3847
3848
3849 \f
3850 /***********************************************************************
3851 Faces
3852 ***********************************************************************/
3853
3854 /* Set up iterator IT from face properties at its current position.
3855 Called from handle_stop. */
3856
3857 static enum prop_handled
3858 handle_face_prop (struct it *it)
3859 {
3860 int new_face_id;
3861 ptrdiff_t next_stop;
3862
3863 if (!STRINGP (it->string))
3864 {
3865 new_face_id
3866 = face_at_buffer_position (it->w,
3867 IT_CHARPOS (*it),
3868 &next_stop,
3869 (IT_CHARPOS (*it)
3870 + TEXT_PROP_DISTANCE_LIMIT),
3871 false, it->base_face_id);
3872
3873 /* Is this a start of a run of characters with box face?
3874 Caveat: this can be called for a freshly initialized
3875 iterator; face_id is -1 in this case. We know that the new
3876 face will not change until limit, i.e. if the new face has a
3877 box, all characters up to limit will have one. But, as
3878 usual, we don't know whether limit is really the end. */
3879 if (new_face_id != it->face_id)
3880 {
3881 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3882 /* If it->face_id is -1, old_face below will be NULL, see
3883 the definition of FACE_FROM_ID. This will happen if this
3884 is the initial call that gets the face. */
3885 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3886
3887 /* If the value of face_id of the iterator is -1, we have to
3888 look in front of IT's position and see whether there is a
3889 face there that's different from new_face_id. */
3890 if (!old_face && IT_CHARPOS (*it) > BEG)
3891 {
3892 int prev_face_id = face_before_it_pos (it);
3893
3894 old_face = FACE_FROM_ID (it->f, prev_face_id);
3895 }
3896
3897 /* If the new face has a box, but the old face does not,
3898 this is the start of a run of characters with box face,
3899 i.e. this character has a shadow on the left side. */
3900 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3901 && (old_face == NULL || !old_face->box));
3902 it->face_box_p = new_face->box != FACE_NO_BOX;
3903 }
3904 }
3905 else
3906 {
3907 int base_face_id;
3908 ptrdiff_t bufpos;
3909 int i;
3910 Lisp_Object from_overlay
3911 = (it->current.overlay_string_index >= 0
3912 ? it->string_overlays[it->current.overlay_string_index
3913 % OVERLAY_STRING_CHUNK_SIZE]
3914 : Qnil);
3915
3916 /* See if we got to this string directly or indirectly from
3917 an overlay property. That includes the before-string or
3918 after-string of an overlay, strings in display properties
3919 provided by an overlay, their text properties, etc.
3920
3921 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3922 if (! NILP (from_overlay))
3923 for (i = it->sp - 1; i >= 0; i--)
3924 {
3925 if (it->stack[i].current.overlay_string_index >= 0)
3926 from_overlay
3927 = it->string_overlays[it->stack[i].current.overlay_string_index
3928 % OVERLAY_STRING_CHUNK_SIZE];
3929 else if (! NILP (it->stack[i].from_overlay))
3930 from_overlay = it->stack[i].from_overlay;
3931
3932 if (!NILP (from_overlay))
3933 break;
3934 }
3935
3936 if (! NILP (from_overlay))
3937 {
3938 bufpos = IT_CHARPOS (*it);
3939 /* For a string from an overlay, the base face depends
3940 only on text properties and ignores overlays. */
3941 base_face_id
3942 = face_for_overlay_string (it->w,
3943 IT_CHARPOS (*it),
3944 &next_stop,
3945 (IT_CHARPOS (*it)
3946 + TEXT_PROP_DISTANCE_LIMIT),
3947 false,
3948 from_overlay);
3949 }
3950 else
3951 {
3952 bufpos = 0;
3953
3954 /* For strings from a `display' property, use the face at
3955 IT's current buffer position as the base face to merge
3956 with, so that overlay strings appear in the same face as
3957 surrounding text, unless they specify their own faces.
3958 For strings from wrap-prefix and line-prefix properties,
3959 use the default face, possibly remapped via
3960 Vface_remapping_alist. */
3961 /* Note that the fact that we use the face at _buffer_
3962 position means that a 'display' property on an overlay
3963 string will not inherit the face of that overlay string,
3964 but will instead revert to the face of buffer text
3965 covered by the overlay. This is visible, e.g., when the
3966 overlay specifies a box face, but neither the buffer nor
3967 the display string do. This sounds like a design bug,
3968 but Emacs always did that since v21.1, so changing that
3969 might be a big deal. */
3970 base_face_id = it->string_from_prefix_prop_p
3971 ? (!NILP (Vface_remapping_alist)
3972 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3973 : DEFAULT_FACE_ID)
3974 : underlying_face_id (it);
3975 }
3976
3977 new_face_id = face_at_string_position (it->w,
3978 it->string,
3979 IT_STRING_CHARPOS (*it),
3980 bufpos,
3981 &next_stop,
3982 base_face_id, false);
3983
3984 /* Is this a start of a run of characters with box? Caveat:
3985 this can be called for a freshly allocated iterator; face_id
3986 is -1 is this case. We know that the new face will not
3987 change until the next check pos, i.e. if the new face has a
3988 box, all characters up to that position will have a
3989 box. But, as usual, we don't know whether that position
3990 is really the end. */
3991 if (new_face_id != it->face_id)
3992 {
3993 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3994 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3995
3996 /* If new face has a box but old face hasn't, this is the
3997 start of a run of characters with box, i.e. it has a
3998 shadow on the left side. */
3999 it->start_of_box_run_p
4000 = new_face->box && (old_face == NULL || !old_face->box);
4001 it->face_box_p = new_face->box != FACE_NO_BOX;
4002 }
4003 }
4004
4005 it->face_id = new_face_id;
4006 return HANDLED_NORMALLY;
4007 }
4008
4009
4010 /* Return the ID of the face ``underlying'' IT's current position,
4011 which is in a string. If the iterator is associated with a
4012 buffer, return the face at IT's current buffer position.
4013 Otherwise, use the iterator's base_face_id. */
4014
4015 static int
4016 underlying_face_id (struct it *it)
4017 {
4018 int face_id = it->base_face_id, i;
4019
4020 eassert (STRINGP (it->string));
4021
4022 for (i = it->sp - 1; i >= 0; --i)
4023 if (NILP (it->stack[i].string))
4024 face_id = it->stack[i].face_id;
4025
4026 return face_id;
4027 }
4028
4029
4030 /* Compute the face one character before or after the current position
4031 of IT, in the visual order. BEFORE_P means get the face
4032 in front (to the left in L2R paragraphs, to the right in R2L
4033 paragraphs) of IT's screen position. Value is the ID of the face. */
4034
4035 static int
4036 face_before_or_after_it_pos (struct it *it, bool before_p)
4037 {
4038 int face_id, limit;
4039 ptrdiff_t next_check_charpos;
4040 struct it it_copy;
4041 void *it_copy_data = NULL;
4042
4043 eassert (it->s == NULL);
4044
4045 if (STRINGP (it->string))
4046 {
4047 ptrdiff_t bufpos, charpos;
4048 int base_face_id;
4049
4050 /* No face change past the end of the string (for the case
4051 we are padding with spaces). No face change before the
4052 string start. */
4053 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4054 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4055 return it->face_id;
4056
4057 if (!it->bidi_p)
4058 {
4059 /* Set charpos to the position before or after IT's current
4060 position, in the logical order, which in the non-bidi
4061 case is the same as the visual order. */
4062 if (before_p)
4063 charpos = IT_STRING_CHARPOS (*it) - 1;
4064 else if (it->what == IT_COMPOSITION)
4065 /* For composition, we must check the character after the
4066 composition. */
4067 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4068 else
4069 charpos = IT_STRING_CHARPOS (*it) + 1;
4070 }
4071 else
4072 {
4073 if (before_p)
4074 {
4075 /* With bidi iteration, the character before the current
4076 in the visual order cannot be found by simple
4077 iteration, because "reverse" reordering is not
4078 supported. Instead, we need to start from the string
4079 beginning and go all the way to the current string
4080 position, remembering the previous position. */
4081 /* Ignore face changes before the first visible
4082 character on this display line. */
4083 if (it->current_x <= it->first_visible_x)
4084 return it->face_id;
4085 SAVE_IT (it_copy, *it, it_copy_data);
4086 IT_STRING_CHARPOS (it_copy) = 0;
4087 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4088
4089 do
4090 {
4091 charpos = IT_STRING_CHARPOS (it_copy);
4092 if (charpos >= SCHARS (it->string))
4093 break;
4094 bidi_move_to_visually_next (&it_copy.bidi_it);
4095 }
4096 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4097
4098 RESTORE_IT (it, it, it_copy_data);
4099 }
4100 else
4101 {
4102 /* Set charpos to the string position of the character
4103 that comes after IT's current position in the visual
4104 order. */
4105 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4106
4107 it_copy = *it;
4108 while (n--)
4109 bidi_move_to_visually_next (&it_copy.bidi_it);
4110
4111 charpos = it_copy.bidi_it.charpos;
4112 }
4113 }
4114 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4115
4116 if (it->current.overlay_string_index >= 0)
4117 bufpos = IT_CHARPOS (*it);
4118 else
4119 bufpos = 0;
4120
4121 base_face_id = underlying_face_id (it);
4122
4123 /* Get the face for ASCII, or unibyte. */
4124 face_id = face_at_string_position (it->w,
4125 it->string,
4126 charpos,
4127 bufpos,
4128 &next_check_charpos,
4129 base_face_id, false);
4130
4131 /* Correct the face for charsets different from ASCII. Do it
4132 for the multibyte case only. The face returned above is
4133 suitable for unibyte text if IT->string is unibyte. */
4134 if (STRING_MULTIBYTE (it->string))
4135 {
4136 struct text_pos pos1 = string_pos (charpos, it->string);
4137 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4138 int c, len;
4139 struct face *face = FACE_FROM_ID (it->f, face_id);
4140
4141 c = string_char_and_length (p, &len);
4142 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4143 }
4144 }
4145 else
4146 {
4147 struct text_pos pos;
4148
4149 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4150 || (IT_CHARPOS (*it) <= BEGV && before_p))
4151 return it->face_id;
4152
4153 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4154 pos = it->current.pos;
4155
4156 if (!it->bidi_p)
4157 {
4158 if (before_p)
4159 DEC_TEXT_POS (pos, it->multibyte_p);
4160 else
4161 {
4162 if (it->what == IT_COMPOSITION)
4163 {
4164 /* For composition, we must check the position after
4165 the composition. */
4166 pos.charpos += it->cmp_it.nchars;
4167 pos.bytepos += it->len;
4168 }
4169 else
4170 INC_TEXT_POS (pos, it->multibyte_p);
4171 }
4172 }
4173 else
4174 {
4175 if (before_p)
4176 {
4177 int current_x;
4178
4179 /* With bidi iteration, the character before the current
4180 in the visual order cannot be found by simple
4181 iteration, because "reverse" reordering is not
4182 supported. Instead, we need to use the move_it_*
4183 family of functions, and move to the previous
4184 character starting from the beginning of the visual
4185 line. */
4186 /* Ignore face changes before the first visible
4187 character on this display line. */
4188 if (it->current_x <= it->first_visible_x)
4189 return it->face_id;
4190 SAVE_IT (it_copy, *it, it_copy_data);
4191 /* Implementation note: Since move_it_in_display_line
4192 works in the iterator geometry, and thinks the first
4193 character is always the leftmost, even in R2L lines,
4194 we don't need to distinguish between the R2L and L2R
4195 cases here. */
4196 current_x = it_copy.current_x;
4197 move_it_vertically_backward (&it_copy, 0);
4198 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4199 pos = it_copy.current.pos;
4200 RESTORE_IT (it, it, it_copy_data);
4201 }
4202 else
4203 {
4204 /* Set charpos to the buffer position of the character
4205 that comes after IT's current position in the visual
4206 order. */
4207 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4208
4209 it_copy = *it;
4210 while (n--)
4211 bidi_move_to_visually_next (&it_copy.bidi_it);
4212
4213 SET_TEXT_POS (pos,
4214 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4215 }
4216 }
4217 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4218
4219 /* Determine face for CHARSET_ASCII, or unibyte. */
4220 face_id = face_at_buffer_position (it->w,
4221 CHARPOS (pos),
4222 &next_check_charpos,
4223 limit, false, -1);
4224
4225 /* Correct the face for charsets different from ASCII. Do it
4226 for the multibyte case only. The face returned above is
4227 suitable for unibyte text if current_buffer is unibyte. */
4228 if (it->multibyte_p)
4229 {
4230 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4231 struct face *face = FACE_FROM_ID (it->f, face_id);
4232 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4233 }
4234 }
4235
4236 return face_id;
4237 }
4238
4239
4240 \f
4241 /***********************************************************************
4242 Invisible text
4243 ***********************************************************************/
4244
4245 /* Set up iterator IT from invisible properties at its current
4246 position. Called from handle_stop. */
4247
4248 static enum prop_handled
4249 handle_invisible_prop (struct it *it)
4250 {
4251 enum prop_handled handled = HANDLED_NORMALLY;
4252 int invis;
4253 Lisp_Object prop;
4254
4255 if (STRINGP (it->string))
4256 {
4257 Lisp_Object end_charpos, limit;
4258
4259 /* Get the value of the invisible text property at the
4260 current position. Value will be nil if there is no such
4261 property. */
4262 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4263 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4264 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4265
4266 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4267 {
4268 /* Record whether we have to display an ellipsis for the
4269 invisible text. */
4270 bool display_ellipsis_p = (invis == 2);
4271 ptrdiff_t len, endpos;
4272
4273 handled = HANDLED_RECOMPUTE_PROPS;
4274
4275 /* Get the position at which the next visible text can be
4276 found in IT->string, if any. */
4277 endpos = len = SCHARS (it->string);
4278 XSETINT (limit, len);
4279 do
4280 {
4281 end_charpos
4282 = Fnext_single_property_change (end_charpos, Qinvisible,
4283 it->string, limit);
4284 /* Since LIMIT is always an integer, so should be the
4285 value returned by Fnext_single_property_change. */
4286 eassert (INTEGERP (end_charpos));
4287 if (INTEGERP (end_charpos))
4288 {
4289 endpos = XFASTINT (end_charpos);
4290 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4291 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4292 if (invis == 2)
4293 display_ellipsis_p = true;
4294 }
4295 else /* Should never happen; but if it does, exit the loop. */
4296 endpos = len;
4297 }
4298 while (invis != 0 && endpos < len);
4299
4300 if (display_ellipsis_p)
4301 it->ellipsis_p = true;
4302
4303 if (endpos < len)
4304 {
4305 /* Text at END_CHARPOS is visible. Move IT there. */
4306 struct text_pos old;
4307 ptrdiff_t oldpos;
4308
4309 old = it->current.string_pos;
4310 oldpos = CHARPOS (old);
4311 if (it->bidi_p)
4312 {
4313 if (it->bidi_it.first_elt
4314 && it->bidi_it.charpos < SCHARS (it->string))
4315 bidi_paragraph_init (it->paragraph_embedding,
4316 &it->bidi_it, true);
4317 /* Bidi-iterate out of the invisible text. */
4318 do
4319 {
4320 bidi_move_to_visually_next (&it->bidi_it);
4321 }
4322 while (oldpos <= it->bidi_it.charpos
4323 && it->bidi_it.charpos < endpos);
4324
4325 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4326 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4327 if (IT_CHARPOS (*it) >= endpos)
4328 it->prev_stop = endpos;
4329 }
4330 else
4331 {
4332 IT_STRING_CHARPOS (*it) = endpos;
4333 compute_string_pos (&it->current.string_pos, old, it->string);
4334 }
4335 }
4336 else
4337 {
4338 /* The rest of the string is invisible. If this is an
4339 overlay string, proceed with the next overlay string
4340 or whatever comes and return a character from there. */
4341 if (it->current.overlay_string_index >= 0
4342 && !display_ellipsis_p)
4343 {
4344 next_overlay_string (it);
4345 /* Don't check for overlay strings when we just
4346 finished processing them. */
4347 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4348 }
4349 else
4350 {
4351 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4352 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4353 }
4354 }
4355 }
4356 }
4357 else
4358 {
4359 ptrdiff_t newpos, next_stop, start_charpos, tem;
4360 Lisp_Object pos, overlay;
4361
4362 /* First of all, is there invisible text at this position? */
4363 tem = start_charpos = IT_CHARPOS (*it);
4364 pos = make_number (tem);
4365 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4366 &overlay);
4367 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4368
4369 /* If we are on invisible text, skip over it. */
4370 if (invis != 0 && start_charpos < it->end_charpos)
4371 {
4372 /* Record whether we have to display an ellipsis for the
4373 invisible text. */
4374 bool display_ellipsis_p = invis == 2;
4375
4376 handled = HANDLED_RECOMPUTE_PROPS;
4377
4378 /* Loop skipping over invisible text. The loop is left at
4379 ZV or with IT on the first char being visible again. */
4380 do
4381 {
4382 /* Try to skip some invisible text. Return value is the
4383 position reached which can be equal to where we start
4384 if there is nothing invisible there. This skips both
4385 over invisible text properties and overlays with
4386 invisible property. */
4387 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4388
4389 /* If we skipped nothing at all we weren't at invisible
4390 text in the first place. If everything to the end of
4391 the buffer was skipped, end the loop. */
4392 if (newpos == tem || newpos >= ZV)
4393 invis = 0;
4394 else
4395 {
4396 /* We skipped some characters but not necessarily
4397 all there are. Check if we ended up on visible
4398 text. Fget_char_property returns the property of
4399 the char before the given position, i.e. if we
4400 get invis = 0, this means that the char at
4401 newpos is visible. */
4402 pos = make_number (newpos);
4403 prop = Fget_char_property (pos, Qinvisible, it->window);
4404 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4405 }
4406
4407 /* If we ended up on invisible text, proceed to
4408 skip starting with next_stop. */
4409 if (invis != 0)
4410 tem = next_stop;
4411
4412 /* If there are adjacent invisible texts, don't lose the
4413 second one's ellipsis. */
4414 if (invis == 2)
4415 display_ellipsis_p = true;
4416 }
4417 while (invis != 0);
4418
4419 /* The position newpos is now either ZV or on visible text. */
4420 if (it->bidi_p)
4421 {
4422 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4423 bool on_newline
4424 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4425 bool after_newline
4426 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4427
4428 /* If the invisible text ends on a newline or on a
4429 character after a newline, we can avoid the costly,
4430 character by character, bidi iteration to NEWPOS, and
4431 instead simply reseat the iterator there. That's
4432 because all bidi reordering information is tossed at
4433 the newline. This is a big win for modes that hide
4434 complete lines, like Outline, Org, etc. */
4435 if (on_newline || after_newline)
4436 {
4437 struct text_pos tpos;
4438 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4439
4440 SET_TEXT_POS (tpos, newpos, bpos);
4441 reseat_1 (it, tpos, false);
4442 /* If we reseat on a newline/ZV, we need to prep the
4443 bidi iterator for advancing to the next character
4444 after the newline/EOB, keeping the current paragraph
4445 direction (so that PRODUCE_GLYPHS does TRT wrt
4446 prepending/appending glyphs to a glyph row). */
4447 if (on_newline)
4448 {
4449 it->bidi_it.first_elt = false;
4450 it->bidi_it.paragraph_dir = pdir;
4451 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4452 it->bidi_it.nchars = 1;
4453 it->bidi_it.ch_len = 1;
4454 }
4455 }
4456 else /* Must use the slow method. */
4457 {
4458 /* With bidi iteration, the region of invisible text
4459 could start and/or end in the middle of a
4460 non-base embedding level. Therefore, we need to
4461 skip invisible text using the bidi iterator,
4462 starting at IT's current position, until we find
4463 ourselves outside of the invisible text.
4464 Skipping invisible text _after_ bidi iteration
4465 avoids affecting the visual order of the
4466 displayed text when invisible properties are
4467 added or removed. */
4468 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4469 {
4470 /* If we were `reseat'ed to a new paragraph,
4471 determine the paragraph base direction. We
4472 need to do it now because
4473 next_element_from_buffer may not have a
4474 chance to do it, if we are going to skip any
4475 text at the beginning, which resets the
4476 FIRST_ELT flag. */
4477 bidi_paragraph_init (it->paragraph_embedding,
4478 &it->bidi_it, true);
4479 }
4480 do
4481 {
4482 bidi_move_to_visually_next (&it->bidi_it);
4483 }
4484 while (it->stop_charpos <= it->bidi_it.charpos
4485 && it->bidi_it.charpos < newpos);
4486 IT_CHARPOS (*it) = it->bidi_it.charpos;
4487 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4488 /* If we overstepped NEWPOS, record its position in
4489 the iterator, so that we skip invisible text if
4490 later the bidi iteration lands us in the
4491 invisible region again. */
4492 if (IT_CHARPOS (*it) >= newpos)
4493 it->prev_stop = newpos;
4494 }
4495 }
4496 else
4497 {
4498 IT_CHARPOS (*it) = newpos;
4499 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4500 }
4501
4502 if (display_ellipsis_p)
4503 {
4504 /* Make sure that the glyphs of the ellipsis will get
4505 correct `charpos' values. If we would not update
4506 it->position here, the glyphs would belong to the
4507 last visible character _before_ the invisible
4508 text, which confuses `set_cursor_from_row'.
4509
4510 We use the last invisible position instead of the
4511 first because this way the cursor is always drawn on
4512 the first "." of the ellipsis, whenever PT is inside
4513 the invisible text. Otherwise the cursor would be
4514 placed _after_ the ellipsis when the point is after the
4515 first invisible character. */
4516 if (!STRINGP (it->object))
4517 {
4518 it->position.charpos = newpos - 1;
4519 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4520 }
4521 }
4522
4523 /* If there are before-strings at the start of invisible
4524 text, and the text is invisible because of a text
4525 property, arrange to show before-strings because 20.x did
4526 it that way. (If the text is invisible because of an
4527 overlay property instead of a text property, this is
4528 already handled in the overlay code.) */
4529 if (NILP (overlay)
4530 && get_overlay_strings (it, it->stop_charpos))
4531 {
4532 handled = HANDLED_RECOMPUTE_PROPS;
4533 if (it->sp > 0)
4534 {
4535 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4536 /* The call to get_overlay_strings above recomputes
4537 it->stop_charpos, but it only considers changes
4538 in properties and overlays beyond iterator's
4539 current position. This causes us to miss changes
4540 that happen exactly where the invisible property
4541 ended. So we play it safe here and force the
4542 iterator to check for potential stop positions
4543 immediately after the invisible text. Note that
4544 if get_overlay_strings returns true, it
4545 normally also pushed the iterator stack, so we
4546 need to update the stop position in the slot
4547 below the current one. */
4548 it->stack[it->sp - 1].stop_charpos
4549 = CHARPOS (it->stack[it->sp - 1].current.pos);
4550 }
4551 }
4552 else if (display_ellipsis_p)
4553 {
4554 it->ellipsis_p = true;
4555 /* Let the ellipsis display before
4556 considering any properties of the following char.
4557 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4558 handled = HANDLED_RETURN;
4559 }
4560 }
4561 }
4562
4563 return handled;
4564 }
4565
4566
4567 /* Make iterator IT return `...' next.
4568 Replaces LEN characters from buffer. */
4569
4570 static void
4571 setup_for_ellipsis (struct it *it, int len)
4572 {
4573 /* Use the display table definition for `...'. Invalid glyphs
4574 will be handled by the method returning elements from dpvec. */
4575 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4576 {
4577 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4578 it->dpvec = v->contents;
4579 it->dpend = v->contents + v->header.size;
4580 }
4581 else
4582 {
4583 /* Default `...'. */
4584 it->dpvec = default_invis_vector;
4585 it->dpend = default_invis_vector + 3;
4586 }
4587
4588 it->dpvec_char_len = len;
4589 it->current.dpvec_index = 0;
4590 it->dpvec_face_id = -1;
4591
4592 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4593 face as the preceding text. IT->saved_face_id was set in
4594 handle_stop to the face of the preceding character, and will be
4595 different from IT->face_id only if the invisible text skipped in
4596 handle_invisible_prop has some non-default face on its first
4597 character. We thus ignore the face of the invisible text when we
4598 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4599 if (it->saved_face_id >= 0)
4600 it->face_id = it->saved_face_id;
4601
4602 /* If the ellipsis represents buffer text, it means we advanced in
4603 the buffer, so we should no longer ignore overlay strings. */
4604 if (it->method == GET_FROM_BUFFER)
4605 it->ignore_overlay_strings_at_pos_p = false;
4606
4607 it->method = GET_FROM_DISPLAY_VECTOR;
4608 it->ellipsis_p = true;
4609 }
4610
4611
4612 \f
4613 /***********************************************************************
4614 'display' property
4615 ***********************************************************************/
4616
4617 /* Set up iterator IT from `display' property at its current position.
4618 Called from handle_stop.
4619 We return HANDLED_RETURN if some part of the display property
4620 overrides the display of the buffer text itself.
4621 Otherwise we return HANDLED_NORMALLY. */
4622
4623 static enum prop_handled
4624 handle_display_prop (struct it *it)
4625 {
4626 Lisp_Object propval, object, overlay;
4627 struct text_pos *position;
4628 ptrdiff_t bufpos;
4629 /* Nonzero if some property replaces the display of the text itself. */
4630 int display_replaced = 0;
4631
4632 if (STRINGP (it->string))
4633 {
4634 object = it->string;
4635 position = &it->current.string_pos;
4636 bufpos = CHARPOS (it->current.pos);
4637 }
4638 else
4639 {
4640 XSETWINDOW (object, it->w);
4641 position = &it->current.pos;
4642 bufpos = CHARPOS (*position);
4643 }
4644
4645 /* Reset those iterator values set from display property values. */
4646 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4647 it->space_width = Qnil;
4648 it->font_height = Qnil;
4649 it->voffset = 0;
4650
4651 /* We don't support recursive `display' properties, i.e. string
4652 values that have a string `display' property, that have a string
4653 `display' property etc. */
4654 if (!it->string_from_display_prop_p)
4655 it->area = TEXT_AREA;
4656
4657 propval = get_char_property_and_overlay (make_number (position->charpos),
4658 Qdisplay, object, &overlay);
4659 if (NILP (propval))
4660 return HANDLED_NORMALLY;
4661 /* Now OVERLAY is the overlay that gave us this property, or nil
4662 if it was a text property. */
4663
4664 if (!STRINGP (it->string))
4665 object = it->w->contents;
4666
4667 display_replaced = handle_display_spec (it, propval, object, overlay,
4668 position, bufpos,
4669 FRAME_WINDOW_P (it->f));
4670 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4671 }
4672
4673 /* Subroutine of handle_display_prop. Returns non-zero if the display
4674 specification in SPEC is a replacing specification, i.e. it would
4675 replace the text covered by `display' property with something else,
4676 such as an image or a display string. If SPEC includes any kind or
4677 `(space ...) specification, the value is 2; this is used by
4678 compute_display_string_pos, which see.
4679
4680 See handle_single_display_spec for documentation of arguments.
4681 FRAME_WINDOW_P is true if the window being redisplayed is on a
4682 GUI frame; this argument is used only if IT is NULL, see below.
4683
4684 IT can be NULL, if this is called by the bidi reordering code
4685 through compute_display_string_pos, which see. In that case, this
4686 function only examines SPEC, but does not otherwise "handle" it, in
4687 the sense that it doesn't set up members of IT from the display
4688 spec. */
4689 static int
4690 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4691 Lisp_Object overlay, struct text_pos *position,
4692 ptrdiff_t bufpos, bool frame_window_p)
4693 {
4694 int replacing = 0;
4695
4696 if (CONSP (spec)
4697 /* Simple specifications. */
4698 && !EQ (XCAR (spec), Qimage)
4699 #ifdef HAVE_XWIDGETS
4700 && !EQ (XCAR (spec), Qxwidget)
4701 #endif
4702 && !EQ (XCAR (spec), Qspace)
4703 && !EQ (XCAR (spec), Qwhen)
4704 && !EQ (XCAR (spec), Qslice)
4705 && !EQ (XCAR (spec), Qspace_width)
4706 && !EQ (XCAR (spec), Qheight)
4707 && !EQ (XCAR (spec), Qraise)
4708 /* Marginal area specifications. */
4709 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4710 && !EQ (XCAR (spec), Qleft_fringe)
4711 && !EQ (XCAR (spec), Qright_fringe)
4712 && !NILP (XCAR (spec)))
4713 {
4714 for (; CONSP (spec); spec = XCDR (spec))
4715 {
4716 int rv = handle_single_display_spec (it, XCAR (spec), object,
4717 overlay, position, bufpos,
4718 replacing, frame_window_p);
4719 if (rv != 0)
4720 {
4721 replacing = rv;
4722 /* If some text in a string is replaced, `position' no
4723 longer points to the position of `object'. */
4724 if (!it || STRINGP (object))
4725 break;
4726 }
4727 }
4728 }
4729 else if (VECTORP (spec))
4730 {
4731 ptrdiff_t i;
4732 for (i = 0; i < ASIZE (spec); ++i)
4733 {
4734 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4735 overlay, position, bufpos,
4736 replacing, frame_window_p);
4737 if (rv != 0)
4738 {
4739 replacing = rv;
4740 /* If some text in a string is replaced, `position' no
4741 longer points to the position of `object'. */
4742 if (!it || STRINGP (object))
4743 break;
4744 }
4745 }
4746 }
4747 else
4748 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4749 bufpos, 0, frame_window_p);
4750 return replacing;
4751 }
4752
4753 /* Value is the position of the end of the `display' property starting
4754 at START_POS in OBJECT. */
4755
4756 static struct text_pos
4757 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4758 {
4759 Lisp_Object end;
4760 struct text_pos end_pos;
4761
4762 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4763 Qdisplay, object, Qnil);
4764 CHARPOS (end_pos) = XFASTINT (end);
4765 if (STRINGP (object))
4766 compute_string_pos (&end_pos, start_pos, it->string);
4767 else
4768 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4769
4770 return end_pos;
4771 }
4772
4773
4774 /* Set up IT from a single `display' property specification SPEC. OBJECT
4775 is the object in which the `display' property was found. *POSITION
4776 is the position in OBJECT at which the `display' property was found.
4777 BUFPOS is the buffer position of OBJECT (different from POSITION if
4778 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4779 previously saw a display specification which already replaced text
4780 display with something else, for example an image; we ignore such
4781 properties after the first one has been processed.
4782
4783 OVERLAY is the overlay this `display' property came from,
4784 or nil if it was a text property.
4785
4786 If SPEC is a `space' or `image' specification, and in some other
4787 cases too, set *POSITION to the position where the `display'
4788 property ends.
4789
4790 If IT is NULL, only examine the property specification in SPEC, but
4791 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4792 is intended to be displayed in a window on a GUI frame.
4793
4794 Value is non-zero if something was found which replaces the display
4795 of buffer or string text. */
4796
4797 static int
4798 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4799 Lisp_Object overlay, struct text_pos *position,
4800 ptrdiff_t bufpos, int display_replaced,
4801 bool frame_window_p)
4802 {
4803 Lisp_Object form;
4804 Lisp_Object location, value;
4805 struct text_pos start_pos = *position;
4806
4807 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4808 If the result is non-nil, use VALUE instead of SPEC. */
4809 form = Qt;
4810 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4811 {
4812 spec = XCDR (spec);
4813 if (!CONSP (spec))
4814 return 0;
4815 form = XCAR (spec);
4816 spec = XCDR (spec);
4817 }
4818
4819 if (!NILP (form) && !EQ (form, Qt))
4820 {
4821 ptrdiff_t count = SPECPDL_INDEX ();
4822
4823 /* Bind `object' to the object having the `display' property, a
4824 buffer or string. Bind `position' to the position in the
4825 object where the property was found, and `buffer-position'
4826 to the current position in the buffer. */
4827
4828 if (NILP (object))
4829 XSETBUFFER (object, current_buffer);
4830 specbind (Qobject, object);
4831 specbind (Qposition, make_number (CHARPOS (*position)));
4832 specbind (Qbuffer_position, make_number (bufpos));
4833 form = safe_eval (form);
4834 unbind_to (count, Qnil);
4835 }
4836
4837 if (NILP (form))
4838 return 0;
4839
4840 /* Handle `(height HEIGHT)' specifications. */
4841 if (CONSP (spec)
4842 && EQ (XCAR (spec), Qheight)
4843 && CONSP (XCDR (spec)))
4844 {
4845 if (it)
4846 {
4847 if (!FRAME_WINDOW_P (it->f))
4848 return 0;
4849
4850 it->font_height = XCAR (XCDR (spec));
4851 if (!NILP (it->font_height))
4852 {
4853 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4854 int new_height = -1;
4855
4856 if (CONSP (it->font_height)
4857 && (EQ (XCAR (it->font_height), Qplus)
4858 || EQ (XCAR (it->font_height), Qminus))
4859 && CONSP (XCDR (it->font_height))
4860 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4861 {
4862 /* `(+ N)' or `(- N)' where N is an integer. */
4863 int steps = XINT (XCAR (XCDR (it->font_height)));
4864 if (EQ (XCAR (it->font_height), Qplus))
4865 steps = - steps;
4866 it->face_id = smaller_face (it->f, it->face_id, steps);
4867 }
4868 else if (FUNCTIONP (it->font_height))
4869 {
4870 /* Call function with current height as argument.
4871 Value is the new height. */
4872 Lisp_Object height;
4873 height = safe_call1 (it->font_height,
4874 face->lface[LFACE_HEIGHT_INDEX]);
4875 if (NUMBERP (height))
4876 new_height = XFLOATINT (height);
4877 }
4878 else if (NUMBERP (it->font_height))
4879 {
4880 /* Value is a multiple of the canonical char height. */
4881 struct face *f;
4882
4883 f = FACE_FROM_ID (it->f,
4884 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4885 new_height = (XFLOATINT (it->font_height)
4886 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4887 }
4888 else
4889 {
4890 /* Evaluate IT->font_height with `height' bound to the
4891 current specified height to get the new height. */
4892 ptrdiff_t count = SPECPDL_INDEX ();
4893
4894 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4895 value = safe_eval (it->font_height);
4896 unbind_to (count, Qnil);
4897
4898 if (NUMBERP (value))
4899 new_height = XFLOATINT (value);
4900 }
4901
4902 if (new_height > 0)
4903 it->face_id = face_with_height (it->f, it->face_id, new_height);
4904 }
4905 }
4906
4907 return 0;
4908 }
4909
4910 /* Handle `(space-width WIDTH)'. */
4911 if (CONSP (spec)
4912 && EQ (XCAR (spec), Qspace_width)
4913 && CONSP (XCDR (spec)))
4914 {
4915 if (it)
4916 {
4917 if (!FRAME_WINDOW_P (it->f))
4918 return 0;
4919
4920 value = XCAR (XCDR (spec));
4921 if (NUMBERP (value) && XFLOATINT (value) > 0)
4922 it->space_width = value;
4923 }
4924
4925 return 0;
4926 }
4927
4928 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4929 if (CONSP (spec)
4930 && EQ (XCAR (spec), Qslice))
4931 {
4932 Lisp_Object tem;
4933
4934 if (it)
4935 {
4936 if (!FRAME_WINDOW_P (it->f))
4937 return 0;
4938
4939 if (tem = XCDR (spec), CONSP (tem))
4940 {
4941 it->slice.x = XCAR (tem);
4942 if (tem = XCDR (tem), CONSP (tem))
4943 {
4944 it->slice.y = XCAR (tem);
4945 if (tem = XCDR (tem), CONSP (tem))
4946 {
4947 it->slice.width = XCAR (tem);
4948 if (tem = XCDR (tem), CONSP (tem))
4949 it->slice.height = XCAR (tem);
4950 }
4951 }
4952 }
4953 }
4954
4955 return 0;
4956 }
4957
4958 /* Handle `(raise FACTOR)'. */
4959 if (CONSP (spec)
4960 && EQ (XCAR (spec), Qraise)
4961 && CONSP (XCDR (spec)))
4962 {
4963 if (it)
4964 {
4965 if (!FRAME_WINDOW_P (it->f))
4966 return 0;
4967
4968 #ifdef HAVE_WINDOW_SYSTEM
4969 value = XCAR (XCDR (spec));
4970 if (NUMBERP (value))
4971 {
4972 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4973 it->voffset = - (XFLOATINT (value)
4974 * (normal_char_height (face->font, -1)));
4975 }
4976 #endif /* HAVE_WINDOW_SYSTEM */
4977 }
4978
4979 return 0;
4980 }
4981
4982 /* Don't handle the other kinds of display specifications
4983 inside a string that we got from a `display' property. */
4984 if (it && it->string_from_display_prop_p)
4985 return 0;
4986
4987 /* Characters having this form of property are not displayed, so
4988 we have to find the end of the property. */
4989 if (it)
4990 {
4991 start_pos = *position;
4992 *position = display_prop_end (it, object, start_pos);
4993 /* If the display property comes from an overlay, don't consider
4994 any potential stop_charpos values before the end of that
4995 overlay. Since display_prop_end will happily find another
4996 'display' property coming from some other overlay or text
4997 property on buffer positions before this overlay's end, we
4998 need to ignore them, or else we risk displaying this
4999 overlay's display string/image twice. */
5000 if (!NILP (overlay))
5001 {
5002 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5003
5004 if (ovendpos > CHARPOS (*position))
5005 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5006 }
5007 }
5008 value = Qnil;
5009
5010 /* Stop the scan at that end position--we assume that all
5011 text properties change there. */
5012 if (it)
5013 it->stop_charpos = position->charpos;
5014
5015 /* Handle `(left-fringe BITMAP [FACE])'
5016 and `(right-fringe BITMAP [FACE])'. */
5017 if (CONSP (spec)
5018 && (EQ (XCAR (spec), Qleft_fringe)
5019 || EQ (XCAR (spec), Qright_fringe))
5020 && CONSP (XCDR (spec)))
5021 {
5022 int fringe_bitmap;
5023
5024 if (it)
5025 {
5026 if (!FRAME_WINDOW_P (it->f))
5027 /* If we return here, POSITION has been advanced
5028 across the text with this property. */
5029 {
5030 /* Synchronize the bidi iterator with POSITION. This is
5031 needed because we are not going to push the iterator
5032 on behalf of this display property, so there will be
5033 no pop_it call to do this synchronization for us. */
5034 if (it->bidi_p)
5035 {
5036 it->position = *position;
5037 iterate_out_of_display_property (it);
5038 *position = it->position;
5039 }
5040 return 1;
5041 }
5042 }
5043 else if (!frame_window_p)
5044 return 1;
5045
5046 #ifdef HAVE_WINDOW_SYSTEM
5047 value = XCAR (XCDR (spec));
5048 if (!SYMBOLP (value)
5049 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
5050 /* If we return here, POSITION has been advanced
5051 across the text with this property. */
5052 {
5053 if (it && it->bidi_p)
5054 {
5055 it->position = *position;
5056 iterate_out_of_display_property (it);
5057 *position = it->position;
5058 }
5059 return 1;
5060 }
5061
5062 if (it)
5063 {
5064 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5065
5066 if (CONSP (XCDR (XCDR (spec))))
5067 {
5068 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5069 int face_id2 = lookup_derived_face (it->f, face_name,
5070 FRINGE_FACE_ID, false);
5071 if (face_id2 >= 0)
5072 face_id = face_id2;
5073 }
5074
5075 /* Save current settings of IT so that we can restore them
5076 when we are finished with the glyph property value. */
5077 push_it (it, position);
5078
5079 it->area = TEXT_AREA;
5080 it->what = IT_IMAGE;
5081 it->image_id = -1; /* no image */
5082 it->position = start_pos;
5083 it->object = NILP (object) ? it->w->contents : object;
5084 it->method = GET_FROM_IMAGE;
5085 it->from_overlay = Qnil;
5086 it->face_id = face_id;
5087 it->from_disp_prop_p = true;
5088
5089 /* Say that we haven't consumed the characters with
5090 `display' property yet. The call to pop_it in
5091 set_iterator_to_next will clean this up. */
5092 *position = start_pos;
5093
5094 if (EQ (XCAR (spec), Qleft_fringe))
5095 {
5096 it->left_user_fringe_bitmap = fringe_bitmap;
5097 it->left_user_fringe_face_id = face_id;
5098 }
5099 else
5100 {
5101 it->right_user_fringe_bitmap = fringe_bitmap;
5102 it->right_user_fringe_face_id = face_id;
5103 }
5104 }
5105 #endif /* HAVE_WINDOW_SYSTEM */
5106 return 1;
5107 }
5108
5109 /* Prepare to handle `((margin left-margin) ...)',
5110 `((margin right-margin) ...)' and `((margin nil) ...)'
5111 prefixes for display specifications. */
5112 location = Qunbound;
5113 if (CONSP (spec) && CONSP (XCAR (spec)))
5114 {
5115 Lisp_Object tem;
5116
5117 value = XCDR (spec);
5118 if (CONSP (value))
5119 value = XCAR (value);
5120
5121 tem = XCAR (spec);
5122 if (EQ (XCAR (tem), Qmargin)
5123 && (tem = XCDR (tem),
5124 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5125 (NILP (tem)
5126 || EQ (tem, Qleft_margin)
5127 || EQ (tem, Qright_margin))))
5128 location = tem;
5129 }
5130
5131 if (EQ (location, Qunbound))
5132 {
5133 location = Qnil;
5134 value = spec;
5135 }
5136
5137 /* After this point, VALUE is the property after any
5138 margin prefix has been stripped. It must be a string,
5139 an image specification, or `(space ...)'.
5140
5141 LOCATION specifies where to display: `left-margin',
5142 `right-margin' or nil. */
5143
5144 bool valid_p = (STRINGP (value)
5145 #ifdef HAVE_WINDOW_SYSTEM
5146 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5147 && valid_image_p (value))
5148 #endif /* not HAVE_WINDOW_SYSTEM */
5149 || (CONSP (value) && EQ (XCAR (value), Qspace))
5150 #ifdef HAVE_XWIDGETS
5151 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5152 && valid_xwidget_spec_p (value))
5153 #endif
5154 );
5155
5156 if (valid_p && display_replaced == 0)
5157 {
5158 int retval = 1;
5159
5160 if (!it)
5161 {
5162 /* Callers need to know whether the display spec is any kind
5163 of `(space ...)' spec that is about to affect text-area
5164 display. */
5165 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5166 retval = 2;
5167 return retval;
5168 }
5169
5170 /* Save current settings of IT so that we can restore them
5171 when we are finished with the glyph property value. */
5172 push_it (it, position);
5173 it->from_overlay = overlay;
5174 it->from_disp_prop_p = true;
5175
5176 if (NILP (location))
5177 it->area = TEXT_AREA;
5178 else if (EQ (location, Qleft_margin))
5179 it->area = LEFT_MARGIN_AREA;
5180 else
5181 it->area = RIGHT_MARGIN_AREA;
5182
5183 if (STRINGP (value))
5184 {
5185 it->string = value;
5186 it->multibyte_p = STRING_MULTIBYTE (it->string);
5187 it->current.overlay_string_index = -1;
5188 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5189 it->end_charpos = it->string_nchars = SCHARS (it->string);
5190 it->method = GET_FROM_STRING;
5191 it->stop_charpos = 0;
5192 it->prev_stop = 0;
5193 it->base_level_stop = 0;
5194 it->string_from_display_prop_p = true;
5195 /* Say that we haven't consumed the characters with
5196 `display' property yet. The call to pop_it in
5197 set_iterator_to_next will clean this up. */
5198 if (BUFFERP (object))
5199 *position = start_pos;
5200
5201 /* Force paragraph direction to be that of the parent
5202 object. If the parent object's paragraph direction is
5203 not yet determined, default to L2R. */
5204 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5205 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5206 else
5207 it->paragraph_embedding = L2R;
5208
5209 /* Set up the bidi iterator for this display string. */
5210 if (it->bidi_p)
5211 {
5212 it->bidi_it.string.lstring = it->string;
5213 it->bidi_it.string.s = NULL;
5214 it->bidi_it.string.schars = it->end_charpos;
5215 it->bidi_it.string.bufpos = bufpos;
5216 it->bidi_it.string.from_disp_str = true;
5217 it->bidi_it.string.unibyte = !it->multibyte_p;
5218 it->bidi_it.w = it->w;
5219 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5220 }
5221 }
5222 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5223 {
5224 it->method = GET_FROM_STRETCH;
5225 it->object = value;
5226 *position = it->position = start_pos;
5227 retval = 1 + (it->area == TEXT_AREA);
5228 }
5229 #ifdef HAVE_XWIDGETS
5230 else if (valid_xwidget_spec_p(value))
5231 {
5232 it->what = IT_XWIDGET;
5233 it->method = GET_FROM_XWIDGET;
5234 it->position = start_pos;
5235 it->object = NILP (object) ? it->w->contents : object;
5236 *position = start_pos;
5237 it->xwidget = lookup_xwidget(value);
5238 }
5239 #endif
5240 #ifdef HAVE_WINDOW_SYSTEM
5241 else
5242 {
5243 it->what = IT_IMAGE;
5244 it->image_id = lookup_image (it->f, value);
5245 it->position = start_pos;
5246 it->object = NILP (object) ? it->w->contents : object;
5247 it->method = GET_FROM_IMAGE;
5248
5249 /* Say that we haven't consumed the characters with
5250 `display' property yet. The call to pop_it in
5251 set_iterator_to_next will clean this up. */
5252 *position = start_pos;
5253 }
5254 #endif /* HAVE_WINDOW_SYSTEM */
5255
5256 return retval;
5257 }
5258
5259 /* Invalid property or property not supported. Restore
5260 POSITION to what it was before. */
5261 *position = start_pos;
5262 return 0;
5263 }
5264
5265 /* Check if PROP is a display property value whose text should be
5266 treated as intangible. OVERLAY is the overlay from which PROP
5267 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5268 specify the buffer position covered by PROP. */
5269
5270 bool
5271 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5272 ptrdiff_t charpos, ptrdiff_t bytepos)
5273 {
5274 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5275 struct text_pos position;
5276
5277 SET_TEXT_POS (position, charpos, bytepos);
5278 return (handle_display_spec (NULL, prop, Qnil, overlay,
5279 &position, charpos, frame_window_p)
5280 != 0);
5281 }
5282
5283
5284 /* Return true if PROP is a display sub-property value containing STRING.
5285
5286 Implementation note: this and the following function are really
5287 special cases of handle_display_spec and
5288 handle_single_display_spec, and should ideally use the same code.
5289 Until they do, these two pairs must be consistent and must be
5290 modified in sync. */
5291
5292 static bool
5293 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5294 {
5295 if (EQ (string, prop))
5296 return true;
5297
5298 /* Skip over `when FORM'. */
5299 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5300 {
5301 prop = XCDR (prop);
5302 if (!CONSP (prop))
5303 return false;
5304 /* Actually, the condition following `when' should be eval'ed,
5305 like handle_single_display_spec does, and we should return
5306 false if it evaluates to nil. However, this function is
5307 called only when the buffer was already displayed and some
5308 glyph in the glyph matrix was found to come from a display
5309 string. Therefore, the condition was already evaluated, and
5310 the result was non-nil, otherwise the display string wouldn't
5311 have been displayed and we would have never been called for
5312 this property. Thus, we can skip the evaluation and assume
5313 its result is non-nil. */
5314 prop = XCDR (prop);
5315 }
5316
5317 if (CONSP (prop))
5318 /* Skip over `margin LOCATION'. */
5319 if (EQ (XCAR (prop), Qmargin))
5320 {
5321 prop = XCDR (prop);
5322 if (!CONSP (prop))
5323 return false;
5324
5325 prop = XCDR (prop);
5326 if (!CONSP (prop))
5327 return false;
5328 }
5329
5330 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5331 }
5332
5333
5334 /* Return true if STRING appears in the `display' property PROP. */
5335
5336 static bool
5337 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5338 {
5339 if (CONSP (prop)
5340 && !EQ (XCAR (prop), Qwhen)
5341 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5342 {
5343 /* A list of sub-properties. */
5344 while (CONSP (prop))
5345 {
5346 if (single_display_spec_string_p (XCAR (prop), string))
5347 return true;
5348 prop = XCDR (prop);
5349 }
5350 }
5351 else if (VECTORP (prop))
5352 {
5353 /* A vector of sub-properties. */
5354 ptrdiff_t i;
5355 for (i = 0; i < ASIZE (prop); ++i)
5356 if (single_display_spec_string_p (AREF (prop, i), string))
5357 return true;
5358 }
5359 else
5360 return single_display_spec_string_p (prop, string);
5361
5362 return false;
5363 }
5364
5365 /* Look for STRING in overlays and text properties in the current
5366 buffer, between character positions FROM and TO (excluding TO).
5367 BACK_P means look back (in this case, TO is supposed to be
5368 less than FROM).
5369 Value is the first character position where STRING was found, or
5370 zero if it wasn't found before hitting TO.
5371
5372 This function may only use code that doesn't eval because it is
5373 called asynchronously from note_mouse_highlight. */
5374
5375 static ptrdiff_t
5376 string_buffer_position_lim (Lisp_Object string,
5377 ptrdiff_t from, ptrdiff_t to, bool back_p)
5378 {
5379 Lisp_Object limit, prop, pos;
5380 bool found = false;
5381
5382 pos = make_number (max (from, BEGV));
5383
5384 if (!back_p) /* looking forward */
5385 {
5386 limit = make_number (min (to, ZV));
5387 while (!found && !EQ (pos, limit))
5388 {
5389 prop = Fget_char_property (pos, Qdisplay, Qnil);
5390 if (!NILP (prop) && display_prop_string_p (prop, string))
5391 found = true;
5392 else
5393 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5394 limit);
5395 }
5396 }
5397 else /* looking back */
5398 {
5399 limit = make_number (max (to, BEGV));
5400 while (!found && !EQ (pos, limit))
5401 {
5402 prop = Fget_char_property (pos, Qdisplay, Qnil);
5403 if (!NILP (prop) && display_prop_string_p (prop, string))
5404 found = true;
5405 else
5406 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5407 limit);
5408 }
5409 }
5410
5411 return found ? XINT (pos) : 0;
5412 }
5413
5414 /* Determine which buffer position in current buffer STRING comes from.
5415 AROUND_CHARPOS is an approximate position where it could come from.
5416 Value is the buffer position or 0 if it couldn't be determined.
5417
5418 This function is necessary because we don't record buffer positions
5419 in glyphs generated from strings (to keep struct glyph small).
5420 This function may only use code that doesn't eval because it is
5421 called asynchronously from note_mouse_highlight. */
5422
5423 static ptrdiff_t
5424 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5425 {
5426 const int MAX_DISTANCE = 1000;
5427 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5428 around_charpos + MAX_DISTANCE,
5429 false);
5430
5431 if (!found)
5432 found = string_buffer_position_lim (string, around_charpos,
5433 around_charpos - MAX_DISTANCE, true);
5434 return found;
5435 }
5436
5437
5438 \f
5439 /***********************************************************************
5440 `composition' property
5441 ***********************************************************************/
5442
5443 /* Set up iterator IT from `composition' property at its current
5444 position. Called from handle_stop. */
5445
5446 static enum prop_handled
5447 handle_composition_prop (struct it *it)
5448 {
5449 Lisp_Object prop, string;
5450 ptrdiff_t pos, pos_byte, start, end;
5451
5452 if (STRINGP (it->string))
5453 {
5454 unsigned char *s;
5455
5456 pos = IT_STRING_CHARPOS (*it);
5457 pos_byte = IT_STRING_BYTEPOS (*it);
5458 string = it->string;
5459 s = SDATA (string) + pos_byte;
5460 it->c = STRING_CHAR (s);
5461 }
5462 else
5463 {
5464 pos = IT_CHARPOS (*it);
5465 pos_byte = IT_BYTEPOS (*it);
5466 string = Qnil;
5467 it->c = FETCH_CHAR (pos_byte);
5468 }
5469
5470 /* If there's a valid composition and point is not inside of the
5471 composition (in the case that the composition is from the current
5472 buffer), draw a glyph composed from the composition components. */
5473 if (find_composition (pos, -1, &start, &end, &prop, string)
5474 && composition_valid_p (start, end, prop)
5475 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5476 {
5477 if (start < pos)
5478 /* As we can't handle this situation (perhaps font-lock added
5479 a new composition), we just return here hoping that next
5480 redisplay will detect this composition much earlier. */
5481 return HANDLED_NORMALLY;
5482 if (start != pos)
5483 {
5484 if (STRINGP (it->string))
5485 pos_byte = string_char_to_byte (it->string, start);
5486 else
5487 pos_byte = CHAR_TO_BYTE (start);
5488 }
5489 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5490 prop, string);
5491
5492 if (it->cmp_it.id >= 0)
5493 {
5494 it->cmp_it.ch = -1;
5495 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5496 it->cmp_it.nglyphs = -1;
5497 }
5498 }
5499
5500 return HANDLED_NORMALLY;
5501 }
5502
5503
5504 \f
5505 /***********************************************************************
5506 Overlay strings
5507 ***********************************************************************/
5508
5509 /* The following structure is used to record overlay strings for
5510 later sorting in load_overlay_strings. */
5511
5512 struct overlay_entry
5513 {
5514 Lisp_Object overlay;
5515 Lisp_Object string;
5516 EMACS_INT priority;
5517 bool after_string_p;
5518 };
5519
5520
5521 /* Set up iterator IT from overlay strings at its current position.
5522 Called from handle_stop. */
5523
5524 static enum prop_handled
5525 handle_overlay_change (struct it *it)
5526 {
5527 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5528 return HANDLED_RECOMPUTE_PROPS;
5529 else
5530 return HANDLED_NORMALLY;
5531 }
5532
5533
5534 /* Set up the next overlay string for delivery by IT, if there is an
5535 overlay string to deliver. Called by set_iterator_to_next when the
5536 end of the current overlay string is reached. If there are more
5537 overlay strings to display, IT->string and
5538 IT->current.overlay_string_index are set appropriately here.
5539 Otherwise IT->string is set to nil. */
5540
5541 static void
5542 next_overlay_string (struct it *it)
5543 {
5544 ++it->current.overlay_string_index;
5545 if (it->current.overlay_string_index == it->n_overlay_strings)
5546 {
5547 /* No more overlay strings. Restore IT's settings to what
5548 they were before overlay strings were processed, and
5549 continue to deliver from current_buffer. */
5550
5551 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5552 pop_it (it);
5553 eassert (it->sp > 0
5554 || (NILP (it->string)
5555 && it->method == GET_FROM_BUFFER
5556 && it->stop_charpos >= BEGV
5557 && it->stop_charpos <= it->end_charpos));
5558 it->current.overlay_string_index = -1;
5559 it->n_overlay_strings = 0;
5560 /* If there's an empty display string on the stack, pop the
5561 stack, to resync the bidi iterator with IT's position. Such
5562 empty strings are pushed onto the stack in
5563 get_overlay_strings_1. */
5564 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5565 pop_it (it);
5566
5567 /* Since we've exhausted overlay strings at this buffer
5568 position, set the flag to ignore overlays until we move to
5569 another position. The flag is reset in
5570 next_element_from_buffer. */
5571 it->ignore_overlay_strings_at_pos_p = true;
5572
5573 /* If we're at the end of the buffer, record that we have
5574 processed the overlay strings there already, so that
5575 next_element_from_buffer doesn't try it again. */
5576 if (NILP (it->string)
5577 && IT_CHARPOS (*it) >= it->end_charpos
5578 && it->overlay_strings_charpos >= it->end_charpos)
5579 it->overlay_strings_at_end_processed_p = true;
5580 /* Note: we reset overlay_strings_charpos only here, to make
5581 sure the just-processed overlays were indeed at EOB.
5582 Otherwise, overlays on text with invisible text property,
5583 which are processed with IT's position past the invisible
5584 text, might fool us into thinking the overlays at EOB were
5585 already processed (linum-mode can cause this, for
5586 example). */
5587 it->overlay_strings_charpos = -1;
5588 }
5589 else
5590 {
5591 /* There are more overlay strings to process. If
5592 IT->current.overlay_string_index has advanced to a position
5593 where we must load IT->overlay_strings with more strings, do
5594 it. We must load at the IT->overlay_strings_charpos where
5595 IT->n_overlay_strings was originally computed; when invisible
5596 text is present, this might not be IT_CHARPOS (Bug#7016). */
5597 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5598
5599 if (it->current.overlay_string_index && i == 0)
5600 load_overlay_strings (it, it->overlay_strings_charpos);
5601
5602 /* Initialize IT to deliver display elements from the overlay
5603 string. */
5604 it->string = it->overlay_strings[i];
5605 it->multibyte_p = STRING_MULTIBYTE (it->string);
5606 SET_TEXT_POS (it->current.string_pos, 0, 0);
5607 it->method = GET_FROM_STRING;
5608 it->stop_charpos = 0;
5609 it->end_charpos = SCHARS (it->string);
5610 if (it->cmp_it.stop_pos >= 0)
5611 it->cmp_it.stop_pos = 0;
5612 it->prev_stop = 0;
5613 it->base_level_stop = 0;
5614
5615 /* Set up the bidi iterator for this overlay string. */
5616 if (it->bidi_p)
5617 {
5618 it->bidi_it.string.lstring = it->string;
5619 it->bidi_it.string.s = NULL;
5620 it->bidi_it.string.schars = SCHARS (it->string);
5621 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5622 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5623 it->bidi_it.string.unibyte = !it->multibyte_p;
5624 it->bidi_it.w = it->w;
5625 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5626 }
5627 }
5628
5629 CHECK_IT (it);
5630 }
5631
5632
5633 /* Compare two overlay_entry structures E1 and E2. Used as a
5634 comparison function for qsort in load_overlay_strings. Overlay
5635 strings for the same position are sorted so that
5636
5637 1. All after-strings come in front of before-strings, except
5638 when they come from the same overlay.
5639
5640 2. Within after-strings, strings are sorted so that overlay strings
5641 from overlays with higher priorities come first.
5642
5643 2. Within before-strings, strings are sorted so that overlay
5644 strings from overlays with higher priorities come last.
5645
5646 Value is analogous to strcmp. */
5647
5648
5649 static int
5650 compare_overlay_entries (const void *e1, const void *e2)
5651 {
5652 struct overlay_entry const *entry1 = e1;
5653 struct overlay_entry const *entry2 = e2;
5654 int result;
5655
5656 if (entry1->after_string_p != entry2->after_string_p)
5657 {
5658 /* Let after-strings appear in front of before-strings if
5659 they come from different overlays. */
5660 if (EQ (entry1->overlay, entry2->overlay))
5661 result = entry1->after_string_p ? 1 : -1;
5662 else
5663 result = entry1->after_string_p ? -1 : 1;
5664 }
5665 else if (entry1->priority != entry2->priority)
5666 {
5667 if (entry1->after_string_p)
5668 /* After-strings sorted in order of decreasing priority. */
5669 result = entry2->priority < entry1->priority ? -1 : 1;
5670 else
5671 /* Before-strings sorted in order of increasing priority. */
5672 result = entry1->priority < entry2->priority ? -1 : 1;
5673 }
5674 else
5675 result = 0;
5676
5677 return result;
5678 }
5679
5680
5681 /* Load the vector IT->overlay_strings with overlay strings from IT's
5682 current buffer position, or from CHARPOS if that is > 0. Set
5683 IT->n_overlays to the total number of overlay strings found.
5684
5685 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5686 a time. On entry into load_overlay_strings,
5687 IT->current.overlay_string_index gives the number of overlay
5688 strings that have already been loaded by previous calls to this
5689 function.
5690
5691 IT->add_overlay_start contains an additional overlay start
5692 position to consider for taking overlay strings from, if non-zero.
5693 This position comes into play when the overlay has an `invisible'
5694 property, and both before and after-strings. When we've skipped to
5695 the end of the overlay, because of its `invisible' property, we
5696 nevertheless want its before-string to appear.
5697 IT->add_overlay_start will contain the overlay start position
5698 in this case.
5699
5700 Overlay strings are sorted so that after-string strings come in
5701 front of before-string strings. Within before and after-strings,
5702 strings are sorted by overlay priority. See also function
5703 compare_overlay_entries. */
5704
5705 static void
5706 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5707 {
5708 Lisp_Object overlay, window, str, invisible;
5709 struct Lisp_Overlay *ov;
5710 ptrdiff_t start, end;
5711 ptrdiff_t n = 0, i, j;
5712 int invis;
5713 struct overlay_entry entriesbuf[20];
5714 ptrdiff_t size = ARRAYELTS (entriesbuf);
5715 struct overlay_entry *entries = entriesbuf;
5716 USE_SAFE_ALLOCA;
5717
5718 if (charpos <= 0)
5719 charpos = IT_CHARPOS (*it);
5720
5721 /* Append the overlay string STRING of overlay OVERLAY to vector
5722 `entries' which has size `size' and currently contains `n'
5723 elements. AFTER_P means STRING is an after-string of
5724 OVERLAY. */
5725 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5726 do \
5727 { \
5728 Lisp_Object priority; \
5729 \
5730 if (n == size) \
5731 { \
5732 struct overlay_entry *old = entries; \
5733 SAFE_NALLOCA (entries, 2, size); \
5734 memcpy (entries, old, size * sizeof *entries); \
5735 size *= 2; \
5736 } \
5737 \
5738 entries[n].string = (STRING); \
5739 entries[n].overlay = (OVERLAY); \
5740 priority = Foverlay_get ((OVERLAY), Qpriority); \
5741 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5742 entries[n].after_string_p = (AFTER_P); \
5743 ++n; \
5744 } \
5745 while (false)
5746
5747 /* Process overlay before the overlay center. */
5748 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5749 {
5750 XSETMISC (overlay, ov);
5751 eassert (OVERLAYP (overlay));
5752 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5753 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5754
5755 if (end < charpos)
5756 break;
5757
5758 /* Skip this overlay if it doesn't start or end at IT's current
5759 position. */
5760 if (end != charpos && start != charpos)
5761 continue;
5762
5763 /* Skip this overlay if it doesn't apply to IT->w. */
5764 window = Foverlay_get (overlay, Qwindow);
5765 if (WINDOWP (window) && XWINDOW (window) != it->w)
5766 continue;
5767
5768 /* If the text ``under'' the overlay is invisible, both before-
5769 and after-strings from this overlay are visible; start and
5770 end position are indistinguishable. */
5771 invisible = Foverlay_get (overlay, Qinvisible);
5772 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5773
5774 /* If overlay has a non-empty before-string, record it. */
5775 if ((start == charpos || (end == charpos && invis != 0))
5776 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5777 && SCHARS (str))
5778 RECORD_OVERLAY_STRING (overlay, str, false);
5779
5780 /* If overlay has a non-empty after-string, record it. */
5781 if ((end == charpos || (start == charpos && invis != 0))
5782 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5783 && SCHARS (str))
5784 RECORD_OVERLAY_STRING (overlay, str, true);
5785 }
5786
5787 /* Process overlays after the overlay center. */
5788 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5789 {
5790 XSETMISC (overlay, ov);
5791 eassert (OVERLAYP (overlay));
5792 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5793 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5794
5795 if (start > charpos)
5796 break;
5797
5798 /* Skip this overlay if it doesn't start or end at IT's current
5799 position. */
5800 if (end != charpos && start != charpos)
5801 continue;
5802
5803 /* Skip this overlay if it doesn't apply to IT->w. */
5804 window = Foverlay_get (overlay, Qwindow);
5805 if (WINDOWP (window) && XWINDOW (window) != it->w)
5806 continue;
5807
5808 /* If the text ``under'' the overlay is invisible, it has a zero
5809 dimension, and both before- and after-strings apply. */
5810 invisible = Foverlay_get (overlay, Qinvisible);
5811 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5812
5813 /* If overlay has a non-empty before-string, record it. */
5814 if ((start == charpos || (end == charpos && invis != 0))
5815 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5816 && SCHARS (str))
5817 RECORD_OVERLAY_STRING (overlay, str, false);
5818
5819 /* If overlay has a non-empty after-string, record it. */
5820 if ((end == charpos || (start == charpos && invis != 0))
5821 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5822 && SCHARS (str))
5823 RECORD_OVERLAY_STRING (overlay, str, true);
5824 }
5825
5826 #undef RECORD_OVERLAY_STRING
5827
5828 /* Sort entries. */
5829 if (n > 1)
5830 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5831
5832 /* Record number of overlay strings, and where we computed it. */
5833 it->n_overlay_strings = n;
5834 it->overlay_strings_charpos = charpos;
5835
5836 /* IT->current.overlay_string_index is the number of overlay strings
5837 that have already been consumed by IT. Copy some of the
5838 remaining overlay strings to IT->overlay_strings. */
5839 i = 0;
5840 j = it->current.overlay_string_index;
5841 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5842 {
5843 it->overlay_strings[i] = entries[j].string;
5844 it->string_overlays[i++] = entries[j++].overlay;
5845 }
5846
5847 CHECK_IT (it);
5848 SAFE_FREE ();
5849 }
5850
5851
5852 /* Get the first chunk of overlay strings at IT's current buffer
5853 position, or at CHARPOS if that is > 0. Value is true if at
5854 least one overlay string was found. */
5855
5856 static bool
5857 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5858 {
5859 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5860 process. This fills IT->overlay_strings with strings, and sets
5861 IT->n_overlay_strings to the total number of strings to process.
5862 IT->pos.overlay_string_index has to be set temporarily to zero
5863 because load_overlay_strings needs this; it must be set to -1
5864 when no overlay strings are found because a zero value would
5865 indicate a position in the first overlay string. */
5866 it->current.overlay_string_index = 0;
5867 load_overlay_strings (it, charpos);
5868
5869 /* If we found overlay strings, set up IT to deliver display
5870 elements from the first one. Otherwise set up IT to deliver
5871 from current_buffer. */
5872 if (it->n_overlay_strings)
5873 {
5874 /* Make sure we know settings in current_buffer, so that we can
5875 restore meaningful values when we're done with the overlay
5876 strings. */
5877 if (compute_stop_p)
5878 compute_stop_pos (it);
5879 eassert (it->face_id >= 0);
5880
5881 /* Save IT's settings. They are restored after all overlay
5882 strings have been processed. */
5883 eassert (!compute_stop_p || it->sp == 0);
5884
5885 /* When called from handle_stop, there might be an empty display
5886 string loaded. In that case, don't bother saving it. But
5887 don't use this optimization with the bidi iterator, since we
5888 need the corresponding pop_it call to resync the bidi
5889 iterator's position with IT's position, after we are done
5890 with the overlay strings. (The corresponding call to pop_it
5891 in case of an empty display string is in
5892 next_overlay_string.) */
5893 if (!(!it->bidi_p
5894 && STRINGP (it->string) && !SCHARS (it->string)))
5895 push_it (it, NULL);
5896
5897 /* Set up IT to deliver display elements from the first overlay
5898 string. */
5899 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5900 it->string = it->overlay_strings[0];
5901 it->from_overlay = Qnil;
5902 it->stop_charpos = 0;
5903 eassert (STRINGP (it->string));
5904 it->end_charpos = SCHARS (it->string);
5905 it->prev_stop = 0;
5906 it->base_level_stop = 0;
5907 it->multibyte_p = STRING_MULTIBYTE (it->string);
5908 it->method = GET_FROM_STRING;
5909 it->from_disp_prop_p = 0;
5910
5911 /* Force paragraph direction to be that of the parent
5912 buffer. */
5913 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5914 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5915 else
5916 it->paragraph_embedding = L2R;
5917
5918 /* Set up the bidi iterator for this overlay string. */
5919 if (it->bidi_p)
5920 {
5921 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5922
5923 it->bidi_it.string.lstring = it->string;
5924 it->bidi_it.string.s = NULL;
5925 it->bidi_it.string.schars = SCHARS (it->string);
5926 it->bidi_it.string.bufpos = pos;
5927 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5928 it->bidi_it.string.unibyte = !it->multibyte_p;
5929 it->bidi_it.w = it->w;
5930 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5931 }
5932 return true;
5933 }
5934
5935 it->current.overlay_string_index = -1;
5936 return false;
5937 }
5938
5939 static bool
5940 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5941 {
5942 it->string = Qnil;
5943 it->method = GET_FROM_BUFFER;
5944
5945 get_overlay_strings_1 (it, charpos, true);
5946
5947 CHECK_IT (it);
5948
5949 /* Value is true if we found at least one overlay string. */
5950 return STRINGP (it->string);
5951 }
5952
5953
5954 \f
5955 /***********************************************************************
5956 Saving and restoring state
5957 ***********************************************************************/
5958
5959 /* Save current settings of IT on IT->stack. Called, for example,
5960 before setting up IT for an overlay string, to be able to restore
5961 IT's settings to what they were after the overlay string has been
5962 processed. If POSITION is non-NULL, it is the position to save on
5963 the stack instead of IT->position. */
5964
5965 static void
5966 push_it (struct it *it, struct text_pos *position)
5967 {
5968 struct iterator_stack_entry *p;
5969
5970 eassert (it->sp < IT_STACK_SIZE);
5971 p = it->stack + it->sp;
5972
5973 p->stop_charpos = it->stop_charpos;
5974 p->prev_stop = it->prev_stop;
5975 p->base_level_stop = it->base_level_stop;
5976 p->cmp_it = it->cmp_it;
5977 eassert (it->face_id >= 0);
5978 p->face_id = it->face_id;
5979 p->string = it->string;
5980 p->method = it->method;
5981 p->from_overlay = it->from_overlay;
5982 switch (p->method)
5983 {
5984 case GET_FROM_IMAGE:
5985 p->u.image.object = it->object;
5986 p->u.image.image_id = it->image_id;
5987 p->u.image.slice = it->slice;
5988 break;
5989 case GET_FROM_STRETCH:
5990 p->u.stretch.object = it->object;
5991 break;
5992 #ifdef HAVE_XWIDGETS
5993 case GET_FROM_XWIDGET:
5994 p->u.xwidget.object = it->object;
5995 break;
5996 #endif
5997 case GET_FROM_BUFFER:
5998 case GET_FROM_DISPLAY_VECTOR:
5999 case GET_FROM_STRING:
6000 case GET_FROM_C_STRING:
6001 break;
6002 default:
6003 emacs_abort ();
6004 }
6005 p->position = position ? *position : it->position;
6006 p->current = it->current;
6007 p->end_charpos = it->end_charpos;
6008 p->string_nchars = it->string_nchars;
6009 p->area = it->area;
6010 p->multibyte_p = it->multibyte_p;
6011 p->avoid_cursor_p = it->avoid_cursor_p;
6012 p->space_width = it->space_width;
6013 p->font_height = it->font_height;
6014 p->voffset = it->voffset;
6015 p->string_from_display_prop_p = it->string_from_display_prop_p;
6016 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6017 p->display_ellipsis_p = false;
6018 p->line_wrap = it->line_wrap;
6019 p->bidi_p = it->bidi_p;
6020 p->paragraph_embedding = it->paragraph_embedding;
6021 p->from_disp_prop_p = it->from_disp_prop_p;
6022 ++it->sp;
6023
6024 /* Save the state of the bidi iterator as well. */
6025 if (it->bidi_p)
6026 bidi_push_it (&it->bidi_it);
6027 }
6028
6029 static void
6030 iterate_out_of_display_property (struct it *it)
6031 {
6032 bool buffer_p = !STRINGP (it->string);
6033 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6034 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6035
6036 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6037
6038 /* Maybe initialize paragraph direction. If we are at the beginning
6039 of a new paragraph, next_element_from_buffer may not have a
6040 chance to do that. */
6041 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6042 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6043 /* prev_stop can be zero, so check against BEGV as well. */
6044 while (it->bidi_it.charpos >= bob
6045 && it->prev_stop <= it->bidi_it.charpos
6046 && it->bidi_it.charpos < CHARPOS (it->position)
6047 && it->bidi_it.charpos < eob)
6048 bidi_move_to_visually_next (&it->bidi_it);
6049 /* Record the stop_pos we just crossed, for when we cross it
6050 back, maybe. */
6051 if (it->bidi_it.charpos > CHARPOS (it->position))
6052 it->prev_stop = CHARPOS (it->position);
6053 /* If we ended up not where pop_it put us, resync IT's
6054 positional members with the bidi iterator. */
6055 if (it->bidi_it.charpos != CHARPOS (it->position))
6056 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6057 if (buffer_p)
6058 it->current.pos = it->position;
6059 else
6060 it->current.string_pos = it->position;
6061 }
6062
6063 /* Restore IT's settings from IT->stack. Called, for example, when no
6064 more overlay strings must be processed, and we return to delivering
6065 display elements from a buffer, or when the end of a string from a
6066 `display' property is reached and we return to delivering display
6067 elements from an overlay string, or from a buffer. */
6068
6069 static void
6070 pop_it (struct it *it)
6071 {
6072 struct iterator_stack_entry *p;
6073 bool from_display_prop = it->from_disp_prop_p;
6074 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6075
6076 eassert (it->sp > 0);
6077 --it->sp;
6078 p = it->stack + it->sp;
6079 it->stop_charpos = p->stop_charpos;
6080 it->prev_stop = p->prev_stop;
6081 it->base_level_stop = p->base_level_stop;
6082 it->cmp_it = p->cmp_it;
6083 it->face_id = p->face_id;
6084 it->current = p->current;
6085 it->position = p->position;
6086 it->string = p->string;
6087 it->from_overlay = p->from_overlay;
6088 if (NILP (it->string))
6089 SET_TEXT_POS (it->current.string_pos, -1, -1);
6090 it->method = p->method;
6091 switch (it->method)
6092 {
6093 case GET_FROM_IMAGE:
6094 it->image_id = p->u.image.image_id;
6095 it->object = p->u.image.object;
6096 it->slice = p->u.image.slice;
6097 break;
6098 #ifdef HAVE_XWIDGETS
6099 case GET_FROM_XWIDGET:
6100 it->object = p->u.xwidget.object;
6101 break;
6102 #endif
6103 case GET_FROM_STRETCH:
6104 it->object = p->u.stretch.object;
6105 break;
6106 case GET_FROM_BUFFER:
6107 it->object = it->w->contents;
6108 break;
6109 case GET_FROM_STRING:
6110 {
6111 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6112
6113 /* Restore the face_box_p flag, since it could have been
6114 overwritten by the face of the object that we just finished
6115 displaying. */
6116 if (face)
6117 it->face_box_p = face->box != FACE_NO_BOX;
6118 it->object = it->string;
6119 }
6120 break;
6121 case GET_FROM_DISPLAY_VECTOR:
6122 if (it->s)
6123 it->method = GET_FROM_C_STRING;
6124 else if (STRINGP (it->string))
6125 it->method = GET_FROM_STRING;
6126 else
6127 {
6128 it->method = GET_FROM_BUFFER;
6129 it->object = it->w->contents;
6130 }
6131 break;
6132 case GET_FROM_C_STRING:
6133 break;
6134 default:
6135 emacs_abort ();
6136 }
6137 it->end_charpos = p->end_charpos;
6138 it->string_nchars = p->string_nchars;
6139 it->area = p->area;
6140 it->multibyte_p = p->multibyte_p;
6141 it->avoid_cursor_p = p->avoid_cursor_p;
6142 it->space_width = p->space_width;
6143 it->font_height = p->font_height;
6144 it->voffset = p->voffset;
6145 it->string_from_display_prop_p = p->string_from_display_prop_p;
6146 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6147 it->line_wrap = p->line_wrap;
6148 it->bidi_p = p->bidi_p;
6149 it->paragraph_embedding = p->paragraph_embedding;
6150 it->from_disp_prop_p = p->from_disp_prop_p;
6151 if (it->bidi_p)
6152 {
6153 bidi_pop_it (&it->bidi_it);
6154 /* Bidi-iterate until we get out of the portion of text, if any,
6155 covered by a `display' text property or by an overlay with
6156 `display' property. (We cannot just jump there, because the
6157 internal coherency of the bidi iterator state can not be
6158 preserved across such jumps.) We also must determine the
6159 paragraph base direction if the overlay we just processed is
6160 at the beginning of a new paragraph. */
6161 if (from_display_prop
6162 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6163 iterate_out_of_display_property (it);
6164
6165 eassert ((BUFFERP (it->object)
6166 && IT_CHARPOS (*it) == it->bidi_it.charpos
6167 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6168 || (STRINGP (it->object)
6169 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6170 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6171 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6172 }
6173 /* If we move the iterator over text covered by a display property
6174 to a new buffer position, any info about previously seen overlays
6175 is no longer valid. */
6176 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6177 it->ignore_overlay_strings_at_pos_p = false;
6178 }
6179
6180
6181 \f
6182 /***********************************************************************
6183 Moving over lines
6184 ***********************************************************************/
6185
6186 /* Set IT's current position to the previous line start. */
6187
6188 static void
6189 back_to_previous_line_start (struct it *it)
6190 {
6191 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6192
6193 DEC_BOTH (cp, bp);
6194 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6195 }
6196
6197
6198 /* Move IT to the next line start.
6199
6200 Value is true if a newline was found. Set *SKIPPED_P to true if
6201 we skipped over part of the text (as opposed to moving the iterator
6202 continuously over the text). Otherwise, don't change the value
6203 of *SKIPPED_P.
6204
6205 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6206 iterator on the newline, if it was found.
6207
6208 Newlines may come from buffer text, overlay strings, or strings
6209 displayed via the `display' property. That's the reason we can't
6210 simply use find_newline_no_quit.
6211
6212 Note that this function may not skip over invisible text that is so
6213 because of text properties and immediately follows a newline. If
6214 it would, function reseat_at_next_visible_line_start, when called
6215 from set_iterator_to_next, would effectively make invisible
6216 characters following a newline part of the wrong glyph row, which
6217 leads to wrong cursor motion. */
6218
6219 static bool
6220 forward_to_next_line_start (struct it *it, bool *skipped_p,
6221 struct bidi_it *bidi_it_prev)
6222 {
6223 ptrdiff_t old_selective;
6224 bool newline_found_p = false;
6225 int n;
6226 const int MAX_NEWLINE_DISTANCE = 500;
6227
6228 /* If already on a newline, just consume it to avoid unintended
6229 skipping over invisible text below. */
6230 if (it->what == IT_CHARACTER
6231 && it->c == '\n'
6232 && CHARPOS (it->position) == IT_CHARPOS (*it))
6233 {
6234 if (it->bidi_p && bidi_it_prev)
6235 *bidi_it_prev = it->bidi_it;
6236 set_iterator_to_next (it, false);
6237 it->c = 0;
6238 return true;
6239 }
6240
6241 /* Don't handle selective display in the following. It's (a)
6242 unnecessary because it's done by the caller, and (b) leads to an
6243 infinite recursion because next_element_from_ellipsis indirectly
6244 calls this function. */
6245 old_selective = it->selective;
6246 it->selective = 0;
6247
6248 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6249 from buffer text. */
6250 for (n = 0;
6251 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6252 n += !STRINGP (it->string))
6253 {
6254 if (!get_next_display_element (it))
6255 return false;
6256 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6257 if (newline_found_p && it->bidi_p && bidi_it_prev)
6258 *bidi_it_prev = it->bidi_it;
6259 set_iterator_to_next (it, false);
6260 }
6261
6262 /* If we didn't find a newline near enough, see if we can use a
6263 short-cut. */
6264 if (!newline_found_p)
6265 {
6266 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6267 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6268 1, &bytepos);
6269 Lisp_Object pos;
6270
6271 eassert (!STRINGP (it->string));
6272
6273 /* If there isn't any `display' property in sight, and no
6274 overlays, we can just use the position of the newline in
6275 buffer text. */
6276 if (it->stop_charpos >= limit
6277 || ((pos = Fnext_single_property_change (make_number (start),
6278 Qdisplay, Qnil,
6279 make_number (limit)),
6280 NILP (pos))
6281 && next_overlay_change (start) == ZV))
6282 {
6283 if (!it->bidi_p)
6284 {
6285 IT_CHARPOS (*it) = limit;
6286 IT_BYTEPOS (*it) = bytepos;
6287 }
6288 else
6289 {
6290 struct bidi_it bprev;
6291
6292 /* Help bidi.c avoid expensive searches for display
6293 properties and overlays, by telling it that there are
6294 none up to `limit'. */
6295 if (it->bidi_it.disp_pos < limit)
6296 {
6297 it->bidi_it.disp_pos = limit;
6298 it->bidi_it.disp_prop = 0;
6299 }
6300 do {
6301 bprev = it->bidi_it;
6302 bidi_move_to_visually_next (&it->bidi_it);
6303 } while (it->bidi_it.charpos != limit);
6304 IT_CHARPOS (*it) = limit;
6305 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6306 if (bidi_it_prev)
6307 *bidi_it_prev = bprev;
6308 }
6309 *skipped_p = newline_found_p = true;
6310 }
6311 else
6312 {
6313 while (get_next_display_element (it)
6314 && !newline_found_p)
6315 {
6316 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6317 if (newline_found_p && it->bidi_p && bidi_it_prev)
6318 *bidi_it_prev = it->bidi_it;
6319 set_iterator_to_next (it, false);
6320 }
6321 }
6322 }
6323
6324 it->selective = old_selective;
6325 return newline_found_p;
6326 }
6327
6328
6329 /* Set IT's current position to the previous visible line start. Skip
6330 invisible text that is so either due to text properties or due to
6331 selective display. Caution: this does not change IT->current_x and
6332 IT->hpos. */
6333
6334 static void
6335 back_to_previous_visible_line_start (struct it *it)
6336 {
6337 while (IT_CHARPOS (*it) > BEGV)
6338 {
6339 back_to_previous_line_start (it);
6340
6341 if (IT_CHARPOS (*it) <= BEGV)
6342 break;
6343
6344 /* If selective > 0, then lines indented more than its value are
6345 invisible. */
6346 if (it->selective > 0
6347 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6348 it->selective))
6349 continue;
6350
6351 /* Check the newline before point for invisibility. */
6352 {
6353 Lisp_Object prop;
6354 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6355 Qinvisible, it->window);
6356 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6357 continue;
6358 }
6359
6360 if (IT_CHARPOS (*it) <= BEGV)
6361 break;
6362
6363 {
6364 struct it it2;
6365 void *it2data = NULL;
6366 ptrdiff_t pos;
6367 ptrdiff_t beg, end;
6368 Lisp_Object val, overlay;
6369
6370 SAVE_IT (it2, *it, it2data);
6371
6372 /* If newline is part of a composition, continue from start of composition */
6373 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6374 && beg < IT_CHARPOS (*it))
6375 goto replaced;
6376
6377 /* If newline is replaced by a display property, find start of overlay
6378 or interval and continue search from that point. */
6379 pos = --IT_CHARPOS (it2);
6380 --IT_BYTEPOS (it2);
6381 it2.sp = 0;
6382 bidi_unshelve_cache (NULL, false);
6383 it2.string_from_display_prop_p = false;
6384 it2.from_disp_prop_p = false;
6385 if (handle_display_prop (&it2) == HANDLED_RETURN
6386 && !NILP (val = get_char_property_and_overlay
6387 (make_number (pos), Qdisplay, Qnil, &overlay))
6388 && (OVERLAYP (overlay)
6389 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6390 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6391 {
6392 RESTORE_IT (it, it, it2data);
6393 goto replaced;
6394 }
6395
6396 /* Newline is not replaced by anything -- so we are done. */
6397 RESTORE_IT (it, it, it2data);
6398 break;
6399
6400 replaced:
6401 if (beg < BEGV)
6402 beg = BEGV;
6403 IT_CHARPOS (*it) = beg;
6404 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6405 }
6406 }
6407
6408 it->continuation_lines_width = 0;
6409
6410 eassert (IT_CHARPOS (*it) >= BEGV);
6411 eassert (IT_CHARPOS (*it) == BEGV
6412 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6413 CHECK_IT (it);
6414 }
6415
6416
6417 /* Reseat iterator IT at the previous visible line start. Skip
6418 invisible text that is so either due to text properties or due to
6419 selective display. At the end, update IT's overlay information,
6420 face information etc. */
6421
6422 void
6423 reseat_at_previous_visible_line_start (struct it *it)
6424 {
6425 back_to_previous_visible_line_start (it);
6426 reseat (it, it->current.pos, true);
6427 CHECK_IT (it);
6428 }
6429
6430
6431 /* Reseat iterator IT on the next visible line start in the current
6432 buffer. ON_NEWLINE_P means position IT on the newline
6433 preceding the line start. Skip over invisible text that is so
6434 because of selective display. Compute faces, overlays etc at the
6435 new position. Note that this function does not skip over text that
6436 is invisible because of text properties. */
6437
6438 static void
6439 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6440 {
6441 bool skipped_p = false;
6442 struct bidi_it bidi_it_prev;
6443 bool newline_found_p
6444 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6445
6446 /* Skip over lines that are invisible because they are indented
6447 more than the value of IT->selective. */
6448 if (it->selective > 0)
6449 while (IT_CHARPOS (*it) < ZV
6450 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6451 it->selective))
6452 {
6453 eassert (IT_BYTEPOS (*it) == BEGV
6454 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6455 newline_found_p =
6456 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6457 }
6458
6459 /* Position on the newline if that's what's requested. */
6460 if (on_newline_p && newline_found_p)
6461 {
6462 if (STRINGP (it->string))
6463 {
6464 if (IT_STRING_CHARPOS (*it) > 0)
6465 {
6466 if (!it->bidi_p)
6467 {
6468 --IT_STRING_CHARPOS (*it);
6469 --IT_STRING_BYTEPOS (*it);
6470 }
6471 else
6472 {
6473 /* We need to restore the bidi iterator to the state
6474 it had on the newline, and resync the IT's
6475 position with that. */
6476 it->bidi_it = bidi_it_prev;
6477 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6478 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6479 }
6480 }
6481 }
6482 else if (IT_CHARPOS (*it) > BEGV)
6483 {
6484 if (!it->bidi_p)
6485 {
6486 --IT_CHARPOS (*it);
6487 --IT_BYTEPOS (*it);
6488 }
6489 else
6490 {
6491 /* We need to restore the bidi iterator to the state it
6492 had on the newline and resync IT with that. */
6493 it->bidi_it = bidi_it_prev;
6494 IT_CHARPOS (*it) = it->bidi_it.charpos;
6495 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6496 }
6497 reseat (it, it->current.pos, false);
6498 }
6499 }
6500 else if (skipped_p)
6501 reseat (it, it->current.pos, false);
6502
6503 CHECK_IT (it);
6504 }
6505
6506
6507 \f
6508 /***********************************************************************
6509 Changing an iterator's position
6510 ***********************************************************************/
6511
6512 /* Change IT's current position to POS in current_buffer.
6513 If FORCE_P, always check for text properties at the new position.
6514 Otherwise, text properties are only looked up if POS >=
6515 IT->check_charpos of a property. */
6516
6517 static void
6518 reseat (struct it *it, struct text_pos pos, bool force_p)
6519 {
6520 ptrdiff_t original_pos = IT_CHARPOS (*it);
6521
6522 reseat_1 (it, pos, false);
6523
6524 /* Determine where to check text properties. Avoid doing it
6525 where possible because text property lookup is very expensive. */
6526 if (force_p
6527 || CHARPOS (pos) > it->stop_charpos
6528 || CHARPOS (pos) < original_pos)
6529 {
6530 if (it->bidi_p)
6531 {
6532 /* For bidi iteration, we need to prime prev_stop and
6533 base_level_stop with our best estimations. */
6534 /* Implementation note: Of course, POS is not necessarily a
6535 stop position, so assigning prev_pos to it is a lie; we
6536 should have called compute_stop_backwards. However, if
6537 the current buffer does not include any R2L characters,
6538 that call would be a waste of cycles, because the
6539 iterator will never move back, and thus never cross this
6540 "fake" stop position. So we delay that backward search
6541 until the time we really need it, in next_element_from_buffer. */
6542 if (CHARPOS (pos) != it->prev_stop)
6543 it->prev_stop = CHARPOS (pos);
6544 if (CHARPOS (pos) < it->base_level_stop)
6545 it->base_level_stop = 0; /* meaning it's unknown */
6546 handle_stop (it);
6547 }
6548 else
6549 {
6550 handle_stop (it);
6551 it->prev_stop = it->base_level_stop = 0;
6552 }
6553
6554 }
6555
6556 CHECK_IT (it);
6557 }
6558
6559
6560 /* Change IT's buffer position to POS. SET_STOP_P means set
6561 IT->stop_pos to POS, also. */
6562
6563 static void
6564 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6565 {
6566 /* Don't call this function when scanning a C string. */
6567 eassert (it->s == NULL);
6568
6569 /* POS must be a reasonable value. */
6570 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6571
6572 it->current.pos = it->position = pos;
6573 it->end_charpos = ZV;
6574 it->dpvec = NULL;
6575 it->current.dpvec_index = -1;
6576 it->current.overlay_string_index = -1;
6577 IT_STRING_CHARPOS (*it) = -1;
6578 IT_STRING_BYTEPOS (*it) = -1;
6579 it->string = Qnil;
6580 it->method = GET_FROM_BUFFER;
6581 it->object = it->w->contents;
6582 it->area = TEXT_AREA;
6583 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6584 it->sp = 0;
6585 it->string_from_display_prop_p = false;
6586 it->string_from_prefix_prop_p = false;
6587
6588 it->from_disp_prop_p = false;
6589 it->face_before_selective_p = false;
6590 if (it->bidi_p)
6591 {
6592 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6593 &it->bidi_it);
6594 bidi_unshelve_cache (NULL, false);
6595 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6596 it->bidi_it.string.s = NULL;
6597 it->bidi_it.string.lstring = Qnil;
6598 it->bidi_it.string.bufpos = 0;
6599 it->bidi_it.string.from_disp_str = false;
6600 it->bidi_it.string.unibyte = false;
6601 it->bidi_it.w = it->w;
6602 }
6603
6604 if (set_stop_p)
6605 {
6606 it->stop_charpos = CHARPOS (pos);
6607 it->base_level_stop = CHARPOS (pos);
6608 }
6609 /* This make the information stored in it->cmp_it invalidate. */
6610 it->cmp_it.id = -1;
6611 }
6612
6613
6614 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6615 If S is non-null, it is a C string to iterate over. Otherwise,
6616 STRING gives a Lisp string to iterate over.
6617
6618 If PRECISION > 0, don't return more then PRECISION number of
6619 characters from the string.
6620
6621 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6622 characters have been returned. FIELD_WIDTH < 0 means an infinite
6623 field width.
6624
6625 MULTIBYTE = 0 means disable processing of multibyte characters,
6626 MULTIBYTE > 0 means enable it,
6627 MULTIBYTE < 0 means use IT->multibyte_p.
6628
6629 IT must be initialized via a prior call to init_iterator before
6630 calling this function. */
6631
6632 static void
6633 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6634 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6635 int multibyte)
6636 {
6637 /* No text property checks performed by default, but see below. */
6638 it->stop_charpos = -1;
6639
6640 /* Set iterator position and end position. */
6641 memset (&it->current, 0, sizeof it->current);
6642 it->current.overlay_string_index = -1;
6643 it->current.dpvec_index = -1;
6644 eassert (charpos >= 0);
6645
6646 /* If STRING is specified, use its multibyteness, otherwise use the
6647 setting of MULTIBYTE, if specified. */
6648 if (multibyte >= 0)
6649 it->multibyte_p = multibyte > 0;
6650
6651 /* Bidirectional reordering of strings is controlled by the default
6652 value of bidi-display-reordering. Don't try to reorder while
6653 loading loadup.el, as the necessary character property tables are
6654 not yet available. */
6655 it->bidi_p =
6656 NILP (Vpurify_flag)
6657 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6658
6659 if (s == NULL)
6660 {
6661 eassert (STRINGP (string));
6662 it->string = string;
6663 it->s = NULL;
6664 it->end_charpos = it->string_nchars = SCHARS (string);
6665 it->method = GET_FROM_STRING;
6666 it->current.string_pos = string_pos (charpos, string);
6667
6668 if (it->bidi_p)
6669 {
6670 it->bidi_it.string.lstring = string;
6671 it->bidi_it.string.s = NULL;
6672 it->bidi_it.string.schars = it->end_charpos;
6673 it->bidi_it.string.bufpos = 0;
6674 it->bidi_it.string.from_disp_str = false;
6675 it->bidi_it.string.unibyte = !it->multibyte_p;
6676 it->bidi_it.w = it->w;
6677 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6678 FRAME_WINDOW_P (it->f), &it->bidi_it);
6679 }
6680 }
6681 else
6682 {
6683 it->s = (const unsigned char *) s;
6684 it->string = Qnil;
6685
6686 /* Note that we use IT->current.pos, not it->current.string_pos,
6687 for displaying C strings. */
6688 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6689 if (it->multibyte_p)
6690 {
6691 it->current.pos = c_string_pos (charpos, s, true);
6692 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6693 }
6694 else
6695 {
6696 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6697 it->end_charpos = it->string_nchars = strlen (s);
6698 }
6699
6700 if (it->bidi_p)
6701 {
6702 it->bidi_it.string.lstring = Qnil;
6703 it->bidi_it.string.s = (const unsigned char *) s;
6704 it->bidi_it.string.schars = it->end_charpos;
6705 it->bidi_it.string.bufpos = 0;
6706 it->bidi_it.string.from_disp_str = false;
6707 it->bidi_it.string.unibyte = !it->multibyte_p;
6708 it->bidi_it.w = it->w;
6709 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6710 &it->bidi_it);
6711 }
6712 it->method = GET_FROM_C_STRING;
6713 }
6714
6715 /* PRECISION > 0 means don't return more than PRECISION characters
6716 from the string. */
6717 if (precision > 0 && it->end_charpos - charpos > precision)
6718 {
6719 it->end_charpos = it->string_nchars = charpos + precision;
6720 if (it->bidi_p)
6721 it->bidi_it.string.schars = it->end_charpos;
6722 }
6723
6724 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6725 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6726 FIELD_WIDTH < 0 means infinite field width. This is useful for
6727 padding with `-' at the end of a mode line. */
6728 if (field_width < 0)
6729 field_width = INFINITY;
6730 /* Implementation note: We deliberately don't enlarge
6731 it->bidi_it.string.schars here to fit it->end_charpos, because
6732 the bidi iterator cannot produce characters out of thin air. */
6733 if (field_width > it->end_charpos - charpos)
6734 it->end_charpos = charpos + field_width;
6735
6736 /* Use the standard display table for displaying strings. */
6737 if (DISP_TABLE_P (Vstandard_display_table))
6738 it->dp = XCHAR_TABLE (Vstandard_display_table);
6739
6740 it->stop_charpos = charpos;
6741 it->prev_stop = charpos;
6742 it->base_level_stop = 0;
6743 if (it->bidi_p)
6744 {
6745 it->bidi_it.first_elt = true;
6746 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6747 it->bidi_it.disp_pos = -1;
6748 }
6749 if (s == NULL && it->multibyte_p)
6750 {
6751 ptrdiff_t endpos = SCHARS (it->string);
6752 if (endpos > it->end_charpos)
6753 endpos = it->end_charpos;
6754 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6755 it->string);
6756 }
6757 CHECK_IT (it);
6758 }
6759
6760
6761 \f
6762 /***********************************************************************
6763 Iteration
6764 ***********************************************************************/
6765
6766 /* Map enum it_method value to corresponding next_element_from_* function. */
6767
6768 typedef bool (*next_element_function) (struct it *);
6769
6770 static next_element_function const get_next_element[NUM_IT_METHODS] =
6771 {
6772 next_element_from_buffer,
6773 next_element_from_display_vector,
6774 next_element_from_string,
6775 next_element_from_c_string,
6776 next_element_from_image,
6777 next_element_from_stretch,
6778 #ifdef HAVE_XWIDGETS
6779 next_element_from_xwidget,
6780 #endif
6781 };
6782
6783 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6784
6785
6786 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6787 (possibly with the following characters). */
6788
6789 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6790 ((IT)->cmp_it.id >= 0 \
6791 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6792 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6793 END_CHARPOS, (IT)->w, \
6794 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6795 (IT)->string)))
6796
6797
6798 /* Lookup the char-table Vglyphless_char_display for character C (-1
6799 if we want information for no-font case), and return the display
6800 method symbol. By side-effect, update it->what and
6801 it->glyphless_method. This function is called from
6802 get_next_display_element for each character element, and from
6803 x_produce_glyphs when no suitable font was found. */
6804
6805 Lisp_Object
6806 lookup_glyphless_char_display (int c, struct it *it)
6807 {
6808 Lisp_Object glyphless_method = Qnil;
6809
6810 if (CHAR_TABLE_P (Vglyphless_char_display)
6811 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6812 {
6813 if (c >= 0)
6814 {
6815 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6816 if (CONSP (glyphless_method))
6817 glyphless_method = FRAME_WINDOW_P (it->f)
6818 ? XCAR (glyphless_method)
6819 : XCDR (glyphless_method);
6820 }
6821 else
6822 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6823 }
6824
6825 retry:
6826 if (NILP (glyphless_method))
6827 {
6828 if (c >= 0)
6829 /* The default is to display the character by a proper font. */
6830 return Qnil;
6831 /* The default for the no-font case is to display an empty box. */
6832 glyphless_method = Qempty_box;
6833 }
6834 if (EQ (glyphless_method, Qzero_width))
6835 {
6836 if (c >= 0)
6837 return glyphless_method;
6838 /* This method can't be used for the no-font case. */
6839 glyphless_method = Qempty_box;
6840 }
6841 if (EQ (glyphless_method, Qthin_space))
6842 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6843 else if (EQ (glyphless_method, Qempty_box))
6844 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6845 else if (EQ (glyphless_method, Qhex_code))
6846 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6847 else if (STRINGP (glyphless_method))
6848 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6849 else
6850 {
6851 /* Invalid value. We use the default method. */
6852 glyphless_method = Qnil;
6853 goto retry;
6854 }
6855 it->what = IT_GLYPHLESS;
6856 return glyphless_method;
6857 }
6858
6859 /* Merge escape glyph face and cache the result. */
6860
6861 static struct frame *last_escape_glyph_frame = NULL;
6862 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6863 static int last_escape_glyph_merged_face_id = 0;
6864
6865 static int
6866 merge_escape_glyph_face (struct it *it)
6867 {
6868 int face_id;
6869
6870 if (it->f == last_escape_glyph_frame
6871 && it->face_id == last_escape_glyph_face_id)
6872 face_id = last_escape_glyph_merged_face_id;
6873 else
6874 {
6875 /* Merge the `escape-glyph' face into the current face. */
6876 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6877 last_escape_glyph_frame = it->f;
6878 last_escape_glyph_face_id = it->face_id;
6879 last_escape_glyph_merged_face_id = face_id;
6880 }
6881 return face_id;
6882 }
6883
6884 /* Likewise for glyphless glyph face. */
6885
6886 static struct frame *last_glyphless_glyph_frame = NULL;
6887 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6888 static int last_glyphless_glyph_merged_face_id = 0;
6889
6890 int
6891 merge_glyphless_glyph_face (struct it *it)
6892 {
6893 int face_id;
6894
6895 if (it->f == last_glyphless_glyph_frame
6896 && it->face_id == last_glyphless_glyph_face_id)
6897 face_id = last_glyphless_glyph_merged_face_id;
6898 else
6899 {
6900 /* Merge the `glyphless-char' face into the current face. */
6901 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6902 last_glyphless_glyph_frame = it->f;
6903 last_glyphless_glyph_face_id = it->face_id;
6904 last_glyphless_glyph_merged_face_id = face_id;
6905 }
6906 return face_id;
6907 }
6908
6909 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6910 be called before redisplaying windows, and when the frame's face
6911 cache is freed. */
6912 void
6913 forget_escape_and_glyphless_faces (void)
6914 {
6915 last_escape_glyph_frame = NULL;
6916 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6917 last_glyphless_glyph_frame = NULL;
6918 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6919 }
6920
6921 /* Load IT's display element fields with information about the next
6922 display element from the current position of IT. Value is false if
6923 end of buffer (or C string) is reached. */
6924
6925 static bool
6926 get_next_display_element (struct it *it)
6927 {
6928 /* True means that we found a display element. False means that
6929 we hit the end of what we iterate over. Performance note: the
6930 function pointer `method' used here turns out to be faster than
6931 using a sequence of if-statements. */
6932 bool success_p;
6933
6934 get_next:
6935 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6936
6937 if (it->what == IT_CHARACTER)
6938 {
6939 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6940 and only if (a) the resolved directionality of that character
6941 is R..." */
6942 /* FIXME: Do we need an exception for characters from display
6943 tables? */
6944 if (it->bidi_p && it->bidi_it.type == STRONG_R
6945 && !inhibit_bidi_mirroring)
6946 it->c = bidi_mirror_char (it->c);
6947 /* Map via display table or translate control characters.
6948 IT->c, IT->len etc. have been set to the next character by
6949 the function call above. If we have a display table, and it
6950 contains an entry for IT->c, translate it. Don't do this if
6951 IT->c itself comes from a display table, otherwise we could
6952 end up in an infinite recursion. (An alternative could be to
6953 count the recursion depth of this function and signal an
6954 error when a certain maximum depth is reached.) Is it worth
6955 it? */
6956 if (success_p && it->dpvec == NULL)
6957 {
6958 Lisp_Object dv;
6959 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6960 bool nonascii_space_p = false;
6961 bool nonascii_hyphen_p = false;
6962 int c = it->c; /* This is the character to display. */
6963
6964 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6965 {
6966 eassert (SINGLE_BYTE_CHAR_P (c));
6967 if (unibyte_display_via_language_environment)
6968 {
6969 c = DECODE_CHAR (unibyte, c);
6970 if (c < 0)
6971 c = BYTE8_TO_CHAR (it->c);
6972 }
6973 else
6974 c = BYTE8_TO_CHAR (it->c);
6975 }
6976
6977 if (it->dp
6978 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6979 VECTORP (dv)))
6980 {
6981 struct Lisp_Vector *v = XVECTOR (dv);
6982
6983 /* Return the first character from the display table
6984 entry, if not empty. If empty, don't display the
6985 current character. */
6986 if (v->header.size)
6987 {
6988 it->dpvec_char_len = it->len;
6989 it->dpvec = v->contents;
6990 it->dpend = v->contents + v->header.size;
6991 it->current.dpvec_index = 0;
6992 it->dpvec_face_id = -1;
6993 it->saved_face_id = it->face_id;
6994 it->method = GET_FROM_DISPLAY_VECTOR;
6995 it->ellipsis_p = false;
6996 }
6997 else
6998 {
6999 set_iterator_to_next (it, false);
7000 }
7001 goto get_next;
7002 }
7003
7004 if (! NILP (lookup_glyphless_char_display (c, it)))
7005 {
7006 if (it->what == IT_GLYPHLESS)
7007 goto done;
7008 /* Don't display this character. */
7009 set_iterator_to_next (it, false);
7010 goto get_next;
7011 }
7012
7013 /* If `nobreak-char-display' is non-nil, we display
7014 non-ASCII spaces and hyphens specially. */
7015 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7016 {
7017 if (c == NO_BREAK_SPACE)
7018 nonascii_space_p = true;
7019 else if (c == SOFT_HYPHEN || c == HYPHEN
7020 || c == NON_BREAKING_HYPHEN)
7021 nonascii_hyphen_p = true;
7022 }
7023
7024 /* Translate control characters into `\003' or `^C' form.
7025 Control characters coming from a display table entry are
7026 currently not translated because we use IT->dpvec to hold
7027 the translation. This could easily be changed but I
7028 don't believe that it is worth doing.
7029
7030 The characters handled by `nobreak-char-display' must be
7031 translated too.
7032
7033 Non-printable characters and raw-byte characters are also
7034 translated to octal form. */
7035 if (((c < ' ' || c == 127) /* ASCII control chars. */
7036 ? (it->area != TEXT_AREA
7037 /* In mode line, treat \n, \t like other crl chars. */
7038 || (c != '\t'
7039 && it->glyph_row
7040 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7041 || (c != '\n' && c != '\t'))
7042 : (nonascii_space_p
7043 || nonascii_hyphen_p
7044 || CHAR_BYTE8_P (c)
7045 || ! CHAR_PRINTABLE_P (c))))
7046 {
7047 /* C is a control character, non-ASCII space/hyphen,
7048 raw-byte, or a non-printable character which must be
7049 displayed either as '\003' or as `^C' where the '\\'
7050 and '^' can be defined in the display table. Fill
7051 IT->ctl_chars with glyphs for what we have to
7052 display. Then, set IT->dpvec to these glyphs. */
7053 Lisp_Object gc;
7054 int ctl_len;
7055 int face_id;
7056 int lface_id = 0;
7057 int escape_glyph;
7058
7059 /* Handle control characters with ^. */
7060
7061 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7062 {
7063 int g;
7064
7065 g = '^'; /* default glyph for Control */
7066 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7067 if (it->dp
7068 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7069 {
7070 g = GLYPH_CODE_CHAR (gc);
7071 lface_id = GLYPH_CODE_FACE (gc);
7072 }
7073
7074 face_id = (lface_id
7075 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7076 : merge_escape_glyph_face (it));
7077
7078 XSETINT (it->ctl_chars[0], g);
7079 XSETINT (it->ctl_chars[1], c ^ 0100);
7080 ctl_len = 2;
7081 goto display_control;
7082 }
7083
7084 /* Handle non-ascii space in the mode where it only gets
7085 highlighting. */
7086
7087 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7088 {
7089 /* Merge `nobreak-space' into the current face. */
7090 face_id = merge_faces (it->f, Qnobreak_space, 0,
7091 it->face_id);
7092 XSETINT (it->ctl_chars[0], ' ');
7093 ctl_len = 1;
7094 goto display_control;
7095 }
7096
7097 /* Handle sequences that start with the "escape glyph". */
7098
7099 /* the default escape glyph is \. */
7100 escape_glyph = '\\';
7101
7102 if (it->dp
7103 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7104 {
7105 escape_glyph = GLYPH_CODE_CHAR (gc);
7106 lface_id = GLYPH_CODE_FACE (gc);
7107 }
7108
7109 face_id = (lface_id
7110 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7111 : merge_escape_glyph_face (it));
7112
7113 /* Draw non-ASCII hyphen with just highlighting: */
7114
7115 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7116 {
7117 XSETINT (it->ctl_chars[0], '-');
7118 ctl_len = 1;
7119 goto display_control;
7120 }
7121
7122 /* Draw non-ASCII space/hyphen with escape glyph: */
7123
7124 if (nonascii_space_p || nonascii_hyphen_p)
7125 {
7126 XSETINT (it->ctl_chars[0], escape_glyph);
7127 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7128 ctl_len = 2;
7129 goto display_control;
7130 }
7131
7132 {
7133 char str[10];
7134 int len, i;
7135
7136 if (CHAR_BYTE8_P (c))
7137 /* Display \200 instead of \17777600. */
7138 c = CHAR_TO_BYTE8 (c);
7139 len = sprintf (str, "%03o", c + 0u);
7140
7141 XSETINT (it->ctl_chars[0], escape_glyph);
7142 for (i = 0; i < len; i++)
7143 XSETINT (it->ctl_chars[i + 1], str[i]);
7144 ctl_len = len + 1;
7145 }
7146
7147 display_control:
7148 /* Set up IT->dpvec and return first character from it. */
7149 it->dpvec_char_len = it->len;
7150 it->dpvec = it->ctl_chars;
7151 it->dpend = it->dpvec + ctl_len;
7152 it->current.dpvec_index = 0;
7153 it->dpvec_face_id = face_id;
7154 it->saved_face_id = it->face_id;
7155 it->method = GET_FROM_DISPLAY_VECTOR;
7156 it->ellipsis_p = false;
7157 goto get_next;
7158 }
7159 it->char_to_display = c;
7160 }
7161 else if (success_p)
7162 {
7163 it->char_to_display = it->c;
7164 }
7165 }
7166
7167 #ifdef HAVE_WINDOW_SYSTEM
7168 /* Adjust face id for a multibyte character. There are no multibyte
7169 character in unibyte text. */
7170 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7171 && it->multibyte_p
7172 && success_p
7173 && FRAME_WINDOW_P (it->f))
7174 {
7175 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7176
7177 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7178 {
7179 /* Automatic composition with glyph-string. */
7180 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7181
7182 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7183 }
7184 else
7185 {
7186 ptrdiff_t pos = (it->s ? -1
7187 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7188 : IT_CHARPOS (*it));
7189 int c;
7190
7191 if (it->what == IT_CHARACTER)
7192 c = it->char_to_display;
7193 else
7194 {
7195 struct composition *cmp = composition_table[it->cmp_it.id];
7196 int i;
7197
7198 c = ' ';
7199 for (i = 0; i < cmp->glyph_len; i++)
7200 /* TAB in a composition means display glyphs with
7201 padding space on the left or right. */
7202 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7203 break;
7204 }
7205 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7206 }
7207 }
7208 #endif /* HAVE_WINDOW_SYSTEM */
7209
7210 done:
7211 /* Is this character the last one of a run of characters with
7212 box? If yes, set IT->end_of_box_run_p to true. */
7213 if (it->face_box_p
7214 && it->s == NULL)
7215 {
7216 if (it->method == GET_FROM_STRING && it->sp)
7217 {
7218 int face_id = underlying_face_id (it);
7219 struct face *face = FACE_FROM_ID (it->f, face_id);
7220
7221 if (face)
7222 {
7223 if (face->box == FACE_NO_BOX)
7224 {
7225 /* If the box comes from face properties in a
7226 display string, check faces in that string. */
7227 int string_face_id = face_after_it_pos (it);
7228 it->end_of_box_run_p
7229 = (FACE_FROM_ID (it->f, string_face_id)->box
7230 == FACE_NO_BOX);
7231 }
7232 /* Otherwise, the box comes from the underlying face.
7233 If this is the last string character displayed, check
7234 the next buffer location. */
7235 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7236 /* n_overlay_strings is unreliable unless
7237 overlay_string_index is non-negative. */
7238 && ((it->current.overlay_string_index >= 0
7239 && (it->current.overlay_string_index
7240 == it->n_overlay_strings - 1))
7241 /* A string from display property. */
7242 || it->from_disp_prop_p))
7243 {
7244 ptrdiff_t ignore;
7245 int next_face_id;
7246 struct text_pos pos = it->current.pos;
7247
7248 /* For a string from a display property, the next
7249 buffer position is stored in the 'position'
7250 member of the iteration stack slot below the
7251 current one, see handle_single_display_spec. By
7252 contrast, it->current.pos was is not yet updated
7253 to point to that buffer position; that will
7254 happen in pop_it, after we finish displaying the
7255 current string. Note that we already checked
7256 above that it->sp is positive, so subtracting one
7257 from it is safe. */
7258 if (it->from_disp_prop_p)
7259 pos = (it->stack + it->sp - 1)->position;
7260 else
7261 INC_TEXT_POS (pos, it->multibyte_p);
7262
7263 if (CHARPOS (pos) >= ZV)
7264 it->end_of_box_run_p = true;
7265 else
7266 {
7267 next_face_id = face_at_buffer_position
7268 (it->w, CHARPOS (pos), &ignore,
7269 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, false, -1);
7270 it->end_of_box_run_p
7271 = (FACE_FROM_ID (it->f, next_face_id)->box
7272 == FACE_NO_BOX);
7273 }
7274 }
7275 }
7276 }
7277 /* next_element_from_display_vector sets this flag according to
7278 faces of the display vector glyphs, see there. */
7279 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7280 {
7281 int face_id = face_after_it_pos (it);
7282 it->end_of_box_run_p
7283 = (face_id != it->face_id
7284 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7285 }
7286 }
7287 /* If we reached the end of the object we've been iterating (e.g., a
7288 display string or an overlay string), and there's something on
7289 IT->stack, proceed with what's on the stack. It doesn't make
7290 sense to return false if there's unprocessed stuff on the stack,
7291 because otherwise that stuff will never be displayed. */
7292 if (!success_p && it->sp > 0)
7293 {
7294 set_iterator_to_next (it, false);
7295 success_p = get_next_display_element (it);
7296 }
7297
7298 /* Value is false if end of buffer or string reached. */
7299 return success_p;
7300 }
7301
7302
7303 /* Move IT to the next display element.
7304
7305 RESEAT_P means if called on a newline in buffer text,
7306 skip to the next visible line start.
7307
7308 Functions get_next_display_element and set_iterator_to_next are
7309 separate because I find this arrangement easier to handle than a
7310 get_next_display_element function that also increments IT's
7311 position. The way it is we can first look at an iterator's current
7312 display element, decide whether it fits on a line, and if it does,
7313 increment the iterator position. The other way around we probably
7314 would either need a flag indicating whether the iterator has to be
7315 incremented the next time, or we would have to implement a
7316 decrement position function which would not be easy to write. */
7317
7318 void
7319 set_iterator_to_next (struct it *it, bool reseat_p)
7320 {
7321 /* Reset flags indicating start and end of a sequence of characters
7322 with box. Reset them at the start of this function because
7323 moving the iterator to a new position might set them. */
7324 it->start_of_box_run_p = it->end_of_box_run_p = false;
7325
7326 switch (it->method)
7327 {
7328 case GET_FROM_BUFFER:
7329 /* The current display element of IT is a character from
7330 current_buffer. Advance in the buffer, and maybe skip over
7331 invisible lines that are so because of selective display. */
7332 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7333 reseat_at_next_visible_line_start (it, false);
7334 else if (it->cmp_it.id >= 0)
7335 {
7336 /* We are currently getting glyphs from a composition. */
7337 if (! it->bidi_p)
7338 {
7339 IT_CHARPOS (*it) += it->cmp_it.nchars;
7340 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7341 }
7342 else
7343 {
7344 int i;
7345
7346 /* Update IT's char/byte positions to point to the first
7347 character of the next grapheme cluster, or to the
7348 character visually after the current composition. */
7349 for (i = 0; i < it->cmp_it.nchars; i++)
7350 bidi_move_to_visually_next (&it->bidi_it);
7351 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7352 IT_CHARPOS (*it) = it->bidi_it.charpos;
7353 }
7354
7355 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7356 && it->cmp_it.to < it->cmp_it.nglyphs)
7357 {
7358 /* Composition created while scanning forward. Proceed
7359 to the next grapheme cluster. */
7360 it->cmp_it.from = it->cmp_it.to;
7361 }
7362 else if ((it->bidi_p && it->cmp_it.reversed_p)
7363 && it->cmp_it.from > 0)
7364 {
7365 /* Composition created while scanning backward. Proceed
7366 to the previous grapheme cluster. */
7367 it->cmp_it.to = it->cmp_it.from;
7368 }
7369 else
7370 {
7371 /* No more grapheme clusters in this composition.
7372 Find the next stop position. */
7373 ptrdiff_t stop = it->end_charpos;
7374
7375 if (it->bidi_it.scan_dir < 0)
7376 /* Now we are scanning backward and don't know
7377 where to stop. */
7378 stop = -1;
7379 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7380 IT_BYTEPOS (*it), stop, Qnil);
7381 }
7382 }
7383 else
7384 {
7385 eassert (it->len != 0);
7386
7387 if (!it->bidi_p)
7388 {
7389 IT_BYTEPOS (*it) += it->len;
7390 IT_CHARPOS (*it) += 1;
7391 }
7392 else
7393 {
7394 int prev_scan_dir = it->bidi_it.scan_dir;
7395 /* If this is a new paragraph, determine its base
7396 direction (a.k.a. its base embedding level). */
7397 if (it->bidi_it.new_paragraph)
7398 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7399 false);
7400 bidi_move_to_visually_next (&it->bidi_it);
7401 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7402 IT_CHARPOS (*it) = it->bidi_it.charpos;
7403 if (prev_scan_dir != it->bidi_it.scan_dir)
7404 {
7405 /* As the scan direction was changed, we must
7406 re-compute the stop position for composition. */
7407 ptrdiff_t stop = it->end_charpos;
7408 if (it->bidi_it.scan_dir < 0)
7409 stop = -1;
7410 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7411 IT_BYTEPOS (*it), stop, Qnil);
7412 }
7413 }
7414 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7415 }
7416 break;
7417
7418 case GET_FROM_C_STRING:
7419 /* Current display element of IT is from a C string. */
7420 if (!it->bidi_p
7421 /* If the string position is beyond string's end, it means
7422 next_element_from_c_string is padding the string with
7423 blanks, in which case we bypass the bidi iterator,
7424 because it cannot deal with such virtual characters. */
7425 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7426 {
7427 IT_BYTEPOS (*it) += it->len;
7428 IT_CHARPOS (*it) += 1;
7429 }
7430 else
7431 {
7432 bidi_move_to_visually_next (&it->bidi_it);
7433 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7434 IT_CHARPOS (*it) = it->bidi_it.charpos;
7435 }
7436 break;
7437
7438 case GET_FROM_DISPLAY_VECTOR:
7439 /* Current display element of IT is from a display table entry.
7440 Advance in the display table definition. Reset it to null if
7441 end reached, and continue with characters from buffers/
7442 strings. */
7443 ++it->current.dpvec_index;
7444
7445 /* Restore face of the iterator to what they were before the
7446 display vector entry (these entries may contain faces). */
7447 it->face_id = it->saved_face_id;
7448
7449 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7450 {
7451 bool recheck_faces = it->ellipsis_p;
7452
7453 if (it->s)
7454 it->method = GET_FROM_C_STRING;
7455 else if (STRINGP (it->string))
7456 it->method = GET_FROM_STRING;
7457 else
7458 {
7459 it->method = GET_FROM_BUFFER;
7460 it->object = it->w->contents;
7461 }
7462
7463 it->dpvec = NULL;
7464 it->current.dpvec_index = -1;
7465
7466 /* Skip over characters which were displayed via IT->dpvec. */
7467 if (it->dpvec_char_len < 0)
7468 reseat_at_next_visible_line_start (it, true);
7469 else if (it->dpvec_char_len > 0)
7470 {
7471 it->len = it->dpvec_char_len;
7472 set_iterator_to_next (it, reseat_p);
7473 }
7474
7475 /* Maybe recheck faces after display vector. */
7476 if (recheck_faces)
7477 {
7478 if (it->method == GET_FROM_STRING)
7479 it->stop_charpos = IT_STRING_CHARPOS (*it);
7480 else
7481 it->stop_charpos = IT_CHARPOS (*it);
7482 }
7483 }
7484 break;
7485
7486 case GET_FROM_STRING:
7487 /* Current display element is a character from a Lisp string. */
7488 eassert (it->s == NULL && STRINGP (it->string));
7489 /* Don't advance past string end. These conditions are true
7490 when set_iterator_to_next is called at the end of
7491 get_next_display_element, in which case the Lisp string is
7492 already exhausted, and all we want is pop the iterator
7493 stack. */
7494 if (it->current.overlay_string_index >= 0)
7495 {
7496 /* This is an overlay string, so there's no padding with
7497 spaces, and the number of characters in the string is
7498 where the string ends. */
7499 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7500 goto consider_string_end;
7501 }
7502 else
7503 {
7504 /* Not an overlay string. There could be padding, so test
7505 against it->end_charpos. */
7506 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7507 goto consider_string_end;
7508 }
7509 if (it->cmp_it.id >= 0)
7510 {
7511 /* We are delivering display elements from a composition.
7512 Update the string position past the grapheme cluster
7513 we've just processed. */
7514 if (! it->bidi_p)
7515 {
7516 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7517 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7518 }
7519 else
7520 {
7521 int i;
7522
7523 for (i = 0; i < it->cmp_it.nchars; i++)
7524 bidi_move_to_visually_next (&it->bidi_it);
7525 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7526 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7527 }
7528
7529 /* Did we exhaust all the grapheme clusters of this
7530 composition? */
7531 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7532 && (it->cmp_it.to < it->cmp_it.nglyphs))
7533 {
7534 /* Not all the grapheme clusters were processed yet;
7535 advance to the next cluster. */
7536 it->cmp_it.from = it->cmp_it.to;
7537 }
7538 else if ((it->bidi_p && it->cmp_it.reversed_p)
7539 && it->cmp_it.from > 0)
7540 {
7541 /* Likewise: advance to the next cluster, but going in
7542 the reverse direction. */
7543 it->cmp_it.to = it->cmp_it.from;
7544 }
7545 else
7546 {
7547 /* This composition was fully processed; find the next
7548 candidate place for checking for composed
7549 characters. */
7550 /* Always limit string searches to the string length;
7551 any padding spaces are not part of the string, and
7552 there cannot be any compositions in that padding. */
7553 ptrdiff_t stop = SCHARS (it->string);
7554
7555 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7556 stop = -1;
7557 else if (it->end_charpos < stop)
7558 {
7559 /* Cf. PRECISION in reseat_to_string: we might be
7560 limited in how many of the string characters we
7561 need to deliver. */
7562 stop = it->end_charpos;
7563 }
7564 composition_compute_stop_pos (&it->cmp_it,
7565 IT_STRING_CHARPOS (*it),
7566 IT_STRING_BYTEPOS (*it), stop,
7567 it->string);
7568 }
7569 }
7570 else
7571 {
7572 if (!it->bidi_p
7573 /* If the string position is beyond string's end, it
7574 means next_element_from_string is padding the string
7575 with blanks, in which case we bypass the bidi
7576 iterator, because it cannot deal with such virtual
7577 characters. */
7578 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7579 {
7580 IT_STRING_BYTEPOS (*it) += it->len;
7581 IT_STRING_CHARPOS (*it) += 1;
7582 }
7583 else
7584 {
7585 int prev_scan_dir = it->bidi_it.scan_dir;
7586
7587 bidi_move_to_visually_next (&it->bidi_it);
7588 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7589 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7590 /* If the scan direction changes, we may need to update
7591 the place where to check for composed characters. */
7592 if (prev_scan_dir != it->bidi_it.scan_dir)
7593 {
7594 ptrdiff_t stop = SCHARS (it->string);
7595
7596 if (it->bidi_it.scan_dir < 0)
7597 stop = -1;
7598 else if (it->end_charpos < stop)
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 }
7608
7609 consider_string_end:
7610
7611 if (it->current.overlay_string_index >= 0)
7612 {
7613 /* IT->string is an overlay string. Advance to the
7614 next, if there is one. */
7615 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7616 {
7617 it->ellipsis_p = false;
7618 next_overlay_string (it);
7619 if (it->ellipsis_p)
7620 setup_for_ellipsis (it, 0);
7621 }
7622 }
7623 else
7624 {
7625 /* IT->string is not an overlay string. If we reached
7626 its end, and there is something on IT->stack, proceed
7627 with what is on the stack. This can be either another
7628 string, this time an overlay string, or a buffer. */
7629 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7630 && it->sp > 0)
7631 {
7632 pop_it (it);
7633 if (it->method == GET_FROM_STRING)
7634 goto consider_string_end;
7635 }
7636 }
7637 break;
7638
7639 case GET_FROM_IMAGE:
7640 case GET_FROM_STRETCH:
7641 #ifdef HAVE_XWIDGETS
7642 case GET_FROM_XWIDGET:
7643 #endif
7644
7645 /* The position etc with which we have to proceed are on
7646 the stack. The position may be at the end of a string,
7647 if the `display' property takes up the whole string. */
7648 eassert (it->sp > 0);
7649 pop_it (it);
7650 if (it->method == GET_FROM_STRING)
7651 goto consider_string_end;
7652 break;
7653
7654 default:
7655 /* There are no other methods defined, so this should be a bug. */
7656 emacs_abort ();
7657 }
7658
7659 eassert (it->method != GET_FROM_STRING
7660 || (STRINGP (it->string)
7661 && IT_STRING_CHARPOS (*it) >= 0));
7662 }
7663
7664 /* Load IT's display element fields with information about the next
7665 display element which comes from a display table entry or from the
7666 result of translating a control character to one of the forms `^C'
7667 or `\003'.
7668
7669 IT->dpvec holds the glyphs to return as characters.
7670 IT->saved_face_id holds the face id before the display vector--it
7671 is restored into IT->face_id in set_iterator_to_next. */
7672
7673 static bool
7674 next_element_from_display_vector (struct it *it)
7675 {
7676 Lisp_Object gc;
7677 int prev_face_id = it->face_id;
7678 int next_face_id;
7679
7680 /* Precondition. */
7681 eassert (it->dpvec && it->current.dpvec_index >= 0);
7682
7683 it->face_id = it->saved_face_id;
7684
7685 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7686 That seemed totally bogus - so I changed it... */
7687 gc = it->dpvec[it->current.dpvec_index];
7688
7689 if (GLYPH_CODE_P (gc))
7690 {
7691 struct face *this_face, *prev_face, *next_face;
7692
7693 it->c = GLYPH_CODE_CHAR (gc);
7694 it->len = CHAR_BYTES (it->c);
7695
7696 /* The entry may contain a face id to use. Such a face id is
7697 the id of a Lisp face, not a realized face. A face id of
7698 zero means no face is specified. */
7699 if (it->dpvec_face_id >= 0)
7700 it->face_id = it->dpvec_face_id;
7701 else
7702 {
7703 int lface_id = GLYPH_CODE_FACE (gc);
7704 if (lface_id > 0)
7705 it->face_id = merge_faces (it->f, Qt, lface_id,
7706 it->saved_face_id);
7707 }
7708
7709 /* Glyphs in the display vector could have the box face, so we
7710 need to set the related flags in the iterator, as
7711 appropriate. */
7712 this_face = FACE_FROM_ID (it->f, it->face_id);
7713 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7714
7715 /* Is this character the first character of a box-face run? */
7716 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7717 && (!prev_face
7718 || prev_face->box == FACE_NO_BOX));
7719
7720 /* For the last character of the box-face run, we need to look
7721 either at the next glyph from the display vector, or at the
7722 face we saw before the display vector. */
7723 next_face_id = it->saved_face_id;
7724 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7725 {
7726 if (it->dpvec_face_id >= 0)
7727 next_face_id = it->dpvec_face_id;
7728 else
7729 {
7730 int lface_id =
7731 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7732
7733 if (lface_id > 0)
7734 next_face_id = merge_faces (it->f, Qt, lface_id,
7735 it->saved_face_id);
7736 }
7737 }
7738 next_face = FACE_FROM_ID (it->f, next_face_id);
7739 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7740 && (!next_face
7741 || next_face->box == FACE_NO_BOX));
7742 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7743 }
7744 else
7745 /* Display table entry is invalid. Return a space. */
7746 it->c = ' ', it->len = 1;
7747
7748 /* Don't change position and object of the iterator here. They are
7749 still the values of the character that had this display table
7750 entry or was translated, and that's what we want. */
7751 it->what = IT_CHARACTER;
7752 return true;
7753 }
7754
7755 /* Get the first element of string/buffer in the visual order, after
7756 being reseated to a new position in a string or a buffer. */
7757 static void
7758 get_visually_first_element (struct it *it)
7759 {
7760 bool string_p = STRINGP (it->string) || it->s;
7761 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7762 ptrdiff_t bob = (string_p ? 0 : BEGV);
7763
7764 if (STRINGP (it->string))
7765 {
7766 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7767 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7768 }
7769 else
7770 {
7771 it->bidi_it.charpos = IT_CHARPOS (*it);
7772 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7773 }
7774
7775 if (it->bidi_it.charpos == eob)
7776 {
7777 /* Nothing to do, but reset the FIRST_ELT flag, like
7778 bidi_paragraph_init does, because we are not going to
7779 call it. */
7780 it->bidi_it.first_elt = false;
7781 }
7782 else if (it->bidi_it.charpos == bob
7783 || (!string_p
7784 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7785 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7786 {
7787 /* If we are at the beginning of a line/string, we can produce
7788 the next element right away. */
7789 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7790 bidi_move_to_visually_next (&it->bidi_it);
7791 }
7792 else
7793 {
7794 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7795
7796 /* We need to prime the bidi iterator starting at the line's or
7797 string's beginning, before we will be able to produce the
7798 next element. */
7799 if (string_p)
7800 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7801 else
7802 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7803 IT_BYTEPOS (*it), -1,
7804 &it->bidi_it.bytepos);
7805 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7806 do
7807 {
7808 /* Now return to buffer/string position where we were asked
7809 to get the next display element, and produce that. */
7810 bidi_move_to_visually_next (&it->bidi_it);
7811 }
7812 while (it->bidi_it.bytepos != orig_bytepos
7813 && it->bidi_it.charpos < eob);
7814 }
7815
7816 /* Adjust IT's position information to where we ended up. */
7817 if (STRINGP (it->string))
7818 {
7819 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7820 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7821 }
7822 else
7823 {
7824 IT_CHARPOS (*it) = it->bidi_it.charpos;
7825 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7826 }
7827
7828 if (STRINGP (it->string) || !it->s)
7829 {
7830 ptrdiff_t stop, charpos, bytepos;
7831
7832 if (STRINGP (it->string))
7833 {
7834 eassert (!it->s);
7835 stop = SCHARS (it->string);
7836 if (stop > it->end_charpos)
7837 stop = it->end_charpos;
7838 charpos = IT_STRING_CHARPOS (*it);
7839 bytepos = IT_STRING_BYTEPOS (*it);
7840 }
7841 else
7842 {
7843 stop = it->end_charpos;
7844 charpos = IT_CHARPOS (*it);
7845 bytepos = IT_BYTEPOS (*it);
7846 }
7847 if (it->bidi_it.scan_dir < 0)
7848 stop = -1;
7849 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7850 it->string);
7851 }
7852 }
7853
7854 /* Load IT with the next display element from Lisp string IT->string.
7855 IT->current.string_pos is the current position within the string.
7856 If IT->current.overlay_string_index >= 0, the Lisp string is an
7857 overlay string. */
7858
7859 static bool
7860 next_element_from_string (struct it *it)
7861 {
7862 struct text_pos position;
7863
7864 eassert (STRINGP (it->string));
7865 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7866 eassert (IT_STRING_CHARPOS (*it) >= 0);
7867 position = it->current.string_pos;
7868
7869 /* With bidi reordering, the character to display might not be the
7870 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7871 that we were reseat()ed to a new string, whose paragraph
7872 direction is not known. */
7873 if (it->bidi_p && it->bidi_it.first_elt)
7874 {
7875 get_visually_first_element (it);
7876 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7877 }
7878
7879 /* Time to check for invisible text? */
7880 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7881 {
7882 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7883 {
7884 if (!(!it->bidi_p
7885 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7886 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7887 {
7888 /* With bidi non-linear iteration, we could find
7889 ourselves far beyond the last computed stop_charpos,
7890 with several other stop positions in between that we
7891 missed. Scan them all now, in buffer's logical
7892 order, until we find and handle the last stop_charpos
7893 that precedes our current position. */
7894 handle_stop_backwards (it, it->stop_charpos);
7895 return GET_NEXT_DISPLAY_ELEMENT (it);
7896 }
7897 else
7898 {
7899 if (it->bidi_p)
7900 {
7901 /* Take note of the stop position we just moved
7902 across, for when we will move back across it. */
7903 it->prev_stop = it->stop_charpos;
7904 /* If we are at base paragraph embedding level, take
7905 note of the last stop position seen at this
7906 level. */
7907 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7908 it->base_level_stop = it->stop_charpos;
7909 }
7910 handle_stop (it);
7911
7912 /* Since a handler may have changed IT->method, we must
7913 recurse here. */
7914 return GET_NEXT_DISPLAY_ELEMENT (it);
7915 }
7916 }
7917 else if (it->bidi_p
7918 /* If we are before prev_stop, we may have overstepped
7919 on our way backwards a stop_pos, and if so, we need
7920 to handle that stop_pos. */
7921 && IT_STRING_CHARPOS (*it) < it->prev_stop
7922 /* We can sometimes back up for reasons that have nothing
7923 to do with bidi reordering. E.g., compositions. The
7924 code below is only needed when we are above the base
7925 embedding level, so test for that explicitly. */
7926 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7927 {
7928 /* If we lost track of base_level_stop, we have no better
7929 place for handle_stop_backwards to start from than string
7930 beginning. This happens, e.g., when we were reseated to
7931 the previous screenful of text by vertical-motion. */
7932 if (it->base_level_stop <= 0
7933 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7934 it->base_level_stop = 0;
7935 handle_stop_backwards (it, it->base_level_stop);
7936 return GET_NEXT_DISPLAY_ELEMENT (it);
7937 }
7938 }
7939
7940 if (it->current.overlay_string_index >= 0)
7941 {
7942 /* Get the next character from an overlay string. In overlay
7943 strings, there is no field width or padding with spaces to
7944 do. */
7945 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7946 {
7947 it->what = IT_EOB;
7948 return false;
7949 }
7950 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7951 IT_STRING_BYTEPOS (*it),
7952 it->bidi_it.scan_dir < 0
7953 ? -1
7954 : SCHARS (it->string))
7955 && next_element_from_composition (it))
7956 {
7957 return true;
7958 }
7959 else if (STRING_MULTIBYTE (it->string))
7960 {
7961 const unsigned char *s = (SDATA (it->string)
7962 + IT_STRING_BYTEPOS (*it));
7963 it->c = string_char_and_length (s, &it->len);
7964 }
7965 else
7966 {
7967 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7968 it->len = 1;
7969 }
7970 }
7971 else
7972 {
7973 /* Get the next character from a Lisp string that is not an
7974 overlay string. Such strings come from the mode line, for
7975 example. We may have to pad with spaces, or truncate the
7976 string. See also next_element_from_c_string. */
7977 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7978 {
7979 it->what = IT_EOB;
7980 return false;
7981 }
7982 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7983 {
7984 /* Pad with spaces. */
7985 it->c = ' ', it->len = 1;
7986 CHARPOS (position) = BYTEPOS (position) = -1;
7987 }
7988 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7989 IT_STRING_BYTEPOS (*it),
7990 it->bidi_it.scan_dir < 0
7991 ? -1
7992 : it->string_nchars)
7993 && next_element_from_composition (it))
7994 {
7995 return true;
7996 }
7997 else if (STRING_MULTIBYTE (it->string))
7998 {
7999 const unsigned char *s = (SDATA (it->string)
8000 + IT_STRING_BYTEPOS (*it));
8001 it->c = string_char_and_length (s, &it->len);
8002 }
8003 else
8004 {
8005 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8006 it->len = 1;
8007 }
8008 }
8009
8010 /* Record what we have and where it came from. */
8011 it->what = IT_CHARACTER;
8012 it->object = it->string;
8013 it->position = position;
8014 return true;
8015 }
8016
8017
8018 /* Load IT with next display element from C string IT->s.
8019 IT->string_nchars is the maximum number of characters to return
8020 from the string. IT->end_charpos may be greater than
8021 IT->string_nchars when this function is called, in which case we
8022 may have to return padding spaces. Value is false if end of string
8023 reached, including padding spaces. */
8024
8025 static bool
8026 next_element_from_c_string (struct it *it)
8027 {
8028 bool success_p = true;
8029
8030 eassert (it->s);
8031 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8032 it->what = IT_CHARACTER;
8033 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8034 it->object = make_number (0);
8035
8036 /* With bidi reordering, the character to display might not be the
8037 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8038 we were reseated to a new string, whose paragraph direction is
8039 not known. */
8040 if (it->bidi_p && it->bidi_it.first_elt)
8041 get_visually_first_element (it);
8042
8043 /* IT's position can be greater than IT->string_nchars in case a
8044 field width or precision has been specified when the iterator was
8045 initialized. */
8046 if (IT_CHARPOS (*it) >= it->end_charpos)
8047 {
8048 /* End of the game. */
8049 it->what = IT_EOB;
8050 success_p = false;
8051 }
8052 else if (IT_CHARPOS (*it) >= it->string_nchars)
8053 {
8054 /* Pad with spaces. */
8055 it->c = ' ', it->len = 1;
8056 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8057 }
8058 else if (it->multibyte_p)
8059 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8060 else
8061 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8062
8063 return success_p;
8064 }
8065
8066
8067 /* Set up IT to return characters from an ellipsis, if appropriate.
8068 The definition of the ellipsis glyphs may come from a display table
8069 entry. This function fills IT with the first glyph from the
8070 ellipsis if an ellipsis is to be displayed. */
8071
8072 static bool
8073 next_element_from_ellipsis (struct it *it)
8074 {
8075 if (it->selective_display_ellipsis_p)
8076 setup_for_ellipsis (it, it->len);
8077 else
8078 {
8079 /* The face at the current position may be different from the
8080 face we find after the invisible text. Remember what it
8081 was in IT->saved_face_id, and signal that it's there by
8082 setting face_before_selective_p. */
8083 it->saved_face_id = it->face_id;
8084 it->method = GET_FROM_BUFFER;
8085 it->object = it->w->contents;
8086 reseat_at_next_visible_line_start (it, true);
8087 it->face_before_selective_p = true;
8088 }
8089
8090 return GET_NEXT_DISPLAY_ELEMENT (it);
8091 }
8092
8093
8094 /* Deliver an image display element. The iterator IT is already
8095 filled with image information (done in handle_display_prop). Value
8096 is always true. */
8097
8098
8099 static bool
8100 next_element_from_image (struct it *it)
8101 {
8102 it->what = IT_IMAGE;
8103 return true;
8104 }
8105
8106 #ifdef HAVE_XWIDGETS
8107 static bool
8108 next_element_from_xwidget (struct it *it)
8109 {
8110 it->what = IT_XWIDGET;
8111 return true;
8112 }
8113 #endif
8114
8115
8116 /* Fill iterator IT with next display element from a stretch glyph
8117 property. IT->object is the value of the text property. Value is
8118 always true. */
8119
8120 static bool
8121 next_element_from_stretch (struct it *it)
8122 {
8123 it->what = IT_STRETCH;
8124 return true;
8125 }
8126
8127 /* Scan backwards from IT's current position until we find a stop
8128 position, or until BEGV. This is called when we find ourself
8129 before both the last known prev_stop and base_level_stop while
8130 reordering bidirectional text. */
8131
8132 static void
8133 compute_stop_pos_backwards (struct it *it)
8134 {
8135 const int SCAN_BACK_LIMIT = 1000;
8136 struct text_pos pos;
8137 struct display_pos save_current = it->current;
8138 struct text_pos save_position = it->position;
8139 ptrdiff_t charpos = IT_CHARPOS (*it);
8140 ptrdiff_t where_we_are = charpos;
8141 ptrdiff_t save_stop_pos = it->stop_charpos;
8142 ptrdiff_t save_end_pos = it->end_charpos;
8143
8144 eassert (NILP (it->string) && !it->s);
8145 eassert (it->bidi_p);
8146 it->bidi_p = false;
8147 do
8148 {
8149 it->end_charpos = min (charpos + 1, ZV);
8150 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8151 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8152 reseat_1 (it, pos, false);
8153 compute_stop_pos (it);
8154 /* We must advance forward, right? */
8155 if (it->stop_charpos <= charpos)
8156 emacs_abort ();
8157 }
8158 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8159
8160 if (it->stop_charpos <= where_we_are)
8161 it->prev_stop = it->stop_charpos;
8162 else
8163 it->prev_stop = BEGV;
8164 it->bidi_p = true;
8165 it->current = save_current;
8166 it->position = save_position;
8167 it->stop_charpos = save_stop_pos;
8168 it->end_charpos = save_end_pos;
8169 }
8170
8171 /* Scan forward from CHARPOS in the current buffer/string, until we
8172 find a stop position > current IT's position. Then handle the stop
8173 position before that. This is called when we bump into a stop
8174 position while reordering bidirectional text. CHARPOS should be
8175 the last previously processed stop_pos (or BEGV/0, if none were
8176 processed yet) whose position is less that IT's current
8177 position. */
8178
8179 static void
8180 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8181 {
8182 bool bufp = !STRINGP (it->string);
8183 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8184 struct display_pos save_current = it->current;
8185 struct text_pos save_position = it->position;
8186 struct text_pos pos1;
8187 ptrdiff_t next_stop;
8188
8189 /* Scan in strict logical order. */
8190 eassert (it->bidi_p);
8191 it->bidi_p = false;
8192 do
8193 {
8194 it->prev_stop = charpos;
8195 if (bufp)
8196 {
8197 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8198 reseat_1 (it, pos1, false);
8199 }
8200 else
8201 it->current.string_pos = string_pos (charpos, it->string);
8202 compute_stop_pos (it);
8203 /* We must advance forward, right? */
8204 if (it->stop_charpos <= it->prev_stop)
8205 emacs_abort ();
8206 charpos = it->stop_charpos;
8207 }
8208 while (charpos <= where_we_are);
8209
8210 it->bidi_p = true;
8211 it->current = save_current;
8212 it->position = save_position;
8213 next_stop = it->stop_charpos;
8214 it->stop_charpos = it->prev_stop;
8215 handle_stop (it);
8216 it->stop_charpos = next_stop;
8217 }
8218
8219 /* Load IT with the next display element from current_buffer. Value
8220 is false if end of buffer reached. IT->stop_charpos is the next
8221 position at which to stop and check for text properties or buffer
8222 end. */
8223
8224 static bool
8225 next_element_from_buffer (struct it *it)
8226 {
8227 bool success_p = true;
8228
8229 eassert (IT_CHARPOS (*it) >= BEGV);
8230 eassert (NILP (it->string) && !it->s);
8231 eassert (!it->bidi_p
8232 || (EQ (it->bidi_it.string.lstring, Qnil)
8233 && it->bidi_it.string.s == NULL));
8234
8235 /* With bidi reordering, the character to display might not be the
8236 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8237 we were reseat()ed to a new buffer position, which is potentially
8238 a different paragraph. */
8239 if (it->bidi_p && it->bidi_it.first_elt)
8240 {
8241 get_visually_first_element (it);
8242 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8243 }
8244
8245 if (IT_CHARPOS (*it) >= it->stop_charpos)
8246 {
8247 if (IT_CHARPOS (*it) >= it->end_charpos)
8248 {
8249 bool overlay_strings_follow_p;
8250
8251 /* End of the game, except when overlay strings follow that
8252 haven't been returned yet. */
8253 if (it->overlay_strings_at_end_processed_p)
8254 overlay_strings_follow_p = false;
8255 else
8256 {
8257 it->overlay_strings_at_end_processed_p = true;
8258 overlay_strings_follow_p = get_overlay_strings (it, 0);
8259 }
8260
8261 if (overlay_strings_follow_p)
8262 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8263 else
8264 {
8265 it->what = IT_EOB;
8266 it->position = it->current.pos;
8267 success_p = false;
8268 }
8269 }
8270 else if (!(!it->bidi_p
8271 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8272 || IT_CHARPOS (*it) == it->stop_charpos))
8273 {
8274 /* With bidi non-linear iteration, we could find ourselves
8275 far beyond the last computed stop_charpos, with several
8276 other stop positions in between that we missed. Scan
8277 them all now, in buffer's logical order, until we find
8278 and handle the last stop_charpos that precedes our
8279 current position. */
8280 handle_stop_backwards (it, it->stop_charpos);
8281 it->ignore_overlay_strings_at_pos_p = false;
8282 return GET_NEXT_DISPLAY_ELEMENT (it);
8283 }
8284 else
8285 {
8286 if (it->bidi_p)
8287 {
8288 /* Take note of the stop position we just moved across,
8289 for when we will move back across it. */
8290 it->prev_stop = it->stop_charpos;
8291 /* If we are at base paragraph embedding level, take
8292 note of the last stop position seen at this
8293 level. */
8294 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8295 it->base_level_stop = it->stop_charpos;
8296 }
8297 handle_stop (it);
8298 it->ignore_overlay_strings_at_pos_p = false;
8299 return GET_NEXT_DISPLAY_ELEMENT (it);
8300 }
8301 }
8302 else if (it->bidi_p
8303 /* If we are before prev_stop, we may have overstepped on
8304 our way backwards a stop_pos, and if so, we need to
8305 handle that stop_pos. */
8306 && IT_CHARPOS (*it) < it->prev_stop
8307 /* We can sometimes back up for reasons that have nothing
8308 to do with bidi reordering. E.g., compositions. The
8309 code below is only needed when we are above the base
8310 embedding level, so test for that explicitly. */
8311 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8312 {
8313 if (it->base_level_stop <= 0
8314 || IT_CHARPOS (*it) < it->base_level_stop)
8315 {
8316 /* If we lost track of base_level_stop, we need to find
8317 prev_stop by looking backwards. This happens, e.g., when
8318 we were reseated to the previous screenful of text by
8319 vertical-motion. */
8320 it->base_level_stop = BEGV;
8321 compute_stop_pos_backwards (it);
8322 handle_stop_backwards (it, it->prev_stop);
8323 }
8324 else
8325 handle_stop_backwards (it, it->base_level_stop);
8326 it->ignore_overlay_strings_at_pos_p = false;
8327 return GET_NEXT_DISPLAY_ELEMENT (it);
8328 }
8329 else
8330 {
8331 /* No face changes, overlays etc. in sight, so just return a
8332 character from current_buffer. */
8333 unsigned char *p;
8334 ptrdiff_t stop;
8335
8336 /* We moved to the next buffer position, so any info about
8337 previously seen overlays is no longer valid. */
8338 it->ignore_overlay_strings_at_pos_p = false;
8339
8340 /* Maybe run the redisplay end trigger hook. Performance note:
8341 This doesn't seem to cost measurable time. */
8342 if (it->redisplay_end_trigger_charpos
8343 && it->glyph_row
8344 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8345 run_redisplay_end_trigger_hook (it);
8346
8347 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8348 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8349 stop)
8350 && next_element_from_composition (it))
8351 {
8352 return true;
8353 }
8354
8355 /* Get the next character, maybe multibyte. */
8356 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8357 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8358 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8359 else
8360 it->c = *p, it->len = 1;
8361
8362 /* Record what we have and where it came from. */
8363 it->what = IT_CHARACTER;
8364 it->object = it->w->contents;
8365 it->position = it->current.pos;
8366
8367 /* Normally we return the character found above, except when we
8368 really want to return an ellipsis for selective display. */
8369 if (it->selective)
8370 {
8371 if (it->c == '\n')
8372 {
8373 /* A value of selective > 0 means hide lines indented more
8374 than that number of columns. */
8375 if (it->selective > 0
8376 && IT_CHARPOS (*it) + 1 < ZV
8377 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8378 IT_BYTEPOS (*it) + 1,
8379 it->selective))
8380 {
8381 success_p = next_element_from_ellipsis (it);
8382 it->dpvec_char_len = -1;
8383 }
8384 }
8385 else if (it->c == '\r' && it->selective == -1)
8386 {
8387 /* A value of selective == -1 means that everything from the
8388 CR to the end of the line is invisible, with maybe an
8389 ellipsis displayed for it. */
8390 success_p = next_element_from_ellipsis (it);
8391 it->dpvec_char_len = -1;
8392 }
8393 }
8394 }
8395
8396 /* Value is false if end of buffer reached. */
8397 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8398 return success_p;
8399 }
8400
8401
8402 /* Run the redisplay end trigger hook for IT. */
8403
8404 static void
8405 run_redisplay_end_trigger_hook (struct it *it)
8406 {
8407 /* IT->glyph_row should be non-null, i.e. we should be actually
8408 displaying something, or otherwise we should not run the hook. */
8409 eassert (it->glyph_row);
8410
8411 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8412 it->redisplay_end_trigger_charpos = 0;
8413
8414 /* Since we are *trying* to run these functions, don't try to run
8415 them again, even if they get an error. */
8416 wset_redisplay_end_trigger (it->w, Qnil);
8417 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8418 make_number (charpos));
8419
8420 /* Notice if it changed the face of the character we are on. */
8421 handle_face_prop (it);
8422 }
8423
8424
8425 /* Deliver a composition display element. Unlike the other
8426 next_element_from_XXX, this function is not registered in the array
8427 get_next_element[]. It is called from next_element_from_buffer and
8428 next_element_from_string when necessary. */
8429
8430 static bool
8431 next_element_from_composition (struct it *it)
8432 {
8433 it->what = IT_COMPOSITION;
8434 it->len = it->cmp_it.nbytes;
8435 if (STRINGP (it->string))
8436 {
8437 if (it->c < 0)
8438 {
8439 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8440 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8441 return false;
8442 }
8443 it->position = it->current.string_pos;
8444 it->object = it->string;
8445 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8446 IT_STRING_BYTEPOS (*it), it->string);
8447 }
8448 else
8449 {
8450 if (it->c < 0)
8451 {
8452 IT_CHARPOS (*it) += it->cmp_it.nchars;
8453 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8454 if (it->bidi_p)
8455 {
8456 if (it->bidi_it.new_paragraph)
8457 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8458 false);
8459 /* Resync the bidi iterator with IT's new position.
8460 FIXME: this doesn't support bidirectional text. */
8461 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8462 bidi_move_to_visually_next (&it->bidi_it);
8463 }
8464 return false;
8465 }
8466 it->position = it->current.pos;
8467 it->object = it->w->contents;
8468 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8469 IT_BYTEPOS (*it), Qnil);
8470 }
8471 return true;
8472 }
8473
8474
8475 \f
8476 /***********************************************************************
8477 Moving an iterator without producing glyphs
8478 ***********************************************************************/
8479
8480 /* Check if iterator is at a position corresponding to a valid buffer
8481 position after some move_it_ call. */
8482
8483 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8484 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8485
8486
8487 /* Move iterator IT to a specified buffer or X position within one
8488 line on the display without producing glyphs.
8489
8490 OP should be a bit mask including some or all of these bits:
8491 MOVE_TO_X: Stop upon reaching x-position TO_X.
8492 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8493 Regardless of OP's value, stop upon reaching the end of the display line.
8494
8495 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8496 This means, in particular, that TO_X includes window's horizontal
8497 scroll amount.
8498
8499 The return value has several possible values that
8500 say what condition caused the scan to stop:
8501
8502 MOVE_POS_MATCH_OR_ZV
8503 - when TO_POS or ZV was reached.
8504
8505 MOVE_X_REACHED
8506 -when TO_X was reached before TO_POS or ZV were reached.
8507
8508 MOVE_LINE_CONTINUED
8509 - when we reached the end of the display area and the line must
8510 be continued.
8511
8512 MOVE_LINE_TRUNCATED
8513 - when we reached the end of the display area and the line is
8514 truncated.
8515
8516 MOVE_NEWLINE_OR_CR
8517 - when we stopped at a line end, i.e. a newline or a CR and selective
8518 display is on. */
8519
8520 static enum move_it_result
8521 move_it_in_display_line_to (struct it *it,
8522 ptrdiff_t to_charpos, int to_x,
8523 enum move_operation_enum op)
8524 {
8525 enum move_it_result result = MOVE_UNDEFINED;
8526 struct glyph_row *saved_glyph_row;
8527 struct it wrap_it, atpos_it, atx_it, ppos_it;
8528 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8529 void *ppos_data = NULL;
8530 bool may_wrap = false;
8531 enum it_method prev_method = it->method;
8532 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8533 bool saw_smaller_pos = prev_pos < to_charpos;
8534
8535 /* Don't produce glyphs in produce_glyphs. */
8536 saved_glyph_row = it->glyph_row;
8537 it->glyph_row = NULL;
8538
8539 /* Use wrap_it to save a copy of IT wherever a word wrap could
8540 occur. Use atpos_it to save a copy of IT at the desired buffer
8541 position, if found, so that we can scan ahead and check if the
8542 word later overshoots the window edge. Use atx_it similarly, for
8543 pixel positions. */
8544 wrap_it.sp = -1;
8545 atpos_it.sp = -1;
8546 atx_it.sp = -1;
8547
8548 /* Use ppos_it under bidi reordering to save a copy of IT for the
8549 initial position. We restore that position in IT when we have
8550 scanned the entire display line without finding a match for
8551 TO_CHARPOS and all the character positions are greater than
8552 TO_CHARPOS. We then restart the scan from the initial position,
8553 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8554 the closest to TO_CHARPOS. */
8555 if (it->bidi_p)
8556 {
8557 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8558 {
8559 SAVE_IT (ppos_it, *it, ppos_data);
8560 closest_pos = IT_CHARPOS (*it);
8561 }
8562 else
8563 closest_pos = ZV;
8564 }
8565
8566 #define BUFFER_POS_REACHED_P() \
8567 ((op & MOVE_TO_POS) != 0 \
8568 && BUFFERP (it->object) \
8569 && (IT_CHARPOS (*it) == to_charpos \
8570 || ((!it->bidi_p \
8571 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8572 && IT_CHARPOS (*it) > to_charpos) \
8573 || (it->what == IT_COMPOSITION \
8574 && ((IT_CHARPOS (*it) > to_charpos \
8575 && to_charpos >= it->cmp_it.charpos) \
8576 || (IT_CHARPOS (*it) < to_charpos \
8577 && to_charpos <= it->cmp_it.charpos)))) \
8578 && (it->method == GET_FROM_BUFFER \
8579 || (it->method == GET_FROM_DISPLAY_VECTOR \
8580 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8581
8582 /* If there's a line-/wrap-prefix, handle it. */
8583 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8584 && it->current_y < it->last_visible_y)
8585 handle_line_prefix (it);
8586
8587 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8588 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8589
8590 while (true)
8591 {
8592 int x, i, ascent = 0, descent = 0;
8593
8594 /* Utility macro to reset an iterator with x, ascent, and descent. */
8595 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8596 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8597 (IT)->max_descent = descent)
8598
8599 /* Stop if we move beyond TO_CHARPOS (after an image or a
8600 display string or stretch glyph). */
8601 if ((op & MOVE_TO_POS) != 0
8602 && BUFFERP (it->object)
8603 && it->method == GET_FROM_BUFFER
8604 && (((!it->bidi_p
8605 /* When the iterator is at base embedding level, we
8606 are guaranteed that characters are delivered for
8607 display in strictly increasing order of their
8608 buffer positions. */
8609 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8610 && IT_CHARPOS (*it) > to_charpos)
8611 || (it->bidi_p
8612 && (prev_method == GET_FROM_IMAGE
8613 || prev_method == GET_FROM_STRETCH
8614 || prev_method == GET_FROM_STRING)
8615 /* Passed TO_CHARPOS from left to right. */
8616 && ((prev_pos < to_charpos
8617 && IT_CHARPOS (*it) > to_charpos)
8618 /* Passed TO_CHARPOS from right to left. */
8619 || (prev_pos > to_charpos
8620 && IT_CHARPOS (*it) < to_charpos)))))
8621 {
8622 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8623 {
8624 result = MOVE_POS_MATCH_OR_ZV;
8625 break;
8626 }
8627 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8628 /* If wrap_it is valid, the current position might be in a
8629 word that is wrapped. So, save the iterator in
8630 atpos_it and continue to see if wrapping happens. */
8631 SAVE_IT (atpos_it, *it, atpos_data);
8632 }
8633
8634 /* Stop when ZV reached.
8635 We used to stop here when TO_CHARPOS reached as well, but that is
8636 too soon if this glyph does not fit on this line. So we handle it
8637 explicitly below. */
8638 if (!get_next_display_element (it))
8639 {
8640 result = MOVE_POS_MATCH_OR_ZV;
8641 break;
8642 }
8643
8644 if (it->line_wrap == TRUNCATE)
8645 {
8646 if (BUFFER_POS_REACHED_P ())
8647 {
8648 result = MOVE_POS_MATCH_OR_ZV;
8649 break;
8650 }
8651 }
8652 else
8653 {
8654 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8655 {
8656 if (IT_DISPLAYING_WHITESPACE (it))
8657 may_wrap = true;
8658 else if (may_wrap)
8659 {
8660 /* We have reached a glyph that follows one or more
8661 whitespace characters. If the position is
8662 already found, we are done. */
8663 if (atpos_it.sp >= 0)
8664 {
8665 RESTORE_IT (it, &atpos_it, atpos_data);
8666 result = MOVE_POS_MATCH_OR_ZV;
8667 goto done;
8668 }
8669 if (atx_it.sp >= 0)
8670 {
8671 RESTORE_IT (it, &atx_it, atx_data);
8672 result = MOVE_X_REACHED;
8673 goto done;
8674 }
8675 /* Otherwise, we can wrap here. */
8676 SAVE_IT (wrap_it, *it, wrap_data);
8677 may_wrap = false;
8678 }
8679 }
8680 }
8681
8682 /* Remember the line height for the current line, in case
8683 the next element doesn't fit on the line. */
8684 ascent = it->max_ascent;
8685 descent = it->max_descent;
8686
8687 /* The call to produce_glyphs will get the metrics of the
8688 display element IT is loaded with. Record the x-position
8689 before this display element, in case it doesn't fit on the
8690 line. */
8691 x = it->current_x;
8692
8693 PRODUCE_GLYPHS (it);
8694
8695 if (it->area != TEXT_AREA)
8696 {
8697 prev_method = it->method;
8698 if (it->method == GET_FROM_BUFFER)
8699 prev_pos = IT_CHARPOS (*it);
8700 set_iterator_to_next (it, true);
8701 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8702 SET_TEXT_POS (this_line_min_pos,
8703 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8704 if (it->bidi_p
8705 && (op & MOVE_TO_POS)
8706 && IT_CHARPOS (*it) > to_charpos
8707 && IT_CHARPOS (*it) < closest_pos)
8708 closest_pos = IT_CHARPOS (*it);
8709 continue;
8710 }
8711
8712 /* The number of glyphs we get back in IT->nglyphs will normally
8713 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8714 character on a terminal frame, or (iii) a line end. For the
8715 second case, IT->nglyphs - 1 padding glyphs will be present.
8716 (On X frames, there is only one glyph produced for a
8717 composite character.)
8718
8719 The behavior implemented below means, for continuation lines,
8720 that as many spaces of a TAB as fit on the current line are
8721 displayed there. For terminal frames, as many glyphs of a
8722 multi-glyph character are displayed in the current line, too.
8723 This is what the old redisplay code did, and we keep it that
8724 way. Under X, the whole shape of a complex character must
8725 fit on the line or it will be completely displayed in the
8726 next line.
8727
8728 Note that both for tabs and padding glyphs, all glyphs have
8729 the same width. */
8730 if (it->nglyphs)
8731 {
8732 /* More than one glyph or glyph doesn't fit on line. All
8733 glyphs have the same width. */
8734 int single_glyph_width = it->pixel_width / it->nglyphs;
8735 int new_x;
8736 int x_before_this_char = x;
8737 int hpos_before_this_char = it->hpos;
8738
8739 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8740 {
8741 new_x = x + single_glyph_width;
8742
8743 /* We want to leave anything reaching TO_X to the caller. */
8744 if ((op & MOVE_TO_X) && new_x > to_x)
8745 {
8746 if (BUFFER_POS_REACHED_P ())
8747 {
8748 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8749 goto buffer_pos_reached;
8750 if (atpos_it.sp < 0)
8751 {
8752 SAVE_IT (atpos_it, *it, atpos_data);
8753 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8754 }
8755 }
8756 else
8757 {
8758 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8759 {
8760 it->current_x = x;
8761 result = MOVE_X_REACHED;
8762 break;
8763 }
8764 if (atx_it.sp < 0)
8765 {
8766 SAVE_IT (atx_it, *it, atx_data);
8767 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8768 }
8769 }
8770 }
8771
8772 if (/* Lines are continued. */
8773 it->line_wrap != TRUNCATE
8774 && (/* And glyph doesn't fit on the line. */
8775 new_x > it->last_visible_x
8776 /* Or it fits exactly and we're on a window
8777 system frame. */
8778 || (new_x == it->last_visible_x
8779 && FRAME_WINDOW_P (it->f)
8780 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8781 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8782 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8783 {
8784 if (/* IT->hpos == 0 means the very first glyph
8785 doesn't fit on the line, e.g. a wide image. */
8786 it->hpos == 0
8787 || (new_x == it->last_visible_x
8788 && FRAME_WINDOW_P (it->f)))
8789 {
8790 ++it->hpos;
8791 it->current_x = new_x;
8792
8793 /* The character's last glyph just barely fits
8794 in this row. */
8795 if (i == it->nglyphs - 1)
8796 {
8797 /* If this is the destination position,
8798 return a position *before* it in this row,
8799 now that we know it fits in this row. */
8800 if (BUFFER_POS_REACHED_P ())
8801 {
8802 if (it->line_wrap != WORD_WRAP
8803 || wrap_it.sp < 0
8804 /* If we've just found whitespace to
8805 wrap, effectively ignore the
8806 previous wrap point -- it is no
8807 longer relevant, but we won't
8808 have an opportunity to update it,
8809 since we've reached the edge of
8810 this screen line. */
8811 || (may_wrap
8812 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8813 {
8814 it->hpos = hpos_before_this_char;
8815 it->current_x = x_before_this_char;
8816 result = MOVE_POS_MATCH_OR_ZV;
8817 break;
8818 }
8819 if (it->line_wrap == WORD_WRAP
8820 && atpos_it.sp < 0)
8821 {
8822 SAVE_IT (atpos_it, *it, atpos_data);
8823 atpos_it.current_x = x_before_this_char;
8824 atpos_it.hpos = hpos_before_this_char;
8825 }
8826 }
8827
8828 prev_method = it->method;
8829 if (it->method == GET_FROM_BUFFER)
8830 prev_pos = IT_CHARPOS (*it);
8831 set_iterator_to_next (it, true);
8832 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8833 SET_TEXT_POS (this_line_min_pos,
8834 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8835 /* On graphical terminals, newlines may
8836 "overflow" into the fringe if
8837 overflow-newline-into-fringe is non-nil.
8838 On text terminals, and on graphical
8839 terminals with no right margin, newlines
8840 may overflow into the last glyph on the
8841 display line.*/
8842 if (!FRAME_WINDOW_P (it->f)
8843 || ((it->bidi_p
8844 && it->bidi_it.paragraph_dir == R2L)
8845 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8846 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8847 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8848 {
8849 if (!get_next_display_element (it))
8850 {
8851 result = MOVE_POS_MATCH_OR_ZV;
8852 break;
8853 }
8854 if (BUFFER_POS_REACHED_P ())
8855 {
8856 if (ITERATOR_AT_END_OF_LINE_P (it))
8857 result = MOVE_POS_MATCH_OR_ZV;
8858 else
8859 result = MOVE_LINE_CONTINUED;
8860 break;
8861 }
8862 if (ITERATOR_AT_END_OF_LINE_P (it)
8863 && (it->line_wrap != WORD_WRAP
8864 || wrap_it.sp < 0
8865 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8866 {
8867 result = MOVE_NEWLINE_OR_CR;
8868 break;
8869 }
8870 }
8871 }
8872 }
8873 else
8874 IT_RESET_X_ASCENT_DESCENT (it);
8875
8876 /* If the screen line ends with whitespace, and we
8877 are under word-wrap, don't use wrap_it: it is no
8878 longer relevant, but we won't have an opportunity
8879 to update it, since we are done with this screen
8880 line. */
8881 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8882 {
8883 /* If we've found TO_X, go back there, as we now
8884 know the last word fits on this screen line. */
8885 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8886 && atx_it.sp >= 0)
8887 {
8888 RESTORE_IT (it, &atx_it, atx_data);
8889 atpos_it.sp = -1;
8890 atx_it.sp = -1;
8891 result = MOVE_X_REACHED;
8892 break;
8893 }
8894 }
8895 else if (wrap_it.sp >= 0)
8896 {
8897 RESTORE_IT (it, &wrap_it, wrap_data);
8898 atpos_it.sp = -1;
8899 atx_it.sp = -1;
8900 }
8901
8902 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8903 IT_CHARPOS (*it)));
8904 result = MOVE_LINE_CONTINUED;
8905 break;
8906 }
8907
8908 if (BUFFER_POS_REACHED_P ())
8909 {
8910 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8911 goto buffer_pos_reached;
8912 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8913 {
8914 SAVE_IT (atpos_it, *it, atpos_data);
8915 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8916 }
8917 }
8918
8919 if (new_x > it->first_visible_x)
8920 {
8921 /* Glyph is visible. Increment number of glyphs that
8922 would be displayed. */
8923 ++it->hpos;
8924 }
8925 }
8926
8927 if (result != MOVE_UNDEFINED)
8928 break;
8929 }
8930 else if (BUFFER_POS_REACHED_P ())
8931 {
8932 buffer_pos_reached:
8933 IT_RESET_X_ASCENT_DESCENT (it);
8934 result = MOVE_POS_MATCH_OR_ZV;
8935 break;
8936 }
8937 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8938 {
8939 /* Stop when TO_X specified and reached. This check is
8940 necessary here because of lines consisting of a line end,
8941 only. The line end will not produce any glyphs and we
8942 would never get MOVE_X_REACHED. */
8943 eassert (it->nglyphs == 0);
8944 result = MOVE_X_REACHED;
8945 break;
8946 }
8947
8948 /* Is this a line end? If yes, we're done. */
8949 if (ITERATOR_AT_END_OF_LINE_P (it))
8950 {
8951 /* If we are past TO_CHARPOS, but never saw any character
8952 positions smaller than TO_CHARPOS, return
8953 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8954 did. */
8955 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8956 {
8957 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8958 {
8959 if (closest_pos < ZV)
8960 {
8961 RESTORE_IT (it, &ppos_it, ppos_data);
8962 /* Don't recurse if closest_pos is equal to
8963 to_charpos, since we have just tried that. */
8964 if (closest_pos != to_charpos)
8965 move_it_in_display_line_to (it, closest_pos, -1,
8966 MOVE_TO_POS);
8967 result = MOVE_POS_MATCH_OR_ZV;
8968 }
8969 else
8970 goto buffer_pos_reached;
8971 }
8972 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8973 && IT_CHARPOS (*it) > to_charpos)
8974 goto buffer_pos_reached;
8975 else
8976 result = MOVE_NEWLINE_OR_CR;
8977 }
8978 else
8979 result = MOVE_NEWLINE_OR_CR;
8980 break;
8981 }
8982
8983 prev_method = it->method;
8984 if (it->method == GET_FROM_BUFFER)
8985 prev_pos = IT_CHARPOS (*it);
8986 /* The current display element has been consumed. Advance
8987 to the next. */
8988 set_iterator_to_next (it, true);
8989 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8990 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8991 if (IT_CHARPOS (*it) < to_charpos)
8992 saw_smaller_pos = true;
8993 if (it->bidi_p
8994 && (op & MOVE_TO_POS)
8995 && IT_CHARPOS (*it) >= to_charpos
8996 && IT_CHARPOS (*it) < closest_pos)
8997 closest_pos = IT_CHARPOS (*it);
8998
8999 /* Stop if lines are truncated and IT's current x-position is
9000 past the right edge of the window now. */
9001 if (it->line_wrap == TRUNCATE
9002 && it->current_x >= it->last_visible_x)
9003 {
9004 if (!FRAME_WINDOW_P (it->f)
9005 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9006 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9007 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9008 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9009 {
9010 bool at_eob_p = false;
9011
9012 if ((at_eob_p = !get_next_display_element (it))
9013 || BUFFER_POS_REACHED_P ()
9014 /* If we are past TO_CHARPOS, but never saw any
9015 character positions smaller than TO_CHARPOS,
9016 return MOVE_POS_MATCH_OR_ZV, like the
9017 unidirectional display did. */
9018 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9019 && !saw_smaller_pos
9020 && IT_CHARPOS (*it) > to_charpos))
9021 {
9022 if (it->bidi_p
9023 && !BUFFER_POS_REACHED_P ()
9024 && !at_eob_p && closest_pos < ZV)
9025 {
9026 RESTORE_IT (it, &ppos_it, ppos_data);
9027 if (closest_pos != to_charpos)
9028 move_it_in_display_line_to (it, closest_pos, -1,
9029 MOVE_TO_POS);
9030 }
9031 result = MOVE_POS_MATCH_OR_ZV;
9032 break;
9033 }
9034 if (ITERATOR_AT_END_OF_LINE_P (it))
9035 {
9036 result = MOVE_NEWLINE_OR_CR;
9037 break;
9038 }
9039 }
9040 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9041 && !saw_smaller_pos
9042 && IT_CHARPOS (*it) > to_charpos)
9043 {
9044 if (closest_pos < ZV)
9045 {
9046 RESTORE_IT (it, &ppos_it, ppos_data);
9047 if (closest_pos != to_charpos)
9048 move_it_in_display_line_to (it, closest_pos, -1,
9049 MOVE_TO_POS);
9050 }
9051 result = MOVE_POS_MATCH_OR_ZV;
9052 break;
9053 }
9054 result = MOVE_LINE_TRUNCATED;
9055 break;
9056 }
9057 #undef IT_RESET_X_ASCENT_DESCENT
9058 }
9059
9060 #undef BUFFER_POS_REACHED_P
9061
9062 /* If we scanned beyond to_pos and didn't find a point to wrap at,
9063 restore the saved iterator. */
9064 if (atpos_it.sp >= 0)
9065 RESTORE_IT (it, &atpos_it, atpos_data);
9066 else if (atx_it.sp >= 0)
9067 RESTORE_IT (it, &atx_it, atx_data);
9068
9069 done:
9070
9071 if (atpos_data)
9072 bidi_unshelve_cache (atpos_data, true);
9073 if (atx_data)
9074 bidi_unshelve_cache (atx_data, true);
9075 if (wrap_data)
9076 bidi_unshelve_cache (wrap_data, true);
9077 if (ppos_data)
9078 bidi_unshelve_cache (ppos_data, true);
9079
9080 /* Restore the iterator settings altered at the beginning of this
9081 function. */
9082 it->glyph_row = saved_glyph_row;
9083 return result;
9084 }
9085
9086 /* For external use. */
9087 void
9088 move_it_in_display_line (struct it *it,
9089 ptrdiff_t to_charpos, int to_x,
9090 enum move_operation_enum op)
9091 {
9092 if (it->line_wrap == WORD_WRAP
9093 && (op & MOVE_TO_X))
9094 {
9095 struct it save_it;
9096 void *save_data = NULL;
9097 int skip;
9098
9099 SAVE_IT (save_it, *it, save_data);
9100 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9101 /* When word-wrap is on, TO_X may lie past the end
9102 of a wrapped line. Then it->current is the
9103 character on the next line, so backtrack to the
9104 space before the wrap point. */
9105 if (skip == MOVE_LINE_CONTINUED)
9106 {
9107 int prev_x = max (it->current_x - 1, 0);
9108 RESTORE_IT (it, &save_it, save_data);
9109 move_it_in_display_line_to
9110 (it, -1, prev_x, MOVE_TO_X);
9111 }
9112 else
9113 bidi_unshelve_cache (save_data, true);
9114 }
9115 else
9116 move_it_in_display_line_to (it, to_charpos, to_x, op);
9117 }
9118
9119
9120 /* Move IT forward until it satisfies one or more of the criteria in
9121 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9122
9123 OP is a bit-mask that specifies where to stop, and in particular,
9124 which of those four position arguments makes a difference. See the
9125 description of enum move_operation_enum.
9126
9127 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9128 screen line, this function will set IT to the next position that is
9129 displayed to the right of TO_CHARPOS on the screen.
9130
9131 Return the maximum pixel length of any line scanned but never more
9132 than it.last_visible_x. */
9133
9134 int
9135 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9136 {
9137 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9138 int line_height, line_start_x = 0, reached = 0;
9139 int max_current_x = 0;
9140 void *backup_data = NULL;
9141
9142 for (;;)
9143 {
9144 if (op & MOVE_TO_VPOS)
9145 {
9146 /* If no TO_CHARPOS and no TO_X specified, stop at the
9147 start of the line TO_VPOS. */
9148 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9149 {
9150 if (it->vpos == to_vpos)
9151 {
9152 reached = 1;
9153 break;
9154 }
9155 else
9156 skip = move_it_in_display_line_to (it, -1, -1, 0);
9157 }
9158 else
9159 {
9160 /* TO_VPOS >= 0 means stop at TO_X in the line at
9161 TO_VPOS, or at TO_POS, whichever comes first. */
9162 if (it->vpos == to_vpos)
9163 {
9164 reached = 2;
9165 break;
9166 }
9167
9168 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9169
9170 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9171 {
9172 reached = 3;
9173 break;
9174 }
9175 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9176 {
9177 /* We have reached TO_X but not in the line we want. */
9178 skip = move_it_in_display_line_to (it, to_charpos,
9179 -1, MOVE_TO_POS);
9180 if (skip == MOVE_POS_MATCH_OR_ZV)
9181 {
9182 reached = 4;
9183 break;
9184 }
9185 }
9186 }
9187 }
9188 else if (op & MOVE_TO_Y)
9189 {
9190 struct it it_backup;
9191
9192 if (it->line_wrap == WORD_WRAP)
9193 SAVE_IT (it_backup, *it, backup_data);
9194
9195 /* TO_Y specified means stop at TO_X in the line containing
9196 TO_Y---or at TO_CHARPOS if this is reached first. The
9197 problem is that we can't really tell whether the line
9198 contains TO_Y before we have completely scanned it, and
9199 this may skip past TO_X. What we do is to first scan to
9200 TO_X.
9201
9202 If TO_X is not specified, use a TO_X of zero. The reason
9203 is to make the outcome of this function more predictable.
9204 If we didn't use TO_X == 0, we would stop at the end of
9205 the line which is probably not what a caller would expect
9206 to happen. */
9207 skip = move_it_in_display_line_to
9208 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9209 (MOVE_TO_X | (op & MOVE_TO_POS)));
9210
9211 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9212 if (skip == MOVE_POS_MATCH_OR_ZV)
9213 reached = 5;
9214 else if (skip == MOVE_X_REACHED)
9215 {
9216 /* If TO_X was reached, we want to know whether TO_Y is
9217 in the line. We know this is the case if the already
9218 scanned glyphs make the line tall enough. Otherwise,
9219 we must check by scanning the rest of the line. */
9220 line_height = it->max_ascent + it->max_descent;
9221 if (to_y >= it->current_y
9222 && to_y < it->current_y + line_height)
9223 {
9224 reached = 6;
9225 break;
9226 }
9227 SAVE_IT (it_backup, *it, backup_data);
9228 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9229 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9230 op & MOVE_TO_POS);
9231 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9232 line_height = it->max_ascent + it->max_descent;
9233 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9234
9235 if (to_y >= it->current_y
9236 && to_y < it->current_y + line_height)
9237 {
9238 /* If TO_Y is in this line and TO_X was reached
9239 above, we scanned too far. We have to restore
9240 IT's settings to the ones before skipping. But
9241 keep the more accurate values of max_ascent and
9242 max_descent we've found while skipping the rest
9243 of the line, for the sake of callers, such as
9244 pos_visible_p, that need to know the line
9245 height. */
9246 int max_ascent = it->max_ascent;
9247 int max_descent = it->max_descent;
9248
9249 RESTORE_IT (it, &it_backup, backup_data);
9250 it->max_ascent = max_ascent;
9251 it->max_descent = max_descent;
9252 reached = 6;
9253 }
9254 else
9255 {
9256 skip = skip2;
9257 if (skip == MOVE_POS_MATCH_OR_ZV)
9258 reached = 7;
9259 }
9260 }
9261 else
9262 {
9263 /* Check whether TO_Y is in this line. */
9264 line_height = it->max_ascent + it->max_descent;
9265 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9266
9267 if (to_y >= it->current_y
9268 && to_y < it->current_y + line_height)
9269 {
9270 if (to_y > it->current_y)
9271 max_current_x = max (it->current_x, max_current_x);
9272
9273 /* When word-wrap is on, TO_X may lie past the end
9274 of a wrapped line. Then it->current is the
9275 character on the next line, so backtrack to the
9276 space before the wrap point. */
9277 if (skip == MOVE_LINE_CONTINUED
9278 && it->line_wrap == WORD_WRAP)
9279 {
9280 int prev_x = max (it->current_x - 1, 0);
9281 RESTORE_IT (it, &it_backup, backup_data);
9282 skip = move_it_in_display_line_to
9283 (it, -1, prev_x, MOVE_TO_X);
9284 }
9285
9286 reached = 6;
9287 }
9288 }
9289
9290 if (reached)
9291 {
9292 max_current_x = max (it->current_x, max_current_x);
9293 break;
9294 }
9295 }
9296 else if (BUFFERP (it->object)
9297 && (it->method == GET_FROM_BUFFER
9298 || it->method == GET_FROM_STRETCH)
9299 && IT_CHARPOS (*it) >= to_charpos
9300 /* Under bidi iteration, a call to set_iterator_to_next
9301 can scan far beyond to_charpos if the initial
9302 portion of the next line needs to be reordered. In
9303 that case, give move_it_in_display_line_to another
9304 chance below. */
9305 && !(it->bidi_p
9306 && it->bidi_it.scan_dir == -1))
9307 skip = MOVE_POS_MATCH_OR_ZV;
9308 else
9309 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9310
9311 switch (skip)
9312 {
9313 case MOVE_POS_MATCH_OR_ZV:
9314 max_current_x = max (it->current_x, max_current_x);
9315 reached = 8;
9316 goto out;
9317
9318 case MOVE_NEWLINE_OR_CR:
9319 max_current_x = max (it->current_x, max_current_x);
9320 set_iterator_to_next (it, true);
9321 it->continuation_lines_width = 0;
9322 break;
9323
9324 case MOVE_LINE_TRUNCATED:
9325 max_current_x = it->last_visible_x;
9326 it->continuation_lines_width = 0;
9327 reseat_at_next_visible_line_start (it, false);
9328 if ((op & MOVE_TO_POS) != 0
9329 && IT_CHARPOS (*it) > to_charpos)
9330 {
9331 reached = 9;
9332 goto out;
9333 }
9334 break;
9335
9336 case MOVE_LINE_CONTINUED:
9337 max_current_x = it->last_visible_x;
9338 /* For continued lines ending in a tab, some of the glyphs
9339 associated with the tab are displayed on the current
9340 line. Since it->current_x does not include these glyphs,
9341 we use it->last_visible_x instead. */
9342 if (it->c == '\t')
9343 {
9344 it->continuation_lines_width += it->last_visible_x;
9345 /* When moving by vpos, ensure that the iterator really
9346 advances to the next line (bug#847, bug#969). Fixme:
9347 do we need to do this in other circumstances? */
9348 if (it->current_x != it->last_visible_x
9349 && (op & MOVE_TO_VPOS)
9350 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9351 {
9352 line_start_x = it->current_x + it->pixel_width
9353 - it->last_visible_x;
9354 if (FRAME_WINDOW_P (it->f))
9355 {
9356 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9357 struct font *face_font = face->font;
9358
9359 /* When display_line produces a continued line
9360 that ends in a TAB, it skips a tab stop that
9361 is closer than the font's space character
9362 width (see x_produce_glyphs where it produces
9363 the stretch glyph which represents a TAB).
9364 We need to reproduce the same logic here. */
9365 eassert (face_font);
9366 if (face_font)
9367 {
9368 if (line_start_x < face_font->space_width)
9369 line_start_x
9370 += it->tab_width * face_font->space_width;
9371 }
9372 }
9373 set_iterator_to_next (it, false);
9374 }
9375 }
9376 else
9377 it->continuation_lines_width += it->current_x;
9378 break;
9379
9380 default:
9381 emacs_abort ();
9382 }
9383
9384 /* Reset/increment for the next run. */
9385 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9386 it->current_x = line_start_x;
9387 line_start_x = 0;
9388 it->hpos = 0;
9389 it->current_y += it->max_ascent + it->max_descent;
9390 ++it->vpos;
9391 last_height = it->max_ascent + it->max_descent;
9392 it->max_ascent = it->max_descent = 0;
9393 }
9394
9395 out:
9396
9397 /* On text terminals, we may stop at the end of a line in the middle
9398 of a multi-character glyph. If the glyph itself is continued,
9399 i.e. it is actually displayed on the next line, don't treat this
9400 stopping point as valid; move to the next line instead (unless
9401 that brings us offscreen). */
9402 if (!FRAME_WINDOW_P (it->f)
9403 && op & MOVE_TO_POS
9404 && IT_CHARPOS (*it) == to_charpos
9405 && it->what == IT_CHARACTER
9406 && it->nglyphs > 1
9407 && it->line_wrap == WINDOW_WRAP
9408 && it->current_x == it->last_visible_x - 1
9409 && it->c != '\n'
9410 && it->c != '\t'
9411 && it->w->window_end_valid
9412 && it->vpos < it->w->window_end_vpos)
9413 {
9414 it->continuation_lines_width += it->current_x;
9415 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9416 it->current_y += it->max_ascent + it->max_descent;
9417 ++it->vpos;
9418 last_height = it->max_ascent + it->max_descent;
9419 }
9420
9421 if (backup_data)
9422 bidi_unshelve_cache (backup_data, true);
9423
9424 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9425
9426 return max_current_x;
9427 }
9428
9429
9430 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9431
9432 If DY > 0, move IT backward at least that many pixels. DY = 0
9433 means move IT backward to the preceding line start or BEGV. This
9434 function may move over more than DY pixels if IT->current_y - DY
9435 ends up in the middle of a line; in this case IT->current_y will be
9436 set to the top of the line moved to. */
9437
9438 void
9439 move_it_vertically_backward (struct it *it, int dy)
9440 {
9441 int nlines, h;
9442 struct it it2, it3;
9443 void *it2data = NULL, *it3data = NULL;
9444 ptrdiff_t start_pos;
9445 int nchars_per_row
9446 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9447 ptrdiff_t pos_limit;
9448
9449 move_further_back:
9450 eassert (dy >= 0);
9451
9452 start_pos = IT_CHARPOS (*it);
9453
9454 /* Estimate how many newlines we must move back. */
9455 nlines = max (1, dy / default_line_pixel_height (it->w));
9456 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9457 pos_limit = BEGV;
9458 else
9459 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9460
9461 /* Set the iterator's position that many lines back. But don't go
9462 back more than NLINES full screen lines -- this wins a day with
9463 buffers which have very long lines. */
9464 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9465 back_to_previous_visible_line_start (it);
9466
9467 /* Reseat the iterator here. When moving backward, we don't want
9468 reseat to skip forward over invisible text, set up the iterator
9469 to deliver from overlay strings at the new position etc. So,
9470 use reseat_1 here. */
9471 reseat_1 (it, it->current.pos, true);
9472
9473 /* We are now surely at a line start. */
9474 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9475 reordering is in effect. */
9476 it->continuation_lines_width = 0;
9477
9478 /* Move forward and see what y-distance we moved. First move to the
9479 start of the next line so that we get its height. We need this
9480 height to be able to tell whether we reached the specified
9481 y-distance. */
9482 SAVE_IT (it2, *it, it2data);
9483 it2.max_ascent = it2.max_descent = 0;
9484 do
9485 {
9486 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9487 MOVE_TO_POS | MOVE_TO_VPOS);
9488 }
9489 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9490 /* If we are in a display string which starts at START_POS,
9491 and that display string includes a newline, and we are
9492 right after that newline (i.e. at the beginning of a
9493 display line), exit the loop, because otherwise we will
9494 infloop, since move_it_to will see that it is already at
9495 START_POS and will not move. */
9496 || (it2.method == GET_FROM_STRING
9497 && IT_CHARPOS (it2) == start_pos
9498 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9499 eassert (IT_CHARPOS (*it) >= BEGV);
9500 SAVE_IT (it3, it2, it3data);
9501
9502 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9503 eassert (IT_CHARPOS (*it) >= BEGV);
9504 /* H is the actual vertical distance from the position in *IT
9505 and the starting position. */
9506 h = it2.current_y - it->current_y;
9507 /* NLINES is the distance in number of lines. */
9508 nlines = it2.vpos - it->vpos;
9509
9510 /* Correct IT's y and vpos position
9511 so that they are relative to the starting point. */
9512 it->vpos -= nlines;
9513 it->current_y -= h;
9514
9515 if (dy == 0)
9516 {
9517 /* DY == 0 means move to the start of the screen line. The
9518 value of nlines is > 0 if continuation lines were involved,
9519 or if the original IT position was at start of a line. */
9520 RESTORE_IT (it, it, it2data);
9521 if (nlines > 0)
9522 move_it_by_lines (it, nlines);
9523 /* The above code moves us to some position NLINES down,
9524 usually to its first glyph (leftmost in an L2R line), but
9525 that's not necessarily the start of the line, under bidi
9526 reordering. We want to get to the character position
9527 that is immediately after the newline of the previous
9528 line. */
9529 if (it->bidi_p
9530 && !it->continuation_lines_width
9531 && !STRINGP (it->string)
9532 && IT_CHARPOS (*it) > BEGV
9533 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9534 {
9535 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9536
9537 DEC_BOTH (cp, bp);
9538 cp = find_newline_no_quit (cp, bp, -1, NULL);
9539 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9540 }
9541 bidi_unshelve_cache (it3data, true);
9542 }
9543 else
9544 {
9545 /* The y-position we try to reach, relative to *IT.
9546 Note that H has been subtracted in front of the if-statement. */
9547 int target_y = it->current_y + h - dy;
9548 int y0 = it3.current_y;
9549 int y1;
9550 int line_height;
9551
9552 RESTORE_IT (&it3, &it3, it3data);
9553 y1 = line_bottom_y (&it3);
9554 line_height = y1 - y0;
9555 RESTORE_IT (it, it, it2data);
9556 /* If we did not reach target_y, try to move further backward if
9557 we can. If we moved too far backward, try to move forward. */
9558 if (target_y < it->current_y
9559 /* This is heuristic. In a window that's 3 lines high, with
9560 a line height of 13 pixels each, recentering with point
9561 on the bottom line will try to move -39/2 = 19 pixels
9562 backward. Try to avoid moving into the first line. */
9563 && (it->current_y - target_y
9564 > min (window_box_height (it->w), line_height * 2 / 3))
9565 && IT_CHARPOS (*it) > BEGV)
9566 {
9567 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9568 target_y - it->current_y));
9569 dy = it->current_y - target_y;
9570 goto move_further_back;
9571 }
9572 else if (target_y >= it->current_y + line_height
9573 && IT_CHARPOS (*it) < ZV)
9574 {
9575 /* Should move forward by at least one line, maybe more.
9576
9577 Note: Calling move_it_by_lines can be expensive on
9578 terminal frames, where compute_motion is used (via
9579 vmotion) to do the job, when there are very long lines
9580 and truncate-lines is nil. That's the reason for
9581 treating terminal frames specially here. */
9582
9583 if (!FRAME_WINDOW_P (it->f))
9584 move_it_vertically (it, target_y - it->current_y);
9585 else
9586 {
9587 do
9588 {
9589 move_it_by_lines (it, 1);
9590 }
9591 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9592 }
9593 }
9594 }
9595 }
9596
9597
9598 /* Move IT by a specified amount of pixel lines DY. DY negative means
9599 move backwards. DY = 0 means move to start of screen line. At the
9600 end, IT will be on the start of a screen line. */
9601
9602 void
9603 move_it_vertically (struct it *it, int dy)
9604 {
9605 if (dy <= 0)
9606 move_it_vertically_backward (it, -dy);
9607 else
9608 {
9609 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9610 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9611 MOVE_TO_POS | MOVE_TO_Y);
9612 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9613
9614 /* If buffer ends in ZV without a newline, move to the start of
9615 the line to satisfy the post-condition. */
9616 if (IT_CHARPOS (*it) == ZV
9617 && ZV > BEGV
9618 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9619 move_it_by_lines (it, 0);
9620 }
9621 }
9622
9623
9624 /* Move iterator IT past the end of the text line it is in. */
9625
9626 void
9627 move_it_past_eol (struct it *it)
9628 {
9629 enum move_it_result rc;
9630
9631 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9632 if (rc == MOVE_NEWLINE_OR_CR)
9633 set_iterator_to_next (it, false);
9634 }
9635
9636
9637 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9638 negative means move up. DVPOS == 0 means move to the start of the
9639 screen line.
9640
9641 Optimization idea: If we would know that IT->f doesn't use
9642 a face with proportional font, we could be faster for
9643 truncate-lines nil. */
9644
9645 void
9646 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9647 {
9648
9649 /* The commented-out optimization uses vmotion on terminals. This
9650 gives bad results, because elements like it->what, on which
9651 callers such as pos_visible_p rely, aren't updated. */
9652 /* struct position pos;
9653 if (!FRAME_WINDOW_P (it->f))
9654 {
9655 struct text_pos textpos;
9656
9657 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9658 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9659 reseat (it, textpos, true);
9660 it->vpos += pos.vpos;
9661 it->current_y += pos.vpos;
9662 }
9663 else */
9664
9665 if (dvpos == 0)
9666 {
9667 /* DVPOS == 0 means move to the start of the screen line. */
9668 move_it_vertically_backward (it, 0);
9669 /* Let next call to line_bottom_y calculate real line height. */
9670 last_height = 0;
9671 }
9672 else if (dvpos > 0)
9673 {
9674 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9675 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9676 {
9677 /* Only move to the next buffer position if we ended up in a
9678 string from display property, not in an overlay string
9679 (before-string or after-string). That is because the
9680 latter don't conceal the underlying buffer position, so
9681 we can ask to move the iterator to the exact position we
9682 are interested in. Note that, even if we are already at
9683 IT_CHARPOS (*it), the call below is not a no-op, as it
9684 will detect that we are at the end of the string, pop the
9685 iterator, and compute it->current_x and it->hpos
9686 correctly. */
9687 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9688 -1, -1, -1, MOVE_TO_POS);
9689 }
9690 }
9691 else
9692 {
9693 struct it it2;
9694 void *it2data = NULL;
9695 ptrdiff_t start_charpos, i;
9696 int nchars_per_row
9697 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9698 bool hit_pos_limit = false;
9699 ptrdiff_t pos_limit;
9700
9701 /* Start at the beginning of the screen line containing IT's
9702 position. This may actually move vertically backwards,
9703 in case of overlays, so adjust dvpos accordingly. */
9704 dvpos += it->vpos;
9705 move_it_vertically_backward (it, 0);
9706 dvpos -= it->vpos;
9707
9708 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9709 screen lines, and reseat the iterator there. */
9710 start_charpos = IT_CHARPOS (*it);
9711 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9712 pos_limit = BEGV;
9713 else
9714 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9715
9716 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9717 back_to_previous_visible_line_start (it);
9718 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9719 hit_pos_limit = true;
9720 reseat (it, it->current.pos, true);
9721
9722 /* Move further back if we end up in a string or an image. */
9723 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9724 {
9725 /* First try to move to start of display line. */
9726 dvpos += it->vpos;
9727 move_it_vertically_backward (it, 0);
9728 dvpos -= it->vpos;
9729 if (IT_POS_VALID_AFTER_MOVE_P (it))
9730 break;
9731 /* If start of line is still in string or image,
9732 move further back. */
9733 back_to_previous_visible_line_start (it);
9734 reseat (it, it->current.pos, true);
9735 dvpos--;
9736 }
9737
9738 it->current_x = it->hpos = 0;
9739
9740 /* Above call may have moved too far if continuation lines
9741 are involved. Scan forward and see if it did. */
9742 SAVE_IT (it2, *it, it2data);
9743 it2.vpos = it2.current_y = 0;
9744 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9745 it->vpos -= it2.vpos;
9746 it->current_y -= it2.current_y;
9747 it->current_x = it->hpos = 0;
9748
9749 /* If we moved too far back, move IT some lines forward. */
9750 if (it2.vpos > -dvpos)
9751 {
9752 int delta = it2.vpos + dvpos;
9753
9754 RESTORE_IT (&it2, &it2, it2data);
9755 SAVE_IT (it2, *it, it2data);
9756 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9757 /* Move back again if we got too far ahead. */
9758 if (IT_CHARPOS (*it) >= start_charpos)
9759 RESTORE_IT (it, &it2, it2data);
9760 else
9761 bidi_unshelve_cache (it2data, true);
9762 }
9763 else if (hit_pos_limit && pos_limit > BEGV
9764 && dvpos < 0 && it2.vpos < -dvpos)
9765 {
9766 /* If we hit the limit, but still didn't make it far enough
9767 back, that means there's a display string with a newline
9768 covering a large chunk of text, and that caused
9769 back_to_previous_visible_line_start try to go too far.
9770 Punish those who commit such atrocities by going back
9771 until we've reached DVPOS, after lifting the limit, which
9772 could make it slow for very long lines. "If it hurts,
9773 don't do that!" */
9774 dvpos += it2.vpos;
9775 RESTORE_IT (it, it, it2data);
9776 for (i = -dvpos; i > 0; --i)
9777 {
9778 back_to_previous_visible_line_start (it);
9779 it->vpos--;
9780 }
9781 reseat_1 (it, it->current.pos, true);
9782 }
9783 else
9784 RESTORE_IT (it, it, it2data);
9785 }
9786 }
9787
9788 /* Return true if IT points into the middle of a display vector. */
9789
9790 bool
9791 in_display_vector_p (struct it *it)
9792 {
9793 return (it->method == GET_FROM_DISPLAY_VECTOR
9794 && it->current.dpvec_index > 0
9795 && it->dpvec + it->current.dpvec_index != it->dpend);
9796 }
9797
9798 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9799 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9800 WINDOW must be a live window and defaults to the selected one. The
9801 return value is a cons of the maximum pixel-width of any text line and
9802 the maximum pixel-height of all text lines.
9803
9804 The optional argument FROM, if non-nil, specifies the first text
9805 position and defaults to the minimum accessible position of the buffer.
9806 If FROM is t, use the minimum accessible position that is not a newline
9807 character. TO, if non-nil, specifies the last text position and
9808 defaults to the maximum accessible position of the buffer. If TO is t,
9809 use the maximum accessible position that is not a newline character.
9810
9811 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9812 width that can be returned. X-LIMIT nil or omitted, means to use the
9813 pixel-width of WINDOW's body; use this if you do not intend to change
9814 the width of WINDOW. Use the maximum width WINDOW may assume if you
9815 intend to change WINDOW's width. In any case, text whose x-coordinate
9816 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9817 can take some time, it's always a good idea to make this argument as
9818 small as possible; in particular, if the buffer contains long lines that
9819 shall be truncated anyway.
9820
9821 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9822 height that can be returned. Text lines whose y-coordinate is beyond
9823 Y-LIMIT are ignored. Since calculating the text height of a large
9824 buffer can take some time, it makes sense to specify this argument if
9825 the size of the buffer is unknown.
9826
9827 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9828 include the height of the mode- or header-line of WINDOW in the return
9829 value. If it is either the symbol `mode-line' or `header-line', include
9830 only the height of that line, if present, in the return value. If t,
9831 include the height of both, if present, in the return value. */)
9832 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9833 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9834 {
9835 struct window *w = decode_live_window (window);
9836 Lisp_Object buffer = w->contents;
9837 struct buffer *b;
9838 struct it it;
9839 struct buffer *old_b = NULL;
9840 ptrdiff_t start, end, pos;
9841 struct text_pos startp;
9842 void *itdata = NULL;
9843 int c, max_y = -1, x = 0, y = 0;
9844
9845 CHECK_BUFFER (buffer);
9846 b = XBUFFER (buffer);
9847
9848 if (b != current_buffer)
9849 {
9850 old_b = current_buffer;
9851 set_buffer_internal (b);
9852 }
9853
9854 if (NILP (from))
9855 start = BEGV;
9856 else if (EQ (from, Qt))
9857 {
9858 start = pos = BEGV;
9859 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9860 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9861 start = pos;
9862 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9863 start = pos;
9864 }
9865 else
9866 {
9867 CHECK_NUMBER_COERCE_MARKER (from);
9868 start = min (max (XINT (from), BEGV), ZV);
9869 }
9870
9871 if (NILP (to))
9872 end = ZV;
9873 else if (EQ (to, Qt))
9874 {
9875 end = pos = ZV;
9876 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9877 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9878 end = pos;
9879 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9880 end = pos;
9881 }
9882 else
9883 {
9884 CHECK_NUMBER_COERCE_MARKER (to);
9885 end = max (start, min (XINT (to), ZV));
9886 }
9887
9888 if (!NILP (y_limit))
9889 {
9890 CHECK_NUMBER (y_limit);
9891 max_y = min (XINT (y_limit), INT_MAX);
9892 }
9893
9894 itdata = bidi_shelve_cache ();
9895 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9896 start_display (&it, w, startp);
9897
9898 if (NILP (x_limit))
9899 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9900 else
9901 {
9902 CHECK_NUMBER (x_limit);
9903 it.last_visible_x = min (XINT (x_limit), INFINITY);
9904 /* Actually, we never want move_it_to stop at to_x. But to make
9905 sure that move_it_in_display_line_to always moves far enough,
9906 we set it to INT_MAX and specify MOVE_TO_X. */
9907 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9908 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9909 }
9910
9911 y = it.current_y + it.max_ascent + it.max_descent;
9912
9913 if (!EQ (mode_and_header_line, Qheader_line)
9914 && !EQ (mode_and_header_line, Qt))
9915 /* Do not count the header-line which was counted automatically by
9916 start_display. */
9917 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9918
9919 if (EQ (mode_and_header_line, Qmode_line)
9920 || EQ (mode_and_header_line, Qt))
9921 /* Do count the mode-line which is not included automatically by
9922 start_display. */
9923 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9924
9925 bidi_unshelve_cache (itdata, false);
9926
9927 if (old_b)
9928 set_buffer_internal (old_b);
9929
9930 return Fcons (make_number (x), make_number (y));
9931 }
9932 \f
9933 /***********************************************************************
9934 Messages
9935 ***********************************************************************/
9936
9937 /* Return the number of arguments the format string FORMAT needs. */
9938
9939 static ptrdiff_t
9940 format_nargs (char const *format)
9941 {
9942 ptrdiff_t nargs = 0;
9943 for (char const *p = format; (p = strchr (p, '%')); p++)
9944 if (p[1] == '%')
9945 p++;
9946 else
9947 nargs++;
9948 return nargs;
9949 }
9950
9951 /* Add a message with format string FORMAT and formatted arguments
9952 to *Messages*. */
9953
9954 void
9955 add_to_log (const char *format, ...)
9956 {
9957 va_list ap;
9958 va_start (ap, format);
9959 vadd_to_log (format, ap);
9960 va_end (ap);
9961 }
9962
9963 void
9964 vadd_to_log (char const *format, va_list ap)
9965 {
9966 ptrdiff_t form_nargs = format_nargs (format);
9967 ptrdiff_t nargs = 1 + form_nargs;
9968 Lisp_Object args[10];
9969 eassert (nargs <= ARRAYELTS (args));
9970 AUTO_STRING (args0, format);
9971 args[0] = args0;
9972 for (ptrdiff_t i = 1; i <= nargs; i++)
9973 args[i] = va_arg (ap, Lisp_Object);
9974 Lisp_Object msg = Qnil;
9975 msg = Fformat_message (nargs, args);
9976
9977 ptrdiff_t len = SBYTES (msg) + 1;
9978 USE_SAFE_ALLOCA;
9979 char *buffer = SAFE_ALLOCA (len);
9980 memcpy (buffer, SDATA (msg), len);
9981
9982 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
9983 SAFE_FREE ();
9984 }
9985
9986
9987 /* Output a newline in the *Messages* buffer if "needs" one. */
9988
9989 void
9990 message_log_maybe_newline (void)
9991 {
9992 if (message_log_need_newline)
9993 message_dolog ("", 0, true, false);
9994 }
9995
9996
9997 /* Add a string M of length NBYTES to the message log, optionally
9998 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9999 true, means interpret the contents of M as multibyte. This
10000 function calls low-level routines in order to bypass text property
10001 hooks, etc. which might not be safe to run.
10002
10003 This may GC (insert may run before/after change hooks),
10004 so the buffer M must NOT point to a Lisp string. */
10005
10006 void
10007 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10008 {
10009 const unsigned char *msg = (const unsigned char *) m;
10010
10011 if (!NILP (Vmemory_full))
10012 return;
10013
10014 if (!NILP (Vmessage_log_max))
10015 {
10016 struct buffer *oldbuf;
10017 Lisp_Object oldpoint, oldbegv, oldzv;
10018 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10019 ptrdiff_t point_at_end = 0;
10020 ptrdiff_t zv_at_end = 0;
10021 Lisp_Object old_deactivate_mark;
10022
10023 old_deactivate_mark = Vdeactivate_mark;
10024 oldbuf = current_buffer;
10025
10026 /* Ensure the Messages buffer exists, and switch to it.
10027 If we created it, set the major-mode. */
10028 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10029 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10030 if (newbuffer
10031 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10032 call0 (intern ("messages-buffer-mode"));
10033
10034 bset_undo_list (current_buffer, Qt);
10035 bset_cache_long_scans (current_buffer, Qnil);
10036
10037 oldpoint = message_dolog_marker1;
10038 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10039 oldbegv = message_dolog_marker2;
10040 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10041 oldzv = message_dolog_marker3;
10042 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10043
10044 if (PT == Z)
10045 point_at_end = 1;
10046 if (ZV == Z)
10047 zv_at_end = 1;
10048
10049 BEGV = BEG;
10050 BEGV_BYTE = BEG_BYTE;
10051 ZV = Z;
10052 ZV_BYTE = Z_BYTE;
10053 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10054
10055 /* Insert the string--maybe converting multibyte to single byte
10056 or vice versa, so that all the text fits the buffer. */
10057 if (multibyte
10058 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10059 {
10060 ptrdiff_t i;
10061 int c, char_bytes;
10062 char work[1];
10063
10064 /* Convert a multibyte string to single-byte
10065 for the *Message* buffer. */
10066 for (i = 0; i < nbytes; i += char_bytes)
10067 {
10068 c = string_char_and_length (msg + i, &char_bytes);
10069 work[0] = CHAR_TO_BYTE8 (c);
10070 insert_1_both (work, 1, 1, true, false, false);
10071 }
10072 }
10073 else if (! multibyte
10074 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10075 {
10076 ptrdiff_t i;
10077 int c, char_bytes;
10078 unsigned char str[MAX_MULTIBYTE_LENGTH];
10079 /* Convert a single-byte string to multibyte
10080 for the *Message* buffer. */
10081 for (i = 0; i < nbytes; i++)
10082 {
10083 c = msg[i];
10084 MAKE_CHAR_MULTIBYTE (c);
10085 char_bytes = CHAR_STRING (c, str);
10086 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10087 }
10088 }
10089 else if (nbytes)
10090 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10091 true, false, false);
10092
10093 if (nlflag)
10094 {
10095 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10096 printmax_t dups;
10097
10098 insert_1_both ("\n", 1, 1, true, false, false);
10099
10100 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10101 this_bol = PT;
10102 this_bol_byte = PT_BYTE;
10103
10104 /* See if this line duplicates the previous one.
10105 If so, combine duplicates. */
10106 if (this_bol > BEG)
10107 {
10108 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10109 prev_bol = PT;
10110 prev_bol_byte = PT_BYTE;
10111
10112 dups = message_log_check_duplicate (prev_bol_byte,
10113 this_bol_byte);
10114 if (dups)
10115 {
10116 del_range_both (prev_bol, prev_bol_byte,
10117 this_bol, this_bol_byte, false);
10118 if (dups > 1)
10119 {
10120 char dupstr[sizeof " [ times]"
10121 + INT_STRLEN_BOUND (printmax_t)];
10122
10123 /* If you change this format, don't forget to also
10124 change message_log_check_duplicate. */
10125 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10126 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10127 insert_1_both (dupstr, duplen, duplen,
10128 true, false, true);
10129 }
10130 }
10131 }
10132
10133 /* If we have more than the desired maximum number of lines
10134 in the *Messages* buffer now, delete the oldest ones.
10135 This is safe because we don't have undo in this buffer. */
10136
10137 if (NATNUMP (Vmessage_log_max))
10138 {
10139 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10140 -XFASTINT (Vmessage_log_max) - 1, false);
10141 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10142 }
10143 }
10144 BEGV = marker_position (oldbegv);
10145 BEGV_BYTE = marker_byte_position (oldbegv);
10146
10147 if (zv_at_end)
10148 {
10149 ZV = Z;
10150 ZV_BYTE = Z_BYTE;
10151 }
10152 else
10153 {
10154 ZV = marker_position (oldzv);
10155 ZV_BYTE = marker_byte_position (oldzv);
10156 }
10157
10158 if (point_at_end)
10159 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10160 else
10161 /* We can't do Fgoto_char (oldpoint) because it will run some
10162 Lisp code. */
10163 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10164 marker_byte_position (oldpoint));
10165
10166 unchain_marker (XMARKER (oldpoint));
10167 unchain_marker (XMARKER (oldbegv));
10168 unchain_marker (XMARKER (oldzv));
10169
10170 /* We called insert_1_both above with its 5th argument (PREPARE)
10171 false, which prevents insert_1_both from calling
10172 prepare_to_modify_buffer, which in turns prevents us from
10173 incrementing windows_or_buffers_changed even if *Messages* is
10174 shown in some window. So we must manually set
10175 windows_or_buffers_changed here to make up for that. */
10176 windows_or_buffers_changed = old_windows_or_buffers_changed;
10177 bset_redisplay (current_buffer);
10178
10179 set_buffer_internal (oldbuf);
10180
10181 message_log_need_newline = !nlflag;
10182 Vdeactivate_mark = old_deactivate_mark;
10183 }
10184 }
10185
10186
10187 /* We are at the end of the buffer after just having inserted a newline.
10188 (Note: We depend on the fact we won't be crossing the gap.)
10189 Check to see if the most recent message looks a lot like the previous one.
10190 Return 0 if different, 1 if the new one should just replace it, or a
10191 value N > 1 if we should also append " [N times]". */
10192
10193 static intmax_t
10194 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10195 {
10196 ptrdiff_t i;
10197 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10198 bool seen_dots = false;
10199 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10200 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10201
10202 for (i = 0; i < len; i++)
10203 {
10204 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10205 seen_dots = true;
10206 if (p1[i] != p2[i])
10207 return seen_dots;
10208 }
10209 p1 += len;
10210 if (*p1 == '\n')
10211 return 2;
10212 if (*p1++ == ' ' && *p1++ == '[')
10213 {
10214 char *pend;
10215 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10216 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10217 return n + 1;
10218 }
10219 return 0;
10220 }
10221 \f
10222
10223 /* Display an echo area message M with a specified length of NBYTES
10224 bytes. The string may include null characters. If M is not a
10225 string, clear out any existing message, and let the mini-buffer
10226 text show through.
10227
10228 This function cancels echoing. */
10229
10230 void
10231 message3 (Lisp_Object m)
10232 {
10233 clear_message (true, true);
10234 cancel_echoing ();
10235
10236 /* First flush out any partial line written with print. */
10237 message_log_maybe_newline ();
10238 if (STRINGP (m))
10239 {
10240 ptrdiff_t nbytes = SBYTES (m);
10241 bool multibyte = STRING_MULTIBYTE (m);
10242 char *buffer;
10243 USE_SAFE_ALLOCA;
10244 SAFE_ALLOCA_STRING (buffer, m);
10245 message_dolog (buffer, nbytes, true, multibyte);
10246 SAFE_FREE ();
10247 }
10248 if (! inhibit_message)
10249 message3_nolog (m);
10250 }
10251
10252 /* Log the message M to stderr. Log an empty line if M is not a string. */
10253
10254 static void
10255 message_to_stderr (Lisp_Object m)
10256 {
10257 if (noninteractive_need_newline)
10258 {
10259 noninteractive_need_newline = false;
10260 fputc ('\n', stderr);
10261 }
10262 if (STRINGP (m))
10263 {
10264 Lisp_Object coding_system = Vlocale_coding_system;
10265 Lisp_Object s;
10266
10267 if (!NILP (Vcoding_system_for_write))
10268 coding_system = Vcoding_system_for_write;
10269 if (!NILP (coding_system))
10270 s = code_convert_string_norecord (m, coding_system, true);
10271 else
10272 s = m;
10273
10274 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10275 }
10276 if (!cursor_in_echo_area)
10277 fputc ('\n', stderr);
10278 fflush (stderr);
10279 }
10280
10281 /* The non-logging version of message3.
10282 This does not cancel echoing, because it is used for echoing.
10283 Perhaps we need to make a separate function for echoing
10284 and make this cancel echoing. */
10285
10286 void
10287 message3_nolog (Lisp_Object m)
10288 {
10289 struct frame *sf = SELECTED_FRAME ();
10290
10291 if (FRAME_INITIAL_P (sf))
10292 message_to_stderr (m);
10293 /* Error messages get reported properly by cmd_error, so this must be just an
10294 informative message; if the frame hasn't really been initialized yet, just
10295 toss it. */
10296 else if (INTERACTIVE && sf->glyphs_initialized_p)
10297 {
10298 /* Get the frame containing the mini-buffer
10299 that the selected frame is using. */
10300 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10301 Lisp_Object frame = XWINDOW (mini_window)->frame;
10302 struct frame *f = XFRAME (frame);
10303
10304 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10305 Fmake_frame_visible (frame);
10306
10307 if (STRINGP (m) && SCHARS (m) > 0)
10308 {
10309 set_message (m);
10310 if (minibuffer_auto_raise)
10311 Fraise_frame (frame);
10312 /* Assume we are not echoing.
10313 (If we are, echo_now will override this.) */
10314 echo_message_buffer = Qnil;
10315 }
10316 else
10317 clear_message (true, true);
10318
10319 do_pending_window_change (false);
10320 echo_area_display (true);
10321 do_pending_window_change (false);
10322 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10323 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10324 }
10325 }
10326
10327
10328 /* Display a null-terminated echo area message M. If M is 0, clear
10329 out any existing message, and let the mini-buffer text show through.
10330
10331 The buffer M must continue to exist until after the echo area gets
10332 cleared or some other message gets displayed there. Do not pass
10333 text that is stored in a Lisp string. Do not pass text in a buffer
10334 that was alloca'd. */
10335
10336 void
10337 message1 (const char *m)
10338 {
10339 message3 (m ? build_unibyte_string (m) : Qnil);
10340 }
10341
10342
10343 /* The non-logging counterpart of message1. */
10344
10345 void
10346 message1_nolog (const char *m)
10347 {
10348 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10349 }
10350
10351 /* Display a message M which contains a single %s
10352 which gets replaced with STRING. */
10353
10354 void
10355 message_with_string (const char *m, Lisp_Object string, bool log)
10356 {
10357 CHECK_STRING (string);
10358
10359 bool need_message;
10360 if (noninteractive)
10361 need_message = !!m;
10362 else if (!INTERACTIVE)
10363 need_message = false;
10364 else
10365 {
10366 /* The frame whose minibuffer we're going to display the message on.
10367 It may be larger than the selected frame, so we need
10368 to use its buffer, not the selected frame's buffer. */
10369 Lisp_Object mini_window;
10370 struct frame *f, *sf = SELECTED_FRAME ();
10371
10372 /* Get the frame containing the minibuffer
10373 that the selected frame is using. */
10374 mini_window = FRAME_MINIBUF_WINDOW (sf);
10375 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10376
10377 /* Error messages get reported properly by cmd_error, so this must be
10378 just an informative message; if the frame hasn't really been
10379 initialized yet, just toss it. */
10380 need_message = f->glyphs_initialized_p;
10381 }
10382
10383 if (need_message)
10384 {
10385 AUTO_STRING (fmt, m);
10386 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10387
10388 if (noninteractive)
10389 message_to_stderr (msg);
10390 else
10391 {
10392 if (log)
10393 message3 (msg);
10394 else
10395 message3_nolog (msg);
10396
10397 /* Print should start at the beginning of the message
10398 buffer next time. */
10399 message_buf_print = false;
10400 }
10401 }
10402 }
10403
10404
10405 /* Dump an informative message to the minibuf. If M is 0, clear out
10406 any existing message, and let the mini-buffer text show through.
10407
10408 The message must be safe ASCII and the format must not contain ` or
10409 '. If your message and format do not fit into this category,
10410 convert your arguments to Lisp objects and use Fmessage instead. */
10411
10412 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10413 vmessage (const char *m, va_list ap)
10414 {
10415 if (noninteractive)
10416 {
10417 if (m)
10418 {
10419 if (noninteractive_need_newline)
10420 putc ('\n', stderr);
10421 noninteractive_need_newline = false;
10422 vfprintf (stderr, m, ap);
10423 if (!cursor_in_echo_area)
10424 fprintf (stderr, "\n");
10425 fflush (stderr);
10426 }
10427 }
10428 else if (INTERACTIVE)
10429 {
10430 /* The frame whose mini-buffer we're going to display the message
10431 on. It may be larger than the selected frame, so we need to
10432 use its buffer, not the selected frame's buffer. */
10433 Lisp_Object mini_window;
10434 struct frame *f, *sf = SELECTED_FRAME ();
10435
10436 /* Get the frame containing the mini-buffer
10437 that the selected frame is using. */
10438 mini_window = FRAME_MINIBUF_WINDOW (sf);
10439 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10440
10441 /* Error messages get reported properly by cmd_error, so this must be
10442 just an informative message; if the frame hasn't really been
10443 initialized yet, just toss it. */
10444 if (f->glyphs_initialized_p)
10445 {
10446 if (m)
10447 {
10448 ptrdiff_t len;
10449 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10450 USE_SAFE_ALLOCA;
10451 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10452
10453 len = doprnt (message_buf, maxsize, m, 0, ap);
10454
10455 message3 (make_string (message_buf, len));
10456 SAFE_FREE ();
10457 }
10458 else
10459 message1 (0);
10460
10461 /* Print should start at the beginning of the message
10462 buffer next time. */
10463 message_buf_print = false;
10464 }
10465 }
10466 }
10467
10468 void
10469 message (const char *m, ...)
10470 {
10471 va_list ap;
10472 va_start (ap, m);
10473 vmessage (m, ap);
10474 va_end (ap);
10475 }
10476
10477
10478 /* Display the current message in the current mini-buffer. This is
10479 only called from error handlers in process.c, and is not time
10480 critical. */
10481
10482 void
10483 update_echo_area (void)
10484 {
10485 if (!NILP (echo_area_buffer[0]))
10486 {
10487 Lisp_Object string;
10488 string = Fcurrent_message ();
10489 message3 (string);
10490 }
10491 }
10492
10493
10494 /* Make sure echo area buffers in `echo_buffers' are live.
10495 If they aren't, make new ones. */
10496
10497 static void
10498 ensure_echo_area_buffers (void)
10499 {
10500 int i;
10501
10502 for (i = 0; i < 2; ++i)
10503 if (!BUFFERP (echo_buffer[i])
10504 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10505 {
10506 char name[30];
10507 Lisp_Object old_buffer;
10508 int j;
10509
10510 old_buffer = echo_buffer[i];
10511 echo_buffer[i] = Fget_buffer_create
10512 (make_formatted_string (name, " *Echo Area %d*", i));
10513 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10514 /* to force word wrap in echo area -
10515 it was decided to postpone this*/
10516 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10517
10518 for (j = 0; j < 2; ++j)
10519 if (EQ (old_buffer, echo_area_buffer[j]))
10520 echo_area_buffer[j] = echo_buffer[i];
10521 }
10522 }
10523
10524
10525 /* Call FN with args A1..A2 with either the current or last displayed
10526 echo_area_buffer as current buffer.
10527
10528 WHICH zero means use the current message buffer
10529 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10530 from echo_buffer[] and clear it.
10531
10532 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10533 suitable buffer from echo_buffer[] and clear it.
10534
10535 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10536 that the current message becomes the last displayed one, make
10537 choose a suitable buffer for echo_area_buffer[0], and clear it.
10538
10539 Value is what FN returns. */
10540
10541 static bool
10542 with_echo_area_buffer (struct window *w, int which,
10543 bool (*fn) (ptrdiff_t, Lisp_Object),
10544 ptrdiff_t a1, Lisp_Object a2)
10545 {
10546 Lisp_Object buffer;
10547 bool this_one, the_other, clear_buffer_p, rc;
10548 ptrdiff_t count = SPECPDL_INDEX ();
10549
10550 /* If buffers aren't live, make new ones. */
10551 ensure_echo_area_buffers ();
10552
10553 clear_buffer_p = false;
10554
10555 if (which == 0)
10556 this_one = false, the_other = true;
10557 else if (which > 0)
10558 this_one = true, the_other = false;
10559 else
10560 {
10561 this_one = false, the_other = true;
10562 clear_buffer_p = true;
10563
10564 /* We need a fresh one in case the current echo buffer equals
10565 the one containing the last displayed echo area message. */
10566 if (!NILP (echo_area_buffer[this_one])
10567 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10568 echo_area_buffer[this_one] = Qnil;
10569 }
10570
10571 /* Choose a suitable buffer from echo_buffer[] is we don't
10572 have one. */
10573 if (NILP (echo_area_buffer[this_one]))
10574 {
10575 echo_area_buffer[this_one]
10576 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10577 ? echo_buffer[the_other]
10578 : echo_buffer[this_one]);
10579 clear_buffer_p = true;
10580 }
10581
10582 buffer = echo_area_buffer[this_one];
10583
10584 /* Don't get confused by reusing the buffer used for echoing
10585 for a different purpose. */
10586 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10587 cancel_echoing ();
10588
10589 record_unwind_protect (unwind_with_echo_area_buffer,
10590 with_echo_area_buffer_unwind_data (w));
10591
10592 /* Make the echo area buffer current. Note that for display
10593 purposes, it is not necessary that the displayed window's buffer
10594 == current_buffer, except for text property lookup. So, let's
10595 only set that buffer temporarily here without doing a full
10596 Fset_window_buffer. We must also change w->pointm, though,
10597 because otherwise an assertions in unshow_buffer fails, and Emacs
10598 aborts. */
10599 set_buffer_internal_1 (XBUFFER (buffer));
10600 if (w)
10601 {
10602 wset_buffer (w, buffer);
10603 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10604 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10605 }
10606
10607 bset_undo_list (current_buffer, Qt);
10608 bset_read_only (current_buffer, Qnil);
10609 specbind (Qinhibit_read_only, Qt);
10610 specbind (Qinhibit_modification_hooks, Qt);
10611
10612 if (clear_buffer_p && Z > BEG)
10613 del_range (BEG, Z);
10614
10615 eassert (BEGV >= BEG);
10616 eassert (ZV <= Z && ZV >= BEGV);
10617
10618 rc = fn (a1, a2);
10619
10620 eassert (BEGV >= BEG);
10621 eassert (ZV <= Z && ZV >= BEGV);
10622
10623 unbind_to (count, Qnil);
10624 return rc;
10625 }
10626
10627
10628 /* Save state that should be preserved around the call to the function
10629 FN called in with_echo_area_buffer. */
10630
10631 static Lisp_Object
10632 with_echo_area_buffer_unwind_data (struct window *w)
10633 {
10634 int i = 0;
10635 Lisp_Object vector, tmp;
10636
10637 /* Reduce consing by keeping one vector in
10638 Vwith_echo_area_save_vector. */
10639 vector = Vwith_echo_area_save_vector;
10640 Vwith_echo_area_save_vector = Qnil;
10641
10642 if (NILP (vector))
10643 vector = Fmake_vector (make_number (11), Qnil);
10644
10645 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10646 ASET (vector, i, Vdeactivate_mark); ++i;
10647 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10648
10649 if (w)
10650 {
10651 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10652 ASET (vector, i, w->contents); ++i;
10653 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10654 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10655 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10656 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10657 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10658 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10659 }
10660 else
10661 {
10662 int end = i + 8;
10663 for (; i < end; ++i)
10664 ASET (vector, i, Qnil);
10665 }
10666
10667 eassert (i == ASIZE (vector));
10668 return vector;
10669 }
10670
10671
10672 /* Restore global state from VECTOR which was created by
10673 with_echo_area_buffer_unwind_data. */
10674
10675 static void
10676 unwind_with_echo_area_buffer (Lisp_Object vector)
10677 {
10678 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10679 Vdeactivate_mark = AREF (vector, 1);
10680 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10681
10682 if (WINDOWP (AREF (vector, 3)))
10683 {
10684 struct window *w;
10685 Lisp_Object buffer;
10686
10687 w = XWINDOW (AREF (vector, 3));
10688 buffer = AREF (vector, 4);
10689
10690 wset_buffer (w, buffer);
10691 set_marker_both (w->pointm, buffer,
10692 XFASTINT (AREF (vector, 5)),
10693 XFASTINT (AREF (vector, 6)));
10694 set_marker_both (w->old_pointm, buffer,
10695 XFASTINT (AREF (vector, 7)),
10696 XFASTINT (AREF (vector, 8)));
10697 set_marker_both (w->start, buffer,
10698 XFASTINT (AREF (vector, 9)),
10699 XFASTINT (AREF (vector, 10)));
10700 }
10701
10702 Vwith_echo_area_save_vector = vector;
10703 }
10704
10705
10706 /* Set up the echo area for use by print functions. MULTIBYTE_P
10707 means we will print multibyte. */
10708
10709 void
10710 setup_echo_area_for_printing (bool multibyte_p)
10711 {
10712 /* If we can't find an echo area any more, exit. */
10713 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10714 Fkill_emacs (Qnil);
10715
10716 ensure_echo_area_buffers ();
10717
10718 if (!message_buf_print)
10719 {
10720 /* A message has been output since the last time we printed.
10721 Choose a fresh echo area buffer. */
10722 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10723 echo_area_buffer[0] = echo_buffer[1];
10724 else
10725 echo_area_buffer[0] = echo_buffer[0];
10726
10727 /* Switch to that buffer and clear it. */
10728 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10729 bset_truncate_lines (current_buffer, Qnil);
10730
10731 if (Z > BEG)
10732 {
10733 ptrdiff_t count = SPECPDL_INDEX ();
10734 specbind (Qinhibit_read_only, Qt);
10735 /* Note that undo recording is always disabled. */
10736 del_range (BEG, Z);
10737 unbind_to (count, Qnil);
10738 }
10739 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10740
10741 /* Set up the buffer for the multibyteness we need. */
10742 if (multibyte_p
10743 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10744 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10745
10746 /* Raise the frame containing the echo area. */
10747 if (minibuffer_auto_raise)
10748 {
10749 struct frame *sf = SELECTED_FRAME ();
10750 Lisp_Object mini_window;
10751 mini_window = FRAME_MINIBUF_WINDOW (sf);
10752 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10753 }
10754
10755 message_log_maybe_newline ();
10756 message_buf_print = true;
10757 }
10758 else
10759 {
10760 if (NILP (echo_area_buffer[0]))
10761 {
10762 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10763 echo_area_buffer[0] = echo_buffer[1];
10764 else
10765 echo_area_buffer[0] = echo_buffer[0];
10766 }
10767
10768 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10769 {
10770 /* Someone switched buffers between print requests. */
10771 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10772 bset_truncate_lines (current_buffer, Qnil);
10773 }
10774 }
10775 }
10776
10777
10778 /* Display an echo area message in window W. Value is true if W's
10779 height is changed. If display_last_displayed_message_p,
10780 display the message that was last displayed, otherwise
10781 display the current message. */
10782
10783 static bool
10784 display_echo_area (struct window *w)
10785 {
10786 bool no_message_p, window_height_changed_p;
10787
10788 /* Temporarily disable garbage collections while displaying the echo
10789 area. This is done because a GC can print a message itself.
10790 That message would modify the echo area buffer's contents while a
10791 redisplay of the buffer is going on, and seriously confuse
10792 redisplay. */
10793 ptrdiff_t count = inhibit_garbage_collection ();
10794
10795 /* If there is no message, we must call display_echo_area_1
10796 nevertheless because it resizes the window. But we will have to
10797 reset the echo_area_buffer in question to nil at the end because
10798 with_echo_area_buffer will sets it to an empty buffer. */
10799 bool i = display_last_displayed_message_p;
10800 /* According to the C99, C11 and C++11 standards, the integral value
10801 of a "bool" is always 0 or 1, so this array access is safe here,
10802 if oddly typed. */
10803 no_message_p = NILP (echo_area_buffer[i]);
10804
10805 window_height_changed_p
10806 = with_echo_area_buffer (w, display_last_displayed_message_p,
10807 display_echo_area_1,
10808 (intptr_t) w, Qnil);
10809
10810 if (no_message_p)
10811 echo_area_buffer[i] = Qnil;
10812
10813 unbind_to (count, Qnil);
10814 return window_height_changed_p;
10815 }
10816
10817
10818 /* Helper for display_echo_area. Display the current buffer which
10819 contains the current echo area message in window W, a mini-window,
10820 a pointer to which is passed in A1. A2..A4 are currently not used.
10821 Change the height of W so that all of the message is displayed.
10822 Value is true if height of W was changed. */
10823
10824 static bool
10825 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10826 {
10827 intptr_t i1 = a1;
10828 struct window *w = (struct window *) i1;
10829 Lisp_Object window;
10830 struct text_pos start;
10831
10832 /* We are about to enter redisplay without going through
10833 redisplay_internal, so we need to forget these faces by hand
10834 here. */
10835 forget_escape_and_glyphless_faces ();
10836
10837 /* Do this before displaying, so that we have a large enough glyph
10838 matrix for the display. If we can't get enough space for the
10839 whole text, display the last N lines. That works by setting w->start. */
10840 bool window_height_changed_p = resize_mini_window (w, false);
10841
10842 /* Use the starting position chosen by resize_mini_window. */
10843 SET_TEXT_POS_FROM_MARKER (start, w->start);
10844
10845 /* Display. */
10846 clear_glyph_matrix (w->desired_matrix);
10847 XSETWINDOW (window, w);
10848 try_window (window, start, 0);
10849
10850 return window_height_changed_p;
10851 }
10852
10853
10854 /* Resize the echo area window to exactly the size needed for the
10855 currently displayed message, if there is one. If a mini-buffer
10856 is active, don't shrink it. */
10857
10858 void
10859 resize_echo_area_exactly (void)
10860 {
10861 if (BUFFERP (echo_area_buffer[0])
10862 && WINDOWP (echo_area_window))
10863 {
10864 struct window *w = XWINDOW (echo_area_window);
10865 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10866 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10867 (intptr_t) w, resize_exactly);
10868 if (resized_p)
10869 {
10870 windows_or_buffers_changed = 42;
10871 update_mode_lines = 30;
10872 redisplay_internal ();
10873 }
10874 }
10875 }
10876
10877
10878 /* Callback function for with_echo_area_buffer, when used from
10879 resize_echo_area_exactly. A1 contains a pointer to the window to
10880 resize, EXACTLY non-nil means resize the mini-window exactly to the
10881 size of the text displayed. A3 and A4 are not used. Value is what
10882 resize_mini_window returns. */
10883
10884 static bool
10885 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10886 {
10887 intptr_t i1 = a1;
10888 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10889 }
10890
10891
10892 /* Resize mini-window W to fit the size of its contents. EXACT_P
10893 means size the window exactly to the size needed. Otherwise, it's
10894 only enlarged until W's buffer is empty.
10895
10896 Set W->start to the right place to begin display. If the whole
10897 contents fit, start at the beginning. Otherwise, start so as
10898 to make the end of the contents appear. This is particularly
10899 important for y-or-n-p, but seems desirable generally.
10900
10901 Value is true if the window height has been changed. */
10902
10903 bool
10904 resize_mini_window (struct window *w, bool exact_p)
10905 {
10906 struct frame *f = XFRAME (w->frame);
10907 bool window_height_changed_p = false;
10908
10909 eassert (MINI_WINDOW_P (w));
10910
10911 /* By default, start display at the beginning. */
10912 set_marker_both (w->start, w->contents,
10913 BUF_BEGV (XBUFFER (w->contents)),
10914 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10915
10916 /* Don't resize windows while redisplaying a window; it would
10917 confuse redisplay functions when the size of the window they are
10918 displaying changes from under them. Such a resizing can happen,
10919 for instance, when which-func prints a long message while
10920 we are running fontification-functions. We're running these
10921 functions with safe_call which binds inhibit-redisplay to t. */
10922 if (!NILP (Vinhibit_redisplay))
10923 return false;
10924
10925 /* Nil means don't try to resize. */
10926 if (NILP (Vresize_mini_windows)
10927 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10928 return false;
10929
10930 if (!FRAME_MINIBUF_ONLY_P (f))
10931 {
10932 struct it it;
10933 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10934 + WINDOW_PIXEL_HEIGHT (w));
10935 int unit = FRAME_LINE_HEIGHT (f);
10936 int height, max_height;
10937 struct text_pos start;
10938 struct buffer *old_current_buffer = NULL;
10939
10940 if (current_buffer != XBUFFER (w->contents))
10941 {
10942 old_current_buffer = current_buffer;
10943 set_buffer_internal (XBUFFER (w->contents));
10944 }
10945
10946 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10947
10948 /* Compute the max. number of lines specified by the user. */
10949 if (FLOATP (Vmax_mini_window_height))
10950 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10951 else if (INTEGERP (Vmax_mini_window_height))
10952 max_height = XINT (Vmax_mini_window_height) * unit;
10953 else
10954 max_height = total_height / 4;
10955
10956 /* Correct that max. height if it's bogus. */
10957 max_height = clip_to_bounds (unit, max_height, total_height);
10958
10959 /* Find out the height of the text in the window. */
10960 if (it.line_wrap == TRUNCATE)
10961 height = unit;
10962 else
10963 {
10964 last_height = 0;
10965 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10966 if (it.max_ascent == 0 && it.max_descent == 0)
10967 height = it.current_y + last_height;
10968 else
10969 height = it.current_y + it.max_ascent + it.max_descent;
10970 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10971 }
10972
10973 /* Compute a suitable window start. */
10974 if (height > max_height)
10975 {
10976 height = (max_height / unit) * unit;
10977 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10978 move_it_vertically_backward (&it, height - unit);
10979 start = it.current.pos;
10980 }
10981 else
10982 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10983 SET_MARKER_FROM_TEXT_POS (w->start, start);
10984
10985 if (EQ (Vresize_mini_windows, Qgrow_only))
10986 {
10987 /* Let it grow only, until we display an empty message, in which
10988 case the window shrinks again. */
10989 if (height > WINDOW_PIXEL_HEIGHT (w))
10990 {
10991 int old_height = WINDOW_PIXEL_HEIGHT (w);
10992
10993 FRAME_WINDOWS_FROZEN (f) = true;
10994 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10995 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10996 }
10997 else if (height < WINDOW_PIXEL_HEIGHT (w)
10998 && (exact_p || BEGV == ZV))
10999 {
11000 int old_height = WINDOW_PIXEL_HEIGHT (w);
11001
11002 FRAME_WINDOWS_FROZEN (f) = false;
11003 shrink_mini_window (w, true);
11004 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11005 }
11006 }
11007 else
11008 {
11009 /* Always resize to exact size needed. */
11010 if (height > WINDOW_PIXEL_HEIGHT (w))
11011 {
11012 int old_height = WINDOW_PIXEL_HEIGHT (w);
11013
11014 FRAME_WINDOWS_FROZEN (f) = true;
11015 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11016 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11017 }
11018 else if (height < WINDOW_PIXEL_HEIGHT (w))
11019 {
11020 int old_height = WINDOW_PIXEL_HEIGHT (w);
11021
11022 FRAME_WINDOWS_FROZEN (f) = false;
11023 shrink_mini_window (w, true);
11024
11025 if (height)
11026 {
11027 FRAME_WINDOWS_FROZEN (f) = true;
11028 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11029 }
11030
11031 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11032 }
11033 }
11034
11035 if (old_current_buffer)
11036 set_buffer_internal (old_current_buffer);
11037 }
11038
11039 return window_height_changed_p;
11040 }
11041
11042
11043 /* Value is the current message, a string, or nil if there is no
11044 current message. */
11045
11046 Lisp_Object
11047 current_message (void)
11048 {
11049 Lisp_Object msg;
11050
11051 if (!BUFFERP (echo_area_buffer[0]))
11052 msg = Qnil;
11053 else
11054 {
11055 with_echo_area_buffer (0, 0, current_message_1,
11056 (intptr_t) &msg, Qnil);
11057 if (NILP (msg))
11058 echo_area_buffer[0] = Qnil;
11059 }
11060
11061 return msg;
11062 }
11063
11064
11065 static bool
11066 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11067 {
11068 intptr_t i1 = a1;
11069 Lisp_Object *msg = (Lisp_Object *) i1;
11070
11071 if (Z > BEG)
11072 *msg = make_buffer_string (BEG, Z, true);
11073 else
11074 *msg = Qnil;
11075 return false;
11076 }
11077
11078
11079 /* Push the current message on Vmessage_stack for later restoration
11080 by restore_message. Value is true if the current message isn't
11081 empty. This is a relatively infrequent operation, so it's not
11082 worth optimizing. */
11083
11084 bool
11085 push_message (void)
11086 {
11087 Lisp_Object msg = current_message ();
11088 Vmessage_stack = Fcons (msg, Vmessage_stack);
11089 return STRINGP (msg);
11090 }
11091
11092
11093 /* Restore message display from the top of Vmessage_stack. */
11094
11095 void
11096 restore_message (void)
11097 {
11098 eassert (CONSP (Vmessage_stack));
11099 message3_nolog (XCAR (Vmessage_stack));
11100 }
11101
11102
11103 /* Handler for unwind-protect calling pop_message. */
11104
11105 void
11106 pop_message_unwind (void)
11107 {
11108 /* Pop the top-most entry off Vmessage_stack. */
11109 eassert (CONSP (Vmessage_stack));
11110 Vmessage_stack = XCDR (Vmessage_stack);
11111 }
11112
11113
11114 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11115 exits. If the stack is not empty, we have a missing pop_message
11116 somewhere. */
11117
11118 void
11119 check_message_stack (void)
11120 {
11121 if (!NILP (Vmessage_stack))
11122 emacs_abort ();
11123 }
11124
11125
11126 /* Truncate to NCHARS what will be displayed in the echo area the next
11127 time we display it---but don't redisplay it now. */
11128
11129 void
11130 truncate_echo_area (ptrdiff_t nchars)
11131 {
11132 if (nchars == 0)
11133 echo_area_buffer[0] = Qnil;
11134 else if (!noninteractive
11135 && INTERACTIVE
11136 && !NILP (echo_area_buffer[0]))
11137 {
11138 struct frame *sf = SELECTED_FRAME ();
11139 /* Error messages get reported properly by cmd_error, so this must be
11140 just an informative message; if the frame hasn't really been
11141 initialized yet, just toss it. */
11142 if (sf->glyphs_initialized_p)
11143 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11144 }
11145 }
11146
11147
11148 /* Helper function for truncate_echo_area. Truncate the current
11149 message to at most NCHARS characters. */
11150
11151 static bool
11152 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11153 {
11154 if (BEG + nchars < Z)
11155 del_range (BEG + nchars, Z);
11156 if (Z == BEG)
11157 echo_area_buffer[0] = Qnil;
11158 return false;
11159 }
11160
11161 /* Set the current message to STRING. */
11162
11163 static void
11164 set_message (Lisp_Object string)
11165 {
11166 eassert (STRINGP (string));
11167
11168 message_enable_multibyte = STRING_MULTIBYTE (string);
11169
11170 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11171 message_buf_print = false;
11172 help_echo_showing_p = false;
11173
11174 if (STRINGP (Vdebug_on_message)
11175 && STRINGP (string)
11176 && fast_string_match (Vdebug_on_message, string) >= 0)
11177 call_debugger (list2 (Qerror, string));
11178 }
11179
11180
11181 /* Helper function for set_message. First argument is ignored and second
11182 argument has the same meaning as for set_message.
11183 This function is called with the echo area buffer being current. */
11184
11185 static bool
11186 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11187 {
11188 eassert (STRINGP (string));
11189
11190 /* Change multibyteness of the echo buffer appropriately. */
11191 if (message_enable_multibyte
11192 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11193 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11194
11195 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11196 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11197 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11198
11199 /* Insert new message at BEG. */
11200 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11201
11202 /* This function takes care of single/multibyte conversion.
11203 We just have to ensure that the echo area buffer has the right
11204 setting of enable_multibyte_characters. */
11205 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11206
11207 return false;
11208 }
11209
11210
11211 /* Clear messages. CURRENT_P means clear the current message.
11212 LAST_DISPLAYED_P means clear the message last displayed. */
11213
11214 void
11215 clear_message (bool current_p, bool last_displayed_p)
11216 {
11217 if (current_p)
11218 {
11219 echo_area_buffer[0] = Qnil;
11220 message_cleared_p = true;
11221 }
11222
11223 if (last_displayed_p)
11224 echo_area_buffer[1] = Qnil;
11225
11226 message_buf_print = false;
11227 }
11228
11229 /* Clear garbaged frames.
11230
11231 This function is used where the old redisplay called
11232 redraw_garbaged_frames which in turn called redraw_frame which in
11233 turn called clear_frame. The call to clear_frame was a source of
11234 flickering. I believe a clear_frame is not necessary. It should
11235 suffice in the new redisplay to invalidate all current matrices,
11236 and ensure a complete redisplay of all windows. */
11237
11238 static void
11239 clear_garbaged_frames (void)
11240 {
11241 if (frame_garbaged)
11242 {
11243 Lisp_Object tail, frame;
11244
11245 FOR_EACH_FRAME (tail, frame)
11246 {
11247 struct frame *f = XFRAME (frame);
11248
11249 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11250 {
11251 if (f->resized_p)
11252 redraw_frame (f);
11253 else
11254 clear_current_matrices (f);
11255 fset_redisplay (f);
11256 f->garbaged = false;
11257 f->resized_p = false;
11258 }
11259 }
11260
11261 frame_garbaged = false;
11262 }
11263 }
11264
11265
11266 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11267 selected_frame. */
11268
11269 static void
11270 echo_area_display (bool update_frame_p)
11271 {
11272 Lisp_Object mini_window;
11273 struct window *w;
11274 struct frame *f;
11275 bool window_height_changed_p = false;
11276 struct frame *sf = SELECTED_FRAME ();
11277
11278 mini_window = FRAME_MINIBUF_WINDOW (sf);
11279 w = XWINDOW (mini_window);
11280 f = XFRAME (WINDOW_FRAME (w));
11281
11282 /* Don't display if frame is invisible or not yet initialized. */
11283 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11284 return;
11285
11286 #ifdef HAVE_WINDOW_SYSTEM
11287 /* When Emacs starts, selected_frame may be the initial terminal
11288 frame. If we let this through, a message would be displayed on
11289 the terminal. */
11290 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11291 return;
11292 #endif /* HAVE_WINDOW_SYSTEM */
11293
11294 /* Redraw garbaged frames. */
11295 clear_garbaged_frames ();
11296
11297 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11298 {
11299 echo_area_window = mini_window;
11300 window_height_changed_p = display_echo_area (w);
11301 w->must_be_updated_p = true;
11302
11303 /* Update the display, unless called from redisplay_internal.
11304 Also don't update the screen during redisplay itself. The
11305 update will happen at the end of redisplay, and an update
11306 here could cause confusion. */
11307 if (update_frame_p && !redisplaying_p)
11308 {
11309 int n = 0;
11310
11311 /* If the display update has been interrupted by pending
11312 input, update mode lines in the frame. Due to the
11313 pending input, it might have been that redisplay hasn't
11314 been called, so that mode lines above the echo area are
11315 garbaged. This looks odd, so we prevent it here. */
11316 if (!display_completed)
11317 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11318
11319 if (window_height_changed_p
11320 /* Don't do this if Emacs is shutting down. Redisplay
11321 needs to run hooks. */
11322 && !NILP (Vrun_hooks))
11323 {
11324 /* Must update other windows. Likewise as in other
11325 cases, don't let this update be interrupted by
11326 pending input. */
11327 ptrdiff_t count = SPECPDL_INDEX ();
11328 specbind (Qredisplay_dont_pause, Qt);
11329 fset_redisplay (f);
11330 redisplay_internal ();
11331 unbind_to (count, Qnil);
11332 }
11333 else if (FRAME_WINDOW_P (f) && n == 0)
11334 {
11335 /* Window configuration is the same as before.
11336 Can do with a display update of the echo area,
11337 unless we displayed some mode lines. */
11338 update_single_window (w);
11339 flush_frame (f);
11340 }
11341 else
11342 update_frame (f, true, true);
11343
11344 /* If cursor is in the echo area, make sure that the next
11345 redisplay displays the minibuffer, so that the cursor will
11346 be replaced with what the minibuffer wants. */
11347 if (cursor_in_echo_area)
11348 wset_redisplay (XWINDOW (mini_window));
11349 }
11350 }
11351 else if (!EQ (mini_window, selected_window))
11352 wset_redisplay (XWINDOW (mini_window));
11353
11354 /* Last displayed message is now the current message. */
11355 echo_area_buffer[1] = echo_area_buffer[0];
11356 /* Inform read_char that we're not echoing. */
11357 echo_message_buffer = Qnil;
11358
11359 /* Prevent redisplay optimization in redisplay_internal by resetting
11360 this_line_start_pos. This is done because the mini-buffer now
11361 displays the message instead of its buffer text. */
11362 if (EQ (mini_window, selected_window))
11363 CHARPOS (this_line_start_pos) = 0;
11364
11365 if (window_height_changed_p)
11366 {
11367 fset_redisplay (f);
11368
11369 /* If window configuration was changed, frames may have been
11370 marked garbaged. Clear them or we will experience
11371 surprises wrt scrolling.
11372 FIXME: How/why/when? */
11373 clear_garbaged_frames ();
11374 }
11375 }
11376
11377 /* True if W's buffer was changed but not saved. */
11378
11379 static bool
11380 window_buffer_changed (struct window *w)
11381 {
11382 struct buffer *b = XBUFFER (w->contents);
11383
11384 eassert (BUFFER_LIVE_P (b));
11385
11386 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11387 }
11388
11389 /* True if W has %c in its mode line and mode line should be updated. */
11390
11391 static bool
11392 mode_line_update_needed (struct window *w)
11393 {
11394 return (w->column_number_displayed != -1
11395 && !(PT == w->last_point && !window_outdated (w))
11396 && (w->column_number_displayed != current_column ()));
11397 }
11398
11399 /* True if window start of W is frozen and may not be changed during
11400 redisplay. */
11401
11402 static bool
11403 window_frozen_p (struct window *w)
11404 {
11405 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11406 {
11407 Lisp_Object window;
11408
11409 XSETWINDOW (window, w);
11410 if (MINI_WINDOW_P (w))
11411 return false;
11412 else if (EQ (window, selected_window))
11413 return false;
11414 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11415 && EQ (window, Vminibuf_scroll_window))
11416 /* This special window can't be frozen too. */
11417 return false;
11418 else
11419 return true;
11420 }
11421 return false;
11422 }
11423
11424 /***********************************************************************
11425 Mode Lines and Frame Titles
11426 ***********************************************************************/
11427
11428 /* A buffer for constructing non-propertized mode-line strings and
11429 frame titles in it; allocated from the heap in init_xdisp and
11430 resized as needed in store_mode_line_noprop_char. */
11431
11432 static char *mode_line_noprop_buf;
11433
11434 /* The buffer's end, and a current output position in it. */
11435
11436 static char *mode_line_noprop_buf_end;
11437 static char *mode_line_noprop_ptr;
11438
11439 #define MODE_LINE_NOPROP_LEN(start) \
11440 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11441
11442 static enum {
11443 MODE_LINE_DISPLAY = 0,
11444 MODE_LINE_TITLE,
11445 MODE_LINE_NOPROP,
11446 MODE_LINE_STRING
11447 } mode_line_target;
11448
11449 /* Alist that caches the results of :propertize.
11450 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11451 static Lisp_Object mode_line_proptrans_alist;
11452
11453 /* List of strings making up the mode-line. */
11454 static Lisp_Object mode_line_string_list;
11455
11456 /* Base face property when building propertized mode line string. */
11457 static Lisp_Object mode_line_string_face;
11458 static Lisp_Object mode_line_string_face_prop;
11459
11460
11461 /* Unwind data for mode line strings */
11462
11463 static Lisp_Object Vmode_line_unwind_vector;
11464
11465 static Lisp_Object
11466 format_mode_line_unwind_data (struct frame *target_frame,
11467 struct buffer *obuf,
11468 Lisp_Object owin,
11469 bool save_proptrans)
11470 {
11471 Lisp_Object vector, tmp;
11472
11473 /* Reduce consing by keeping one vector in
11474 Vwith_echo_area_save_vector. */
11475 vector = Vmode_line_unwind_vector;
11476 Vmode_line_unwind_vector = Qnil;
11477
11478 if (NILP (vector))
11479 vector = Fmake_vector (make_number (10), Qnil);
11480
11481 ASET (vector, 0, make_number (mode_line_target));
11482 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11483 ASET (vector, 2, mode_line_string_list);
11484 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11485 ASET (vector, 4, mode_line_string_face);
11486 ASET (vector, 5, mode_line_string_face_prop);
11487
11488 if (obuf)
11489 XSETBUFFER (tmp, obuf);
11490 else
11491 tmp = Qnil;
11492 ASET (vector, 6, tmp);
11493 ASET (vector, 7, owin);
11494 if (target_frame)
11495 {
11496 /* Similarly to `with-selected-window', if the operation selects
11497 a window on another frame, we must restore that frame's
11498 selected window, and (for a tty) the top-frame. */
11499 ASET (vector, 8, target_frame->selected_window);
11500 if (FRAME_TERMCAP_P (target_frame))
11501 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11502 }
11503
11504 return vector;
11505 }
11506
11507 static void
11508 unwind_format_mode_line (Lisp_Object vector)
11509 {
11510 Lisp_Object old_window = AREF (vector, 7);
11511 Lisp_Object target_frame_window = AREF (vector, 8);
11512 Lisp_Object old_top_frame = AREF (vector, 9);
11513
11514 mode_line_target = XINT (AREF (vector, 0));
11515 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11516 mode_line_string_list = AREF (vector, 2);
11517 if (! EQ (AREF (vector, 3), Qt))
11518 mode_line_proptrans_alist = AREF (vector, 3);
11519 mode_line_string_face = AREF (vector, 4);
11520 mode_line_string_face_prop = AREF (vector, 5);
11521
11522 /* Select window before buffer, since it may change the buffer. */
11523 if (!NILP (old_window))
11524 {
11525 /* If the operation that we are unwinding had selected a window
11526 on a different frame, reset its frame-selected-window. For a
11527 text terminal, reset its top-frame if necessary. */
11528 if (!NILP (target_frame_window))
11529 {
11530 Lisp_Object frame
11531 = WINDOW_FRAME (XWINDOW (target_frame_window));
11532
11533 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11534 Fselect_window (target_frame_window, Qt);
11535
11536 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11537 Fselect_frame (old_top_frame, Qt);
11538 }
11539
11540 Fselect_window (old_window, Qt);
11541 }
11542
11543 if (!NILP (AREF (vector, 6)))
11544 {
11545 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11546 ASET (vector, 6, Qnil);
11547 }
11548
11549 Vmode_line_unwind_vector = vector;
11550 }
11551
11552
11553 /* Store a single character C for the frame title in mode_line_noprop_buf.
11554 Re-allocate mode_line_noprop_buf if necessary. */
11555
11556 static void
11557 store_mode_line_noprop_char (char c)
11558 {
11559 /* If output position has reached the end of the allocated buffer,
11560 increase the buffer's size. */
11561 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11562 {
11563 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11564 ptrdiff_t size = len;
11565 mode_line_noprop_buf =
11566 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11567 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11568 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11569 }
11570
11571 *mode_line_noprop_ptr++ = c;
11572 }
11573
11574
11575 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11576 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11577 characters that yield more columns than PRECISION; PRECISION <= 0
11578 means copy the whole string. Pad with spaces until FIELD_WIDTH
11579 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11580 pad. Called from display_mode_element when it is used to build a
11581 frame title. */
11582
11583 static int
11584 store_mode_line_noprop (const char *string, int field_width, int precision)
11585 {
11586 const unsigned char *str = (const unsigned char *) string;
11587 int n = 0;
11588 ptrdiff_t dummy, nbytes;
11589
11590 /* Copy at most PRECISION chars from STR. */
11591 nbytes = strlen (string);
11592 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11593 while (nbytes--)
11594 store_mode_line_noprop_char (*str++);
11595
11596 /* Fill up with spaces until FIELD_WIDTH reached. */
11597 while (field_width > 0
11598 && n < field_width)
11599 {
11600 store_mode_line_noprop_char (' ');
11601 ++n;
11602 }
11603
11604 return n;
11605 }
11606
11607 /***********************************************************************
11608 Frame Titles
11609 ***********************************************************************/
11610
11611 #ifdef HAVE_WINDOW_SYSTEM
11612
11613 /* Set the title of FRAME, if it has changed. The title format is
11614 Vicon_title_format if FRAME is iconified, otherwise it is
11615 frame_title_format. */
11616
11617 static void
11618 x_consider_frame_title (Lisp_Object frame)
11619 {
11620 struct frame *f = XFRAME (frame);
11621
11622 if ((FRAME_WINDOW_P (f)
11623 || FRAME_MINIBUF_ONLY_P (f)
11624 || f->explicit_name)
11625 && NILP (Fframe_parameter (frame, Qtooltip)))
11626 {
11627 /* Do we have more than one visible frame on this X display? */
11628 Lisp_Object tail, other_frame, fmt;
11629 ptrdiff_t title_start;
11630 char *title;
11631 ptrdiff_t len;
11632 struct it it;
11633 ptrdiff_t count = SPECPDL_INDEX ();
11634
11635 FOR_EACH_FRAME (tail, other_frame)
11636 {
11637 struct frame *tf = XFRAME (other_frame);
11638
11639 if (tf != f
11640 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11641 && !FRAME_MINIBUF_ONLY_P (tf)
11642 && !EQ (other_frame, tip_frame)
11643 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11644 break;
11645 }
11646
11647 /* Set global variable indicating that multiple frames exist. */
11648 multiple_frames = CONSP (tail);
11649
11650 /* Switch to the buffer of selected window of the frame. Set up
11651 mode_line_target so that display_mode_element will output into
11652 mode_line_noprop_buf; then display the title. */
11653 record_unwind_protect (unwind_format_mode_line,
11654 format_mode_line_unwind_data
11655 (f, current_buffer, selected_window, false));
11656
11657 Fselect_window (f->selected_window, Qt);
11658 set_buffer_internal_1
11659 (XBUFFER (XWINDOW (f->selected_window)->contents));
11660 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11661
11662 mode_line_target = MODE_LINE_TITLE;
11663 title_start = MODE_LINE_NOPROP_LEN (0);
11664 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11665 NULL, DEFAULT_FACE_ID);
11666 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11667 len = MODE_LINE_NOPROP_LEN (title_start);
11668 title = mode_line_noprop_buf + title_start;
11669 unbind_to (count, Qnil);
11670
11671 /* Set the title only if it's changed. This avoids consing in
11672 the common case where it hasn't. (If it turns out that we've
11673 already wasted too much time by walking through the list with
11674 display_mode_element, then we might need to optimize at a
11675 higher level than this.) */
11676 if (! STRINGP (f->name)
11677 || SBYTES (f->name) != len
11678 || memcmp (title, SDATA (f->name), len) != 0)
11679 x_implicitly_set_name (f, make_string (title, len), Qnil);
11680 }
11681 }
11682
11683 #endif /* not HAVE_WINDOW_SYSTEM */
11684
11685 \f
11686 /***********************************************************************
11687 Menu Bars
11688 ***********************************************************************/
11689
11690 /* True if we will not redisplay all visible windows. */
11691 #define REDISPLAY_SOME_P() \
11692 ((windows_or_buffers_changed == 0 \
11693 || windows_or_buffers_changed == REDISPLAY_SOME) \
11694 && (update_mode_lines == 0 \
11695 || update_mode_lines == REDISPLAY_SOME))
11696
11697 /* Prepare for redisplay by updating menu-bar item lists when
11698 appropriate. This can call eval. */
11699
11700 static void
11701 prepare_menu_bars (void)
11702 {
11703 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11704 bool some_windows = REDISPLAY_SOME_P ();
11705 Lisp_Object tooltip_frame;
11706
11707 #ifdef HAVE_WINDOW_SYSTEM
11708 tooltip_frame = tip_frame;
11709 #else
11710 tooltip_frame = Qnil;
11711 #endif
11712
11713 if (FUNCTIONP (Vpre_redisplay_function))
11714 {
11715 Lisp_Object windows = all_windows ? Qt : Qnil;
11716 if (all_windows && some_windows)
11717 {
11718 Lisp_Object ws = window_list ();
11719 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11720 {
11721 Lisp_Object this = XCAR (ws);
11722 struct window *w = XWINDOW (this);
11723 if (w->redisplay
11724 || XFRAME (w->frame)->redisplay
11725 || XBUFFER (w->contents)->text->redisplay)
11726 {
11727 windows = Fcons (this, windows);
11728 }
11729 }
11730 }
11731 safe__call1 (true, Vpre_redisplay_function, windows);
11732 }
11733
11734 /* Update all frame titles based on their buffer names, etc. We do
11735 this before the menu bars so that the buffer-menu will show the
11736 up-to-date frame titles. */
11737 #ifdef HAVE_WINDOW_SYSTEM
11738 if (all_windows)
11739 {
11740 Lisp_Object tail, frame;
11741
11742 FOR_EACH_FRAME (tail, frame)
11743 {
11744 struct frame *f = XFRAME (frame);
11745 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11746 if (some_windows
11747 && !f->redisplay
11748 && !w->redisplay
11749 && !XBUFFER (w->contents)->text->redisplay)
11750 continue;
11751
11752 if (!EQ (frame, tooltip_frame)
11753 && (FRAME_ICONIFIED_P (f)
11754 || FRAME_VISIBLE_P (f) == 1
11755 /* Exclude TTY frames that are obscured because they
11756 are not the top frame on their console. This is
11757 because x_consider_frame_title actually switches
11758 to the frame, which for TTY frames means it is
11759 marked as garbaged, and will be completely
11760 redrawn on the next redisplay cycle. This causes
11761 TTY frames to be completely redrawn, when there
11762 are more than one of them, even though nothing
11763 should be changed on display. */
11764 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11765 x_consider_frame_title (frame);
11766 }
11767 }
11768 #endif /* HAVE_WINDOW_SYSTEM */
11769
11770 /* Update the menu bar item lists, if appropriate. This has to be
11771 done before any actual redisplay or generation of display lines. */
11772
11773 if (all_windows)
11774 {
11775 Lisp_Object tail, frame;
11776 ptrdiff_t count = SPECPDL_INDEX ();
11777 /* True means that update_menu_bar has run its hooks
11778 so any further calls to update_menu_bar shouldn't do so again. */
11779 bool menu_bar_hooks_run = false;
11780
11781 record_unwind_save_match_data ();
11782
11783 FOR_EACH_FRAME (tail, frame)
11784 {
11785 struct frame *f = XFRAME (frame);
11786 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11787
11788 /* Ignore tooltip frame. */
11789 if (EQ (frame, tooltip_frame))
11790 continue;
11791
11792 if (some_windows
11793 && !f->redisplay
11794 && !w->redisplay
11795 && !XBUFFER (w->contents)->text->redisplay)
11796 continue;
11797
11798 /* If a window on this frame changed size, report that to
11799 the user and clear the size-change flag. */
11800 if (FRAME_WINDOW_SIZES_CHANGED (f))
11801 {
11802 Lisp_Object functions;
11803
11804 /* Clear flag first in case we get an error below. */
11805 FRAME_WINDOW_SIZES_CHANGED (f) = false;
11806 functions = Vwindow_size_change_functions;
11807
11808 while (CONSP (functions))
11809 {
11810 if (!EQ (XCAR (functions), Qt))
11811 call1 (XCAR (functions), frame);
11812 functions = XCDR (functions);
11813 }
11814 }
11815
11816 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11817 #ifdef HAVE_WINDOW_SYSTEM
11818 update_tool_bar (f, false);
11819 #endif
11820 }
11821
11822 unbind_to (count, Qnil);
11823 }
11824 else
11825 {
11826 struct frame *sf = SELECTED_FRAME ();
11827 update_menu_bar (sf, true, false);
11828 #ifdef HAVE_WINDOW_SYSTEM
11829 update_tool_bar (sf, true);
11830 #endif
11831 }
11832 }
11833
11834
11835 /* Update the menu bar item list for frame F. This has to be done
11836 before we start to fill in any display lines, because it can call
11837 eval.
11838
11839 If SAVE_MATCH_DATA, we must save and restore it here.
11840
11841 If HOOKS_RUN, a previous call to update_menu_bar
11842 already ran the menu bar hooks for this redisplay, so there
11843 is no need to run them again. The return value is the
11844 updated value of this flag, to pass to the next call. */
11845
11846 static bool
11847 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11848 {
11849 Lisp_Object window;
11850 struct window *w;
11851
11852 /* If called recursively during a menu update, do nothing. This can
11853 happen when, for instance, an activate-menubar-hook causes a
11854 redisplay. */
11855 if (inhibit_menubar_update)
11856 return hooks_run;
11857
11858 window = FRAME_SELECTED_WINDOW (f);
11859 w = XWINDOW (window);
11860
11861 if (FRAME_WINDOW_P (f)
11862 ?
11863 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11864 || defined (HAVE_NS) || defined (USE_GTK)
11865 FRAME_EXTERNAL_MENU_BAR (f)
11866 #else
11867 FRAME_MENU_BAR_LINES (f) > 0
11868 #endif
11869 : FRAME_MENU_BAR_LINES (f) > 0)
11870 {
11871 /* If the user has switched buffers or windows, we need to
11872 recompute to reflect the new bindings. But we'll
11873 recompute when update_mode_lines is set too; that means
11874 that people can use force-mode-line-update to request
11875 that the menu bar be recomputed. The adverse effect on
11876 the rest of the redisplay algorithm is about the same as
11877 windows_or_buffers_changed anyway. */
11878 if (windows_or_buffers_changed
11879 /* This used to test w->update_mode_line, but we believe
11880 there is no need to recompute the menu in that case. */
11881 || update_mode_lines
11882 || window_buffer_changed (w))
11883 {
11884 struct buffer *prev = current_buffer;
11885 ptrdiff_t count = SPECPDL_INDEX ();
11886
11887 specbind (Qinhibit_menubar_update, Qt);
11888
11889 set_buffer_internal_1 (XBUFFER (w->contents));
11890 if (save_match_data)
11891 record_unwind_save_match_data ();
11892 if (NILP (Voverriding_local_map_menu_flag))
11893 {
11894 specbind (Qoverriding_terminal_local_map, Qnil);
11895 specbind (Qoverriding_local_map, Qnil);
11896 }
11897
11898 if (!hooks_run)
11899 {
11900 /* Run the Lucid hook. */
11901 safe_run_hooks (Qactivate_menubar_hook);
11902
11903 /* If it has changed current-menubar from previous value,
11904 really recompute the menu-bar from the value. */
11905 if (! NILP (Vlucid_menu_bar_dirty_flag))
11906 call0 (Qrecompute_lucid_menubar);
11907
11908 safe_run_hooks (Qmenu_bar_update_hook);
11909
11910 hooks_run = true;
11911 }
11912
11913 XSETFRAME (Vmenu_updating_frame, f);
11914 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11915
11916 /* Redisplay the menu bar in case we changed it. */
11917 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11918 || defined (HAVE_NS) || defined (USE_GTK)
11919 if (FRAME_WINDOW_P (f))
11920 {
11921 #if defined (HAVE_NS)
11922 /* All frames on Mac OS share the same menubar. So only
11923 the selected frame should be allowed to set it. */
11924 if (f == SELECTED_FRAME ())
11925 #endif
11926 set_frame_menubar (f, false, false);
11927 }
11928 else
11929 /* On a terminal screen, the menu bar is an ordinary screen
11930 line, and this makes it get updated. */
11931 w->update_mode_line = true;
11932 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11933 /* In the non-toolkit version, the menu bar is an ordinary screen
11934 line, and this makes it get updated. */
11935 w->update_mode_line = true;
11936 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11937
11938 unbind_to (count, Qnil);
11939 set_buffer_internal_1 (prev);
11940 }
11941 }
11942
11943 return hooks_run;
11944 }
11945
11946 /***********************************************************************
11947 Tool-bars
11948 ***********************************************************************/
11949
11950 #ifdef HAVE_WINDOW_SYSTEM
11951
11952 /* Select `frame' temporarily without running all the code in
11953 do_switch_frame.
11954 FIXME: Maybe do_switch_frame should be trimmed down similarly
11955 when `norecord' is set. */
11956 static void
11957 fast_set_selected_frame (Lisp_Object frame)
11958 {
11959 if (!EQ (selected_frame, frame))
11960 {
11961 selected_frame = frame;
11962 selected_window = XFRAME (frame)->selected_window;
11963 }
11964 }
11965
11966 /* Update the tool-bar item list for frame F. This has to be done
11967 before we start to fill in any display lines. Called from
11968 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
11969 and restore it here. */
11970
11971 static void
11972 update_tool_bar (struct frame *f, bool save_match_data)
11973 {
11974 #if defined (USE_GTK) || defined (HAVE_NS)
11975 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11976 #else
11977 bool do_update = (WINDOWP (f->tool_bar_window)
11978 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11979 #endif
11980
11981 if (do_update)
11982 {
11983 Lisp_Object window;
11984 struct window *w;
11985
11986 window = FRAME_SELECTED_WINDOW (f);
11987 w = XWINDOW (window);
11988
11989 /* If the user has switched buffers or windows, we need to
11990 recompute to reflect the new bindings. But we'll
11991 recompute when update_mode_lines is set too; that means
11992 that people can use force-mode-line-update to request
11993 that the menu bar be recomputed. The adverse effect on
11994 the rest of the redisplay algorithm is about the same as
11995 windows_or_buffers_changed anyway. */
11996 if (windows_or_buffers_changed
11997 || w->update_mode_line
11998 || update_mode_lines
11999 || window_buffer_changed (w))
12000 {
12001 struct buffer *prev = current_buffer;
12002 ptrdiff_t count = SPECPDL_INDEX ();
12003 Lisp_Object frame, new_tool_bar;
12004 int new_n_tool_bar;
12005
12006 /* Set current_buffer to the buffer of the selected
12007 window of the frame, so that we get the right local
12008 keymaps. */
12009 set_buffer_internal_1 (XBUFFER (w->contents));
12010
12011 /* Save match data, if we must. */
12012 if (save_match_data)
12013 record_unwind_save_match_data ();
12014
12015 /* Make sure that we don't accidentally use bogus keymaps. */
12016 if (NILP (Voverriding_local_map_menu_flag))
12017 {
12018 specbind (Qoverriding_terminal_local_map, Qnil);
12019 specbind (Qoverriding_local_map, Qnil);
12020 }
12021
12022 /* We must temporarily set the selected frame to this frame
12023 before calling tool_bar_items, because the calculation of
12024 the tool-bar keymap uses the selected frame (see
12025 `tool-bar-make-keymap' in tool-bar.el). */
12026 eassert (EQ (selected_window,
12027 /* Since we only explicitly preserve selected_frame,
12028 check that selected_window would be redundant. */
12029 XFRAME (selected_frame)->selected_window));
12030 record_unwind_protect (fast_set_selected_frame, selected_frame);
12031 XSETFRAME (frame, f);
12032 fast_set_selected_frame (frame);
12033
12034 /* Build desired tool-bar items from keymaps. */
12035 new_tool_bar
12036 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12037 &new_n_tool_bar);
12038
12039 /* Redisplay the tool-bar if we changed it. */
12040 if (new_n_tool_bar != f->n_tool_bar_items
12041 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12042 {
12043 /* Redisplay that happens asynchronously due to an expose event
12044 may access f->tool_bar_items. Make sure we update both
12045 variables within BLOCK_INPUT so no such event interrupts. */
12046 block_input ();
12047 fset_tool_bar_items (f, new_tool_bar);
12048 f->n_tool_bar_items = new_n_tool_bar;
12049 w->update_mode_line = true;
12050 unblock_input ();
12051 }
12052
12053 unbind_to (count, Qnil);
12054 set_buffer_internal_1 (prev);
12055 }
12056 }
12057 }
12058
12059 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12060
12061 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12062 F's desired tool-bar contents. F->tool_bar_items must have
12063 been set up previously by calling prepare_menu_bars. */
12064
12065 static void
12066 build_desired_tool_bar_string (struct frame *f)
12067 {
12068 int i, size, size_needed;
12069 Lisp_Object image, plist;
12070
12071 image = plist = Qnil;
12072
12073 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12074 Otherwise, make a new string. */
12075
12076 /* The size of the string we might be able to reuse. */
12077 size = (STRINGP (f->desired_tool_bar_string)
12078 ? SCHARS (f->desired_tool_bar_string)
12079 : 0);
12080
12081 /* We need one space in the string for each image. */
12082 size_needed = f->n_tool_bar_items;
12083
12084 /* Reuse f->desired_tool_bar_string, if possible. */
12085 if (size < size_needed || NILP (f->desired_tool_bar_string))
12086 fset_desired_tool_bar_string
12087 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12088 else
12089 {
12090 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12091 Fremove_text_properties (make_number (0), make_number (size),
12092 props, f->desired_tool_bar_string);
12093 }
12094
12095 /* Put a `display' property on the string for the images to display,
12096 put a `menu_item' property on tool-bar items with a value that
12097 is the index of the item in F's tool-bar item vector. */
12098 for (i = 0; i < f->n_tool_bar_items; ++i)
12099 {
12100 #define PROP(IDX) \
12101 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12102
12103 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12104 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12105 int hmargin, vmargin, relief, idx, end;
12106
12107 /* If image is a vector, choose the image according to the
12108 button state. */
12109 image = PROP (TOOL_BAR_ITEM_IMAGES);
12110 if (VECTORP (image))
12111 {
12112 if (enabled_p)
12113 idx = (selected_p
12114 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12115 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12116 else
12117 idx = (selected_p
12118 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12119 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12120
12121 eassert (ASIZE (image) >= idx);
12122 image = AREF (image, idx);
12123 }
12124 else
12125 idx = -1;
12126
12127 /* Ignore invalid image specifications. */
12128 if (!valid_image_p (image))
12129 continue;
12130
12131 /* Display the tool-bar button pressed, or depressed. */
12132 plist = Fcopy_sequence (XCDR (image));
12133
12134 /* Compute margin and relief to draw. */
12135 relief = (tool_bar_button_relief >= 0
12136 ? tool_bar_button_relief
12137 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12138 hmargin = vmargin = relief;
12139
12140 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12141 INT_MAX - max (hmargin, vmargin)))
12142 {
12143 hmargin += XFASTINT (Vtool_bar_button_margin);
12144 vmargin += XFASTINT (Vtool_bar_button_margin);
12145 }
12146 else if (CONSP (Vtool_bar_button_margin))
12147 {
12148 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12149 INT_MAX - hmargin))
12150 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12151
12152 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12153 INT_MAX - vmargin))
12154 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12155 }
12156
12157 if (auto_raise_tool_bar_buttons_p)
12158 {
12159 /* Add a `:relief' property to the image spec if the item is
12160 selected. */
12161 if (selected_p)
12162 {
12163 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12164 hmargin -= relief;
12165 vmargin -= relief;
12166 }
12167 }
12168 else
12169 {
12170 /* If image is selected, display it pressed, i.e. with a
12171 negative relief. If it's not selected, display it with a
12172 raised relief. */
12173 plist = Fplist_put (plist, QCrelief,
12174 (selected_p
12175 ? make_number (-relief)
12176 : make_number (relief)));
12177 hmargin -= relief;
12178 vmargin -= relief;
12179 }
12180
12181 /* Put a margin around the image. */
12182 if (hmargin || vmargin)
12183 {
12184 if (hmargin == vmargin)
12185 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12186 else
12187 plist = Fplist_put (plist, QCmargin,
12188 Fcons (make_number (hmargin),
12189 make_number (vmargin)));
12190 }
12191
12192 /* If button is not enabled, and we don't have special images
12193 for the disabled state, make the image appear disabled by
12194 applying an appropriate algorithm to it. */
12195 if (!enabled_p && idx < 0)
12196 plist = Fplist_put (plist, QCconversion, Qdisabled);
12197
12198 /* Put a `display' text property on the string for the image to
12199 display. Put a `menu-item' property on the string that gives
12200 the start of this item's properties in the tool-bar items
12201 vector. */
12202 image = Fcons (Qimage, plist);
12203 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12204 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12205
12206 /* Let the last image hide all remaining spaces in the tool bar
12207 string. The string can be longer than needed when we reuse a
12208 previous string. */
12209 if (i + 1 == f->n_tool_bar_items)
12210 end = SCHARS (f->desired_tool_bar_string);
12211 else
12212 end = i + 1;
12213 Fadd_text_properties (make_number (i), make_number (end),
12214 props, f->desired_tool_bar_string);
12215 #undef PROP
12216 }
12217 }
12218
12219
12220 /* Display one line of the tool-bar of frame IT->f.
12221
12222 HEIGHT specifies the desired height of the tool-bar line.
12223 If the actual height of the glyph row is less than HEIGHT, the
12224 row's height is increased to HEIGHT, and the icons are centered
12225 vertically in the new height.
12226
12227 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12228 count a final empty row in case the tool-bar width exactly matches
12229 the window width.
12230 */
12231
12232 static void
12233 display_tool_bar_line (struct it *it, int height)
12234 {
12235 struct glyph_row *row = it->glyph_row;
12236 int max_x = it->last_visible_x;
12237 struct glyph *last;
12238
12239 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12240 clear_glyph_row (row);
12241 row->enabled_p = true;
12242 row->y = it->current_y;
12243
12244 /* Note that this isn't made use of if the face hasn't a box,
12245 so there's no need to check the face here. */
12246 it->start_of_box_run_p = true;
12247
12248 while (it->current_x < max_x)
12249 {
12250 int x, n_glyphs_before, i, nglyphs;
12251 struct it it_before;
12252
12253 /* Get the next display element. */
12254 if (!get_next_display_element (it))
12255 {
12256 /* Don't count empty row if we are counting needed tool-bar lines. */
12257 if (height < 0 && !it->hpos)
12258 return;
12259 break;
12260 }
12261
12262 /* Produce glyphs. */
12263 n_glyphs_before = row->used[TEXT_AREA];
12264 it_before = *it;
12265
12266 PRODUCE_GLYPHS (it);
12267
12268 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12269 i = 0;
12270 x = it_before.current_x;
12271 while (i < nglyphs)
12272 {
12273 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12274
12275 if (x + glyph->pixel_width > max_x)
12276 {
12277 /* Glyph doesn't fit on line. Backtrack. */
12278 row->used[TEXT_AREA] = n_glyphs_before;
12279 *it = it_before;
12280 /* If this is the only glyph on this line, it will never fit on the
12281 tool-bar, so skip it. But ensure there is at least one glyph,
12282 so we don't accidentally disable the tool-bar. */
12283 if (n_glyphs_before == 0
12284 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12285 break;
12286 goto out;
12287 }
12288
12289 ++it->hpos;
12290 x += glyph->pixel_width;
12291 ++i;
12292 }
12293
12294 /* Stop at line end. */
12295 if (ITERATOR_AT_END_OF_LINE_P (it))
12296 break;
12297
12298 set_iterator_to_next (it, true);
12299 }
12300
12301 out:;
12302
12303 row->displays_text_p = row->used[TEXT_AREA] != 0;
12304
12305 /* Use default face for the border below the tool bar.
12306
12307 FIXME: When auto-resize-tool-bars is grow-only, there is
12308 no additional border below the possibly empty tool-bar lines.
12309 So to make the extra empty lines look "normal", we have to
12310 use the tool-bar face for the border too. */
12311 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12312 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12313 it->face_id = DEFAULT_FACE_ID;
12314
12315 extend_face_to_end_of_line (it);
12316 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12317 last->right_box_line_p = true;
12318 if (last == row->glyphs[TEXT_AREA])
12319 last->left_box_line_p = true;
12320
12321 /* Make line the desired height and center it vertically. */
12322 if ((height -= it->max_ascent + it->max_descent) > 0)
12323 {
12324 /* Don't add more than one line height. */
12325 height %= FRAME_LINE_HEIGHT (it->f);
12326 it->max_ascent += height / 2;
12327 it->max_descent += (height + 1) / 2;
12328 }
12329
12330 compute_line_metrics (it);
12331
12332 /* If line is empty, make it occupy the rest of the tool-bar. */
12333 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12334 {
12335 row->height = row->phys_height = it->last_visible_y - row->y;
12336 row->visible_height = row->height;
12337 row->ascent = row->phys_ascent = 0;
12338 row->extra_line_spacing = 0;
12339 }
12340
12341 row->full_width_p = true;
12342 row->continued_p = false;
12343 row->truncated_on_left_p = false;
12344 row->truncated_on_right_p = false;
12345
12346 it->current_x = it->hpos = 0;
12347 it->current_y += row->height;
12348 ++it->vpos;
12349 ++it->glyph_row;
12350 }
12351
12352
12353 /* Value is the number of pixels needed to make all tool-bar items of
12354 frame F visible. The actual number of glyph rows needed is
12355 returned in *N_ROWS if non-NULL. */
12356 static int
12357 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12358 {
12359 struct window *w = XWINDOW (f->tool_bar_window);
12360 struct it it;
12361 /* tool_bar_height is called from redisplay_tool_bar after building
12362 the desired matrix, so use (unused) mode-line row as temporary row to
12363 avoid destroying the first tool-bar row. */
12364 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12365
12366 /* Initialize an iterator for iteration over
12367 F->desired_tool_bar_string in the tool-bar window of frame F. */
12368 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12369 temp_row->reversed_p = false;
12370 it.first_visible_x = 0;
12371 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12372 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12373 it.paragraph_embedding = L2R;
12374
12375 while (!ITERATOR_AT_END_P (&it))
12376 {
12377 clear_glyph_row (temp_row);
12378 it.glyph_row = temp_row;
12379 display_tool_bar_line (&it, -1);
12380 }
12381 clear_glyph_row (temp_row);
12382
12383 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12384 if (n_rows)
12385 *n_rows = it.vpos > 0 ? it.vpos : -1;
12386
12387 if (pixelwise)
12388 return it.current_y;
12389 else
12390 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12391 }
12392
12393 #endif /* !USE_GTK && !HAVE_NS */
12394
12395 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12396 0, 2, 0,
12397 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12398 If FRAME is nil or omitted, use the selected frame. Optional argument
12399 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12400 (Lisp_Object frame, Lisp_Object pixelwise)
12401 {
12402 int height = 0;
12403
12404 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12405 struct frame *f = decode_any_frame (frame);
12406
12407 if (WINDOWP (f->tool_bar_window)
12408 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12409 {
12410 update_tool_bar (f, true);
12411 if (f->n_tool_bar_items)
12412 {
12413 build_desired_tool_bar_string (f);
12414 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12415 }
12416 }
12417 #endif
12418
12419 return make_number (height);
12420 }
12421
12422
12423 /* Display the tool-bar of frame F. Value is true if tool-bar's
12424 height should be changed. */
12425 static bool
12426 redisplay_tool_bar (struct frame *f)
12427 {
12428 f->tool_bar_redisplayed = true;
12429 #if defined (USE_GTK) || defined (HAVE_NS)
12430
12431 if (FRAME_EXTERNAL_TOOL_BAR (f))
12432 update_frame_tool_bar (f);
12433 return false;
12434
12435 #else /* !USE_GTK && !HAVE_NS */
12436
12437 struct window *w;
12438 struct it it;
12439 struct glyph_row *row;
12440
12441 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12442 do anything. This means you must start with tool-bar-lines
12443 non-zero to get the auto-sizing effect. Or in other words, you
12444 can turn off tool-bars by specifying tool-bar-lines zero. */
12445 if (!WINDOWP (f->tool_bar_window)
12446 || (w = XWINDOW (f->tool_bar_window),
12447 WINDOW_TOTAL_LINES (w) == 0))
12448 return false;
12449
12450 /* Set up an iterator for the tool-bar window. */
12451 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12452 it.first_visible_x = 0;
12453 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12454 row = it.glyph_row;
12455 row->reversed_p = false;
12456
12457 /* Build a string that represents the contents of the tool-bar. */
12458 build_desired_tool_bar_string (f);
12459 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12460 /* FIXME: This should be controlled by a user option. But it
12461 doesn't make sense to have an R2L tool bar if the menu bar cannot
12462 be drawn also R2L, and making the menu bar R2L is tricky due
12463 toolkit-specific code that implements it. If an R2L tool bar is
12464 ever supported, display_tool_bar_line should also be augmented to
12465 call unproduce_glyphs like display_line and display_string
12466 do. */
12467 it.paragraph_embedding = L2R;
12468
12469 if (f->n_tool_bar_rows == 0)
12470 {
12471 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12472
12473 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12474 {
12475 x_change_tool_bar_height (f, new_height);
12476 frame_default_tool_bar_height = new_height;
12477 /* Always do that now. */
12478 clear_glyph_matrix (w->desired_matrix);
12479 f->fonts_changed = true;
12480 return true;
12481 }
12482 }
12483
12484 /* Display as many lines as needed to display all tool-bar items. */
12485
12486 if (f->n_tool_bar_rows > 0)
12487 {
12488 int border, rows, height, extra;
12489
12490 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12491 border = XINT (Vtool_bar_border);
12492 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12493 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12494 else if (EQ (Vtool_bar_border, Qborder_width))
12495 border = f->border_width;
12496 else
12497 border = 0;
12498 if (border < 0)
12499 border = 0;
12500
12501 rows = f->n_tool_bar_rows;
12502 height = max (1, (it.last_visible_y - border) / rows);
12503 extra = it.last_visible_y - border - height * rows;
12504
12505 while (it.current_y < it.last_visible_y)
12506 {
12507 int h = 0;
12508 if (extra > 0 && rows-- > 0)
12509 {
12510 h = (extra + rows - 1) / rows;
12511 extra -= h;
12512 }
12513 display_tool_bar_line (&it, height + h);
12514 }
12515 }
12516 else
12517 {
12518 while (it.current_y < it.last_visible_y)
12519 display_tool_bar_line (&it, 0);
12520 }
12521
12522 /* It doesn't make much sense to try scrolling in the tool-bar
12523 window, so don't do it. */
12524 w->desired_matrix->no_scrolling_p = true;
12525 w->must_be_updated_p = true;
12526
12527 if (!NILP (Vauto_resize_tool_bars))
12528 {
12529 bool change_height_p = true;
12530
12531 /* If we couldn't display everything, change the tool-bar's
12532 height if there is room for more. */
12533 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12534 change_height_p = true;
12535
12536 /* We subtract 1 because display_tool_bar_line advances the
12537 glyph_row pointer before returning to its caller. We want to
12538 examine the last glyph row produced by
12539 display_tool_bar_line. */
12540 row = it.glyph_row - 1;
12541
12542 /* If there are blank lines at the end, except for a partially
12543 visible blank line at the end that is smaller than
12544 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12545 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12546 && row->height >= FRAME_LINE_HEIGHT (f))
12547 change_height_p = true;
12548
12549 /* If row displays tool-bar items, but is partially visible,
12550 change the tool-bar's height. */
12551 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12552 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12553 change_height_p = true;
12554
12555 /* Resize windows as needed by changing the `tool-bar-lines'
12556 frame parameter. */
12557 if (change_height_p)
12558 {
12559 int nrows;
12560 int new_height = tool_bar_height (f, &nrows, true);
12561
12562 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12563 && !f->minimize_tool_bar_window_p)
12564 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12565 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12566 f->minimize_tool_bar_window_p = false;
12567
12568 if (change_height_p)
12569 {
12570 x_change_tool_bar_height (f, new_height);
12571 frame_default_tool_bar_height = new_height;
12572 clear_glyph_matrix (w->desired_matrix);
12573 f->n_tool_bar_rows = nrows;
12574 f->fonts_changed = true;
12575
12576 return true;
12577 }
12578 }
12579 }
12580
12581 f->minimize_tool_bar_window_p = false;
12582 return false;
12583
12584 #endif /* USE_GTK || HAVE_NS */
12585 }
12586
12587 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12588
12589 /* Get information about the tool-bar item which is displayed in GLYPH
12590 on frame F. Return in *PROP_IDX the index where tool-bar item
12591 properties start in F->tool_bar_items. Value is false if
12592 GLYPH doesn't display a tool-bar item. */
12593
12594 static bool
12595 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12596 {
12597 Lisp_Object prop;
12598 int charpos;
12599
12600 /* This function can be called asynchronously, which means we must
12601 exclude any possibility that Fget_text_property signals an
12602 error. */
12603 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12604 charpos = max (0, charpos);
12605
12606 /* Get the text property `menu-item' at pos. The value of that
12607 property is the start index of this item's properties in
12608 F->tool_bar_items. */
12609 prop = Fget_text_property (make_number (charpos),
12610 Qmenu_item, f->current_tool_bar_string);
12611 if (! INTEGERP (prop))
12612 return false;
12613 *prop_idx = XINT (prop);
12614 return true;
12615 }
12616
12617 \f
12618 /* Get information about the tool-bar item at position X/Y on frame F.
12619 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12620 the current matrix of the tool-bar window of F, or NULL if not
12621 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12622 item in F->tool_bar_items. Value is
12623
12624 -1 if X/Y is not on a tool-bar item
12625 0 if X/Y is on the same item that was highlighted before.
12626 1 otherwise. */
12627
12628 static int
12629 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12630 int *hpos, int *vpos, int *prop_idx)
12631 {
12632 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12633 struct window *w = XWINDOW (f->tool_bar_window);
12634 int area;
12635
12636 /* Find the glyph under X/Y. */
12637 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12638 if (*glyph == NULL)
12639 return -1;
12640
12641 /* Get the start of this tool-bar item's properties in
12642 f->tool_bar_items. */
12643 if (!tool_bar_item_info (f, *glyph, prop_idx))
12644 return -1;
12645
12646 /* Is mouse on the highlighted item? */
12647 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12648 && *vpos >= hlinfo->mouse_face_beg_row
12649 && *vpos <= hlinfo->mouse_face_end_row
12650 && (*vpos > hlinfo->mouse_face_beg_row
12651 || *hpos >= hlinfo->mouse_face_beg_col)
12652 && (*vpos < hlinfo->mouse_face_end_row
12653 || *hpos < hlinfo->mouse_face_end_col
12654 || hlinfo->mouse_face_past_end))
12655 return 0;
12656
12657 return 1;
12658 }
12659
12660
12661 /* EXPORT:
12662 Handle mouse button event on the tool-bar of frame F, at
12663 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12664 false for button release. MODIFIERS is event modifiers for button
12665 release. */
12666
12667 void
12668 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12669 int modifiers)
12670 {
12671 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12672 struct window *w = XWINDOW (f->tool_bar_window);
12673 int hpos, vpos, prop_idx;
12674 struct glyph *glyph;
12675 Lisp_Object enabled_p;
12676 int ts;
12677
12678 /* If not on the highlighted tool-bar item, and mouse-highlight is
12679 non-nil, return. This is so we generate the tool-bar button
12680 click only when the mouse button is released on the same item as
12681 where it was pressed. However, when mouse-highlight is disabled,
12682 generate the click when the button is released regardless of the
12683 highlight, since tool-bar items are not highlighted in that
12684 case. */
12685 frame_to_window_pixel_xy (w, &x, &y);
12686 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12687 if (ts == -1
12688 || (ts != 0 && !NILP (Vmouse_highlight)))
12689 return;
12690
12691 /* When mouse-highlight is off, generate the click for the item
12692 where the button was pressed, disregarding where it was
12693 released. */
12694 if (NILP (Vmouse_highlight) && !down_p)
12695 prop_idx = f->last_tool_bar_item;
12696
12697 /* If item is disabled, do nothing. */
12698 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12699 if (NILP (enabled_p))
12700 return;
12701
12702 if (down_p)
12703 {
12704 /* Show item in pressed state. */
12705 if (!NILP (Vmouse_highlight))
12706 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12707 f->last_tool_bar_item = prop_idx;
12708 }
12709 else
12710 {
12711 Lisp_Object key, frame;
12712 struct input_event event;
12713 EVENT_INIT (event);
12714
12715 /* Show item in released state. */
12716 if (!NILP (Vmouse_highlight))
12717 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12718
12719 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12720
12721 XSETFRAME (frame, f);
12722 event.kind = TOOL_BAR_EVENT;
12723 event.frame_or_window = frame;
12724 event.arg = frame;
12725 kbd_buffer_store_event (&event);
12726
12727 event.kind = TOOL_BAR_EVENT;
12728 event.frame_or_window = frame;
12729 event.arg = key;
12730 event.modifiers = modifiers;
12731 kbd_buffer_store_event (&event);
12732 f->last_tool_bar_item = -1;
12733 }
12734 }
12735
12736
12737 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12738 tool-bar window-relative coordinates X/Y. Called from
12739 note_mouse_highlight. */
12740
12741 static void
12742 note_tool_bar_highlight (struct frame *f, int x, int y)
12743 {
12744 Lisp_Object window = f->tool_bar_window;
12745 struct window *w = XWINDOW (window);
12746 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12747 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12748 int hpos, vpos;
12749 struct glyph *glyph;
12750 struct glyph_row *row;
12751 int i;
12752 Lisp_Object enabled_p;
12753 int prop_idx;
12754 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12755 bool mouse_down_p;
12756 int rc;
12757
12758 /* Function note_mouse_highlight is called with negative X/Y
12759 values when mouse moves outside of the frame. */
12760 if (x <= 0 || y <= 0)
12761 {
12762 clear_mouse_face (hlinfo);
12763 return;
12764 }
12765
12766 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12767 if (rc < 0)
12768 {
12769 /* Not on tool-bar item. */
12770 clear_mouse_face (hlinfo);
12771 return;
12772 }
12773 else if (rc == 0)
12774 /* On same tool-bar item as before. */
12775 goto set_help_echo;
12776
12777 clear_mouse_face (hlinfo);
12778
12779 /* Mouse is down, but on different tool-bar item? */
12780 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12781 && f == dpyinfo->last_mouse_frame);
12782
12783 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12784 return;
12785
12786 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12787
12788 /* If tool-bar item is not enabled, don't highlight it. */
12789 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12790 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12791 {
12792 /* Compute the x-position of the glyph. In front and past the
12793 image is a space. We include this in the highlighted area. */
12794 row = MATRIX_ROW (w->current_matrix, vpos);
12795 for (i = x = 0; i < hpos; ++i)
12796 x += row->glyphs[TEXT_AREA][i].pixel_width;
12797
12798 /* Record this as the current active region. */
12799 hlinfo->mouse_face_beg_col = hpos;
12800 hlinfo->mouse_face_beg_row = vpos;
12801 hlinfo->mouse_face_beg_x = x;
12802 hlinfo->mouse_face_past_end = false;
12803
12804 hlinfo->mouse_face_end_col = hpos + 1;
12805 hlinfo->mouse_face_end_row = vpos;
12806 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12807 hlinfo->mouse_face_window = window;
12808 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12809
12810 /* Display it as active. */
12811 show_mouse_face (hlinfo, draw);
12812 }
12813
12814 set_help_echo:
12815
12816 /* Set help_echo_string to a help string to display for this tool-bar item.
12817 XTread_socket does the rest. */
12818 help_echo_object = help_echo_window = Qnil;
12819 help_echo_pos = -1;
12820 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12821 if (NILP (help_echo_string))
12822 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12823 }
12824
12825 #endif /* !USE_GTK && !HAVE_NS */
12826
12827 #endif /* HAVE_WINDOW_SYSTEM */
12828
12829
12830 \f
12831 /************************************************************************
12832 Horizontal scrolling
12833 ************************************************************************/
12834
12835 /* For all leaf windows in the window tree rooted at WINDOW, set their
12836 hscroll value so that PT is (i) visible in the window, and (ii) so
12837 that it is not within a certain margin at the window's left and
12838 right border. Value is true if any window's hscroll has been
12839 changed. */
12840
12841 static bool
12842 hscroll_window_tree (Lisp_Object window)
12843 {
12844 bool hscrolled_p = false;
12845 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12846 int hscroll_step_abs = 0;
12847 double hscroll_step_rel = 0;
12848
12849 if (hscroll_relative_p)
12850 {
12851 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12852 if (hscroll_step_rel < 0)
12853 {
12854 hscroll_relative_p = false;
12855 hscroll_step_abs = 0;
12856 }
12857 }
12858 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12859 {
12860 hscroll_step_abs = XINT (Vhscroll_step);
12861 if (hscroll_step_abs < 0)
12862 hscroll_step_abs = 0;
12863 }
12864 else
12865 hscroll_step_abs = 0;
12866
12867 while (WINDOWP (window))
12868 {
12869 struct window *w = XWINDOW (window);
12870
12871 if (WINDOWP (w->contents))
12872 hscrolled_p |= hscroll_window_tree (w->contents);
12873 else if (w->cursor.vpos >= 0)
12874 {
12875 int h_margin;
12876 int text_area_width;
12877 struct glyph_row *cursor_row;
12878 struct glyph_row *bottom_row;
12879
12880 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12881 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12882 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12883 else
12884 cursor_row = bottom_row - 1;
12885
12886 if (!cursor_row->enabled_p)
12887 {
12888 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12889 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12890 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12891 else
12892 cursor_row = bottom_row - 1;
12893 }
12894 bool row_r2l_p = cursor_row->reversed_p;
12895
12896 text_area_width = window_box_width (w, TEXT_AREA);
12897
12898 /* Scroll when cursor is inside this scroll margin. */
12899 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12900
12901 /* If the position of this window's point has explicitly
12902 changed, no more suspend auto hscrolling. */
12903 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12904 w->suspend_auto_hscroll = false;
12905
12906 /* Remember window point. */
12907 Fset_marker (w->old_pointm,
12908 ((w == XWINDOW (selected_window))
12909 ? make_number (BUF_PT (XBUFFER (w->contents)))
12910 : Fmarker_position (w->pointm)),
12911 w->contents);
12912
12913 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12914 && !w->suspend_auto_hscroll
12915 /* In some pathological cases, like restoring a window
12916 configuration into a frame that is much smaller than
12917 the one from which the configuration was saved, we
12918 get glyph rows whose start and end have zero buffer
12919 positions, which we cannot handle below. Just skip
12920 such windows. */
12921 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12922 /* For left-to-right rows, hscroll when cursor is either
12923 (i) inside the right hscroll margin, or (ii) if it is
12924 inside the left margin and the window is already
12925 hscrolled. */
12926 && ((!row_r2l_p
12927 && ((w->hscroll && w->cursor.x <= h_margin)
12928 || (cursor_row->enabled_p
12929 && cursor_row->truncated_on_right_p
12930 && (w->cursor.x >= text_area_width - h_margin))))
12931 /* For right-to-left rows, the logic is similar,
12932 except that rules for scrolling to left and right
12933 are reversed. E.g., if cursor.x <= h_margin, we
12934 need to hscroll "to the right" unconditionally,
12935 and that will scroll the screen to the left so as
12936 to reveal the next portion of the row. */
12937 || (row_r2l_p
12938 && ((cursor_row->enabled_p
12939 /* FIXME: It is confusing to set the
12940 truncated_on_right_p flag when R2L rows
12941 are actually truncated on the left. */
12942 && cursor_row->truncated_on_right_p
12943 && w->cursor.x <= h_margin)
12944 || (w->hscroll
12945 && (w->cursor.x >= text_area_width - h_margin))))))
12946 {
12947 struct it it;
12948 ptrdiff_t hscroll;
12949 struct buffer *saved_current_buffer;
12950 ptrdiff_t pt;
12951 int wanted_x;
12952
12953 /* Find point in a display of infinite width. */
12954 saved_current_buffer = current_buffer;
12955 current_buffer = XBUFFER (w->contents);
12956
12957 if (w == XWINDOW (selected_window))
12958 pt = PT;
12959 else
12960 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12961
12962 /* Move iterator to pt starting at cursor_row->start in
12963 a line with infinite width. */
12964 init_to_row_start (&it, w, cursor_row);
12965 it.last_visible_x = INFINITY;
12966 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12967 current_buffer = saved_current_buffer;
12968
12969 /* Position cursor in window. */
12970 if (!hscroll_relative_p && hscroll_step_abs == 0)
12971 hscroll = max (0, (it.current_x
12972 - (ITERATOR_AT_END_OF_LINE_P (&it)
12973 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12974 : (text_area_width / 2))))
12975 / FRAME_COLUMN_WIDTH (it.f);
12976 else if ((!row_r2l_p
12977 && w->cursor.x >= text_area_width - h_margin)
12978 || (row_r2l_p && w->cursor.x <= h_margin))
12979 {
12980 if (hscroll_relative_p)
12981 wanted_x = text_area_width * (1 - hscroll_step_rel)
12982 - h_margin;
12983 else
12984 wanted_x = text_area_width
12985 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12986 - h_margin;
12987 hscroll
12988 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12989 }
12990 else
12991 {
12992 if (hscroll_relative_p)
12993 wanted_x = text_area_width * hscroll_step_rel
12994 + h_margin;
12995 else
12996 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12997 + h_margin;
12998 hscroll
12999 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13000 }
13001 hscroll = max (hscroll, w->min_hscroll);
13002
13003 /* Don't prevent redisplay optimizations if hscroll
13004 hasn't changed, as it will unnecessarily slow down
13005 redisplay. */
13006 if (w->hscroll != hscroll)
13007 {
13008 struct buffer *b = XBUFFER (w->contents);
13009 b->prevent_redisplay_optimizations_p = true;
13010 w->hscroll = hscroll;
13011 hscrolled_p = true;
13012 }
13013 }
13014 }
13015
13016 window = w->next;
13017 }
13018
13019 /* Value is true if hscroll of any leaf window has been changed. */
13020 return hscrolled_p;
13021 }
13022
13023
13024 /* Set hscroll so that cursor is visible and not inside horizontal
13025 scroll margins for all windows in the tree rooted at WINDOW. See
13026 also hscroll_window_tree above. Value is true if any window's
13027 hscroll has been changed. If it has, desired matrices on the frame
13028 of WINDOW are cleared. */
13029
13030 static bool
13031 hscroll_windows (Lisp_Object window)
13032 {
13033 bool hscrolled_p = hscroll_window_tree (window);
13034 if (hscrolled_p)
13035 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13036 return hscrolled_p;
13037 }
13038
13039
13040 \f
13041 /************************************************************************
13042 Redisplay
13043 ************************************************************************/
13044
13045 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13046 This is sometimes handy to have in a debugger session. */
13047
13048 #ifdef GLYPH_DEBUG
13049
13050 /* First and last unchanged row for try_window_id. */
13051
13052 static int debug_first_unchanged_at_end_vpos;
13053 static int debug_last_unchanged_at_beg_vpos;
13054
13055 /* Delta vpos and y. */
13056
13057 static int debug_dvpos, debug_dy;
13058
13059 /* Delta in characters and bytes for try_window_id. */
13060
13061 static ptrdiff_t debug_delta, debug_delta_bytes;
13062
13063 /* Values of window_end_pos and window_end_vpos at the end of
13064 try_window_id. */
13065
13066 static ptrdiff_t debug_end_vpos;
13067
13068 /* Append a string to W->desired_matrix->method. FMT is a printf
13069 format string. If trace_redisplay_p is true also printf the
13070 resulting string to stderr. */
13071
13072 static void debug_method_add (struct window *, char const *, ...)
13073 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13074
13075 static void
13076 debug_method_add (struct window *w, char const *fmt, ...)
13077 {
13078 void *ptr = w;
13079 char *method = w->desired_matrix->method;
13080 int len = strlen (method);
13081 int size = sizeof w->desired_matrix->method;
13082 int remaining = size - len - 1;
13083 va_list ap;
13084
13085 if (len && remaining)
13086 {
13087 method[len] = '|';
13088 --remaining, ++len;
13089 }
13090
13091 va_start (ap, fmt);
13092 vsnprintf (method + len, remaining + 1, fmt, ap);
13093 va_end (ap);
13094
13095 if (trace_redisplay_p)
13096 fprintf (stderr, "%p (%s): %s\n",
13097 ptr,
13098 ((BUFFERP (w->contents)
13099 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13100 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13101 : "no buffer"),
13102 method + len);
13103 }
13104
13105 #endif /* GLYPH_DEBUG */
13106
13107
13108 /* Value is true if all changes in window W, which displays
13109 current_buffer, are in the text between START and END. START is a
13110 buffer position, END is given as a distance from Z. Used in
13111 redisplay_internal for display optimization. */
13112
13113 static bool
13114 text_outside_line_unchanged_p (struct window *w,
13115 ptrdiff_t start, ptrdiff_t end)
13116 {
13117 bool unchanged_p = true;
13118
13119 /* If text or overlays have changed, see where. */
13120 if (window_outdated (w))
13121 {
13122 /* Gap in the line? */
13123 if (GPT < start || Z - GPT < end)
13124 unchanged_p = false;
13125
13126 /* Changes start in front of the line, or end after it? */
13127 if (unchanged_p
13128 && (BEG_UNCHANGED < start - 1
13129 || END_UNCHANGED < end))
13130 unchanged_p = false;
13131
13132 /* If selective display, can't optimize if changes start at the
13133 beginning of the line. */
13134 if (unchanged_p
13135 && INTEGERP (BVAR (current_buffer, selective_display))
13136 && XINT (BVAR (current_buffer, selective_display)) > 0
13137 && (BEG_UNCHANGED < start || GPT <= start))
13138 unchanged_p = false;
13139
13140 /* If there are overlays at the start or end of the line, these
13141 may have overlay strings with newlines in them. A change at
13142 START, for instance, may actually concern the display of such
13143 overlay strings as well, and they are displayed on different
13144 lines. So, quickly rule out this case. (For the future, it
13145 might be desirable to implement something more telling than
13146 just BEG/END_UNCHANGED.) */
13147 if (unchanged_p)
13148 {
13149 if (BEG + BEG_UNCHANGED == start
13150 && overlay_touches_p (start))
13151 unchanged_p = false;
13152 if (END_UNCHANGED == end
13153 && overlay_touches_p (Z - end))
13154 unchanged_p = false;
13155 }
13156
13157 /* Under bidi reordering, adding or deleting a character in the
13158 beginning of a paragraph, before the first strong directional
13159 character, can change the base direction of the paragraph (unless
13160 the buffer specifies a fixed paragraph direction), which will
13161 require to redisplay the whole paragraph. It might be worthwhile
13162 to find the paragraph limits and widen the range of redisplayed
13163 lines to that, but for now just give up this optimization. */
13164 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13165 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13166 unchanged_p = false;
13167 }
13168
13169 return unchanged_p;
13170 }
13171
13172
13173 /* Do a frame update, taking possible shortcuts into account. This is
13174 the main external entry point for redisplay.
13175
13176 If the last redisplay displayed an echo area message and that message
13177 is no longer requested, we clear the echo area or bring back the
13178 mini-buffer if that is in use. */
13179
13180 void
13181 redisplay (void)
13182 {
13183 redisplay_internal ();
13184 }
13185
13186
13187 static Lisp_Object
13188 overlay_arrow_string_or_property (Lisp_Object var)
13189 {
13190 Lisp_Object val;
13191
13192 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13193 return val;
13194
13195 return Voverlay_arrow_string;
13196 }
13197
13198 /* Return true if there are any overlay-arrows in current_buffer. */
13199 static bool
13200 overlay_arrow_in_current_buffer_p (void)
13201 {
13202 Lisp_Object vlist;
13203
13204 for (vlist = Voverlay_arrow_variable_list;
13205 CONSP (vlist);
13206 vlist = XCDR (vlist))
13207 {
13208 Lisp_Object var = XCAR (vlist);
13209 Lisp_Object val;
13210
13211 if (!SYMBOLP (var))
13212 continue;
13213 val = find_symbol_value (var);
13214 if (MARKERP (val)
13215 && current_buffer == XMARKER (val)->buffer)
13216 return true;
13217 }
13218 return false;
13219 }
13220
13221
13222 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13223 has changed. */
13224
13225 static bool
13226 overlay_arrows_changed_p (void)
13227 {
13228 Lisp_Object vlist;
13229
13230 for (vlist = Voverlay_arrow_variable_list;
13231 CONSP (vlist);
13232 vlist = XCDR (vlist))
13233 {
13234 Lisp_Object var = XCAR (vlist);
13235 Lisp_Object val, pstr;
13236
13237 if (!SYMBOLP (var))
13238 continue;
13239 val = find_symbol_value (var);
13240 if (!MARKERP (val))
13241 continue;
13242 if (! EQ (COERCE_MARKER (val),
13243 Fget (var, Qlast_arrow_position))
13244 || ! (pstr = overlay_arrow_string_or_property (var),
13245 EQ (pstr, Fget (var, Qlast_arrow_string))))
13246 return true;
13247 }
13248 return false;
13249 }
13250
13251 /* Mark overlay arrows to be updated on next redisplay. */
13252
13253 static void
13254 update_overlay_arrows (int up_to_date)
13255 {
13256 Lisp_Object vlist;
13257
13258 for (vlist = Voverlay_arrow_variable_list;
13259 CONSP (vlist);
13260 vlist = XCDR (vlist))
13261 {
13262 Lisp_Object var = XCAR (vlist);
13263
13264 if (!SYMBOLP (var))
13265 continue;
13266
13267 if (up_to_date > 0)
13268 {
13269 Lisp_Object val = find_symbol_value (var);
13270 Fput (var, Qlast_arrow_position,
13271 COERCE_MARKER (val));
13272 Fput (var, Qlast_arrow_string,
13273 overlay_arrow_string_or_property (var));
13274 }
13275 else if (up_to_date < 0
13276 || !NILP (Fget (var, Qlast_arrow_position)))
13277 {
13278 Fput (var, Qlast_arrow_position, Qt);
13279 Fput (var, Qlast_arrow_string, Qt);
13280 }
13281 }
13282 }
13283
13284
13285 /* Return overlay arrow string to display at row.
13286 Return integer (bitmap number) for arrow bitmap in left fringe.
13287 Return nil if no overlay arrow. */
13288
13289 static Lisp_Object
13290 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13291 {
13292 Lisp_Object vlist;
13293
13294 for (vlist = Voverlay_arrow_variable_list;
13295 CONSP (vlist);
13296 vlist = XCDR (vlist))
13297 {
13298 Lisp_Object var = XCAR (vlist);
13299 Lisp_Object val;
13300
13301 if (!SYMBOLP (var))
13302 continue;
13303
13304 val = find_symbol_value (var);
13305
13306 if (MARKERP (val)
13307 && current_buffer == XMARKER (val)->buffer
13308 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13309 {
13310 if (FRAME_WINDOW_P (it->f)
13311 /* FIXME: if ROW->reversed_p is set, this should test
13312 the right fringe, not the left one. */
13313 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13314 {
13315 #ifdef HAVE_WINDOW_SYSTEM
13316 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13317 {
13318 int fringe_bitmap = lookup_fringe_bitmap (val);
13319 if (fringe_bitmap != 0)
13320 return make_number (fringe_bitmap);
13321 }
13322 #endif
13323 return make_number (-1); /* Use default arrow bitmap. */
13324 }
13325 return overlay_arrow_string_or_property (var);
13326 }
13327 }
13328
13329 return Qnil;
13330 }
13331
13332 /* Return true if point moved out of or into a composition. Otherwise
13333 return false. PREV_BUF and PREV_PT are the last point buffer and
13334 position. BUF and PT are the current point buffer and position. */
13335
13336 static bool
13337 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13338 struct buffer *buf, ptrdiff_t pt)
13339 {
13340 ptrdiff_t start, end;
13341 Lisp_Object prop;
13342 Lisp_Object buffer;
13343
13344 XSETBUFFER (buffer, buf);
13345 /* Check a composition at the last point if point moved within the
13346 same buffer. */
13347 if (prev_buf == buf)
13348 {
13349 if (prev_pt == pt)
13350 /* Point didn't move. */
13351 return false;
13352
13353 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13354 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13355 && composition_valid_p (start, end, prop)
13356 && start < prev_pt && end > prev_pt)
13357 /* The last point was within the composition. Return true iff
13358 point moved out of the composition. */
13359 return (pt <= start || pt >= end);
13360 }
13361
13362 /* Check a composition at the current point. */
13363 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13364 && find_composition (pt, -1, &start, &end, &prop, buffer)
13365 && composition_valid_p (start, end, prop)
13366 && start < pt && end > pt);
13367 }
13368
13369 /* Reconsider the clip changes of buffer which is displayed in W. */
13370
13371 static void
13372 reconsider_clip_changes (struct window *w)
13373 {
13374 struct buffer *b = XBUFFER (w->contents);
13375
13376 if (b->clip_changed
13377 && w->window_end_valid
13378 && w->current_matrix->buffer == b
13379 && w->current_matrix->zv == BUF_ZV (b)
13380 && w->current_matrix->begv == BUF_BEGV (b))
13381 b->clip_changed = false;
13382
13383 /* If display wasn't paused, and W is not a tool bar window, see if
13384 point has been moved into or out of a composition. In that case,
13385 set b->clip_changed to force updating the screen. If
13386 b->clip_changed has already been set, skip this check. */
13387 if (!b->clip_changed && w->window_end_valid)
13388 {
13389 ptrdiff_t pt = (w == XWINDOW (selected_window)
13390 ? PT : marker_position (w->pointm));
13391
13392 if ((w->current_matrix->buffer != b || pt != w->last_point)
13393 && check_point_in_composition (w->current_matrix->buffer,
13394 w->last_point, b, pt))
13395 b->clip_changed = true;
13396 }
13397 }
13398
13399 static void
13400 propagate_buffer_redisplay (void)
13401 { /* Resetting b->text->redisplay is problematic!
13402 We can't just reset it in the case that some window that displays
13403 it has not been redisplayed; and such a window can stay
13404 unredisplayed for a long time if it's currently invisible.
13405 But we do want to reset it at the end of redisplay otherwise
13406 its displayed windows will keep being redisplayed over and over
13407 again.
13408 So we copy all b->text->redisplay flags up to their windows here,
13409 such that mark_window_display_accurate can safely reset
13410 b->text->redisplay. */
13411 Lisp_Object ws = window_list ();
13412 for (; CONSP (ws); ws = XCDR (ws))
13413 {
13414 struct window *thisw = XWINDOW (XCAR (ws));
13415 struct buffer *thisb = XBUFFER (thisw->contents);
13416 if (thisb->text->redisplay)
13417 thisw->redisplay = true;
13418 }
13419 }
13420
13421 #define STOP_POLLING \
13422 do { if (! polling_stopped_here) stop_polling (); \
13423 polling_stopped_here = true; } while (false)
13424
13425 #define RESUME_POLLING \
13426 do { if (polling_stopped_here) start_polling (); \
13427 polling_stopped_here = false; } while (false)
13428
13429
13430 /* Perhaps in the future avoid recentering windows if it
13431 is not necessary; currently that causes some problems. */
13432
13433 static void
13434 redisplay_internal (void)
13435 {
13436 struct window *w = XWINDOW (selected_window);
13437 struct window *sw;
13438 struct frame *fr;
13439 bool pending;
13440 bool must_finish = false, match_p;
13441 struct text_pos tlbufpos, tlendpos;
13442 int number_of_visible_frames;
13443 ptrdiff_t count;
13444 struct frame *sf;
13445 bool polling_stopped_here = false;
13446 Lisp_Object tail, frame;
13447
13448 /* True means redisplay has to consider all windows on all
13449 frames. False, only selected_window is considered. */
13450 bool consider_all_windows_p;
13451
13452 /* True means redisplay has to redisplay the miniwindow. */
13453 bool update_miniwindow_p = false;
13454
13455 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13456
13457 /* No redisplay if running in batch mode or frame is not yet fully
13458 initialized, or redisplay is explicitly turned off by setting
13459 Vinhibit_redisplay. */
13460 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13461 || !NILP (Vinhibit_redisplay))
13462 return;
13463
13464 /* Don't examine these until after testing Vinhibit_redisplay.
13465 When Emacs is shutting down, perhaps because its connection to
13466 X has dropped, we should not look at them at all. */
13467 fr = XFRAME (w->frame);
13468 sf = SELECTED_FRAME ();
13469
13470 if (!fr->glyphs_initialized_p)
13471 return;
13472
13473 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13474 if (popup_activated ())
13475 return;
13476 #endif
13477
13478 /* I don't think this happens but let's be paranoid. */
13479 if (redisplaying_p)
13480 return;
13481
13482 /* Record a function that clears redisplaying_p
13483 when we leave this function. */
13484 count = SPECPDL_INDEX ();
13485 record_unwind_protect_void (unwind_redisplay);
13486 redisplaying_p = true;
13487 specbind (Qinhibit_free_realized_faces, Qnil);
13488
13489 /* Record this function, so it appears on the profiler's backtraces. */
13490 record_in_backtrace (Qredisplay_internal, 0, 0);
13491
13492 FOR_EACH_FRAME (tail, frame)
13493 XFRAME (frame)->already_hscrolled_p = false;
13494
13495 retry:
13496 /* Remember the currently selected window. */
13497 sw = w;
13498
13499 pending = false;
13500 forget_escape_and_glyphless_faces ();
13501
13502 inhibit_free_realized_faces = false;
13503
13504 /* If face_change, init_iterator will free all realized faces, which
13505 includes the faces referenced from current matrices. So, we
13506 can't reuse current matrices in this case. */
13507 if (face_change)
13508 windows_or_buffers_changed = 47;
13509
13510 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13511 && FRAME_TTY (sf)->previous_frame != sf)
13512 {
13513 /* Since frames on a single ASCII terminal share the same
13514 display area, displaying a different frame means redisplay
13515 the whole thing. */
13516 SET_FRAME_GARBAGED (sf);
13517 #ifndef DOS_NT
13518 set_tty_color_mode (FRAME_TTY (sf), sf);
13519 #endif
13520 FRAME_TTY (sf)->previous_frame = sf;
13521 }
13522
13523 /* Set the visible flags for all frames. Do this before checking for
13524 resized or garbaged frames; they want to know if their frames are
13525 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13526 number_of_visible_frames = 0;
13527
13528 FOR_EACH_FRAME (tail, frame)
13529 {
13530 struct frame *f = XFRAME (frame);
13531
13532 if (FRAME_VISIBLE_P (f))
13533 {
13534 ++number_of_visible_frames;
13535 /* Adjust matrices for visible frames only. */
13536 if (f->fonts_changed)
13537 {
13538 adjust_frame_glyphs (f);
13539 /* Disable all redisplay optimizations for this frame.
13540 This is because adjust_frame_glyphs resets the
13541 enabled_p flag for all glyph rows of all windows, so
13542 many optimizations will fail anyway, and some might
13543 fail to test that flag and do bogus things as
13544 result. */
13545 SET_FRAME_GARBAGED (f);
13546 f->fonts_changed = false;
13547 }
13548 /* If cursor type has been changed on the frame
13549 other than selected, consider all frames. */
13550 if (f != sf && f->cursor_type_changed)
13551 fset_redisplay (f);
13552 }
13553 clear_desired_matrices (f);
13554 }
13555
13556 /* Notice any pending interrupt request to change frame size. */
13557 do_pending_window_change (true);
13558
13559 /* do_pending_window_change could change the selected_window due to
13560 frame resizing which makes the selected window too small. */
13561 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13562 sw = w;
13563
13564 /* Clear frames marked as garbaged. */
13565 clear_garbaged_frames ();
13566
13567 /* Build menubar and tool-bar items. */
13568 if (NILP (Vmemory_full))
13569 prepare_menu_bars ();
13570
13571 reconsider_clip_changes (w);
13572
13573 /* In most cases selected window displays current buffer. */
13574 match_p = XBUFFER (w->contents) == current_buffer;
13575 if (match_p)
13576 {
13577 /* Detect case that we need to write or remove a star in the mode line. */
13578 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13579 w->update_mode_line = true;
13580
13581 if (mode_line_update_needed (w))
13582 w->update_mode_line = true;
13583
13584 /* If reconsider_clip_changes above decided that the narrowing
13585 in the current buffer changed, make sure all other windows
13586 showing that buffer will be redisplayed. */
13587 if (current_buffer->clip_changed)
13588 bset_update_mode_line (current_buffer);
13589 }
13590
13591 /* Normally the message* functions will have already displayed and
13592 updated the echo area, but the frame may have been trashed, or
13593 the update may have been preempted, so display the echo area
13594 again here. Checking message_cleared_p captures the case that
13595 the echo area should be cleared. */
13596 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13597 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13598 || (message_cleared_p
13599 && minibuf_level == 0
13600 /* If the mini-window is currently selected, this means the
13601 echo-area doesn't show through. */
13602 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13603 {
13604 echo_area_display (false);
13605
13606 /* If echo_area_display resizes the mini-window, the redisplay and
13607 window_sizes_changed flags of the selected frame are set, but
13608 it's too late for the hooks in window-size-change-functions,
13609 which have been examined already in prepare_menu_bars. So in
13610 that case we call the hooks here only for the selected frame. */
13611 if (sf->redisplay && FRAME_WINDOW_SIZES_CHANGED (sf))
13612 {
13613 Lisp_Object functions;
13614 ptrdiff_t count1 = SPECPDL_INDEX ();
13615
13616 record_unwind_save_match_data ();
13617
13618 /* Clear flag first in case we get an error below. */
13619 FRAME_WINDOW_SIZES_CHANGED (sf) = false;
13620 functions = Vwindow_size_change_functions;
13621
13622 while (CONSP (functions))
13623 {
13624 if (!EQ (XCAR (functions), Qt))
13625 call1 (XCAR (functions), selected_frame);
13626 functions = XCDR (functions);
13627 }
13628
13629 unbind_to (count1, Qnil);
13630 }
13631
13632 if (message_cleared_p)
13633 update_miniwindow_p = true;
13634
13635 must_finish = true;
13636
13637 /* If we don't display the current message, don't clear the
13638 message_cleared_p flag, because, if we did, we wouldn't clear
13639 the echo area in the next redisplay which doesn't preserve
13640 the echo area. */
13641 if (!display_last_displayed_message_p)
13642 message_cleared_p = false;
13643 }
13644 else if (EQ (selected_window, minibuf_window)
13645 && (current_buffer->clip_changed || window_outdated (w))
13646 && resize_mini_window (w, false))
13647 {
13648 if (sf->redisplay)
13649 {
13650 Lisp_Object functions;
13651 ptrdiff_t count1 = SPECPDL_INDEX ();
13652
13653 record_unwind_save_match_data ();
13654
13655 /* Clear flag first in case we get an error below. */
13656 FRAME_WINDOW_SIZES_CHANGED (sf) = false;
13657 functions = Vwindow_size_change_functions;
13658
13659 while (CONSP (functions))
13660 {
13661 if (!EQ (XCAR (functions), Qt))
13662 call1 (XCAR (functions), selected_frame);
13663 functions = XCDR (functions);
13664 }
13665
13666 unbind_to (count1, Qnil);
13667 }
13668
13669 /* Resized active mini-window to fit the size of what it is
13670 showing if its contents might have changed. */
13671 must_finish = true;
13672
13673 /* If window configuration was changed, frames may have been
13674 marked garbaged. Clear them or we will experience
13675 surprises wrt scrolling. */
13676 clear_garbaged_frames ();
13677 }
13678
13679 if (windows_or_buffers_changed && !update_mode_lines)
13680 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13681 only the windows's contents needs to be refreshed, or whether the
13682 mode-lines also need a refresh. */
13683 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13684 ? REDISPLAY_SOME : 32);
13685
13686 /* If specs for an arrow have changed, do thorough redisplay
13687 to ensure we remove any arrow that should no longer exist. */
13688 if (overlay_arrows_changed_p ())
13689 /* Apparently, this is the only case where we update other windows,
13690 without updating other mode-lines. */
13691 windows_or_buffers_changed = 49;
13692
13693 consider_all_windows_p = (update_mode_lines
13694 || windows_or_buffers_changed);
13695
13696 #define AINC(a,i) \
13697 { \
13698 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13699 if (INTEGERP (entry)) \
13700 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13701 }
13702
13703 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13704 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13705
13706 /* Optimize the case that only the line containing the cursor in the
13707 selected window has changed. Variables starting with this_ are
13708 set in display_line and record information about the line
13709 containing the cursor. */
13710 tlbufpos = this_line_start_pos;
13711 tlendpos = this_line_end_pos;
13712 if (!consider_all_windows_p
13713 && CHARPOS (tlbufpos) > 0
13714 && !w->update_mode_line
13715 && !current_buffer->clip_changed
13716 && !current_buffer->prevent_redisplay_optimizations_p
13717 && FRAME_VISIBLE_P (XFRAME (w->frame))
13718 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13719 && !XFRAME (w->frame)->cursor_type_changed
13720 && !XFRAME (w->frame)->face_change
13721 /* Make sure recorded data applies to current buffer, etc. */
13722 && this_line_buffer == current_buffer
13723 && match_p
13724 && !w->force_start
13725 && !w->optional_new_start
13726 /* Point must be on the line that we have info recorded about. */
13727 && PT >= CHARPOS (tlbufpos)
13728 && PT <= Z - CHARPOS (tlendpos)
13729 /* All text outside that line, including its final newline,
13730 must be unchanged. */
13731 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13732 CHARPOS (tlendpos)))
13733 {
13734 if (CHARPOS (tlbufpos) > BEGV
13735 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13736 && (CHARPOS (tlbufpos) == ZV
13737 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13738 /* Former continuation line has disappeared by becoming empty. */
13739 goto cancel;
13740 else if (window_outdated (w) || MINI_WINDOW_P (w))
13741 {
13742 /* We have to handle the case of continuation around a
13743 wide-column character (see the comment in indent.c around
13744 line 1340).
13745
13746 For instance, in the following case:
13747
13748 -------- Insert --------
13749 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13750 J_I_ ==> J_I_ `^^' are cursors.
13751 ^^ ^^
13752 -------- --------
13753
13754 As we have to redraw the line above, we cannot use this
13755 optimization. */
13756
13757 struct it it;
13758 int line_height_before = this_line_pixel_height;
13759
13760 /* Note that start_display will handle the case that the
13761 line starting at tlbufpos is a continuation line. */
13762 start_display (&it, w, tlbufpos);
13763
13764 /* Implementation note: It this still necessary? */
13765 if (it.current_x != this_line_start_x)
13766 goto cancel;
13767
13768 TRACE ((stderr, "trying display optimization 1\n"));
13769 w->cursor.vpos = -1;
13770 overlay_arrow_seen = false;
13771 it.vpos = this_line_vpos;
13772 it.current_y = this_line_y;
13773 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13774 display_line (&it);
13775
13776 /* If line contains point, is not continued,
13777 and ends at same distance from eob as before, we win. */
13778 if (w->cursor.vpos >= 0
13779 /* Line is not continued, otherwise this_line_start_pos
13780 would have been set to 0 in display_line. */
13781 && CHARPOS (this_line_start_pos)
13782 /* Line ends as before. */
13783 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13784 /* Line has same height as before. Otherwise other lines
13785 would have to be shifted up or down. */
13786 && this_line_pixel_height == line_height_before)
13787 {
13788 /* If this is not the window's last line, we must adjust
13789 the charstarts of the lines below. */
13790 if (it.current_y < it.last_visible_y)
13791 {
13792 struct glyph_row *row
13793 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13794 ptrdiff_t delta, delta_bytes;
13795
13796 /* We used to distinguish between two cases here,
13797 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13798 when the line ends in a newline or the end of the
13799 buffer's accessible portion. But both cases did
13800 the same, so they were collapsed. */
13801 delta = (Z
13802 - CHARPOS (tlendpos)
13803 - MATRIX_ROW_START_CHARPOS (row));
13804 delta_bytes = (Z_BYTE
13805 - BYTEPOS (tlendpos)
13806 - MATRIX_ROW_START_BYTEPOS (row));
13807
13808 increment_matrix_positions (w->current_matrix,
13809 this_line_vpos + 1,
13810 w->current_matrix->nrows,
13811 delta, delta_bytes);
13812 }
13813
13814 /* If this row displays text now but previously didn't,
13815 or vice versa, w->window_end_vpos may have to be
13816 adjusted. */
13817 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13818 {
13819 if (w->window_end_vpos < this_line_vpos)
13820 w->window_end_vpos = this_line_vpos;
13821 }
13822 else if (w->window_end_vpos == this_line_vpos
13823 && this_line_vpos > 0)
13824 w->window_end_vpos = this_line_vpos - 1;
13825 w->window_end_valid = false;
13826
13827 /* Update hint: No need to try to scroll in update_window. */
13828 w->desired_matrix->no_scrolling_p = true;
13829
13830 #ifdef GLYPH_DEBUG
13831 *w->desired_matrix->method = 0;
13832 debug_method_add (w, "optimization 1");
13833 #endif
13834 #ifdef HAVE_WINDOW_SYSTEM
13835 update_window_fringes (w, false);
13836 #endif
13837 goto update;
13838 }
13839 else
13840 goto cancel;
13841 }
13842 else if (/* Cursor position hasn't changed. */
13843 PT == w->last_point
13844 /* Make sure the cursor was last displayed
13845 in this window. Otherwise we have to reposition it. */
13846
13847 /* PXW: Must be converted to pixels, probably. */
13848 && 0 <= w->cursor.vpos
13849 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13850 {
13851 if (!must_finish)
13852 {
13853 do_pending_window_change (true);
13854 /* If selected_window changed, redisplay again. */
13855 if (WINDOWP (selected_window)
13856 && (w = XWINDOW (selected_window)) != sw)
13857 goto retry;
13858
13859 /* We used to always goto end_of_redisplay here, but this
13860 isn't enough if we have a blinking cursor. */
13861 if (w->cursor_off_p == w->last_cursor_off_p)
13862 goto end_of_redisplay;
13863 }
13864 goto update;
13865 }
13866 /* If highlighting the region, or if the cursor is in the echo area,
13867 then we can't just move the cursor. */
13868 else if (NILP (Vshow_trailing_whitespace)
13869 && !cursor_in_echo_area)
13870 {
13871 struct it it;
13872 struct glyph_row *row;
13873
13874 /* Skip from tlbufpos to PT and see where it is. Note that
13875 PT may be in invisible text. If so, we will end at the
13876 next visible position. */
13877 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13878 NULL, DEFAULT_FACE_ID);
13879 it.current_x = this_line_start_x;
13880 it.current_y = this_line_y;
13881 it.vpos = this_line_vpos;
13882
13883 /* The call to move_it_to stops in front of PT, but
13884 moves over before-strings. */
13885 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13886
13887 if (it.vpos == this_line_vpos
13888 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13889 row->enabled_p))
13890 {
13891 eassert (this_line_vpos == it.vpos);
13892 eassert (this_line_y == it.current_y);
13893 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13894 #ifdef GLYPH_DEBUG
13895 *w->desired_matrix->method = 0;
13896 debug_method_add (w, "optimization 3");
13897 #endif
13898 goto update;
13899 }
13900 else
13901 goto cancel;
13902 }
13903
13904 cancel:
13905 /* Text changed drastically or point moved off of line. */
13906 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13907 }
13908
13909 CHARPOS (this_line_start_pos) = 0;
13910 ++clear_face_cache_count;
13911 #ifdef HAVE_WINDOW_SYSTEM
13912 ++clear_image_cache_count;
13913 #endif
13914
13915 /* Build desired matrices, and update the display. If
13916 consider_all_windows_p, do it for all windows on all frames that
13917 require redisplay, as specified by their 'redisplay' flag.
13918 Otherwise do it for selected_window, only. */
13919
13920 if (consider_all_windows_p)
13921 {
13922 FOR_EACH_FRAME (tail, frame)
13923 XFRAME (frame)->updated_p = false;
13924
13925 propagate_buffer_redisplay ();
13926
13927 FOR_EACH_FRAME (tail, frame)
13928 {
13929 struct frame *f = XFRAME (frame);
13930
13931 /* We don't have to do anything for unselected terminal
13932 frames. */
13933 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13934 && !EQ (FRAME_TTY (f)->top_frame, frame))
13935 continue;
13936
13937 retry_frame:
13938 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13939 {
13940 bool gcscrollbars
13941 /* Only GC scrollbars when we redisplay the whole frame. */
13942 = f->redisplay || !REDISPLAY_SOME_P ();
13943 bool f_redisplay_flag = f->redisplay;
13944 /* Mark all the scroll bars to be removed; we'll redeem
13945 the ones we want when we redisplay their windows. */
13946 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13947 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13948
13949 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13950 redisplay_windows (FRAME_ROOT_WINDOW (f));
13951 /* Remember that the invisible frames need to be redisplayed next
13952 time they're visible. */
13953 else if (!REDISPLAY_SOME_P ())
13954 f->redisplay = true;
13955
13956 /* The X error handler may have deleted that frame. */
13957 if (!FRAME_LIVE_P (f))
13958 continue;
13959
13960 /* Any scroll bars which redisplay_windows should have
13961 nuked should now go away. */
13962 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13963 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13964
13965 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13966 {
13967 /* If fonts changed on visible frame, display again. */
13968 if (f->fonts_changed)
13969 {
13970 adjust_frame_glyphs (f);
13971 /* Disable all redisplay optimizations for this
13972 frame. For the reasons, see the comment near
13973 the previous call to adjust_frame_glyphs above. */
13974 SET_FRAME_GARBAGED (f);
13975 f->fonts_changed = false;
13976 goto retry_frame;
13977 }
13978
13979 /* See if we have to hscroll. */
13980 if (!f->already_hscrolled_p)
13981 {
13982 f->already_hscrolled_p = true;
13983 if (hscroll_windows (f->root_window))
13984 goto retry_frame;
13985 }
13986
13987 /* If the frame's redisplay flag was not set before
13988 we went about redisplaying its windows, but it is
13989 set now, that means we employed some redisplay
13990 optimizations inside redisplay_windows, and
13991 bypassed producing some screen lines. But if
13992 f->redisplay is now set, it might mean the old
13993 faces are no longer valid (e.g., if redisplaying
13994 some window called some Lisp which defined a new
13995 face or redefined an existing face), so trying to
13996 use them in update_frame will segfault.
13997 Therefore, we must redisplay this frame. */
13998 if (!f_redisplay_flag && f->redisplay)
13999 goto retry_frame;
14000
14001 /* Prevent various kinds of signals during display
14002 update. stdio is not robust about handling
14003 signals, which can cause an apparent I/O error. */
14004 if (interrupt_input)
14005 unrequest_sigio ();
14006 STOP_POLLING;
14007
14008 pending |= update_frame (f, false, false);
14009 f->cursor_type_changed = false;
14010 f->updated_p = true;
14011 }
14012 }
14013 }
14014
14015 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14016
14017 if (!pending)
14018 {
14019 /* Do the mark_window_display_accurate after all windows have
14020 been redisplayed because this call resets flags in buffers
14021 which are needed for proper redisplay. */
14022 FOR_EACH_FRAME (tail, frame)
14023 {
14024 struct frame *f = XFRAME (frame);
14025 if (f->updated_p)
14026 {
14027 f->redisplay = false;
14028 mark_window_display_accurate (f->root_window, true);
14029 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14030 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14031 }
14032 }
14033 }
14034 }
14035 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14036 {
14037 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14038 struct frame *mini_frame;
14039
14040 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14041 /* Use list_of_error, not Qerror, so that
14042 we catch only errors and don't run the debugger. */
14043 internal_condition_case_1 (redisplay_window_1, selected_window,
14044 list_of_error,
14045 redisplay_window_error);
14046 if (update_miniwindow_p)
14047 internal_condition_case_1 (redisplay_window_1, mini_window,
14048 list_of_error,
14049 redisplay_window_error);
14050
14051 /* Compare desired and current matrices, perform output. */
14052
14053 update:
14054 /* If fonts changed, display again. Likewise if redisplay_window_1
14055 above caused some change (e.g., a change in faces) that requires
14056 considering the entire frame again. */
14057 if (sf->fonts_changed || sf->redisplay)
14058 {
14059 if (sf->redisplay)
14060 {
14061 /* Set this to force a more thorough redisplay.
14062 Otherwise, we might immediately loop back to the
14063 above "else-if" clause (since all the conditions that
14064 led here might still be true), and we will then
14065 infloop, because the selected-frame's redisplay flag
14066 is not (and cannot be) reset. */
14067 windows_or_buffers_changed = 50;
14068 }
14069 goto retry;
14070 }
14071
14072 /* Prevent freeing of realized faces, since desired matrices are
14073 pending that reference the faces we computed and cached. */
14074 inhibit_free_realized_faces = true;
14075
14076 /* Prevent various kinds of signals during display update.
14077 stdio is not robust about handling signals,
14078 which can cause an apparent I/O error. */
14079 if (interrupt_input)
14080 unrequest_sigio ();
14081 STOP_POLLING;
14082
14083 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14084 {
14085 if (hscroll_windows (selected_window))
14086 goto retry;
14087
14088 XWINDOW (selected_window)->must_be_updated_p = true;
14089 pending = update_frame (sf, false, false);
14090 sf->cursor_type_changed = false;
14091 }
14092
14093 /* We may have called echo_area_display at the top of this
14094 function. If the echo area is on another frame, that may
14095 have put text on a frame other than the selected one, so the
14096 above call to update_frame would not have caught it. Catch
14097 it here. */
14098 mini_window = FRAME_MINIBUF_WINDOW (sf);
14099 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14100
14101 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14102 {
14103 XWINDOW (mini_window)->must_be_updated_p = true;
14104 pending |= update_frame (mini_frame, false, false);
14105 mini_frame->cursor_type_changed = false;
14106 if (!pending && hscroll_windows (mini_window))
14107 goto retry;
14108 }
14109 }
14110
14111 /* If display was paused because of pending input, make sure we do a
14112 thorough update the next time. */
14113 if (pending)
14114 {
14115 /* Prevent the optimization at the beginning of
14116 redisplay_internal that tries a single-line update of the
14117 line containing the cursor in the selected window. */
14118 CHARPOS (this_line_start_pos) = 0;
14119
14120 /* Let the overlay arrow be updated the next time. */
14121 update_overlay_arrows (0);
14122
14123 /* If we pause after scrolling, some rows in the current
14124 matrices of some windows are not valid. */
14125 if (!WINDOW_FULL_WIDTH_P (w)
14126 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14127 update_mode_lines = 36;
14128 }
14129 else
14130 {
14131 if (!consider_all_windows_p)
14132 {
14133 /* This has already been done above if
14134 consider_all_windows_p is set. */
14135 if (XBUFFER (w->contents)->text->redisplay
14136 && buffer_window_count (XBUFFER (w->contents)) > 1)
14137 /* This can happen if b->text->redisplay was set during
14138 jit-lock. */
14139 propagate_buffer_redisplay ();
14140 mark_window_display_accurate_1 (w, true);
14141
14142 /* Say overlay arrows are up to date. */
14143 update_overlay_arrows (1);
14144
14145 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14146 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14147 }
14148
14149 update_mode_lines = 0;
14150 windows_or_buffers_changed = 0;
14151 }
14152
14153 /* Start SIGIO interrupts coming again. Having them off during the
14154 code above makes it less likely one will discard output, but not
14155 impossible, since there might be stuff in the system buffer here.
14156 But it is much hairier to try to do anything about that. */
14157 if (interrupt_input)
14158 request_sigio ();
14159 RESUME_POLLING;
14160
14161 /* If a frame has become visible which was not before, redisplay
14162 again, so that we display it. Expose events for such a frame
14163 (which it gets when becoming visible) don't call the parts of
14164 redisplay constructing glyphs, so simply exposing a frame won't
14165 display anything in this case. So, we have to display these
14166 frames here explicitly. */
14167 if (!pending)
14168 {
14169 int new_count = 0;
14170
14171 FOR_EACH_FRAME (tail, frame)
14172 {
14173 if (XFRAME (frame)->visible)
14174 new_count++;
14175 }
14176
14177 if (new_count != number_of_visible_frames)
14178 windows_or_buffers_changed = 52;
14179 }
14180
14181 /* Change frame size now if a change is pending. */
14182 do_pending_window_change (true);
14183
14184 /* If we just did a pending size change, or have additional
14185 visible frames, or selected_window changed, redisplay again. */
14186 if ((windows_or_buffers_changed && !pending)
14187 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14188 goto retry;
14189
14190 /* Clear the face and image caches.
14191
14192 We used to do this only if consider_all_windows_p. But the cache
14193 needs to be cleared if a timer creates images in the current
14194 buffer (e.g. the test case in Bug#6230). */
14195
14196 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14197 {
14198 clear_face_cache (false);
14199 clear_face_cache_count = 0;
14200 }
14201
14202 #ifdef HAVE_WINDOW_SYSTEM
14203 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14204 {
14205 clear_image_caches (Qnil);
14206 clear_image_cache_count = 0;
14207 }
14208 #endif /* HAVE_WINDOW_SYSTEM */
14209
14210 end_of_redisplay:
14211 #ifdef HAVE_NS
14212 ns_set_doc_edited ();
14213 #endif
14214 if (interrupt_input && interrupts_deferred)
14215 request_sigio ();
14216
14217 unbind_to (count, Qnil);
14218 RESUME_POLLING;
14219 }
14220
14221
14222 /* Redisplay, but leave alone any recent echo area message unless
14223 another message has been requested in its place.
14224
14225 This is useful in situations where you need to redisplay but no
14226 user action has occurred, making it inappropriate for the message
14227 area to be cleared. See tracking_off and
14228 wait_reading_process_output for examples of these situations.
14229
14230 FROM_WHERE is an integer saying from where this function was
14231 called. This is useful for debugging. */
14232
14233 void
14234 redisplay_preserve_echo_area (int from_where)
14235 {
14236 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14237
14238 if (!NILP (echo_area_buffer[1]))
14239 {
14240 /* We have a previously displayed message, but no current
14241 message. Redisplay the previous message. */
14242 display_last_displayed_message_p = true;
14243 redisplay_internal ();
14244 display_last_displayed_message_p = false;
14245 }
14246 else
14247 redisplay_internal ();
14248
14249 flush_frame (SELECTED_FRAME ());
14250 }
14251
14252
14253 /* Function registered with record_unwind_protect in redisplay_internal. */
14254
14255 static void
14256 unwind_redisplay (void)
14257 {
14258 redisplaying_p = false;
14259 }
14260
14261
14262 /* Mark the display of leaf window W as accurate or inaccurate.
14263 If ACCURATE_P, mark display of W as accurate.
14264 If !ACCURATE_P, arrange for W to be redisplayed the next
14265 time redisplay_internal is called. */
14266
14267 static void
14268 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14269 {
14270 struct buffer *b = XBUFFER (w->contents);
14271
14272 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14273 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14274 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14275
14276 if (accurate_p)
14277 {
14278 b->clip_changed = false;
14279 b->prevent_redisplay_optimizations_p = false;
14280 eassert (buffer_window_count (b) > 0);
14281 /* Resetting b->text->redisplay is problematic!
14282 In order to make it safer to do it here, redisplay_internal must
14283 have copied all b->text->redisplay to their respective windows. */
14284 b->text->redisplay = false;
14285
14286 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14287 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14288 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14289 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14290
14291 w->current_matrix->buffer = b;
14292 w->current_matrix->begv = BUF_BEGV (b);
14293 w->current_matrix->zv = BUF_ZV (b);
14294
14295 w->last_cursor_vpos = w->cursor.vpos;
14296 w->last_cursor_off_p = w->cursor_off_p;
14297
14298 if (w == XWINDOW (selected_window))
14299 w->last_point = BUF_PT (b);
14300 else
14301 w->last_point = marker_position (w->pointm);
14302
14303 w->window_end_valid = true;
14304 w->update_mode_line = false;
14305 }
14306
14307 w->redisplay = !accurate_p;
14308 }
14309
14310
14311 /* Mark the display of windows in the window tree rooted at WINDOW as
14312 accurate or inaccurate. If ACCURATE_P, mark display of
14313 windows as accurate. If !ACCURATE_P, arrange for windows to
14314 be redisplayed the next time redisplay_internal is called. */
14315
14316 void
14317 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14318 {
14319 struct window *w;
14320
14321 for (; !NILP (window); window = w->next)
14322 {
14323 w = XWINDOW (window);
14324 if (WINDOWP (w->contents))
14325 mark_window_display_accurate (w->contents, accurate_p);
14326 else
14327 mark_window_display_accurate_1 (w, accurate_p);
14328 }
14329
14330 if (accurate_p)
14331 update_overlay_arrows (1);
14332 else
14333 /* Force a thorough redisplay the next time by setting
14334 last_arrow_position and last_arrow_string to t, which is
14335 unequal to any useful value of Voverlay_arrow_... */
14336 update_overlay_arrows (-1);
14337 }
14338
14339
14340 /* Return value in display table DP (Lisp_Char_Table *) for character
14341 C. Since a display table doesn't have any parent, we don't have to
14342 follow parent. Do not call this function directly but use the
14343 macro DISP_CHAR_VECTOR. */
14344
14345 Lisp_Object
14346 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14347 {
14348 Lisp_Object val;
14349
14350 if (ASCII_CHAR_P (c))
14351 {
14352 val = dp->ascii;
14353 if (SUB_CHAR_TABLE_P (val))
14354 val = XSUB_CHAR_TABLE (val)->contents[c];
14355 }
14356 else
14357 {
14358 Lisp_Object table;
14359
14360 XSETCHAR_TABLE (table, dp);
14361 val = char_table_ref (table, c);
14362 }
14363 if (NILP (val))
14364 val = dp->defalt;
14365 return val;
14366 }
14367
14368
14369 \f
14370 /***********************************************************************
14371 Window Redisplay
14372 ***********************************************************************/
14373
14374 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14375
14376 static void
14377 redisplay_windows (Lisp_Object window)
14378 {
14379 while (!NILP (window))
14380 {
14381 struct window *w = XWINDOW (window);
14382
14383 if (WINDOWP (w->contents))
14384 redisplay_windows (w->contents);
14385 else if (BUFFERP (w->contents))
14386 {
14387 displayed_buffer = XBUFFER (w->contents);
14388 /* Use list_of_error, not Qerror, so that
14389 we catch only errors and don't run the debugger. */
14390 internal_condition_case_1 (redisplay_window_0, window,
14391 list_of_error,
14392 redisplay_window_error);
14393 }
14394
14395 window = w->next;
14396 }
14397 }
14398
14399 static Lisp_Object
14400 redisplay_window_error (Lisp_Object ignore)
14401 {
14402 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14403 return Qnil;
14404 }
14405
14406 static Lisp_Object
14407 redisplay_window_0 (Lisp_Object window)
14408 {
14409 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14410 redisplay_window (window, false);
14411 return Qnil;
14412 }
14413
14414 static Lisp_Object
14415 redisplay_window_1 (Lisp_Object window)
14416 {
14417 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14418 redisplay_window (window, true);
14419 return Qnil;
14420 }
14421 \f
14422
14423 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14424 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14425 which positions recorded in ROW differ from current buffer
14426 positions.
14427
14428 Return true iff cursor is on this row. */
14429
14430 static bool
14431 set_cursor_from_row (struct window *w, struct glyph_row *row,
14432 struct glyph_matrix *matrix,
14433 ptrdiff_t delta, ptrdiff_t delta_bytes,
14434 int dy, int dvpos)
14435 {
14436 struct glyph *glyph = row->glyphs[TEXT_AREA];
14437 struct glyph *end = glyph + row->used[TEXT_AREA];
14438 struct glyph *cursor = NULL;
14439 /* The last known character position in row. */
14440 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14441 int x = row->x;
14442 ptrdiff_t pt_old = PT - delta;
14443 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14444 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14445 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14446 /* A glyph beyond the edge of TEXT_AREA which we should never
14447 touch. */
14448 struct glyph *glyphs_end = end;
14449 /* True means we've found a match for cursor position, but that
14450 glyph has the avoid_cursor_p flag set. */
14451 bool match_with_avoid_cursor = false;
14452 /* True means we've seen at least one glyph that came from a
14453 display string. */
14454 bool string_seen = false;
14455 /* Largest and smallest buffer positions seen so far during scan of
14456 glyph row. */
14457 ptrdiff_t bpos_max = pos_before;
14458 ptrdiff_t bpos_min = pos_after;
14459 /* Last buffer position covered by an overlay string with an integer
14460 `cursor' property. */
14461 ptrdiff_t bpos_covered = 0;
14462 /* True means the display string on which to display the cursor
14463 comes from a text property, not from an overlay. */
14464 bool string_from_text_prop = false;
14465
14466 /* Don't even try doing anything if called for a mode-line or
14467 header-line row, since the rest of the code isn't prepared to
14468 deal with such calamities. */
14469 eassert (!row->mode_line_p);
14470 if (row->mode_line_p)
14471 return false;
14472
14473 /* Skip over glyphs not having an object at the start and the end of
14474 the row. These are special glyphs like truncation marks on
14475 terminal frames. */
14476 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14477 {
14478 if (!row->reversed_p)
14479 {
14480 while (glyph < end
14481 && NILP (glyph->object)
14482 && glyph->charpos < 0)
14483 {
14484 x += glyph->pixel_width;
14485 ++glyph;
14486 }
14487 while (end > glyph
14488 && NILP ((end - 1)->object)
14489 /* CHARPOS is zero for blanks and stretch glyphs
14490 inserted by extend_face_to_end_of_line. */
14491 && (end - 1)->charpos <= 0)
14492 --end;
14493 glyph_before = glyph - 1;
14494 glyph_after = end;
14495 }
14496 else
14497 {
14498 struct glyph *g;
14499
14500 /* If the glyph row is reversed, we need to process it from back
14501 to front, so swap the edge pointers. */
14502 glyphs_end = end = glyph - 1;
14503 glyph += row->used[TEXT_AREA] - 1;
14504
14505 while (glyph > end + 1
14506 && NILP (glyph->object)
14507 && glyph->charpos < 0)
14508 {
14509 --glyph;
14510 x -= glyph->pixel_width;
14511 }
14512 if (NILP (glyph->object) && glyph->charpos < 0)
14513 --glyph;
14514 /* By default, in reversed rows we put the cursor on the
14515 rightmost (first in the reading order) glyph. */
14516 for (g = end + 1; g < glyph; g++)
14517 x += g->pixel_width;
14518 while (end < glyph
14519 && NILP ((end + 1)->object)
14520 && (end + 1)->charpos <= 0)
14521 ++end;
14522 glyph_before = glyph + 1;
14523 glyph_after = end;
14524 }
14525 }
14526 else if (row->reversed_p)
14527 {
14528 /* In R2L rows that don't display text, put the cursor on the
14529 rightmost glyph. Case in point: an empty last line that is
14530 part of an R2L paragraph. */
14531 cursor = end - 1;
14532 /* Avoid placing the cursor on the last glyph of the row, where
14533 on terminal frames we hold the vertical border between
14534 adjacent windows. */
14535 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14536 && !WINDOW_RIGHTMOST_P (w)
14537 && cursor == row->glyphs[LAST_AREA] - 1)
14538 cursor--;
14539 x = -1; /* will be computed below, at label compute_x */
14540 }
14541
14542 /* Step 1: Try to find the glyph whose character position
14543 corresponds to point. If that's not possible, find 2 glyphs
14544 whose character positions are the closest to point, one before
14545 point, the other after it. */
14546 if (!row->reversed_p)
14547 while (/* not marched to end of glyph row */
14548 glyph < end
14549 /* glyph was not inserted by redisplay for internal purposes */
14550 && !NILP (glyph->object))
14551 {
14552 if (BUFFERP (glyph->object))
14553 {
14554 ptrdiff_t dpos = glyph->charpos - pt_old;
14555
14556 if (glyph->charpos > bpos_max)
14557 bpos_max = glyph->charpos;
14558 if (glyph->charpos < bpos_min)
14559 bpos_min = glyph->charpos;
14560 if (!glyph->avoid_cursor_p)
14561 {
14562 /* If we hit point, we've found the glyph on which to
14563 display the cursor. */
14564 if (dpos == 0)
14565 {
14566 match_with_avoid_cursor = false;
14567 break;
14568 }
14569 /* See if we've found a better approximation to
14570 POS_BEFORE or to POS_AFTER. */
14571 if (0 > dpos && dpos > pos_before - pt_old)
14572 {
14573 pos_before = glyph->charpos;
14574 glyph_before = glyph;
14575 }
14576 else if (0 < dpos && dpos < pos_after - pt_old)
14577 {
14578 pos_after = glyph->charpos;
14579 glyph_after = glyph;
14580 }
14581 }
14582 else if (dpos == 0)
14583 match_with_avoid_cursor = true;
14584 }
14585 else if (STRINGP (glyph->object))
14586 {
14587 Lisp_Object chprop;
14588 ptrdiff_t glyph_pos = glyph->charpos;
14589
14590 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14591 glyph->object);
14592 if (!NILP (chprop))
14593 {
14594 /* If the string came from a `display' text property,
14595 look up the buffer position of that property and
14596 use that position to update bpos_max, as if we
14597 actually saw such a position in one of the row's
14598 glyphs. This helps with supporting integer values
14599 of `cursor' property on the display string in
14600 situations where most or all of the row's buffer
14601 text is completely covered by display properties,
14602 so that no glyph with valid buffer positions is
14603 ever seen in the row. */
14604 ptrdiff_t prop_pos =
14605 string_buffer_position_lim (glyph->object, pos_before,
14606 pos_after, false);
14607
14608 if (prop_pos >= pos_before)
14609 bpos_max = prop_pos;
14610 }
14611 if (INTEGERP (chprop))
14612 {
14613 bpos_covered = bpos_max + XINT (chprop);
14614 /* If the `cursor' property covers buffer positions up
14615 to and including point, we should display cursor on
14616 this glyph. Note that, if a `cursor' property on one
14617 of the string's characters has an integer value, we
14618 will break out of the loop below _before_ we get to
14619 the position match above. IOW, integer values of
14620 the `cursor' property override the "exact match for
14621 point" strategy of positioning the cursor. */
14622 /* Implementation note: bpos_max == pt_old when, e.g.,
14623 we are in an empty line, where bpos_max is set to
14624 MATRIX_ROW_START_CHARPOS, see above. */
14625 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14626 {
14627 cursor = glyph;
14628 break;
14629 }
14630 }
14631
14632 string_seen = true;
14633 }
14634 x += glyph->pixel_width;
14635 ++glyph;
14636 }
14637 else if (glyph > end) /* row is reversed */
14638 while (!NILP (glyph->object))
14639 {
14640 if (BUFFERP (glyph->object))
14641 {
14642 ptrdiff_t dpos = glyph->charpos - pt_old;
14643
14644 if (glyph->charpos > bpos_max)
14645 bpos_max = glyph->charpos;
14646 if (glyph->charpos < bpos_min)
14647 bpos_min = glyph->charpos;
14648 if (!glyph->avoid_cursor_p)
14649 {
14650 if (dpos == 0)
14651 {
14652 match_with_avoid_cursor = false;
14653 break;
14654 }
14655 if (0 > dpos && dpos > pos_before - pt_old)
14656 {
14657 pos_before = glyph->charpos;
14658 glyph_before = glyph;
14659 }
14660 else if (0 < dpos && dpos < pos_after - pt_old)
14661 {
14662 pos_after = glyph->charpos;
14663 glyph_after = glyph;
14664 }
14665 }
14666 else if (dpos == 0)
14667 match_with_avoid_cursor = true;
14668 }
14669 else if (STRINGP (glyph->object))
14670 {
14671 Lisp_Object chprop;
14672 ptrdiff_t glyph_pos = glyph->charpos;
14673
14674 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14675 glyph->object);
14676 if (!NILP (chprop))
14677 {
14678 ptrdiff_t prop_pos =
14679 string_buffer_position_lim (glyph->object, pos_before,
14680 pos_after, false);
14681
14682 if (prop_pos >= pos_before)
14683 bpos_max = prop_pos;
14684 }
14685 if (INTEGERP (chprop))
14686 {
14687 bpos_covered = bpos_max + XINT (chprop);
14688 /* If the `cursor' property covers buffer positions up
14689 to and including point, we should display cursor on
14690 this glyph. */
14691 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14692 {
14693 cursor = glyph;
14694 break;
14695 }
14696 }
14697 string_seen = true;
14698 }
14699 --glyph;
14700 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14701 {
14702 x--; /* can't use any pixel_width */
14703 break;
14704 }
14705 x -= glyph->pixel_width;
14706 }
14707
14708 /* Step 2: If we didn't find an exact match for point, we need to
14709 look for a proper place to put the cursor among glyphs between
14710 GLYPH_BEFORE and GLYPH_AFTER. */
14711 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14712 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14713 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14714 {
14715 /* An empty line has a single glyph whose OBJECT is nil and
14716 whose CHARPOS is the position of a newline on that line.
14717 Note that on a TTY, there are more glyphs after that, which
14718 were produced by extend_face_to_end_of_line, but their
14719 CHARPOS is zero or negative. */
14720 bool empty_line_p =
14721 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14722 && NILP (glyph->object) && glyph->charpos > 0
14723 /* On a TTY, continued and truncated rows also have a glyph at
14724 their end whose OBJECT is nil and whose CHARPOS is
14725 positive (the continuation and truncation glyphs), but such
14726 rows are obviously not "empty". */
14727 && !(row->continued_p || row->truncated_on_right_p));
14728
14729 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14730 {
14731 ptrdiff_t ellipsis_pos;
14732
14733 /* Scan back over the ellipsis glyphs. */
14734 if (!row->reversed_p)
14735 {
14736 ellipsis_pos = (glyph - 1)->charpos;
14737 while (glyph > row->glyphs[TEXT_AREA]
14738 && (glyph - 1)->charpos == ellipsis_pos)
14739 glyph--, x -= glyph->pixel_width;
14740 /* That loop always goes one position too far, including
14741 the glyph before the ellipsis. So scan forward over
14742 that one. */
14743 x += glyph->pixel_width;
14744 glyph++;
14745 }
14746 else /* row is reversed */
14747 {
14748 ellipsis_pos = (glyph + 1)->charpos;
14749 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14750 && (glyph + 1)->charpos == ellipsis_pos)
14751 glyph++, x += glyph->pixel_width;
14752 x -= glyph->pixel_width;
14753 glyph--;
14754 }
14755 }
14756 else if (match_with_avoid_cursor)
14757 {
14758 cursor = glyph_after;
14759 x = -1;
14760 }
14761 else if (string_seen)
14762 {
14763 int incr = row->reversed_p ? -1 : +1;
14764
14765 /* Need to find the glyph that came out of a string which is
14766 present at point. That glyph is somewhere between
14767 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14768 positioned between POS_BEFORE and POS_AFTER in the
14769 buffer. */
14770 struct glyph *start, *stop;
14771 ptrdiff_t pos = pos_before;
14772
14773 x = -1;
14774
14775 /* If the row ends in a newline from a display string,
14776 reordering could have moved the glyphs belonging to the
14777 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14778 in this case we extend the search to the last glyph in
14779 the row that was not inserted by redisplay. */
14780 if (row->ends_in_newline_from_string_p)
14781 {
14782 glyph_after = end;
14783 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14784 }
14785
14786 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14787 correspond to POS_BEFORE and POS_AFTER, respectively. We
14788 need START and STOP in the order that corresponds to the
14789 row's direction as given by its reversed_p flag. If the
14790 directionality of characters between POS_BEFORE and
14791 POS_AFTER is the opposite of the row's base direction,
14792 these characters will have been reordered for display,
14793 and we need to reverse START and STOP. */
14794 if (!row->reversed_p)
14795 {
14796 start = min (glyph_before, glyph_after);
14797 stop = max (glyph_before, glyph_after);
14798 }
14799 else
14800 {
14801 start = max (glyph_before, glyph_after);
14802 stop = min (glyph_before, glyph_after);
14803 }
14804 for (glyph = start + incr;
14805 row->reversed_p ? glyph > stop : glyph < stop; )
14806 {
14807
14808 /* Any glyphs that come from the buffer are here because
14809 of bidi reordering. Skip them, and only pay
14810 attention to glyphs that came from some string. */
14811 if (STRINGP (glyph->object))
14812 {
14813 Lisp_Object str;
14814 ptrdiff_t tem;
14815 /* If the display property covers the newline, we
14816 need to search for it one position farther. */
14817 ptrdiff_t lim = pos_after
14818 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14819
14820 string_from_text_prop = false;
14821 str = glyph->object;
14822 tem = string_buffer_position_lim (str, pos, lim, false);
14823 if (tem == 0 /* from overlay */
14824 || pos <= tem)
14825 {
14826 /* If the string from which this glyph came is
14827 found in the buffer at point, or at position
14828 that is closer to point than pos_after, then
14829 we've found the glyph we've been looking for.
14830 If it comes from an overlay (tem == 0), and
14831 it has the `cursor' property on one of its
14832 glyphs, record that glyph as a candidate for
14833 displaying the cursor. (As in the
14834 unidirectional version, we will display the
14835 cursor on the last candidate we find.) */
14836 if (tem == 0
14837 || tem == pt_old
14838 || (tem - pt_old > 0 && tem < pos_after))
14839 {
14840 /* The glyphs from this string could have
14841 been reordered. Find the one with the
14842 smallest string position. Or there could
14843 be a character in the string with the
14844 `cursor' property, which means display
14845 cursor on that character's glyph. */
14846 ptrdiff_t strpos = glyph->charpos;
14847
14848 if (tem)
14849 {
14850 cursor = glyph;
14851 string_from_text_prop = true;
14852 }
14853 for ( ;
14854 (row->reversed_p ? glyph > stop : glyph < stop)
14855 && EQ (glyph->object, str);
14856 glyph += incr)
14857 {
14858 Lisp_Object cprop;
14859 ptrdiff_t gpos = glyph->charpos;
14860
14861 cprop = Fget_char_property (make_number (gpos),
14862 Qcursor,
14863 glyph->object);
14864 if (!NILP (cprop))
14865 {
14866 cursor = glyph;
14867 break;
14868 }
14869 if (tem && glyph->charpos < strpos)
14870 {
14871 strpos = glyph->charpos;
14872 cursor = glyph;
14873 }
14874 }
14875
14876 if (tem == pt_old
14877 || (tem - pt_old > 0 && tem < pos_after))
14878 goto compute_x;
14879 }
14880 if (tem)
14881 pos = tem + 1; /* don't find previous instances */
14882 }
14883 /* This string is not what we want; skip all of the
14884 glyphs that came from it. */
14885 while ((row->reversed_p ? glyph > stop : glyph < stop)
14886 && EQ (glyph->object, str))
14887 glyph += incr;
14888 }
14889 else
14890 glyph += incr;
14891 }
14892
14893 /* If we reached the end of the line, and END was from a string,
14894 the cursor is not on this line. */
14895 if (cursor == NULL
14896 && (row->reversed_p ? glyph <= end : glyph >= end)
14897 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14898 && STRINGP (end->object)
14899 && row->continued_p)
14900 return false;
14901 }
14902 /* A truncated row may not include PT among its character positions.
14903 Setting the cursor inside the scroll margin will trigger
14904 recalculation of hscroll in hscroll_window_tree. But if a
14905 display string covers point, defer to the string-handling
14906 code below to figure this out. */
14907 else if (row->truncated_on_left_p && pt_old < bpos_min)
14908 {
14909 cursor = glyph_before;
14910 x = -1;
14911 }
14912 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14913 /* Zero-width characters produce no glyphs. */
14914 || (!empty_line_p
14915 && (row->reversed_p
14916 ? glyph_after > glyphs_end
14917 : glyph_after < glyphs_end)))
14918 {
14919 cursor = glyph_after;
14920 x = -1;
14921 }
14922 }
14923
14924 compute_x:
14925 if (cursor != NULL)
14926 glyph = cursor;
14927 else if (glyph == glyphs_end
14928 && pos_before == pos_after
14929 && STRINGP ((row->reversed_p
14930 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14931 : row->glyphs[TEXT_AREA])->object))
14932 {
14933 /* If all the glyphs of this row came from strings, put the
14934 cursor on the first glyph of the row. This avoids having the
14935 cursor outside of the text area in this very rare and hard
14936 use case. */
14937 glyph =
14938 row->reversed_p
14939 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14940 : row->glyphs[TEXT_AREA];
14941 }
14942 if (x < 0)
14943 {
14944 struct glyph *g;
14945
14946 /* Need to compute x that corresponds to GLYPH. */
14947 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14948 {
14949 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14950 emacs_abort ();
14951 x += g->pixel_width;
14952 }
14953 }
14954
14955 /* ROW could be part of a continued line, which, under bidi
14956 reordering, might have other rows whose start and end charpos
14957 occlude point. Only set w->cursor if we found a better
14958 approximation to the cursor position than we have from previously
14959 examined candidate rows belonging to the same continued line. */
14960 if (/* We already have a candidate row. */
14961 w->cursor.vpos >= 0
14962 /* That candidate is not the row we are processing. */
14963 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14964 /* Make sure cursor.vpos specifies a row whose start and end
14965 charpos occlude point, and it is valid candidate for being a
14966 cursor-row. This is because some callers of this function
14967 leave cursor.vpos at the row where the cursor was displayed
14968 during the last redisplay cycle. */
14969 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14970 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14971 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14972 {
14973 struct glyph *g1
14974 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14975
14976 /* Don't consider glyphs that are outside TEXT_AREA. */
14977 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14978 return false;
14979 /* Keep the candidate whose buffer position is the closest to
14980 point or has the `cursor' property. */
14981 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14982 w->cursor.hpos >= 0
14983 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14984 && ((BUFFERP (g1->object)
14985 && (g1->charpos == pt_old /* An exact match always wins. */
14986 || (BUFFERP (glyph->object)
14987 && eabs (g1->charpos - pt_old)
14988 < eabs (glyph->charpos - pt_old))))
14989 /* Previous candidate is a glyph from a string that has
14990 a non-nil `cursor' property. */
14991 || (STRINGP (g1->object)
14992 && (!NILP (Fget_char_property (make_number (g1->charpos),
14993 Qcursor, g1->object))
14994 /* Previous candidate is from the same display
14995 string as this one, and the display string
14996 came from a text property. */
14997 || (EQ (g1->object, glyph->object)
14998 && string_from_text_prop)
14999 /* this candidate is from newline and its
15000 position is not an exact match */
15001 || (NILP (glyph->object)
15002 && glyph->charpos != pt_old)))))
15003 return false;
15004 /* If this candidate gives an exact match, use that. */
15005 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15006 /* If this candidate is a glyph created for the
15007 terminating newline of a line, and point is on that
15008 newline, it wins because it's an exact match. */
15009 || (!row->continued_p
15010 && NILP (glyph->object)
15011 && glyph->charpos == 0
15012 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15013 /* Otherwise, keep the candidate that comes from a row
15014 spanning less buffer positions. This may win when one or
15015 both candidate positions are on glyphs that came from
15016 display strings, for which we cannot compare buffer
15017 positions. */
15018 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15019 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15020 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15021 return false;
15022 }
15023 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15024 w->cursor.x = x;
15025 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15026 w->cursor.y = row->y + dy;
15027
15028 if (w == XWINDOW (selected_window))
15029 {
15030 if (!row->continued_p
15031 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15032 && row->x == 0)
15033 {
15034 this_line_buffer = XBUFFER (w->contents);
15035
15036 CHARPOS (this_line_start_pos)
15037 = MATRIX_ROW_START_CHARPOS (row) + delta;
15038 BYTEPOS (this_line_start_pos)
15039 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15040
15041 CHARPOS (this_line_end_pos)
15042 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15043 BYTEPOS (this_line_end_pos)
15044 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15045
15046 this_line_y = w->cursor.y;
15047 this_line_pixel_height = row->height;
15048 this_line_vpos = w->cursor.vpos;
15049 this_line_start_x = row->x;
15050 }
15051 else
15052 CHARPOS (this_line_start_pos) = 0;
15053 }
15054
15055 return true;
15056 }
15057
15058
15059 /* Run window scroll functions, if any, for WINDOW with new window
15060 start STARTP. Sets the window start of WINDOW to that position.
15061
15062 We assume that the window's buffer is really current. */
15063
15064 static struct text_pos
15065 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15066 {
15067 struct window *w = XWINDOW (window);
15068 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15069
15070 eassert (current_buffer == XBUFFER (w->contents));
15071
15072 if (!NILP (Vwindow_scroll_functions))
15073 {
15074 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15075 make_number (CHARPOS (startp)));
15076 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15077 /* In case the hook functions switch buffers. */
15078 set_buffer_internal (XBUFFER (w->contents));
15079 }
15080
15081 return startp;
15082 }
15083
15084
15085 /* Make sure the line containing the cursor is fully visible.
15086 A value of true means there is nothing to be done.
15087 (Either the line is fully visible, or it cannot be made so,
15088 or we cannot tell.)
15089
15090 If FORCE_P, return false even if partial visible cursor row
15091 is higher than window.
15092
15093 If CURRENT_MATRIX_P, use the information from the
15094 window's current glyph matrix; otherwise use the desired glyph
15095 matrix.
15096
15097 A value of false means the caller should do scrolling
15098 as if point had gone off the screen. */
15099
15100 static bool
15101 cursor_row_fully_visible_p (struct window *w, bool force_p,
15102 bool current_matrix_p)
15103 {
15104 struct glyph_matrix *matrix;
15105 struct glyph_row *row;
15106 int window_height;
15107
15108 if (!make_cursor_line_fully_visible_p)
15109 return true;
15110
15111 /* It's not always possible to find the cursor, e.g, when a window
15112 is full of overlay strings. Don't do anything in that case. */
15113 if (w->cursor.vpos < 0)
15114 return true;
15115
15116 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15117 row = MATRIX_ROW (matrix, w->cursor.vpos);
15118
15119 /* If the cursor row is not partially visible, there's nothing to do. */
15120 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15121 return true;
15122
15123 /* If the row the cursor is in is taller than the window's height,
15124 it's not clear what to do, so do nothing. */
15125 window_height = window_box_height (w);
15126 if (row->height >= window_height)
15127 {
15128 if (!force_p || MINI_WINDOW_P (w)
15129 || w->vscroll || w->cursor.vpos == 0)
15130 return true;
15131 }
15132 return false;
15133 }
15134
15135
15136 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15137 means only WINDOW is redisplayed in redisplay_internal.
15138 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15139 in redisplay_window to bring a partially visible line into view in
15140 the case that only the cursor has moved.
15141
15142 LAST_LINE_MISFIT should be true if we're scrolling because the
15143 last screen line's vertical height extends past the end of the screen.
15144
15145 Value is
15146
15147 1 if scrolling succeeded
15148
15149 0 if scrolling didn't find point.
15150
15151 -1 if new fonts have been loaded so that we must interrupt
15152 redisplay, adjust glyph matrices, and try again. */
15153
15154 enum
15155 {
15156 SCROLLING_SUCCESS,
15157 SCROLLING_FAILED,
15158 SCROLLING_NEED_LARGER_MATRICES
15159 };
15160
15161 /* If scroll-conservatively is more than this, never recenter.
15162
15163 If you change this, don't forget to update the doc string of
15164 `scroll-conservatively' and the Emacs manual. */
15165 #define SCROLL_LIMIT 100
15166
15167 static int
15168 try_scrolling (Lisp_Object window, bool just_this_one_p,
15169 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15170 bool temp_scroll_step, bool last_line_misfit)
15171 {
15172 struct window *w = XWINDOW (window);
15173 struct frame *f = XFRAME (w->frame);
15174 struct text_pos pos, startp;
15175 struct it it;
15176 int this_scroll_margin, scroll_max, rc, height;
15177 int dy = 0, amount_to_scroll = 0;
15178 bool scroll_down_p = false;
15179 int extra_scroll_margin_lines = last_line_misfit;
15180 Lisp_Object aggressive;
15181 /* We will never try scrolling more than this number of lines. */
15182 int scroll_limit = SCROLL_LIMIT;
15183 int frame_line_height = default_line_pixel_height (w);
15184 int window_total_lines
15185 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15186
15187 #ifdef GLYPH_DEBUG
15188 debug_method_add (w, "try_scrolling");
15189 #endif
15190
15191 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15192
15193 /* Compute scroll margin height in pixels. We scroll when point is
15194 within this distance from the top or bottom of the window. */
15195 if (scroll_margin > 0)
15196 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15197 * frame_line_height;
15198 else
15199 this_scroll_margin = 0;
15200
15201 /* Force arg_scroll_conservatively to have a reasonable value, to
15202 avoid scrolling too far away with slow move_it_* functions. Note
15203 that the user can supply scroll-conservatively equal to
15204 `most-positive-fixnum', which can be larger than INT_MAX. */
15205 if (arg_scroll_conservatively > scroll_limit)
15206 {
15207 arg_scroll_conservatively = scroll_limit + 1;
15208 scroll_max = scroll_limit * frame_line_height;
15209 }
15210 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15211 /* Compute how much we should try to scroll maximally to bring
15212 point into view. */
15213 scroll_max = (max (scroll_step,
15214 max (arg_scroll_conservatively, temp_scroll_step))
15215 * frame_line_height);
15216 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15217 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15218 /* We're trying to scroll because of aggressive scrolling but no
15219 scroll_step is set. Choose an arbitrary one. */
15220 scroll_max = 10 * frame_line_height;
15221 else
15222 scroll_max = 0;
15223
15224 too_near_end:
15225
15226 /* Decide whether to scroll down. */
15227 if (PT > CHARPOS (startp))
15228 {
15229 int scroll_margin_y;
15230
15231 /* Compute the pixel ypos of the scroll margin, then move IT to
15232 either that ypos or PT, whichever comes first. */
15233 start_display (&it, w, startp);
15234 scroll_margin_y = it.last_visible_y - this_scroll_margin
15235 - frame_line_height * extra_scroll_margin_lines;
15236 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15237 (MOVE_TO_POS | MOVE_TO_Y));
15238
15239 if (PT > CHARPOS (it.current.pos))
15240 {
15241 int y0 = line_bottom_y (&it);
15242 /* Compute how many pixels below window bottom to stop searching
15243 for PT. This avoids costly search for PT that is far away if
15244 the user limited scrolling by a small number of lines, but
15245 always finds PT if scroll_conservatively is set to a large
15246 number, such as most-positive-fixnum. */
15247 int slack = max (scroll_max, 10 * frame_line_height);
15248 int y_to_move = it.last_visible_y + slack;
15249
15250 /* Compute the distance from the scroll margin to PT or to
15251 the scroll limit, whichever comes first. This should
15252 include the height of the cursor line, to make that line
15253 fully visible. */
15254 move_it_to (&it, PT, -1, y_to_move,
15255 -1, MOVE_TO_POS | MOVE_TO_Y);
15256 dy = line_bottom_y (&it) - y0;
15257
15258 if (dy > scroll_max)
15259 return SCROLLING_FAILED;
15260
15261 if (dy > 0)
15262 scroll_down_p = true;
15263 }
15264 }
15265
15266 if (scroll_down_p)
15267 {
15268 /* Point is in or below the bottom scroll margin, so move the
15269 window start down. If scrolling conservatively, move it just
15270 enough down to make point visible. If scroll_step is set,
15271 move it down by scroll_step. */
15272 if (arg_scroll_conservatively)
15273 amount_to_scroll
15274 = min (max (dy, frame_line_height),
15275 frame_line_height * arg_scroll_conservatively);
15276 else if (scroll_step || temp_scroll_step)
15277 amount_to_scroll = scroll_max;
15278 else
15279 {
15280 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15281 height = WINDOW_BOX_TEXT_HEIGHT (w);
15282 if (NUMBERP (aggressive))
15283 {
15284 double float_amount = XFLOATINT (aggressive) * height;
15285 int aggressive_scroll = float_amount;
15286 if (aggressive_scroll == 0 && float_amount > 0)
15287 aggressive_scroll = 1;
15288 /* Don't let point enter the scroll margin near top of
15289 the window. This could happen if the value of
15290 scroll_up_aggressively is too large and there are
15291 non-zero margins, because scroll_up_aggressively
15292 means put point that fraction of window height
15293 _from_the_bottom_margin_. */
15294 if (aggressive_scroll + 2 * this_scroll_margin > height)
15295 aggressive_scroll = height - 2 * this_scroll_margin;
15296 amount_to_scroll = dy + aggressive_scroll;
15297 }
15298 }
15299
15300 if (amount_to_scroll <= 0)
15301 return SCROLLING_FAILED;
15302
15303 start_display (&it, w, startp);
15304 if (arg_scroll_conservatively <= scroll_limit)
15305 move_it_vertically (&it, amount_to_scroll);
15306 else
15307 {
15308 /* Extra precision for users who set scroll-conservatively
15309 to a large number: make sure the amount we scroll
15310 the window start is never less than amount_to_scroll,
15311 which was computed as distance from window bottom to
15312 point. This matters when lines at window top and lines
15313 below window bottom have different height. */
15314 struct it it1;
15315 void *it1data = NULL;
15316 /* We use a temporary it1 because line_bottom_y can modify
15317 its argument, if it moves one line down; see there. */
15318 int start_y;
15319
15320 SAVE_IT (it1, it, it1data);
15321 start_y = line_bottom_y (&it1);
15322 do {
15323 RESTORE_IT (&it, &it, it1data);
15324 move_it_by_lines (&it, 1);
15325 SAVE_IT (it1, it, it1data);
15326 } while (IT_CHARPOS (it) < ZV
15327 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15328 bidi_unshelve_cache (it1data, true);
15329 }
15330
15331 /* If STARTP is unchanged, move it down another screen line. */
15332 if (IT_CHARPOS (it) == CHARPOS (startp))
15333 move_it_by_lines (&it, 1);
15334 startp = it.current.pos;
15335 }
15336 else
15337 {
15338 struct text_pos scroll_margin_pos = startp;
15339 int y_offset = 0;
15340
15341 /* See if point is inside the scroll margin at the top of the
15342 window. */
15343 if (this_scroll_margin)
15344 {
15345 int y_start;
15346
15347 start_display (&it, w, startp);
15348 y_start = it.current_y;
15349 move_it_vertically (&it, this_scroll_margin);
15350 scroll_margin_pos = it.current.pos;
15351 /* If we didn't move enough before hitting ZV, request
15352 additional amount of scroll, to move point out of the
15353 scroll margin. */
15354 if (IT_CHARPOS (it) == ZV
15355 && it.current_y - y_start < this_scroll_margin)
15356 y_offset = this_scroll_margin - (it.current_y - y_start);
15357 }
15358
15359 if (PT < CHARPOS (scroll_margin_pos))
15360 {
15361 /* Point is in the scroll margin at the top of the window or
15362 above what is displayed in the window. */
15363 int y0, y_to_move;
15364
15365 /* Compute the vertical distance from PT to the scroll
15366 margin position. Move as far as scroll_max allows, or
15367 one screenful, or 10 screen lines, whichever is largest.
15368 Give up if distance is greater than scroll_max or if we
15369 didn't reach the scroll margin position. */
15370 SET_TEXT_POS (pos, PT, PT_BYTE);
15371 start_display (&it, w, pos);
15372 y0 = it.current_y;
15373 y_to_move = max (it.last_visible_y,
15374 max (scroll_max, 10 * frame_line_height));
15375 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15376 y_to_move, -1,
15377 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15378 dy = it.current_y - y0;
15379 if (dy > scroll_max
15380 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15381 return SCROLLING_FAILED;
15382
15383 /* Additional scroll for when ZV was too close to point. */
15384 dy += y_offset;
15385
15386 /* Compute new window start. */
15387 start_display (&it, w, startp);
15388
15389 if (arg_scroll_conservatively)
15390 amount_to_scroll = max (dy, frame_line_height
15391 * max (scroll_step, temp_scroll_step));
15392 else if (scroll_step || temp_scroll_step)
15393 amount_to_scroll = scroll_max;
15394 else
15395 {
15396 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15397 height = WINDOW_BOX_TEXT_HEIGHT (w);
15398 if (NUMBERP (aggressive))
15399 {
15400 double float_amount = XFLOATINT (aggressive) * height;
15401 int aggressive_scroll = float_amount;
15402 if (aggressive_scroll == 0 && float_amount > 0)
15403 aggressive_scroll = 1;
15404 /* Don't let point enter the scroll margin near
15405 bottom of the window, if the value of
15406 scroll_down_aggressively happens to be too
15407 large. */
15408 if (aggressive_scroll + 2 * this_scroll_margin > height)
15409 aggressive_scroll = height - 2 * this_scroll_margin;
15410 amount_to_scroll = dy + aggressive_scroll;
15411 }
15412 }
15413
15414 if (amount_to_scroll <= 0)
15415 return SCROLLING_FAILED;
15416
15417 move_it_vertically_backward (&it, amount_to_scroll);
15418 startp = it.current.pos;
15419 }
15420 }
15421
15422 /* Run window scroll functions. */
15423 startp = run_window_scroll_functions (window, startp);
15424
15425 /* Display the window. Give up if new fonts are loaded, or if point
15426 doesn't appear. */
15427 if (!try_window (window, startp, 0))
15428 rc = SCROLLING_NEED_LARGER_MATRICES;
15429 else if (w->cursor.vpos < 0)
15430 {
15431 clear_glyph_matrix (w->desired_matrix);
15432 rc = SCROLLING_FAILED;
15433 }
15434 else
15435 {
15436 /* Maybe forget recorded base line for line number display. */
15437 if (!just_this_one_p
15438 || current_buffer->clip_changed
15439 || BEG_UNCHANGED < CHARPOS (startp))
15440 w->base_line_number = 0;
15441
15442 /* If cursor ends up on a partially visible line,
15443 treat that as being off the bottom of the screen. */
15444 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15445 false)
15446 /* It's possible that the cursor is on the first line of the
15447 buffer, which is partially obscured due to a vscroll
15448 (Bug#7537). In that case, avoid looping forever. */
15449 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15450 {
15451 clear_glyph_matrix (w->desired_matrix);
15452 ++extra_scroll_margin_lines;
15453 goto too_near_end;
15454 }
15455 rc = SCROLLING_SUCCESS;
15456 }
15457
15458 return rc;
15459 }
15460
15461
15462 /* Compute a suitable window start for window W if display of W starts
15463 on a continuation line. Value is true if a new window start
15464 was computed.
15465
15466 The new window start will be computed, based on W's width, starting
15467 from the start of the continued line. It is the start of the
15468 screen line with the minimum distance from the old start W->start. */
15469
15470 static bool
15471 compute_window_start_on_continuation_line (struct window *w)
15472 {
15473 struct text_pos pos, start_pos;
15474 bool window_start_changed_p = false;
15475
15476 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15477
15478 /* If window start is on a continuation line... Window start may be
15479 < BEGV in case there's invisible text at the start of the
15480 buffer (M-x rmail, for example). */
15481 if (CHARPOS (start_pos) > BEGV
15482 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15483 {
15484 struct it it;
15485 struct glyph_row *row;
15486
15487 /* Handle the case that the window start is out of range. */
15488 if (CHARPOS (start_pos) < BEGV)
15489 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15490 else if (CHARPOS (start_pos) > ZV)
15491 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15492
15493 /* Find the start of the continued line. This should be fast
15494 because find_newline is fast (newline cache). */
15495 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15496 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15497 row, DEFAULT_FACE_ID);
15498 reseat_at_previous_visible_line_start (&it);
15499
15500 /* If the line start is "too far" away from the window start,
15501 say it takes too much time to compute a new window start. */
15502 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15503 /* PXW: Do we need upper bounds here? */
15504 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15505 {
15506 int min_distance, distance;
15507
15508 /* Move forward by display lines to find the new window
15509 start. If window width was enlarged, the new start can
15510 be expected to be > the old start. If window width was
15511 decreased, the new window start will be < the old start.
15512 So, we're looking for the display line start with the
15513 minimum distance from the old window start. */
15514 pos = it.current.pos;
15515 min_distance = INFINITY;
15516 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15517 distance < min_distance)
15518 {
15519 min_distance = distance;
15520 pos = it.current.pos;
15521 if (it.line_wrap == WORD_WRAP)
15522 {
15523 /* Under WORD_WRAP, move_it_by_lines is likely to
15524 overshoot and stop not at the first, but the
15525 second character from the left margin. So in
15526 that case, we need a more tight control on the X
15527 coordinate of the iterator than move_it_by_lines
15528 promises in its contract. The method is to first
15529 go to the last (rightmost) visible character of a
15530 line, then move to the leftmost character on the
15531 next line in a separate call. */
15532 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15533 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15534 move_it_to (&it, ZV, 0,
15535 it.current_y + it.max_ascent + it.max_descent, -1,
15536 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15537 }
15538 else
15539 move_it_by_lines (&it, 1);
15540 }
15541
15542 /* Set the window start there. */
15543 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15544 window_start_changed_p = true;
15545 }
15546 }
15547
15548 return window_start_changed_p;
15549 }
15550
15551
15552 /* Try cursor movement in case text has not changed in window WINDOW,
15553 with window start STARTP. Value is
15554
15555 CURSOR_MOVEMENT_SUCCESS if successful
15556
15557 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15558
15559 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15560 display. *SCROLL_STEP is set to true, under certain circumstances, if
15561 we want to scroll as if scroll-step were set to 1. See the code.
15562
15563 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15564 which case we have to abort this redisplay, and adjust matrices
15565 first. */
15566
15567 enum
15568 {
15569 CURSOR_MOVEMENT_SUCCESS,
15570 CURSOR_MOVEMENT_CANNOT_BE_USED,
15571 CURSOR_MOVEMENT_MUST_SCROLL,
15572 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15573 };
15574
15575 static int
15576 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15577 bool *scroll_step)
15578 {
15579 struct window *w = XWINDOW (window);
15580 struct frame *f = XFRAME (w->frame);
15581 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15582
15583 #ifdef GLYPH_DEBUG
15584 if (inhibit_try_cursor_movement)
15585 return rc;
15586 #endif
15587
15588 /* Previously, there was a check for Lisp integer in the
15589 if-statement below. Now, this field is converted to
15590 ptrdiff_t, thus zero means invalid position in a buffer. */
15591 eassert (w->last_point > 0);
15592 /* Likewise there was a check whether window_end_vpos is nil or larger
15593 than the window. Now window_end_vpos is int and so never nil, but
15594 let's leave eassert to check whether it fits in the window. */
15595 eassert (!w->window_end_valid
15596 || w->window_end_vpos < w->current_matrix->nrows);
15597
15598 /* Handle case where text has not changed, only point, and it has
15599 not moved off the frame. */
15600 if (/* Point may be in this window. */
15601 PT >= CHARPOS (startp)
15602 /* Selective display hasn't changed. */
15603 && !current_buffer->clip_changed
15604 /* Function force-mode-line-update is used to force a thorough
15605 redisplay. It sets either windows_or_buffers_changed or
15606 update_mode_lines. So don't take a shortcut here for these
15607 cases. */
15608 && !update_mode_lines
15609 && !windows_or_buffers_changed
15610 && !f->cursor_type_changed
15611 && NILP (Vshow_trailing_whitespace)
15612 /* This code is not used for mini-buffer for the sake of the case
15613 of redisplaying to replace an echo area message; since in
15614 that case the mini-buffer contents per se are usually
15615 unchanged. This code is of no real use in the mini-buffer
15616 since the handling of this_line_start_pos, etc., in redisplay
15617 handles the same cases. */
15618 && !EQ (window, minibuf_window)
15619 && (FRAME_WINDOW_P (f)
15620 || !overlay_arrow_in_current_buffer_p ()))
15621 {
15622 int this_scroll_margin, top_scroll_margin;
15623 struct glyph_row *row = NULL;
15624 int frame_line_height = default_line_pixel_height (w);
15625 int window_total_lines
15626 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15627
15628 #ifdef GLYPH_DEBUG
15629 debug_method_add (w, "cursor movement");
15630 #endif
15631
15632 /* Scroll if point within this distance from the top or bottom
15633 of the window. This is a pixel value. */
15634 if (scroll_margin > 0)
15635 {
15636 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15637 this_scroll_margin *= frame_line_height;
15638 }
15639 else
15640 this_scroll_margin = 0;
15641
15642 top_scroll_margin = this_scroll_margin;
15643 if (WINDOW_WANTS_HEADER_LINE_P (w))
15644 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15645
15646 /* Start with the row the cursor was displayed during the last
15647 not paused redisplay. Give up if that row is not valid. */
15648 if (w->last_cursor_vpos < 0
15649 || w->last_cursor_vpos >= w->current_matrix->nrows)
15650 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15651 else
15652 {
15653 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15654 if (row->mode_line_p)
15655 ++row;
15656 if (!row->enabled_p)
15657 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15658 }
15659
15660 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15661 {
15662 bool scroll_p = false, must_scroll = false;
15663 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15664
15665 if (PT > w->last_point)
15666 {
15667 /* Point has moved forward. */
15668 while (MATRIX_ROW_END_CHARPOS (row) < PT
15669 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15670 {
15671 eassert (row->enabled_p);
15672 ++row;
15673 }
15674
15675 /* If the end position of a row equals the start
15676 position of the next row, and PT is at that position,
15677 we would rather display cursor in the next line. */
15678 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15679 && MATRIX_ROW_END_CHARPOS (row) == PT
15680 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15681 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15682 && !cursor_row_p (row))
15683 ++row;
15684
15685 /* If within the scroll margin, scroll. Note that
15686 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15687 the next line would be drawn, and that
15688 this_scroll_margin can be zero. */
15689 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15690 || PT > MATRIX_ROW_END_CHARPOS (row)
15691 /* Line is completely visible last line in window
15692 and PT is to be set in the next line. */
15693 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15694 && PT == MATRIX_ROW_END_CHARPOS (row)
15695 && !row->ends_at_zv_p
15696 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15697 scroll_p = true;
15698 }
15699 else if (PT < w->last_point)
15700 {
15701 /* Cursor has to be moved backward. Note that PT >=
15702 CHARPOS (startp) because of the outer if-statement. */
15703 while (!row->mode_line_p
15704 && (MATRIX_ROW_START_CHARPOS (row) > PT
15705 || (MATRIX_ROW_START_CHARPOS (row) == PT
15706 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15707 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15708 row > w->current_matrix->rows
15709 && (row-1)->ends_in_newline_from_string_p))))
15710 && (row->y > top_scroll_margin
15711 || CHARPOS (startp) == BEGV))
15712 {
15713 eassert (row->enabled_p);
15714 --row;
15715 }
15716
15717 /* Consider the following case: Window starts at BEGV,
15718 there is invisible, intangible text at BEGV, so that
15719 display starts at some point START > BEGV. It can
15720 happen that we are called with PT somewhere between
15721 BEGV and START. Try to handle that case. */
15722 if (row < w->current_matrix->rows
15723 || row->mode_line_p)
15724 {
15725 row = w->current_matrix->rows;
15726 if (row->mode_line_p)
15727 ++row;
15728 }
15729
15730 /* Due to newlines in overlay strings, we may have to
15731 skip forward over overlay strings. */
15732 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15733 && MATRIX_ROW_END_CHARPOS (row) == PT
15734 && !cursor_row_p (row))
15735 ++row;
15736
15737 /* If within the scroll margin, scroll. */
15738 if (row->y < top_scroll_margin
15739 && CHARPOS (startp) != BEGV)
15740 scroll_p = true;
15741 }
15742 else
15743 {
15744 /* Cursor did not move. So don't scroll even if cursor line
15745 is partially visible, as it was so before. */
15746 rc = CURSOR_MOVEMENT_SUCCESS;
15747 }
15748
15749 if (PT < MATRIX_ROW_START_CHARPOS (row)
15750 || PT > MATRIX_ROW_END_CHARPOS (row))
15751 {
15752 /* if PT is not in the glyph row, give up. */
15753 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15754 must_scroll = true;
15755 }
15756 else if (rc != CURSOR_MOVEMENT_SUCCESS
15757 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15758 {
15759 struct glyph_row *row1;
15760
15761 /* If rows are bidi-reordered and point moved, back up
15762 until we find a row that does not belong to a
15763 continuation line. This is because we must consider
15764 all rows of a continued line as candidates for the
15765 new cursor positioning, since row start and end
15766 positions change non-linearly with vertical position
15767 in such rows. */
15768 /* FIXME: Revisit this when glyph ``spilling'' in
15769 continuation lines' rows is implemented for
15770 bidi-reordered rows. */
15771 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15772 MATRIX_ROW_CONTINUATION_LINE_P (row);
15773 --row)
15774 {
15775 /* If we hit the beginning of the displayed portion
15776 without finding the first row of a continued
15777 line, give up. */
15778 if (row <= row1)
15779 {
15780 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15781 break;
15782 }
15783 eassert (row->enabled_p);
15784 }
15785 }
15786 if (must_scroll)
15787 ;
15788 else if (rc != CURSOR_MOVEMENT_SUCCESS
15789 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15790 /* Make sure this isn't a header line by any chance, since
15791 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15792 && !row->mode_line_p
15793 && make_cursor_line_fully_visible_p)
15794 {
15795 if (PT == MATRIX_ROW_END_CHARPOS (row)
15796 && !row->ends_at_zv_p
15797 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15798 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15799 else if (row->height > window_box_height (w))
15800 {
15801 /* If we end up in a partially visible line, let's
15802 make it fully visible, except when it's taller
15803 than the window, in which case we can't do much
15804 about it. */
15805 *scroll_step = true;
15806 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15807 }
15808 else
15809 {
15810 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15811 if (!cursor_row_fully_visible_p (w, false, true))
15812 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15813 else
15814 rc = CURSOR_MOVEMENT_SUCCESS;
15815 }
15816 }
15817 else if (scroll_p)
15818 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15819 else if (rc != CURSOR_MOVEMENT_SUCCESS
15820 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15821 {
15822 /* With bidi-reordered rows, there could be more than
15823 one candidate row whose start and end positions
15824 occlude point. We need to let set_cursor_from_row
15825 find the best candidate. */
15826 /* FIXME: Revisit this when glyph ``spilling'' in
15827 continuation lines' rows is implemented for
15828 bidi-reordered rows. */
15829 bool rv = false;
15830
15831 do
15832 {
15833 bool at_zv_p = false, exact_match_p = false;
15834
15835 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15836 && PT <= MATRIX_ROW_END_CHARPOS (row)
15837 && cursor_row_p (row))
15838 rv |= set_cursor_from_row (w, row, w->current_matrix,
15839 0, 0, 0, 0);
15840 /* As soon as we've found the exact match for point,
15841 or the first suitable row whose ends_at_zv_p flag
15842 is set, we are done. */
15843 if (rv)
15844 {
15845 at_zv_p = MATRIX_ROW (w->current_matrix,
15846 w->cursor.vpos)->ends_at_zv_p;
15847 if (!at_zv_p
15848 && w->cursor.hpos >= 0
15849 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15850 w->cursor.vpos))
15851 {
15852 struct glyph_row *candidate =
15853 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15854 struct glyph *g =
15855 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15856 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15857
15858 exact_match_p =
15859 (BUFFERP (g->object) && g->charpos == PT)
15860 || (NILP (g->object)
15861 && (g->charpos == PT
15862 || (g->charpos == 0 && endpos - 1 == PT)));
15863 }
15864 if (at_zv_p || exact_match_p)
15865 {
15866 rc = CURSOR_MOVEMENT_SUCCESS;
15867 break;
15868 }
15869 }
15870 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15871 break;
15872 ++row;
15873 }
15874 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15875 || row->continued_p)
15876 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15877 || (MATRIX_ROW_START_CHARPOS (row) == PT
15878 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15879 /* If we didn't find any candidate rows, or exited the
15880 loop before all the candidates were examined, signal
15881 to the caller that this method failed. */
15882 if (rc != CURSOR_MOVEMENT_SUCCESS
15883 && !(rv
15884 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15885 && !row->continued_p))
15886 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15887 else if (rv)
15888 rc = CURSOR_MOVEMENT_SUCCESS;
15889 }
15890 else
15891 {
15892 do
15893 {
15894 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15895 {
15896 rc = CURSOR_MOVEMENT_SUCCESS;
15897 break;
15898 }
15899 ++row;
15900 }
15901 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15902 && MATRIX_ROW_START_CHARPOS (row) == PT
15903 && cursor_row_p (row));
15904 }
15905 }
15906 }
15907
15908 return rc;
15909 }
15910
15911
15912 void
15913 set_vertical_scroll_bar (struct window *w)
15914 {
15915 ptrdiff_t start, end, whole;
15916
15917 /* Calculate the start and end positions for the current window.
15918 At some point, it would be nice to choose between scrollbars
15919 which reflect the whole buffer size, with special markers
15920 indicating narrowing, and scrollbars which reflect only the
15921 visible region.
15922
15923 Note that mini-buffers sometimes aren't displaying any text. */
15924 if (!MINI_WINDOW_P (w)
15925 || (w == XWINDOW (minibuf_window)
15926 && NILP (echo_area_buffer[0])))
15927 {
15928 struct buffer *buf = XBUFFER (w->contents);
15929 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15930 start = marker_position (w->start) - BUF_BEGV (buf);
15931 /* I don't think this is guaranteed to be right. For the
15932 moment, we'll pretend it is. */
15933 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15934
15935 if (end < start)
15936 end = start;
15937 if (whole < (end - start))
15938 whole = end - start;
15939 }
15940 else
15941 start = end = whole = 0;
15942
15943 /* Indicate what this scroll bar ought to be displaying now. */
15944 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15945 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15946 (w, end - start, whole, start);
15947 }
15948
15949
15950 void
15951 set_horizontal_scroll_bar (struct window *w)
15952 {
15953 int start, end, whole, portion;
15954
15955 if (!MINI_WINDOW_P (w)
15956 || (w == XWINDOW (minibuf_window)
15957 && NILP (echo_area_buffer[0])))
15958 {
15959 struct buffer *b = XBUFFER (w->contents);
15960 struct buffer *old_buffer = NULL;
15961 struct it it;
15962 struct text_pos startp;
15963
15964 if (b != current_buffer)
15965 {
15966 old_buffer = current_buffer;
15967 set_buffer_internal (b);
15968 }
15969
15970 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15971 start_display (&it, w, startp);
15972 it.last_visible_x = INT_MAX;
15973 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15974 MOVE_TO_X | MOVE_TO_Y);
15975 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15976 window_box_height (w), -1,
15977 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15978
15979 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15980 end = start + window_box_width (w, TEXT_AREA);
15981 portion = end - start;
15982 /* After enlarging a horizontally scrolled window such that it
15983 gets at least as wide as the text it contains, make sure that
15984 the thumb doesn't fill the entire scroll bar so we can still
15985 drag it back to see the entire text. */
15986 whole = max (whole, end);
15987
15988 if (it.bidi_p)
15989 {
15990 Lisp_Object pdir;
15991
15992 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15993 if (EQ (pdir, Qright_to_left))
15994 {
15995 start = whole - end;
15996 end = start + portion;
15997 }
15998 }
15999
16000 if (old_buffer)
16001 set_buffer_internal (old_buffer);
16002 }
16003 else
16004 start = end = whole = portion = 0;
16005
16006 w->hscroll_whole = whole;
16007
16008 /* Indicate what this scroll bar ought to be displaying now. */
16009 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16010 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16011 (w, portion, whole, start);
16012 }
16013
16014
16015 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16016 selected_window is redisplayed.
16017
16018 We can return without actually redisplaying the window if fonts has been
16019 changed on window's frame. In that case, redisplay_internal will retry.
16020
16021 As one of the important parts of redisplaying a window, we need to
16022 decide whether the previous window-start position (stored in the
16023 window's w->start marker position) is still valid, and if it isn't,
16024 recompute it. Some details about that:
16025
16026 . The previous window-start could be in a continuation line, in
16027 which case we need to recompute it when the window width
16028 changes. See compute_window_start_on_continuation_line and its
16029 call below.
16030
16031 . The text that changed since last redisplay could include the
16032 previous window-start position. In that case, we try to salvage
16033 what we can from the current glyph matrix by calling
16034 try_scrolling, which see.
16035
16036 . Some Emacs command could force us to use a specific window-start
16037 position by setting the window's force_start flag, or gently
16038 propose doing that by setting the window's optional_new_start
16039 flag. In these cases, we try using the specified start point if
16040 that succeeds (i.e. the window desired matrix is successfully
16041 recomputed, and point location is within the window). In case
16042 of optional_new_start, we first check if the specified start
16043 position is feasible, i.e. if it will allow point to be
16044 displayed in the window. If using the specified start point
16045 fails, e.g., if new fonts are needed to be loaded, we abort the
16046 redisplay cycle and leave it up to the next cycle to figure out
16047 things.
16048
16049 . Note that the window's force_start flag is sometimes set by
16050 redisplay itself, when it decides that the previous window start
16051 point is fine and should be kept. Search for "goto force_start"
16052 below to see the details. Like the values of window-start
16053 specified outside of redisplay, these internally-deduced values
16054 are tested for feasibility, and ignored if found to be
16055 unfeasible.
16056
16057 . Note that the function try_window, used to completely redisplay
16058 a window, accepts the window's start point as its argument.
16059 This is used several times in the redisplay code to control
16060 where the window start will be, according to user options such
16061 as scroll-conservatively, and also to ensure the screen line
16062 showing point will be fully (as opposed to partially) visible on
16063 display. */
16064
16065 static void
16066 redisplay_window (Lisp_Object window, bool just_this_one_p)
16067 {
16068 struct window *w = XWINDOW (window);
16069 struct frame *f = XFRAME (w->frame);
16070 struct buffer *buffer = XBUFFER (w->contents);
16071 struct buffer *old = current_buffer;
16072 struct text_pos lpoint, opoint, startp;
16073 bool update_mode_line;
16074 int tem;
16075 struct it it;
16076 /* Record it now because it's overwritten. */
16077 bool current_matrix_up_to_date_p = false;
16078 bool used_current_matrix_p = false;
16079 /* This is less strict than current_matrix_up_to_date_p.
16080 It indicates that the buffer contents and narrowing are unchanged. */
16081 bool buffer_unchanged_p = false;
16082 bool temp_scroll_step = false;
16083 ptrdiff_t count = SPECPDL_INDEX ();
16084 int rc;
16085 int centering_position = -1;
16086 bool last_line_misfit = false;
16087 ptrdiff_t beg_unchanged, end_unchanged;
16088 int frame_line_height;
16089
16090 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16091 opoint = lpoint;
16092
16093 #ifdef GLYPH_DEBUG
16094 *w->desired_matrix->method = 0;
16095 #endif
16096
16097 if (!just_this_one_p
16098 && REDISPLAY_SOME_P ()
16099 && !w->redisplay
16100 && !w->update_mode_line
16101 && !f->face_change
16102 && !f->redisplay
16103 && !buffer->text->redisplay
16104 && BUF_PT (buffer) == w->last_point)
16105 return;
16106
16107 /* Make sure that both W's markers are valid. */
16108 eassert (XMARKER (w->start)->buffer == buffer);
16109 eassert (XMARKER (w->pointm)->buffer == buffer);
16110
16111 /* We come here again if we need to run window-text-change-functions
16112 below. */
16113 restart:
16114 reconsider_clip_changes (w);
16115 frame_line_height = default_line_pixel_height (w);
16116
16117 /* Has the mode line to be updated? */
16118 update_mode_line = (w->update_mode_line
16119 || update_mode_lines
16120 || buffer->clip_changed
16121 || buffer->prevent_redisplay_optimizations_p);
16122
16123 if (!just_this_one_p)
16124 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16125 cleverly elsewhere. */
16126 w->must_be_updated_p = true;
16127
16128 if (MINI_WINDOW_P (w))
16129 {
16130 if (w == XWINDOW (echo_area_window)
16131 && !NILP (echo_area_buffer[0]))
16132 {
16133 if (update_mode_line)
16134 /* We may have to update a tty frame's menu bar or a
16135 tool-bar. Example `M-x C-h C-h C-g'. */
16136 goto finish_menu_bars;
16137 else
16138 /* We've already displayed the echo area glyphs in this window. */
16139 goto finish_scroll_bars;
16140 }
16141 else if ((w != XWINDOW (minibuf_window)
16142 || minibuf_level == 0)
16143 /* When buffer is nonempty, redisplay window normally. */
16144 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16145 /* Quail displays non-mini buffers in minibuffer window.
16146 In that case, redisplay the window normally. */
16147 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16148 {
16149 /* W is a mini-buffer window, but it's not active, so clear
16150 it. */
16151 int yb = window_text_bottom_y (w);
16152 struct glyph_row *row;
16153 int y;
16154
16155 for (y = 0, row = w->desired_matrix->rows;
16156 y < yb;
16157 y += row->height, ++row)
16158 blank_row (w, row, y);
16159 goto finish_scroll_bars;
16160 }
16161
16162 clear_glyph_matrix (w->desired_matrix);
16163 }
16164
16165 /* Otherwise set up data on this window; select its buffer and point
16166 value. */
16167 /* Really select the buffer, for the sake of buffer-local
16168 variables. */
16169 set_buffer_internal_1 (XBUFFER (w->contents));
16170
16171 current_matrix_up_to_date_p
16172 = (w->window_end_valid
16173 && !current_buffer->clip_changed
16174 && !current_buffer->prevent_redisplay_optimizations_p
16175 && !window_outdated (w));
16176
16177 /* Run the window-text-change-functions
16178 if it is possible that the text on the screen has changed
16179 (either due to modification of the text, or any other reason). */
16180 if (!current_matrix_up_to_date_p
16181 && !NILP (Vwindow_text_change_functions))
16182 {
16183 safe_run_hooks (Qwindow_text_change_functions);
16184 goto restart;
16185 }
16186
16187 beg_unchanged = BEG_UNCHANGED;
16188 end_unchanged = END_UNCHANGED;
16189
16190 SET_TEXT_POS (opoint, PT, PT_BYTE);
16191
16192 specbind (Qinhibit_point_motion_hooks, Qt);
16193
16194 buffer_unchanged_p
16195 = (w->window_end_valid
16196 && !current_buffer->clip_changed
16197 && !window_outdated (w));
16198
16199 /* When windows_or_buffers_changed is non-zero, we can't rely
16200 on the window end being valid, so set it to zero there. */
16201 if (windows_or_buffers_changed)
16202 {
16203 /* If window starts on a continuation line, maybe adjust the
16204 window start in case the window's width changed. */
16205 if (XMARKER (w->start)->buffer == current_buffer)
16206 compute_window_start_on_continuation_line (w);
16207
16208 w->window_end_valid = false;
16209 /* If so, we also can't rely on current matrix
16210 and should not fool try_cursor_movement below. */
16211 current_matrix_up_to_date_p = false;
16212 }
16213
16214 /* Some sanity checks. */
16215 CHECK_WINDOW_END (w);
16216 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16217 emacs_abort ();
16218 if (BYTEPOS (opoint) < CHARPOS (opoint))
16219 emacs_abort ();
16220
16221 if (mode_line_update_needed (w))
16222 update_mode_line = true;
16223
16224 /* Point refers normally to the selected window. For any other
16225 window, set up appropriate value. */
16226 if (!EQ (window, selected_window))
16227 {
16228 ptrdiff_t new_pt = marker_position (w->pointm);
16229 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16230
16231 if (new_pt < BEGV)
16232 {
16233 new_pt = BEGV;
16234 new_pt_byte = BEGV_BYTE;
16235 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16236 }
16237 else if (new_pt > (ZV - 1))
16238 {
16239 new_pt = ZV;
16240 new_pt_byte = ZV_BYTE;
16241 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16242 }
16243
16244 /* We don't use SET_PT so that the point-motion hooks don't run. */
16245 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16246 }
16247
16248 /* If any of the character widths specified in the display table
16249 have changed, invalidate the width run cache. It's true that
16250 this may be a bit late to catch such changes, but the rest of
16251 redisplay goes (non-fatally) haywire when the display table is
16252 changed, so why should we worry about doing any better? */
16253 if (current_buffer->width_run_cache
16254 || (current_buffer->base_buffer
16255 && current_buffer->base_buffer->width_run_cache))
16256 {
16257 struct Lisp_Char_Table *disptab = buffer_display_table ();
16258
16259 if (! disptab_matches_widthtab
16260 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16261 {
16262 struct buffer *buf = current_buffer;
16263
16264 if (buf->base_buffer)
16265 buf = buf->base_buffer;
16266 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16267 recompute_width_table (current_buffer, disptab);
16268 }
16269 }
16270
16271 /* If window-start is screwed up, choose a new one. */
16272 if (XMARKER (w->start)->buffer != current_buffer)
16273 goto recenter;
16274
16275 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16276
16277 /* If someone specified a new starting point but did not insist,
16278 check whether it can be used. */
16279 if ((w->optional_new_start || window_frozen_p (w))
16280 && CHARPOS (startp) >= BEGV
16281 && CHARPOS (startp) <= ZV)
16282 {
16283 ptrdiff_t it_charpos;
16284
16285 w->optional_new_start = false;
16286 start_display (&it, w, startp);
16287 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16288 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16289 /* Record IT's position now, since line_bottom_y might change
16290 that. */
16291 it_charpos = IT_CHARPOS (it);
16292 /* Make sure we set the force_start flag only if the cursor row
16293 will be fully visible. Otherwise, the code under force_start
16294 label below will try to move point back into view, which is
16295 not what the code which sets optional_new_start wants. */
16296 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16297 && !w->force_start)
16298 {
16299 if (it_charpos == PT)
16300 w->force_start = true;
16301 /* IT may overshoot PT if text at PT is invisible. */
16302 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16303 w->force_start = true;
16304 #ifdef GLYPH_DEBUG
16305 if (w->force_start)
16306 {
16307 if (window_frozen_p (w))
16308 debug_method_add (w, "set force_start from frozen window start");
16309 else
16310 debug_method_add (w, "set force_start from optional_new_start");
16311 }
16312 #endif
16313 }
16314 }
16315
16316 force_start:
16317
16318 /* Handle case where place to start displaying has been specified,
16319 unless the specified location is outside the accessible range. */
16320 if (w->force_start)
16321 {
16322 /* We set this later on if we have to adjust point. */
16323 int new_vpos = -1;
16324
16325 w->force_start = false;
16326 w->vscroll = 0;
16327 w->window_end_valid = false;
16328
16329 /* Forget any recorded base line for line number display. */
16330 if (!buffer_unchanged_p)
16331 w->base_line_number = 0;
16332
16333 /* Redisplay the mode line. Select the buffer properly for that.
16334 Also, run the hook window-scroll-functions
16335 because we have scrolled. */
16336 /* Note, we do this after clearing force_start because
16337 if there's an error, it is better to forget about force_start
16338 than to get into an infinite loop calling the hook functions
16339 and having them get more errors. */
16340 if (!update_mode_line
16341 || ! NILP (Vwindow_scroll_functions))
16342 {
16343 update_mode_line = true;
16344 w->update_mode_line = true;
16345 startp = run_window_scroll_functions (window, startp);
16346 }
16347
16348 if (CHARPOS (startp) < BEGV)
16349 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16350 else if (CHARPOS (startp) > ZV)
16351 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16352
16353 /* Redisplay, then check if cursor has been set during the
16354 redisplay. Give up if new fonts were loaded. */
16355 /* We used to issue a CHECK_MARGINS argument to try_window here,
16356 but this causes scrolling to fail when point begins inside
16357 the scroll margin (bug#148) -- cyd */
16358 if (!try_window (window, startp, 0))
16359 {
16360 w->force_start = true;
16361 clear_glyph_matrix (w->desired_matrix);
16362 goto need_larger_matrices;
16363 }
16364
16365 if (w->cursor.vpos < 0)
16366 {
16367 /* If point does not appear, try to move point so it does
16368 appear. The desired matrix has been built above, so we
16369 can use it here. First see if point is in invisible
16370 text, and if so, move it to the first visible buffer
16371 position past that. */
16372 struct glyph_row *r = NULL;
16373 Lisp_Object invprop =
16374 get_char_property_and_overlay (make_number (PT), Qinvisible,
16375 Qnil, NULL);
16376
16377 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16378 {
16379 ptrdiff_t alt_pt;
16380 Lisp_Object invprop_end =
16381 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16382 Qnil, Qnil);
16383
16384 if (NATNUMP (invprop_end))
16385 alt_pt = XFASTINT (invprop_end);
16386 else
16387 alt_pt = ZV;
16388 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16389 NULL, 0);
16390 }
16391 if (r)
16392 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16393 else /* Give up and just move to the middle of the window. */
16394 new_vpos = window_box_height (w) / 2;
16395 }
16396
16397 if (!cursor_row_fully_visible_p (w, false, false))
16398 {
16399 /* Point does appear, but on a line partly visible at end of window.
16400 Move it back to a fully-visible line. */
16401 new_vpos = window_box_height (w);
16402 /* But if window_box_height suggests a Y coordinate that is
16403 not less than we already have, that line will clearly not
16404 be fully visible, so give up and scroll the display.
16405 This can happen when the default face uses a font whose
16406 dimensions are different from the frame's default
16407 font. */
16408 if (new_vpos >= w->cursor.y)
16409 {
16410 w->cursor.vpos = -1;
16411 clear_glyph_matrix (w->desired_matrix);
16412 goto try_to_scroll;
16413 }
16414 }
16415 else if (w->cursor.vpos >= 0)
16416 {
16417 /* Some people insist on not letting point enter the scroll
16418 margin, even though this part handles windows that didn't
16419 scroll at all. */
16420 int window_total_lines
16421 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16422 int margin = min (scroll_margin, window_total_lines / 4);
16423 int pixel_margin = margin * frame_line_height;
16424 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16425
16426 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16427 below, which finds the row to move point to, advances by
16428 the Y coordinate of the _next_ row, see the definition of
16429 MATRIX_ROW_BOTTOM_Y. */
16430 if (w->cursor.vpos < margin + header_line)
16431 {
16432 w->cursor.vpos = -1;
16433 clear_glyph_matrix (w->desired_matrix);
16434 goto try_to_scroll;
16435 }
16436 else
16437 {
16438 int window_height = window_box_height (w);
16439
16440 if (header_line)
16441 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16442 if (w->cursor.y >= window_height - pixel_margin)
16443 {
16444 w->cursor.vpos = -1;
16445 clear_glyph_matrix (w->desired_matrix);
16446 goto try_to_scroll;
16447 }
16448 }
16449 }
16450
16451 /* If we need to move point for either of the above reasons,
16452 now actually do it. */
16453 if (new_vpos >= 0)
16454 {
16455 struct glyph_row *row;
16456
16457 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16458 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16459 ++row;
16460
16461 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16462 MATRIX_ROW_START_BYTEPOS (row));
16463
16464 if (w != XWINDOW (selected_window))
16465 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16466 else if (current_buffer == old)
16467 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16468
16469 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16470
16471 /* Re-run pre-redisplay-function so it can update the region
16472 according to the new position of point. */
16473 /* Other than the cursor, w's redisplay is done so we can set its
16474 redisplay to false. Also the buffer's redisplay can be set to
16475 false, since propagate_buffer_redisplay should have already
16476 propagated its info to `w' anyway. */
16477 w->redisplay = false;
16478 XBUFFER (w->contents)->text->redisplay = false;
16479 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16480
16481 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16482 {
16483 /* pre-redisplay-function made changes (e.g. move the region)
16484 that require another round of redisplay. */
16485 clear_glyph_matrix (w->desired_matrix);
16486 if (!try_window (window, startp, 0))
16487 goto need_larger_matrices;
16488 }
16489 }
16490 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16491 {
16492 clear_glyph_matrix (w->desired_matrix);
16493 goto try_to_scroll;
16494 }
16495
16496 #ifdef GLYPH_DEBUG
16497 debug_method_add (w, "forced window start");
16498 #endif
16499 goto done;
16500 }
16501
16502 /* Handle case where text has not changed, only point, and it has
16503 not moved off the frame, and we are not retrying after hscroll.
16504 (current_matrix_up_to_date_p is true when retrying.) */
16505 if (current_matrix_up_to_date_p
16506 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16507 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16508 {
16509 switch (rc)
16510 {
16511 case CURSOR_MOVEMENT_SUCCESS:
16512 used_current_matrix_p = true;
16513 goto done;
16514
16515 case CURSOR_MOVEMENT_MUST_SCROLL:
16516 goto try_to_scroll;
16517
16518 default:
16519 emacs_abort ();
16520 }
16521 }
16522 /* If current starting point was originally the beginning of a line
16523 but no longer is, find a new starting point. */
16524 else if (w->start_at_line_beg
16525 && !(CHARPOS (startp) <= BEGV
16526 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16527 {
16528 #ifdef GLYPH_DEBUG
16529 debug_method_add (w, "recenter 1");
16530 #endif
16531 goto recenter;
16532 }
16533
16534 /* Try scrolling with try_window_id. Value is > 0 if update has
16535 been done, it is -1 if we know that the same window start will
16536 not work. It is 0 if unsuccessful for some other reason. */
16537 else if ((tem = try_window_id (w)) != 0)
16538 {
16539 #ifdef GLYPH_DEBUG
16540 debug_method_add (w, "try_window_id %d", tem);
16541 #endif
16542
16543 if (f->fonts_changed)
16544 goto need_larger_matrices;
16545 if (tem > 0)
16546 goto done;
16547
16548 /* Otherwise try_window_id has returned -1 which means that we
16549 don't want the alternative below this comment to execute. */
16550 }
16551 else if (CHARPOS (startp) >= BEGV
16552 && CHARPOS (startp) <= ZV
16553 && PT >= CHARPOS (startp)
16554 && (CHARPOS (startp) < ZV
16555 /* Avoid starting at end of buffer. */
16556 || CHARPOS (startp) == BEGV
16557 || !window_outdated (w)))
16558 {
16559 int d1, d2, d5, d6;
16560 int rtop, rbot;
16561
16562 /* If first window line is a continuation line, and window start
16563 is inside the modified region, but the first change is before
16564 current window start, we must select a new window start.
16565
16566 However, if this is the result of a down-mouse event (e.g. by
16567 extending the mouse-drag-overlay), we don't want to select a
16568 new window start, since that would change the position under
16569 the mouse, resulting in an unwanted mouse-movement rather
16570 than a simple mouse-click. */
16571 if (!w->start_at_line_beg
16572 && NILP (do_mouse_tracking)
16573 && CHARPOS (startp) > BEGV
16574 && CHARPOS (startp) > BEG + beg_unchanged
16575 && CHARPOS (startp) <= Z - end_unchanged
16576 /* Even if w->start_at_line_beg is nil, a new window may
16577 start at a line_beg, since that's how set_buffer_window
16578 sets it. So, we need to check the return value of
16579 compute_window_start_on_continuation_line. (See also
16580 bug#197). */
16581 && XMARKER (w->start)->buffer == current_buffer
16582 && compute_window_start_on_continuation_line (w)
16583 /* It doesn't make sense to force the window start like we
16584 do at label force_start if it is already known that point
16585 will not be fully visible in the resulting window, because
16586 doing so will move point from its correct position
16587 instead of scrolling the window to bring point into view.
16588 See bug#9324. */
16589 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16590 /* A very tall row could need more than the window height,
16591 in which case we accept that it is partially visible. */
16592 && (rtop != 0) == (rbot != 0))
16593 {
16594 w->force_start = true;
16595 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16596 #ifdef GLYPH_DEBUG
16597 debug_method_add (w, "recomputed window start in continuation line");
16598 #endif
16599 goto force_start;
16600 }
16601
16602 #ifdef GLYPH_DEBUG
16603 debug_method_add (w, "same window start");
16604 #endif
16605
16606 /* Try to redisplay starting at same place as before.
16607 If point has not moved off frame, accept the results. */
16608 if (!current_matrix_up_to_date_p
16609 /* Don't use try_window_reusing_current_matrix in this case
16610 because a window scroll function can have changed the
16611 buffer. */
16612 || !NILP (Vwindow_scroll_functions)
16613 || MINI_WINDOW_P (w)
16614 || !(used_current_matrix_p
16615 = try_window_reusing_current_matrix (w)))
16616 {
16617 IF_DEBUG (debug_method_add (w, "1"));
16618 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16619 /* -1 means we need to scroll.
16620 0 means we need new matrices, but fonts_changed
16621 is set in that case, so we will detect it below. */
16622 goto try_to_scroll;
16623 }
16624
16625 if (f->fonts_changed)
16626 goto need_larger_matrices;
16627
16628 if (w->cursor.vpos >= 0)
16629 {
16630 if (!just_this_one_p
16631 || current_buffer->clip_changed
16632 || BEG_UNCHANGED < CHARPOS (startp))
16633 /* Forget any recorded base line for line number display. */
16634 w->base_line_number = 0;
16635
16636 if (!cursor_row_fully_visible_p (w, true, false))
16637 {
16638 clear_glyph_matrix (w->desired_matrix);
16639 last_line_misfit = true;
16640 }
16641 /* Drop through and scroll. */
16642 else
16643 goto done;
16644 }
16645 else
16646 clear_glyph_matrix (w->desired_matrix);
16647 }
16648
16649 try_to_scroll:
16650
16651 /* Redisplay the mode line. Select the buffer properly for that. */
16652 if (!update_mode_line)
16653 {
16654 update_mode_line = true;
16655 w->update_mode_line = true;
16656 }
16657
16658 /* Try to scroll by specified few lines. */
16659 if ((scroll_conservatively
16660 || emacs_scroll_step
16661 || temp_scroll_step
16662 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16663 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16664 && CHARPOS (startp) >= BEGV
16665 && CHARPOS (startp) <= ZV)
16666 {
16667 /* The function returns -1 if new fonts were loaded, 1 if
16668 successful, 0 if not successful. */
16669 int ss = try_scrolling (window, just_this_one_p,
16670 scroll_conservatively,
16671 emacs_scroll_step,
16672 temp_scroll_step, last_line_misfit);
16673 switch (ss)
16674 {
16675 case SCROLLING_SUCCESS:
16676 goto done;
16677
16678 case SCROLLING_NEED_LARGER_MATRICES:
16679 goto need_larger_matrices;
16680
16681 case SCROLLING_FAILED:
16682 break;
16683
16684 default:
16685 emacs_abort ();
16686 }
16687 }
16688
16689 /* Finally, just choose a place to start which positions point
16690 according to user preferences. */
16691
16692 recenter:
16693
16694 #ifdef GLYPH_DEBUG
16695 debug_method_add (w, "recenter");
16696 #endif
16697
16698 /* Forget any previously recorded base line for line number display. */
16699 if (!buffer_unchanged_p)
16700 w->base_line_number = 0;
16701
16702 /* Determine the window start relative to point. */
16703 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16704 it.current_y = it.last_visible_y;
16705 if (centering_position < 0)
16706 {
16707 int window_total_lines
16708 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16709 int margin
16710 = scroll_margin > 0
16711 ? min (scroll_margin, window_total_lines / 4)
16712 : 0;
16713 ptrdiff_t margin_pos = CHARPOS (startp);
16714 Lisp_Object aggressive;
16715 bool scrolling_up;
16716
16717 /* If there is a scroll margin at the top of the window, find
16718 its character position. */
16719 if (margin
16720 /* Cannot call start_display if startp is not in the
16721 accessible region of the buffer. This can happen when we
16722 have just switched to a different buffer and/or changed
16723 its restriction. In that case, startp is initialized to
16724 the character position 1 (BEGV) because we did not yet
16725 have chance to display the buffer even once. */
16726 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16727 {
16728 struct it it1;
16729 void *it1data = NULL;
16730
16731 SAVE_IT (it1, it, it1data);
16732 start_display (&it1, w, startp);
16733 move_it_vertically (&it1, margin * frame_line_height);
16734 margin_pos = IT_CHARPOS (it1);
16735 RESTORE_IT (&it, &it, it1data);
16736 }
16737 scrolling_up = PT > margin_pos;
16738 aggressive =
16739 scrolling_up
16740 ? BVAR (current_buffer, scroll_up_aggressively)
16741 : BVAR (current_buffer, scroll_down_aggressively);
16742
16743 if (!MINI_WINDOW_P (w)
16744 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16745 {
16746 int pt_offset = 0;
16747
16748 /* Setting scroll-conservatively overrides
16749 scroll-*-aggressively. */
16750 if (!scroll_conservatively && NUMBERP (aggressive))
16751 {
16752 double float_amount = XFLOATINT (aggressive);
16753
16754 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16755 if (pt_offset == 0 && float_amount > 0)
16756 pt_offset = 1;
16757 if (pt_offset && margin > 0)
16758 margin -= 1;
16759 }
16760 /* Compute how much to move the window start backward from
16761 point so that point will be displayed where the user
16762 wants it. */
16763 if (scrolling_up)
16764 {
16765 centering_position = it.last_visible_y;
16766 if (pt_offset)
16767 centering_position -= pt_offset;
16768 centering_position -=
16769 (frame_line_height * (1 + margin + last_line_misfit)
16770 + WINDOW_HEADER_LINE_HEIGHT (w));
16771 /* Don't let point enter the scroll margin near top of
16772 the window. */
16773 if (centering_position < margin * frame_line_height)
16774 centering_position = margin * frame_line_height;
16775 }
16776 else
16777 centering_position = margin * frame_line_height + pt_offset;
16778 }
16779 else
16780 /* Set the window start half the height of the window backward
16781 from point. */
16782 centering_position = window_box_height (w) / 2;
16783 }
16784 move_it_vertically_backward (&it, centering_position);
16785
16786 eassert (IT_CHARPOS (it) >= BEGV);
16787
16788 /* The function move_it_vertically_backward may move over more
16789 than the specified y-distance. If it->w is small, e.g. a
16790 mini-buffer window, we may end up in front of the window's
16791 display area. Start displaying at the start of the line
16792 containing PT in this case. */
16793 if (it.current_y <= 0)
16794 {
16795 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16796 move_it_vertically_backward (&it, 0);
16797 it.current_y = 0;
16798 }
16799
16800 it.current_x = it.hpos = 0;
16801
16802 /* Set the window start position here explicitly, to avoid an
16803 infinite loop in case the functions in window-scroll-functions
16804 get errors. */
16805 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16806
16807 /* Run scroll hooks. */
16808 startp = run_window_scroll_functions (window, it.current.pos);
16809
16810 /* Redisplay the window. */
16811 bool use_desired_matrix = false;
16812 if (!current_matrix_up_to_date_p
16813 || windows_or_buffers_changed
16814 || f->cursor_type_changed
16815 /* Don't use try_window_reusing_current_matrix in this case
16816 because it can have changed the buffer. */
16817 || !NILP (Vwindow_scroll_functions)
16818 || !just_this_one_p
16819 || MINI_WINDOW_P (w)
16820 || !(used_current_matrix_p
16821 = try_window_reusing_current_matrix (w)))
16822 use_desired_matrix = (try_window (window, startp, 0) == 1);
16823
16824 /* If new fonts have been loaded (due to fontsets), give up. We
16825 have to start a new redisplay since we need to re-adjust glyph
16826 matrices. */
16827 if (f->fonts_changed)
16828 goto need_larger_matrices;
16829
16830 /* If cursor did not appear assume that the middle of the window is
16831 in the first line of the window. Do it again with the next line.
16832 (Imagine a window of height 100, displaying two lines of height
16833 60. Moving back 50 from it->last_visible_y will end in the first
16834 line.) */
16835 if (w->cursor.vpos < 0)
16836 {
16837 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16838 {
16839 clear_glyph_matrix (w->desired_matrix);
16840 move_it_by_lines (&it, 1);
16841 try_window (window, it.current.pos, 0);
16842 }
16843 else if (PT < IT_CHARPOS (it))
16844 {
16845 clear_glyph_matrix (w->desired_matrix);
16846 move_it_by_lines (&it, -1);
16847 try_window (window, it.current.pos, 0);
16848 }
16849 else
16850 {
16851 /* Not much we can do about it. */
16852 }
16853 }
16854
16855 /* Consider the following case: Window starts at BEGV, there is
16856 invisible, intangible text at BEGV, so that display starts at
16857 some point START > BEGV. It can happen that we are called with
16858 PT somewhere between BEGV and START. Try to handle that case,
16859 and similar ones. */
16860 if (w->cursor.vpos < 0)
16861 {
16862 /* Prefer the desired matrix to the current matrix, if possible,
16863 in the fallback calculations below. This is because using
16864 the current matrix might completely goof, e.g. if its first
16865 row is after point. */
16866 struct glyph_matrix *matrix =
16867 use_desired_matrix ? w->desired_matrix : w->current_matrix;
16868 /* First, try locating the proper glyph row for PT. */
16869 struct glyph_row *row =
16870 row_containing_pos (w, PT, matrix->rows, NULL, 0);
16871
16872 /* Sometimes point is at the beginning of invisible text that is
16873 before the 1st character displayed in the row. In that case,
16874 row_containing_pos fails to find the row, because no glyphs
16875 with appropriate buffer positions are present in the row.
16876 Therefore, we next try to find the row which shows the 1st
16877 position after the invisible text. */
16878 if (!row)
16879 {
16880 Lisp_Object val =
16881 get_char_property_and_overlay (make_number (PT), Qinvisible,
16882 Qnil, NULL);
16883
16884 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
16885 {
16886 ptrdiff_t alt_pos;
16887 Lisp_Object invis_end =
16888 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16889 Qnil, Qnil);
16890
16891 if (NATNUMP (invis_end))
16892 alt_pos = XFASTINT (invis_end);
16893 else
16894 alt_pos = ZV;
16895 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
16896 }
16897 }
16898 /* Finally, fall back on the first row of the window after the
16899 header line (if any). This is slightly better than not
16900 displaying the cursor at all. */
16901 if (!row)
16902 {
16903 row = matrix->rows;
16904 if (row->mode_line_p)
16905 ++row;
16906 }
16907 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
16908 }
16909
16910 if (!cursor_row_fully_visible_p (w, false, false))
16911 {
16912 /* If vscroll is enabled, disable it and try again. */
16913 if (w->vscroll)
16914 {
16915 w->vscroll = 0;
16916 clear_glyph_matrix (w->desired_matrix);
16917 goto recenter;
16918 }
16919
16920 /* Users who set scroll-conservatively to a large number want
16921 point just above/below the scroll margin. If we ended up
16922 with point's row partially visible, move the window start to
16923 make that row fully visible and out of the margin. */
16924 if (scroll_conservatively > SCROLL_LIMIT)
16925 {
16926 int window_total_lines
16927 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16928 int margin =
16929 scroll_margin > 0
16930 ? min (scroll_margin, window_total_lines / 4)
16931 : 0;
16932 bool move_down = w->cursor.vpos >= window_total_lines / 2;
16933
16934 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16935 clear_glyph_matrix (w->desired_matrix);
16936 if (1 == try_window (window, it.current.pos,
16937 TRY_WINDOW_CHECK_MARGINS))
16938 goto done;
16939 }
16940
16941 /* If centering point failed to make the whole line visible,
16942 put point at the top instead. That has to make the whole line
16943 visible, if it can be done. */
16944 if (centering_position == 0)
16945 goto done;
16946
16947 clear_glyph_matrix (w->desired_matrix);
16948 centering_position = 0;
16949 goto recenter;
16950 }
16951
16952 done:
16953
16954 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16955 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16956 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16957
16958 /* Display the mode line, if we must. */
16959 if ((update_mode_line
16960 /* If window not full width, must redo its mode line
16961 if (a) the window to its side is being redone and
16962 (b) we do a frame-based redisplay. This is a consequence
16963 of how inverted lines are drawn in frame-based redisplay. */
16964 || (!just_this_one_p
16965 && !FRAME_WINDOW_P (f)
16966 && !WINDOW_FULL_WIDTH_P (w))
16967 /* Line number to display. */
16968 || w->base_line_pos > 0
16969 /* Column number is displayed and different from the one displayed. */
16970 || (w->column_number_displayed != -1
16971 && (w->column_number_displayed != current_column ())))
16972 /* This means that the window has a mode line. */
16973 && (WINDOW_WANTS_MODELINE_P (w)
16974 || WINDOW_WANTS_HEADER_LINE_P (w)))
16975 {
16976
16977 display_mode_lines (w);
16978
16979 /* If mode line height has changed, arrange for a thorough
16980 immediate redisplay using the correct mode line height. */
16981 if (WINDOW_WANTS_MODELINE_P (w)
16982 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16983 {
16984 f->fonts_changed = true;
16985 w->mode_line_height = -1;
16986 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16987 = DESIRED_MODE_LINE_HEIGHT (w);
16988 }
16989
16990 /* If header line height has changed, arrange for a thorough
16991 immediate redisplay using the correct header line height. */
16992 if (WINDOW_WANTS_HEADER_LINE_P (w)
16993 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16994 {
16995 f->fonts_changed = true;
16996 w->header_line_height = -1;
16997 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16998 = DESIRED_HEADER_LINE_HEIGHT (w);
16999 }
17000
17001 if (f->fonts_changed)
17002 goto need_larger_matrices;
17003 }
17004
17005 if (!line_number_displayed && w->base_line_pos != -1)
17006 {
17007 w->base_line_pos = 0;
17008 w->base_line_number = 0;
17009 }
17010
17011 finish_menu_bars:
17012
17013 /* When we reach a frame's selected window, redo the frame's menu
17014 bar and the frame's title. */
17015 if (update_mode_line
17016 && EQ (FRAME_SELECTED_WINDOW (f), window))
17017 {
17018 bool redisplay_menu_p;
17019
17020 if (FRAME_WINDOW_P (f))
17021 {
17022 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17023 || defined (HAVE_NS) || defined (USE_GTK)
17024 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17025 #else
17026 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17027 #endif
17028 }
17029 else
17030 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17031
17032 if (redisplay_menu_p)
17033 display_menu_bar (w);
17034
17035 #ifdef HAVE_WINDOW_SYSTEM
17036 if (FRAME_WINDOW_P (f))
17037 {
17038 #if defined (USE_GTK) || defined (HAVE_NS)
17039 if (FRAME_EXTERNAL_TOOL_BAR (f))
17040 redisplay_tool_bar (f);
17041 #else
17042 if (WINDOWP (f->tool_bar_window)
17043 && (FRAME_TOOL_BAR_LINES (f) > 0
17044 || !NILP (Vauto_resize_tool_bars))
17045 && redisplay_tool_bar (f))
17046 ignore_mouse_drag_p = true;
17047 #endif
17048 }
17049 x_consider_frame_title (w->frame);
17050 #endif
17051 }
17052
17053 #ifdef HAVE_WINDOW_SYSTEM
17054 if (FRAME_WINDOW_P (f)
17055 && update_window_fringes (w, (just_this_one_p
17056 || (!used_current_matrix_p && !overlay_arrow_seen)
17057 || w->pseudo_window_p)))
17058 {
17059 update_begin (f);
17060 block_input ();
17061 if (draw_window_fringes (w, true))
17062 {
17063 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17064 x_draw_right_divider (w);
17065 else
17066 x_draw_vertical_border (w);
17067 }
17068 unblock_input ();
17069 update_end (f);
17070 }
17071
17072 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17073 x_draw_bottom_divider (w);
17074 #endif /* HAVE_WINDOW_SYSTEM */
17075
17076 /* We go to this label, with fonts_changed set, if it is
17077 necessary to try again using larger glyph matrices.
17078 We have to redeem the scroll bar even in this case,
17079 because the loop in redisplay_internal expects that. */
17080 need_larger_matrices:
17081 ;
17082 finish_scroll_bars:
17083
17084 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17085 {
17086 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17087 /* Set the thumb's position and size. */
17088 set_vertical_scroll_bar (w);
17089
17090 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17091 /* Set the thumb's position and size. */
17092 set_horizontal_scroll_bar (w);
17093
17094 /* Note that we actually used the scroll bar attached to this
17095 window, so it shouldn't be deleted at the end of redisplay. */
17096 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17097 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17098 }
17099
17100 /* Restore current_buffer and value of point in it. The window
17101 update may have changed the buffer, so first make sure `opoint'
17102 is still valid (Bug#6177). */
17103 if (CHARPOS (opoint) < BEGV)
17104 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17105 else if (CHARPOS (opoint) > ZV)
17106 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17107 else
17108 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17109
17110 set_buffer_internal_1 (old);
17111 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17112 shorter. This can be caused by log truncation in *Messages*. */
17113 if (CHARPOS (lpoint) <= ZV)
17114 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17115
17116 unbind_to (count, Qnil);
17117 }
17118
17119
17120 /* Build the complete desired matrix of WINDOW with a window start
17121 buffer position POS.
17122
17123 Value is 1 if successful. It is zero if fonts were loaded during
17124 redisplay which makes re-adjusting glyph matrices necessary, and -1
17125 if point would appear in the scroll margins.
17126 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17127 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17128 set in FLAGS.) */
17129
17130 int
17131 try_window (Lisp_Object window, struct text_pos pos, int flags)
17132 {
17133 struct window *w = XWINDOW (window);
17134 struct it it;
17135 struct glyph_row *last_text_row = NULL;
17136 struct frame *f = XFRAME (w->frame);
17137 int frame_line_height = default_line_pixel_height (w);
17138
17139 /* Make POS the new window start. */
17140 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17141
17142 /* Mark cursor position as unknown. No overlay arrow seen. */
17143 w->cursor.vpos = -1;
17144 overlay_arrow_seen = false;
17145
17146 /* Initialize iterator and info to start at POS. */
17147 start_display (&it, w, pos);
17148 it.glyph_row->reversed_p = false;
17149
17150 /* Display all lines of W. */
17151 while (it.current_y < it.last_visible_y)
17152 {
17153 if (display_line (&it))
17154 last_text_row = it.glyph_row - 1;
17155 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17156 return 0;
17157 }
17158
17159 /* Don't let the cursor end in the scroll margins. */
17160 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17161 && !MINI_WINDOW_P (w))
17162 {
17163 int this_scroll_margin;
17164 int window_total_lines
17165 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17166
17167 if (scroll_margin > 0)
17168 {
17169 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
17170 this_scroll_margin *= frame_line_height;
17171 }
17172 else
17173 this_scroll_margin = 0;
17174
17175 if ((w->cursor.y >= 0 /* not vscrolled */
17176 && w->cursor.y < this_scroll_margin
17177 && CHARPOS (pos) > BEGV
17178 && IT_CHARPOS (it) < ZV)
17179 /* rms: considering make_cursor_line_fully_visible_p here
17180 seems to give wrong results. We don't want to recenter
17181 when the last line is partly visible, we want to allow
17182 that case to be handled in the usual way. */
17183 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
17184 {
17185 w->cursor.vpos = -1;
17186 clear_glyph_matrix (w->desired_matrix);
17187 return -1;
17188 }
17189 }
17190
17191 /* If bottom moved off end of frame, change mode line percentage. */
17192 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
17193 w->update_mode_line = true;
17194
17195 /* Set window_end_pos to the offset of the last character displayed
17196 on the window from the end of current_buffer. Set
17197 window_end_vpos to its row number. */
17198 if (last_text_row)
17199 {
17200 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17201 adjust_window_ends (w, last_text_row, false);
17202 eassert
17203 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17204 w->window_end_vpos)));
17205 }
17206 else
17207 {
17208 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17209 w->window_end_pos = Z - ZV;
17210 w->window_end_vpos = 0;
17211 }
17212
17213 /* But that is not valid info until redisplay finishes. */
17214 w->window_end_valid = false;
17215 return 1;
17216 }
17217
17218
17219 \f
17220 /************************************************************************
17221 Window redisplay reusing current matrix when buffer has not changed
17222 ************************************************************************/
17223
17224 /* Try redisplay of window W showing an unchanged buffer with a
17225 different window start than the last time it was displayed by
17226 reusing its current matrix. Value is true if successful.
17227 W->start is the new window start. */
17228
17229 static bool
17230 try_window_reusing_current_matrix (struct window *w)
17231 {
17232 struct frame *f = XFRAME (w->frame);
17233 struct glyph_row *bottom_row;
17234 struct it it;
17235 struct run run;
17236 struct text_pos start, new_start;
17237 int nrows_scrolled, i;
17238 struct glyph_row *last_text_row;
17239 struct glyph_row *last_reused_text_row;
17240 struct glyph_row *start_row;
17241 int start_vpos, min_y, max_y;
17242
17243 #ifdef GLYPH_DEBUG
17244 if (inhibit_try_window_reusing)
17245 return false;
17246 #endif
17247
17248 if (/* This function doesn't handle terminal frames. */
17249 !FRAME_WINDOW_P (f)
17250 /* Don't try to reuse the display if windows have been split
17251 or such. */
17252 || windows_or_buffers_changed
17253 || f->cursor_type_changed)
17254 return false;
17255
17256 /* Can't do this if showing trailing whitespace. */
17257 if (!NILP (Vshow_trailing_whitespace))
17258 return false;
17259
17260 /* If top-line visibility has changed, give up. */
17261 if (WINDOW_WANTS_HEADER_LINE_P (w)
17262 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17263 return false;
17264
17265 /* Give up if old or new display is scrolled vertically. We could
17266 make this function handle this, but right now it doesn't. */
17267 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17268 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17269 return false;
17270
17271 /* The variable new_start now holds the new window start. The old
17272 start `start' can be determined from the current matrix. */
17273 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17274 start = start_row->minpos;
17275 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17276
17277 /* Clear the desired matrix for the display below. */
17278 clear_glyph_matrix (w->desired_matrix);
17279
17280 if (CHARPOS (new_start) <= CHARPOS (start))
17281 {
17282 /* Don't use this method if the display starts with an ellipsis
17283 displayed for invisible text. It's not easy to handle that case
17284 below, and it's certainly not worth the effort since this is
17285 not a frequent case. */
17286 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17287 return false;
17288
17289 IF_DEBUG (debug_method_add (w, "twu1"));
17290
17291 /* Display up to a row that can be reused. The variable
17292 last_text_row is set to the last row displayed that displays
17293 text. Note that it.vpos == 0 if or if not there is a
17294 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17295 start_display (&it, w, new_start);
17296 w->cursor.vpos = -1;
17297 last_text_row = last_reused_text_row = NULL;
17298
17299 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17300 {
17301 /* If we have reached into the characters in the START row,
17302 that means the line boundaries have changed. So we
17303 can't start copying with the row START. Maybe it will
17304 work to start copying with the following row. */
17305 while (IT_CHARPOS (it) > CHARPOS (start))
17306 {
17307 /* Advance to the next row as the "start". */
17308 start_row++;
17309 start = start_row->minpos;
17310 /* If there are no more rows to try, or just one, give up. */
17311 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17312 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17313 || CHARPOS (start) == ZV)
17314 {
17315 clear_glyph_matrix (w->desired_matrix);
17316 return false;
17317 }
17318
17319 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17320 }
17321 /* If we have reached alignment, we can copy the rest of the
17322 rows. */
17323 if (IT_CHARPOS (it) == CHARPOS (start)
17324 /* Don't accept "alignment" inside a display vector,
17325 since start_row could have started in the middle of
17326 that same display vector (thus their character
17327 positions match), and we have no way of telling if
17328 that is the case. */
17329 && it.current.dpvec_index < 0)
17330 break;
17331
17332 it.glyph_row->reversed_p = false;
17333 if (display_line (&it))
17334 last_text_row = it.glyph_row - 1;
17335
17336 }
17337
17338 /* A value of current_y < last_visible_y means that we stopped
17339 at the previous window start, which in turn means that we
17340 have at least one reusable row. */
17341 if (it.current_y < it.last_visible_y)
17342 {
17343 struct glyph_row *row;
17344
17345 /* IT.vpos always starts from 0; it counts text lines. */
17346 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17347
17348 /* Find PT if not already found in the lines displayed. */
17349 if (w->cursor.vpos < 0)
17350 {
17351 int dy = it.current_y - start_row->y;
17352
17353 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17354 row = row_containing_pos (w, PT, row, NULL, dy);
17355 if (row)
17356 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17357 dy, nrows_scrolled);
17358 else
17359 {
17360 clear_glyph_matrix (w->desired_matrix);
17361 return false;
17362 }
17363 }
17364
17365 /* Scroll the display. Do it before the current matrix is
17366 changed. The problem here is that update has not yet
17367 run, i.e. part of the current matrix is not up to date.
17368 scroll_run_hook will clear the cursor, and use the
17369 current matrix to get the height of the row the cursor is
17370 in. */
17371 run.current_y = start_row->y;
17372 run.desired_y = it.current_y;
17373 run.height = it.last_visible_y - it.current_y;
17374
17375 if (run.height > 0 && run.current_y != run.desired_y)
17376 {
17377 update_begin (f);
17378 FRAME_RIF (f)->update_window_begin_hook (w);
17379 FRAME_RIF (f)->clear_window_mouse_face (w);
17380 FRAME_RIF (f)->scroll_run_hook (w, &run);
17381 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17382 update_end (f);
17383 }
17384
17385 /* Shift current matrix down by nrows_scrolled lines. */
17386 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17387 rotate_matrix (w->current_matrix,
17388 start_vpos,
17389 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17390 nrows_scrolled);
17391
17392 /* Disable lines that must be updated. */
17393 for (i = 0; i < nrows_scrolled; ++i)
17394 (start_row + i)->enabled_p = false;
17395
17396 /* Re-compute Y positions. */
17397 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17398 max_y = it.last_visible_y;
17399 for (row = start_row + nrows_scrolled;
17400 row < bottom_row;
17401 ++row)
17402 {
17403 row->y = it.current_y;
17404 row->visible_height = row->height;
17405
17406 if (row->y < min_y)
17407 row->visible_height -= min_y - row->y;
17408 if (row->y + row->height > max_y)
17409 row->visible_height -= row->y + row->height - max_y;
17410 if (row->fringe_bitmap_periodic_p)
17411 row->redraw_fringe_bitmaps_p = true;
17412
17413 it.current_y += row->height;
17414
17415 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17416 last_reused_text_row = row;
17417 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17418 break;
17419 }
17420
17421 /* Disable lines in the current matrix which are now
17422 below the window. */
17423 for (++row; row < bottom_row; ++row)
17424 row->enabled_p = row->mode_line_p = false;
17425 }
17426
17427 /* Update window_end_pos etc.; last_reused_text_row is the last
17428 reused row from the current matrix containing text, if any.
17429 The value of last_text_row is the last displayed line
17430 containing text. */
17431 if (last_reused_text_row)
17432 adjust_window_ends (w, last_reused_text_row, true);
17433 else if (last_text_row)
17434 adjust_window_ends (w, last_text_row, false);
17435 else
17436 {
17437 /* This window must be completely empty. */
17438 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17439 w->window_end_pos = Z - ZV;
17440 w->window_end_vpos = 0;
17441 }
17442 w->window_end_valid = false;
17443
17444 /* Update hint: don't try scrolling again in update_window. */
17445 w->desired_matrix->no_scrolling_p = true;
17446
17447 #ifdef GLYPH_DEBUG
17448 debug_method_add (w, "try_window_reusing_current_matrix 1");
17449 #endif
17450 return true;
17451 }
17452 else if (CHARPOS (new_start) > CHARPOS (start))
17453 {
17454 struct glyph_row *pt_row, *row;
17455 struct glyph_row *first_reusable_row;
17456 struct glyph_row *first_row_to_display;
17457 int dy;
17458 int yb = window_text_bottom_y (w);
17459
17460 /* Find the row starting at new_start, if there is one. Don't
17461 reuse a partially visible line at the end. */
17462 first_reusable_row = start_row;
17463 while (first_reusable_row->enabled_p
17464 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17465 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17466 < CHARPOS (new_start)))
17467 ++first_reusable_row;
17468
17469 /* Give up if there is no row to reuse. */
17470 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17471 || !first_reusable_row->enabled_p
17472 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17473 != CHARPOS (new_start)))
17474 return false;
17475
17476 /* We can reuse fully visible rows beginning with
17477 first_reusable_row to the end of the window. Set
17478 first_row_to_display to the first row that cannot be reused.
17479 Set pt_row to the row containing point, if there is any. */
17480 pt_row = NULL;
17481 for (first_row_to_display = first_reusable_row;
17482 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17483 ++first_row_to_display)
17484 {
17485 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17486 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17487 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17488 && first_row_to_display->ends_at_zv_p
17489 && pt_row == NULL)))
17490 pt_row = first_row_to_display;
17491 }
17492
17493 /* Start displaying at the start of first_row_to_display. */
17494 eassert (first_row_to_display->y < yb);
17495 init_to_row_start (&it, w, first_row_to_display);
17496
17497 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17498 - start_vpos);
17499 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17500 - nrows_scrolled);
17501 it.current_y = (first_row_to_display->y - first_reusable_row->y
17502 + WINDOW_HEADER_LINE_HEIGHT (w));
17503
17504 /* Display lines beginning with first_row_to_display in the
17505 desired matrix. Set last_text_row to the last row displayed
17506 that displays text. */
17507 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17508 if (pt_row == NULL)
17509 w->cursor.vpos = -1;
17510 last_text_row = NULL;
17511 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17512 if (display_line (&it))
17513 last_text_row = it.glyph_row - 1;
17514
17515 /* If point is in a reused row, adjust y and vpos of the cursor
17516 position. */
17517 if (pt_row)
17518 {
17519 w->cursor.vpos -= nrows_scrolled;
17520 w->cursor.y -= first_reusable_row->y - start_row->y;
17521 }
17522
17523 /* Give up if point isn't in a row displayed or reused. (This
17524 also handles the case where w->cursor.vpos < nrows_scrolled
17525 after the calls to display_line, which can happen with scroll
17526 margins. See bug#1295.) */
17527 if (w->cursor.vpos < 0)
17528 {
17529 clear_glyph_matrix (w->desired_matrix);
17530 return false;
17531 }
17532
17533 /* Scroll the display. */
17534 run.current_y = first_reusable_row->y;
17535 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17536 run.height = it.last_visible_y - run.current_y;
17537 dy = run.current_y - run.desired_y;
17538
17539 if (run.height)
17540 {
17541 update_begin (f);
17542 FRAME_RIF (f)->update_window_begin_hook (w);
17543 FRAME_RIF (f)->clear_window_mouse_face (w);
17544 FRAME_RIF (f)->scroll_run_hook (w, &run);
17545 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17546 update_end (f);
17547 }
17548
17549 /* Adjust Y positions of reused rows. */
17550 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17551 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17552 max_y = it.last_visible_y;
17553 for (row = first_reusable_row; row < first_row_to_display; ++row)
17554 {
17555 row->y -= dy;
17556 row->visible_height = row->height;
17557 if (row->y < min_y)
17558 row->visible_height -= min_y - row->y;
17559 if (row->y + row->height > max_y)
17560 row->visible_height -= row->y + row->height - max_y;
17561 if (row->fringe_bitmap_periodic_p)
17562 row->redraw_fringe_bitmaps_p = true;
17563 }
17564
17565 /* Scroll the current matrix. */
17566 eassert (nrows_scrolled > 0);
17567 rotate_matrix (w->current_matrix,
17568 start_vpos,
17569 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17570 -nrows_scrolled);
17571
17572 /* Disable rows not reused. */
17573 for (row -= nrows_scrolled; row < bottom_row; ++row)
17574 row->enabled_p = false;
17575
17576 /* Point may have moved to a different line, so we cannot assume that
17577 the previous cursor position is valid; locate the correct row. */
17578 if (pt_row)
17579 {
17580 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17581 row < bottom_row
17582 && PT >= MATRIX_ROW_END_CHARPOS (row)
17583 && !row->ends_at_zv_p;
17584 row++)
17585 {
17586 w->cursor.vpos++;
17587 w->cursor.y = row->y;
17588 }
17589 if (row < bottom_row)
17590 {
17591 /* Can't simply scan the row for point with
17592 bidi-reordered glyph rows. Let set_cursor_from_row
17593 figure out where to put the cursor, and if it fails,
17594 give up. */
17595 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17596 {
17597 if (!set_cursor_from_row (w, row, w->current_matrix,
17598 0, 0, 0, 0))
17599 {
17600 clear_glyph_matrix (w->desired_matrix);
17601 return false;
17602 }
17603 }
17604 else
17605 {
17606 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17607 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17608
17609 for (; glyph < end
17610 && (!BUFFERP (glyph->object)
17611 || glyph->charpos < PT);
17612 glyph++)
17613 {
17614 w->cursor.hpos++;
17615 w->cursor.x += glyph->pixel_width;
17616 }
17617 }
17618 }
17619 }
17620
17621 /* Adjust window end. A null value of last_text_row means that
17622 the window end is in reused rows which in turn means that
17623 only its vpos can have changed. */
17624 if (last_text_row)
17625 adjust_window_ends (w, last_text_row, false);
17626 else
17627 w->window_end_vpos -= nrows_scrolled;
17628
17629 w->window_end_valid = false;
17630 w->desired_matrix->no_scrolling_p = true;
17631
17632 #ifdef GLYPH_DEBUG
17633 debug_method_add (w, "try_window_reusing_current_matrix 2");
17634 #endif
17635 return true;
17636 }
17637
17638 return false;
17639 }
17640
17641
17642 \f
17643 /************************************************************************
17644 Window redisplay reusing current matrix when buffer has changed
17645 ************************************************************************/
17646
17647 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17648 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17649 ptrdiff_t *, ptrdiff_t *);
17650 static struct glyph_row *
17651 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17652 struct glyph_row *);
17653
17654
17655 /* Return the last row in MATRIX displaying text. If row START is
17656 non-null, start searching with that row. IT gives the dimensions
17657 of the display. Value is null if matrix is empty; otherwise it is
17658 a pointer to the row found. */
17659
17660 static struct glyph_row *
17661 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17662 struct glyph_row *start)
17663 {
17664 struct glyph_row *row, *row_found;
17665
17666 /* Set row_found to the last row in IT->w's current matrix
17667 displaying text. The loop looks funny but think of partially
17668 visible lines. */
17669 row_found = NULL;
17670 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17671 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17672 {
17673 eassert (row->enabled_p);
17674 row_found = row;
17675 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17676 break;
17677 ++row;
17678 }
17679
17680 return row_found;
17681 }
17682
17683
17684 /* Return the last row in the current matrix of W that is not affected
17685 by changes at the start of current_buffer that occurred since W's
17686 current matrix was built. Value is null if no such row exists.
17687
17688 BEG_UNCHANGED us the number of characters unchanged at the start of
17689 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17690 first changed character in current_buffer. Characters at positions <
17691 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17692 when the current matrix was built. */
17693
17694 static struct glyph_row *
17695 find_last_unchanged_at_beg_row (struct window *w)
17696 {
17697 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17698 struct glyph_row *row;
17699 struct glyph_row *row_found = NULL;
17700 int yb = window_text_bottom_y (w);
17701
17702 /* Find the last row displaying unchanged text. */
17703 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17704 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17705 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17706 ++row)
17707 {
17708 if (/* If row ends before first_changed_pos, it is unchanged,
17709 except in some case. */
17710 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17711 /* When row ends in ZV and we write at ZV it is not
17712 unchanged. */
17713 && !row->ends_at_zv_p
17714 /* When first_changed_pos is the end of a continued line,
17715 row is not unchanged because it may be no longer
17716 continued. */
17717 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17718 && (row->continued_p
17719 || row->exact_window_width_line_p))
17720 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17721 needs to be recomputed, so don't consider this row as
17722 unchanged. This happens when the last line was
17723 bidi-reordered and was killed immediately before this
17724 redisplay cycle. In that case, ROW->end stores the
17725 buffer position of the first visual-order character of
17726 the killed text, which is now beyond ZV. */
17727 && CHARPOS (row->end.pos) <= ZV)
17728 row_found = row;
17729
17730 /* Stop if last visible row. */
17731 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17732 break;
17733 }
17734
17735 return row_found;
17736 }
17737
17738
17739 /* Find the first glyph row in the current matrix of W that is not
17740 affected by changes at the end of current_buffer since the
17741 time W's current matrix was built.
17742
17743 Return in *DELTA the number of chars by which buffer positions in
17744 unchanged text at the end of current_buffer must be adjusted.
17745
17746 Return in *DELTA_BYTES the corresponding number of bytes.
17747
17748 Value is null if no such row exists, i.e. all rows are affected by
17749 changes. */
17750
17751 static struct glyph_row *
17752 find_first_unchanged_at_end_row (struct window *w,
17753 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17754 {
17755 struct glyph_row *row;
17756 struct glyph_row *row_found = NULL;
17757
17758 *delta = *delta_bytes = 0;
17759
17760 /* Display must not have been paused, otherwise the current matrix
17761 is not up to date. */
17762 eassert (w->window_end_valid);
17763
17764 /* A value of window_end_pos >= END_UNCHANGED means that the window
17765 end is in the range of changed text. If so, there is no
17766 unchanged row at the end of W's current matrix. */
17767 if (w->window_end_pos >= END_UNCHANGED)
17768 return NULL;
17769
17770 /* Set row to the last row in W's current matrix displaying text. */
17771 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17772
17773 /* If matrix is entirely empty, no unchanged row exists. */
17774 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17775 {
17776 /* The value of row is the last glyph row in the matrix having a
17777 meaningful buffer position in it. The end position of row
17778 corresponds to window_end_pos. This allows us to translate
17779 buffer positions in the current matrix to current buffer
17780 positions for characters not in changed text. */
17781 ptrdiff_t Z_old =
17782 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17783 ptrdiff_t Z_BYTE_old =
17784 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17785 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17786 struct glyph_row *first_text_row
17787 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17788
17789 *delta = Z - Z_old;
17790 *delta_bytes = Z_BYTE - Z_BYTE_old;
17791
17792 /* Set last_unchanged_pos to the buffer position of the last
17793 character in the buffer that has not been changed. Z is the
17794 index + 1 of the last character in current_buffer, i.e. by
17795 subtracting END_UNCHANGED we get the index of the last
17796 unchanged character, and we have to add BEG to get its buffer
17797 position. */
17798 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17799 last_unchanged_pos_old = last_unchanged_pos - *delta;
17800
17801 /* Search backward from ROW for a row displaying a line that
17802 starts at a minimum position >= last_unchanged_pos_old. */
17803 for (; row > first_text_row; --row)
17804 {
17805 /* This used to abort, but it can happen.
17806 It is ok to just stop the search instead here. KFS. */
17807 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17808 break;
17809
17810 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17811 row_found = row;
17812 }
17813 }
17814
17815 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17816
17817 return row_found;
17818 }
17819
17820
17821 /* Make sure that glyph rows in the current matrix of window W
17822 reference the same glyph memory as corresponding rows in the
17823 frame's frame matrix. This function is called after scrolling W's
17824 current matrix on a terminal frame in try_window_id and
17825 try_window_reusing_current_matrix. */
17826
17827 static void
17828 sync_frame_with_window_matrix_rows (struct window *w)
17829 {
17830 struct frame *f = XFRAME (w->frame);
17831 struct glyph_row *window_row, *window_row_end, *frame_row;
17832
17833 /* Preconditions: W must be a leaf window and full-width. Its frame
17834 must have a frame matrix. */
17835 eassert (BUFFERP (w->contents));
17836 eassert (WINDOW_FULL_WIDTH_P (w));
17837 eassert (!FRAME_WINDOW_P (f));
17838
17839 /* If W is a full-width window, glyph pointers in W's current matrix
17840 have, by definition, to be the same as glyph pointers in the
17841 corresponding frame matrix. Note that frame matrices have no
17842 marginal areas (see build_frame_matrix). */
17843 window_row = w->current_matrix->rows;
17844 window_row_end = window_row + w->current_matrix->nrows;
17845 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17846 while (window_row < window_row_end)
17847 {
17848 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17849 struct glyph *end = window_row->glyphs[LAST_AREA];
17850
17851 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17852 frame_row->glyphs[TEXT_AREA] = start;
17853 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17854 frame_row->glyphs[LAST_AREA] = end;
17855
17856 /* Disable frame rows whose corresponding window rows have
17857 been disabled in try_window_id. */
17858 if (!window_row->enabled_p)
17859 frame_row->enabled_p = false;
17860
17861 ++window_row, ++frame_row;
17862 }
17863 }
17864
17865
17866 /* Find the glyph row in window W containing CHARPOS. Consider all
17867 rows between START and END (not inclusive). END null means search
17868 all rows to the end of the display area of W. Value is the row
17869 containing CHARPOS or null. */
17870
17871 struct glyph_row *
17872 row_containing_pos (struct window *w, ptrdiff_t charpos,
17873 struct glyph_row *start, struct glyph_row *end, int dy)
17874 {
17875 struct glyph_row *row = start;
17876 struct glyph_row *best_row = NULL;
17877 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17878 int last_y;
17879
17880 /* If we happen to start on a header-line, skip that. */
17881 if (row->mode_line_p)
17882 ++row;
17883
17884 if ((end && row >= end) || !row->enabled_p)
17885 return NULL;
17886
17887 last_y = window_text_bottom_y (w) - dy;
17888
17889 while (true)
17890 {
17891 /* Give up if we have gone too far. */
17892 if ((end && row >= end) || !row->enabled_p)
17893 return NULL;
17894 /* This formerly returned if they were equal.
17895 I think that both quantities are of a "last plus one" type;
17896 if so, when they are equal, the row is within the screen. -- rms. */
17897 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17898 return NULL;
17899
17900 /* If it is in this row, return this row. */
17901 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17902 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17903 /* The end position of a row equals the start
17904 position of the next row. If CHARPOS is there, we
17905 would rather consider it displayed in the next
17906 line, except when this line ends in ZV. */
17907 && !row_for_charpos_p (row, charpos)))
17908 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17909 {
17910 struct glyph *g;
17911
17912 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17913 || (!best_row && !row->continued_p))
17914 return row;
17915 /* In bidi-reordered rows, there could be several rows whose
17916 edges surround CHARPOS, all of these rows belonging to
17917 the same continued line. We need to find the row which
17918 fits CHARPOS the best. */
17919 for (g = row->glyphs[TEXT_AREA];
17920 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17921 g++)
17922 {
17923 if (!STRINGP (g->object))
17924 {
17925 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17926 {
17927 mindif = eabs (g->charpos - charpos);
17928 best_row = row;
17929 /* Exact match always wins. */
17930 if (mindif == 0)
17931 return best_row;
17932 }
17933 }
17934 }
17935 }
17936 else if (best_row && !row->continued_p)
17937 return best_row;
17938 ++row;
17939 }
17940 }
17941
17942
17943 /* Try to redisplay window W by reusing its existing display. W's
17944 current matrix must be up to date when this function is called,
17945 i.e., window_end_valid must be true.
17946
17947 Value is
17948
17949 >= 1 if successful, i.e. display has been updated
17950 specifically:
17951 1 means the changes were in front of a newline that precedes
17952 the window start, and the whole current matrix was reused
17953 2 means the changes were after the last position displayed
17954 in the window, and the whole current matrix was reused
17955 3 means portions of the current matrix were reused, while
17956 some of the screen lines were redrawn
17957 -1 if redisplay with same window start is known not to succeed
17958 0 if otherwise unsuccessful
17959
17960 The following steps are performed:
17961
17962 1. Find the last row in the current matrix of W that is not
17963 affected by changes at the start of current_buffer. If no such row
17964 is found, give up.
17965
17966 2. Find the first row in W's current matrix that is not affected by
17967 changes at the end of current_buffer. Maybe there is no such row.
17968
17969 3. Display lines beginning with the row + 1 found in step 1 to the
17970 row found in step 2 or, if step 2 didn't find a row, to the end of
17971 the window.
17972
17973 4. If cursor is not known to appear on the window, give up.
17974
17975 5. If display stopped at the row found in step 2, scroll the
17976 display and current matrix as needed.
17977
17978 6. Maybe display some lines at the end of W, if we must. This can
17979 happen under various circumstances, like a partially visible line
17980 becoming fully visible, or because newly displayed lines are displayed
17981 in smaller font sizes.
17982
17983 7. Update W's window end information. */
17984
17985 static int
17986 try_window_id (struct window *w)
17987 {
17988 struct frame *f = XFRAME (w->frame);
17989 struct glyph_matrix *current_matrix = w->current_matrix;
17990 struct glyph_matrix *desired_matrix = w->desired_matrix;
17991 struct glyph_row *last_unchanged_at_beg_row;
17992 struct glyph_row *first_unchanged_at_end_row;
17993 struct glyph_row *row;
17994 struct glyph_row *bottom_row;
17995 int bottom_vpos;
17996 struct it it;
17997 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17998 int dvpos, dy;
17999 struct text_pos start_pos;
18000 struct run run;
18001 int first_unchanged_at_end_vpos = 0;
18002 struct glyph_row *last_text_row, *last_text_row_at_end;
18003 struct text_pos start;
18004 ptrdiff_t first_changed_charpos, last_changed_charpos;
18005
18006 #ifdef GLYPH_DEBUG
18007 if (inhibit_try_window_id)
18008 return 0;
18009 #endif
18010
18011 /* This is handy for debugging. */
18012 #if false
18013 #define GIVE_UP(X) \
18014 do { \
18015 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18016 return 0; \
18017 } while (false)
18018 #else
18019 #define GIVE_UP(X) return 0
18020 #endif
18021
18022 SET_TEXT_POS_FROM_MARKER (start, w->start);
18023
18024 /* Don't use this for mini-windows because these can show
18025 messages and mini-buffers, and we don't handle that here. */
18026 if (MINI_WINDOW_P (w))
18027 GIVE_UP (1);
18028
18029 /* This flag is used to prevent redisplay optimizations. */
18030 if (windows_or_buffers_changed || f->cursor_type_changed)
18031 GIVE_UP (2);
18032
18033 /* This function's optimizations cannot be used if overlays have
18034 changed in the buffer displayed by the window, so give up if they
18035 have. */
18036 if (w->last_overlay_modified != OVERLAY_MODIFF)
18037 GIVE_UP (200);
18038
18039 /* Verify that narrowing has not changed.
18040 Also verify that we were not told to prevent redisplay optimizations.
18041 It would be nice to further
18042 reduce the number of cases where this prevents try_window_id. */
18043 if (current_buffer->clip_changed
18044 || current_buffer->prevent_redisplay_optimizations_p)
18045 GIVE_UP (3);
18046
18047 /* Window must either use window-based redisplay or be full width. */
18048 if (!FRAME_WINDOW_P (f)
18049 && (!FRAME_LINE_INS_DEL_OK (f)
18050 || !WINDOW_FULL_WIDTH_P (w)))
18051 GIVE_UP (4);
18052
18053 /* Give up if point is known NOT to appear in W. */
18054 if (PT < CHARPOS (start))
18055 GIVE_UP (5);
18056
18057 /* Another way to prevent redisplay optimizations. */
18058 if (w->last_modified == 0)
18059 GIVE_UP (6);
18060
18061 /* Verify that window is not hscrolled. */
18062 if (w->hscroll != 0)
18063 GIVE_UP (7);
18064
18065 /* Verify that display wasn't paused. */
18066 if (!w->window_end_valid)
18067 GIVE_UP (8);
18068
18069 /* Likewise if highlighting trailing whitespace. */
18070 if (!NILP (Vshow_trailing_whitespace))
18071 GIVE_UP (11);
18072
18073 /* Can't use this if overlay arrow position and/or string have
18074 changed. */
18075 if (overlay_arrows_changed_p ())
18076 GIVE_UP (12);
18077
18078 /* When word-wrap is on, adding a space to the first word of a
18079 wrapped line can change the wrap position, altering the line
18080 above it. It might be worthwhile to handle this more
18081 intelligently, but for now just redisplay from scratch. */
18082 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18083 GIVE_UP (21);
18084
18085 /* Under bidi reordering, adding or deleting a character in the
18086 beginning of a paragraph, before the first strong directional
18087 character, can change the base direction of the paragraph (unless
18088 the buffer specifies a fixed paragraph direction), which will
18089 require to redisplay the whole paragraph. It might be worthwhile
18090 to find the paragraph limits and widen the range of redisplayed
18091 lines to that, but for now just give up this optimization and
18092 redisplay from scratch. */
18093 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18094 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18095 GIVE_UP (22);
18096
18097 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18098 to that variable require thorough redisplay. */
18099 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18100 GIVE_UP (23);
18101
18102 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18103 only if buffer has really changed. The reason is that the gap is
18104 initially at Z for freshly visited files. The code below would
18105 set end_unchanged to 0 in that case. */
18106 if (MODIFF > SAVE_MODIFF
18107 /* This seems to happen sometimes after saving a buffer. */
18108 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18109 {
18110 if (GPT - BEG < BEG_UNCHANGED)
18111 BEG_UNCHANGED = GPT - BEG;
18112 if (Z - GPT < END_UNCHANGED)
18113 END_UNCHANGED = Z - GPT;
18114 }
18115
18116 /* The position of the first and last character that has been changed. */
18117 first_changed_charpos = BEG + BEG_UNCHANGED;
18118 last_changed_charpos = Z - END_UNCHANGED;
18119
18120 /* If window starts after a line end, and the last change is in
18121 front of that newline, then changes don't affect the display.
18122 This case happens with stealth-fontification. Note that although
18123 the display is unchanged, glyph positions in the matrix have to
18124 be adjusted, of course. */
18125 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18126 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18127 && ((last_changed_charpos < CHARPOS (start)
18128 && CHARPOS (start) == BEGV)
18129 || (last_changed_charpos < CHARPOS (start) - 1
18130 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18131 {
18132 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18133 struct glyph_row *r0;
18134
18135 /* Compute how many chars/bytes have been added to or removed
18136 from the buffer. */
18137 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18138 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18139 Z_delta = Z - Z_old;
18140 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18141
18142 /* Give up if PT is not in the window. Note that it already has
18143 been checked at the start of try_window_id that PT is not in
18144 front of the window start. */
18145 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18146 GIVE_UP (13);
18147
18148 /* If window start is unchanged, we can reuse the whole matrix
18149 as is, after adjusting glyph positions. No need to compute
18150 the window end again, since its offset from Z hasn't changed. */
18151 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18152 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18153 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18154 /* PT must not be in a partially visible line. */
18155 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18156 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18157 {
18158 /* Adjust positions in the glyph matrix. */
18159 if (Z_delta || Z_delta_bytes)
18160 {
18161 struct glyph_row *r1
18162 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18163 increment_matrix_positions (w->current_matrix,
18164 MATRIX_ROW_VPOS (r0, current_matrix),
18165 MATRIX_ROW_VPOS (r1, current_matrix),
18166 Z_delta, Z_delta_bytes);
18167 }
18168
18169 /* Set the cursor. */
18170 row = row_containing_pos (w, PT, r0, NULL, 0);
18171 if (row)
18172 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18173 return 1;
18174 }
18175 }
18176
18177 /* Handle the case that changes are all below what is displayed in
18178 the window, and that PT is in the window. This shortcut cannot
18179 be taken if ZV is visible in the window, and text has been added
18180 there that is visible in the window. */
18181 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18182 /* ZV is not visible in the window, or there are no
18183 changes at ZV, actually. */
18184 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18185 || first_changed_charpos == last_changed_charpos))
18186 {
18187 struct glyph_row *r0;
18188
18189 /* Give up if PT is not in the window. Note that it already has
18190 been checked at the start of try_window_id that PT is not in
18191 front of the window start. */
18192 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18193 GIVE_UP (14);
18194
18195 /* If window start is unchanged, we can reuse the whole matrix
18196 as is, without changing glyph positions since no text has
18197 been added/removed in front of the window end. */
18198 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18199 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18200 /* PT must not be in a partially visible line. */
18201 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18202 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18203 {
18204 /* We have to compute the window end anew since text
18205 could have been added/removed after it. */
18206 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18207 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18208
18209 /* Set the cursor. */
18210 row = row_containing_pos (w, PT, r0, NULL, 0);
18211 if (row)
18212 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18213 return 2;
18214 }
18215 }
18216
18217 /* Give up if window start is in the changed area.
18218
18219 The condition used to read
18220
18221 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18222
18223 but why that was tested escapes me at the moment. */
18224 if (CHARPOS (start) >= first_changed_charpos
18225 && CHARPOS (start) <= last_changed_charpos)
18226 GIVE_UP (15);
18227
18228 /* Check that window start agrees with the start of the first glyph
18229 row in its current matrix. Check this after we know the window
18230 start is not in changed text, otherwise positions would not be
18231 comparable. */
18232 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18233 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18234 GIVE_UP (16);
18235
18236 /* Give up if the window ends in strings. Overlay strings
18237 at the end are difficult to handle, so don't try. */
18238 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18239 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18240 GIVE_UP (20);
18241
18242 /* Compute the position at which we have to start displaying new
18243 lines. Some of the lines at the top of the window might be
18244 reusable because they are not displaying changed text. Find the
18245 last row in W's current matrix not affected by changes at the
18246 start of current_buffer. Value is null if changes start in the
18247 first line of window. */
18248 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18249 if (last_unchanged_at_beg_row)
18250 {
18251 /* Avoid starting to display in the middle of a character, a TAB
18252 for instance. This is easier than to set up the iterator
18253 exactly, and it's not a frequent case, so the additional
18254 effort wouldn't really pay off. */
18255 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18256 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18257 && last_unchanged_at_beg_row > w->current_matrix->rows)
18258 --last_unchanged_at_beg_row;
18259
18260 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18261 GIVE_UP (17);
18262
18263 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18264 GIVE_UP (18);
18265 start_pos = it.current.pos;
18266
18267 /* Start displaying new lines in the desired matrix at the same
18268 vpos we would use in the current matrix, i.e. below
18269 last_unchanged_at_beg_row. */
18270 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18271 current_matrix);
18272 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18273 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18274
18275 eassert (it.hpos == 0 && it.current_x == 0);
18276 }
18277 else
18278 {
18279 /* There are no reusable lines at the start of the window.
18280 Start displaying in the first text line. */
18281 start_display (&it, w, start);
18282 it.vpos = it.first_vpos;
18283 start_pos = it.current.pos;
18284 }
18285
18286 /* Find the first row that is not affected by changes at the end of
18287 the buffer. Value will be null if there is no unchanged row, in
18288 which case we must redisplay to the end of the window. delta
18289 will be set to the value by which buffer positions beginning with
18290 first_unchanged_at_end_row have to be adjusted due to text
18291 changes. */
18292 first_unchanged_at_end_row
18293 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18294 IF_DEBUG (debug_delta = delta);
18295 IF_DEBUG (debug_delta_bytes = delta_bytes);
18296
18297 /* Set stop_pos to the buffer position up to which we will have to
18298 display new lines. If first_unchanged_at_end_row != NULL, this
18299 is the buffer position of the start of the line displayed in that
18300 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18301 that we don't stop at a buffer position. */
18302 stop_pos = 0;
18303 if (first_unchanged_at_end_row)
18304 {
18305 eassert (last_unchanged_at_beg_row == NULL
18306 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18307
18308 /* If this is a continuation line, move forward to the next one
18309 that isn't. Changes in lines above affect this line.
18310 Caution: this may move first_unchanged_at_end_row to a row
18311 not displaying text. */
18312 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18313 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18314 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18315 < it.last_visible_y))
18316 ++first_unchanged_at_end_row;
18317
18318 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18319 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18320 >= it.last_visible_y))
18321 first_unchanged_at_end_row = NULL;
18322 else
18323 {
18324 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18325 + delta);
18326 first_unchanged_at_end_vpos
18327 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18328 eassert (stop_pos >= Z - END_UNCHANGED);
18329 }
18330 }
18331 else if (last_unchanged_at_beg_row == NULL)
18332 GIVE_UP (19);
18333
18334
18335 #ifdef GLYPH_DEBUG
18336
18337 /* Either there is no unchanged row at the end, or the one we have
18338 now displays text. This is a necessary condition for the window
18339 end pos calculation at the end of this function. */
18340 eassert (first_unchanged_at_end_row == NULL
18341 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18342
18343 debug_last_unchanged_at_beg_vpos
18344 = (last_unchanged_at_beg_row
18345 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18346 : -1);
18347 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18348
18349 #endif /* GLYPH_DEBUG */
18350
18351
18352 /* Display new lines. Set last_text_row to the last new line
18353 displayed which has text on it, i.e. might end up as being the
18354 line where the window_end_vpos is. */
18355 w->cursor.vpos = -1;
18356 last_text_row = NULL;
18357 overlay_arrow_seen = false;
18358 if (it.current_y < it.last_visible_y
18359 && !f->fonts_changed
18360 && (first_unchanged_at_end_row == NULL
18361 || IT_CHARPOS (it) < stop_pos))
18362 it.glyph_row->reversed_p = false;
18363 while (it.current_y < it.last_visible_y
18364 && !f->fonts_changed
18365 && (first_unchanged_at_end_row == NULL
18366 || IT_CHARPOS (it) < stop_pos))
18367 {
18368 if (display_line (&it))
18369 last_text_row = it.glyph_row - 1;
18370 }
18371
18372 if (f->fonts_changed)
18373 return -1;
18374
18375 /* The redisplay iterations in display_line above could have
18376 triggered font-lock, which could have done something that
18377 invalidates IT->w window's end-point information, on which we
18378 rely below. E.g., one package, which will remain unnamed, used
18379 to install a font-lock-fontify-region-function that called
18380 bury-buffer, whose side effect is to switch the buffer displayed
18381 by IT->w, and that predictably resets IT->w's window_end_valid
18382 flag, which we already tested at the entry to this function.
18383 Amply punish such packages/modes by giving up on this
18384 optimization in those cases. */
18385 if (!w->window_end_valid)
18386 {
18387 clear_glyph_matrix (w->desired_matrix);
18388 return -1;
18389 }
18390
18391 /* Compute differences in buffer positions, y-positions etc. for
18392 lines reused at the bottom of the window. Compute what we can
18393 scroll. */
18394 if (first_unchanged_at_end_row
18395 /* No lines reused because we displayed everything up to the
18396 bottom of the window. */
18397 && it.current_y < it.last_visible_y)
18398 {
18399 dvpos = (it.vpos
18400 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18401 current_matrix));
18402 dy = it.current_y - first_unchanged_at_end_row->y;
18403 run.current_y = first_unchanged_at_end_row->y;
18404 run.desired_y = run.current_y + dy;
18405 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18406 }
18407 else
18408 {
18409 delta = delta_bytes = dvpos = dy
18410 = run.current_y = run.desired_y = run.height = 0;
18411 first_unchanged_at_end_row = NULL;
18412 }
18413 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18414
18415
18416 /* Find the cursor if not already found. We have to decide whether
18417 PT will appear on this window (it sometimes doesn't, but this is
18418 not a very frequent case.) This decision has to be made before
18419 the current matrix is altered. A value of cursor.vpos < 0 means
18420 that PT is either in one of the lines beginning at
18421 first_unchanged_at_end_row or below the window. Don't care for
18422 lines that might be displayed later at the window end; as
18423 mentioned, this is not a frequent case. */
18424 if (w->cursor.vpos < 0)
18425 {
18426 /* Cursor in unchanged rows at the top? */
18427 if (PT < CHARPOS (start_pos)
18428 && last_unchanged_at_beg_row)
18429 {
18430 row = row_containing_pos (w, PT,
18431 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18432 last_unchanged_at_beg_row + 1, 0);
18433 if (row)
18434 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18435 }
18436
18437 /* Start from first_unchanged_at_end_row looking for PT. */
18438 else if (first_unchanged_at_end_row)
18439 {
18440 row = row_containing_pos (w, PT - delta,
18441 first_unchanged_at_end_row, NULL, 0);
18442 if (row)
18443 set_cursor_from_row (w, row, w->current_matrix, delta,
18444 delta_bytes, dy, dvpos);
18445 }
18446
18447 /* Give up if cursor was not found. */
18448 if (w->cursor.vpos < 0)
18449 {
18450 clear_glyph_matrix (w->desired_matrix);
18451 return -1;
18452 }
18453 }
18454
18455 /* Don't let the cursor end in the scroll margins. */
18456 {
18457 int this_scroll_margin, cursor_height;
18458 int frame_line_height = default_line_pixel_height (w);
18459 int window_total_lines
18460 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18461
18462 this_scroll_margin =
18463 max (0, min (scroll_margin, window_total_lines / 4));
18464 this_scroll_margin *= frame_line_height;
18465 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18466
18467 if ((w->cursor.y < this_scroll_margin
18468 && CHARPOS (start) > BEGV)
18469 /* Old redisplay didn't take scroll margin into account at the bottom,
18470 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18471 || (w->cursor.y + (make_cursor_line_fully_visible_p
18472 ? cursor_height + this_scroll_margin
18473 : 1)) > it.last_visible_y)
18474 {
18475 w->cursor.vpos = -1;
18476 clear_glyph_matrix (w->desired_matrix);
18477 return -1;
18478 }
18479 }
18480
18481 /* Scroll the display. Do it before changing the current matrix so
18482 that xterm.c doesn't get confused about where the cursor glyph is
18483 found. */
18484 if (dy && run.height)
18485 {
18486 update_begin (f);
18487
18488 if (FRAME_WINDOW_P (f))
18489 {
18490 FRAME_RIF (f)->update_window_begin_hook (w);
18491 FRAME_RIF (f)->clear_window_mouse_face (w);
18492 FRAME_RIF (f)->scroll_run_hook (w, &run);
18493 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18494 }
18495 else
18496 {
18497 /* Terminal frame. In this case, dvpos gives the number of
18498 lines to scroll by; dvpos < 0 means scroll up. */
18499 int from_vpos
18500 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18501 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18502 int end = (WINDOW_TOP_EDGE_LINE (w)
18503 + WINDOW_WANTS_HEADER_LINE_P (w)
18504 + window_internal_height (w));
18505
18506 #if defined (HAVE_GPM) || defined (MSDOS)
18507 x_clear_window_mouse_face (w);
18508 #endif
18509 /* Perform the operation on the screen. */
18510 if (dvpos > 0)
18511 {
18512 /* Scroll last_unchanged_at_beg_row to the end of the
18513 window down dvpos lines. */
18514 set_terminal_window (f, end);
18515
18516 /* On dumb terminals delete dvpos lines at the end
18517 before inserting dvpos empty lines. */
18518 if (!FRAME_SCROLL_REGION_OK (f))
18519 ins_del_lines (f, end - dvpos, -dvpos);
18520
18521 /* Insert dvpos empty lines in front of
18522 last_unchanged_at_beg_row. */
18523 ins_del_lines (f, from, dvpos);
18524 }
18525 else if (dvpos < 0)
18526 {
18527 /* Scroll up last_unchanged_at_beg_vpos to the end of
18528 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18529 set_terminal_window (f, end);
18530
18531 /* Delete dvpos lines in front of
18532 last_unchanged_at_beg_vpos. ins_del_lines will set
18533 the cursor to the given vpos and emit |dvpos| delete
18534 line sequences. */
18535 ins_del_lines (f, from + dvpos, dvpos);
18536
18537 /* On a dumb terminal insert dvpos empty lines at the
18538 end. */
18539 if (!FRAME_SCROLL_REGION_OK (f))
18540 ins_del_lines (f, end + dvpos, -dvpos);
18541 }
18542
18543 set_terminal_window (f, 0);
18544 }
18545
18546 update_end (f);
18547 }
18548
18549 /* Shift reused rows of the current matrix to the right position.
18550 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18551 text. */
18552 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18553 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18554 if (dvpos < 0)
18555 {
18556 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18557 bottom_vpos, dvpos);
18558 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18559 bottom_vpos);
18560 }
18561 else if (dvpos > 0)
18562 {
18563 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18564 bottom_vpos, dvpos);
18565 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18566 first_unchanged_at_end_vpos + dvpos);
18567 }
18568
18569 /* For frame-based redisplay, make sure that current frame and window
18570 matrix are in sync with respect to glyph memory. */
18571 if (!FRAME_WINDOW_P (f))
18572 sync_frame_with_window_matrix_rows (w);
18573
18574 /* Adjust buffer positions in reused rows. */
18575 if (delta || delta_bytes)
18576 increment_matrix_positions (current_matrix,
18577 first_unchanged_at_end_vpos + dvpos,
18578 bottom_vpos, delta, delta_bytes);
18579
18580 /* Adjust Y positions. */
18581 if (dy)
18582 shift_glyph_matrix (w, current_matrix,
18583 first_unchanged_at_end_vpos + dvpos,
18584 bottom_vpos, dy);
18585
18586 if (first_unchanged_at_end_row)
18587 {
18588 first_unchanged_at_end_row += dvpos;
18589 if (first_unchanged_at_end_row->y >= it.last_visible_y
18590 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18591 first_unchanged_at_end_row = NULL;
18592 }
18593
18594 /* If scrolling up, there may be some lines to display at the end of
18595 the window. */
18596 last_text_row_at_end = NULL;
18597 if (dy < 0)
18598 {
18599 /* Scrolling up can leave for example a partially visible line
18600 at the end of the window to be redisplayed. */
18601 /* Set last_row to the glyph row in the current matrix where the
18602 window end line is found. It has been moved up or down in
18603 the matrix by dvpos. */
18604 int last_vpos = w->window_end_vpos + dvpos;
18605 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18606
18607 /* If last_row is the window end line, it should display text. */
18608 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18609
18610 /* If window end line was partially visible before, begin
18611 displaying at that line. Otherwise begin displaying with the
18612 line following it. */
18613 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18614 {
18615 init_to_row_start (&it, w, last_row);
18616 it.vpos = last_vpos;
18617 it.current_y = last_row->y;
18618 }
18619 else
18620 {
18621 init_to_row_end (&it, w, last_row);
18622 it.vpos = 1 + last_vpos;
18623 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18624 ++last_row;
18625 }
18626
18627 /* We may start in a continuation line. If so, we have to
18628 get the right continuation_lines_width and current_x. */
18629 it.continuation_lines_width = last_row->continuation_lines_width;
18630 it.hpos = it.current_x = 0;
18631
18632 /* Display the rest of the lines at the window end. */
18633 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18634 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18635 {
18636 /* Is it always sure that the display agrees with lines in
18637 the current matrix? I don't think so, so we mark rows
18638 displayed invalid in the current matrix by setting their
18639 enabled_p flag to false. */
18640 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18641 if (display_line (&it))
18642 last_text_row_at_end = it.glyph_row - 1;
18643 }
18644 }
18645
18646 /* Update window_end_pos and window_end_vpos. */
18647 if (first_unchanged_at_end_row && !last_text_row_at_end)
18648 {
18649 /* Window end line if one of the preserved rows from the current
18650 matrix. Set row to the last row displaying text in current
18651 matrix starting at first_unchanged_at_end_row, after
18652 scrolling. */
18653 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18654 row = find_last_row_displaying_text (w->current_matrix, &it,
18655 first_unchanged_at_end_row);
18656 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18657 adjust_window_ends (w, row, true);
18658 eassert (w->window_end_bytepos >= 0);
18659 IF_DEBUG (debug_method_add (w, "A"));
18660 }
18661 else if (last_text_row_at_end)
18662 {
18663 adjust_window_ends (w, last_text_row_at_end, false);
18664 eassert (w->window_end_bytepos >= 0);
18665 IF_DEBUG (debug_method_add (w, "B"));
18666 }
18667 else if (last_text_row)
18668 {
18669 /* We have displayed either to the end of the window or at the
18670 end of the window, i.e. the last row with text is to be found
18671 in the desired matrix. */
18672 adjust_window_ends (w, last_text_row, false);
18673 eassert (w->window_end_bytepos >= 0);
18674 }
18675 else if (first_unchanged_at_end_row == NULL
18676 && last_text_row == NULL
18677 && last_text_row_at_end == NULL)
18678 {
18679 /* Displayed to end of window, but no line containing text was
18680 displayed. Lines were deleted at the end of the window. */
18681 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18682 int vpos = w->window_end_vpos;
18683 struct glyph_row *current_row = current_matrix->rows + vpos;
18684 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18685
18686 for (row = NULL;
18687 row == NULL && vpos >= first_vpos;
18688 --vpos, --current_row, --desired_row)
18689 {
18690 if (desired_row->enabled_p)
18691 {
18692 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18693 row = desired_row;
18694 }
18695 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18696 row = current_row;
18697 }
18698
18699 eassert (row != NULL);
18700 w->window_end_vpos = vpos + 1;
18701 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18702 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18703 eassert (w->window_end_bytepos >= 0);
18704 IF_DEBUG (debug_method_add (w, "C"));
18705 }
18706 else
18707 emacs_abort ();
18708
18709 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18710 debug_end_vpos = w->window_end_vpos));
18711
18712 /* Record that display has not been completed. */
18713 w->window_end_valid = false;
18714 w->desired_matrix->no_scrolling_p = true;
18715 return 3;
18716
18717 #undef GIVE_UP
18718 }
18719
18720
18721 \f
18722 /***********************************************************************
18723 More debugging support
18724 ***********************************************************************/
18725
18726 #ifdef GLYPH_DEBUG
18727
18728 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18729 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18730 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18731
18732
18733 /* Dump the contents of glyph matrix MATRIX on stderr.
18734
18735 GLYPHS 0 means don't show glyph contents.
18736 GLYPHS 1 means show glyphs in short form
18737 GLYPHS > 1 means show glyphs in long form. */
18738
18739 void
18740 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18741 {
18742 int i;
18743 for (i = 0; i < matrix->nrows; ++i)
18744 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18745 }
18746
18747
18748 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18749 the glyph row and area where the glyph comes from. */
18750
18751 void
18752 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18753 {
18754 if (glyph->type == CHAR_GLYPH
18755 || glyph->type == GLYPHLESS_GLYPH)
18756 {
18757 fprintf (stderr,
18758 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18759 glyph - row->glyphs[TEXT_AREA],
18760 (glyph->type == CHAR_GLYPH
18761 ? 'C'
18762 : 'G'),
18763 glyph->charpos,
18764 (BUFFERP (glyph->object)
18765 ? 'B'
18766 : (STRINGP (glyph->object)
18767 ? 'S'
18768 : (NILP (glyph->object)
18769 ? '0'
18770 : '-'))),
18771 glyph->pixel_width,
18772 glyph->u.ch,
18773 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18774 ? glyph->u.ch
18775 : '.'),
18776 glyph->face_id,
18777 glyph->left_box_line_p,
18778 glyph->right_box_line_p);
18779 }
18780 else if (glyph->type == STRETCH_GLYPH)
18781 {
18782 fprintf (stderr,
18783 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18784 glyph - row->glyphs[TEXT_AREA],
18785 'S',
18786 glyph->charpos,
18787 (BUFFERP (glyph->object)
18788 ? 'B'
18789 : (STRINGP (glyph->object)
18790 ? 'S'
18791 : (NILP (glyph->object)
18792 ? '0'
18793 : '-'))),
18794 glyph->pixel_width,
18795 0,
18796 ' ',
18797 glyph->face_id,
18798 glyph->left_box_line_p,
18799 glyph->right_box_line_p);
18800 }
18801 else if (glyph->type == IMAGE_GLYPH)
18802 {
18803 fprintf (stderr,
18804 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18805 glyph - row->glyphs[TEXT_AREA],
18806 'I',
18807 glyph->charpos,
18808 (BUFFERP (glyph->object)
18809 ? 'B'
18810 : (STRINGP (glyph->object)
18811 ? 'S'
18812 : (NILP (glyph->object)
18813 ? '0'
18814 : '-'))),
18815 glyph->pixel_width,
18816 glyph->u.img_id,
18817 '.',
18818 glyph->face_id,
18819 glyph->left_box_line_p,
18820 glyph->right_box_line_p);
18821 }
18822 else if (glyph->type == COMPOSITE_GLYPH)
18823 {
18824 fprintf (stderr,
18825 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18826 glyph - row->glyphs[TEXT_AREA],
18827 '+',
18828 glyph->charpos,
18829 (BUFFERP (glyph->object)
18830 ? 'B'
18831 : (STRINGP (glyph->object)
18832 ? 'S'
18833 : (NILP (glyph->object)
18834 ? '0'
18835 : '-'))),
18836 glyph->pixel_width,
18837 glyph->u.cmp.id);
18838 if (glyph->u.cmp.automatic)
18839 fprintf (stderr,
18840 "[%d-%d]",
18841 glyph->slice.cmp.from, glyph->slice.cmp.to);
18842 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18843 glyph->face_id,
18844 glyph->left_box_line_p,
18845 glyph->right_box_line_p);
18846 }
18847 #ifdef HAVE_XWIDGETS
18848 else if (glyph->type == XWIDGET_GLYPH)
18849 {
18850 fprintf (stderr,
18851 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18852 glyph - row->glyphs[TEXT_AREA],
18853 'X',
18854 glyph->charpos,
18855 (BUFFERP (glyph->object)
18856 ? 'B'
18857 : (STRINGP (glyph->object)
18858 ? 'S'
18859 : '-')),
18860 glyph->pixel_width,
18861 glyph->u.xwidget,
18862 '.',
18863 glyph->face_id,
18864 glyph->left_box_line_p,
18865 glyph->right_box_line_p);
18866
18867 }
18868 #endif
18869 }
18870
18871
18872 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18873 GLYPHS 0 means don't show glyph contents.
18874 GLYPHS 1 means show glyphs in short form
18875 GLYPHS > 1 means show glyphs in long form. */
18876
18877 void
18878 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18879 {
18880 if (glyphs != 1)
18881 {
18882 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18883 fprintf (stderr, "==============================================================================\n");
18884
18885 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18886 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18887 vpos,
18888 MATRIX_ROW_START_CHARPOS (row),
18889 MATRIX_ROW_END_CHARPOS (row),
18890 row->used[TEXT_AREA],
18891 row->contains_overlapping_glyphs_p,
18892 row->enabled_p,
18893 row->truncated_on_left_p,
18894 row->truncated_on_right_p,
18895 row->continued_p,
18896 MATRIX_ROW_CONTINUATION_LINE_P (row),
18897 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18898 row->ends_at_zv_p,
18899 row->fill_line_p,
18900 row->ends_in_middle_of_char_p,
18901 row->starts_in_middle_of_char_p,
18902 row->mouse_face_p,
18903 row->x,
18904 row->y,
18905 row->pixel_width,
18906 row->height,
18907 row->visible_height,
18908 row->ascent,
18909 row->phys_ascent);
18910 /* The next 3 lines should align to "Start" in the header. */
18911 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18912 row->end.overlay_string_index,
18913 row->continuation_lines_width);
18914 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18915 CHARPOS (row->start.string_pos),
18916 CHARPOS (row->end.string_pos));
18917 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18918 row->end.dpvec_index);
18919 }
18920
18921 if (glyphs > 1)
18922 {
18923 int area;
18924
18925 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18926 {
18927 struct glyph *glyph = row->glyphs[area];
18928 struct glyph *glyph_end = glyph + row->used[area];
18929
18930 /* Glyph for a line end in text. */
18931 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18932 ++glyph_end;
18933
18934 if (glyph < glyph_end)
18935 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18936
18937 for (; glyph < glyph_end; ++glyph)
18938 dump_glyph (row, glyph, area);
18939 }
18940 }
18941 else if (glyphs == 1)
18942 {
18943 int area;
18944 char s[SHRT_MAX + 4];
18945
18946 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18947 {
18948 int i;
18949
18950 for (i = 0; i < row->used[area]; ++i)
18951 {
18952 struct glyph *glyph = row->glyphs[area] + i;
18953 if (i == row->used[area] - 1
18954 && area == TEXT_AREA
18955 && NILP (glyph->object)
18956 && glyph->type == CHAR_GLYPH
18957 && glyph->u.ch == ' ')
18958 {
18959 strcpy (&s[i], "[\\n]");
18960 i += 4;
18961 }
18962 else if (glyph->type == CHAR_GLYPH
18963 && glyph->u.ch < 0x80
18964 && glyph->u.ch >= ' ')
18965 s[i] = glyph->u.ch;
18966 else
18967 s[i] = '.';
18968 }
18969
18970 s[i] = '\0';
18971 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18972 }
18973 }
18974 }
18975
18976
18977 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18978 Sdump_glyph_matrix, 0, 1, "p",
18979 doc: /* Dump the current matrix of the selected window to stderr.
18980 Shows contents of glyph row structures. With non-nil
18981 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18982 glyphs in short form, otherwise show glyphs in long form.
18983
18984 Interactively, no argument means show glyphs in short form;
18985 with numeric argument, its value is passed as the GLYPHS flag. */)
18986 (Lisp_Object glyphs)
18987 {
18988 struct window *w = XWINDOW (selected_window);
18989 struct buffer *buffer = XBUFFER (w->contents);
18990
18991 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18992 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18993 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18994 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18995 fprintf (stderr, "=============================================\n");
18996 dump_glyph_matrix (w->current_matrix,
18997 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18998 return Qnil;
18999 }
19000
19001
19002 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19003 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19004 Only text-mode frames have frame glyph matrices. */)
19005 (void)
19006 {
19007 struct frame *f = XFRAME (selected_frame);
19008
19009 if (f->current_matrix)
19010 dump_glyph_matrix (f->current_matrix, 1);
19011 else
19012 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19013 return Qnil;
19014 }
19015
19016
19017 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
19018 doc: /* Dump glyph row ROW to stderr.
19019 GLYPH 0 means don't dump glyphs.
19020 GLYPH 1 means dump glyphs in short form.
19021 GLYPH > 1 or omitted means dump glyphs in long form. */)
19022 (Lisp_Object row, Lisp_Object glyphs)
19023 {
19024 struct glyph_matrix *matrix;
19025 EMACS_INT vpos;
19026
19027 CHECK_NUMBER (row);
19028 matrix = XWINDOW (selected_window)->current_matrix;
19029 vpos = XINT (row);
19030 if (vpos >= 0 && vpos < matrix->nrows)
19031 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19032 vpos,
19033 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19034 return Qnil;
19035 }
19036
19037
19038 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
19039 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19040 GLYPH 0 means don't dump glyphs.
19041 GLYPH 1 means dump glyphs in short form.
19042 GLYPH > 1 or omitted means dump glyphs in long form.
19043
19044 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19045 do nothing. */)
19046 (Lisp_Object row, Lisp_Object glyphs)
19047 {
19048 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19049 struct frame *sf = SELECTED_FRAME ();
19050 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19051 EMACS_INT vpos;
19052
19053 CHECK_NUMBER (row);
19054 vpos = XINT (row);
19055 if (vpos >= 0 && vpos < m->nrows)
19056 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19057 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19058 #endif
19059 return Qnil;
19060 }
19061
19062
19063 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19064 doc: /* Toggle tracing of redisplay.
19065 With ARG, turn tracing on if and only if ARG is positive. */)
19066 (Lisp_Object arg)
19067 {
19068 if (NILP (arg))
19069 trace_redisplay_p = !trace_redisplay_p;
19070 else
19071 {
19072 arg = Fprefix_numeric_value (arg);
19073 trace_redisplay_p = XINT (arg) > 0;
19074 }
19075
19076 return Qnil;
19077 }
19078
19079
19080 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19081 doc: /* Like `format', but print result to stderr.
19082 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19083 (ptrdiff_t nargs, Lisp_Object *args)
19084 {
19085 Lisp_Object s = Fformat (nargs, args);
19086 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19087 return Qnil;
19088 }
19089
19090 #endif /* GLYPH_DEBUG */
19091
19092
19093 \f
19094 /***********************************************************************
19095 Building Desired Matrix Rows
19096 ***********************************************************************/
19097
19098 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19099 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19100
19101 static struct glyph_row *
19102 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19103 {
19104 struct frame *f = XFRAME (WINDOW_FRAME (w));
19105 struct buffer *buffer = XBUFFER (w->contents);
19106 struct buffer *old = current_buffer;
19107 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19108 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19109 const unsigned char *arrow_end = arrow_string + arrow_len;
19110 const unsigned char *p;
19111 struct it it;
19112 bool multibyte_p;
19113 int n_glyphs_before;
19114
19115 set_buffer_temp (buffer);
19116 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19117 scratch_glyph_row.reversed_p = false;
19118 it.glyph_row->used[TEXT_AREA] = 0;
19119 SET_TEXT_POS (it.position, 0, 0);
19120
19121 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19122 p = arrow_string;
19123 while (p < arrow_end)
19124 {
19125 Lisp_Object face, ilisp;
19126
19127 /* Get the next character. */
19128 if (multibyte_p)
19129 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19130 else
19131 {
19132 it.c = it.char_to_display = *p, it.len = 1;
19133 if (! ASCII_CHAR_P (it.c))
19134 it.char_to_display = BYTE8_TO_CHAR (it.c);
19135 }
19136 p += it.len;
19137
19138 /* Get its face. */
19139 ilisp = make_number (p - arrow_string);
19140 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19141 it.face_id = compute_char_face (f, it.char_to_display, face);
19142
19143 /* Compute its width, get its glyphs. */
19144 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19145 SET_TEXT_POS (it.position, -1, -1);
19146 PRODUCE_GLYPHS (&it);
19147
19148 /* If this character doesn't fit any more in the line, we have
19149 to remove some glyphs. */
19150 if (it.current_x > it.last_visible_x)
19151 {
19152 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19153 break;
19154 }
19155 }
19156
19157 set_buffer_temp (old);
19158 return it.glyph_row;
19159 }
19160
19161
19162 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19163 glyphs to insert is determined by produce_special_glyphs. */
19164
19165 static void
19166 insert_left_trunc_glyphs (struct it *it)
19167 {
19168 struct it truncate_it;
19169 struct glyph *from, *end, *to, *toend;
19170
19171 eassert (!FRAME_WINDOW_P (it->f)
19172 || (!it->glyph_row->reversed_p
19173 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19174 || (it->glyph_row->reversed_p
19175 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19176
19177 /* Get the truncation glyphs. */
19178 truncate_it = *it;
19179 truncate_it.current_x = 0;
19180 truncate_it.face_id = DEFAULT_FACE_ID;
19181 truncate_it.glyph_row = &scratch_glyph_row;
19182 truncate_it.area = TEXT_AREA;
19183 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19184 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19185 truncate_it.object = Qnil;
19186 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19187
19188 /* Overwrite glyphs from IT with truncation glyphs. */
19189 if (!it->glyph_row->reversed_p)
19190 {
19191 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19192
19193 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19194 end = from + tused;
19195 to = it->glyph_row->glyphs[TEXT_AREA];
19196 toend = to + it->glyph_row->used[TEXT_AREA];
19197 if (FRAME_WINDOW_P (it->f))
19198 {
19199 /* On GUI frames, when variable-size fonts are displayed,
19200 the truncation glyphs may need more pixels than the row's
19201 glyphs they overwrite. We overwrite more glyphs to free
19202 enough screen real estate, and enlarge the stretch glyph
19203 on the right (see display_line), if there is one, to
19204 preserve the screen position of the truncation glyphs on
19205 the right. */
19206 int w = 0;
19207 struct glyph *g = to;
19208 short used;
19209
19210 /* The first glyph could be partially visible, in which case
19211 it->glyph_row->x will be negative. But we want the left
19212 truncation glyphs to be aligned at the left margin of the
19213 window, so we override the x coordinate at which the row
19214 will begin. */
19215 it->glyph_row->x = 0;
19216 while (g < toend && w < it->truncation_pixel_width)
19217 {
19218 w += g->pixel_width;
19219 ++g;
19220 }
19221 if (g - to - tused > 0)
19222 {
19223 memmove (to + tused, g, (toend - g) * sizeof(*g));
19224 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19225 }
19226 used = it->glyph_row->used[TEXT_AREA];
19227 if (it->glyph_row->truncated_on_right_p
19228 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19229 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19230 == STRETCH_GLYPH)
19231 {
19232 int extra = w - it->truncation_pixel_width;
19233
19234 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19235 }
19236 }
19237
19238 while (from < end)
19239 *to++ = *from++;
19240
19241 /* There may be padding glyphs left over. Overwrite them too. */
19242 if (!FRAME_WINDOW_P (it->f))
19243 {
19244 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19245 {
19246 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19247 while (from < end)
19248 *to++ = *from++;
19249 }
19250 }
19251
19252 if (to > toend)
19253 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19254 }
19255 else
19256 {
19257 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19258
19259 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19260 that back to front. */
19261 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19262 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19263 toend = it->glyph_row->glyphs[TEXT_AREA];
19264 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19265 if (FRAME_WINDOW_P (it->f))
19266 {
19267 int w = 0;
19268 struct glyph *g = to;
19269
19270 while (g >= toend && w < it->truncation_pixel_width)
19271 {
19272 w += g->pixel_width;
19273 --g;
19274 }
19275 if (to - g - tused > 0)
19276 to = g + tused;
19277 if (it->glyph_row->truncated_on_right_p
19278 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19279 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19280 {
19281 int extra = w - it->truncation_pixel_width;
19282
19283 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19284 }
19285 }
19286
19287 while (from >= end && to >= toend)
19288 *to-- = *from--;
19289 if (!FRAME_WINDOW_P (it->f))
19290 {
19291 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19292 {
19293 from =
19294 truncate_it.glyph_row->glyphs[TEXT_AREA]
19295 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19296 while (from >= end && to >= toend)
19297 *to-- = *from--;
19298 }
19299 }
19300 if (from >= end)
19301 {
19302 /* Need to free some room before prepending additional
19303 glyphs. */
19304 int move_by = from - end + 1;
19305 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19306 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19307
19308 for ( ; g >= g0; g--)
19309 g[move_by] = *g;
19310 while (from >= end)
19311 *to-- = *from--;
19312 it->glyph_row->used[TEXT_AREA] += move_by;
19313 }
19314 }
19315 }
19316
19317 /* Compute the hash code for ROW. */
19318 unsigned
19319 row_hash (struct glyph_row *row)
19320 {
19321 int area, k;
19322 unsigned hashval = 0;
19323
19324 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19325 for (k = 0; k < row->used[area]; ++k)
19326 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19327 + row->glyphs[area][k].u.val
19328 + row->glyphs[area][k].face_id
19329 + row->glyphs[area][k].padding_p
19330 + (row->glyphs[area][k].type << 2));
19331
19332 return hashval;
19333 }
19334
19335 /* Compute the pixel height and width of IT->glyph_row.
19336
19337 Most of the time, ascent and height of a display line will be equal
19338 to the max_ascent and max_height values of the display iterator
19339 structure. This is not the case if
19340
19341 1. We hit ZV without displaying anything. In this case, max_ascent
19342 and max_height will be zero.
19343
19344 2. We have some glyphs that don't contribute to the line height.
19345 (The glyph row flag contributes_to_line_height_p is for future
19346 pixmap extensions).
19347
19348 The first case is easily covered by using default values because in
19349 these cases, the line height does not really matter, except that it
19350 must not be zero. */
19351
19352 static void
19353 compute_line_metrics (struct it *it)
19354 {
19355 struct glyph_row *row = it->glyph_row;
19356
19357 if (FRAME_WINDOW_P (it->f))
19358 {
19359 int i, min_y, max_y;
19360
19361 /* The line may consist of one space only, that was added to
19362 place the cursor on it. If so, the row's height hasn't been
19363 computed yet. */
19364 if (row->height == 0)
19365 {
19366 if (it->max_ascent + it->max_descent == 0)
19367 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19368 row->ascent = it->max_ascent;
19369 row->height = it->max_ascent + it->max_descent;
19370 row->phys_ascent = it->max_phys_ascent;
19371 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19372 row->extra_line_spacing = it->max_extra_line_spacing;
19373 }
19374
19375 /* Compute the width of this line. */
19376 row->pixel_width = row->x;
19377 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19378 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19379
19380 eassert (row->pixel_width >= 0);
19381 eassert (row->ascent >= 0 && row->height > 0);
19382
19383 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19384 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19385
19386 /* If first line's physical ascent is larger than its logical
19387 ascent, use the physical ascent, and make the row taller.
19388 This makes accented characters fully visible. */
19389 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19390 && row->phys_ascent > row->ascent)
19391 {
19392 row->height += row->phys_ascent - row->ascent;
19393 row->ascent = row->phys_ascent;
19394 }
19395
19396 /* Compute how much of the line is visible. */
19397 row->visible_height = row->height;
19398
19399 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19400 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19401
19402 if (row->y < min_y)
19403 row->visible_height -= min_y - row->y;
19404 if (row->y + row->height > max_y)
19405 row->visible_height -= row->y + row->height - max_y;
19406 }
19407 else
19408 {
19409 row->pixel_width = row->used[TEXT_AREA];
19410 if (row->continued_p)
19411 row->pixel_width -= it->continuation_pixel_width;
19412 else if (row->truncated_on_right_p)
19413 row->pixel_width -= it->truncation_pixel_width;
19414 row->ascent = row->phys_ascent = 0;
19415 row->height = row->phys_height = row->visible_height = 1;
19416 row->extra_line_spacing = 0;
19417 }
19418
19419 /* Compute a hash code for this row. */
19420 row->hash = row_hash (row);
19421
19422 it->max_ascent = it->max_descent = 0;
19423 it->max_phys_ascent = it->max_phys_descent = 0;
19424 }
19425
19426
19427 /* Append one space to the glyph row of iterator IT if doing a
19428 window-based redisplay. The space has the same face as
19429 IT->face_id. Value is true if a space was added.
19430
19431 This function is called to make sure that there is always one glyph
19432 at the end of a glyph row that the cursor can be set on under
19433 window-systems. (If there weren't such a glyph we would not know
19434 how wide and tall a box cursor should be displayed).
19435
19436 At the same time this space let's a nicely handle clearing to the
19437 end of the line if the row ends in italic text. */
19438
19439 static bool
19440 append_space_for_newline (struct it *it, bool default_face_p)
19441 {
19442 if (FRAME_WINDOW_P (it->f))
19443 {
19444 int n = it->glyph_row->used[TEXT_AREA];
19445
19446 if (it->glyph_row->glyphs[TEXT_AREA] + n
19447 < it->glyph_row->glyphs[1 + TEXT_AREA])
19448 {
19449 /* Save some values that must not be changed.
19450 Must save IT->c and IT->len because otherwise
19451 ITERATOR_AT_END_P wouldn't work anymore after
19452 append_space_for_newline has been called. */
19453 enum display_element_type saved_what = it->what;
19454 int saved_c = it->c, saved_len = it->len;
19455 int saved_char_to_display = it->char_to_display;
19456 int saved_x = it->current_x;
19457 int saved_face_id = it->face_id;
19458 bool saved_box_end = it->end_of_box_run_p;
19459 struct text_pos saved_pos;
19460 Lisp_Object saved_object;
19461 struct face *face;
19462 struct glyph *g;
19463
19464 saved_object = it->object;
19465 saved_pos = it->position;
19466
19467 it->what = IT_CHARACTER;
19468 memset (&it->position, 0, sizeof it->position);
19469 it->object = Qnil;
19470 it->c = it->char_to_display = ' ';
19471 it->len = 1;
19472
19473 /* If the default face was remapped, be sure to use the
19474 remapped face for the appended newline. */
19475 if (default_face_p)
19476 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19477 else if (it->face_before_selective_p)
19478 it->face_id = it->saved_face_id;
19479 face = FACE_FROM_ID (it->f, it->face_id);
19480 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19481 /* In R2L rows, we will prepend a stretch glyph that will
19482 have the end_of_box_run_p flag set for it, so there's no
19483 need for the appended newline glyph to have that flag
19484 set. */
19485 if (it->glyph_row->reversed_p
19486 /* But if the appended newline glyph goes all the way to
19487 the end of the row, there will be no stretch glyph,
19488 so leave the box flag set. */
19489 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19490 it->end_of_box_run_p = false;
19491
19492 PRODUCE_GLYPHS (it);
19493
19494 #ifdef HAVE_WINDOW_SYSTEM
19495 /* Make sure this space glyph has the right ascent and
19496 descent values, or else cursor at end of line will look
19497 funny, and height of empty lines will be incorrect. */
19498 g = it->glyph_row->glyphs[TEXT_AREA] + n;
19499 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19500 if (n == 0)
19501 {
19502 Lisp_Object height, total_height;
19503 int extra_line_spacing = it->extra_line_spacing;
19504 int boff = font->baseline_offset;
19505
19506 if (font->vertical_centering)
19507 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19508
19509 it->object = saved_object; /* get_it_property needs this */
19510 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19511 /* Must do a subset of line height processing from
19512 x_produce_glyph for newline characters. */
19513 height = get_it_property (it, Qline_height);
19514 if (CONSP (height)
19515 && CONSP (XCDR (height))
19516 && NILP (XCDR (XCDR (height))))
19517 {
19518 total_height = XCAR (XCDR (height));
19519 height = XCAR (height);
19520 }
19521 else
19522 total_height = Qnil;
19523 height = calc_line_height_property (it, height, font, boff, true);
19524
19525 if (it->override_ascent >= 0)
19526 {
19527 it->ascent = it->override_ascent;
19528 it->descent = it->override_descent;
19529 boff = it->override_boff;
19530 }
19531 if (EQ (height, Qt))
19532 extra_line_spacing = 0;
19533 else
19534 {
19535 Lisp_Object spacing;
19536
19537 it->phys_ascent = it->ascent;
19538 it->phys_descent = it->descent;
19539 if (!NILP (height)
19540 && XINT (height) > it->ascent + it->descent)
19541 it->ascent = XINT (height) - it->descent;
19542
19543 if (!NILP (total_height))
19544 spacing = calc_line_height_property (it, total_height, font,
19545 boff, false);
19546 else
19547 {
19548 spacing = get_it_property (it, Qline_spacing);
19549 spacing = calc_line_height_property (it, spacing, font,
19550 boff, false);
19551 }
19552 if (INTEGERP (spacing))
19553 {
19554 extra_line_spacing = XINT (spacing);
19555 if (!NILP (total_height))
19556 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19557 }
19558 }
19559 if (extra_line_spacing > 0)
19560 {
19561 it->descent += extra_line_spacing;
19562 if (extra_line_spacing > it->max_extra_line_spacing)
19563 it->max_extra_line_spacing = extra_line_spacing;
19564 }
19565 it->max_ascent = it->ascent;
19566 it->max_descent = it->descent;
19567 /* Make sure compute_line_metrics recomputes the row height. */
19568 it->glyph_row->height = 0;
19569 }
19570
19571 g->ascent = it->max_ascent;
19572 g->descent = it->max_descent;
19573 #endif
19574
19575 it->override_ascent = -1;
19576 it->constrain_row_ascent_descent_p = false;
19577 it->current_x = saved_x;
19578 it->object = saved_object;
19579 it->position = saved_pos;
19580 it->what = saved_what;
19581 it->face_id = saved_face_id;
19582 it->len = saved_len;
19583 it->c = saved_c;
19584 it->char_to_display = saved_char_to_display;
19585 it->end_of_box_run_p = saved_box_end;
19586 return true;
19587 }
19588 }
19589
19590 return false;
19591 }
19592
19593
19594 /* Extend the face of the last glyph in the text area of IT->glyph_row
19595 to the end of the display line. Called from display_line. If the
19596 glyph row is empty, add a space glyph to it so that we know the
19597 face to draw. Set the glyph row flag fill_line_p. If the glyph
19598 row is R2L, prepend a stretch glyph to cover the empty space to the
19599 left of the leftmost glyph. */
19600
19601 static void
19602 extend_face_to_end_of_line (struct it *it)
19603 {
19604 struct face *face, *default_face;
19605 struct frame *f = it->f;
19606
19607 /* If line is already filled, do nothing. Non window-system frames
19608 get a grace of one more ``pixel'' because their characters are
19609 1-``pixel'' wide, so they hit the equality too early. This grace
19610 is needed only for R2L rows that are not continued, to produce
19611 one extra blank where we could display the cursor. */
19612 if ((it->current_x >= it->last_visible_x
19613 + (!FRAME_WINDOW_P (f)
19614 && it->glyph_row->reversed_p
19615 && !it->glyph_row->continued_p))
19616 /* If the window has display margins, we will need to extend
19617 their face even if the text area is filled. */
19618 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19619 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19620 return;
19621
19622 /* The default face, possibly remapped. */
19623 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19624
19625 /* Face extension extends the background and box of IT->face_id
19626 to the end of the line. If the background equals the background
19627 of the frame, we don't have to do anything. */
19628 if (it->face_before_selective_p)
19629 face = FACE_FROM_ID (f, it->saved_face_id);
19630 else
19631 face = FACE_FROM_ID (f, it->face_id);
19632
19633 if (FRAME_WINDOW_P (f)
19634 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19635 && face->box == FACE_NO_BOX
19636 && face->background == FRAME_BACKGROUND_PIXEL (f)
19637 #ifdef HAVE_WINDOW_SYSTEM
19638 && !face->stipple
19639 #endif
19640 && !it->glyph_row->reversed_p)
19641 return;
19642
19643 /* Set the glyph row flag indicating that the face of the last glyph
19644 in the text area has to be drawn to the end of the text area. */
19645 it->glyph_row->fill_line_p = true;
19646
19647 /* If current character of IT is not ASCII, make sure we have the
19648 ASCII face. This will be automatically undone the next time
19649 get_next_display_element returns a multibyte character. Note
19650 that the character will always be single byte in unibyte
19651 text. */
19652 if (!ASCII_CHAR_P (it->c))
19653 {
19654 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19655 }
19656
19657 if (FRAME_WINDOW_P (f))
19658 {
19659 /* If the row is empty, add a space with the current face of IT,
19660 so that we know which face to draw. */
19661 if (it->glyph_row->used[TEXT_AREA] == 0)
19662 {
19663 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19664 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19665 it->glyph_row->used[TEXT_AREA] = 1;
19666 }
19667 /* Mode line and the header line don't have margins, and
19668 likewise the frame's tool-bar window, if there is any. */
19669 if (!(it->glyph_row->mode_line_p
19670 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19671 || (WINDOWP (f->tool_bar_window)
19672 && it->w == XWINDOW (f->tool_bar_window))
19673 #endif
19674 ))
19675 {
19676 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19677 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19678 {
19679 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19680 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19681 default_face->id;
19682 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19683 }
19684 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19685 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19686 {
19687 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19688 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19689 default_face->id;
19690 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19691 }
19692 }
19693 #ifdef HAVE_WINDOW_SYSTEM
19694 if (it->glyph_row->reversed_p)
19695 {
19696 /* Prepend a stretch glyph to the row, such that the
19697 rightmost glyph will be drawn flushed all the way to the
19698 right margin of the window. The stretch glyph that will
19699 occupy the empty space, if any, to the left of the
19700 glyphs. */
19701 struct font *font = face->font ? face->font : FRAME_FONT (f);
19702 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19703 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19704 struct glyph *g;
19705 int row_width, stretch_ascent, stretch_width;
19706 struct text_pos saved_pos;
19707 int saved_face_id;
19708 bool saved_avoid_cursor, saved_box_start;
19709
19710 for (row_width = 0, g = row_start; g < row_end; g++)
19711 row_width += g->pixel_width;
19712
19713 /* FIXME: There are various minor display glitches in R2L
19714 rows when only one of the fringes is missing. The
19715 strange condition below produces the least bad effect. */
19716 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19717 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19718 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19719 stretch_width = window_box_width (it->w, TEXT_AREA);
19720 else
19721 stretch_width = it->last_visible_x - it->first_visible_x;
19722 stretch_width -= row_width;
19723
19724 if (stretch_width > 0)
19725 {
19726 stretch_ascent =
19727 (((it->ascent + it->descent)
19728 * FONT_BASE (font)) / FONT_HEIGHT (font));
19729 saved_pos = it->position;
19730 memset (&it->position, 0, sizeof it->position);
19731 saved_avoid_cursor = it->avoid_cursor_p;
19732 it->avoid_cursor_p = true;
19733 saved_face_id = it->face_id;
19734 saved_box_start = it->start_of_box_run_p;
19735 /* The last row's stretch glyph should get the default
19736 face, to avoid painting the rest of the window with
19737 the region face, if the region ends at ZV. */
19738 if (it->glyph_row->ends_at_zv_p)
19739 it->face_id = default_face->id;
19740 else
19741 it->face_id = face->id;
19742 it->start_of_box_run_p = false;
19743 append_stretch_glyph (it, Qnil, stretch_width,
19744 it->ascent + it->descent, stretch_ascent);
19745 it->position = saved_pos;
19746 it->avoid_cursor_p = saved_avoid_cursor;
19747 it->face_id = saved_face_id;
19748 it->start_of_box_run_p = saved_box_start;
19749 }
19750 /* If stretch_width comes out negative, it means that the
19751 last glyph is only partially visible. In R2L rows, we
19752 want the leftmost glyph to be partially visible, so we
19753 need to give the row the corresponding left offset. */
19754 if (stretch_width < 0)
19755 it->glyph_row->x = stretch_width;
19756 }
19757 #endif /* HAVE_WINDOW_SYSTEM */
19758 }
19759 else
19760 {
19761 /* Save some values that must not be changed. */
19762 int saved_x = it->current_x;
19763 struct text_pos saved_pos;
19764 Lisp_Object saved_object;
19765 enum display_element_type saved_what = it->what;
19766 int saved_face_id = it->face_id;
19767
19768 saved_object = it->object;
19769 saved_pos = it->position;
19770
19771 it->what = IT_CHARACTER;
19772 memset (&it->position, 0, sizeof it->position);
19773 it->object = Qnil;
19774 it->c = it->char_to_display = ' ';
19775 it->len = 1;
19776
19777 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19778 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19779 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19780 && !it->glyph_row->mode_line_p
19781 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19782 {
19783 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19784 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19785
19786 for (it->current_x = 0; g < e; g++)
19787 it->current_x += g->pixel_width;
19788
19789 it->area = LEFT_MARGIN_AREA;
19790 it->face_id = default_face->id;
19791 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19792 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19793 {
19794 PRODUCE_GLYPHS (it);
19795 /* term.c:produce_glyphs advances it->current_x only for
19796 TEXT_AREA. */
19797 it->current_x += it->pixel_width;
19798 }
19799
19800 it->current_x = saved_x;
19801 it->area = TEXT_AREA;
19802 }
19803
19804 /* The last row's blank glyphs should get the default face, to
19805 avoid painting the rest of the window with the region face,
19806 if the region ends at ZV. */
19807 if (it->glyph_row->ends_at_zv_p)
19808 it->face_id = default_face->id;
19809 else
19810 it->face_id = face->id;
19811 PRODUCE_GLYPHS (it);
19812
19813 while (it->current_x <= it->last_visible_x)
19814 PRODUCE_GLYPHS (it);
19815
19816 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19817 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19818 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19819 && !it->glyph_row->mode_line_p
19820 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19821 {
19822 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19823 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19824
19825 for ( ; g < e; g++)
19826 it->current_x += g->pixel_width;
19827
19828 it->area = RIGHT_MARGIN_AREA;
19829 it->face_id = default_face->id;
19830 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19831 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19832 {
19833 PRODUCE_GLYPHS (it);
19834 it->current_x += it->pixel_width;
19835 }
19836
19837 it->area = TEXT_AREA;
19838 }
19839
19840 /* Don't count these blanks really. It would let us insert a left
19841 truncation glyph below and make us set the cursor on them, maybe. */
19842 it->current_x = saved_x;
19843 it->object = saved_object;
19844 it->position = saved_pos;
19845 it->what = saved_what;
19846 it->face_id = saved_face_id;
19847 }
19848 }
19849
19850
19851 /* Value is true if text starting at CHARPOS in current_buffer is
19852 trailing whitespace. */
19853
19854 static bool
19855 trailing_whitespace_p (ptrdiff_t charpos)
19856 {
19857 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19858 int c = 0;
19859
19860 while (bytepos < ZV_BYTE
19861 && (c = FETCH_CHAR (bytepos),
19862 c == ' ' || c == '\t'))
19863 ++bytepos;
19864
19865 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19866 {
19867 if (bytepos != PT_BYTE)
19868 return true;
19869 }
19870 return false;
19871 }
19872
19873
19874 /* Highlight trailing whitespace, if any, in ROW. */
19875
19876 static void
19877 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19878 {
19879 int used = row->used[TEXT_AREA];
19880
19881 if (used)
19882 {
19883 struct glyph *start = row->glyphs[TEXT_AREA];
19884 struct glyph *glyph = start + used - 1;
19885
19886 if (row->reversed_p)
19887 {
19888 /* Right-to-left rows need to be processed in the opposite
19889 direction, so swap the edge pointers. */
19890 glyph = start;
19891 start = row->glyphs[TEXT_AREA] + used - 1;
19892 }
19893
19894 /* Skip over glyphs inserted to display the cursor at the
19895 end of a line, for extending the face of the last glyph
19896 to the end of the line on terminals, and for truncation
19897 and continuation glyphs. */
19898 if (!row->reversed_p)
19899 {
19900 while (glyph >= start
19901 && glyph->type == CHAR_GLYPH
19902 && NILP (glyph->object))
19903 --glyph;
19904 }
19905 else
19906 {
19907 while (glyph <= start
19908 && glyph->type == CHAR_GLYPH
19909 && NILP (glyph->object))
19910 ++glyph;
19911 }
19912
19913 /* If last glyph is a space or stretch, and it's trailing
19914 whitespace, set the face of all trailing whitespace glyphs in
19915 IT->glyph_row to `trailing-whitespace'. */
19916 if ((row->reversed_p ? glyph <= start : glyph >= start)
19917 && BUFFERP (glyph->object)
19918 && (glyph->type == STRETCH_GLYPH
19919 || (glyph->type == CHAR_GLYPH
19920 && glyph->u.ch == ' '))
19921 && trailing_whitespace_p (glyph->charpos))
19922 {
19923 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
19924 if (face_id < 0)
19925 return;
19926
19927 if (!row->reversed_p)
19928 {
19929 while (glyph >= start
19930 && BUFFERP (glyph->object)
19931 && (glyph->type == STRETCH_GLYPH
19932 || (glyph->type == CHAR_GLYPH
19933 && glyph->u.ch == ' ')))
19934 (glyph--)->face_id = face_id;
19935 }
19936 else
19937 {
19938 while (glyph <= start
19939 && BUFFERP (glyph->object)
19940 && (glyph->type == STRETCH_GLYPH
19941 || (glyph->type == CHAR_GLYPH
19942 && glyph->u.ch == ' ')))
19943 (glyph++)->face_id = face_id;
19944 }
19945 }
19946 }
19947 }
19948
19949
19950 /* Value is true if glyph row ROW should be
19951 considered to hold the buffer position CHARPOS. */
19952
19953 static bool
19954 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19955 {
19956 bool result = true;
19957
19958 if (charpos == CHARPOS (row->end.pos)
19959 || charpos == MATRIX_ROW_END_CHARPOS (row))
19960 {
19961 /* Suppose the row ends on a string.
19962 Unless the row is continued, that means it ends on a newline
19963 in the string. If it's anything other than a display string
19964 (e.g., a before-string from an overlay), we don't want the
19965 cursor there. (This heuristic seems to give the optimal
19966 behavior for the various types of multi-line strings.)
19967 One exception: if the string has `cursor' property on one of
19968 its characters, we _do_ want the cursor there. */
19969 if (CHARPOS (row->end.string_pos) >= 0)
19970 {
19971 if (row->continued_p)
19972 result = true;
19973 else
19974 {
19975 /* Check for `display' property. */
19976 struct glyph *beg = row->glyphs[TEXT_AREA];
19977 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19978 struct glyph *glyph;
19979
19980 result = false;
19981 for (glyph = end; glyph >= beg; --glyph)
19982 if (STRINGP (glyph->object))
19983 {
19984 Lisp_Object prop
19985 = Fget_char_property (make_number (charpos),
19986 Qdisplay, Qnil);
19987 result =
19988 (!NILP (prop)
19989 && display_prop_string_p (prop, glyph->object));
19990 /* If there's a `cursor' property on one of the
19991 string's characters, this row is a cursor row,
19992 even though this is not a display string. */
19993 if (!result)
19994 {
19995 Lisp_Object s = glyph->object;
19996
19997 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19998 {
19999 ptrdiff_t gpos = glyph->charpos;
20000
20001 if (!NILP (Fget_char_property (make_number (gpos),
20002 Qcursor, s)))
20003 {
20004 result = true;
20005 break;
20006 }
20007 }
20008 }
20009 break;
20010 }
20011 }
20012 }
20013 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20014 {
20015 /* If the row ends in middle of a real character,
20016 and the line is continued, we want the cursor here.
20017 That's because CHARPOS (ROW->end.pos) would equal
20018 PT if PT is before the character. */
20019 if (!row->ends_in_ellipsis_p)
20020 result = row->continued_p;
20021 else
20022 /* If the row ends in an ellipsis, then
20023 CHARPOS (ROW->end.pos) will equal point after the
20024 invisible text. We want that position to be displayed
20025 after the ellipsis. */
20026 result = false;
20027 }
20028 /* If the row ends at ZV, display the cursor at the end of that
20029 row instead of at the start of the row below. */
20030 else
20031 result = row->ends_at_zv_p;
20032 }
20033
20034 return result;
20035 }
20036
20037 /* Value is true if glyph row ROW should be
20038 used to hold the cursor. */
20039
20040 static bool
20041 cursor_row_p (struct glyph_row *row)
20042 {
20043 return row_for_charpos_p (row, PT);
20044 }
20045
20046 \f
20047
20048 /* Push the property PROP so that it will be rendered at the current
20049 position in IT. Return true if PROP was successfully pushed, false
20050 otherwise. Called from handle_line_prefix to handle the
20051 `line-prefix' and `wrap-prefix' properties. */
20052
20053 static bool
20054 push_prefix_prop (struct it *it, Lisp_Object prop)
20055 {
20056 struct text_pos pos =
20057 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20058
20059 eassert (it->method == GET_FROM_BUFFER
20060 || it->method == GET_FROM_DISPLAY_VECTOR
20061 || it->method == GET_FROM_STRING
20062 || it->method == GET_FROM_IMAGE);
20063
20064 /* We need to save the current buffer/string position, so it will be
20065 restored by pop_it, because iterate_out_of_display_property
20066 depends on that being set correctly, but some situations leave
20067 it->position not yet set when this function is called. */
20068 push_it (it, &pos);
20069
20070 if (STRINGP (prop))
20071 {
20072 if (SCHARS (prop) == 0)
20073 {
20074 pop_it (it);
20075 return false;
20076 }
20077
20078 it->string = prop;
20079 it->string_from_prefix_prop_p = true;
20080 it->multibyte_p = STRING_MULTIBYTE (it->string);
20081 it->current.overlay_string_index = -1;
20082 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20083 it->end_charpos = it->string_nchars = SCHARS (it->string);
20084 it->method = GET_FROM_STRING;
20085 it->stop_charpos = 0;
20086 it->prev_stop = 0;
20087 it->base_level_stop = 0;
20088
20089 /* Force paragraph direction to be that of the parent
20090 buffer/string. */
20091 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20092 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20093 else
20094 it->paragraph_embedding = L2R;
20095
20096 /* Set up the bidi iterator for this display string. */
20097 if (it->bidi_p)
20098 {
20099 it->bidi_it.string.lstring = it->string;
20100 it->bidi_it.string.s = NULL;
20101 it->bidi_it.string.schars = it->end_charpos;
20102 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20103 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20104 it->bidi_it.string.unibyte = !it->multibyte_p;
20105 it->bidi_it.w = it->w;
20106 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20107 }
20108 }
20109 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20110 {
20111 it->method = GET_FROM_STRETCH;
20112 it->object = prop;
20113 }
20114 #ifdef HAVE_WINDOW_SYSTEM
20115 else if (IMAGEP (prop))
20116 {
20117 it->what = IT_IMAGE;
20118 it->image_id = lookup_image (it->f, prop);
20119 it->method = GET_FROM_IMAGE;
20120 }
20121 #endif /* HAVE_WINDOW_SYSTEM */
20122 else
20123 {
20124 pop_it (it); /* bogus display property, give up */
20125 return false;
20126 }
20127
20128 return true;
20129 }
20130
20131 /* Return the character-property PROP at the current position in IT. */
20132
20133 static Lisp_Object
20134 get_it_property (struct it *it, Lisp_Object prop)
20135 {
20136 Lisp_Object position, object = it->object;
20137
20138 if (STRINGP (object))
20139 position = make_number (IT_STRING_CHARPOS (*it));
20140 else if (BUFFERP (object))
20141 {
20142 position = make_number (IT_CHARPOS (*it));
20143 object = it->window;
20144 }
20145 else
20146 return Qnil;
20147
20148 return Fget_char_property (position, prop, object);
20149 }
20150
20151 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20152
20153 static void
20154 handle_line_prefix (struct it *it)
20155 {
20156 Lisp_Object prefix;
20157
20158 if (it->continuation_lines_width > 0)
20159 {
20160 prefix = get_it_property (it, Qwrap_prefix);
20161 if (NILP (prefix))
20162 prefix = Vwrap_prefix;
20163 }
20164 else
20165 {
20166 prefix = get_it_property (it, Qline_prefix);
20167 if (NILP (prefix))
20168 prefix = Vline_prefix;
20169 }
20170 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20171 {
20172 /* If the prefix is wider than the window, and we try to wrap
20173 it, it would acquire its own wrap prefix, and so on till the
20174 iterator stack overflows. So, don't wrap the prefix. */
20175 it->line_wrap = TRUNCATE;
20176 it->avoid_cursor_p = true;
20177 }
20178 }
20179
20180 \f
20181
20182 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20183 only for R2L lines from display_line and display_string, when they
20184 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20185 the line/string needs to be continued on the next glyph row. */
20186 static void
20187 unproduce_glyphs (struct it *it, int n)
20188 {
20189 struct glyph *glyph, *end;
20190
20191 eassert (it->glyph_row);
20192 eassert (it->glyph_row->reversed_p);
20193 eassert (it->area == TEXT_AREA);
20194 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20195
20196 if (n > it->glyph_row->used[TEXT_AREA])
20197 n = it->glyph_row->used[TEXT_AREA];
20198 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20199 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20200 for ( ; glyph < end; glyph++)
20201 glyph[-n] = *glyph;
20202 }
20203
20204 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20205 and ROW->maxpos. */
20206 static void
20207 find_row_edges (struct it *it, struct glyph_row *row,
20208 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20209 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20210 {
20211 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20212 lines' rows is implemented for bidi-reordered rows. */
20213
20214 /* ROW->minpos is the value of min_pos, the minimal buffer position
20215 we have in ROW, or ROW->start.pos if that is smaller. */
20216 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20217 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20218 else
20219 /* We didn't find buffer positions smaller than ROW->start, or
20220 didn't find _any_ valid buffer positions in any of the glyphs,
20221 so we must trust the iterator's computed positions. */
20222 row->minpos = row->start.pos;
20223 if (max_pos <= 0)
20224 {
20225 max_pos = CHARPOS (it->current.pos);
20226 max_bpos = BYTEPOS (it->current.pos);
20227 }
20228
20229 /* Here are the various use-cases for ending the row, and the
20230 corresponding values for ROW->maxpos:
20231
20232 Line ends in a newline from buffer eol_pos + 1
20233 Line is continued from buffer max_pos + 1
20234 Line is truncated on right it->current.pos
20235 Line ends in a newline from string max_pos + 1(*)
20236 (*) + 1 only when line ends in a forward scan
20237 Line is continued from string max_pos
20238 Line is continued from display vector max_pos
20239 Line is entirely from a string min_pos == max_pos
20240 Line is entirely from a display vector min_pos == max_pos
20241 Line that ends at ZV ZV
20242
20243 If you discover other use-cases, please add them here as
20244 appropriate. */
20245 if (row->ends_at_zv_p)
20246 row->maxpos = it->current.pos;
20247 else if (row->used[TEXT_AREA])
20248 {
20249 bool seen_this_string = false;
20250 struct glyph_row *r1 = row - 1;
20251
20252 /* Did we see the same display string on the previous row? */
20253 if (STRINGP (it->object)
20254 /* this is not the first row */
20255 && row > it->w->desired_matrix->rows
20256 /* previous row is not the header line */
20257 && !r1->mode_line_p
20258 /* previous row also ends in a newline from a string */
20259 && r1->ends_in_newline_from_string_p)
20260 {
20261 struct glyph *start, *end;
20262
20263 /* Search for the last glyph of the previous row that came
20264 from buffer or string. Depending on whether the row is
20265 L2R or R2L, we need to process it front to back or the
20266 other way round. */
20267 if (!r1->reversed_p)
20268 {
20269 start = r1->glyphs[TEXT_AREA];
20270 end = start + r1->used[TEXT_AREA];
20271 /* Glyphs inserted by redisplay have nil as their object. */
20272 while (end > start
20273 && NILP ((end - 1)->object)
20274 && (end - 1)->charpos <= 0)
20275 --end;
20276 if (end > start)
20277 {
20278 if (EQ ((end - 1)->object, it->object))
20279 seen_this_string = true;
20280 }
20281 else
20282 /* If all the glyphs of the previous row were inserted
20283 by redisplay, it means the previous row was
20284 produced from a single newline, which is only
20285 possible if that newline came from the same string
20286 as the one which produced this ROW. */
20287 seen_this_string = true;
20288 }
20289 else
20290 {
20291 end = r1->glyphs[TEXT_AREA] - 1;
20292 start = end + r1->used[TEXT_AREA];
20293 while (end < start
20294 && NILP ((end + 1)->object)
20295 && (end + 1)->charpos <= 0)
20296 ++end;
20297 if (end < start)
20298 {
20299 if (EQ ((end + 1)->object, it->object))
20300 seen_this_string = true;
20301 }
20302 else
20303 seen_this_string = true;
20304 }
20305 }
20306 /* Take note of each display string that covers a newline only
20307 once, the first time we see it. This is for when a display
20308 string includes more than one newline in it. */
20309 if (row->ends_in_newline_from_string_p && !seen_this_string)
20310 {
20311 /* If we were scanning the buffer forward when we displayed
20312 the string, we want to account for at least one buffer
20313 position that belongs to this row (position covered by
20314 the display string), so that cursor positioning will
20315 consider this row as a candidate when point is at the end
20316 of the visual line represented by this row. This is not
20317 required when scanning back, because max_pos will already
20318 have a much larger value. */
20319 if (CHARPOS (row->end.pos) > max_pos)
20320 INC_BOTH (max_pos, max_bpos);
20321 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20322 }
20323 else if (CHARPOS (it->eol_pos) > 0)
20324 SET_TEXT_POS (row->maxpos,
20325 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20326 else if (row->continued_p)
20327 {
20328 /* If max_pos is different from IT's current position, it
20329 means IT->method does not belong to the display element
20330 at max_pos. However, it also means that the display
20331 element at max_pos was displayed in its entirety on this
20332 line, which is equivalent to saying that the next line
20333 starts at the next buffer position. */
20334 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20335 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20336 else
20337 {
20338 INC_BOTH (max_pos, max_bpos);
20339 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20340 }
20341 }
20342 else if (row->truncated_on_right_p)
20343 /* display_line already called reseat_at_next_visible_line_start,
20344 which puts the iterator at the beginning of the next line, in
20345 the logical order. */
20346 row->maxpos = it->current.pos;
20347 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20348 /* A line that is entirely from a string/image/stretch... */
20349 row->maxpos = row->minpos;
20350 else
20351 emacs_abort ();
20352 }
20353 else
20354 row->maxpos = it->current.pos;
20355 }
20356
20357 /* Construct the glyph row IT->glyph_row in the desired matrix of
20358 IT->w from text at the current position of IT. See dispextern.h
20359 for an overview of struct it. Value is true if
20360 IT->glyph_row displays text, as opposed to a line displaying ZV
20361 only. */
20362
20363 static bool
20364 display_line (struct it *it)
20365 {
20366 struct glyph_row *row = it->glyph_row;
20367 Lisp_Object overlay_arrow_string;
20368 struct it wrap_it;
20369 void *wrap_data = NULL;
20370 bool may_wrap = false;
20371 int wrap_x IF_LINT (= 0);
20372 int wrap_row_used = -1;
20373 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20374 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20375 int wrap_row_extra_line_spacing IF_LINT (= 0);
20376 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20377 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20378 int cvpos;
20379 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20380 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20381 bool pending_handle_line_prefix = false;
20382
20383 /* We always start displaying at hpos zero even if hscrolled. */
20384 eassert (it->hpos == 0 && it->current_x == 0);
20385
20386 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20387 >= it->w->desired_matrix->nrows)
20388 {
20389 it->w->nrows_scale_factor++;
20390 it->f->fonts_changed = true;
20391 return false;
20392 }
20393
20394 /* Clear the result glyph row and enable it. */
20395 prepare_desired_row (it->w, row, false);
20396
20397 row->y = it->current_y;
20398 row->start = it->start;
20399 row->continuation_lines_width = it->continuation_lines_width;
20400 row->displays_text_p = true;
20401 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20402 it->starts_in_middle_of_char_p = false;
20403
20404 /* Arrange the overlays nicely for our purposes. Usually, we call
20405 display_line on only one line at a time, in which case this
20406 can't really hurt too much, or we call it on lines which appear
20407 one after another in the buffer, in which case all calls to
20408 recenter_overlay_lists but the first will be pretty cheap. */
20409 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20410
20411 /* Move over display elements that are not visible because we are
20412 hscrolled. This may stop at an x-position < IT->first_visible_x
20413 if the first glyph is partially visible or if we hit a line end. */
20414 if (it->current_x < it->first_visible_x)
20415 {
20416 enum move_it_result move_result;
20417
20418 this_line_min_pos = row->start.pos;
20419 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20420 MOVE_TO_POS | MOVE_TO_X);
20421 /* If we are under a large hscroll, move_it_in_display_line_to
20422 could hit the end of the line without reaching
20423 it->first_visible_x. Pretend that we did reach it. This is
20424 especially important on a TTY, where we will call
20425 extend_face_to_end_of_line, which needs to know how many
20426 blank glyphs to produce. */
20427 if (it->current_x < it->first_visible_x
20428 && (move_result == MOVE_NEWLINE_OR_CR
20429 || move_result == MOVE_POS_MATCH_OR_ZV))
20430 it->current_x = it->first_visible_x;
20431
20432 /* Record the smallest positions seen while we moved over
20433 display elements that are not visible. This is needed by
20434 redisplay_internal for optimizing the case where the cursor
20435 stays inside the same line. The rest of this function only
20436 considers positions that are actually displayed, so
20437 RECORD_MAX_MIN_POS will not otherwise record positions that
20438 are hscrolled to the left of the left edge of the window. */
20439 min_pos = CHARPOS (this_line_min_pos);
20440 min_bpos = BYTEPOS (this_line_min_pos);
20441 }
20442 else if (it->area == TEXT_AREA)
20443 {
20444 /* We only do this when not calling move_it_in_display_line_to
20445 above, because that function calls itself handle_line_prefix. */
20446 handle_line_prefix (it);
20447 }
20448 else
20449 {
20450 /* Line-prefix and wrap-prefix are always displayed in the text
20451 area. But if this is the first call to display_line after
20452 init_iterator, the iterator might have been set up to write
20453 into a marginal area, e.g. if the line begins with some
20454 display property that writes to the margins. So we need to
20455 wait with the call to handle_line_prefix until whatever
20456 writes to the margin has done its job. */
20457 pending_handle_line_prefix = true;
20458 }
20459
20460 /* Get the initial row height. This is either the height of the
20461 text hscrolled, if there is any, or zero. */
20462 row->ascent = it->max_ascent;
20463 row->height = it->max_ascent + it->max_descent;
20464 row->phys_ascent = it->max_phys_ascent;
20465 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20466 row->extra_line_spacing = it->max_extra_line_spacing;
20467
20468 /* Utility macro to record max and min buffer positions seen until now. */
20469 #define RECORD_MAX_MIN_POS(IT) \
20470 do \
20471 { \
20472 bool composition_p \
20473 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20474 ptrdiff_t current_pos = \
20475 composition_p ? (IT)->cmp_it.charpos \
20476 : IT_CHARPOS (*(IT)); \
20477 ptrdiff_t current_bpos = \
20478 composition_p ? CHAR_TO_BYTE (current_pos) \
20479 : IT_BYTEPOS (*(IT)); \
20480 if (current_pos < min_pos) \
20481 { \
20482 min_pos = current_pos; \
20483 min_bpos = current_bpos; \
20484 } \
20485 if (IT_CHARPOS (*it) > max_pos) \
20486 { \
20487 max_pos = IT_CHARPOS (*it); \
20488 max_bpos = IT_BYTEPOS (*it); \
20489 } \
20490 } \
20491 while (false)
20492
20493 /* Loop generating characters. The loop is left with IT on the next
20494 character to display. */
20495 while (true)
20496 {
20497 int n_glyphs_before, hpos_before, x_before;
20498 int x, nglyphs;
20499 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20500
20501 /* Retrieve the next thing to display. Value is false if end of
20502 buffer reached. */
20503 if (!get_next_display_element (it))
20504 {
20505 /* Maybe add a space at the end of this line that is used to
20506 display the cursor there under X. Set the charpos of the
20507 first glyph of blank lines not corresponding to any text
20508 to -1. */
20509 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20510 row->exact_window_width_line_p = true;
20511 else if ((append_space_for_newline (it, true)
20512 && row->used[TEXT_AREA] == 1)
20513 || row->used[TEXT_AREA] == 0)
20514 {
20515 row->glyphs[TEXT_AREA]->charpos = -1;
20516 row->displays_text_p = false;
20517
20518 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20519 && (!MINI_WINDOW_P (it->w)
20520 || (minibuf_level && EQ (it->window, minibuf_window))))
20521 row->indicate_empty_line_p = true;
20522 }
20523
20524 it->continuation_lines_width = 0;
20525 row->ends_at_zv_p = true;
20526 /* A row that displays right-to-left text must always have
20527 its last face extended all the way to the end of line,
20528 even if this row ends in ZV, because we still write to
20529 the screen left to right. We also need to extend the
20530 last face if the default face is remapped to some
20531 different face, otherwise the functions that clear
20532 portions of the screen will clear with the default face's
20533 background color. */
20534 if (row->reversed_p
20535 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20536 extend_face_to_end_of_line (it);
20537 break;
20538 }
20539
20540 /* Now, get the metrics of what we want to display. This also
20541 generates glyphs in `row' (which is IT->glyph_row). */
20542 n_glyphs_before = row->used[TEXT_AREA];
20543 x = it->current_x;
20544
20545 /* Remember the line height so far in case the next element doesn't
20546 fit on the line. */
20547 if (it->line_wrap != TRUNCATE)
20548 {
20549 ascent = it->max_ascent;
20550 descent = it->max_descent;
20551 phys_ascent = it->max_phys_ascent;
20552 phys_descent = it->max_phys_descent;
20553
20554 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20555 {
20556 if (IT_DISPLAYING_WHITESPACE (it))
20557 may_wrap = true;
20558 else if (may_wrap)
20559 {
20560 SAVE_IT (wrap_it, *it, wrap_data);
20561 wrap_x = x;
20562 wrap_row_used = row->used[TEXT_AREA];
20563 wrap_row_ascent = row->ascent;
20564 wrap_row_height = row->height;
20565 wrap_row_phys_ascent = row->phys_ascent;
20566 wrap_row_phys_height = row->phys_height;
20567 wrap_row_extra_line_spacing = row->extra_line_spacing;
20568 wrap_row_min_pos = min_pos;
20569 wrap_row_min_bpos = min_bpos;
20570 wrap_row_max_pos = max_pos;
20571 wrap_row_max_bpos = max_bpos;
20572 may_wrap = false;
20573 }
20574 }
20575 }
20576
20577 PRODUCE_GLYPHS (it);
20578
20579 /* If this display element was in marginal areas, continue with
20580 the next one. */
20581 if (it->area != TEXT_AREA)
20582 {
20583 row->ascent = max (row->ascent, it->max_ascent);
20584 row->height = max (row->height, it->max_ascent + it->max_descent);
20585 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20586 row->phys_height = max (row->phys_height,
20587 it->max_phys_ascent + it->max_phys_descent);
20588 row->extra_line_spacing = max (row->extra_line_spacing,
20589 it->max_extra_line_spacing);
20590 set_iterator_to_next (it, true);
20591 /* If we didn't handle the line/wrap prefix above, and the
20592 call to set_iterator_to_next just switched to TEXT_AREA,
20593 process the prefix now. */
20594 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20595 {
20596 pending_handle_line_prefix = false;
20597 handle_line_prefix (it);
20598 }
20599 continue;
20600 }
20601
20602 /* Does the display element fit on the line? If we truncate
20603 lines, we should draw past the right edge of the window. If
20604 we don't truncate, we want to stop so that we can display the
20605 continuation glyph before the right margin. If lines are
20606 continued, there are two possible strategies for characters
20607 resulting in more than 1 glyph (e.g. tabs): Display as many
20608 glyphs as possible in this line and leave the rest for the
20609 continuation line, or display the whole element in the next
20610 line. Original redisplay did the former, so we do it also. */
20611 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20612 hpos_before = it->hpos;
20613 x_before = x;
20614
20615 if (/* Not a newline. */
20616 nglyphs > 0
20617 /* Glyphs produced fit entirely in the line. */
20618 && it->current_x < it->last_visible_x)
20619 {
20620 it->hpos += nglyphs;
20621 row->ascent = max (row->ascent, it->max_ascent);
20622 row->height = max (row->height, it->max_ascent + it->max_descent);
20623 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20624 row->phys_height = max (row->phys_height,
20625 it->max_phys_ascent + it->max_phys_descent);
20626 row->extra_line_spacing = max (row->extra_line_spacing,
20627 it->max_extra_line_spacing);
20628 if (it->current_x - it->pixel_width < it->first_visible_x
20629 /* In R2L rows, we arrange in extend_face_to_end_of_line
20630 to add a right offset to the line, by a suitable
20631 change to the stretch glyph that is the leftmost
20632 glyph of the line. */
20633 && !row->reversed_p)
20634 row->x = x - it->first_visible_x;
20635 /* Record the maximum and minimum buffer positions seen so
20636 far in glyphs that will be displayed by this row. */
20637 if (it->bidi_p)
20638 RECORD_MAX_MIN_POS (it);
20639 }
20640 else
20641 {
20642 int i, new_x;
20643 struct glyph *glyph;
20644
20645 for (i = 0; i < nglyphs; ++i, x = new_x)
20646 {
20647 /* Identify the glyphs added by the last call to
20648 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20649 the previous glyphs. */
20650 if (!row->reversed_p)
20651 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20652 else
20653 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20654 new_x = x + glyph->pixel_width;
20655
20656 if (/* Lines are continued. */
20657 it->line_wrap != TRUNCATE
20658 && (/* Glyph doesn't fit on the line. */
20659 new_x > it->last_visible_x
20660 /* Or it fits exactly on a window system frame. */
20661 || (new_x == it->last_visible_x
20662 && FRAME_WINDOW_P (it->f)
20663 && (row->reversed_p
20664 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20665 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20666 {
20667 /* End of a continued line. */
20668
20669 if (it->hpos == 0
20670 || (new_x == it->last_visible_x
20671 && FRAME_WINDOW_P (it->f)
20672 && (row->reversed_p
20673 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20674 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20675 {
20676 /* Current glyph is the only one on the line or
20677 fits exactly on the line. We must continue
20678 the line because we can't draw the cursor
20679 after the glyph. */
20680 row->continued_p = true;
20681 it->current_x = new_x;
20682 it->continuation_lines_width += new_x;
20683 ++it->hpos;
20684 if (i == nglyphs - 1)
20685 {
20686 /* If line-wrap is on, check if a previous
20687 wrap point was found. */
20688 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20689 && wrap_row_used > 0
20690 /* Even if there is a previous wrap
20691 point, continue the line here as
20692 usual, if (i) the previous character
20693 was a space or tab AND (ii) the
20694 current character is not. */
20695 && (!may_wrap
20696 || IT_DISPLAYING_WHITESPACE (it)))
20697 goto back_to_wrap;
20698
20699 /* Record the maximum and minimum buffer
20700 positions seen so far in glyphs that will be
20701 displayed by this row. */
20702 if (it->bidi_p)
20703 RECORD_MAX_MIN_POS (it);
20704 set_iterator_to_next (it, true);
20705 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20706 {
20707 if (!get_next_display_element (it))
20708 {
20709 row->exact_window_width_line_p = true;
20710 it->continuation_lines_width = 0;
20711 row->continued_p = false;
20712 row->ends_at_zv_p = true;
20713 }
20714 else if (ITERATOR_AT_END_OF_LINE_P (it))
20715 {
20716 row->continued_p = false;
20717 row->exact_window_width_line_p = true;
20718 }
20719 /* If line-wrap is on, check if a
20720 previous wrap point was found. */
20721 else if (wrap_row_used > 0
20722 /* Even if there is a previous wrap
20723 point, continue the line here as
20724 usual, if (i) the previous character
20725 was a space or tab AND (ii) the
20726 current character is not. */
20727 && (!may_wrap
20728 || IT_DISPLAYING_WHITESPACE (it)))
20729 goto back_to_wrap;
20730
20731 }
20732 }
20733 else if (it->bidi_p)
20734 RECORD_MAX_MIN_POS (it);
20735 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20736 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20737 extend_face_to_end_of_line (it);
20738 }
20739 else if (CHAR_GLYPH_PADDING_P (*glyph)
20740 && !FRAME_WINDOW_P (it->f))
20741 {
20742 /* A padding glyph that doesn't fit on this line.
20743 This means the whole character doesn't fit
20744 on the line. */
20745 if (row->reversed_p)
20746 unproduce_glyphs (it, row->used[TEXT_AREA]
20747 - n_glyphs_before);
20748 row->used[TEXT_AREA] = n_glyphs_before;
20749
20750 /* Fill the rest of the row with continuation
20751 glyphs like in 20.x. */
20752 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20753 < row->glyphs[1 + TEXT_AREA])
20754 produce_special_glyphs (it, IT_CONTINUATION);
20755
20756 row->continued_p = true;
20757 it->current_x = x_before;
20758 it->continuation_lines_width += x_before;
20759
20760 /* Restore the height to what it was before the
20761 element not fitting on the line. */
20762 it->max_ascent = ascent;
20763 it->max_descent = descent;
20764 it->max_phys_ascent = phys_ascent;
20765 it->max_phys_descent = phys_descent;
20766 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20767 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20768 extend_face_to_end_of_line (it);
20769 }
20770 else if (wrap_row_used > 0)
20771 {
20772 back_to_wrap:
20773 if (row->reversed_p)
20774 unproduce_glyphs (it,
20775 row->used[TEXT_AREA] - wrap_row_used);
20776 RESTORE_IT (it, &wrap_it, wrap_data);
20777 it->continuation_lines_width += wrap_x;
20778 row->used[TEXT_AREA] = wrap_row_used;
20779 row->ascent = wrap_row_ascent;
20780 row->height = wrap_row_height;
20781 row->phys_ascent = wrap_row_phys_ascent;
20782 row->phys_height = wrap_row_phys_height;
20783 row->extra_line_spacing = wrap_row_extra_line_spacing;
20784 min_pos = wrap_row_min_pos;
20785 min_bpos = wrap_row_min_bpos;
20786 max_pos = wrap_row_max_pos;
20787 max_bpos = wrap_row_max_bpos;
20788 row->continued_p = true;
20789 row->ends_at_zv_p = false;
20790 row->exact_window_width_line_p = false;
20791 it->continuation_lines_width += x;
20792
20793 /* Make sure that a non-default face is extended
20794 up to the right margin of the window. */
20795 extend_face_to_end_of_line (it);
20796 }
20797 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20798 {
20799 /* A TAB that extends past the right edge of the
20800 window. This produces a single glyph on
20801 window system frames. We leave the glyph in
20802 this row and let it fill the row, but don't
20803 consume the TAB. */
20804 if ((row->reversed_p
20805 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20806 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20807 produce_special_glyphs (it, IT_CONTINUATION);
20808 it->continuation_lines_width += it->last_visible_x;
20809 row->ends_in_middle_of_char_p = true;
20810 row->continued_p = true;
20811 glyph->pixel_width = it->last_visible_x - x;
20812 it->starts_in_middle_of_char_p = true;
20813 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20814 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20815 extend_face_to_end_of_line (it);
20816 }
20817 else
20818 {
20819 /* Something other than a TAB that draws past
20820 the right edge of the window. Restore
20821 positions to values before the element. */
20822 if (row->reversed_p)
20823 unproduce_glyphs (it, row->used[TEXT_AREA]
20824 - (n_glyphs_before + i));
20825 row->used[TEXT_AREA] = n_glyphs_before + i;
20826
20827 /* Display continuation glyphs. */
20828 it->current_x = x_before;
20829 it->continuation_lines_width += x;
20830 if (!FRAME_WINDOW_P (it->f)
20831 || (row->reversed_p
20832 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20833 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20834 produce_special_glyphs (it, IT_CONTINUATION);
20835 row->continued_p = true;
20836
20837 extend_face_to_end_of_line (it);
20838
20839 if (nglyphs > 1 && i > 0)
20840 {
20841 row->ends_in_middle_of_char_p = true;
20842 it->starts_in_middle_of_char_p = true;
20843 }
20844
20845 /* Restore the height to what it was before the
20846 element not fitting on the line. */
20847 it->max_ascent = ascent;
20848 it->max_descent = descent;
20849 it->max_phys_ascent = phys_ascent;
20850 it->max_phys_descent = phys_descent;
20851 }
20852
20853 break;
20854 }
20855 else if (new_x > it->first_visible_x)
20856 {
20857 /* Increment number of glyphs actually displayed. */
20858 ++it->hpos;
20859
20860 /* Record the maximum and minimum buffer positions
20861 seen so far in glyphs that will be displayed by
20862 this row. */
20863 if (it->bidi_p)
20864 RECORD_MAX_MIN_POS (it);
20865
20866 if (x < it->first_visible_x && !row->reversed_p)
20867 /* Glyph is partially visible, i.e. row starts at
20868 negative X position. Don't do that in R2L
20869 rows, where we arrange to add a right offset to
20870 the line in extend_face_to_end_of_line, by a
20871 suitable change to the stretch glyph that is
20872 the leftmost glyph of the line. */
20873 row->x = x - it->first_visible_x;
20874 /* When the last glyph of an R2L row only fits
20875 partially on the line, we need to set row->x to a
20876 negative offset, so that the leftmost glyph is
20877 the one that is partially visible. But if we are
20878 going to produce the truncation glyph, this will
20879 be taken care of in produce_special_glyphs. */
20880 if (row->reversed_p
20881 && new_x > it->last_visible_x
20882 && !(it->line_wrap == TRUNCATE
20883 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20884 {
20885 eassert (FRAME_WINDOW_P (it->f));
20886 row->x = it->last_visible_x - new_x;
20887 }
20888 }
20889 else
20890 {
20891 /* Glyph is completely off the left margin of the
20892 window. This should not happen because of the
20893 move_it_in_display_line at the start of this
20894 function, unless the text display area of the
20895 window is empty. */
20896 eassert (it->first_visible_x <= it->last_visible_x);
20897 }
20898 }
20899 /* Even if this display element produced no glyphs at all,
20900 we want to record its position. */
20901 if (it->bidi_p && nglyphs == 0)
20902 RECORD_MAX_MIN_POS (it);
20903
20904 row->ascent = max (row->ascent, it->max_ascent);
20905 row->height = max (row->height, it->max_ascent + it->max_descent);
20906 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20907 row->phys_height = max (row->phys_height,
20908 it->max_phys_ascent + it->max_phys_descent);
20909 row->extra_line_spacing = max (row->extra_line_spacing,
20910 it->max_extra_line_spacing);
20911
20912 /* End of this display line if row is continued. */
20913 if (row->continued_p || row->ends_at_zv_p)
20914 break;
20915 }
20916
20917 at_end_of_line:
20918 /* Is this a line end? If yes, we're also done, after making
20919 sure that a non-default face is extended up to the right
20920 margin of the window. */
20921 if (ITERATOR_AT_END_OF_LINE_P (it))
20922 {
20923 int used_before = row->used[TEXT_AREA];
20924
20925 row->ends_in_newline_from_string_p = STRINGP (it->object);
20926
20927 /* Add a space at the end of the line that is used to
20928 display the cursor there. */
20929 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20930 append_space_for_newline (it, false);
20931
20932 /* Extend the face to the end of the line. */
20933 extend_face_to_end_of_line (it);
20934
20935 /* Make sure we have the position. */
20936 if (used_before == 0)
20937 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20938
20939 /* Record the position of the newline, for use in
20940 find_row_edges. */
20941 it->eol_pos = it->current.pos;
20942
20943 /* Consume the line end. This skips over invisible lines. */
20944 set_iterator_to_next (it, true);
20945 it->continuation_lines_width = 0;
20946 break;
20947 }
20948
20949 /* Proceed with next display element. Note that this skips
20950 over lines invisible because of selective display. */
20951 set_iterator_to_next (it, true);
20952
20953 /* If we truncate lines, we are done when the last displayed
20954 glyphs reach past the right margin of the window. */
20955 if (it->line_wrap == TRUNCATE
20956 && ((FRAME_WINDOW_P (it->f)
20957 /* Images are preprocessed in produce_image_glyph such
20958 that they are cropped at the right edge of the
20959 window, so an image glyph will always end exactly at
20960 last_visible_x, even if there's no right fringe. */
20961 && ((row->reversed_p
20962 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20963 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20964 || it->what == IT_IMAGE))
20965 ? (it->current_x >= it->last_visible_x)
20966 : (it->current_x > it->last_visible_x)))
20967 {
20968 /* Maybe add truncation glyphs. */
20969 if (!FRAME_WINDOW_P (it->f)
20970 || (row->reversed_p
20971 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20972 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20973 {
20974 int i, n;
20975
20976 if (!row->reversed_p)
20977 {
20978 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20979 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20980 break;
20981 }
20982 else
20983 {
20984 for (i = 0; i < row->used[TEXT_AREA]; i++)
20985 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20986 break;
20987 /* Remove any padding glyphs at the front of ROW, to
20988 make room for the truncation glyphs we will be
20989 adding below. The loop below always inserts at
20990 least one truncation glyph, so also remove the
20991 last glyph added to ROW. */
20992 unproduce_glyphs (it, i + 1);
20993 /* Adjust i for the loop below. */
20994 i = row->used[TEXT_AREA] - (i + 1);
20995 }
20996
20997 /* produce_special_glyphs overwrites the last glyph, so
20998 we don't want that if we want to keep that last
20999 glyph, which means it's an image. */
21000 if (it->current_x > it->last_visible_x)
21001 {
21002 it->current_x = x_before;
21003 if (!FRAME_WINDOW_P (it->f))
21004 {
21005 for (n = row->used[TEXT_AREA]; i < n; ++i)
21006 {
21007 row->used[TEXT_AREA] = i;
21008 produce_special_glyphs (it, IT_TRUNCATION);
21009 }
21010 }
21011 else
21012 {
21013 row->used[TEXT_AREA] = i;
21014 produce_special_glyphs (it, IT_TRUNCATION);
21015 }
21016 it->hpos = hpos_before;
21017 }
21018 }
21019 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21020 {
21021 /* Don't truncate if we can overflow newline into fringe. */
21022 if (!get_next_display_element (it))
21023 {
21024 it->continuation_lines_width = 0;
21025 row->ends_at_zv_p = true;
21026 row->exact_window_width_line_p = true;
21027 break;
21028 }
21029 if (ITERATOR_AT_END_OF_LINE_P (it))
21030 {
21031 row->exact_window_width_line_p = true;
21032 goto at_end_of_line;
21033 }
21034 it->current_x = x_before;
21035 it->hpos = hpos_before;
21036 }
21037
21038 row->truncated_on_right_p = true;
21039 it->continuation_lines_width = 0;
21040 reseat_at_next_visible_line_start (it, false);
21041 /* We insist below that IT's position be at ZV because in
21042 bidi-reordered lines the character at visible line start
21043 might not be the character that follows the newline in
21044 the logical order. */
21045 if (IT_BYTEPOS (*it) > BEG_BYTE)
21046 row->ends_at_zv_p =
21047 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21048 else
21049 row->ends_at_zv_p = false;
21050 break;
21051 }
21052 }
21053
21054 if (wrap_data)
21055 bidi_unshelve_cache (wrap_data, true);
21056
21057 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21058 at the left window margin. */
21059 if (it->first_visible_x
21060 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21061 {
21062 if (!FRAME_WINDOW_P (it->f)
21063 || (((row->reversed_p
21064 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21065 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21066 /* Don't let insert_left_trunc_glyphs overwrite the
21067 first glyph of the row if it is an image. */
21068 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21069 insert_left_trunc_glyphs (it);
21070 row->truncated_on_left_p = true;
21071 }
21072
21073 /* Remember the position at which this line ends.
21074
21075 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21076 cannot be before the call to find_row_edges below, since that is
21077 where these positions are determined. */
21078 row->end = it->current;
21079 if (!it->bidi_p)
21080 {
21081 row->minpos = row->start.pos;
21082 row->maxpos = row->end.pos;
21083 }
21084 else
21085 {
21086 /* ROW->minpos and ROW->maxpos must be the smallest and
21087 `1 + the largest' buffer positions in ROW. But if ROW was
21088 bidi-reordered, these two positions can be anywhere in the
21089 row, so we must determine them now. */
21090 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21091 }
21092
21093 /* If the start of this line is the overlay arrow-position, then
21094 mark this glyph row as the one containing the overlay arrow.
21095 This is clearly a mess with variable size fonts. It would be
21096 better to let it be displayed like cursors under X. */
21097 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21098 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21099 !NILP (overlay_arrow_string)))
21100 {
21101 /* Overlay arrow in window redisplay is a fringe bitmap. */
21102 if (STRINGP (overlay_arrow_string))
21103 {
21104 struct glyph_row *arrow_row
21105 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21106 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21107 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21108 struct glyph *p = row->glyphs[TEXT_AREA];
21109 struct glyph *p2, *end;
21110
21111 /* Copy the arrow glyphs. */
21112 while (glyph < arrow_end)
21113 *p++ = *glyph++;
21114
21115 /* Throw away padding glyphs. */
21116 p2 = p;
21117 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21118 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21119 ++p2;
21120 if (p2 > p)
21121 {
21122 while (p2 < end)
21123 *p++ = *p2++;
21124 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21125 }
21126 }
21127 else
21128 {
21129 eassert (INTEGERP (overlay_arrow_string));
21130 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21131 }
21132 overlay_arrow_seen = true;
21133 }
21134
21135 /* Highlight trailing whitespace. */
21136 if (!NILP (Vshow_trailing_whitespace))
21137 highlight_trailing_whitespace (it->f, it->glyph_row);
21138
21139 /* Compute pixel dimensions of this line. */
21140 compute_line_metrics (it);
21141
21142 /* Implementation note: No changes in the glyphs of ROW or in their
21143 faces can be done past this point, because compute_line_metrics
21144 computes ROW's hash value and stores it within the glyph_row
21145 structure. */
21146
21147 /* Record whether this row ends inside an ellipsis. */
21148 row->ends_in_ellipsis_p
21149 = (it->method == GET_FROM_DISPLAY_VECTOR
21150 && it->ellipsis_p);
21151
21152 /* Save fringe bitmaps in this row. */
21153 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
21154 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
21155 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
21156 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
21157
21158 it->left_user_fringe_bitmap = 0;
21159 it->left_user_fringe_face_id = 0;
21160 it->right_user_fringe_bitmap = 0;
21161 it->right_user_fringe_face_id = 0;
21162
21163 /* Maybe set the cursor. */
21164 cvpos = it->w->cursor.vpos;
21165 if ((cvpos < 0
21166 /* In bidi-reordered rows, keep checking for proper cursor
21167 position even if one has been found already, because buffer
21168 positions in such rows change non-linearly with ROW->VPOS,
21169 when a line is continued. One exception: when we are at ZV,
21170 display cursor on the first suitable glyph row, since all
21171 the empty rows after that also have their position set to ZV. */
21172 /* FIXME: Revisit this when glyph ``spilling'' in continuation
21173 lines' rows is implemented for bidi-reordered rows. */
21174 || (it->bidi_p
21175 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
21176 && PT >= MATRIX_ROW_START_CHARPOS (row)
21177 && PT <= MATRIX_ROW_END_CHARPOS (row)
21178 && cursor_row_p (row))
21179 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21180
21181 /* Prepare for the next line. This line starts horizontally at (X
21182 HPOS) = (0 0). Vertical positions are incremented. As a
21183 convenience for the caller, IT->glyph_row is set to the next
21184 row to be used. */
21185 it->current_x = it->hpos = 0;
21186 it->current_y += row->height;
21187 SET_TEXT_POS (it->eol_pos, 0, 0);
21188 ++it->vpos;
21189 ++it->glyph_row;
21190 /* The next row should by default use the same value of the
21191 reversed_p flag as this one. set_iterator_to_next decides when
21192 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21193 the flag accordingly. */
21194 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21195 it->glyph_row->reversed_p = row->reversed_p;
21196 it->start = row->end;
21197 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21198
21199 #undef RECORD_MAX_MIN_POS
21200 }
21201
21202 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21203 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21204 doc: /* Return paragraph direction at point in BUFFER.
21205 Value is either `left-to-right' or `right-to-left'.
21206 If BUFFER is omitted or nil, it defaults to the current buffer.
21207
21208 Paragraph direction determines how the text in the paragraph is displayed.
21209 In left-to-right paragraphs, text begins at the left margin of the window
21210 and the reading direction is generally left to right. In right-to-left
21211 paragraphs, text begins at the right margin and is read from right to left.
21212
21213 See also `bidi-paragraph-direction'. */)
21214 (Lisp_Object buffer)
21215 {
21216 struct buffer *buf = current_buffer;
21217 struct buffer *old = buf;
21218
21219 if (! NILP (buffer))
21220 {
21221 CHECK_BUFFER (buffer);
21222 buf = XBUFFER (buffer);
21223 }
21224
21225 if (NILP (BVAR (buf, bidi_display_reordering))
21226 || NILP (BVAR (buf, enable_multibyte_characters))
21227 /* When we are loading loadup.el, the character property tables
21228 needed for bidi iteration are not yet available. */
21229 || !NILP (Vpurify_flag))
21230 return Qleft_to_right;
21231 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21232 return BVAR (buf, bidi_paragraph_direction);
21233 else
21234 {
21235 /* Determine the direction from buffer text. We could try to
21236 use current_matrix if it is up to date, but this seems fast
21237 enough as it is. */
21238 struct bidi_it itb;
21239 ptrdiff_t pos = BUF_PT (buf);
21240 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21241 int c;
21242 void *itb_data = bidi_shelve_cache ();
21243
21244 set_buffer_temp (buf);
21245 /* bidi_paragraph_init finds the base direction of the paragraph
21246 by searching forward from paragraph start. We need the base
21247 direction of the current or _previous_ paragraph, so we need
21248 to make sure we are within that paragraph. To that end, find
21249 the previous non-empty line. */
21250 if (pos >= ZV && pos > BEGV)
21251 DEC_BOTH (pos, bytepos);
21252 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21253 if (fast_looking_at (trailing_white_space,
21254 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21255 {
21256 while ((c = FETCH_BYTE (bytepos)) == '\n'
21257 || c == ' ' || c == '\t' || c == '\f')
21258 {
21259 if (bytepos <= BEGV_BYTE)
21260 break;
21261 bytepos--;
21262 pos--;
21263 }
21264 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21265 bytepos--;
21266 }
21267 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21268 itb.paragraph_dir = NEUTRAL_DIR;
21269 itb.string.s = NULL;
21270 itb.string.lstring = Qnil;
21271 itb.string.bufpos = 0;
21272 itb.string.from_disp_str = false;
21273 itb.string.unibyte = false;
21274 /* We have no window to use here for ignoring window-specific
21275 overlays. Using NULL for window pointer will cause
21276 compute_display_string_pos to use the current buffer. */
21277 itb.w = NULL;
21278 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21279 bidi_unshelve_cache (itb_data, false);
21280 set_buffer_temp (old);
21281 switch (itb.paragraph_dir)
21282 {
21283 case L2R:
21284 return Qleft_to_right;
21285 break;
21286 case R2L:
21287 return Qright_to_left;
21288 break;
21289 default:
21290 emacs_abort ();
21291 }
21292 }
21293 }
21294
21295 DEFUN ("bidi-find-overridden-directionality",
21296 Fbidi_find_overridden_directionality,
21297 Sbidi_find_overridden_directionality, 2, 3, 0,
21298 doc: /* Return position between FROM and TO where directionality was overridden.
21299
21300 This function returns the first character position in the specified
21301 region of OBJECT where there is a character whose `bidi-class' property
21302 is `L', but which was forced to display as `R' by a directional
21303 override, and likewise with characters whose `bidi-class' is `R'
21304 or `AL' that were forced to display as `L'.
21305
21306 If no such character is found, the function returns nil.
21307
21308 OBJECT is a Lisp string or buffer to search for overridden
21309 directionality, and defaults to the current buffer if nil or omitted.
21310 OBJECT can also be a window, in which case the function will search
21311 the buffer displayed in that window. Passing the window instead of
21312 a buffer is preferable when the buffer is displayed in some window,
21313 because this function will then be able to correctly account for
21314 window-specific overlays, which can affect the results.
21315
21316 Strong directional characters `L', `R', and `AL' can have their
21317 intrinsic directionality overridden by directional override
21318 control characters RLO (u+202e) and LRO (u+202d). See the
21319 function `get-char-code-property' for a way to inquire about
21320 the `bidi-class' property of a character. */)
21321 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21322 {
21323 struct buffer *buf = current_buffer;
21324 struct buffer *old = buf;
21325 struct window *w = NULL;
21326 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21327 struct bidi_it itb;
21328 ptrdiff_t from_pos, to_pos, from_bpos;
21329 void *itb_data;
21330
21331 if (!NILP (object))
21332 {
21333 if (BUFFERP (object))
21334 buf = XBUFFER (object);
21335 else if (WINDOWP (object))
21336 {
21337 w = decode_live_window (object);
21338 buf = XBUFFER (w->contents);
21339 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21340 }
21341 else
21342 CHECK_STRING (object);
21343 }
21344
21345 if (STRINGP (object))
21346 {
21347 /* Characters in unibyte strings are always treated by bidi.c as
21348 strong LTR. */
21349 if (!STRING_MULTIBYTE (object)
21350 /* When we are loading loadup.el, the character property
21351 tables needed for bidi iteration are not yet
21352 available. */
21353 || !NILP (Vpurify_flag))
21354 return Qnil;
21355
21356 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21357 if (from_pos >= SCHARS (object))
21358 return Qnil;
21359
21360 /* Set up the bidi iterator. */
21361 itb_data = bidi_shelve_cache ();
21362 itb.paragraph_dir = NEUTRAL_DIR;
21363 itb.string.lstring = object;
21364 itb.string.s = NULL;
21365 itb.string.schars = SCHARS (object);
21366 itb.string.bufpos = 0;
21367 itb.string.from_disp_str = false;
21368 itb.string.unibyte = false;
21369 itb.w = w;
21370 bidi_init_it (0, 0, frame_window_p, &itb);
21371 }
21372 else
21373 {
21374 /* Nothing this fancy can happen in unibyte buffers, or in a
21375 buffer that disabled reordering, or if FROM is at EOB. */
21376 if (NILP (BVAR (buf, bidi_display_reordering))
21377 || NILP (BVAR (buf, enable_multibyte_characters))
21378 /* When we are loading loadup.el, the character property
21379 tables needed for bidi iteration are not yet
21380 available. */
21381 || !NILP (Vpurify_flag))
21382 return Qnil;
21383
21384 set_buffer_temp (buf);
21385 validate_region (&from, &to);
21386 from_pos = XINT (from);
21387 to_pos = XINT (to);
21388 if (from_pos >= ZV)
21389 return Qnil;
21390
21391 /* Set up the bidi iterator. */
21392 itb_data = bidi_shelve_cache ();
21393 from_bpos = CHAR_TO_BYTE (from_pos);
21394 if (from_pos == BEGV)
21395 {
21396 itb.charpos = BEGV;
21397 itb.bytepos = BEGV_BYTE;
21398 }
21399 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21400 {
21401 itb.charpos = from_pos;
21402 itb.bytepos = from_bpos;
21403 }
21404 else
21405 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21406 -1, &itb.bytepos);
21407 itb.paragraph_dir = NEUTRAL_DIR;
21408 itb.string.s = NULL;
21409 itb.string.lstring = Qnil;
21410 itb.string.bufpos = 0;
21411 itb.string.from_disp_str = false;
21412 itb.string.unibyte = false;
21413 itb.w = w;
21414 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21415 }
21416
21417 ptrdiff_t found;
21418 do {
21419 /* For the purposes of this function, the actual base direction of
21420 the paragraph doesn't matter, so just set it to L2R. */
21421 bidi_paragraph_init (L2R, &itb, false);
21422 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21423 ;
21424 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21425
21426 bidi_unshelve_cache (itb_data, false);
21427 set_buffer_temp (old);
21428
21429 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21430 }
21431
21432 DEFUN ("move-point-visually", Fmove_point_visually,
21433 Smove_point_visually, 1, 1, 0,
21434 doc: /* Move point in the visual order in the specified DIRECTION.
21435 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21436 left.
21437
21438 Value is the new character position of point. */)
21439 (Lisp_Object direction)
21440 {
21441 struct window *w = XWINDOW (selected_window);
21442 struct buffer *b = XBUFFER (w->contents);
21443 struct glyph_row *row;
21444 int dir;
21445 Lisp_Object paragraph_dir;
21446
21447 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21448 (!(ROW)->continued_p \
21449 && NILP ((GLYPH)->object) \
21450 && (GLYPH)->type == CHAR_GLYPH \
21451 && (GLYPH)->u.ch == ' ' \
21452 && (GLYPH)->charpos >= 0 \
21453 && !(GLYPH)->avoid_cursor_p)
21454
21455 CHECK_NUMBER (direction);
21456 dir = XINT (direction);
21457 if (dir > 0)
21458 dir = 1;
21459 else
21460 dir = -1;
21461
21462 /* If current matrix is up-to-date, we can use the information
21463 recorded in the glyphs, at least as long as the goal is on the
21464 screen. */
21465 if (w->window_end_valid
21466 && !windows_or_buffers_changed
21467 && b
21468 && !b->clip_changed
21469 && !b->prevent_redisplay_optimizations_p
21470 && !window_outdated (w)
21471 /* We rely below on the cursor coordinates to be up to date, but
21472 we cannot trust them if some command moved point since the
21473 last complete redisplay. */
21474 && w->last_point == BUF_PT (b)
21475 && w->cursor.vpos >= 0
21476 && w->cursor.vpos < w->current_matrix->nrows
21477 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21478 {
21479 struct glyph *g = row->glyphs[TEXT_AREA];
21480 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21481 struct glyph *gpt = g + w->cursor.hpos;
21482
21483 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21484 {
21485 if (BUFFERP (g->object) && g->charpos != PT)
21486 {
21487 SET_PT (g->charpos);
21488 w->cursor.vpos = -1;
21489 return make_number (PT);
21490 }
21491 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21492 {
21493 ptrdiff_t new_pos;
21494
21495 if (BUFFERP (gpt->object))
21496 {
21497 new_pos = PT;
21498 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21499 new_pos += (row->reversed_p ? -dir : dir);
21500 else
21501 new_pos -= (row->reversed_p ? -dir : dir);
21502 }
21503 else if (BUFFERP (g->object))
21504 new_pos = g->charpos;
21505 else
21506 break;
21507 SET_PT (new_pos);
21508 w->cursor.vpos = -1;
21509 return make_number (PT);
21510 }
21511 else if (ROW_GLYPH_NEWLINE_P (row, g))
21512 {
21513 /* Glyphs inserted at the end of a non-empty line for
21514 positioning the cursor have zero charpos, so we must
21515 deduce the value of point by other means. */
21516 if (g->charpos > 0)
21517 SET_PT (g->charpos);
21518 else if (row->ends_at_zv_p && PT != ZV)
21519 SET_PT (ZV);
21520 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21521 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21522 else
21523 break;
21524 w->cursor.vpos = -1;
21525 return make_number (PT);
21526 }
21527 }
21528 if (g == e || NILP (g->object))
21529 {
21530 if (row->truncated_on_left_p || row->truncated_on_right_p)
21531 goto simulate_display;
21532 if (!row->reversed_p)
21533 row += dir;
21534 else
21535 row -= dir;
21536 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21537 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21538 goto simulate_display;
21539
21540 if (dir > 0)
21541 {
21542 if (row->reversed_p && !row->continued_p)
21543 {
21544 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21545 w->cursor.vpos = -1;
21546 return make_number (PT);
21547 }
21548 g = row->glyphs[TEXT_AREA];
21549 e = g + row->used[TEXT_AREA];
21550 for ( ; g < e; g++)
21551 {
21552 if (BUFFERP (g->object)
21553 /* Empty lines have only one glyph, which stands
21554 for the newline, and whose charpos is the
21555 buffer position of the newline. */
21556 || ROW_GLYPH_NEWLINE_P (row, g)
21557 /* When the buffer ends in a newline, the line at
21558 EOB also has one glyph, but its charpos is -1. */
21559 || (row->ends_at_zv_p
21560 && !row->reversed_p
21561 && NILP (g->object)
21562 && g->type == CHAR_GLYPH
21563 && g->u.ch == ' '))
21564 {
21565 if (g->charpos > 0)
21566 SET_PT (g->charpos);
21567 else if (!row->reversed_p
21568 && row->ends_at_zv_p
21569 && PT != ZV)
21570 SET_PT (ZV);
21571 else
21572 continue;
21573 w->cursor.vpos = -1;
21574 return make_number (PT);
21575 }
21576 }
21577 }
21578 else
21579 {
21580 if (!row->reversed_p && !row->continued_p)
21581 {
21582 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21583 w->cursor.vpos = -1;
21584 return make_number (PT);
21585 }
21586 e = row->glyphs[TEXT_AREA];
21587 g = e + row->used[TEXT_AREA] - 1;
21588 for ( ; g >= e; g--)
21589 {
21590 if (BUFFERP (g->object)
21591 || (ROW_GLYPH_NEWLINE_P (row, g)
21592 && g->charpos > 0)
21593 /* Empty R2L lines on GUI frames have the buffer
21594 position of the newline stored in the stretch
21595 glyph. */
21596 || g->type == STRETCH_GLYPH
21597 || (row->ends_at_zv_p
21598 && row->reversed_p
21599 && NILP (g->object)
21600 && g->type == CHAR_GLYPH
21601 && g->u.ch == ' '))
21602 {
21603 if (g->charpos > 0)
21604 SET_PT (g->charpos);
21605 else if (row->reversed_p
21606 && row->ends_at_zv_p
21607 && PT != ZV)
21608 SET_PT (ZV);
21609 else
21610 continue;
21611 w->cursor.vpos = -1;
21612 return make_number (PT);
21613 }
21614 }
21615 }
21616 }
21617 }
21618
21619 simulate_display:
21620
21621 /* If we wind up here, we failed to move by using the glyphs, so we
21622 need to simulate display instead. */
21623
21624 if (b)
21625 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21626 else
21627 paragraph_dir = Qleft_to_right;
21628 if (EQ (paragraph_dir, Qright_to_left))
21629 dir = -dir;
21630 if (PT <= BEGV && dir < 0)
21631 xsignal0 (Qbeginning_of_buffer);
21632 else if (PT >= ZV && dir > 0)
21633 xsignal0 (Qend_of_buffer);
21634 else
21635 {
21636 struct text_pos pt;
21637 struct it it;
21638 int pt_x, target_x, pixel_width, pt_vpos;
21639 bool at_eol_p;
21640 bool overshoot_expected = false;
21641 bool target_is_eol_p = false;
21642
21643 /* Setup the arena. */
21644 SET_TEXT_POS (pt, PT, PT_BYTE);
21645 start_display (&it, w, pt);
21646 /* When lines are truncated, we could be called with point
21647 outside of the windows edges, in which case move_it_*
21648 functions either prematurely stop at window's edge or jump to
21649 the next screen line, whereas we rely below on our ability to
21650 reach point, in order to start from its X coordinate. So we
21651 need to disregard the window's horizontal extent in that case. */
21652 if (it.line_wrap == TRUNCATE)
21653 it.last_visible_x = INFINITY;
21654
21655 if (it.cmp_it.id < 0
21656 && it.method == GET_FROM_STRING
21657 && it.area == TEXT_AREA
21658 && it.string_from_display_prop_p
21659 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21660 overshoot_expected = true;
21661
21662 /* Find the X coordinate of point. We start from the beginning
21663 of this or previous line to make sure we are before point in
21664 the logical order (since the move_it_* functions can only
21665 move forward). */
21666 reseat:
21667 reseat_at_previous_visible_line_start (&it);
21668 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21669 if (IT_CHARPOS (it) != PT)
21670 {
21671 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21672 -1, -1, -1, MOVE_TO_POS);
21673 /* If we missed point because the character there is
21674 displayed out of a display vector that has more than one
21675 glyph, retry expecting overshoot. */
21676 if (it.method == GET_FROM_DISPLAY_VECTOR
21677 && it.current.dpvec_index > 0
21678 && !overshoot_expected)
21679 {
21680 overshoot_expected = true;
21681 goto reseat;
21682 }
21683 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21684 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21685 }
21686 pt_x = it.current_x;
21687 pt_vpos = it.vpos;
21688 if (dir > 0 || overshoot_expected)
21689 {
21690 struct glyph_row *row = it.glyph_row;
21691
21692 /* When point is at beginning of line, we don't have
21693 information about the glyph there loaded into struct
21694 it. Calling get_next_display_element fixes that. */
21695 if (pt_x == 0)
21696 get_next_display_element (&it);
21697 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21698 it.glyph_row = NULL;
21699 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21700 it.glyph_row = row;
21701 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21702 it, lest it will become out of sync with it's buffer
21703 position. */
21704 it.current_x = pt_x;
21705 }
21706 else
21707 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21708 pixel_width = it.pixel_width;
21709 if (overshoot_expected && at_eol_p)
21710 pixel_width = 0;
21711 else if (pixel_width <= 0)
21712 pixel_width = 1;
21713
21714 /* If there's a display string (or something similar) at point,
21715 we are actually at the glyph to the left of point, so we need
21716 to correct the X coordinate. */
21717 if (overshoot_expected)
21718 {
21719 if (it.bidi_p)
21720 pt_x += pixel_width * it.bidi_it.scan_dir;
21721 else
21722 pt_x += pixel_width;
21723 }
21724
21725 /* Compute target X coordinate, either to the left or to the
21726 right of point. On TTY frames, all characters have the same
21727 pixel width of 1, so we can use that. On GUI frames we don't
21728 have an easy way of getting at the pixel width of the
21729 character to the left of point, so we use a different method
21730 of getting to that place. */
21731 if (dir > 0)
21732 target_x = pt_x + pixel_width;
21733 else
21734 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21735
21736 /* Target X coordinate could be one line above or below the line
21737 of point, in which case we need to adjust the target X
21738 coordinate. Also, if moving to the left, we need to begin at
21739 the left edge of the point's screen line. */
21740 if (dir < 0)
21741 {
21742 if (pt_x > 0)
21743 {
21744 start_display (&it, w, pt);
21745 if (it.line_wrap == TRUNCATE)
21746 it.last_visible_x = INFINITY;
21747 reseat_at_previous_visible_line_start (&it);
21748 it.current_x = it.current_y = it.hpos = 0;
21749 if (pt_vpos != 0)
21750 move_it_by_lines (&it, pt_vpos);
21751 }
21752 else
21753 {
21754 move_it_by_lines (&it, -1);
21755 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21756 target_is_eol_p = true;
21757 /* Under word-wrap, we don't know the x coordinate of
21758 the last character displayed on the previous line,
21759 which immediately precedes the wrap point. To find
21760 out its x coordinate, we try moving to the right
21761 margin of the window, which will stop at the wrap
21762 point, and then reset target_x to point at the
21763 character that precedes the wrap point. This is not
21764 needed on GUI frames, because (see below) there we
21765 move from the left margin one grapheme cluster at a
21766 time, and stop when we hit the wrap point. */
21767 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21768 {
21769 void *it_data = NULL;
21770 struct it it2;
21771
21772 SAVE_IT (it2, it, it_data);
21773 move_it_in_display_line_to (&it, ZV, target_x,
21774 MOVE_TO_POS | MOVE_TO_X);
21775 /* If we arrived at target_x, that _is_ the last
21776 character on the previous line. */
21777 if (it.current_x != target_x)
21778 target_x = it.current_x - 1;
21779 RESTORE_IT (&it, &it2, it_data);
21780 }
21781 }
21782 }
21783 else
21784 {
21785 if (at_eol_p
21786 || (target_x >= it.last_visible_x
21787 && it.line_wrap != TRUNCATE))
21788 {
21789 if (pt_x > 0)
21790 move_it_by_lines (&it, 0);
21791 move_it_by_lines (&it, 1);
21792 target_x = 0;
21793 }
21794 }
21795
21796 /* Move to the target X coordinate. */
21797 #ifdef HAVE_WINDOW_SYSTEM
21798 /* On GUI frames, as we don't know the X coordinate of the
21799 character to the left of point, moving point to the left
21800 requires walking, one grapheme cluster at a time, until we
21801 find ourself at a place immediately to the left of the
21802 character at point. */
21803 if (FRAME_WINDOW_P (it.f) && dir < 0)
21804 {
21805 struct text_pos new_pos;
21806 enum move_it_result rc = MOVE_X_REACHED;
21807
21808 if (it.current_x == 0)
21809 get_next_display_element (&it);
21810 if (it.what == IT_COMPOSITION)
21811 {
21812 new_pos.charpos = it.cmp_it.charpos;
21813 new_pos.bytepos = -1;
21814 }
21815 else
21816 new_pos = it.current.pos;
21817
21818 while (it.current_x + it.pixel_width <= target_x
21819 && (rc == MOVE_X_REACHED
21820 /* Under word-wrap, move_it_in_display_line_to
21821 stops at correct coordinates, but sometimes
21822 returns MOVE_POS_MATCH_OR_ZV. */
21823 || (it.line_wrap == WORD_WRAP
21824 && rc == MOVE_POS_MATCH_OR_ZV)))
21825 {
21826 int new_x = it.current_x + it.pixel_width;
21827
21828 /* For composed characters, we want the position of the
21829 first character in the grapheme cluster (usually, the
21830 composition's base character), whereas it.current
21831 might give us the position of the _last_ one, e.g. if
21832 the composition is rendered in reverse due to bidi
21833 reordering. */
21834 if (it.what == IT_COMPOSITION)
21835 {
21836 new_pos.charpos = it.cmp_it.charpos;
21837 new_pos.bytepos = -1;
21838 }
21839 else
21840 new_pos = it.current.pos;
21841 if (new_x == it.current_x)
21842 new_x++;
21843 rc = move_it_in_display_line_to (&it, ZV, new_x,
21844 MOVE_TO_POS | MOVE_TO_X);
21845 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21846 break;
21847 }
21848 /* The previous position we saw in the loop is the one we
21849 want. */
21850 if (new_pos.bytepos == -1)
21851 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21852 it.current.pos = new_pos;
21853 }
21854 else
21855 #endif
21856 if (it.current_x != target_x)
21857 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21858
21859 /* If we ended up in a display string that covers point, move to
21860 buffer position to the right in the visual order. */
21861 if (dir > 0)
21862 {
21863 while (IT_CHARPOS (it) == PT)
21864 {
21865 set_iterator_to_next (&it, false);
21866 if (!get_next_display_element (&it))
21867 break;
21868 }
21869 }
21870
21871 /* Move point to that position. */
21872 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21873 }
21874
21875 return make_number (PT);
21876
21877 #undef ROW_GLYPH_NEWLINE_P
21878 }
21879
21880 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21881 Sbidi_resolved_levels, 0, 1, 0,
21882 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21883
21884 The resolved levels are produced by the Emacs bidi reordering engine
21885 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21886 read the Unicode Standard Annex 9 (UAX#9) for background information
21887 about these levels.
21888
21889 VPOS is the zero-based number of the current window's screen line
21890 for which to produce the resolved levels. If VPOS is nil or omitted,
21891 it defaults to the screen line of point. If the window displays a
21892 header line, VPOS of zero will report on the header line, and first
21893 line of text in the window will have VPOS of 1.
21894
21895 Value is an array of resolved levels, indexed by glyph number.
21896 Glyphs are numbered from zero starting from the beginning of the
21897 screen line, i.e. the left edge of the window for left-to-right lines
21898 and from the right edge for right-to-left lines. The resolved levels
21899 are produced only for the window's text area; text in display margins
21900 is not included.
21901
21902 If the selected window's display is not up-to-date, or if the specified
21903 screen line does not display text, this function returns nil. It is
21904 highly recommended to bind this function to some simple key, like F8,
21905 in order to avoid these problems.
21906
21907 This function exists mainly for testing the correctness of the
21908 Emacs UBA implementation, in particular with the test suite. */)
21909 (Lisp_Object vpos)
21910 {
21911 struct window *w = XWINDOW (selected_window);
21912 struct buffer *b = XBUFFER (w->contents);
21913 int nrow;
21914 struct glyph_row *row;
21915
21916 if (NILP (vpos))
21917 {
21918 int d1, d2, d3, d4, d5;
21919
21920 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21921 }
21922 else
21923 {
21924 CHECK_NUMBER_COERCE_MARKER (vpos);
21925 nrow = XINT (vpos);
21926 }
21927
21928 /* We require up-to-date glyph matrix for this window. */
21929 if (w->window_end_valid
21930 && !windows_or_buffers_changed
21931 && b
21932 && !b->clip_changed
21933 && !b->prevent_redisplay_optimizations_p
21934 && !window_outdated (w)
21935 && nrow >= 0
21936 && nrow < w->current_matrix->nrows
21937 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21938 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21939 {
21940 struct glyph *g, *e, *g1;
21941 int nglyphs, i;
21942 Lisp_Object levels;
21943
21944 if (!row->reversed_p) /* Left-to-right glyph row. */
21945 {
21946 g = g1 = row->glyphs[TEXT_AREA];
21947 e = g + row->used[TEXT_AREA];
21948
21949 /* Skip over glyphs at the start of the row that was
21950 generated by redisplay for its own needs. */
21951 while (g < e
21952 && NILP (g->object)
21953 && g->charpos < 0)
21954 g++;
21955 g1 = g;
21956
21957 /* Count the "interesting" glyphs in this row. */
21958 for (nglyphs = 0; g < e && !NILP (g->object); g++)
21959 nglyphs++;
21960
21961 /* Create and fill the array. */
21962 levels = make_uninit_vector (nglyphs);
21963 for (i = 0; g1 < g; i++, g1++)
21964 ASET (levels, i, make_number (g1->resolved_level));
21965 }
21966 else /* Right-to-left glyph row. */
21967 {
21968 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21969 e = row->glyphs[TEXT_AREA] - 1;
21970 while (g > e
21971 && NILP (g->object)
21972 && g->charpos < 0)
21973 g--;
21974 g1 = g;
21975 for (nglyphs = 0; g > e && !NILP (g->object); g--)
21976 nglyphs++;
21977 levels = make_uninit_vector (nglyphs);
21978 for (i = 0; g1 > g; i++, g1--)
21979 ASET (levels, i, make_number (g1->resolved_level));
21980 }
21981 return levels;
21982 }
21983 else
21984 return Qnil;
21985 }
21986
21987
21988 \f
21989 /***********************************************************************
21990 Menu Bar
21991 ***********************************************************************/
21992
21993 /* Redisplay the menu bar in the frame for window W.
21994
21995 The menu bar of X frames that don't have X toolkit support is
21996 displayed in a special window W->frame->menu_bar_window.
21997
21998 The menu bar of terminal frames is treated specially as far as
21999 glyph matrices are concerned. Menu bar lines are not part of
22000 windows, so the update is done directly on the frame matrix rows
22001 for the menu bar. */
22002
22003 static void
22004 display_menu_bar (struct window *w)
22005 {
22006 struct frame *f = XFRAME (WINDOW_FRAME (w));
22007 struct it it;
22008 Lisp_Object items;
22009 int i;
22010
22011 /* Don't do all this for graphical frames. */
22012 #ifdef HAVE_NTGUI
22013 if (FRAME_W32_P (f))
22014 return;
22015 #endif
22016 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22017 if (FRAME_X_P (f))
22018 return;
22019 #endif
22020
22021 #ifdef HAVE_NS
22022 if (FRAME_NS_P (f))
22023 return;
22024 #endif /* HAVE_NS */
22025
22026 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22027 eassert (!FRAME_WINDOW_P (f));
22028 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22029 it.first_visible_x = 0;
22030 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22031 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22032 if (FRAME_WINDOW_P (f))
22033 {
22034 /* Menu bar lines are displayed in the desired matrix of the
22035 dummy window menu_bar_window. */
22036 struct window *menu_w;
22037 menu_w = XWINDOW (f->menu_bar_window);
22038 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22039 MENU_FACE_ID);
22040 it.first_visible_x = 0;
22041 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22042 }
22043 else
22044 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22045 {
22046 /* This is a TTY frame, i.e. character hpos/vpos are used as
22047 pixel x/y. */
22048 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22049 MENU_FACE_ID);
22050 it.first_visible_x = 0;
22051 it.last_visible_x = FRAME_COLS (f);
22052 }
22053
22054 /* FIXME: This should be controlled by a user option. See the
22055 comments in redisplay_tool_bar and display_mode_line about
22056 this. */
22057 it.paragraph_embedding = L2R;
22058
22059 /* Clear all rows of the menu bar. */
22060 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22061 {
22062 struct glyph_row *row = it.glyph_row + i;
22063 clear_glyph_row (row);
22064 row->enabled_p = true;
22065 row->full_width_p = true;
22066 row->reversed_p = false;
22067 }
22068
22069 /* Display all items of the menu bar. */
22070 items = FRAME_MENU_BAR_ITEMS (it.f);
22071 for (i = 0; i < ASIZE (items); i += 4)
22072 {
22073 Lisp_Object string;
22074
22075 /* Stop at nil string. */
22076 string = AREF (items, i + 1);
22077 if (NILP (string))
22078 break;
22079
22080 /* Remember where item was displayed. */
22081 ASET (items, i + 3, make_number (it.hpos));
22082
22083 /* Display the item, pad with one space. */
22084 if (it.current_x < it.last_visible_x)
22085 display_string (NULL, string, Qnil, 0, 0, &it,
22086 SCHARS (string) + 1, 0, 0, -1);
22087 }
22088
22089 /* Fill out the line with spaces. */
22090 if (it.current_x < it.last_visible_x)
22091 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22092
22093 /* Compute the total height of the lines. */
22094 compute_line_metrics (&it);
22095 }
22096
22097 /* Deep copy of a glyph row, including the glyphs. */
22098 static void
22099 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22100 {
22101 struct glyph *pointers[1 + LAST_AREA];
22102 int to_used = to->used[TEXT_AREA];
22103
22104 /* Save glyph pointers of TO. */
22105 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22106
22107 /* Do a structure assignment. */
22108 *to = *from;
22109
22110 /* Restore original glyph pointers of TO. */
22111 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22112
22113 /* Copy the glyphs. */
22114 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22115 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22116
22117 /* If we filled only part of the TO row, fill the rest with
22118 space_glyph (which will display as empty space). */
22119 if (to_used > from->used[TEXT_AREA])
22120 fill_up_frame_row_with_spaces (to, to_used);
22121 }
22122
22123 /* Display one menu item on a TTY, by overwriting the glyphs in the
22124 frame F's desired glyph matrix with glyphs produced from the menu
22125 item text. Called from term.c to display TTY drop-down menus one
22126 item at a time.
22127
22128 ITEM_TEXT is the menu item text as a C string.
22129
22130 FACE_ID is the face ID to be used for this menu item. FACE_ID
22131 could specify one of 3 faces: a face for an enabled item, a face
22132 for a disabled item, or a face for a selected item.
22133
22134 X and Y are coordinates of the first glyph in the frame's desired
22135 matrix to be overwritten by the menu item. Since this is a TTY, Y
22136 is the zero-based number of the glyph row and X is the zero-based
22137 glyph number in the row, starting from left, where to start
22138 displaying the item.
22139
22140 SUBMENU means this menu item drops down a submenu, which
22141 should be indicated by displaying a proper visual cue after the
22142 item text. */
22143
22144 void
22145 display_tty_menu_item (const char *item_text, int width, int face_id,
22146 int x, int y, bool submenu)
22147 {
22148 struct it it;
22149 struct frame *f = SELECTED_FRAME ();
22150 struct window *w = XWINDOW (f->selected_window);
22151 struct glyph_row *row;
22152 size_t item_len = strlen (item_text);
22153
22154 eassert (FRAME_TERMCAP_P (f));
22155
22156 /* Don't write beyond the matrix's last row. This can happen for
22157 TTY screens that are not high enough to show the entire menu.
22158 (This is actually a bit of defensive programming, as
22159 tty_menu_display already limits the number of menu items to one
22160 less than the number of screen lines.) */
22161 if (y >= f->desired_matrix->nrows)
22162 return;
22163
22164 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
22165 it.first_visible_x = 0;
22166 it.last_visible_x = FRAME_COLS (f) - 1;
22167 row = it.glyph_row;
22168 /* Start with the row contents from the current matrix. */
22169 deep_copy_glyph_row (row, f->current_matrix->rows + y);
22170 bool saved_width = row->full_width_p;
22171 row->full_width_p = true;
22172 bool saved_reversed = row->reversed_p;
22173 row->reversed_p = false;
22174 row->enabled_p = true;
22175
22176 /* Arrange for the menu item glyphs to start at (X,Y) and have the
22177 desired face. */
22178 eassert (x < f->desired_matrix->matrix_w);
22179 it.current_x = it.hpos = x;
22180 it.current_y = it.vpos = y;
22181 int saved_used = row->used[TEXT_AREA];
22182 bool saved_truncated = row->truncated_on_right_p;
22183 row->used[TEXT_AREA] = x;
22184 it.face_id = face_id;
22185 it.line_wrap = TRUNCATE;
22186
22187 /* FIXME: This should be controlled by a user option. See the
22188 comments in redisplay_tool_bar and display_mode_line about this.
22189 Also, if paragraph_embedding could ever be R2L, changes will be
22190 needed to avoid shifting to the right the row characters in
22191 term.c:append_glyph. */
22192 it.paragraph_embedding = L2R;
22193
22194 /* Pad with a space on the left. */
22195 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22196 width--;
22197 /* Display the menu item, pad with spaces to WIDTH. */
22198 if (submenu)
22199 {
22200 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22201 item_len, 0, FRAME_COLS (f) - 1, -1);
22202 width -= item_len;
22203 /* Indicate with " >" that there's a submenu. */
22204 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22205 FRAME_COLS (f) - 1, -1);
22206 }
22207 else
22208 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22209 width, 0, FRAME_COLS (f) - 1, -1);
22210
22211 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22212 row->truncated_on_right_p = saved_truncated;
22213 row->hash = row_hash (row);
22214 row->full_width_p = saved_width;
22215 row->reversed_p = saved_reversed;
22216 }
22217 \f
22218 /***********************************************************************
22219 Mode Line
22220 ***********************************************************************/
22221
22222 /* Redisplay mode lines in the window tree whose root is WINDOW.
22223 If FORCE, redisplay mode lines unconditionally.
22224 Otherwise, redisplay only mode lines that are garbaged. Value is
22225 the number of windows whose mode lines were redisplayed. */
22226
22227 static int
22228 redisplay_mode_lines (Lisp_Object window, bool force)
22229 {
22230 int nwindows = 0;
22231
22232 while (!NILP (window))
22233 {
22234 struct window *w = XWINDOW (window);
22235
22236 if (WINDOWP (w->contents))
22237 nwindows += redisplay_mode_lines (w->contents, force);
22238 else if (force
22239 || FRAME_GARBAGED_P (XFRAME (w->frame))
22240 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22241 {
22242 struct text_pos lpoint;
22243 struct buffer *old = current_buffer;
22244
22245 /* Set the window's buffer for the mode line display. */
22246 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22247 set_buffer_internal_1 (XBUFFER (w->contents));
22248
22249 /* Point refers normally to the selected window. For any
22250 other window, set up appropriate value. */
22251 if (!EQ (window, selected_window))
22252 {
22253 struct text_pos pt;
22254
22255 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22256 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22257 }
22258
22259 /* Display mode lines. */
22260 clear_glyph_matrix (w->desired_matrix);
22261 if (display_mode_lines (w))
22262 ++nwindows;
22263
22264 /* Restore old settings. */
22265 set_buffer_internal_1 (old);
22266 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22267 }
22268
22269 window = w->next;
22270 }
22271
22272 return nwindows;
22273 }
22274
22275
22276 /* Display the mode and/or header line of window W. Value is the
22277 sum number of mode lines and header lines displayed. */
22278
22279 static int
22280 display_mode_lines (struct window *w)
22281 {
22282 Lisp_Object old_selected_window = selected_window;
22283 Lisp_Object old_selected_frame = selected_frame;
22284 Lisp_Object new_frame = w->frame;
22285 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22286 int n = 0;
22287
22288 selected_frame = new_frame;
22289 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22290 or window's point, then we'd need select_window_1 here as well. */
22291 XSETWINDOW (selected_window, w);
22292 XFRAME (new_frame)->selected_window = selected_window;
22293
22294 /* These will be set while the mode line specs are processed. */
22295 line_number_displayed = false;
22296 w->column_number_displayed = -1;
22297
22298 if (WINDOW_WANTS_MODELINE_P (w))
22299 {
22300 struct window *sel_w = XWINDOW (old_selected_window);
22301
22302 /* Select mode line face based on the real selected window. */
22303 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22304 BVAR (current_buffer, mode_line_format));
22305 ++n;
22306 }
22307
22308 if (WINDOW_WANTS_HEADER_LINE_P (w))
22309 {
22310 display_mode_line (w, HEADER_LINE_FACE_ID,
22311 BVAR (current_buffer, header_line_format));
22312 ++n;
22313 }
22314
22315 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22316 selected_frame = old_selected_frame;
22317 selected_window = old_selected_window;
22318 if (n > 0)
22319 w->must_be_updated_p = true;
22320 return n;
22321 }
22322
22323
22324 /* Display mode or header line of window W. FACE_ID specifies which
22325 line to display; it is either MODE_LINE_FACE_ID or
22326 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22327 display. Value is the pixel height of the mode/header line
22328 displayed. */
22329
22330 static int
22331 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22332 {
22333 struct it it;
22334 struct face *face;
22335 ptrdiff_t count = SPECPDL_INDEX ();
22336
22337 init_iterator (&it, w, -1, -1, NULL, face_id);
22338 /* Don't extend on a previously drawn mode-line.
22339 This may happen if called from pos_visible_p. */
22340 it.glyph_row->enabled_p = false;
22341 prepare_desired_row (w, it.glyph_row, true);
22342
22343 it.glyph_row->mode_line_p = true;
22344
22345 /* FIXME: This should be controlled by a user option. But
22346 supporting such an option is not trivial, since the mode line is
22347 made up of many separate strings. */
22348 it.paragraph_embedding = L2R;
22349
22350 record_unwind_protect (unwind_format_mode_line,
22351 format_mode_line_unwind_data (NULL, NULL,
22352 Qnil, false));
22353
22354 mode_line_target = MODE_LINE_DISPLAY;
22355
22356 /* Temporarily make frame's keyboard the current kboard so that
22357 kboard-local variables in the mode_line_format will get the right
22358 values. */
22359 push_kboard (FRAME_KBOARD (it.f));
22360 record_unwind_save_match_data ();
22361 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22362 pop_kboard ();
22363
22364 unbind_to (count, Qnil);
22365
22366 /* Fill up with spaces. */
22367 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22368
22369 compute_line_metrics (&it);
22370 it.glyph_row->full_width_p = true;
22371 it.glyph_row->continued_p = false;
22372 it.glyph_row->truncated_on_left_p = false;
22373 it.glyph_row->truncated_on_right_p = false;
22374
22375 /* Make a 3D mode-line have a shadow at its right end. */
22376 face = FACE_FROM_ID (it.f, face_id);
22377 extend_face_to_end_of_line (&it);
22378 if (face->box != FACE_NO_BOX)
22379 {
22380 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22381 + it.glyph_row->used[TEXT_AREA] - 1);
22382 last->right_box_line_p = true;
22383 }
22384
22385 return it.glyph_row->height;
22386 }
22387
22388 /* Move element ELT in LIST to the front of LIST.
22389 Return the updated list. */
22390
22391 static Lisp_Object
22392 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22393 {
22394 register Lisp_Object tail, prev;
22395 register Lisp_Object tem;
22396
22397 tail = list;
22398 prev = Qnil;
22399 while (CONSP (tail))
22400 {
22401 tem = XCAR (tail);
22402
22403 if (EQ (elt, tem))
22404 {
22405 /* Splice out the link TAIL. */
22406 if (NILP (prev))
22407 list = XCDR (tail);
22408 else
22409 Fsetcdr (prev, XCDR (tail));
22410
22411 /* Now make it the first. */
22412 Fsetcdr (tail, list);
22413 return tail;
22414 }
22415 else
22416 prev = tail;
22417 tail = XCDR (tail);
22418 QUIT;
22419 }
22420
22421 /* Not found--return unchanged LIST. */
22422 return list;
22423 }
22424
22425 /* Contribute ELT to the mode line for window IT->w. How it
22426 translates into text depends on its data type.
22427
22428 IT describes the display environment in which we display, as usual.
22429
22430 DEPTH is the depth in recursion. It is used to prevent
22431 infinite recursion here.
22432
22433 FIELD_WIDTH is the number of characters the display of ELT should
22434 occupy in the mode line, and PRECISION is the maximum number of
22435 characters to display from ELT's representation. See
22436 display_string for details.
22437
22438 Returns the hpos of the end of the text generated by ELT.
22439
22440 PROPS is a property list to add to any string we encounter.
22441
22442 If RISKY, remove (disregard) any properties in any string
22443 we encounter, and ignore :eval and :propertize.
22444
22445 The global variable `mode_line_target' determines whether the
22446 output is passed to `store_mode_line_noprop',
22447 `store_mode_line_string', or `display_string'. */
22448
22449 static int
22450 display_mode_element (struct it *it, int depth, int field_width, int precision,
22451 Lisp_Object elt, Lisp_Object props, bool risky)
22452 {
22453 int n = 0, field, prec;
22454 bool literal = false;
22455
22456 tail_recurse:
22457 if (depth > 100)
22458 elt = build_string ("*too-deep*");
22459
22460 depth++;
22461
22462 switch (XTYPE (elt))
22463 {
22464 case Lisp_String:
22465 {
22466 /* A string: output it and check for %-constructs within it. */
22467 unsigned char c;
22468 ptrdiff_t offset = 0;
22469
22470 if (SCHARS (elt) > 0
22471 && (!NILP (props) || risky))
22472 {
22473 Lisp_Object oprops, aelt;
22474 oprops = Ftext_properties_at (make_number (0), elt);
22475
22476 /* If the starting string's properties are not what
22477 we want, translate the string. Also, if the string
22478 is risky, do that anyway. */
22479
22480 if (NILP (Fequal (props, oprops)) || risky)
22481 {
22482 /* If the starting string has properties,
22483 merge the specified ones onto the existing ones. */
22484 if (! NILP (oprops) && !risky)
22485 {
22486 Lisp_Object tem;
22487
22488 oprops = Fcopy_sequence (oprops);
22489 tem = props;
22490 while (CONSP (tem))
22491 {
22492 oprops = Fplist_put (oprops, XCAR (tem),
22493 XCAR (XCDR (tem)));
22494 tem = XCDR (XCDR (tem));
22495 }
22496 props = oprops;
22497 }
22498
22499 aelt = Fassoc (elt, mode_line_proptrans_alist);
22500 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22501 {
22502 /* AELT is what we want. Move it to the front
22503 without consing. */
22504 elt = XCAR (aelt);
22505 mode_line_proptrans_alist
22506 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22507 }
22508 else
22509 {
22510 Lisp_Object tem;
22511
22512 /* If AELT has the wrong props, it is useless.
22513 so get rid of it. */
22514 if (! NILP (aelt))
22515 mode_line_proptrans_alist
22516 = Fdelq (aelt, mode_line_proptrans_alist);
22517
22518 elt = Fcopy_sequence (elt);
22519 Fset_text_properties (make_number (0), Flength (elt),
22520 props, elt);
22521 /* Add this item to mode_line_proptrans_alist. */
22522 mode_line_proptrans_alist
22523 = Fcons (Fcons (elt, props),
22524 mode_line_proptrans_alist);
22525 /* Truncate mode_line_proptrans_alist
22526 to at most 50 elements. */
22527 tem = Fnthcdr (make_number (50),
22528 mode_line_proptrans_alist);
22529 if (! NILP (tem))
22530 XSETCDR (tem, Qnil);
22531 }
22532 }
22533 }
22534
22535 offset = 0;
22536
22537 if (literal)
22538 {
22539 prec = precision - n;
22540 switch (mode_line_target)
22541 {
22542 case MODE_LINE_NOPROP:
22543 case MODE_LINE_TITLE:
22544 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22545 break;
22546 case MODE_LINE_STRING:
22547 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22548 break;
22549 case MODE_LINE_DISPLAY:
22550 n += display_string (NULL, elt, Qnil, 0, 0, it,
22551 0, prec, 0, STRING_MULTIBYTE (elt));
22552 break;
22553 }
22554
22555 break;
22556 }
22557
22558 /* Handle the non-literal case. */
22559
22560 while ((precision <= 0 || n < precision)
22561 && SREF (elt, offset) != 0
22562 && (mode_line_target != MODE_LINE_DISPLAY
22563 || it->current_x < it->last_visible_x))
22564 {
22565 ptrdiff_t last_offset = offset;
22566
22567 /* Advance to end of string or next format specifier. */
22568 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22569 ;
22570
22571 if (offset - 1 != last_offset)
22572 {
22573 ptrdiff_t nchars, nbytes;
22574
22575 /* Output to end of string or up to '%'. Field width
22576 is length of string. Don't output more than
22577 PRECISION allows us. */
22578 offset--;
22579
22580 prec = c_string_width (SDATA (elt) + last_offset,
22581 offset - last_offset, precision - n,
22582 &nchars, &nbytes);
22583
22584 switch (mode_line_target)
22585 {
22586 case MODE_LINE_NOPROP:
22587 case MODE_LINE_TITLE:
22588 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22589 break;
22590 case MODE_LINE_STRING:
22591 {
22592 ptrdiff_t bytepos = last_offset;
22593 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22594 ptrdiff_t endpos = (precision <= 0
22595 ? string_byte_to_char (elt, offset)
22596 : charpos + nchars);
22597 Lisp_Object mode_string
22598 = Fsubstring (elt, make_number (charpos),
22599 make_number (endpos));
22600 n += store_mode_line_string (NULL, mode_string, false,
22601 0, 0, Qnil);
22602 }
22603 break;
22604 case MODE_LINE_DISPLAY:
22605 {
22606 ptrdiff_t bytepos = last_offset;
22607 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22608
22609 if (precision <= 0)
22610 nchars = string_byte_to_char (elt, offset) - charpos;
22611 n += display_string (NULL, elt, Qnil, 0, charpos,
22612 it, 0, nchars, 0,
22613 STRING_MULTIBYTE (elt));
22614 }
22615 break;
22616 }
22617 }
22618 else /* c == '%' */
22619 {
22620 ptrdiff_t percent_position = offset;
22621
22622 /* Get the specified minimum width. Zero means
22623 don't pad. */
22624 field = 0;
22625 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22626 field = field * 10 + c - '0';
22627
22628 /* Don't pad beyond the total padding allowed. */
22629 if (field_width - n > 0 && field > field_width - n)
22630 field = field_width - n;
22631
22632 /* Note that either PRECISION <= 0 or N < PRECISION. */
22633 prec = precision - n;
22634
22635 if (c == 'M')
22636 n += display_mode_element (it, depth, field, prec,
22637 Vglobal_mode_string, props,
22638 risky);
22639 else if (c != 0)
22640 {
22641 bool multibyte;
22642 ptrdiff_t bytepos, charpos;
22643 const char *spec;
22644 Lisp_Object string;
22645
22646 bytepos = percent_position;
22647 charpos = (STRING_MULTIBYTE (elt)
22648 ? string_byte_to_char (elt, bytepos)
22649 : bytepos);
22650 spec = decode_mode_spec (it->w, c, field, &string);
22651 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22652
22653 switch (mode_line_target)
22654 {
22655 case MODE_LINE_NOPROP:
22656 case MODE_LINE_TITLE:
22657 n += store_mode_line_noprop (spec, field, prec);
22658 break;
22659 case MODE_LINE_STRING:
22660 {
22661 Lisp_Object tem = build_string (spec);
22662 props = Ftext_properties_at (make_number (charpos), elt);
22663 /* Should only keep face property in props */
22664 n += store_mode_line_string (NULL, tem, false,
22665 field, prec, props);
22666 }
22667 break;
22668 case MODE_LINE_DISPLAY:
22669 {
22670 int nglyphs_before, nwritten;
22671
22672 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22673 nwritten = display_string (spec, string, elt,
22674 charpos, 0, it,
22675 field, prec, 0,
22676 multibyte);
22677
22678 /* Assign to the glyphs written above the
22679 string where the `%x' came from, position
22680 of the `%'. */
22681 if (nwritten > 0)
22682 {
22683 struct glyph *glyph
22684 = (it->glyph_row->glyphs[TEXT_AREA]
22685 + nglyphs_before);
22686 int i;
22687
22688 for (i = 0; i < nwritten; ++i)
22689 {
22690 glyph[i].object = elt;
22691 glyph[i].charpos = charpos;
22692 }
22693
22694 n += nwritten;
22695 }
22696 }
22697 break;
22698 }
22699 }
22700 else /* c == 0 */
22701 break;
22702 }
22703 }
22704 }
22705 break;
22706
22707 case Lisp_Symbol:
22708 /* A symbol: process the value of the symbol recursively
22709 as if it appeared here directly. Avoid error if symbol void.
22710 Special case: if value of symbol is a string, output the string
22711 literally. */
22712 {
22713 register Lisp_Object tem;
22714
22715 /* If the variable is not marked as risky to set
22716 then its contents are risky to use. */
22717 if (NILP (Fget (elt, Qrisky_local_variable)))
22718 risky = true;
22719
22720 tem = Fboundp (elt);
22721 if (!NILP (tem))
22722 {
22723 tem = Fsymbol_value (elt);
22724 /* If value is a string, output that string literally:
22725 don't check for % within it. */
22726 if (STRINGP (tem))
22727 literal = true;
22728
22729 if (!EQ (tem, elt))
22730 {
22731 /* Give up right away for nil or t. */
22732 elt = tem;
22733 goto tail_recurse;
22734 }
22735 }
22736 }
22737 break;
22738
22739 case Lisp_Cons:
22740 {
22741 register Lisp_Object car, tem;
22742
22743 /* A cons cell: five distinct cases.
22744 If first element is :eval or :propertize, do something special.
22745 If first element is a string or a cons, process all the elements
22746 and effectively concatenate them.
22747 If first element is a negative number, truncate displaying cdr to
22748 at most that many characters. If positive, pad (with spaces)
22749 to at least that many characters.
22750 If first element is a symbol, process the cadr or caddr recursively
22751 according to whether the symbol's value is non-nil or nil. */
22752 car = XCAR (elt);
22753 if (EQ (car, QCeval))
22754 {
22755 /* An element of the form (:eval FORM) means evaluate FORM
22756 and use the result as mode line elements. */
22757
22758 if (risky)
22759 break;
22760
22761 if (CONSP (XCDR (elt)))
22762 {
22763 Lisp_Object spec;
22764 spec = safe__eval (true, XCAR (XCDR (elt)));
22765 n += display_mode_element (it, depth, field_width - n,
22766 precision - n, spec, props,
22767 risky);
22768 }
22769 }
22770 else if (EQ (car, QCpropertize))
22771 {
22772 /* An element of the form (:propertize ELT PROPS...)
22773 means display ELT but applying properties PROPS. */
22774
22775 if (risky)
22776 break;
22777
22778 if (CONSP (XCDR (elt)))
22779 n += display_mode_element (it, depth, field_width - n,
22780 precision - n, XCAR (XCDR (elt)),
22781 XCDR (XCDR (elt)), risky);
22782 }
22783 else if (SYMBOLP (car))
22784 {
22785 tem = Fboundp (car);
22786 elt = XCDR (elt);
22787 if (!CONSP (elt))
22788 goto invalid;
22789 /* elt is now the cdr, and we know it is a cons cell.
22790 Use its car if CAR has a non-nil value. */
22791 if (!NILP (tem))
22792 {
22793 tem = Fsymbol_value (car);
22794 if (!NILP (tem))
22795 {
22796 elt = XCAR (elt);
22797 goto tail_recurse;
22798 }
22799 }
22800 /* Symbol's value is nil (or symbol is unbound)
22801 Get the cddr of the original list
22802 and if possible find the caddr and use that. */
22803 elt = XCDR (elt);
22804 if (NILP (elt))
22805 break;
22806 else if (!CONSP (elt))
22807 goto invalid;
22808 elt = XCAR (elt);
22809 goto tail_recurse;
22810 }
22811 else if (INTEGERP (car))
22812 {
22813 register int lim = XINT (car);
22814 elt = XCDR (elt);
22815 if (lim < 0)
22816 {
22817 /* Negative int means reduce maximum width. */
22818 if (precision <= 0)
22819 precision = -lim;
22820 else
22821 precision = min (precision, -lim);
22822 }
22823 else if (lim > 0)
22824 {
22825 /* Padding specified. Don't let it be more than
22826 current maximum. */
22827 if (precision > 0)
22828 lim = min (precision, lim);
22829
22830 /* If that's more padding than already wanted, queue it.
22831 But don't reduce padding already specified even if
22832 that is beyond the current truncation point. */
22833 field_width = max (lim, field_width);
22834 }
22835 goto tail_recurse;
22836 }
22837 else if (STRINGP (car) || CONSP (car))
22838 {
22839 Lisp_Object halftail = elt;
22840 int len = 0;
22841
22842 while (CONSP (elt)
22843 && (precision <= 0 || n < precision))
22844 {
22845 n += display_mode_element (it, depth,
22846 /* Do padding only after the last
22847 element in the list. */
22848 (! CONSP (XCDR (elt))
22849 ? field_width - n
22850 : 0),
22851 precision - n, XCAR (elt),
22852 props, risky);
22853 elt = XCDR (elt);
22854 len++;
22855 if ((len & 1) == 0)
22856 halftail = XCDR (halftail);
22857 /* Check for cycle. */
22858 if (EQ (halftail, elt))
22859 break;
22860 }
22861 }
22862 }
22863 break;
22864
22865 default:
22866 invalid:
22867 elt = build_string ("*invalid*");
22868 goto tail_recurse;
22869 }
22870
22871 /* Pad to FIELD_WIDTH. */
22872 if (field_width > 0 && n < field_width)
22873 {
22874 switch (mode_line_target)
22875 {
22876 case MODE_LINE_NOPROP:
22877 case MODE_LINE_TITLE:
22878 n += store_mode_line_noprop ("", field_width - n, 0);
22879 break;
22880 case MODE_LINE_STRING:
22881 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
22882 Qnil);
22883 break;
22884 case MODE_LINE_DISPLAY:
22885 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22886 0, 0, 0);
22887 break;
22888 }
22889 }
22890
22891 return n;
22892 }
22893
22894 /* Store a mode-line string element in mode_line_string_list.
22895
22896 If STRING is non-null, display that C string. Otherwise, the Lisp
22897 string LISP_STRING is displayed.
22898
22899 FIELD_WIDTH is the minimum number of output glyphs to produce.
22900 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22901 with spaces. FIELD_WIDTH <= 0 means don't pad.
22902
22903 PRECISION is the maximum number of characters to output from
22904 STRING. PRECISION <= 0 means don't truncate the string.
22905
22906 If COPY_STRING, make a copy of LISP_STRING before adding
22907 properties to the string.
22908
22909 PROPS are the properties to add to the string.
22910 The mode_line_string_face face property is always added to the string.
22911 */
22912
22913 static int
22914 store_mode_line_string (const char *string, Lisp_Object lisp_string,
22915 bool copy_string,
22916 int field_width, int precision, Lisp_Object props)
22917 {
22918 ptrdiff_t len;
22919 int n = 0;
22920
22921 if (string != NULL)
22922 {
22923 len = strlen (string);
22924 if (precision > 0 && len > precision)
22925 len = precision;
22926 lisp_string = make_string (string, len);
22927 if (NILP (props))
22928 props = mode_line_string_face_prop;
22929 else if (!NILP (mode_line_string_face))
22930 {
22931 Lisp_Object face = Fplist_get (props, Qface);
22932 props = Fcopy_sequence (props);
22933 if (NILP (face))
22934 face = mode_line_string_face;
22935 else
22936 face = list2 (face, mode_line_string_face);
22937 props = Fplist_put (props, Qface, face);
22938 }
22939 Fadd_text_properties (make_number (0), make_number (len),
22940 props, lisp_string);
22941 }
22942 else
22943 {
22944 len = XFASTINT (Flength (lisp_string));
22945 if (precision > 0 && len > precision)
22946 {
22947 len = precision;
22948 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22949 precision = -1;
22950 }
22951 if (!NILP (mode_line_string_face))
22952 {
22953 Lisp_Object face;
22954 if (NILP (props))
22955 props = Ftext_properties_at (make_number (0), lisp_string);
22956 face = Fplist_get (props, Qface);
22957 if (NILP (face))
22958 face = mode_line_string_face;
22959 else
22960 face = list2 (face, mode_line_string_face);
22961 props = list2 (Qface, face);
22962 if (copy_string)
22963 lisp_string = Fcopy_sequence (lisp_string);
22964 }
22965 if (!NILP (props))
22966 Fadd_text_properties (make_number (0), make_number (len),
22967 props, lisp_string);
22968 }
22969
22970 if (len > 0)
22971 {
22972 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22973 n += len;
22974 }
22975
22976 if (field_width > len)
22977 {
22978 field_width -= len;
22979 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22980 if (!NILP (props))
22981 Fadd_text_properties (make_number (0), make_number (field_width),
22982 props, lisp_string);
22983 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22984 n += field_width;
22985 }
22986
22987 return n;
22988 }
22989
22990
22991 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22992 1, 4, 0,
22993 doc: /* Format a string out of a mode line format specification.
22994 First arg FORMAT specifies the mode line format (see `mode-line-format'
22995 for details) to use.
22996
22997 By default, the format is evaluated for the currently selected window.
22998
22999 Optional second arg FACE specifies the face property to put on all
23000 characters for which no face is specified. The value nil means the
23001 default face. The value t means whatever face the window's mode line
23002 currently uses (either `mode-line' or `mode-line-inactive',
23003 depending on whether the window is the selected window or not).
23004 An integer value means the value string has no text
23005 properties.
23006
23007 Optional third and fourth args WINDOW and BUFFER specify the window
23008 and buffer to use as the context for the formatting (defaults
23009 are the selected window and the WINDOW's buffer). */)
23010 (Lisp_Object format, Lisp_Object face,
23011 Lisp_Object window, Lisp_Object buffer)
23012 {
23013 struct it it;
23014 int len;
23015 struct window *w;
23016 struct buffer *old_buffer = NULL;
23017 int face_id;
23018 bool no_props = INTEGERP (face);
23019 ptrdiff_t count = SPECPDL_INDEX ();
23020 Lisp_Object str;
23021 int string_start = 0;
23022
23023 w = decode_any_window (window);
23024 XSETWINDOW (window, w);
23025
23026 if (NILP (buffer))
23027 buffer = w->contents;
23028 CHECK_BUFFER (buffer);
23029
23030 /* Make formatting the modeline a non-op when noninteractive, otherwise
23031 there will be problems later caused by a partially initialized frame. */
23032 if (NILP (format) || noninteractive)
23033 return empty_unibyte_string;
23034
23035 if (no_props)
23036 face = Qnil;
23037
23038 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23039 : EQ (face, Qt) ? (EQ (window, selected_window)
23040 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23041 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23042 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23043 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23044 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23045 : DEFAULT_FACE_ID;
23046
23047 old_buffer = current_buffer;
23048
23049 /* Save things including mode_line_proptrans_alist,
23050 and set that to nil so that we don't alter the outer value. */
23051 record_unwind_protect (unwind_format_mode_line,
23052 format_mode_line_unwind_data
23053 (XFRAME (WINDOW_FRAME (w)),
23054 old_buffer, selected_window, true));
23055 mode_line_proptrans_alist = Qnil;
23056
23057 Fselect_window (window, Qt);
23058 set_buffer_internal_1 (XBUFFER (buffer));
23059
23060 init_iterator (&it, w, -1, -1, NULL, face_id);
23061
23062 if (no_props)
23063 {
23064 mode_line_target = MODE_LINE_NOPROP;
23065 mode_line_string_face_prop = Qnil;
23066 mode_line_string_list = Qnil;
23067 string_start = MODE_LINE_NOPROP_LEN (0);
23068 }
23069 else
23070 {
23071 mode_line_target = MODE_LINE_STRING;
23072 mode_line_string_list = Qnil;
23073 mode_line_string_face = face;
23074 mode_line_string_face_prop
23075 = NILP (face) ? Qnil : list2 (Qface, face);
23076 }
23077
23078 push_kboard (FRAME_KBOARD (it.f));
23079 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23080 pop_kboard ();
23081
23082 if (no_props)
23083 {
23084 len = MODE_LINE_NOPROP_LEN (string_start);
23085 str = make_string (mode_line_noprop_buf + string_start, len);
23086 }
23087 else
23088 {
23089 mode_line_string_list = Fnreverse (mode_line_string_list);
23090 str = Fmapconcat (Qidentity, mode_line_string_list,
23091 empty_unibyte_string);
23092 }
23093
23094 unbind_to (count, Qnil);
23095 return str;
23096 }
23097
23098 /* Write a null-terminated, right justified decimal representation of
23099 the positive integer D to BUF using a minimal field width WIDTH. */
23100
23101 static void
23102 pint2str (register char *buf, register int width, register ptrdiff_t d)
23103 {
23104 register char *p = buf;
23105
23106 if (d <= 0)
23107 *p++ = '0';
23108 else
23109 {
23110 while (d > 0)
23111 {
23112 *p++ = d % 10 + '0';
23113 d /= 10;
23114 }
23115 }
23116
23117 for (width -= (int) (p - buf); width > 0; --width)
23118 *p++ = ' ';
23119 *p-- = '\0';
23120 while (p > buf)
23121 {
23122 d = *buf;
23123 *buf++ = *p;
23124 *p-- = d;
23125 }
23126 }
23127
23128 /* Write a null-terminated, right justified decimal and "human
23129 readable" representation of the nonnegative integer D to BUF using
23130 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23131
23132 static const char power_letter[] =
23133 {
23134 0, /* no letter */
23135 'k', /* kilo */
23136 'M', /* mega */
23137 'G', /* giga */
23138 'T', /* tera */
23139 'P', /* peta */
23140 'E', /* exa */
23141 'Z', /* zetta */
23142 'Y' /* yotta */
23143 };
23144
23145 static void
23146 pint2hrstr (char *buf, int width, ptrdiff_t d)
23147 {
23148 /* We aim to represent the nonnegative integer D as
23149 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
23150 ptrdiff_t quotient = d;
23151 int remainder = 0;
23152 /* -1 means: do not use TENTHS. */
23153 int tenths = -1;
23154 int exponent = 0;
23155
23156 /* Length of QUOTIENT.TENTHS as a string. */
23157 int length;
23158
23159 char * psuffix;
23160 char * p;
23161
23162 if (quotient >= 1000)
23163 {
23164 /* Scale to the appropriate EXPONENT. */
23165 do
23166 {
23167 remainder = quotient % 1000;
23168 quotient /= 1000;
23169 exponent++;
23170 }
23171 while (quotient >= 1000);
23172
23173 /* Round to nearest and decide whether to use TENTHS or not. */
23174 if (quotient <= 9)
23175 {
23176 tenths = remainder / 100;
23177 if (remainder % 100 >= 50)
23178 {
23179 if (tenths < 9)
23180 tenths++;
23181 else
23182 {
23183 quotient++;
23184 if (quotient == 10)
23185 tenths = -1;
23186 else
23187 tenths = 0;
23188 }
23189 }
23190 }
23191 else
23192 if (remainder >= 500)
23193 {
23194 if (quotient < 999)
23195 quotient++;
23196 else
23197 {
23198 quotient = 1;
23199 exponent++;
23200 tenths = 0;
23201 }
23202 }
23203 }
23204
23205 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23206 if (tenths == -1 && quotient <= 99)
23207 if (quotient <= 9)
23208 length = 1;
23209 else
23210 length = 2;
23211 else
23212 length = 3;
23213 p = psuffix = buf + max (width, length);
23214
23215 /* Print EXPONENT. */
23216 *psuffix++ = power_letter[exponent];
23217 *psuffix = '\0';
23218
23219 /* Print TENTHS. */
23220 if (tenths >= 0)
23221 {
23222 *--p = '0' + tenths;
23223 *--p = '.';
23224 }
23225
23226 /* Print QUOTIENT. */
23227 do
23228 {
23229 int digit = quotient % 10;
23230 *--p = '0' + digit;
23231 }
23232 while ((quotient /= 10) != 0);
23233
23234 /* Print leading spaces. */
23235 while (buf < p)
23236 *--p = ' ';
23237 }
23238
23239 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23240 If EOL_FLAG, set also a mnemonic character for end-of-line
23241 type of CODING_SYSTEM. Return updated pointer into BUF. */
23242
23243 static unsigned char invalid_eol_type[] = "(*invalid*)";
23244
23245 static char *
23246 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23247 {
23248 Lisp_Object val;
23249 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23250 const unsigned char *eol_str;
23251 int eol_str_len;
23252 /* The EOL conversion we are using. */
23253 Lisp_Object eoltype;
23254
23255 val = CODING_SYSTEM_SPEC (coding_system);
23256 eoltype = Qnil;
23257
23258 if (!VECTORP (val)) /* Not yet decided. */
23259 {
23260 *buf++ = multibyte ? '-' : ' ';
23261 if (eol_flag)
23262 eoltype = eol_mnemonic_undecided;
23263 /* Don't mention EOL conversion if it isn't decided. */
23264 }
23265 else
23266 {
23267 Lisp_Object attrs;
23268 Lisp_Object eolvalue;
23269
23270 attrs = AREF (val, 0);
23271 eolvalue = AREF (val, 2);
23272
23273 *buf++ = multibyte
23274 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23275 : ' ';
23276
23277 if (eol_flag)
23278 {
23279 /* The EOL conversion that is normal on this system. */
23280
23281 if (NILP (eolvalue)) /* Not yet decided. */
23282 eoltype = eol_mnemonic_undecided;
23283 else if (VECTORP (eolvalue)) /* Not yet decided. */
23284 eoltype = eol_mnemonic_undecided;
23285 else /* eolvalue is Qunix, Qdos, or Qmac. */
23286 eoltype = (EQ (eolvalue, Qunix)
23287 ? eol_mnemonic_unix
23288 : EQ (eolvalue, Qdos)
23289 ? eol_mnemonic_dos : eol_mnemonic_mac);
23290 }
23291 }
23292
23293 if (eol_flag)
23294 {
23295 /* Mention the EOL conversion if it is not the usual one. */
23296 if (STRINGP (eoltype))
23297 {
23298 eol_str = SDATA (eoltype);
23299 eol_str_len = SBYTES (eoltype);
23300 }
23301 else if (CHARACTERP (eoltype))
23302 {
23303 int c = XFASTINT (eoltype);
23304 return buf + CHAR_STRING (c, (unsigned char *) buf);
23305 }
23306 else
23307 {
23308 eol_str = invalid_eol_type;
23309 eol_str_len = sizeof (invalid_eol_type) - 1;
23310 }
23311 memcpy (buf, eol_str, eol_str_len);
23312 buf += eol_str_len;
23313 }
23314
23315 return buf;
23316 }
23317
23318 /* Return a string for the output of a mode line %-spec for window W,
23319 generated by character C. FIELD_WIDTH > 0 means pad the string
23320 returned with spaces to that value. Return a Lisp string in
23321 *STRING if the resulting string is taken from that Lisp string.
23322
23323 Note we operate on the current buffer for most purposes. */
23324
23325 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23326
23327 static const char *
23328 decode_mode_spec (struct window *w, register int c, int field_width,
23329 Lisp_Object *string)
23330 {
23331 Lisp_Object obj;
23332 struct frame *f = XFRAME (WINDOW_FRAME (w));
23333 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23334 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23335 produce strings from numerical values, so limit preposterously
23336 large values of FIELD_WIDTH to avoid overrunning the buffer's
23337 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23338 bytes plus the terminating null. */
23339 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23340 struct buffer *b = current_buffer;
23341
23342 obj = Qnil;
23343 *string = Qnil;
23344
23345 switch (c)
23346 {
23347 case '*':
23348 if (!NILP (BVAR (b, read_only)))
23349 return "%";
23350 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23351 return "*";
23352 return "-";
23353
23354 case '+':
23355 /* This differs from %* only for a modified read-only buffer. */
23356 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23357 return "*";
23358 if (!NILP (BVAR (b, read_only)))
23359 return "%";
23360 return "-";
23361
23362 case '&':
23363 /* This differs from %* in ignoring read-only-ness. */
23364 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23365 return "*";
23366 return "-";
23367
23368 case '%':
23369 return "%";
23370
23371 case '[':
23372 {
23373 int i;
23374 char *p;
23375
23376 if (command_loop_level > 5)
23377 return "[[[... ";
23378 p = decode_mode_spec_buf;
23379 for (i = 0; i < command_loop_level; i++)
23380 *p++ = '[';
23381 *p = 0;
23382 return decode_mode_spec_buf;
23383 }
23384
23385 case ']':
23386 {
23387 int i;
23388 char *p;
23389
23390 if (command_loop_level > 5)
23391 return " ...]]]";
23392 p = decode_mode_spec_buf;
23393 for (i = 0; i < command_loop_level; i++)
23394 *p++ = ']';
23395 *p = 0;
23396 return decode_mode_spec_buf;
23397 }
23398
23399 case '-':
23400 {
23401 register int i;
23402
23403 /* Let lots_of_dashes be a string of infinite length. */
23404 if (mode_line_target == MODE_LINE_NOPROP
23405 || mode_line_target == MODE_LINE_STRING)
23406 return "--";
23407 if (field_width <= 0
23408 || field_width > sizeof (lots_of_dashes))
23409 {
23410 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23411 decode_mode_spec_buf[i] = '-';
23412 decode_mode_spec_buf[i] = '\0';
23413 return decode_mode_spec_buf;
23414 }
23415 else
23416 return lots_of_dashes;
23417 }
23418
23419 case 'b':
23420 obj = BVAR (b, name);
23421 break;
23422
23423 case 'c':
23424 /* %c and %l are ignored in `frame-title-format'.
23425 (In redisplay_internal, the frame title is drawn _before_ the
23426 windows are updated, so the stuff which depends on actual
23427 window contents (such as %l) may fail to render properly, or
23428 even crash emacs.) */
23429 if (mode_line_target == MODE_LINE_TITLE)
23430 return "";
23431 else
23432 {
23433 ptrdiff_t col = current_column ();
23434 w->column_number_displayed = col;
23435 pint2str (decode_mode_spec_buf, width, col);
23436 return decode_mode_spec_buf;
23437 }
23438
23439 case 'e':
23440 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23441 {
23442 if (NILP (Vmemory_full))
23443 return "";
23444 else
23445 return "!MEM FULL! ";
23446 }
23447 #else
23448 return "";
23449 #endif
23450
23451 case 'F':
23452 /* %F displays the frame name. */
23453 if (!NILP (f->title))
23454 return SSDATA (f->title);
23455 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23456 return SSDATA (f->name);
23457 return "Emacs";
23458
23459 case 'f':
23460 obj = BVAR (b, filename);
23461 break;
23462
23463 case 'i':
23464 {
23465 ptrdiff_t size = ZV - BEGV;
23466 pint2str (decode_mode_spec_buf, width, size);
23467 return decode_mode_spec_buf;
23468 }
23469
23470 case 'I':
23471 {
23472 ptrdiff_t size = ZV - BEGV;
23473 pint2hrstr (decode_mode_spec_buf, width, size);
23474 return decode_mode_spec_buf;
23475 }
23476
23477 case 'l':
23478 {
23479 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23480 ptrdiff_t topline, nlines, height;
23481 ptrdiff_t junk;
23482
23483 /* %c and %l are ignored in `frame-title-format'. */
23484 if (mode_line_target == MODE_LINE_TITLE)
23485 return "";
23486
23487 startpos = marker_position (w->start);
23488 startpos_byte = marker_byte_position (w->start);
23489 height = WINDOW_TOTAL_LINES (w);
23490
23491 /* If we decided that this buffer isn't suitable for line numbers,
23492 don't forget that too fast. */
23493 if (w->base_line_pos == -1)
23494 goto no_value;
23495
23496 /* If the buffer is very big, don't waste time. */
23497 if (INTEGERP (Vline_number_display_limit)
23498 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23499 {
23500 w->base_line_pos = 0;
23501 w->base_line_number = 0;
23502 goto no_value;
23503 }
23504
23505 if (w->base_line_number > 0
23506 && w->base_line_pos > 0
23507 && w->base_line_pos <= startpos)
23508 {
23509 line = w->base_line_number;
23510 linepos = w->base_line_pos;
23511 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23512 }
23513 else
23514 {
23515 line = 1;
23516 linepos = BUF_BEGV (b);
23517 linepos_byte = BUF_BEGV_BYTE (b);
23518 }
23519
23520 /* Count lines from base line to window start position. */
23521 nlines = display_count_lines (linepos_byte,
23522 startpos_byte,
23523 startpos, &junk);
23524
23525 topline = nlines + line;
23526
23527 /* Determine a new base line, if the old one is too close
23528 or too far away, or if we did not have one.
23529 "Too close" means it's plausible a scroll-down would
23530 go back past it. */
23531 if (startpos == BUF_BEGV (b))
23532 {
23533 w->base_line_number = topline;
23534 w->base_line_pos = BUF_BEGV (b);
23535 }
23536 else if (nlines < height + 25 || nlines > height * 3 + 50
23537 || linepos == BUF_BEGV (b))
23538 {
23539 ptrdiff_t limit = BUF_BEGV (b);
23540 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23541 ptrdiff_t position;
23542 ptrdiff_t distance =
23543 (height * 2 + 30) * line_number_display_limit_width;
23544
23545 if (startpos - distance > limit)
23546 {
23547 limit = startpos - distance;
23548 limit_byte = CHAR_TO_BYTE (limit);
23549 }
23550
23551 nlines = display_count_lines (startpos_byte,
23552 limit_byte,
23553 - (height * 2 + 30),
23554 &position);
23555 /* If we couldn't find the lines we wanted within
23556 line_number_display_limit_width chars per line,
23557 give up on line numbers for this window. */
23558 if (position == limit_byte && limit == startpos - distance)
23559 {
23560 w->base_line_pos = -1;
23561 w->base_line_number = 0;
23562 goto no_value;
23563 }
23564
23565 w->base_line_number = topline - nlines;
23566 w->base_line_pos = BYTE_TO_CHAR (position);
23567 }
23568
23569 /* Now count lines from the start pos to point. */
23570 nlines = display_count_lines (startpos_byte,
23571 PT_BYTE, PT, &junk);
23572
23573 /* Record that we did display the line number. */
23574 line_number_displayed = true;
23575
23576 /* Make the string to show. */
23577 pint2str (decode_mode_spec_buf, width, topline + nlines);
23578 return decode_mode_spec_buf;
23579 no_value:
23580 {
23581 char *p = decode_mode_spec_buf;
23582 int pad = width - 2;
23583 while (pad-- > 0)
23584 *p++ = ' ';
23585 *p++ = '?';
23586 *p++ = '?';
23587 *p = '\0';
23588 return decode_mode_spec_buf;
23589 }
23590 }
23591 break;
23592
23593 case 'm':
23594 obj = BVAR (b, mode_name);
23595 break;
23596
23597 case 'n':
23598 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23599 return " Narrow";
23600 break;
23601
23602 case 'p':
23603 {
23604 ptrdiff_t pos = marker_position (w->start);
23605 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23606
23607 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23608 {
23609 if (pos <= BUF_BEGV (b))
23610 return "All";
23611 else
23612 return "Bottom";
23613 }
23614 else if (pos <= BUF_BEGV (b))
23615 return "Top";
23616 else
23617 {
23618 if (total > 1000000)
23619 /* Do it differently for a large value, to avoid overflow. */
23620 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23621 else
23622 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23623 /* We can't normally display a 3-digit number,
23624 so get us a 2-digit number that is close. */
23625 if (total == 100)
23626 total = 99;
23627 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23628 return decode_mode_spec_buf;
23629 }
23630 }
23631
23632 /* Display percentage of size above the bottom of the screen. */
23633 case 'P':
23634 {
23635 ptrdiff_t toppos = marker_position (w->start);
23636 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23637 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23638
23639 if (botpos >= BUF_ZV (b))
23640 {
23641 if (toppos <= BUF_BEGV (b))
23642 return "All";
23643 else
23644 return "Bottom";
23645 }
23646 else
23647 {
23648 if (total > 1000000)
23649 /* Do it differently for a large value, to avoid overflow. */
23650 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23651 else
23652 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23653 /* We can't normally display a 3-digit number,
23654 so get us a 2-digit number that is close. */
23655 if (total == 100)
23656 total = 99;
23657 if (toppos <= BUF_BEGV (b))
23658 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23659 else
23660 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23661 return decode_mode_spec_buf;
23662 }
23663 }
23664
23665 case 's':
23666 /* status of process */
23667 obj = Fget_buffer_process (Fcurrent_buffer ());
23668 if (NILP (obj))
23669 return "no process";
23670 #ifndef MSDOS
23671 obj = Fsymbol_name (Fprocess_status (obj));
23672 #endif
23673 break;
23674
23675 case '@':
23676 {
23677 ptrdiff_t count = inhibit_garbage_collection ();
23678 Lisp_Object curdir = BVAR (current_buffer, directory);
23679 Lisp_Object val = Qnil;
23680
23681 if (STRINGP (curdir))
23682 val = call1 (intern ("file-remote-p"), curdir);
23683
23684 unbind_to (count, Qnil);
23685
23686 if (NILP (val))
23687 return "-";
23688 else
23689 return "@";
23690 }
23691
23692 case 'z':
23693 /* coding-system (not including end-of-line format) */
23694 case 'Z':
23695 /* coding-system (including end-of-line type) */
23696 {
23697 bool eol_flag = (c == 'Z');
23698 char *p = decode_mode_spec_buf;
23699
23700 if (! FRAME_WINDOW_P (f))
23701 {
23702 /* No need to mention EOL here--the terminal never needs
23703 to do EOL conversion. */
23704 p = decode_mode_spec_coding (CODING_ID_NAME
23705 (FRAME_KEYBOARD_CODING (f)->id),
23706 p, false);
23707 p = decode_mode_spec_coding (CODING_ID_NAME
23708 (FRAME_TERMINAL_CODING (f)->id),
23709 p, false);
23710 }
23711 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23712 p, eol_flag);
23713
23714 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23715 #ifdef subprocesses
23716 obj = Fget_buffer_process (Fcurrent_buffer ());
23717 if (PROCESSP (obj))
23718 {
23719 p = decode_mode_spec_coding
23720 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23721 p = decode_mode_spec_coding
23722 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23723 }
23724 #endif /* subprocesses */
23725 #endif /* false */
23726 *p = 0;
23727 return decode_mode_spec_buf;
23728 }
23729 }
23730
23731 if (STRINGP (obj))
23732 {
23733 *string = obj;
23734 return SSDATA (obj);
23735 }
23736 else
23737 return "";
23738 }
23739
23740
23741 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23742 means count lines back from START_BYTE. But don't go beyond
23743 LIMIT_BYTE. Return the number of lines thus found (always
23744 nonnegative).
23745
23746 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23747 either the position COUNT lines after/before START_BYTE, if we
23748 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23749 COUNT lines. */
23750
23751 static ptrdiff_t
23752 display_count_lines (ptrdiff_t start_byte,
23753 ptrdiff_t limit_byte, ptrdiff_t count,
23754 ptrdiff_t *byte_pos_ptr)
23755 {
23756 register unsigned char *cursor;
23757 unsigned char *base;
23758
23759 register ptrdiff_t ceiling;
23760 register unsigned char *ceiling_addr;
23761 ptrdiff_t orig_count = count;
23762
23763 /* If we are not in selective display mode,
23764 check only for newlines. */
23765 bool selective_display
23766 = (!NILP (BVAR (current_buffer, selective_display))
23767 && !INTEGERP (BVAR (current_buffer, selective_display)));
23768
23769 if (count > 0)
23770 {
23771 while (start_byte < limit_byte)
23772 {
23773 ceiling = BUFFER_CEILING_OF (start_byte);
23774 ceiling = min (limit_byte - 1, ceiling);
23775 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23776 base = (cursor = BYTE_POS_ADDR (start_byte));
23777
23778 do
23779 {
23780 if (selective_display)
23781 {
23782 while (*cursor != '\n' && *cursor != 015
23783 && ++cursor != ceiling_addr)
23784 continue;
23785 if (cursor == ceiling_addr)
23786 break;
23787 }
23788 else
23789 {
23790 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23791 if (! cursor)
23792 break;
23793 }
23794
23795 cursor++;
23796
23797 if (--count == 0)
23798 {
23799 start_byte += cursor - base;
23800 *byte_pos_ptr = start_byte;
23801 return orig_count;
23802 }
23803 }
23804 while (cursor < ceiling_addr);
23805
23806 start_byte += ceiling_addr - base;
23807 }
23808 }
23809 else
23810 {
23811 while (start_byte > limit_byte)
23812 {
23813 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23814 ceiling = max (limit_byte, ceiling);
23815 ceiling_addr = BYTE_POS_ADDR (ceiling);
23816 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23817 while (true)
23818 {
23819 if (selective_display)
23820 {
23821 while (--cursor >= ceiling_addr
23822 && *cursor != '\n' && *cursor != 015)
23823 continue;
23824 if (cursor < ceiling_addr)
23825 break;
23826 }
23827 else
23828 {
23829 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23830 if (! cursor)
23831 break;
23832 }
23833
23834 if (++count == 0)
23835 {
23836 start_byte += cursor - base + 1;
23837 *byte_pos_ptr = start_byte;
23838 /* When scanning backwards, we should
23839 not count the newline posterior to which we stop. */
23840 return - orig_count - 1;
23841 }
23842 }
23843 start_byte += ceiling_addr - base;
23844 }
23845 }
23846
23847 *byte_pos_ptr = limit_byte;
23848
23849 if (count < 0)
23850 return - orig_count + count;
23851 return orig_count - count;
23852
23853 }
23854
23855
23856 \f
23857 /***********************************************************************
23858 Displaying strings
23859 ***********************************************************************/
23860
23861 /* Display a NUL-terminated string, starting with index START.
23862
23863 If STRING is non-null, display that C string. Otherwise, the Lisp
23864 string LISP_STRING is displayed. There's a case that STRING is
23865 non-null and LISP_STRING is not nil. It means STRING is a string
23866 data of LISP_STRING. In that case, we display LISP_STRING while
23867 ignoring its text properties.
23868
23869 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23870 FACE_STRING. Display STRING or LISP_STRING with the face at
23871 FACE_STRING_POS in FACE_STRING:
23872
23873 Display the string in the environment given by IT, but use the
23874 standard display table, temporarily.
23875
23876 FIELD_WIDTH is the minimum number of output glyphs to produce.
23877 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23878 with spaces. If STRING has more characters, more than FIELD_WIDTH
23879 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23880
23881 PRECISION is the maximum number of characters to output from
23882 STRING. PRECISION < 0 means don't truncate the string.
23883
23884 This is roughly equivalent to printf format specifiers:
23885
23886 FIELD_WIDTH PRECISION PRINTF
23887 ----------------------------------------
23888 -1 -1 %s
23889 -1 10 %.10s
23890 10 -1 %10s
23891 20 10 %20.10s
23892
23893 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23894 display them, and < 0 means obey the current buffer's value of
23895 enable_multibyte_characters.
23896
23897 Value is the number of columns displayed. */
23898
23899 static int
23900 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23901 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23902 int field_width, int precision, int max_x, int multibyte)
23903 {
23904 int hpos_at_start = it->hpos;
23905 int saved_face_id = it->face_id;
23906 struct glyph_row *row = it->glyph_row;
23907 ptrdiff_t it_charpos;
23908
23909 /* Initialize the iterator IT for iteration over STRING beginning
23910 with index START. */
23911 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23912 precision, field_width, multibyte);
23913 if (string && STRINGP (lisp_string))
23914 /* LISP_STRING is the one returned by decode_mode_spec. We should
23915 ignore its text properties. */
23916 it->stop_charpos = it->end_charpos;
23917
23918 /* If displaying STRING, set up the face of the iterator from
23919 FACE_STRING, if that's given. */
23920 if (STRINGP (face_string))
23921 {
23922 ptrdiff_t endptr;
23923 struct face *face;
23924
23925 it->face_id
23926 = face_at_string_position (it->w, face_string, face_string_pos,
23927 0, &endptr, it->base_face_id, false);
23928 face = FACE_FROM_ID (it->f, it->face_id);
23929 it->face_box_p = face->box != FACE_NO_BOX;
23930 }
23931
23932 /* Set max_x to the maximum allowed X position. Don't let it go
23933 beyond the right edge of the window. */
23934 if (max_x <= 0)
23935 max_x = it->last_visible_x;
23936 else
23937 max_x = min (max_x, it->last_visible_x);
23938
23939 /* Skip over display elements that are not visible. because IT->w is
23940 hscrolled. */
23941 if (it->current_x < it->first_visible_x)
23942 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23943 MOVE_TO_POS | MOVE_TO_X);
23944
23945 row->ascent = it->max_ascent;
23946 row->height = it->max_ascent + it->max_descent;
23947 row->phys_ascent = it->max_phys_ascent;
23948 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23949 row->extra_line_spacing = it->max_extra_line_spacing;
23950
23951 if (STRINGP (it->string))
23952 it_charpos = IT_STRING_CHARPOS (*it);
23953 else
23954 it_charpos = IT_CHARPOS (*it);
23955
23956 /* This condition is for the case that we are called with current_x
23957 past last_visible_x. */
23958 while (it->current_x < max_x)
23959 {
23960 int x_before, x, n_glyphs_before, i, nglyphs;
23961
23962 /* Get the next display element. */
23963 if (!get_next_display_element (it))
23964 break;
23965
23966 /* Produce glyphs. */
23967 x_before = it->current_x;
23968 n_glyphs_before = row->used[TEXT_AREA];
23969 PRODUCE_GLYPHS (it);
23970
23971 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23972 i = 0;
23973 x = x_before;
23974 while (i < nglyphs)
23975 {
23976 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23977
23978 if (it->line_wrap != TRUNCATE
23979 && x + glyph->pixel_width > max_x)
23980 {
23981 /* End of continued line or max_x reached. */
23982 if (CHAR_GLYPH_PADDING_P (*glyph))
23983 {
23984 /* A wide character is unbreakable. */
23985 if (row->reversed_p)
23986 unproduce_glyphs (it, row->used[TEXT_AREA]
23987 - n_glyphs_before);
23988 row->used[TEXT_AREA] = n_glyphs_before;
23989 it->current_x = x_before;
23990 }
23991 else
23992 {
23993 if (row->reversed_p)
23994 unproduce_glyphs (it, row->used[TEXT_AREA]
23995 - (n_glyphs_before + i));
23996 row->used[TEXT_AREA] = n_glyphs_before + i;
23997 it->current_x = x;
23998 }
23999 break;
24000 }
24001 else if (x + glyph->pixel_width >= it->first_visible_x)
24002 {
24003 /* Glyph is at least partially visible. */
24004 ++it->hpos;
24005 if (x < it->first_visible_x)
24006 row->x = x - it->first_visible_x;
24007 }
24008 else
24009 {
24010 /* Glyph is off the left margin of the display area.
24011 Should not happen. */
24012 emacs_abort ();
24013 }
24014
24015 row->ascent = max (row->ascent, it->max_ascent);
24016 row->height = max (row->height, it->max_ascent + it->max_descent);
24017 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24018 row->phys_height = max (row->phys_height,
24019 it->max_phys_ascent + it->max_phys_descent);
24020 row->extra_line_spacing = max (row->extra_line_spacing,
24021 it->max_extra_line_spacing);
24022 x += glyph->pixel_width;
24023 ++i;
24024 }
24025
24026 /* Stop if max_x reached. */
24027 if (i < nglyphs)
24028 break;
24029
24030 /* Stop at line ends. */
24031 if (ITERATOR_AT_END_OF_LINE_P (it))
24032 {
24033 it->continuation_lines_width = 0;
24034 break;
24035 }
24036
24037 set_iterator_to_next (it, true);
24038 if (STRINGP (it->string))
24039 it_charpos = IT_STRING_CHARPOS (*it);
24040 else
24041 it_charpos = IT_CHARPOS (*it);
24042
24043 /* Stop if truncating at the right edge. */
24044 if (it->line_wrap == TRUNCATE
24045 && it->current_x >= it->last_visible_x)
24046 {
24047 /* Add truncation mark, but don't do it if the line is
24048 truncated at a padding space. */
24049 if (it_charpos < it->string_nchars)
24050 {
24051 if (!FRAME_WINDOW_P (it->f))
24052 {
24053 int ii, n;
24054
24055 if (it->current_x > it->last_visible_x)
24056 {
24057 if (!row->reversed_p)
24058 {
24059 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24060 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24061 break;
24062 }
24063 else
24064 {
24065 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24066 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24067 break;
24068 unproduce_glyphs (it, ii + 1);
24069 ii = row->used[TEXT_AREA] - (ii + 1);
24070 }
24071 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24072 {
24073 row->used[TEXT_AREA] = ii;
24074 produce_special_glyphs (it, IT_TRUNCATION);
24075 }
24076 }
24077 produce_special_glyphs (it, IT_TRUNCATION);
24078 }
24079 row->truncated_on_right_p = true;
24080 }
24081 break;
24082 }
24083 }
24084
24085 /* Maybe insert a truncation at the left. */
24086 if (it->first_visible_x
24087 && it_charpos > 0)
24088 {
24089 if (!FRAME_WINDOW_P (it->f)
24090 || (row->reversed_p
24091 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24092 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24093 insert_left_trunc_glyphs (it);
24094 row->truncated_on_left_p = true;
24095 }
24096
24097 it->face_id = saved_face_id;
24098
24099 /* Value is number of columns displayed. */
24100 return it->hpos - hpos_at_start;
24101 }
24102
24103
24104 \f
24105 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24106 appears as an element of LIST or as the car of an element of LIST.
24107 If PROPVAL is a list, compare each element against LIST in that
24108 way, and return 1/2 if any element of PROPVAL is found in LIST.
24109 Otherwise return 0. This function cannot quit.
24110 The return value is 2 if the text is invisible but with an ellipsis
24111 and 1 if it's invisible and without an ellipsis. */
24112
24113 int
24114 invisible_prop (Lisp_Object propval, Lisp_Object list)
24115 {
24116 Lisp_Object tail, proptail;
24117
24118 for (tail = list; CONSP (tail); tail = XCDR (tail))
24119 {
24120 register Lisp_Object tem;
24121 tem = XCAR (tail);
24122 if (EQ (propval, tem))
24123 return 1;
24124 if (CONSP (tem) && EQ (propval, XCAR (tem)))
24125 return NILP (XCDR (tem)) ? 1 : 2;
24126 }
24127
24128 if (CONSP (propval))
24129 {
24130 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
24131 {
24132 Lisp_Object propelt;
24133 propelt = XCAR (proptail);
24134 for (tail = list; CONSP (tail); tail = XCDR (tail))
24135 {
24136 register Lisp_Object tem;
24137 tem = XCAR (tail);
24138 if (EQ (propelt, tem))
24139 return 1;
24140 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
24141 return NILP (XCDR (tem)) ? 1 : 2;
24142 }
24143 }
24144 }
24145
24146 return 0;
24147 }
24148
24149 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
24150 doc: /* Non-nil if the property makes the text invisible.
24151 POS-OR-PROP can be a marker or number, in which case it is taken to be
24152 a position in the current buffer and the value of the `invisible' property
24153 is checked; or it can be some other value, which is then presumed to be the
24154 value of the `invisible' property of the text of interest.
24155 The non-nil value returned can be t for truly invisible text or something
24156 else if the text is replaced by an ellipsis. */)
24157 (Lisp_Object pos_or_prop)
24158 {
24159 Lisp_Object prop
24160 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
24161 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
24162 : pos_or_prop);
24163 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
24164 return (invis == 0 ? Qnil
24165 : invis == 1 ? Qt
24166 : make_number (invis));
24167 }
24168
24169 /* Calculate a width or height in pixels from a specification using
24170 the following elements:
24171
24172 SPEC ::=
24173 NUM - a (fractional) multiple of the default font width/height
24174 (NUM) - specifies exactly NUM pixels
24175 UNIT - a fixed number of pixels, see below.
24176 ELEMENT - size of a display element in pixels, see below.
24177 (NUM . SPEC) - equals NUM * SPEC
24178 (+ SPEC SPEC ...) - add pixel values
24179 (- SPEC SPEC ...) - subtract pixel values
24180 (- SPEC) - negate pixel value
24181
24182 NUM ::=
24183 INT or FLOAT - a number constant
24184 SYMBOL - use symbol's (buffer local) variable binding.
24185
24186 UNIT ::=
24187 in - pixels per inch *)
24188 mm - pixels per 1/1000 meter *)
24189 cm - pixels per 1/100 meter *)
24190 width - width of current font in pixels.
24191 height - height of current font in pixels.
24192
24193 *) using the ratio(s) defined in display-pixels-per-inch.
24194
24195 ELEMENT ::=
24196
24197 left-fringe - left fringe width in pixels
24198 right-fringe - right fringe width in pixels
24199
24200 left-margin - left margin width in pixels
24201 right-margin - right margin width in pixels
24202
24203 scroll-bar - scroll-bar area width in pixels
24204
24205 Examples:
24206
24207 Pixels corresponding to 5 inches:
24208 (5 . in)
24209
24210 Total width of non-text areas on left side of window (if scroll-bar is on left):
24211 '(space :width (+ left-fringe left-margin scroll-bar))
24212
24213 Align to first text column (in header line):
24214 '(space :align-to 0)
24215
24216 Align to middle of text area minus half the width of variable `my-image'
24217 containing a loaded image:
24218 '(space :align-to (0.5 . (- text my-image)))
24219
24220 Width of left margin minus width of 1 character in the default font:
24221 '(space :width (- left-margin 1))
24222
24223 Width of left margin minus width of 2 characters in the current font:
24224 '(space :width (- left-margin (2 . width)))
24225
24226 Center 1 character over left-margin (in header line):
24227 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24228
24229 Different ways to express width of left fringe plus left margin minus one pixel:
24230 '(space :width (- (+ left-fringe left-margin) (1)))
24231 '(space :width (+ left-fringe left-margin (- (1))))
24232 '(space :width (+ left-fringe left-margin (-1)))
24233
24234 */
24235
24236 static bool
24237 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24238 struct font *font, bool width_p, int *align_to)
24239 {
24240 double pixels;
24241
24242 # define OK_PIXELS(val) (*res = (val), true)
24243 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24244
24245 if (NILP (prop))
24246 return OK_PIXELS (0);
24247
24248 eassert (FRAME_LIVE_P (it->f));
24249
24250 if (SYMBOLP (prop))
24251 {
24252 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24253 {
24254 char *unit = SSDATA (SYMBOL_NAME (prop));
24255
24256 if (unit[0] == 'i' && unit[1] == 'n')
24257 pixels = 1.0;
24258 else if (unit[0] == 'm' && unit[1] == 'm')
24259 pixels = 25.4;
24260 else if (unit[0] == 'c' && unit[1] == 'm')
24261 pixels = 2.54;
24262 else
24263 pixels = 0;
24264 if (pixels > 0)
24265 {
24266 double ppi = (width_p ? FRAME_RES_X (it->f)
24267 : FRAME_RES_Y (it->f));
24268
24269 if (ppi > 0)
24270 return OK_PIXELS (ppi / pixels);
24271 return false;
24272 }
24273 }
24274
24275 #ifdef HAVE_WINDOW_SYSTEM
24276 if (EQ (prop, Qheight))
24277 return OK_PIXELS (font
24278 ? normal_char_height (font, -1)
24279 : FRAME_LINE_HEIGHT (it->f));
24280 if (EQ (prop, Qwidth))
24281 return OK_PIXELS (font
24282 ? FONT_WIDTH (font)
24283 : FRAME_COLUMN_WIDTH (it->f));
24284 #else
24285 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24286 return OK_PIXELS (1);
24287 #endif
24288
24289 if (EQ (prop, Qtext))
24290 return OK_PIXELS (width_p
24291 ? window_box_width (it->w, TEXT_AREA)
24292 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24293
24294 if (align_to && *align_to < 0)
24295 {
24296 *res = 0;
24297 if (EQ (prop, Qleft))
24298 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24299 if (EQ (prop, Qright))
24300 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24301 if (EQ (prop, Qcenter))
24302 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24303 + window_box_width (it->w, TEXT_AREA) / 2);
24304 if (EQ (prop, Qleft_fringe))
24305 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24306 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24307 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24308 if (EQ (prop, Qright_fringe))
24309 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24310 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24311 : window_box_right_offset (it->w, TEXT_AREA));
24312 if (EQ (prop, Qleft_margin))
24313 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24314 if (EQ (prop, Qright_margin))
24315 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24316 if (EQ (prop, Qscroll_bar))
24317 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24318 ? 0
24319 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24320 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24321 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24322 : 0)));
24323 }
24324 else
24325 {
24326 if (EQ (prop, Qleft_fringe))
24327 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24328 if (EQ (prop, Qright_fringe))
24329 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24330 if (EQ (prop, Qleft_margin))
24331 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24332 if (EQ (prop, Qright_margin))
24333 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24334 if (EQ (prop, Qscroll_bar))
24335 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24336 }
24337
24338 prop = buffer_local_value (prop, it->w->contents);
24339 if (EQ (prop, Qunbound))
24340 prop = Qnil;
24341 }
24342
24343 if (NUMBERP (prop))
24344 {
24345 int base_unit = (width_p
24346 ? FRAME_COLUMN_WIDTH (it->f)
24347 : FRAME_LINE_HEIGHT (it->f));
24348 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24349 }
24350
24351 if (CONSP (prop))
24352 {
24353 Lisp_Object car = XCAR (prop);
24354 Lisp_Object cdr = XCDR (prop);
24355
24356 if (SYMBOLP (car))
24357 {
24358 #ifdef HAVE_WINDOW_SYSTEM
24359 if (FRAME_WINDOW_P (it->f)
24360 && valid_image_p (prop))
24361 {
24362 ptrdiff_t id = lookup_image (it->f, prop);
24363 struct image *img = IMAGE_FROM_ID (it->f, id);
24364
24365 return OK_PIXELS (width_p ? img->width : img->height);
24366 }
24367 # ifdef HAVE_XWIDGETS
24368 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
24369 {
24370 // TODO: Don't return dummy size.
24371 return OK_PIXELS (100);
24372 }
24373 # endif
24374 #endif
24375 if (EQ (car, Qplus) || EQ (car, Qminus))
24376 {
24377 bool first = true;
24378 double px;
24379
24380 pixels = 0;
24381 while (CONSP (cdr))
24382 {
24383 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24384 font, width_p, align_to))
24385 return false;
24386 if (first)
24387 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24388 else
24389 pixels += px;
24390 cdr = XCDR (cdr);
24391 }
24392 if (EQ (car, Qminus))
24393 pixels = -pixels;
24394 return OK_PIXELS (pixels);
24395 }
24396
24397 car = buffer_local_value (car, it->w->contents);
24398 if (EQ (car, Qunbound))
24399 car = Qnil;
24400 }
24401
24402 if (NUMBERP (car))
24403 {
24404 double fact;
24405 pixels = XFLOATINT (car);
24406 if (NILP (cdr))
24407 return OK_PIXELS (pixels);
24408 if (calc_pixel_width_or_height (&fact, it, cdr,
24409 font, width_p, align_to))
24410 return OK_PIXELS (pixels * fact);
24411 return false;
24412 }
24413
24414 return false;
24415 }
24416
24417 return false;
24418 }
24419
24420 void
24421 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24422 {
24423 #ifdef HAVE_WINDOW_SYSTEM
24424 normal_char_ascent_descent (font, -1, ascent, descent);
24425 #else
24426 *ascent = 1;
24427 *descent = 0;
24428 #endif
24429 }
24430
24431 \f
24432 /***********************************************************************
24433 Glyph Display
24434 ***********************************************************************/
24435
24436 #ifdef HAVE_WINDOW_SYSTEM
24437
24438 #ifdef GLYPH_DEBUG
24439
24440 void
24441 dump_glyph_string (struct glyph_string *s)
24442 {
24443 fprintf (stderr, "glyph string\n");
24444 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24445 s->x, s->y, s->width, s->height);
24446 fprintf (stderr, " ybase = %d\n", s->ybase);
24447 fprintf (stderr, " hl = %d\n", s->hl);
24448 fprintf (stderr, " left overhang = %d, right = %d\n",
24449 s->left_overhang, s->right_overhang);
24450 fprintf (stderr, " nchars = %d\n", s->nchars);
24451 fprintf (stderr, " extends to end of line = %d\n",
24452 s->extends_to_end_of_line_p);
24453 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24454 fprintf (stderr, " bg width = %d\n", s->background_width);
24455 }
24456
24457 #endif /* GLYPH_DEBUG */
24458
24459 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24460 of XChar2b structures for S; it can't be allocated in
24461 init_glyph_string because it must be allocated via `alloca'. W
24462 is the window on which S is drawn. ROW and AREA are the glyph row
24463 and area within the row from which S is constructed. START is the
24464 index of the first glyph structure covered by S. HL is a
24465 face-override for drawing S. */
24466
24467 #ifdef HAVE_NTGUI
24468 #define OPTIONAL_HDC(hdc) HDC hdc,
24469 #define DECLARE_HDC(hdc) HDC hdc;
24470 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24471 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24472 #endif
24473
24474 #ifndef OPTIONAL_HDC
24475 #define OPTIONAL_HDC(hdc)
24476 #define DECLARE_HDC(hdc)
24477 #define ALLOCATE_HDC(hdc, f)
24478 #define RELEASE_HDC(hdc, f)
24479 #endif
24480
24481 static void
24482 init_glyph_string (struct glyph_string *s,
24483 OPTIONAL_HDC (hdc)
24484 XChar2b *char2b, struct window *w, struct glyph_row *row,
24485 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24486 {
24487 memset (s, 0, sizeof *s);
24488 s->w = w;
24489 s->f = XFRAME (w->frame);
24490 #ifdef HAVE_NTGUI
24491 s->hdc = hdc;
24492 #endif
24493 s->display = FRAME_X_DISPLAY (s->f);
24494 s->window = FRAME_X_WINDOW (s->f);
24495 s->char2b = char2b;
24496 s->hl = hl;
24497 s->row = row;
24498 s->area = area;
24499 s->first_glyph = row->glyphs[area] + start;
24500 s->height = row->height;
24501 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24502 s->ybase = s->y + row->ascent;
24503 }
24504
24505
24506 /* Append the list of glyph strings with head H and tail T to the list
24507 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24508
24509 static void
24510 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24511 struct glyph_string *h, struct glyph_string *t)
24512 {
24513 if (h)
24514 {
24515 if (*head)
24516 (*tail)->next = h;
24517 else
24518 *head = h;
24519 h->prev = *tail;
24520 *tail = t;
24521 }
24522 }
24523
24524
24525 /* Prepend the list of glyph strings with head H and tail T to the
24526 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24527 result. */
24528
24529 static void
24530 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24531 struct glyph_string *h, struct glyph_string *t)
24532 {
24533 if (h)
24534 {
24535 if (*head)
24536 (*head)->prev = t;
24537 else
24538 *tail = t;
24539 t->next = *head;
24540 *head = h;
24541 }
24542 }
24543
24544
24545 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24546 Set *HEAD and *TAIL to the resulting list. */
24547
24548 static void
24549 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24550 struct glyph_string *s)
24551 {
24552 s->next = s->prev = NULL;
24553 append_glyph_string_lists (head, tail, s, s);
24554 }
24555
24556
24557 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24558 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24559 make sure that X resources for the face returned are allocated.
24560 Value is a pointer to a realized face that is ready for display if
24561 DISPLAY_P. */
24562
24563 static struct face *
24564 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24565 XChar2b *char2b, bool display_p)
24566 {
24567 struct face *face = FACE_FROM_ID (f, face_id);
24568 unsigned code = 0;
24569
24570 if (face->font)
24571 {
24572 code = face->font->driver->encode_char (face->font, c);
24573
24574 if (code == FONT_INVALID_CODE)
24575 code = 0;
24576 }
24577 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24578
24579 /* Make sure X resources of the face are allocated. */
24580 #ifdef HAVE_X_WINDOWS
24581 if (display_p)
24582 #endif
24583 {
24584 eassert (face != NULL);
24585 prepare_face_for_display (f, face);
24586 }
24587
24588 return face;
24589 }
24590
24591
24592 /* Get face and two-byte form of character glyph GLYPH on frame F.
24593 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24594 a pointer to a realized face that is ready for display. */
24595
24596 static struct face *
24597 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24598 XChar2b *char2b)
24599 {
24600 struct face *face;
24601 unsigned code = 0;
24602
24603 eassert (glyph->type == CHAR_GLYPH);
24604 face = FACE_FROM_ID (f, glyph->face_id);
24605
24606 /* Make sure X resources of the face are allocated. */
24607 eassert (face != NULL);
24608 prepare_face_for_display (f, face);
24609
24610 if (face->font)
24611 {
24612 if (CHAR_BYTE8_P (glyph->u.ch))
24613 code = CHAR_TO_BYTE8 (glyph->u.ch);
24614 else
24615 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24616
24617 if (code == FONT_INVALID_CODE)
24618 code = 0;
24619 }
24620
24621 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24622 return face;
24623 }
24624
24625
24626 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24627 Return true iff FONT has a glyph for C. */
24628
24629 static bool
24630 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24631 {
24632 unsigned code;
24633
24634 if (CHAR_BYTE8_P (c))
24635 code = CHAR_TO_BYTE8 (c);
24636 else
24637 code = font->driver->encode_char (font, c);
24638
24639 if (code == FONT_INVALID_CODE)
24640 return false;
24641 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24642 return true;
24643 }
24644
24645
24646 /* Fill glyph string S with composition components specified by S->cmp.
24647
24648 BASE_FACE is the base face of the composition.
24649 S->cmp_from is the index of the first component for S.
24650
24651 OVERLAPS non-zero means S should draw the foreground only, and use
24652 its physical height for clipping. See also draw_glyphs.
24653
24654 Value is the index of a component not in S. */
24655
24656 static int
24657 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24658 int overlaps)
24659 {
24660 int i;
24661 /* For all glyphs of this composition, starting at the offset
24662 S->cmp_from, until we reach the end of the definition or encounter a
24663 glyph that requires the different face, add it to S. */
24664 struct face *face;
24665
24666 eassert (s);
24667
24668 s->for_overlaps = overlaps;
24669 s->face = NULL;
24670 s->font = NULL;
24671 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24672 {
24673 int c = COMPOSITION_GLYPH (s->cmp, i);
24674
24675 /* TAB in a composition means display glyphs with padding space
24676 on the left or right. */
24677 if (c != '\t')
24678 {
24679 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24680 -1, Qnil);
24681
24682 face = get_char_face_and_encoding (s->f, c, face_id,
24683 s->char2b + i, true);
24684 if (face)
24685 {
24686 if (! s->face)
24687 {
24688 s->face = face;
24689 s->font = s->face->font;
24690 }
24691 else if (s->face != face)
24692 break;
24693 }
24694 }
24695 ++s->nchars;
24696 }
24697 s->cmp_to = i;
24698
24699 if (s->face == NULL)
24700 {
24701 s->face = base_face->ascii_face;
24702 s->font = s->face->font;
24703 }
24704
24705 /* All glyph strings for the same composition has the same width,
24706 i.e. the width set for the first component of the composition. */
24707 s->width = s->first_glyph->pixel_width;
24708
24709 /* If the specified font could not be loaded, use the frame's
24710 default font, but record the fact that we couldn't load it in
24711 the glyph string so that we can draw rectangles for the
24712 characters of the glyph string. */
24713 if (s->font == NULL)
24714 {
24715 s->font_not_found_p = true;
24716 s->font = FRAME_FONT (s->f);
24717 }
24718
24719 /* Adjust base line for subscript/superscript text. */
24720 s->ybase += s->first_glyph->voffset;
24721
24722 return s->cmp_to;
24723 }
24724
24725 static int
24726 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24727 int start, int end, int overlaps)
24728 {
24729 struct glyph *glyph, *last;
24730 Lisp_Object lgstring;
24731 int i;
24732
24733 s->for_overlaps = overlaps;
24734 glyph = s->row->glyphs[s->area] + start;
24735 last = s->row->glyphs[s->area] + end;
24736 s->cmp_id = glyph->u.cmp.id;
24737 s->cmp_from = glyph->slice.cmp.from;
24738 s->cmp_to = glyph->slice.cmp.to + 1;
24739 s->face = FACE_FROM_ID (s->f, face_id);
24740 lgstring = composition_gstring_from_id (s->cmp_id);
24741 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24742 glyph++;
24743 while (glyph < last
24744 && glyph->u.cmp.automatic
24745 && glyph->u.cmp.id == s->cmp_id
24746 && s->cmp_to == glyph->slice.cmp.from)
24747 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24748
24749 for (i = s->cmp_from; i < s->cmp_to; i++)
24750 {
24751 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24752 unsigned code = LGLYPH_CODE (lglyph);
24753
24754 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24755 }
24756 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24757 return glyph - s->row->glyphs[s->area];
24758 }
24759
24760
24761 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24762 See the comment of fill_glyph_string for arguments.
24763 Value is the index of the first glyph not in S. */
24764
24765
24766 static int
24767 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24768 int start, int end, int overlaps)
24769 {
24770 struct glyph *glyph, *last;
24771 int voffset;
24772
24773 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24774 s->for_overlaps = overlaps;
24775 glyph = s->row->glyphs[s->area] + start;
24776 last = s->row->glyphs[s->area] + end;
24777 voffset = glyph->voffset;
24778 s->face = FACE_FROM_ID (s->f, face_id);
24779 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24780 s->nchars = 1;
24781 s->width = glyph->pixel_width;
24782 glyph++;
24783 while (glyph < last
24784 && glyph->type == GLYPHLESS_GLYPH
24785 && glyph->voffset == voffset
24786 && glyph->face_id == face_id)
24787 {
24788 s->nchars++;
24789 s->width += glyph->pixel_width;
24790 glyph++;
24791 }
24792 s->ybase += voffset;
24793 return glyph - s->row->glyphs[s->area];
24794 }
24795
24796
24797 /* Fill glyph string S from a sequence of character glyphs.
24798
24799 FACE_ID is the face id of the string. START is the index of the
24800 first glyph to consider, END is the index of the last + 1.
24801 OVERLAPS non-zero means S should draw the foreground only, and use
24802 its physical height for clipping. See also draw_glyphs.
24803
24804 Value is the index of the first glyph not in S. */
24805
24806 static int
24807 fill_glyph_string (struct glyph_string *s, int face_id,
24808 int start, int end, int overlaps)
24809 {
24810 struct glyph *glyph, *last;
24811 int voffset;
24812 bool glyph_not_available_p;
24813
24814 eassert (s->f == XFRAME (s->w->frame));
24815 eassert (s->nchars == 0);
24816 eassert (start >= 0 && end > start);
24817
24818 s->for_overlaps = overlaps;
24819 glyph = s->row->glyphs[s->area] + start;
24820 last = s->row->glyphs[s->area] + end;
24821 voffset = glyph->voffset;
24822 s->padding_p = glyph->padding_p;
24823 glyph_not_available_p = glyph->glyph_not_available_p;
24824
24825 while (glyph < last
24826 && glyph->type == CHAR_GLYPH
24827 && glyph->voffset == voffset
24828 /* Same face id implies same font, nowadays. */
24829 && glyph->face_id == face_id
24830 && glyph->glyph_not_available_p == glyph_not_available_p)
24831 {
24832 s->face = get_glyph_face_and_encoding (s->f, glyph,
24833 s->char2b + s->nchars);
24834 ++s->nchars;
24835 eassert (s->nchars <= end - start);
24836 s->width += glyph->pixel_width;
24837 if (glyph++->padding_p != s->padding_p)
24838 break;
24839 }
24840
24841 s->font = s->face->font;
24842
24843 /* If the specified font could not be loaded, use the frame's font,
24844 but record the fact that we couldn't load it in
24845 S->font_not_found_p so that we can draw rectangles for the
24846 characters of the glyph string. */
24847 if (s->font == NULL || glyph_not_available_p)
24848 {
24849 s->font_not_found_p = true;
24850 s->font = FRAME_FONT (s->f);
24851 }
24852
24853 /* Adjust base line for subscript/superscript text. */
24854 s->ybase += voffset;
24855
24856 eassert (s->face && s->face->gc);
24857 return glyph - s->row->glyphs[s->area];
24858 }
24859
24860
24861 /* Fill glyph string S from image glyph S->first_glyph. */
24862
24863 static void
24864 fill_image_glyph_string (struct glyph_string *s)
24865 {
24866 eassert (s->first_glyph->type == IMAGE_GLYPH);
24867 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24868 eassert (s->img);
24869 s->slice = s->first_glyph->slice.img;
24870 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24871 s->font = s->face->font;
24872 s->width = s->first_glyph->pixel_width;
24873
24874 /* Adjust base line for subscript/superscript text. */
24875 s->ybase += s->first_glyph->voffset;
24876 }
24877
24878
24879 #ifdef HAVE_XWIDGETS
24880 static void
24881 fill_xwidget_glyph_string (struct glyph_string *s)
24882 {
24883 eassert (s->first_glyph->type == XWIDGET_GLYPH);
24884 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24885 s->font = s->face->font;
24886 s->width = s->first_glyph->pixel_width;
24887 s->ybase += s->first_glyph->voffset;
24888 s->xwidget = s->first_glyph->u.xwidget;
24889 }
24890 #endif
24891 /* Fill glyph string S from a sequence of stretch glyphs.
24892
24893 START is the index of the first glyph to consider,
24894 END is the index of the last + 1.
24895
24896 Value is the index of the first glyph not in S. */
24897
24898 static int
24899 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24900 {
24901 struct glyph *glyph, *last;
24902 int voffset, face_id;
24903
24904 eassert (s->first_glyph->type == STRETCH_GLYPH);
24905
24906 glyph = s->row->glyphs[s->area] + start;
24907 last = s->row->glyphs[s->area] + end;
24908 face_id = glyph->face_id;
24909 s->face = FACE_FROM_ID (s->f, face_id);
24910 s->font = s->face->font;
24911 s->width = glyph->pixel_width;
24912 s->nchars = 1;
24913 voffset = glyph->voffset;
24914
24915 for (++glyph;
24916 (glyph < last
24917 && glyph->type == STRETCH_GLYPH
24918 && glyph->voffset == voffset
24919 && glyph->face_id == face_id);
24920 ++glyph)
24921 s->width += glyph->pixel_width;
24922
24923 /* Adjust base line for subscript/superscript text. */
24924 s->ybase += voffset;
24925
24926 /* The case that face->gc == 0 is handled when drawing the glyph
24927 string by calling prepare_face_for_display. */
24928 eassert (s->face);
24929 return glyph - s->row->glyphs[s->area];
24930 }
24931
24932 static struct font_metrics *
24933 get_per_char_metric (struct font *font, XChar2b *char2b)
24934 {
24935 static struct font_metrics metrics;
24936 unsigned code;
24937
24938 if (! font)
24939 return NULL;
24940 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24941 if (code == FONT_INVALID_CODE)
24942 return NULL;
24943 font->driver->text_extents (font, &code, 1, &metrics);
24944 return &metrics;
24945 }
24946
24947 /* A subroutine that computes "normal" values of ASCENT and DESCENT
24948 for FONT. Values are taken from font-global ones, except for fonts
24949 that claim preposterously large values, but whose glyphs actually
24950 have reasonable dimensions. C is the character to use for metrics
24951 if the font-global values are too large; if C is negative, the
24952 function selects a default character. */
24953 static void
24954 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
24955 {
24956 *ascent = FONT_BASE (font);
24957 *descent = FONT_DESCENT (font);
24958
24959 if (FONT_TOO_HIGH (font))
24960 {
24961 XChar2b char2b;
24962
24963 /* Get metrics of C, defaulting to a reasonably sized ASCII
24964 character. */
24965 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
24966 {
24967 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
24968
24969 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
24970 {
24971 /* We add 1 pixel to character dimensions as heuristics
24972 that produces nicer display, e.g. when the face has
24973 the box attribute. */
24974 *ascent = pcm->ascent + 1;
24975 *descent = pcm->descent + 1;
24976 }
24977 }
24978 }
24979 }
24980
24981 /* A subroutine that computes a reasonable "normal character height"
24982 for fonts that claim preposterously large vertical dimensions, but
24983 whose glyphs are actually reasonably sized. C is the character
24984 whose metrics to use for those fonts, or -1 for default
24985 character. */
24986 static int
24987 normal_char_height (struct font *font, int c)
24988 {
24989 int ascent, descent;
24990
24991 normal_char_ascent_descent (font, c, &ascent, &descent);
24992
24993 return ascent + descent;
24994 }
24995
24996 /* EXPORT for RIF:
24997 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24998 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24999 assumed to be zero. */
25000
25001 void
25002 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
25003 {
25004 *left = *right = 0;
25005
25006 if (glyph->type == CHAR_GLYPH)
25007 {
25008 XChar2b char2b;
25009 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
25010 if (face->font)
25011 {
25012 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
25013 if (pcm)
25014 {
25015 if (pcm->rbearing > pcm->width)
25016 *right = pcm->rbearing - pcm->width;
25017 if (pcm->lbearing < 0)
25018 *left = -pcm->lbearing;
25019 }
25020 }
25021 }
25022 else if (glyph->type == COMPOSITE_GLYPH)
25023 {
25024 if (! glyph->u.cmp.automatic)
25025 {
25026 struct composition *cmp = composition_table[glyph->u.cmp.id];
25027
25028 if (cmp->rbearing > cmp->pixel_width)
25029 *right = cmp->rbearing - cmp->pixel_width;
25030 if (cmp->lbearing < 0)
25031 *left = - cmp->lbearing;
25032 }
25033 else
25034 {
25035 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25036 struct font_metrics metrics;
25037
25038 composition_gstring_width (gstring, glyph->slice.cmp.from,
25039 glyph->slice.cmp.to + 1, &metrics);
25040 if (metrics.rbearing > metrics.width)
25041 *right = metrics.rbearing - metrics.width;
25042 if (metrics.lbearing < 0)
25043 *left = - metrics.lbearing;
25044 }
25045 }
25046 }
25047
25048
25049 /* Return the index of the first glyph preceding glyph string S that
25050 is overwritten by S because of S's left overhang. Value is -1
25051 if no glyphs are overwritten. */
25052
25053 static int
25054 left_overwritten (struct glyph_string *s)
25055 {
25056 int k;
25057
25058 if (s->left_overhang)
25059 {
25060 int x = 0, i;
25061 struct glyph *glyphs = s->row->glyphs[s->area];
25062 int first = s->first_glyph - glyphs;
25063
25064 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25065 x -= glyphs[i].pixel_width;
25066
25067 k = i + 1;
25068 }
25069 else
25070 k = -1;
25071
25072 return k;
25073 }
25074
25075
25076 /* Return the index of the first glyph preceding glyph string S that
25077 is overwriting S because of its right overhang. Value is -1 if no
25078 glyph in front of S overwrites S. */
25079
25080 static int
25081 left_overwriting (struct glyph_string *s)
25082 {
25083 int i, k, x;
25084 struct glyph *glyphs = s->row->glyphs[s->area];
25085 int first = s->first_glyph - glyphs;
25086
25087 k = -1;
25088 x = 0;
25089 for (i = first - 1; i >= 0; --i)
25090 {
25091 int left, right;
25092 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25093 if (x + right > 0)
25094 k = i;
25095 x -= glyphs[i].pixel_width;
25096 }
25097
25098 return k;
25099 }
25100
25101
25102 /* Return the index of the last glyph following glyph string S that is
25103 overwritten by S because of S's right overhang. Value is -1 if
25104 no such glyph is found. */
25105
25106 static int
25107 right_overwritten (struct glyph_string *s)
25108 {
25109 int k = -1;
25110
25111 if (s->right_overhang)
25112 {
25113 int x = 0, i;
25114 struct glyph *glyphs = s->row->glyphs[s->area];
25115 int first = (s->first_glyph - glyphs
25116 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25117 int end = s->row->used[s->area];
25118
25119 for (i = first; i < end && s->right_overhang > x; ++i)
25120 x += glyphs[i].pixel_width;
25121
25122 k = i;
25123 }
25124
25125 return k;
25126 }
25127
25128
25129 /* Return the index of the last glyph following glyph string S that
25130 overwrites S because of its left overhang. Value is negative
25131 if no such glyph is found. */
25132
25133 static int
25134 right_overwriting (struct glyph_string *s)
25135 {
25136 int i, k, x;
25137 int end = s->row->used[s->area];
25138 struct glyph *glyphs = s->row->glyphs[s->area];
25139 int first = (s->first_glyph - glyphs
25140 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25141
25142 k = -1;
25143 x = 0;
25144 for (i = first; i < end; ++i)
25145 {
25146 int left, right;
25147 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25148 if (x - left < 0)
25149 k = i;
25150 x += glyphs[i].pixel_width;
25151 }
25152
25153 return k;
25154 }
25155
25156
25157 /* Set background width of glyph string S. START is the index of the
25158 first glyph following S. LAST_X is the right-most x-position + 1
25159 in the drawing area. */
25160
25161 static void
25162 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
25163 {
25164 /* If the face of this glyph string has to be drawn to the end of
25165 the drawing area, set S->extends_to_end_of_line_p. */
25166
25167 if (start == s->row->used[s->area]
25168 && ((s->row->fill_line_p
25169 && (s->hl == DRAW_NORMAL_TEXT
25170 || s->hl == DRAW_IMAGE_RAISED
25171 || s->hl == DRAW_IMAGE_SUNKEN))
25172 || s->hl == DRAW_MOUSE_FACE))
25173 s->extends_to_end_of_line_p = true;
25174
25175 /* If S extends its face to the end of the line, set its
25176 background_width to the distance to the right edge of the drawing
25177 area. */
25178 if (s->extends_to_end_of_line_p)
25179 s->background_width = last_x - s->x + 1;
25180 else
25181 s->background_width = s->width;
25182 }
25183
25184
25185 /* Compute overhangs and x-positions for glyph string S and its
25186 predecessors, or successors. X is the starting x-position for S.
25187 BACKWARD_P means process predecessors. */
25188
25189 static void
25190 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
25191 {
25192 if (backward_p)
25193 {
25194 while (s)
25195 {
25196 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25197 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25198 x -= s->width;
25199 s->x = x;
25200 s = s->prev;
25201 }
25202 }
25203 else
25204 {
25205 while (s)
25206 {
25207 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25208 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25209 s->x = x;
25210 x += s->width;
25211 s = s->next;
25212 }
25213 }
25214 }
25215
25216
25217
25218 /* The following macros are only called from draw_glyphs below.
25219 They reference the following parameters of that function directly:
25220 `w', `row', `area', and `overlap_p'
25221 as well as the following local variables:
25222 `s', `f', and `hdc' (in W32) */
25223
25224 #ifdef HAVE_NTGUI
25225 /* On W32, silently add local `hdc' variable to argument list of
25226 init_glyph_string. */
25227 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25228 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25229 #else
25230 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25231 init_glyph_string (s, char2b, w, row, area, start, hl)
25232 #endif
25233
25234 /* Add a glyph string for a stretch glyph to the list of strings
25235 between HEAD and TAIL. START is the index of the stretch glyph in
25236 row area AREA of glyph row ROW. END is the index of the last glyph
25237 in that glyph row area. X is the current output position assigned
25238 to the new glyph string constructed. HL overrides that face of the
25239 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25240 is the right-most x-position of the drawing area. */
25241
25242 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25243 and below -- keep them on one line. */
25244 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25245 do \
25246 { \
25247 s = alloca (sizeof *s); \
25248 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25249 START = fill_stretch_glyph_string (s, START, END); \
25250 append_glyph_string (&HEAD, &TAIL, s); \
25251 s->x = (X); \
25252 } \
25253 while (false)
25254
25255
25256 /* Add a glyph string for an image glyph to the list of strings
25257 between HEAD and TAIL. START is the index of the image glyph in
25258 row area AREA of glyph row ROW. END is the index of the last glyph
25259 in that glyph row area. X is the current output position assigned
25260 to the new glyph string constructed. HL overrides that face of the
25261 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25262 is the right-most x-position of the drawing area. */
25263
25264 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25265 do \
25266 { \
25267 s = alloca (sizeof *s); \
25268 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25269 fill_image_glyph_string (s); \
25270 append_glyph_string (&HEAD, &TAIL, s); \
25271 ++START; \
25272 s->x = (X); \
25273 } \
25274 while (false)
25275
25276 #ifdef HAVE_XWIDGETS
25277 #define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25278 do \
25279 { \
25280 s = alloca (sizeof *s); \
25281 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25282 fill_xwidget_glyph_string (s); \
25283 append_glyph_string (&(HEAD), &(TAIL), s); \
25284 ++(START); \
25285 s->x = (X); \
25286 } \
25287 while (false)
25288 #endif
25289
25290
25291 /* Add a glyph string for a sequence of character glyphs to the list
25292 of strings between HEAD and TAIL. START is the index of the first
25293 glyph in row area AREA of glyph row ROW that is part of the new
25294 glyph string. END is the index of the last glyph in that glyph row
25295 area. X is the current output position assigned to the new glyph
25296 string constructed. HL overrides that face of the glyph; e.g. it
25297 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25298 right-most x-position of the drawing area. */
25299
25300 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25301 do \
25302 { \
25303 int face_id; \
25304 XChar2b *char2b; \
25305 \
25306 face_id = (row)->glyphs[area][START].face_id; \
25307 \
25308 s = alloca (sizeof *s); \
25309 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25310 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25311 append_glyph_string (&HEAD, &TAIL, s); \
25312 s->x = (X); \
25313 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25314 } \
25315 while (false)
25316
25317
25318 /* Add a glyph string for a composite sequence to the list of strings
25319 between HEAD and TAIL. START is the index of the first glyph in
25320 row area AREA of glyph row ROW that is part of the new glyph
25321 string. END is the index of the last glyph in that glyph row area.
25322 X is the current output position assigned to the new glyph string
25323 constructed. HL overrides that face of the glyph; e.g. it is
25324 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25325 x-position of the drawing area. */
25326
25327 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25328 do { \
25329 int face_id = (row)->glyphs[area][START].face_id; \
25330 struct face *base_face = FACE_FROM_ID (f, face_id); \
25331 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25332 struct composition *cmp = composition_table[cmp_id]; \
25333 XChar2b *char2b; \
25334 struct glyph_string *first_s = NULL; \
25335 int n; \
25336 \
25337 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25338 \
25339 /* Make glyph_strings for each glyph sequence that is drawable by \
25340 the same face, and append them to HEAD/TAIL. */ \
25341 for (n = 0; n < cmp->glyph_len;) \
25342 { \
25343 s = alloca (sizeof *s); \
25344 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25345 append_glyph_string (&(HEAD), &(TAIL), s); \
25346 s->cmp = cmp; \
25347 s->cmp_from = n; \
25348 s->x = (X); \
25349 if (n == 0) \
25350 first_s = s; \
25351 n = fill_composite_glyph_string (s, base_face, overlaps); \
25352 } \
25353 \
25354 ++START; \
25355 s = first_s; \
25356 } while (false)
25357
25358
25359 /* Add a glyph string for a glyph-string sequence to the list of strings
25360 between HEAD and TAIL. */
25361
25362 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25363 do { \
25364 int face_id; \
25365 XChar2b *char2b; \
25366 Lisp_Object gstring; \
25367 \
25368 face_id = (row)->glyphs[area][START].face_id; \
25369 gstring = (composition_gstring_from_id \
25370 ((row)->glyphs[area][START].u.cmp.id)); \
25371 s = alloca (sizeof *s); \
25372 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25373 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25374 append_glyph_string (&(HEAD), &(TAIL), s); \
25375 s->x = (X); \
25376 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25377 } while (false)
25378
25379
25380 /* Add a glyph string for a sequence of glyphless character's glyphs
25381 to the list of strings between HEAD and TAIL. The meanings of
25382 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25383
25384 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25385 do \
25386 { \
25387 int face_id; \
25388 \
25389 face_id = (row)->glyphs[area][START].face_id; \
25390 \
25391 s = alloca (sizeof *s); \
25392 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25393 append_glyph_string (&HEAD, &TAIL, s); \
25394 s->x = (X); \
25395 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25396 overlaps); \
25397 } \
25398 while (false)
25399
25400
25401 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25402 of AREA of glyph row ROW on window W between indices START and END.
25403 HL overrides the face for drawing glyph strings, e.g. it is
25404 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25405 x-positions of the drawing area.
25406
25407 This is an ugly monster macro construct because we must use alloca
25408 to allocate glyph strings (because draw_glyphs can be called
25409 asynchronously). */
25410
25411 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25412 do \
25413 { \
25414 HEAD = TAIL = NULL; \
25415 while (START < END) \
25416 { \
25417 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25418 switch (first_glyph->type) \
25419 { \
25420 case CHAR_GLYPH: \
25421 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25422 HL, X, LAST_X); \
25423 break; \
25424 \
25425 case COMPOSITE_GLYPH: \
25426 if (first_glyph->u.cmp.automatic) \
25427 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25428 HL, X, LAST_X); \
25429 else \
25430 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25431 HL, X, LAST_X); \
25432 break; \
25433 \
25434 case STRETCH_GLYPH: \
25435 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25436 HL, X, LAST_X); \
25437 break; \
25438 \
25439 case IMAGE_GLYPH: \
25440 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25441 HL, X, LAST_X); \
25442 break;
25443
25444 #ifdef HAVE_XWIDGETS
25445 # define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25446 case XWIDGET_GLYPH: \
25447 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
25448 HL, X, LAST_X); \
25449 break;
25450 #endif
25451
25452 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
25453 case GLYPHLESS_GLYPH: \
25454 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25455 HL, X, LAST_X); \
25456 break; \
25457 \
25458 default: \
25459 emacs_abort (); \
25460 } \
25461 \
25462 if (s) \
25463 { \
25464 set_glyph_string_background_width (s, START, LAST_X); \
25465 (X) += s->width; \
25466 } \
25467 } \
25468 } while (false)
25469
25470
25471 #ifdef HAVE_XWIDGETS
25472 # define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25473 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25474 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25475 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
25476 #else
25477 # define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25478 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25479 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
25480 #endif
25481
25482
25483 /* Draw glyphs between START and END in AREA of ROW on window W,
25484 starting at x-position X. X is relative to AREA in W. HL is a
25485 face-override with the following meaning:
25486
25487 DRAW_NORMAL_TEXT draw normally
25488 DRAW_CURSOR draw in cursor face
25489 DRAW_MOUSE_FACE draw in mouse face.
25490 DRAW_INVERSE_VIDEO draw in mode line face
25491 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25492 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25493
25494 If OVERLAPS is non-zero, draw only the foreground of characters and
25495 clip to the physical height of ROW. Non-zero value also defines
25496 the overlapping part to be drawn:
25497
25498 OVERLAPS_PRED overlap with preceding rows
25499 OVERLAPS_SUCC overlap with succeeding rows
25500 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25501 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25502
25503 Value is the x-position reached, relative to AREA of W. */
25504
25505 static int
25506 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25507 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25508 enum draw_glyphs_face hl, int overlaps)
25509 {
25510 struct glyph_string *head, *tail;
25511 struct glyph_string *s;
25512 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25513 int i, j, x_reached, last_x, area_left = 0;
25514 struct frame *f = XFRAME (WINDOW_FRAME (w));
25515 DECLARE_HDC (hdc);
25516
25517 ALLOCATE_HDC (hdc, f);
25518
25519 /* Let's rather be paranoid than getting a SEGV. */
25520 end = min (end, row->used[area]);
25521 start = clip_to_bounds (0, start, end);
25522
25523 /* Translate X to frame coordinates. Set last_x to the right
25524 end of the drawing area. */
25525 if (row->full_width_p)
25526 {
25527 /* X is relative to the left edge of W, without scroll bars
25528 or fringes. */
25529 area_left = WINDOW_LEFT_EDGE_X (w);
25530 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25531 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25532 }
25533 else
25534 {
25535 area_left = window_box_left (w, area);
25536 last_x = area_left + window_box_width (w, area);
25537 }
25538 x += area_left;
25539
25540 /* Build a doubly-linked list of glyph_string structures between
25541 head and tail from what we have to draw. Note that the macro
25542 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25543 the reason we use a separate variable `i'. */
25544 i = start;
25545 USE_SAFE_ALLOCA;
25546 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25547 if (tail)
25548 x_reached = tail->x + tail->background_width;
25549 else
25550 x_reached = x;
25551
25552 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25553 the row, redraw some glyphs in front or following the glyph
25554 strings built above. */
25555 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25556 {
25557 struct glyph_string *h, *t;
25558 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25559 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25560 bool check_mouse_face = false;
25561 int dummy_x = 0;
25562
25563 /* If mouse highlighting is on, we may need to draw adjacent
25564 glyphs using mouse-face highlighting. */
25565 if (area == TEXT_AREA && row->mouse_face_p
25566 && hlinfo->mouse_face_beg_row >= 0
25567 && hlinfo->mouse_face_end_row >= 0)
25568 {
25569 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25570
25571 if (row_vpos >= hlinfo->mouse_face_beg_row
25572 && row_vpos <= hlinfo->mouse_face_end_row)
25573 {
25574 check_mouse_face = true;
25575 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25576 ? hlinfo->mouse_face_beg_col : 0;
25577 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25578 ? hlinfo->mouse_face_end_col
25579 : row->used[TEXT_AREA];
25580 }
25581 }
25582
25583 /* Compute overhangs for all glyph strings. */
25584 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25585 for (s = head; s; s = s->next)
25586 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25587
25588 /* Prepend glyph strings for glyphs in front of the first glyph
25589 string that are overwritten because of the first glyph
25590 string's left overhang. The background of all strings
25591 prepended must be drawn because the first glyph string
25592 draws over it. */
25593 i = left_overwritten (head);
25594 if (i >= 0)
25595 {
25596 enum draw_glyphs_face overlap_hl;
25597
25598 /* If this row contains mouse highlighting, attempt to draw
25599 the overlapped glyphs with the correct highlight. This
25600 code fails if the overlap encompasses more than one glyph
25601 and mouse-highlight spans only some of these glyphs.
25602 However, making it work perfectly involves a lot more
25603 code, and I don't know if the pathological case occurs in
25604 practice, so we'll stick to this for now. --- cyd */
25605 if (check_mouse_face
25606 && mouse_beg_col < start && mouse_end_col > i)
25607 overlap_hl = DRAW_MOUSE_FACE;
25608 else
25609 overlap_hl = DRAW_NORMAL_TEXT;
25610
25611 if (hl != overlap_hl)
25612 clip_head = head;
25613 j = i;
25614 BUILD_GLYPH_STRINGS (j, start, h, t,
25615 overlap_hl, dummy_x, last_x);
25616 start = i;
25617 compute_overhangs_and_x (t, head->x, true);
25618 prepend_glyph_string_lists (&head, &tail, h, t);
25619 if (clip_head == NULL)
25620 clip_head = head;
25621 }
25622
25623 /* Prepend glyph strings for glyphs in front of the first glyph
25624 string that overwrite that glyph string because of their
25625 right overhang. For these strings, only the foreground must
25626 be drawn, because it draws over the glyph string at `head'.
25627 The background must not be drawn because this would overwrite
25628 right overhangs of preceding glyphs for which no glyph
25629 strings exist. */
25630 i = left_overwriting (head);
25631 if (i >= 0)
25632 {
25633 enum draw_glyphs_face overlap_hl;
25634
25635 if (check_mouse_face
25636 && mouse_beg_col < start && mouse_end_col > i)
25637 overlap_hl = DRAW_MOUSE_FACE;
25638 else
25639 overlap_hl = DRAW_NORMAL_TEXT;
25640
25641 if (hl == overlap_hl || clip_head == NULL)
25642 clip_head = head;
25643 BUILD_GLYPH_STRINGS (i, start, h, t,
25644 overlap_hl, dummy_x, last_x);
25645 for (s = h; s; s = s->next)
25646 s->background_filled_p = true;
25647 compute_overhangs_and_x (t, head->x, true);
25648 prepend_glyph_string_lists (&head, &tail, h, t);
25649 }
25650
25651 /* Append glyphs strings for glyphs following the last glyph
25652 string tail that are overwritten by tail. The background of
25653 these strings has to be drawn because tail's foreground draws
25654 over it. */
25655 i = right_overwritten (tail);
25656 if (i >= 0)
25657 {
25658 enum draw_glyphs_face overlap_hl;
25659
25660 if (check_mouse_face
25661 && mouse_beg_col < i && mouse_end_col > end)
25662 overlap_hl = DRAW_MOUSE_FACE;
25663 else
25664 overlap_hl = DRAW_NORMAL_TEXT;
25665
25666 if (hl != overlap_hl)
25667 clip_tail = tail;
25668 BUILD_GLYPH_STRINGS (end, i, h, t,
25669 overlap_hl, x, last_x);
25670 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25671 we don't have `end = i;' here. */
25672 compute_overhangs_and_x (h, tail->x + tail->width, false);
25673 append_glyph_string_lists (&head, &tail, h, t);
25674 if (clip_tail == NULL)
25675 clip_tail = tail;
25676 }
25677
25678 /* Append glyph strings for glyphs following the last glyph
25679 string tail that overwrite tail. The foreground of such
25680 glyphs has to be drawn because it writes into the background
25681 of tail. The background must not be drawn because it could
25682 paint over the foreground of following glyphs. */
25683 i = right_overwriting (tail);
25684 if (i >= 0)
25685 {
25686 enum draw_glyphs_face overlap_hl;
25687 if (check_mouse_face
25688 && mouse_beg_col < i && mouse_end_col > end)
25689 overlap_hl = DRAW_MOUSE_FACE;
25690 else
25691 overlap_hl = DRAW_NORMAL_TEXT;
25692
25693 if (hl == overlap_hl || clip_tail == NULL)
25694 clip_tail = tail;
25695 i++; /* We must include the Ith glyph. */
25696 BUILD_GLYPH_STRINGS (end, i, h, t,
25697 overlap_hl, x, last_x);
25698 for (s = h; s; s = s->next)
25699 s->background_filled_p = true;
25700 compute_overhangs_and_x (h, tail->x + tail->width, false);
25701 append_glyph_string_lists (&head, &tail, h, t);
25702 }
25703 if (clip_head || clip_tail)
25704 for (s = head; s; s = s->next)
25705 {
25706 s->clip_head = clip_head;
25707 s->clip_tail = clip_tail;
25708 }
25709 }
25710
25711 /* Draw all strings. */
25712 for (s = head; s; s = s->next)
25713 FRAME_RIF (f)->draw_glyph_string (s);
25714
25715 #ifndef HAVE_NS
25716 /* When focus a sole frame and move horizontally, this clears on_p
25717 causing a failure to erase prev cursor position. */
25718 if (area == TEXT_AREA
25719 && !row->full_width_p
25720 /* When drawing overlapping rows, only the glyph strings'
25721 foreground is drawn, which doesn't erase a cursor
25722 completely. */
25723 && !overlaps)
25724 {
25725 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25726 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25727 : (tail ? tail->x + tail->background_width : x));
25728 x0 -= area_left;
25729 x1 -= area_left;
25730
25731 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25732 row->y, MATRIX_ROW_BOTTOM_Y (row));
25733 }
25734 #endif
25735
25736 /* Value is the x-position up to which drawn, relative to AREA of W.
25737 This doesn't include parts drawn because of overhangs. */
25738 if (row->full_width_p)
25739 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25740 else
25741 x_reached -= area_left;
25742
25743 RELEASE_HDC (hdc, f);
25744
25745 SAFE_FREE ();
25746 return x_reached;
25747 }
25748
25749 /* Expand row matrix if too narrow. Don't expand if area
25750 is not present. */
25751
25752 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25753 { \
25754 if (!it->f->fonts_changed \
25755 && (it->glyph_row->glyphs[area] \
25756 < it->glyph_row->glyphs[area + 1])) \
25757 { \
25758 it->w->ncols_scale_factor++; \
25759 it->f->fonts_changed = true; \
25760 } \
25761 }
25762
25763 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25764 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25765
25766 static void
25767 append_glyph (struct it *it)
25768 {
25769 struct glyph *glyph;
25770 enum glyph_row_area area = it->area;
25771
25772 eassert (it->glyph_row);
25773 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25774
25775 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25776 if (glyph < it->glyph_row->glyphs[area + 1])
25777 {
25778 /* If the glyph row is reversed, we need to prepend the glyph
25779 rather than append it. */
25780 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25781 {
25782 struct glyph *g;
25783
25784 /* Make room for the additional glyph. */
25785 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25786 g[1] = *g;
25787 glyph = it->glyph_row->glyphs[area];
25788 }
25789 glyph->charpos = CHARPOS (it->position);
25790 glyph->object = it->object;
25791 if (it->pixel_width > 0)
25792 {
25793 glyph->pixel_width = it->pixel_width;
25794 glyph->padding_p = false;
25795 }
25796 else
25797 {
25798 /* Assure at least 1-pixel width. Otherwise, cursor can't
25799 be displayed correctly. */
25800 glyph->pixel_width = 1;
25801 glyph->padding_p = true;
25802 }
25803 glyph->ascent = it->ascent;
25804 glyph->descent = it->descent;
25805 glyph->voffset = it->voffset;
25806 glyph->type = CHAR_GLYPH;
25807 glyph->avoid_cursor_p = it->avoid_cursor_p;
25808 glyph->multibyte_p = it->multibyte_p;
25809 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25810 {
25811 /* In R2L rows, the left and the right box edges need to be
25812 drawn in reverse direction. */
25813 glyph->right_box_line_p = it->start_of_box_run_p;
25814 glyph->left_box_line_p = it->end_of_box_run_p;
25815 }
25816 else
25817 {
25818 glyph->left_box_line_p = it->start_of_box_run_p;
25819 glyph->right_box_line_p = it->end_of_box_run_p;
25820 }
25821 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25822 || it->phys_descent > it->descent);
25823 glyph->glyph_not_available_p = it->glyph_not_available_p;
25824 glyph->face_id = it->face_id;
25825 glyph->u.ch = it->char_to_display;
25826 glyph->slice.img = null_glyph_slice;
25827 glyph->font_type = FONT_TYPE_UNKNOWN;
25828 if (it->bidi_p)
25829 {
25830 glyph->resolved_level = it->bidi_it.resolved_level;
25831 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25832 glyph->bidi_type = it->bidi_it.type;
25833 }
25834 else
25835 {
25836 glyph->resolved_level = 0;
25837 glyph->bidi_type = UNKNOWN_BT;
25838 }
25839 ++it->glyph_row->used[area];
25840 }
25841 else
25842 IT_EXPAND_MATRIX_WIDTH (it, area);
25843 }
25844
25845 /* Store one glyph for the composition IT->cmp_it.id in
25846 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25847 non-null. */
25848
25849 static void
25850 append_composite_glyph (struct it *it)
25851 {
25852 struct glyph *glyph;
25853 enum glyph_row_area area = it->area;
25854
25855 eassert (it->glyph_row);
25856
25857 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25858 if (glyph < it->glyph_row->glyphs[area + 1])
25859 {
25860 /* If the glyph row is reversed, we need to prepend the glyph
25861 rather than append it. */
25862 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25863 {
25864 struct glyph *g;
25865
25866 /* Make room for the new glyph. */
25867 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25868 g[1] = *g;
25869 glyph = it->glyph_row->glyphs[it->area];
25870 }
25871 glyph->charpos = it->cmp_it.charpos;
25872 glyph->object = it->object;
25873 glyph->pixel_width = it->pixel_width;
25874 glyph->ascent = it->ascent;
25875 glyph->descent = it->descent;
25876 glyph->voffset = it->voffset;
25877 glyph->type = COMPOSITE_GLYPH;
25878 if (it->cmp_it.ch < 0)
25879 {
25880 glyph->u.cmp.automatic = false;
25881 glyph->u.cmp.id = it->cmp_it.id;
25882 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25883 }
25884 else
25885 {
25886 glyph->u.cmp.automatic = true;
25887 glyph->u.cmp.id = it->cmp_it.id;
25888 glyph->slice.cmp.from = it->cmp_it.from;
25889 glyph->slice.cmp.to = it->cmp_it.to - 1;
25890 }
25891 glyph->avoid_cursor_p = it->avoid_cursor_p;
25892 glyph->multibyte_p = it->multibyte_p;
25893 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25894 {
25895 /* In R2L rows, the left and the right box edges need to be
25896 drawn in reverse direction. */
25897 glyph->right_box_line_p = it->start_of_box_run_p;
25898 glyph->left_box_line_p = it->end_of_box_run_p;
25899 }
25900 else
25901 {
25902 glyph->left_box_line_p = it->start_of_box_run_p;
25903 glyph->right_box_line_p = it->end_of_box_run_p;
25904 }
25905 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25906 || it->phys_descent > it->descent);
25907 glyph->padding_p = false;
25908 glyph->glyph_not_available_p = false;
25909 glyph->face_id = it->face_id;
25910 glyph->font_type = FONT_TYPE_UNKNOWN;
25911 if (it->bidi_p)
25912 {
25913 glyph->resolved_level = it->bidi_it.resolved_level;
25914 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25915 glyph->bidi_type = it->bidi_it.type;
25916 }
25917 ++it->glyph_row->used[area];
25918 }
25919 else
25920 IT_EXPAND_MATRIX_WIDTH (it, area);
25921 }
25922
25923
25924 /* Change IT->ascent and IT->height according to the setting of
25925 IT->voffset. */
25926
25927 static void
25928 take_vertical_position_into_account (struct it *it)
25929 {
25930 if (it->voffset)
25931 {
25932 if (it->voffset < 0)
25933 /* Increase the ascent so that we can display the text higher
25934 in the line. */
25935 it->ascent -= it->voffset;
25936 else
25937 /* Increase the descent so that we can display the text lower
25938 in the line. */
25939 it->descent += it->voffset;
25940 }
25941 }
25942
25943
25944 /* Produce glyphs/get display metrics for the image IT is loaded with.
25945 See the description of struct display_iterator in dispextern.h for
25946 an overview of struct display_iterator. */
25947
25948 static void
25949 produce_image_glyph (struct it *it)
25950 {
25951 struct image *img;
25952 struct face *face;
25953 int glyph_ascent, crop;
25954 struct glyph_slice slice;
25955
25956 eassert (it->what == IT_IMAGE);
25957
25958 face = FACE_FROM_ID (it->f, it->face_id);
25959 eassert (face);
25960 /* Make sure X resources of the face is loaded. */
25961 prepare_face_for_display (it->f, face);
25962
25963 if (it->image_id < 0)
25964 {
25965 /* Fringe bitmap. */
25966 it->ascent = it->phys_ascent = 0;
25967 it->descent = it->phys_descent = 0;
25968 it->pixel_width = 0;
25969 it->nglyphs = 0;
25970 return;
25971 }
25972
25973 img = IMAGE_FROM_ID (it->f, it->image_id);
25974 eassert (img);
25975 /* Make sure X resources of the image is loaded. */
25976 prepare_image_for_display (it->f, img);
25977
25978 slice.x = slice.y = 0;
25979 slice.width = img->width;
25980 slice.height = img->height;
25981
25982 if (INTEGERP (it->slice.x))
25983 slice.x = XINT (it->slice.x);
25984 else if (FLOATP (it->slice.x))
25985 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25986
25987 if (INTEGERP (it->slice.y))
25988 slice.y = XINT (it->slice.y);
25989 else if (FLOATP (it->slice.y))
25990 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25991
25992 if (INTEGERP (it->slice.width))
25993 slice.width = XINT (it->slice.width);
25994 else if (FLOATP (it->slice.width))
25995 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25996
25997 if (INTEGERP (it->slice.height))
25998 slice.height = XINT (it->slice.height);
25999 else if (FLOATP (it->slice.height))
26000 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
26001
26002 if (slice.x >= img->width)
26003 slice.x = img->width;
26004 if (slice.y >= img->height)
26005 slice.y = img->height;
26006 if (slice.x + slice.width >= img->width)
26007 slice.width = img->width - slice.x;
26008 if (slice.y + slice.height > img->height)
26009 slice.height = img->height - slice.y;
26010
26011 if (slice.width == 0 || slice.height == 0)
26012 return;
26013
26014 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
26015
26016 it->descent = slice.height - glyph_ascent;
26017 if (slice.y == 0)
26018 it->descent += img->vmargin;
26019 if (slice.y + slice.height == img->height)
26020 it->descent += img->vmargin;
26021 it->phys_descent = it->descent;
26022
26023 it->pixel_width = slice.width;
26024 if (slice.x == 0)
26025 it->pixel_width += img->hmargin;
26026 if (slice.x + slice.width == img->width)
26027 it->pixel_width += img->hmargin;
26028
26029 /* It's quite possible for images to have an ascent greater than
26030 their height, so don't get confused in that case. */
26031 if (it->descent < 0)
26032 it->descent = 0;
26033
26034 it->nglyphs = 1;
26035
26036 if (face->box != FACE_NO_BOX)
26037 {
26038 if (face->box_line_width > 0)
26039 {
26040 if (slice.y == 0)
26041 it->ascent += face->box_line_width;
26042 if (slice.y + slice.height == img->height)
26043 it->descent += face->box_line_width;
26044 }
26045
26046 if (it->start_of_box_run_p && slice.x == 0)
26047 it->pixel_width += eabs (face->box_line_width);
26048 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
26049 it->pixel_width += eabs (face->box_line_width);
26050 }
26051
26052 take_vertical_position_into_account (it);
26053
26054 /* Automatically crop wide image glyphs at right edge so we can
26055 draw the cursor on same display row. */
26056 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
26057 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26058 {
26059 it->pixel_width -= crop;
26060 slice.width -= crop;
26061 }
26062
26063 if (it->glyph_row)
26064 {
26065 struct glyph *glyph;
26066 enum glyph_row_area area = it->area;
26067
26068 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26069 if (it->glyph_row->reversed_p)
26070 {
26071 struct glyph *g;
26072
26073 /* Make room for the new glyph. */
26074 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26075 g[1] = *g;
26076 glyph = it->glyph_row->glyphs[it->area];
26077 }
26078 if (glyph < it->glyph_row->glyphs[area + 1])
26079 {
26080 glyph->charpos = CHARPOS (it->position);
26081 glyph->object = it->object;
26082 glyph->pixel_width = it->pixel_width;
26083 glyph->ascent = glyph_ascent;
26084 glyph->descent = it->descent;
26085 glyph->voffset = it->voffset;
26086 glyph->type = IMAGE_GLYPH;
26087 glyph->avoid_cursor_p = it->avoid_cursor_p;
26088 glyph->multibyte_p = it->multibyte_p;
26089 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26090 {
26091 /* In R2L rows, the left and the right box edges need to be
26092 drawn in reverse direction. */
26093 glyph->right_box_line_p = it->start_of_box_run_p;
26094 glyph->left_box_line_p = it->end_of_box_run_p;
26095 }
26096 else
26097 {
26098 glyph->left_box_line_p = it->start_of_box_run_p;
26099 glyph->right_box_line_p = it->end_of_box_run_p;
26100 }
26101 glyph->overlaps_vertically_p = false;
26102 glyph->padding_p = false;
26103 glyph->glyph_not_available_p = false;
26104 glyph->face_id = it->face_id;
26105 glyph->u.img_id = img->id;
26106 glyph->slice.img = slice;
26107 glyph->font_type = FONT_TYPE_UNKNOWN;
26108 if (it->bidi_p)
26109 {
26110 glyph->resolved_level = it->bidi_it.resolved_level;
26111 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26112 glyph->bidi_type = it->bidi_it.type;
26113 }
26114 ++it->glyph_row->used[area];
26115 }
26116 else
26117 IT_EXPAND_MATRIX_WIDTH (it, area);
26118 }
26119 }
26120
26121 #ifdef HAVE_XWIDGETS
26122 static void
26123 produce_xwidget_glyph (struct it *it)
26124 {
26125 struct xwidget *xw;
26126 int glyph_ascent, crop;
26127 eassert (it->what == IT_XWIDGET);
26128
26129 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26130 eassert (face);
26131 /* Make sure X resources of the face is loaded. */
26132 prepare_face_for_display (it->f, face);
26133
26134 xw = it->xwidget;
26135 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
26136 it->descent = xw->height/2;
26137 it->phys_descent = it->descent;
26138 it->pixel_width = xw->width;
26139 /* It's quite possible for images to have an ascent greater than
26140 their height, so don't get confused in that case. */
26141 if (it->descent < 0)
26142 it->descent = 0;
26143
26144 it->nglyphs = 1;
26145
26146 if (face->box != FACE_NO_BOX)
26147 {
26148 if (face->box_line_width > 0)
26149 {
26150 it->ascent += face->box_line_width;
26151 it->descent += face->box_line_width;
26152 }
26153
26154 if (it->start_of_box_run_p)
26155 it->pixel_width += eabs (face->box_line_width);
26156 it->pixel_width += eabs (face->box_line_width);
26157 }
26158
26159 take_vertical_position_into_account (it);
26160
26161 /* Automatically crop wide image glyphs at right edge so we can
26162 draw the cursor on same display row. */
26163 crop = it->pixel_width - (it->last_visible_x - it->current_x);
26164 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26165 it->pixel_width -= crop;
26166
26167 if (it->glyph_row)
26168 {
26169 enum glyph_row_area area = it->area;
26170 struct glyph *glyph
26171 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26172
26173 if (it->glyph_row->reversed_p)
26174 {
26175 struct glyph *g;
26176
26177 /* Make room for the new glyph. */
26178 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26179 g[1] = *g;
26180 glyph = it->glyph_row->glyphs[it->area];
26181 }
26182 if (glyph < it->glyph_row->glyphs[area + 1])
26183 {
26184 glyph->charpos = CHARPOS (it->position);
26185 glyph->object = it->object;
26186 glyph->pixel_width = it->pixel_width;
26187 glyph->ascent = glyph_ascent;
26188 glyph->descent = it->descent;
26189 glyph->voffset = it->voffset;
26190 glyph->type = XWIDGET_GLYPH;
26191 glyph->avoid_cursor_p = it->avoid_cursor_p;
26192 glyph->multibyte_p = it->multibyte_p;
26193 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26194 {
26195 /* In R2L rows, the left and the right box edges need to be
26196 drawn in reverse direction. */
26197 glyph->right_box_line_p = it->start_of_box_run_p;
26198 glyph->left_box_line_p = it->end_of_box_run_p;
26199 }
26200 else
26201 {
26202 glyph->left_box_line_p = it->start_of_box_run_p;
26203 glyph->right_box_line_p = it->end_of_box_run_p;
26204 }
26205 glyph->overlaps_vertically_p = 0;
26206 glyph->padding_p = 0;
26207 glyph->glyph_not_available_p = 0;
26208 glyph->face_id = it->face_id;
26209 glyph->u.xwidget = it->xwidget;
26210 glyph->font_type = FONT_TYPE_UNKNOWN;
26211 if (it->bidi_p)
26212 {
26213 glyph->resolved_level = it->bidi_it.resolved_level;
26214 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26215 glyph->bidi_type = it->bidi_it.type;
26216 }
26217 ++it->glyph_row->used[area];
26218 }
26219 else
26220 IT_EXPAND_MATRIX_WIDTH (it, area);
26221 }
26222 }
26223 #endif
26224
26225 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
26226 of the glyph, WIDTH and HEIGHT are the width and height of the
26227 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
26228
26229 static void
26230 append_stretch_glyph (struct it *it, Lisp_Object object,
26231 int width, int height, int ascent)
26232 {
26233 struct glyph *glyph;
26234 enum glyph_row_area area = it->area;
26235
26236 eassert (ascent >= 0 && ascent <= height);
26237
26238 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26239 if (glyph < it->glyph_row->glyphs[area + 1])
26240 {
26241 /* If the glyph row is reversed, we need to prepend the glyph
26242 rather than append it. */
26243 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26244 {
26245 struct glyph *g;
26246
26247 /* Make room for the additional glyph. */
26248 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26249 g[1] = *g;
26250 glyph = it->glyph_row->glyphs[area];
26251
26252 /* Decrease the width of the first glyph of the row that
26253 begins before first_visible_x (e.g., due to hscroll).
26254 This is so the overall width of the row becomes smaller
26255 by the scroll amount, and the stretch glyph appended by
26256 extend_face_to_end_of_line will be wider, to shift the
26257 row glyphs to the right. (In L2R rows, the corresponding
26258 left-shift effect is accomplished by setting row->x to a
26259 negative value, which won't work with R2L rows.)
26260
26261 This must leave us with a positive value of WIDTH, since
26262 otherwise the call to move_it_in_display_line_to at the
26263 beginning of display_line would have got past the entire
26264 first glyph, and then it->current_x would have been
26265 greater or equal to it->first_visible_x. */
26266 if (it->current_x < it->first_visible_x)
26267 width -= it->first_visible_x - it->current_x;
26268 eassert (width > 0);
26269 }
26270 glyph->charpos = CHARPOS (it->position);
26271 glyph->object = object;
26272 glyph->pixel_width = width;
26273 glyph->ascent = ascent;
26274 glyph->descent = height - ascent;
26275 glyph->voffset = it->voffset;
26276 glyph->type = STRETCH_GLYPH;
26277 glyph->avoid_cursor_p = it->avoid_cursor_p;
26278 glyph->multibyte_p = it->multibyte_p;
26279 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26280 {
26281 /* In R2L rows, the left and the right box edges need to be
26282 drawn in reverse direction. */
26283 glyph->right_box_line_p = it->start_of_box_run_p;
26284 glyph->left_box_line_p = it->end_of_box_run_p;
26285 }
26286 else
26287 {
26288 glyph->left_box_line_p = it->start_of_box_run_p;
26289 glyph->right_box_line_p = it->end_of_box_run_p;
26290 }
26291 glyph->overlaps_vertically_p = false;
26292 glyph->padding_p = false;
26293 glyph->glyph_not_available_p = false;
26294 glyph->face_id = it->face_id;
26295 glyph->u.stretch.ascent = ascent;
26296 glyph->u.stretch.height = height;
26297 glyph->slice.img = null_glyph_slice;
26298 glyph->font_type = FONT_TYPE_UNKNOWN;
26299 if (it->bidi_p)
26300 {
26301 glyph->resolved_level = it->bidi_it.resolved_level;
26302 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26303 glyph->bidi_type = it->bidi_it.type;
26304 }
26305 else
26306 {
26307 glyph->resolved_level = 0;
26308 glyph->bidi_type = UNKNOWN_BT;
26309 }
26310 ++it->glyph_row->used[area];
26311 }
26312 else
26313 IT_EXPAND_MATRIX_WIDTH (it, area);
26314 }
26315
26316 #endif /* HAVE_WINDOW_SYSTEM */
26317
26318 /* Produce a stretch glyph for iterator IT. IT->object is the value
26319 of the glyph property displayed. The value must be a list
26320 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
26321 being recognized:
26322
26323 1. `:width WIDTH' specifies that the space should be WIDTH *
26324 canonical char width wide. WIDTH may be an integer or floating
26325 point number.
26326
26327 2. `:relative-width FACTOR' specifies that the width of the stretch
26328 should be computed from the width of the first character having the
26329 `glyph' property, and should be FACTOR times that width.
26330
26331 3. `:align-to HPOS' specifies that the space should be wide enough
26332 to reach HPOS, a value in canonical character units.
26333
26334 Exactly one of the above pairs must be present.
26335
26336 4. `:height HEIGHT' specifies that the height of the stretch produced
26337 should be HEIGHT, measured in canonical character units.
26338
26339 5. `:relative-height FACTOR' specifies that the height of the
26340 stretch should be FACTOR times the height of the characters having
26341 the glyph property.
26342
26343 Either none or exactly one of 4 or 5 must be present.
26344
26345 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26346 of the stretch should be used for the ascent of the stretch.
26347 ASCENT must be in the range 0 <= ASCENT <= 100. */
26348
26349 void
26350 produce_stretch_glyph (struct it *it)
26351 {
26352 /* (space :width WIDTH :height HEIGHT ...) */
26353 Lisp_Object prop, plist;
26354 int width = 0, height = 0, align_to = -1;
26355 bool zero_width_ok_p = false;
26356 double tem;
26357 struct font *font = NULL;
26358
26359 #ifdef HAVE_WINDOW_SYSTEM
26360 int ascent = 0;
26361 bool zero_height_ok_p = false;
26362
26363 if (FRAME_WINDOW_P (it->f))
26364 {
26365 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26366 font = face->font ? face->font : FRAME_FONT (it->f);
26367 prepare_face_for_display (it->f, face);
26368 }
26369 #endif
26370
26371 /* List should start with `space'. */
26372 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26373 plist = XCDR (it->object);
26374
26375 /* Compute the width of the stretch. */
26376 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26377 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26378 {
26379 /* Absolute width `:width WIDTH' specified and valid. */
26380 zero_width_ok_p = true;
26381 width = (int)tem;
26382 }
26383 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26384 {
26385 /* Relative width `:relative-width FACTOR' specified and valid.
26386 Compute the width of the characters having the `glyph'
26387 property. */
26388 struct it it2;
26389 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26390
26391 it2 = *it;
26392 if (it->multibyte_p)
26393 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26394 else
26395 {
26396 it2.c = it2.char_to_display = *p, it2.len = 1;
26397 if (! ASCII_CHAR_P (it2.c))
26398 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26399 }
26400
26401 it2.glyph_row = NULL;
26402 it2.what = IT_CHARACTER;
26403 PRODUCE_GLYPHS (&it2);
26404 width = NUMVAL (prop) * it2.pixel_width;
26405 }
26406 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26407 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26408 &align_to))
26409 {
26410 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26411 align_to = (align_to < 0
26412 ? 0
26413 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26414 else if (align_to < 0)
26415 align_to = window_box_left_offset (it->w, TEXT_AREA);
26416 width = max (0, (int)tem + align_to - it->current_x);
26417 zero_width_ok_p = true;
26418 }
26419 else
26420 /* Nothing specified -> width defaults to canonical char width. */
26421 width = FRAME_COLUMN_WIDTH (it->f);
26422
26423 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26424 width = 1;
26425
26426 #ifdef HAVE_WINDOW_SYSTEM
26427 /* Compute height. */
26428 if (FRAME_WINDOW_P (it->f))
26429 {
26430 int default_height = normal_char_height (font, ' ');
26431
26432 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26433 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26434 {
26435 height = (int)tem;
26436 zero_height_ok_p = true;
26437 }
26438 else if (prop = Fplist_get (plist, QCrelative_height),
26439 NUMVAL (prop) > 0)
26440 height = default_height * NUMVAL (prop);
26441 else
26442 height = default_height;
26443
26444 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26445 height = 1;
26446
26447 /* Compute percentage of height used for ascent. If
26448 `:ascent ASCENT' is present and valid, use that. Otherwise,
26449 derive the ascent from the font in use. */
26450 if (prop = Fplist_get (plist, QCascent),
26451 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26452 ascent = height * NUMVAL (prop) / 100.0;
26453 else if (!NILP (prop)
26454 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26455 ascent = min (max (0, (int)tem), height);
26456 else
26457 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26458 }
26459 else
26460 #endif /* HAVE_WINDOW_SYSTEM */
26461 height = 1;
26462
26463 if (width > 0 && it->line_wrap != TRUNCATE
26464 && it->current_x + width > it->last_visible_x)
26465 {
26466 width = it->last_visible_x - it->current_x;
26467 #ifdef HAVE_WINDOW_SYSTEM
26468 /* Subtract one more pixel from the stretch width, but only on
26469 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26470 width -= FRAME_WINDOW_P (it->f);
26471 #endif
26472 }
26473
26474 if (width > 0 && height > 0 && it->glyph_row)
26475 {
26476 Lisp_Object o_object = it->object;
26477 Lisp_Object object = it->stack[it->sp - 1].string;
26478 int n = width;
26479
26480 if (!STRINGP (object))
26481 object = it->w->contents;
26482 #ifdef HAVE_WINDOW_SYSTEM
26483 if (FRAME_WINDOW_P (it->f))
26484 append_stretch_glyph (it, object, width, height, ascent);
26485 else
26486 #endif
26487 {
26488 it->object = object;
26489 it->char_to_display = ' ';
26490 it->pixel_width = it->len = 1;
26491 while (n--)
26492 tty_append_glyph (it);
26493 it->object = o_object;
26494 }
26495 }
26496
26497 it->pixel_width = width;
26498 #ifdef HAVE_WINDOW_SYSTEM
26499 if (FRAME_WINDOW_P (it->f))
26500 {
26501 it->ascent = it->phys_ascent = ascent;
26502 it->descent = it->phys_descent = height - it->ascent;
26503 it->nglyphs = width > 0 && height > 0;
26504 take_vertical_position_into_account (it);
26505 }
26506 else
26507 #endif
26508 it->nglyphs = width;
26509 }
26510
26511 /* Get information about special display element WHAT in an
26512 environment described by IT. WHAT is one of IT_TRUNCATION or
26513 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26514 non-null glyph_row member. This function ensures that fields like
26515 face_id, c, len of IT are left untouched. */
26516
26517 static void
26518 produce_special_glyphs (struct it *it, enum display_element_type what)
26519 {
26520 struct it temp_it;
26521 Lisp_Object gc;
26522 GLYPH glyph;
26523
26524 temp_it = *it;
26525 temp_it.object = Qnil;
26526 memset (&temp_it.current, 0, sizeof temp_it.current);
26527
26528 if (what == IT_CONTINUATION)
26529 {
26530 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26531 if (it->bidi_it.paragraph_dir == R2L)
26532 SET_GLYPH_FROM_CHAR (glyph, '/');
26533 else
26534 SET_GLYPH_FROM_CHAR (glyph, '\\');
26535 if (it->dp
26536 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26537 {
26538 /* FIXME: Should we mirror GC for R2L lines? */
26539 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26540 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26541 }
26542 }
26543 else if (what == IT_TRUNCATION)
26544 {
26545 /* Truncation glyph. */
26546 SET_GLYPH_FROM_CHAR (glyph, '$');
26547 if (it->dp
26548 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26549 {
26550 /* FIXME: Should we mirror GC for R2L lines? */
26551 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26552 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26553 }
26554 }
26555 else
26556 emacs_abort ();
26557
26558 #ifdef HAVE_WINDOW_SYSTEM
26559 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26560 is turned off, we precede the truncation/continuation glyphs by a
26561 stretch glyph whose width is computed such that these special
26562 glyphs are aligned at the window margin, even when very different
26563 fonts are used in different glyph rows. */
26564 if (FRAME_WINDOW_P (temp_it.f)
26565 /* init_iterator calls this with it->glyph_row == NULL, and it
26566 wants only the pixel width of the truncation/continuation
26567 glyphs. */
26568 && temp_it.glyph_row
26569 /* insert_left_trunc_glyphs calls us at the beginning of the
26570 row, and it has its own calculation of the stretch glyph
26571 width. */
26572 && temp_it.glyph_row->used[TEXT_AREA] > 0
26573 && (temp_it.glyph_row->reversed_p
26574 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26575 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26576 {
26577 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26578
26579 if (stretch_width > 0)
26580 {
26581 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26582 struct font *font =
26583 face->font ? face->font : FRAME_FONT (temp_it.f);
26584 int stretch_ascent =
26585 (((temp_it.ascent + temp_it.descent)
26586 * FONT_BASE (font)) / FONT_HEIGHT (font));
26587
26588 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26589 temp_it.ascent + temp_it.descent,
26590 stretch_ascent);
26591 }
26592 }
26593 #endif
26594
26595 temp_it.dp = NULL;
26596 temp_it.what = IT_CHARACTER;
26597 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26598 temp_it.face_id = GLYPH_FACE (glyph);
26599 temp_it.len = CHAR_BYTES (temp_it.c);
26600
26601 PRODUCE_GLYPHS (&temp_it);
26602 it->pixel_width = temp_it.pixel_width;
26603 it->nglyphs = temp_it.nglyphs;
26604 }
26605
26606 #ifdef HAVE_WINDOW_SYSTEM
26607
26608 /* Calculate line-height and line-spacing properties.
26609 An integer value specifies explicit pixel value.
26610 A float value specifies relative value to current face height.
26611 A cons (float . face-name) specifies relative value to
26612 height of specified face font.
26613
26614 Returns height in pixels, or nil. */
26615
26616 static Lisp_Object
26617 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26618 int boff, bool override)
26619 {
26620 Lisp_Object face_name = Qnil;
26621 int ascent, descent, height;
26622
26623 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26624 return val;
26625
26626 if (CONSP (val))
26627 {
26628 face_name = XCAR (val);
26629 val = XCDR (val);
26630 if (!NUMBERP (val))
26631 val = make_number (1);
26632 if (NILP (face_name))
26633 {
26634 height = it->ascent + it->descent;
26635 goto scale;
26636 }
26637 }
26638
26639 if (NILP (face_name))
26640 {
26641 font = FRAME_FONT (it->f);
26642 boff = FRAME_BASELINE_OFFSET (it->f);
26643 }
26644 else if (EQ (face_name, Qt))
26645 {
26646 override = false;
26647 }
26648 else
26649 {
26650 int face_id;
26651 struct face *face;
26652
26653 face_id = lookup_named_face (it->f, face_name, false);
26654 if (face_id < 0)
26655 return make_number (-1);
26656
26657 face = FACE_FROM_ID (it->f, face_id);
26658 font = face->font;
26659 if (font == NULL)
26660 return make_number (-1);
26661 boff = font->baseline_offset;
26662 if (font->vertical_centering)
26663 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26664 }
26665
26666 normal_char_ascent_descent (font, -1, &ascent, &descent);
26667
26668 if (override)
26669 {
26670 it->override_ascent = ascent;
26671 it->override_descent = descent;
26672 it->override_boff = boff;
26673 }
26674
26675 height = ascent + descent;
26676
26677 scale:
26678 if (FLOATP (val))
26679 height = (int)(XFLOAT_DATA (val) * height);
26680 else if (INTEGERP (val))
26681 height *= XINT (val);
26682
26683 return make_number (height);
26684 }
26685
26686
26687 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26688 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26689 and only if this is for a character for which no font was found.
26690
26691 If the display method (it->glyphless_method) is
26692 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26693 length of the acronym or the hexadecimal string, UPPER_XOFF and
26694 UPPER_YOFF are pixel offsets for the upper part of the string,
26695 LOWER_XOFF and LOWER_YOFF are for the lower part.
26696
26697 For the other display methods, LEN through LOWER_YOFF are zero. */
26698
26699 static void
26700 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26701 short upper_xoff, short upper_yoff,
26702 short lower_xoff, short lower_yoff)
26703 {
26704 struct glyph *glyph;
26705 enum glyph_row_area area = it->area;
26706
26707 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26708 if (glyph < it->glyph_row->glyphs[area + 1])
26709 {
26710 /* If the glyph row is reversed, we need to prepend the glyph
26711 rather than append it. */
26712 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26713 {
26714 struct glyph *g;
26715
26716 /* Make room for the additional glyph. */
26717 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26718 g[1] = *g;
26719 glyph = it->glyph_row->glyphs[area];
26720 }
26721 glyph->charpos = CHARPOS (it->position);
26722 glyph->object = it->object;
26723 glyph->pixel_width = it->pixel_width;
26724 glyph->ascent = it->ascent;
26725 glyph->descent = it->descent;
26726 glyph->voffset = it->voffset;
26727 glyph->type = GLYPHLESS_GLYPH;
26728 glyph->u.glyphless.method = it->glyphless_method;
26729 glyph->u.glyphless.for_no_font = for_no_font;
26730 glyph->u.glyphless.len = len;
26731 glyph->u.glyphless.ch = it->c;
26732 glyph->slice.glyphless.upper_xoff = upper_xoff;
26733 glyph->slice.glyphless.upper_yoff = upper_yoff;
26734 glyph->slice.glyphless.lower_xoff = lower_xoff;
26735 glyph->slice.glyphless.lower_yoff = lower_yoff;
26736 glyph->avoid_cursor_p = it->avoid_cursor_p;
26737 glyph->multibyte_p = it->multibyte_p;
26738 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26739 {
26740 /* In R2L rows, the left and the right box edges need to be
26741 drawn in reverse direction. */
26742 glyph->right_box_line_p = it->start_of_box_run_p;
26743 glyph->left_box_line_p = it->end_of_box_run_p;
26744 }
26745 else
26746 {
26747 glyph->left_box_line_p = it->start_of_box_run_p;
26748 glyph->right_box_line_p = it->end_of_box_run_p;
26749 }
26750 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26751 || it->phys_descent > it->descent);
26752 glyph->padding_p = false;
26753 glyph->glyph_not_available_p = false;
26754 glyph->face_id = face_id;
26755 glyph->font_type = FONT_TYPE_UNKNOWN;
26756 if (it->bidi_p)
26757 {
26758 glyph->resolved_level = it->bidi_it.resolved_level;
26759 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26760 glyph->bidi_type = it->bidi_it.type;
26761 }
26762 ++it->glyph_row->used[area];
26763 }
26764 else
26765 IT_EXPAND_MATRIX_WIDTH (it, area);
26766 }
26767
26768
26769 /* Produce a glyph for a glyphless character for iterator IT.
26770 IT->glyphless_method specifies which method to use for displaying
26771 the character. See the description of enum
26772 glyphless_display_method in dispextern.h for the detail.
26773
26774 FOR_NO_FONT is true if and only if this is for a character for
26775 which no font was found. ACRONYM, if non-nil, is an acronym string
26776 for the character. */
26777
26778 static void
26779 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26780 {
26781 int face_id;
26782 struct face *face;
26783 struct font *font;
26784 int base_width, base_height, width, height;
26785 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26786 int len;
26787
26788 /* Get the metrics of the base font. We always refer to the current
26789 ASCII face. */
26790 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26791 font = face->font ? face->font : FRAME_FONT (it->f);
26792 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
26793 it->ascent += font->baseline_offset;
26794 it->descent -= font->baseline_offset;
26795 base_height = it->ascent + it->descent;
26796 base_width = font->average_width;
26797
26798 face_id = merge_glyphless_glyph_face (it);
26799
26800 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26801 {
26802 it->pixel_width = THIN_SPACE_WIDTH;
26803 len = 0;
26804 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26805 }
26806 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26807 {
26808 width = CHAR_WIDTH (it->c);
26809 if (width == 0)
26810 width = 1;
26811 else if (width > 4)
26812 width = 4;
26813 it->pixel_width = base_width * width;
26814 len = 0;
26815 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26816 }
26817 else
26818 {
26819 char buf[7];
26820 const char *str;
26821 unsigned int code[6];
26822 int upper_len;
26823 int ascent, descent;
26824 struct font_metrics metrics_upper, metrics_lower;
26825
26826 face = FACE_FROM_ID (it->f, face_id);
26827 font = face->font ? face->font : FRAME_FONT (it->f);
26828 prepare_face_for_display (it->f, face);
26829
26830 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26831 {
26832 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26833 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26834 if (CONSP (acronym))
26835 acronym = XCAR (acronym);
26836 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26837 }
26838 else
26839 {
26840 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26841 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
26842 str = buf;
26843 }
26844 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26845 code[len] = font->driver->encode_char (font, str[len]);
26846 upper_len = (len + 1) / 2;
26847 font->driver->text_extents (font, code, upper_len,
26848 &metrics_upper);
26849 font->driver->text_extents (font, code + upper_len, len - upper_len,
26850 &metrics_lower);
26851
26852
26853
26854 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26855 width = max (metrics_upper.width, metrics_lower.width) + 4;
26856 upper_xoff = upper_yoff = 2; /* the typical case */
26857 if (base_width >= width)
26858 {
26859 /* Align the upper to the left, the lower to the right. */
26860 it->pixel_width = base_width;
26861 lower_xoff = base_width - 2 - metrics_lower.width;
26862 }
26863 else
26864 {
26865 /* Center the shorter one. */
26866 it->pixel_width = width;
26867 if (metrics_upper.width >= metrics_lower.width)
26868 lower_xoff = (width - metrics_lower.width) / 2;
26869 else
26870 {
26871 /* FIXME: This code doesn't look right. It formerly was
26872 missing the "lower_xoff = 0;", which couldn't have
26873 been right since it left lower_xoff uninitialized. */
26874 lower_xoff = 0;
26875 upper_xoff = (width - metrics_upper.width) / 2;
26876 }
26877 }
26878
26879 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26880 top, bottom, and between upper and lower strings. */
26881 height = (metrics_upper.ascent + metrics_upper.descent
26882 + metrics_lower.ascent + metrics_lower.descent) + 5;
26883 /* Center vertically.
26884 H:base_height, D:base_descent
26885 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26886
26887 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26888 descent = D - H/2 + h/2;
26889 lower_yoff = descent - 2 - ld;
26890 upper_yoff = lower_yoff - la - 1 - ud; */
26891 ascent = - (it->descent - (base_height + height + 1) / 2);
26892 descent = it->descent - (base_height - height) / 2;
26893 lower_yoff = descent - 2 - metrics_lower.descent;
26894 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26895 - metrics_upper.descent);
26896 /* Don't make the height shorter than the base height. */
26897 if (height > base_height)
26898 {
26899 it->ascent = ascent;
26900 it->descent = descent;
26901 }
26902 }
26903
26904 it->phys_ascent = it->ascent;
26905 it->phys_descent = it->descent;
26906 if (it->glyph_row)
26907 append_glyphless_glyph (it, face_id, for_no_font, len,
26908 upper_xoff, upper_yoff,
26909 lower_xoff, lower_yoff);
26910 it->nglyphs = 1;
26911 take_vertical_position_into_account (it);
26912 }
26913
26914
26915 /* RIF:
26916 Produce glyphs/get display metrics for the display element IT is
26917 loaded with. See the description of struct it in dispextern.h
26918 for an overview of struct it. */
26919
26920 void
26921 x_produce_glyphs (struct it *it)
26922 {
26923 int extra_line_spacing = it->extra_line_spacing;
26924
26925 it->glyph_not_available_p = false;
26926
26927 if (it->what == IT_CHARACTER)
26928 {
26929 XChar2b char2b;
26930 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26931 struct font *font = face->font;
26932 struct font_metrics *pcm = NULL;
26933 int boff; /* Baseline offset. */
26934
26935 if (font == NULL)
26936 {
26937 /* When no suitable font is found, display this character by
26938 the method specified in the first extra slot of
26939 Vglyphless_char_display. */
26940 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26941
26942 eassert (it->what == IT_GLYPHLESS);
26943 produce_glyphless_glyph (it, true,
26944 STRINGP (acronym) ? acronym : Qnil);
26945 goto done;
26946 }
26947
26948 boff = font->baseline_offset;
26949 if (font->vertical_centering)
26950 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26951
26952 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26953 {
26954 it->nglyphs = 1;
26955
26956 if (it->override_ascent >= 0)
26957 {
26958 it->ascent = it->override_ascent;
26959 it->descent = it->override_descent;
26960 boff = it->override_boff;
26961 }
26962 else
26963 {
26964 it->ascent = FONT_BASE (font) + boff;
26965 it->descent = FONT_DESCENT (font) - boff;
26966 }
26967
26968 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26969 {
26970 pcm = get_per_char_metric (font, &char2b);
26971 if (pcm->width == 0
26972 && pcm->rbearing == 0 && pcm->lbearing == 0)
26973 pcm = NULL;
26974 }
26975
26976 if (pcm)
26977 {
26978 it->phys_ascent = pcm->ascent + boff;
26979 it->phys_descent = pcm->descent - boff;
26980 it->pixel_width = pcm->width;
26981 /* Don't use font-global values for ascent and descent
26982 if they result in an exceedingly large line height. */
26983 if (it->override_ascent < 0)
26984 {
26985 if (FONT_TOO_HIGH (font))
26986 {
26987 it->ascent = it->phys_ascent;
26988 it->descent = it->phys_descent;
26989 /* These limitations are enforced by an
26990 assertion near the end of this function. */
26991 if (it->ascent < 0)
26992 it->ascent = 0;
26993 if (it->descent < 0)
26994 it->descent = 0;
26995 }
26996 }
26997 }
26998 else
26999 {
27000 it->glyph_not_available_p = true;
27001 it->phys_ascent = it->ascent;
27002 it->phys_descent = it->descent;
27003 it->pixel_width = font->space_width;
27004 }
27005
27006 if (it->constrain_row_ascent_descent_p)
27007 {
27008 if (it->descent > it->max_descent)
27009 {
27010 it->ascent += it->descent - it->max_descent;
27011 it->descent = it->max_descent;
27012 }
27013 if (it->ascent > it->max_ascent)
27014 {
27015 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27016 it->ascent = it->max_ascent;
27017 }
27018 it->phys_ascent = min (it->phys_ascent, it->ascent);
27019 it->phys_descent = min (it->phys_descent, it->descent);
27020 extra_line_spacing = 0;
27021 }
27022
27023 /* If this is a space inside a region of text with
27024 `space-width' property, change its width. */
27025 bool stretched_p
27026 = it->char_to_display == ' ' && !NILP (it->space_width);
27027 if (stretched_p)
27028 it->pixel_width *= XFLOATINT (it->space_width);
27029
27030 /* If face has a box, add the box thickness to the character
27031 height. If character has a box line to the left and/or
27032 right, add the box line width to the character's width. */
27033 if (face->box != FACE_NO_BOX)
27034 {
27035 int thick = face->box_line_width;
27036
27037 if (thick > 0)
27038 {
27039 it->ascent += thick;
27040 it->descent += thick;
27041 }
27042 else
27043 thick = -thick;
27044
27045 if (it->start_of_box_run_p)
27046 it->pixel_width += thick;
27047 if (it->end_of_box_run_p)
27048 it->pixel_width += thick;
27049 }
27050
27051 /* If face has an overline, add the height of the overline
27052 (1 pixel) and a 1 pixel margin to the character height. */
27053 if (face->overline_p)
27054 it->ascent += overline_margin;
27055
27056 if (it->constrain_row_ascent_descent_p)
27057 {
27058 if (it->ascent > it->max_ascent)
27059 it->ascent = it->max_ascent;
27060 if (it->descent > it->max_descent)
27061 it->descent = it->max_descent;
27062 }
27063
27064 take_vertical_position_into_account (it);
27065
27066 /* If we have to actually produce glyphs, do it. */
27067 if (it->glyph_row)
27068 {
27069 if (stretched_p)
27070 {
27071 /* Translate a space with a `space-width' property
27072 into a stretch glyph. */
27073 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
27074 / FONT_HEIGHT (font));
27075 append_stretch_glyph (it, it->object, it->pixel_width,
27076 it->ascent + it->descent, ascent);
27077 }
27078 else
27079 append_glyph (it);
27080
27081 /* If characters with lbearing or rbearing are displayed
27082 in this line, record that fact in a flag of the
27083 glyph row. This is used to optimize X output code. */
27084 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
27085 it->glyph_row->contains_overlapping_glyphs_p = true;
27086 }
27087 if (! stretched_p && it->pixel_width == 0)
27088 /* We assure that all visible glyphs have at least 1-pixel
27089 width. */
27090 it->pixel_width = 1;
27091 }
27092 else if (it->char_to_display == '\n')
27093 {
27094 /* A newline has no width, but we need the height of the
27095 line. But if previous part of the line sets a height,
27096 don't increase that height. */
27097
27098 Lisp_Object height;
27099 Lisp_Object total_height = Qnil;
27100
27101 it->override_ascent = -1;
27102 it->pixel_width = 0;
27103 it->nglyphs = 0;
27104
27105 height = get_it_property (it, Qline_height);
27106 /* Split (line-height total-height) list. */
27107 if (CONSP (height)
27108 && CONSP (XCDR (height))
27109 && NILP (XCDR (XCDR (height))))
27110 {
27111 total_height = XCAR (XCDR (height));
27112 height = XCAR (height);
27113 }
27114 height = calc_line_height_property (it, height, font, boff, true);
27115
27116 if (it->override_ascent >= 0)
27117 {
27118 it->ascent = it->override_ascent;
27119 it->descent = it->override_descent;
27120 boff = it->override_boff;
27121 }
27122 else
27123 {
27124 if (FONT_TOO_HIGH (font))
27125 {
27126 it->ascent = font->pixel_size + boff - 1;
27127 it->descent = -boff + 1;
27128 if (it->descent < 0)
27129 it->descent = 0;
27130 }
27131 else
27132 {
27133 it->ascent = FONT_BASE (font) + boff;
27134 it->descent = FONT_DESCENT (font) - boff;
27135 }
27136 }
27137
27138 if (EQ (height, Qt))
27139 {
27140 if (it->descent > it->max_descent)
27141 {
27142 it->ascent += it->descent - it->max_descent;
27143 it->descent = it->max_descent;
27144 }
27145 if (it->ascent > it->max_ascent)
27146 {
27147 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27148 it->ascent = it->max_ascent;
27149 }
27150 it->phys_ascent = min (it->phys_ascent, it->ascent);
27151 it->phys_descent = min (it->phys_descent, it->descent);
27152 it->constrain_row_ascent_descent_p = true;
27153 extra_line_spacing = 0;
27154 }
27155 else
27156 {
27157 Lisp_Object spacing;
27158
27159 it->phys_ascent = it->ascent;
27160 it->phys_descent = it->descent;
27161
27162 if ((it->max_ascent > 0 || it->max_descent > 0)
27163 && face->box != FACE_NO_BOX
27164 && face->box_line_width > 0)
27165 {
27166 it->ascent += face->box_line_width;
27167 it->descent += face->box_line_width;
27168 }
27169 if (!NILP (height)
27170 && XINT (height) > it->ascent + it->descent)
27171 it->ascent = XINT (height) - it->descent;
27172
27173 if (!NILP (total_height))
27174 spacing = calc_line_height_property (it, total_height, font,
27175 boff, false);
27176 else
27177 {
27178 spacing = get_it_property (it, Qline_spacing);
27179 spacing = calc_line_height_property (it, spacing, font,
27180 boff, false);
27181 }
27182 if (INTEGERP (spacing))
27183 {
27184 extra_line_spacing = XINT (spacing);
27185 if (!NILP (total_height))
27186 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
27187 }
27188 }
27189 }
27190 else /* i.e. (it->char_to_display == '\t') */
27191 {
27192 if (font->space_width > 0)
27193 {
27194 int tab_width = it->tab_width * font->space_width;
27195 int x = it->current_x + it->continuation_lines_width;
27196 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
27197
27198 /* If the distance from the current position to the next tab
27199 stop is less than a space character width, use the
27200 tab stop after that. */
27201 if (next_tab_x - x < font->space_width)
27202 next_tab_x += tab_width;
27203
27204 it->pixel_width = next_tab_x - x;
27205 it->nglyphs = 1;
27206 if (FONT_TOO_HIGH (font))
27207 {
27208 if (get_char_glyph_code (' ', font, &char2b))
27209 {
27210 pcm = get_per_char_metric (font, &char2b);
27211 if (pcm->width == 0
27212 && pcm->rbearing == 0 && pcm->lbearing == 0)
27213 pcm = NULL;
27214 }
27215
27216 if (pcm)
27217 {
27218 it->ascent = pcm->ascent + boff;
27219 it->descent = pcm->descent - boff;
27220 }
27221 else
27222 {
27223 it->ascent = font->pixel_size + boff - 1;
27224 it->descent = -boff + 1;
27225 }
27226 if (it->ascent < 0)
27227 it->ascent = 0;
27228 if (it->descent < 0)
27229 it->descent = 0;
27230 }
27231 else
27232 {
27233 it->ascent = FONT_BASE (font) + boff;
27234 it->descent = FONT_DESCENT (font) - boff;
27235 }
27236 it->phys_ascent = it->ascent;
27237 it->phys_descent = it->descent;
27238
27239 if (it->glyph_row)
27240 {
27241 append_stretch_glyph (it, it->object, it->pixel_width,
27242 it->ascent + it->descent, it->ascent);
27243 }
27244 }
27245 else
27246 {
27247 it->pixel_width = 0;
27248 it->nglyphs = 1;
27249 }
27250 }
27251
27252 if (FONT_TOO_HIGH (font))
27253 {
27254 int font_ascent, font_descent;
27255
27256 /* For very large fonts, where we ignore the declared font
27257 dimensions, and go by per-character metrics instead,
27258 don't let the row ascent and descent values (and the row
27259 height computed from them) be smaller than the "normal"
27260 character metrics. This avoids unpleasant effects
27261 whereby lines on display would change their height
27262 depending on which characters are shown. */
27263 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27264 it->max_ascent = max (it->max_ascent, font_ascent);
27265 it->max_descent = max (it->max_descent, font_descent);
27266 }
27267 }
27268 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
27269 {
27270 /* A static composition.
27271
27272 Note: A composition is represented as one glyph in the
27273 glyph matrix. There are no padding glyphs.
27274
27275 Important note: pixel_width, ascent, and descent are the
27276 values of what is drawn by draw_glyphs (i.e. the values of
27277 the overall glyphs composed). */
27278 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27279 int boff; /* baseline offset */
27280 struct composition *cmp = composition_table[it->cmp_it.id];
27281 int glyph_len = cmp->glyph_len;
27282 struct font *font = face->font;
27283
27284 it->nglyphs = 1;
27285
27286 /* If we have not yet calculated pixel size data of glyphs of
27287 the composition for the current face font, calculate them
27288 now. Theoretically, we have to check all fonts for the
27289 glyphs, but that requires much time and memory space. So,
27290 here we check only the font of the first glyph. This may
27291 lead to incorrect display, but it's very rare, and C-l
27292 (recenter-top-bottom) can correct the display anyway. */
27293 if (! cmp->font || cmp->font != font)
27294 {
27295 /* Ascent and descent of the font of the first character
27296 of this composition (adjusted by baseline offset).
27297 Ascent and descent of overall glyphs should not be less
27298 than these, respectively. */
27299 int font_ascent, font_descent, font_height;
27300 /* Bounding box of the overall glyphs. */
27301 int leftmost, rightmost, lowest, highest;
27302 int lbearing, rbearing;
27303 int i, width, ascent, descent;
27304 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
27305 XChar2b char2b;
27306 struct font_metrics *pcm;
27307 ptrdiff_t pos;
27308
27309 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
27310 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
27311 break;
27312 bool right_padded = glyph_len < cmp->glyph_len;
27313 for (i = 0; i < glyph_len; i++)
27314 {
27315 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
27316 break;
27317 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27318 }
27319 bool left_padded = i > 0;
27320
27321 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
27322 : IT_CHARPOS (*it));
27323 /* If no suitable font is found, use the default font. */
27324 bool font_not_found_p = font == NULL;
27325 if (font_not_found_p)
27326 {
27327 face = face->ascii_face;
27328 font = face->font;
27329 }
27330 boff = font->baseline_offset;
27331 if (font->vertical_centering)
27332 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27333 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27334 font_ascent += boff;
27335 font_descent -= boff;
27336 font_height = font_ascent + font_descent;
27337
27338 cmp->font = font;
27339
27340 pcm = NULL;
27341 if (! font_not_found_p)
27342 {
27343 get_char_face_and_encoding (it->f, c, it->face_id,
27344 &char2b, false);
27345 pcm = get_per_char_metric (font, &char2b);
27346 }
27347
27348 /* Initialize the bounding box. */
27349 if (pcm)
27350 {
27351 width = cmp->glyph_len > 0 ? pcm->width : 0;
27352 ascent = pcm->ascent;
27353 descent = pcm->descent;
27354 lbearing = pcm->lbearing;
27355 rbearing = pcm->rbearing;
27356 }
27357 else
27358 {
27359 width = cmp->glyph_len > 0 ? font->space_width : 0;
27360 ascent = FONT_BASE (font);
27361 descent = FONT_DESCENT (font);
27362 lbearing = 0;
27363 rbearing = width;
27364 }
27365
27366 rightmost = width;
27367 leftmost = 0;
27368 lowest = - descent + boff;
27369 highest = ascent + boff;
27370
27371 if (! font_not_found_p
27372 && font->default_ascent
27373 && CHAR_TABLE_P (Vuse_default_ascent)
27374 && !NILP (Faref (Vuse_default_ascent,
27375 make_number (it->char_to_display))))
27376 highest = font->default_ascent + boff;
27377
27378 /* Draw the first glyph at the normal position. It may be
27379 shifted to right later if some other glyphs are drawn
27380 at the left. */
27381 cmp->offsets[i * 2] = 0;
27382 cmp->offsets[i * 2 + 1] = boff;
27383 cmp->lbearing = lbearing;
27384 cmp->rbearing = rbearing;
27385
27386 /* Set cmp->offsets for the remaining glyphs. */
27387 for (i++; i < glyph_len; i++)
27388 {
27389 int left, right, btm, top;
27390 int ch = COMPOSITION_GLYPH (cmp, i);
27391 int face_id;
27392 struct face *this_face;
27393
27394 if (ch == '\t')
27395 ch = ' ';
27396 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27397 this_face = FACE_FROM_ID (it->f, face_id);
27398 font = this_face->font;
27399
27400 if (font == NULL)
27401 pcm = NULL;
27402 else
27403 {
27404 get_char_face_and_encoding (it->f, ch, face_id,
27405 &char2b, false);
27406 pcm = get_per_char_metric (font, &char2b);
27407 }
27408 if (! pcm)
27409 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27410 else
27411 {
27412 width = pcm->width;
27413 ascent = pcm->ascent;
27414 descent = pcm->descent;
27415 lbearing = pcm->lbearing;
27416 rbearing = pcm->rbearing;
27417 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27418 {
27419 /* Relative composition with or without
27420 alternate chars. */
27421 left = (leftmost + rightmost - width) / 2;
27422 btm = - descent + boff;
27423 if (font->relative_compose
27424 && (! CHAR_TABLE_P (Vignore_relative_composition)
27425 || NILP (Faref (Vignore_relative_composition,
27426 make_number (ch)))))
27427 {
27428
27429 if (- descent >= font->relative_compose)
27430 /* One extra pixel between two glyphs. */
27431 btm = highest + 1;
27432 else if (ascent <= 0)
27433 /* One extra pixel between two glyphs. */
27434 btm = lowest - 1 - ascent - descent;
27435 }
27436 }
27437 else
27438 {
27439 /* A composition rule is specified by an integer
27440 value that encodes global and new reference
27441 points (GREF and NREF). GREF and NREF are
27442 specified by numbers as below:
27443
27444 0---1---2 -- ascent
27445 | |
27446 | |
27447 | |
27448 9--10--11 -- center
27449 | |
27450 ---3---4---5--- baseline
27451 | |
27452 6---7---8 -- descent
27453 */
27454 int rule = COMPOSITION_RULE (cmp, i);
27455 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27456
27457 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27458 grefx = gref % 3, nrefx = nref % 3;
27459 grefy = gref / 3, nrefy = nref / 3;
27460 if (xoff)
27461 xoff = font_height * (xoff - 128) / 256;
27462 if (yoff)
27463 yoff = font_height * (yoff - 128) / 256;
27464
27465 left = (leftmost
27466 + grefx * (rightmost - leftmost) / 2
27467 - nrefx * width / 2
27468 + xoff);
27469
27470 btm = ((grefy == 0 ? highest
27471 : grefy == 1 ? 0
27472 : grefy == 2 ? lowest
27473 : (highest + lowest) / 2)
27474 - (nrefy == 0 ? ascent + descent
27475 : nrefy == 1 ? descent - boff
27476 : nrefy == 2 ? 0
27477 : (ascent + descent) / 2)
27478 + yoff);
27479 }
27480
27481 cmp->offsets[i * 2] = left;
27482 cmp->offsets[i * 2 + 1] = btm + descent;
27483
27484 /* Update the bounding box of the overall glyphs. */
27485 if (width > 0)
27486 {
27487 right = left + width;
27488 if (left < leftmost)
27489 leftmost = left;
27490 if (right > rightmost)
27491 rightmost = right;
27492 }
27493 top = btm + descent + ascent;
27494 if (top > highest)
27495 highest = top;
27496 if (btm < lowest)
27497 lowest = btm;
27498
27499 if (cmp->lbearing > left + lbearing)
27500 cmp->lbearing = left + lbearing;
27501 if (cmp->rbearing < left + rbearing)
27502 cmp->rbearing = left + rbearing;
27503 }
27504 }
27505
27506 /* If there are glyphs whose x-offsets are negative,
27507 shift all glyphs to the right and make all x-offsets
27508 non-negative. */
27509 if (leftmost < 0)
27510 {
27511 for (i = 0; i < cmp->glyph_len; i++)
27512 cmp->offsets[i * 2] -= leftmost;
27513 rightmost -= leftmost;
27514 cmp->lbearing -= leftmost;
27515 cmp->rbearing -= leftmost;
27516 }
27517
27518 if (left_padded && cmp->lbearing < 0)
27519 {
27520 for (i = 0; i < cmp->glyph_len; i++)
27521 cmp->offsets[i * 2] -= cmp->lbearing;
27522 rightmost -= cmp->lbearing;
27523 cmp->rbearing -= cmp->lbearing;
27524 cmp->lbearing = 0;
27525 }
27526 if (right_padded && rightmost < cmp->rbearing)
27527 {
27528 rightmost = cmp->rbearing;
27529 }
27530
27531 cmp->pixel_width = rightmost;
27532 cmp->ascent = highest;
27533 cmp->descent = - lowest;
27534 if (cmp->ascent < font_ascent)
27535 cmp->ascent = font_ascent;
27536 if (cmp->descent < font_descent)
27537 cmp->descent = font_descent;
27538 }
27539
27540 if (it->glyph_row
27541 && (cmp->lbearing < 0
27542 || cmp->rbearing > cmp->pixel_width))
27543 it->glyph_row->contains_overlapping_glyphs_p = true;
27544
27545 it->pixel_width = cmp->pixel_width;
27546 it->ascent = it->phys_ascent = cmp->ascent;
27547 it->descent = it->phys_descent = cmp->descent;
27548 if (face->box != FACE_NO_BOX)
27549 {
27550 int thick = face->box_line_width;
27551
27552 if (thick > 0)
27553 {
27554 it->ascent += thick;
27555 it->descent += thick;
27556 }
27557 else
27558 thick = - thick;
27559
27560 if (it->start_of_box_run_p)
27561 it->pixel_width += thick;
27562 if (it->end_of_box_run_p)
27563 it->pixel_width += thick;
27564 }
27565
27566 /* If face has an overline, add the height of the overline
27567 (1 pixel) and a 1 pixel margin to the character height. */
27568 if (face->overline_p)
27569 it->ascent += overline_margin;
27570
27571 take_vertical_position_into_account (it);
27572 if (it->ascent < 0)
27573 it->ascent = 0;
27574 if (it->descent < 0)
27575 it->descent = 0;
27576
27577 if (it->glyph_row && cmp->glyph_len > 0)
27578 append_composite_glyph (it);
27579 }
27580 else if (it->what == IT_COMPOSITION)
27581 {
27582 /* A dynamic (automatic) composition. */
27583 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27584 Lisp_Object gstring;
27585 struct font_metrics metrics;
27586
27587 it->nglyphs = 1;
27588
27589 gstring = composition_gstring_from_id (it->cmp_it.id);
27590 it->pixel_width
27591 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27592 &metrics);
27593 if (it->glyph_row
27594 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27595 it->glyph_row->contains_overlapping_glyphs_p = true;
27596 it->ascent = it->phys_ascent = metrics.ascent;
27597 it->descent = it->phys_descent = metrics.descent;
27598 if (face->box != FACE_NO_BOX)
27599 {
27600 int thick = face->box_line_width;
27601
27602 if (thick > 0)
27603 {
27604 it->ascent += thick;
27605 it->descent += thick;
27606 }
27607 else
27608 thick = - thick;
27609
27610 if (it->start_of_box_run_p)
27611 it->pixel_width += thick;
27612 if (it->end_of_box_run_p)
27613 it->pixel_width += thick;
27614 }
27615 /* If face has an overline, add the height of the overline
27616 (1 pixel) and a 1 pixel margin to the character height. */
27617 if (face->overline_p)
27618 it->ascent += overline_margin;
27619 take_vertical_position_into_account (it);
27620 if (it->ascent < 0)
27621 it->ascent = 0;
27622 if (it->descent < 0)
27623 it->descent = 0;
27624
27625 if (it->glyph_row)
27626 append_composite_glyph (it);
27627 }
27628 else if (it->what == IT_GLYPHLESS)
27629 produce_glyphless_glyph (it, false, Qnil);
27630 else if (it->what == IT_IMAGE)
27631 produce_image_glyph (it);
27632 else if (it->what == IT_STRETCH)
27633 produce_stretch_glyph (it);
27634 #ifdef HAVE_XWIDGETS
27635 else if (it->what == IT_XWIDGET)
27636 produce_xwidget_glyph (it);
27637 #endif
27638
27639 done:
27640 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27641 because this isn't true for images with `:ascent 100'. */
27642 eassert (it->ascent >= 0 && it->descent >= 0);
27643 if (it->area == TEXT_AREA)
27644 it->current_x += it->pixel_width;
27645
27646 if (extra_line_spacing > 0)
27647 {
27648 it->descent += extra_line_spacing;
27649 if (extra_line_spacing > it->max_extra_line_spacing)
27650 it->max_extra_line_spacing = extra_line_spacing;
27651 }
27652
27653 it->max_ascent = max (it->max_ascent, it->ascent);
27654 it->max_descent = max (it->max_descent, it->descent);
27655 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27656 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27657 }
27658
27659 /* EXPORT for RIF:
27660 Output LEN glyphs starting at START at the nominal cursor position.
27661 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27662 being updated, and UPDATED_AREA is the area of that row being updated. */
27663
27664 void
27665 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27666 struct glyph *start, enum glyph_row_area updated_area, int len)
27667 {
27668 int x, hpos, chpos = w->phys_cursor.hpos;
27669
27670 eassert (updated_row);
27671 /* When the window is hscrolled, cursor hpos can legitimately be out
27672 of bounds, but we draw the cursor at the corresponding window
27673 margin in that case. */
27674 if (!updated_row->reversed_p && chpos < 0)
27675 chpos = 0;
27676 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27677 chpos = updated_row->used[TEXT_AREA] - 1;
27678
27679 block_input ();
27680
27681 /* Write glyphs. */
27682
27683 hpos = start - updated_row->glyphs[updated_area];
27684 x = draw_glyphs (w, w->output_cursor.x,
27685 updated_row, updated_area,
27686 hpos, hpos + len,
27687 DRAW_NORMAL_TEXT, 0);
27688
27689 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27690 if (updated_area == TEXT_AREA
27691 && w->phys_cursor_on_p
27692 && w->phys_cursor.vpos == w->output_cursor.vpos
27693 && chpos >= hpos
27694 && chpos < hpos + len)
27695 w->phys_cursor_on_p = false;
27696
27697 unblock_input ();
27698
27699 /* Advance the output cursor. */
27700 w->output_cursor.hpos += len;
27701 w->output_cursor.x = x;
27702 }
27703
27704
27705 /* EXPORT for RIF:
27706 Insert LEN glyphs from START at the nominal cursor position. */
27707
27708 void
27709 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27710 struct glyph *start, enum glyph_row_area updated_area, int len)
27711 {
27712 struct frame *f;
27713 int line_height, shift_by_width, shifted_region_width;
27714 struct glyph_row *row;
27715 struct glyph *glyph;
27716 int frame_x, frame_y;
27717 ptrdiff_t hpos;
27718
27719 eassert (updated_row);
27720 block_input ();
27721 f = XFRAME (WINDOW_FRAME (w));
27722
27723 /* Get the height of the line we are in. */
27724 row = updated_row;
27725 line_height = row->height;
27726
27727 /* Get the width of the glyphs to insert. */
27728 shift_by_width = 0;
27729 for (glyph = start; glyph < start + len; ++glyph)
27730 shift_by_width += glyph->pixel_width;
27731
27732 /* Get the width of the region to shift right. */
27733 shifted_region_width = (window_box_width (w, updated_area)
27734 - w->output_cursor.x
27735 - shift_by_width);
27736
27737 /* Shift right. */
27738 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27739 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27740
27741 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27742 line_height, shift_by_width);
27743
27744 /* Write the glyphs. */
27745 hpos = start - row->glyphs[updated_area];
27746 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27747 hpos, hpos + len,
27748 DRAW_NORMAL_TEXT, 0);
27749
27750 /* Advance the output cursor. */
27751 w->output_cursor.hpos += len;
27752 w->output_cursor.x += shift_by_width;
27753 unblock_input ();
27754 }
27755
27756
27757 /* EXPORT for RIF:
27758 Erase the current text line from the nominal cursor position
27759 (inclusive) to pixel column TO_X (exclusive). The idea is that
27760 everything from TO_X onward is already erased.
27761
27762 TO_X is a pixel position relative to UPDATED_AREA of currently
27763 updated window W. TO_X == -1 means clear to the end of this area. */
27764
27765 void
27766 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27767 enum glyph_row_area updated_area, int to_x)
27768 {
27769 struct frame *f;
27770 int max_x, min_y, max_y;
27771 int from_x, from_y, to_y;
27772
27773 eassert (updated_row);
27774 f = XFRAME (w->frame);
27775
27776 if (updated_row->full_width_p)
27777 max_x = (WINDOW_PIXEL_WIDTH (w)
27778 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27779 else
27780 max_x = window_box_width (w, updated_area);
27781 max_y = window_text_bottom_y (w);
27782
27783 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27784 of window. For TO_X > 0, truncate to end of drawing area. */
27785 if (to_x == 0)
27786 return;
27787 else if (to_x < 0)
27788 to_x = max_x;
27789 else
27790 to_x = min (to_x, max_x);
27791
27792 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27793
27794 /* Notice if the cursor will be cleared by this operation. */
27795 if (!updated_row->full_width_p)
27796 notice_overwritten_cursor (w, updated_area,
27797 w->output_cursor.x, -1,
27798 updated_row->y,
27799 MATRIX_ROW_BOTTOM_Y (updated_row));
27800
27801 from_x = w->output_cursor.x;
27802
27803 /* Translate to frame coordinates. */
27804 if (updated_row->full_width_p)
27805 {
27806 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27807 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27808 }
27809 else
27810 {
27811 int area_left = window_box_left (w, updated_area);
27812 from_x += area_left;
27813 to_x += area_left;
27814 }
27815
27816 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27817 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27818 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27819
27820 /* Prevent inadvertently clearing to end of the X window. */
27821 if (to_x > from_x && to_y > from_y)
27822 {
27823 block_input ();
27824 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27825 to_x - from_x, to_y - from_y);
27826 unblock_input ();
27827 }
27828 }
27829
27830 #endif /* HAVE_WINDOW_SYSTEM */
27831
27832
27833 \f
27834 /***********************************************************************
27835 Cursor types
27836 ***********************************************************************/
27837
27838 /* Value is the internal representation of the specified cursor type
27839 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27840 of the bar cursor. */
27841
27842 static enum text_cursor_kinds
27843 get_specified_cursor_type (Lisp_Object arg, int *width)
27844 {
27845 enum text_cursor_kinds type;
27846
27847 if (NILP (arg))
27848 return NO_CURSOR;
27849
27850 if (EQ (arg, Qbox))
27851 return FILLED_BOX_CURSOR;
27852
27853 if (EQ (arg, Qhollow))
27854 return HOLLOW_BOX_CURSOR;
27855
27856 if (EQ (arg, Qbar))
27857 {
27858 *width = 2;
27859 return BAR_CURSOR;
27860 }
27861
27862 if (CONSP (arg)
27863 && EQ (XCAR (arg), Qbar)
27864 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27865 {
27866 *width = XINT (XCDR (arg));
27867 return BAR_CURSOR;
27868 }
27869
27870 if (EQ (arg, Qhbar))
27871 {
27872 *width = 2;
27873 return HBAR_CURSOR;
27874 }
27875
27876 if (CONSP (arg)
27877 && EQ (XCAR (arg), Qhbar)
27878 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27879 {
27880 *width = XINT (XCDR (arg));
27881 return HBAR_CURSOR;
27882 }
27883
27884 /* Treat anything unknown as "hollow box cursor".
27885 It was bad to signal an error; people have trouble fixing
27886 .Xdefaults with Emacs, when it has something bad in it. */
27887 type = HOLLOW_BOX_CURSOR;
27888
27889 return type;
27890 }
27891
27892 /* Set the default cursor types for specified frame. */
27893 void
27894 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27895 {
27896 int width = 1;
27897 Lisp_Object tem;
27898
27899 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27900 FRAME_CURSOR_WIDTH (f) = width;
27901
27902 /* By default, set up the blink-off state depending on the on-state. */
27903
27904 tem = Fassoc (arg, Vblink_cursor_alist);
27905 if (!NILP (tem))
27906 {
27907 FRAME_BLINK_OFF_CURSOR (f)
27908 = get_specified_cursor_type (XCDR (tem), &width);
27909 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27910 }
27911 else
27912 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27913
27914 /* Make sure the cursor gets redrawn. */
27915 f->cursor_type_changed = true;
27916 }
27917
27918
27919 #ifdef HAVE_WINDOW_SYSTEM
27920
27921 /* Return the cursor we want to be displayed in window W. Return
27922 width of bar/hbar cursor through WIDTH arg. Return with
27923 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
27924 (i.e. if the `system caret' should track this cursor).
27925
27926 In a mini-buffer window, we want the cursor only to appear if we
27927 are reading input from this window. For the selected window, we
27928 want the cursor type given by the frame parameter or buffer local
27929 setting of cursor-type. If explicitly marked off, draw no cursor.
27930 In all other cases, we want a hollow box cursor. */
27931
27932 static enum text_cursor_kinds
27933 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27934 bool *active_cursor)
27935 {
27936 struct frame *f = XFRAME (w->frame);
27937 struct buffer *b = XBUFFER (w->contents);
27938 int cursor_type = DEFAULT_CURSOR;
27939 Lisp_Object alt_cursor;
27940 bool non_selected = false;
27941
27942 *active_cursor = true;
27943
27944 /* Echo area */
27945 if (cursor_in_echo_area
27946 && FRAME_HAS_MINIBUF_P (f)
27947 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27948 {
27949 if (w == XWINDOW (echo_area_window))
27950 {
27951 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27952 {
27953 *width = FRAME_CURSOR_WIDTH (f);
27954 return FRAME_DESIRED_CURSOR (f);
27955 }
27956 else
27957 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27958 }
27959
27960 *active_cursor = false;
27961 non_selected = true;
27962 }
27963
27964 /* Detect a nonselected window or nonselected frame. */
27965 else if (w != XWINDOW (f->selected_window)
27966 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27967 {
27968 *active_cursor = false;
27969
27970 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27971 return NO_CURSOR;
27972
27973 non_selected = true;
27974 }
27975
27976 /* Never display a cursor in a window in which cursor-type is nil. */
27977 if (NILP (BVAR (b, cursor_type)))
27978 return NO_CURSOR;
27979
27980 /* Get the normal cursor type for this window. */
27981 if (EQ (BVAR (b, cursor_type), Qt))
27982 {
27983 cursor_type = FRAME_DESIRED_CURSOR (f);
27984 *width = FRAME_CURSOR_WIDTH (f);
27985 }
27986 else
27987 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27988
27989 /* Use cursor-in-non-selected-windows instead
27990 for non-selected window or frame. */
27991 if (non_selected)
27992 {
27993 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27994 if (!EQ (Qt, alt_cursor))
27995 return get_specified_cursor_type (alt_cursor, width);
27996 /* t means modify the normal cursor type. */
27997 if (cursor_type == FILLED_BOX_CURSOR)
27998 cursor_type = HOLLOW_BOX_CURSOR;
27999 else if (cursor_type == BAR_CURSOR && *width > 1)
28000 --*width;
28001 return cursor_type;
28002 }
28003
28004 /* Use normal cursor if not blinked off. */
28005 if (!w->cursor_off_p)
28006 {
28007 #ifdef HAVE_XWIDGETS
28008 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
28009 return NO_CURSOR;
28010 #endif
28011 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28012 {
28013 if (cursor_type == FILLED_BOX_CURSOR)
28014 {
28015 /* Using a block cursor on large images can be very annoying.
28016 So use a hollow cursor for "large" images.
28017 If image is not transparent (no mask), also use hollow cursor. */
28018 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28019 if (img != NULL && IMAGEP (img->spec))
28020 {
28021 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
28022 where N = size of default frame font size.
28023 This should cover most of the "tiny" icons people may use. */
28024 if (!img->mask
28025 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
28026 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
28027 cursor_type = HOLLOW_BOX_CURSOR;
28028 }
28029 }
28030 else if (cursor_type != NO_CURSOR)
28031 {
28032 /* Display current only supports BOX and HOLLOW cursors for images.
28033 So for now, unconditionally use a HOLLOW cursor when cursor is
28034 not a solid box cursor. */
28035 cursor_type = HOLLOW_BOX_CURSOR;
28036 }
28037 }
28038 return cursor_type;
28039 }
28040
28041 /* Cursor is blinked off, so determine how to "toggle" it. */
28042
28043 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
28044 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
28045 return get_specified_cursor_type (XCDR (alt_cursor), width);
28046
28047 /* Then see if frame has specified a specific blink off cursor type. */
28048 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
28049 {
28050 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
28051 return FRAME_BLINK_OFF_CURSOR (f);
28052 }
28053
28054 #if false
28055 /* Some people liked having a permanently visible blinking cursor,
28056 while others had very strong opinions against it. So it was
28057 decided to remove it. KFS 2003-09-03 */
28058
28059 /* Finally perform built-in cursor blinking:
28060 filled box <-> hollow box
28061 wide [h]bar <-> narrow [h]bar
28062 narrow [h]bar <-> no cursor
28063 other type <-> no cursor */
28064
28065 if (cursor_type == FILLED_BOX_CURSOR)
28066 return HOLLOW_BOX_CURSOR;
28067
28068 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
28069 {
28070 *width = 1;
28071 return cursor_type;
28072 }
28073 #endif
28074
28075 return NO_CURSOR;
28076 }
28077
28078
28079 /* Notice when the text cursor of window W has been completely
28080 overwritten by a drawing operation that outputs glyphs in AREA
28081 starting at X0 and ending at X1 in the line starting at Y0 and
28082 ending at Y1. X coordinates are area-relative. X1 < 0 means all
28083 the rest of the line after X0 has been written. Y coordinates
28084 are window-relative. */
28085
28086 static void
28087 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
28088 int x0, int x1, int y0, int y1)
28089 {
28090 int cx0, cx1, cy0, cy1;
28091 struct glyph_row *row;
28092
28093 if (!w->phys_cursor_on_p)
28094 return;
28095 if (area != TEXT_AREA)
28096 return;
28097
28098 if (w->phys_cursor.vpos < 0
28099 || w->phys_cursor.vpos >= w->current_matrix->nrows
28100 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
28101 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
28102 return;
28103
28104 if (row->cursor_in_fringe_p)
28105 {
28106 row->cursor_in_fringe_p = false;
28107 draw_fringe_bitmap (w, row, row->reversed_p);
28108 w->phys_cursor_on_p = false;
28109 return;
28110 }
28111
28112 cx0 = w->phys_cursor.x;
28113 cx1 = cx0 + w->phys_cursor_width;
28114 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
28115 return;
28116
28117 /* The cursor image will be completely removed from the
28118 screen if the output area intersects the cursor area in
28119 y-direction. When we draw in [y0 y1[, and some part of
28120 the cursor is at y < y0, that part must have been drawn
28121 before. When scrolling, the cursor is erased before
28122 actually scrolling, so we don't come here. When not
28123 scrolling, the rows above the old cursor row must have
28124 changed, and in this case these rows must have written
28125 over the cursor image.
28126
28127 Likewise if part of the cursor is below y1, with the
28128 exception of the cursor being in the first blank row at
28129 the buffer and window end because update_text_area
28130 doesn't draw that row. (Except when it does, but
28131 that's handled in update_text_area.) */
28132
28133 cy0 = w->phys_cursor.y;
28134 cy1 = cy0 + w->phys_cursor_height;
28135 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
28136 return;
28137
28138 w->phys_cursor_on_p = false;
28139 }
28140
28141 #endif /* HAVE_WINDOW_SYSTEM */
28142
28143 \f
28144 /************************************************************************
28145 Mouse Face
28146 ************************************************************************/
28147
28148 #ifdef HAVE_WINDOW_SYSTEM
28149
28150 /* EXPORT for RIF:
28151 Fix the display of area AREA of overlapping row ROW in window W
28152 with respect to the overlapping part OVERLAPS. */
28153
28154 void
28155 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
28156 enum glyph_row_area area, int overlaps)
28157 {
28158 int i, x;
28159
28160 block_input ();
28161
28162 x = 0;
28163 for (i = 0; i < row->used[area];)
28164 {
28165 if (row->glyphs[area][i].overlaps_vertically_p)
28166 {
28167 int start = i, start_x = x;
28168
28169 do
28170 {
28171 x += row->glyphs[area][i].pixel_width;
28172 ++i;
28173 }
28174 while (i < row->used[area]
28175 && row->glyphs[area][i].overlaps_vertically_p);
28176
28177 draw_glyphs (w, start_x, row, area,
28178 start, i,
28179 DRAW_NORMAL_TEXT, overlaps);
28180 }
28181 else
28182 {
28183 x += row->glyphs[area][i].pixel_width;
28184 ++i;
28185 }
28186 }
28187
28188 unblock_input ();
28189 }
28190
28191
28192 /* EXPORT:
28193 Draw the cursor glyph of window W in glyph row ROW. See the
28194 comment of draw_glyphs for the meaning of HL. */
28195
28196 void
28197 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
28198 enum draw_glyphs_face hl)
28199 {
28200 /* If cursor hpos is out of bounds, don't draw garbage. This can
28201 happen in mini-buffer windows when switching between echo area
28202 glyphs and mini-buffer. */
28203 if ((row->reversed_p
28204 ? (w->phys_cursor.hpos >= 0)
28205 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
28206 {
28207 bool on_p = w->phys_cursor_on_p;
28208 int x1;
28209 int hpos = w->phys_cursor.hpos;
28210
28211 /* When the window is hscrolled, cursor hpos can legitimately be
28212 out of bounds, but we draw the cursor at the corresponding
28213 window margin in that case. */
28214 if (!row->reversed_p && hpos < 0)
28215 hpos = 0;
28216 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28217 hpos = row->used[TEXT_AREA] - 1;
28218
28219 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
28220 hl, 0);
28221 w->phys_cursor_on_p = on_p;
28222
28223 if (hl == DRAW_CURSOR)
28224 w->phys_cursor_width = x1 - w->phys_cursor.x;
28225 /* When we erase the cursor, and ROW is overlapped by other
28226 rows, make sure that these overlapping parts of other rows
28227 are redrawn. */
28228 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
28229 {
28230 w->phys_cursor_width = x1 - w->phys_cursor.x;
28231
28232 if (row > w->current_matrix->rows
28233 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
28234 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
28235 OVERLAPS_ERASED_CURSOR);
28236
28237 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
28238 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
28239 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
28240 OVERLAPS_ERASED_CURSOR);
28241 }
28242 }
28243 }
28244
28245
28246 /* Erase the image of a cursor of window W from the screen. */
28247
28248 void
28249 erase_phys_cursor (struct window *w)
28250 {
28251 struct frame *f = XFRAME (w->frame);
28252 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28253 int hpos = w->phys_cursor.hpos;
28254 int vpos = w->phys_cursor.vpos;
28255 bool mouse_face_here_p = false;
28256 struct glyph_matrix *active_glyphs = w->current_matrix;
28257 struct glyph_row *cursor_row;
28258 struct glyph *cursor_glyph;
28259 enum draw_glyphs_face hl;
28260
28261 /* No cursor displayed or row invalidated => nothing to do on the
28262 screen. */
28263 if (w->phys_cursor_type == NO_CURSOR)
28264 goto mark_cursor_off;
28265
28266 /* VPOS >= active_glyphs->nrows means that window has been resized.
28267 Don't bother to erase the cursor. */
28268 if (vpos >= active_glyphs->nrows)
28269 goto mark_cursor_off;
28270
28271 /* If row containing cursor is marked invalid, there is nothing we
28272 can do. */
28273 cursor_row = MATRIX_ROW (active_glyphs, vpos);
28274 if (!cursor_row->enabled_p)
28275 goto mark_cursor_off;
28276
28277 /* If line spacing is > 0, old cursor may only be partially visible in
28278 window after split-window. So adjust visible height. */
28279 cursor_row->visible_height = min (cursor_row->visible_height,
28280 window_text_bottom_y (w) - cursor_row->y);
28281
28282 /* If row is completely invisible, don't attempt to delete a cursor which
28283 isn't there. This can happen if cursor is at top of a window, and
28284 we switch to a buffer with a header line in that window. */
28285 if (cursor_row->visible_height <= 0)
28286 goto mark_cursor_off;
28287
28288 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
28289 if (cursor_row->cursor_in_fringe_p)
28290 {
28291 cursor_row->cursor_in_fringe_p = false;
28292 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
28293 goto mark_cursor_off;
28294 }
28295
28296 /* This can happen when the new row is shorter than the old one.
28297 In this case, either draw_glyphs or clear_end_of_line
28298 should have cleared the cursor. Note that we wouldn't be
28299 able to erase the cursor in this case because we don't have a
28300 cursor glyph at hand. */
28301 if ((cursor_row->reversed_p
28302 ? (w->phys_cursor.hpos < 0)
28303 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
28304 goto mark_cursor_off;
28305
28306 /* When the window is hscrolled, cursor hpos can legitimately be out
28307 of bounds, but we draw the cursor at the corresponding window
28308 margin in that case. */
28309 if (!cursor_row->reversed_p && hpos < 0)
28310 hpos = 0;
28311 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
28312 hpos = cursor_row->used[TEXT_AREA] - 1;
28313
28314 /* If the cursor is in the mouse face area, redisplay that when
28315 we clear the cursor. */
28316 if (! NILP (hlinfo->mouse_face_window)
28317 && coords_in_mouse_face_p (w, hpos, vpos)
28318 /* Don't redraw the cursor's spot in mouse face if it is at the
28319 end of a line (on a newline). The cursor appears there, but
28320 mouse highlighting does not. */
28321 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
28322 mouse_face_here_p = true;
28323
28324 /* Maybe clear the display under the cursor. */
28325 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
28326 {
28327 int x, y;
28328 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
28329 int width;
28330
28331 cursor_glyph = get_phys_cursor_glyph (w);
28332 if (cursor_glyph == NULL)
28333 goto mark_cursor_off;
28334
28335 width = cursor_glyph->pixel_width;
28336 x = w->phys_cursor.x;
28337 if (x < 0)
28338 {
28339 width += x;
28340 x = 0;
28341 }
28342 width = min (width, window_box_width (w, TEXT_AREA) - x);
28343 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
28344 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28345
28346 if (width > 0)
28347 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28348 }
28349
28350 /* Erase the cursor by redrawing the character underneath it. */
28351 if (mouse_face_here_p)
28352 hl = DRAW_MOUSE_FACE;
28353 else
28354 hl = DRAW_NORMAL_TEXT;
28355 draw_phys_cursor_glyph (w, cursor_row, hl);
28356
28357 mark_cursor_off:
28358 w->phys_cursor_on_p = false;
28359 w->phys_cursor_type = NO_CURSOR;
28360 }
28361
28362
28363 /* Display or clear cursor of window W. If !ON, clear the cursor.
28364 If ON, display the cursor; where to put the cursor is specified by
28365 HPOS, VPOS, X and Y. */
28366
28367 void
28368 display_and_set_cursor (struct window *w, bool on,
28369 int hpos, int vpos, int x, int y)
28370 {
28371 struct frame *f = XFRAME (w->frame);
28372 int new_cursor_type;
28373 int new_cursor_width;
28374 bool active_cursor;
28375 struct glyph_row *glyph_row;
28376 struct glyph *glyph;
28377
28378 /* This is pointless on invisible frames, and dangerous on garbaged
28379 windows and frames; in the latter case, the frame or window may
28380 be in the midst of changing its size, and x and y may be off the
28381 window. */
28382 if (! FRAME_VISIBLE_P (f)
28383 || FRAME_GARBAGED_P (f)
28384 || vpos >= w->current_matrix->nrows
28385 || hpos >= w->current_matrix->matrix_w)
28386 return;
28387
28388 /* If cursor is off and we want it off, return quickly. */
28389 if (!on && !w->phys_cursor_on_p)
28390 return;
28391
28392 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28393 /* If cursor row is not enabled, we don't really know where to
28394 display the cursor. */
28395 if (!glyph_row->enabled_p)
28396 {
28397 w->phys_cursor_on_p = false;
28398 return;
28399 }
28400
28401 glyph = NULL;
28402 if (!glyph_row->exact_window_width_line_p
28403 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
28404 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28405
28406 eassert (input_blocked_p ());
28407
28408 /* Set new_cursor_type to the cursor we want to be displayed. */
28409 new_cursor_type = get_window_cursor_type (w, glyph,
28410 &new_cursor_width, &active_cursor);
28411
28412 /* If cursor is currently being shown and we don't want it to be or
28413 it is in the wrong place, or the cursor type is not what we want,
28414 erase it. */
28415 if (w->phys_cursor_on_p
28416 && (!on
28417 || w->phys_cursor.x != x
28418 || w->phys_cursor.y != y
28419 /* HPOS can be negative in R2L rows whose
28420 exact_window_width_line_p flag is set (i.e. their newline
28421 would "overflow into the fringe"). */
28422 || hpos < 0
28423 || new_cursor_type != w->phys_cursor_type
28424 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28425 && new_cursor_width != w->phys_cursor_width)))
28426 erase_phys_cursor (w);
28427
28428 /* Don't check phys_cursor_on_p here because that flag is only set
28429 to false in some cases where we know that the cursor has been
28430 completely erased, to avoid the extra work of erasing the cursor
28431 twice. In other words, phys_cursor_on_p can be true and the cursor
28432 still not be visible, or it has only been partly erased. */
28433 if (on)
28434 {
28435 w->phys_cursor_ascent = glyph_row->ascent;
28436 w->phys_cursor_height = glyph_row->height;
28437
28438 /* Set phys_cursor_.* before x_draw_.* is called because some
28439 of them may need the information. */
28440 w->phys_cursor.x = x;
28441 w->phys_cursor.y = glyph_row->y;
28442 w->phys_cursor.hpos = hpos;
28443 w->phys_cursor.vpos = vpos;
28444 }
28445
28446 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28447 new_cursor_type, new_cursor_width,
28448 on, active_cursor);
28449 }
28450
28451
28452 /* Switch the display of W's cursor on or off, according to the value
28453 of ON. */
28454
28455 static void
28456 update_window_cursor (struct window *w, bool on)
28457 {
28458 /* Don't update cursor in windows whose frame is in the process
28459 of being deleted. */
28460 if (w->current_matrix)
28461 {
28462 int hpos = w->phys_cursor.hpos;
28463 int vpos = w->phys_cursor.vpos;
28464 struct glyph_row *row;
28465
28466 if (vpos >= w->current_matrix->nrows
28467 || hpos >= w->current_matrix->matrix_w)
28468 return;
28469
28470 row = MATRIX_ROW (w->current_matrix, vpos);
28471
28472 /* When the window is hscrolled, cursor hpos can legitimately be
28473 out of bounds, but we draw the cursor at the corresponding
28474 window margin in that case. */
28475 if (!row->reversed_p && hpos < 0)
28476 hpos = 0;
28477 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28478 hpos = row->used[TEXT_AREA] - 1;
28479
28480 block_input ();
28481 display_and_set_cursor (w, on, hpos, vpos,
28482 w->phys_cursor.x, w->phys_cursor.y);
28483 unblock_input ();
28484 }
28485 }
28486
28487
28488 /* Call update_window_cursor with parameter ON_P on all leaf windows
28489 in the window tree rooted at W. */
28490
28491 static void
28492 update_cursor_in_window_tree (struct window *w, bool on_p)
28493 {
28494 while (w)
28495 {
28496 if (WINDOWP (w->contents))
28497 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28498 else
28499 update_window_cursor (w, on_p);
28500
28501 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28502 }
28503 }
28504
28505
28506 /* EXPORT:
28507 Display the cursor on window W, or clear it, according to ON_P.
28508 Don't change the cursor's position. */
28509
28510 void
28511 x_update_cursor (struct frame *f, bool on_p)
28512 {
28513 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28514 }
28515
28516
28517 /* EXPORT:
28518 Clear the cursor of window W to background color, and mark the
28519 cursor as not shown. This is used when the text where the cursor
28520 is about to be rewritten. */
28521
28522 void
28523 x_clear_cursor (struct window *w)
28524 {
28525 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28526 update_window_cursor (w, false);
28527 }
28528
28529 #endif /* HAVE_WINDOW_SYSTEM */
28530
28531 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28532 and MSDOS. */
28533 static void
28534 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28535 int start_hpos, int end_hpos,
28536 enum draw_glyphs_face draw)
28537 {
28538 #ifdef HAVE_WINDOW_SYSTEM
28539 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28540 {
28541 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28542 return;
28543 }
28544 #endif
28545 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28546 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28547 #endif
28548 }
28549
28550 /* Display the active region described by mouse_face_* according to DRAW. */
28551
28552 static void
28553 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28554 {
28555 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28556 struct frame *f = XFRAME (WINDOW_FRAME (w));
28557
28558 if (/* If window is in the process of being destroyed, don't bother
28559 to do anything. */
28560 w->current_matrix != NULL
28561 /* Don't update mouse highlight if hidden. */
28562 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28563 /* Recognize when we are called to operate on rows that don't exist
28564 anymore. This can happen when a window is split. */
28565 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28566 {
28567 bool phys_cursor_on_p = w->phys_cursor_on_p;
28568 struct glyph_row *row, *first, *last;
28569
28570 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28571 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28572
28573 for (row = first; row <= last && row->enabled_p; ++row)
28574 {
28575 int start_hpos, end_hpos, start_x;
28576
28577 /* For all but the first row, the highlight starts at column 0. */
28578 if (row == first)
28579 {
28580 /* R2L rows have BEG and END in reversed order, but the
28581 screen drawing geometry is always left to right. So
28582 we need to mirror the beginning and end of the
28583 highlighted area in R2L rows. */
28584 if (!row->reversed_p)
28585 {
28586 start_hpos = hlinfo->mouse_face_beg_col;
28587 start_x = hlinfo->mouse_face_beg_x;
28588 }
28589 else if (row == last)
28590 {
28591 start_hpos = hlinfo->mouse_face_end_col;
28592 start_x = hlinfo->mouse_face_end_x;
28593 }
28594 else
28595 {
28596 start_hpos = 0;
28597 start_x = 0;
28598 }
28599 }
28600 else if (row->reversed_p && row == last)
28601 {
28602 start_hpos = hlinfo->mouse_face_end_col;
28603 start_x = hlinfo->mouse_face_end_x;
28604 }
28605 else
28606 {
28607 start_hpos = 0;
28608 start_x = 0;
28609 }
28610
28611 if (row == last)
28612 {
28613 if (!row->reversed_p)
28614 end_hpos = hlinfo->mouse_face_end_col;
28615 else if (row == first)
28616 end_hpos = hlinfo->mouse_face_beg_col;
28617 else
28618 {
28619 end_hpos = row->used[TEXT_AREA];
28620 if (draw == DRAW_NORMAL_TEXT)
28621 row->fill_line_p = true; /* Clear to end of line. */
28622 }
28623 }
28624 else if (row->reversed_p && row == first)
28625 end_hpos = hlinfo->mouse_face_beg_col;
28626 else
28627 {
28628 end_hpos = row->used[TEXT_AREA];
28629 if (draw == DRAW_NORMAL_TEXT)
28630 row->fill_line_p = true; /* Clear to end of line. */
28631 }
28632
28633 if (end_hpos > start_hpos)
28634 {
28635 draw_row_with_mouse_face (w, start_x, row,
28636 start_hpos, end_hpos, draw);
28637
28638 row->mouse_face_p
28639 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28640 }
28641 }
28642
28643 #ifdef HAVE_WINDOW_SYSTEM
28644 /* When we've written over the cursor, arrange for it to
28645 be displayed again. */
28646 if (FRAME_WINDOW_P (f)
28647 && phys_cursor_on_p && !w->phys_cursor_on_p)
28648 {
28649 int hpos = w->phys_cursor.hpos;
28650
28651 /* When the window is hscrolled, cursor hpos can legitimately be
28652 out of bounds, but we draw the cursor at the corresponding
28653 window margin in that case. */
28654 if (!row->reversed_p && hpos < 0)
28655 hpos = 0;
28656 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28657 hpos = row->used[TEXT_AREA] - 1;
28658
28659 block_input ();
28660 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28661 w->phys_cursor.x, w->phys_cursor.y);
28662 unblock_input ();
28663 }
28664 #endif /* HAVE_WINDOW_SYSTEM */
28665 }
28666
28667 #ifdef HAVE_WINDOW_SYSTEM
28668 /* Change the mouse cursor. */
28669 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28670 {
28671 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28672 if (draw == DRAW_NORMAL_TEXT
28673 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28674 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28675 else
28676 #endif
28677 if (draw == DRAW_MOUSE_FACE)
28678 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28679 else
28680 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28681 }
28682 #endif /* HAVE_WINDOW_SYSTEM */
28683 }
28684
28685 /* EXPORT:
28686 Clear out the mouse-highlighted active region.
28687 Redraw it un-highlighted first. Value is true if mouse
28688 face was actually drawn unhighlighted. */
28689
28690 bool
28691 clear_mouse_face (Mouse_HLInfo *hlinfo)
28692 {
28693 bool cleared
28694 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28695 if (cleared)
28696 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28697 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28698 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28699 hlinfo->mouse_face_window = Qnil;
28700 hlinfo->mouse_face_overlay = Qnil;
28701 return cleared;
28702 }
28703
28704 /* Return true if the coordinates HPOS and VPOS on windows W are
28705 within the mouse face on that window. */
28706 static bool
28707 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28708 {
28709 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28710
28711 /* Quickly resolve the easy cases. */
28712 if (!(WINDOWP (hlinfo->mouse_face_window)
28713 && XWINDOW (hlinfo->mouse_face_window) == w))
28714 return false;
28715 if (vpos < hlinfo->mouse_face_beg_row
28716 || vpos > hlinfo->mouse_face_end_row)
28717 return false;
28718 if (vpos > hlinfo->mouse_face_beg_row
28719 && vpos < hlinfo->mouse_face_end_row)
28720 return true;
28721
28722 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28723 {
28724 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28725 {
28726 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28727 return true;
28728 }
28729 else if ((vpos == hlinfo->mouse_face_beg_row
28730 && hpos >= hlinfo->mouse_face_beg_col)
28731 || (vpos == hlinfo->mouse_face_end_row
28732 && hpos < hlinfo->mouse_face_end_col))
28733 return true;
28734 }
28735 else
28736 {
28737 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28738 {
28739 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28740 return true;
28741 }
28742 else if ((vpos == hlinfo->mouse_face_beg_row
28743 && hpos <= hlinfo->mouse_face_beg_col)
28744 || (vpos == hlinfo->mouse_face_end_row
28745 && hpos > hlinfo->mouse_face_end_col))
28746 return true;
28747 }
28748 return false;
28749 }
28750
28751
28752 /* EXPORT:
28753 True if physical cursor of window W is within mouse face. */
28754
28755 bool
28756 cursor_in_mouse_face_p (struct window *w)
28757 {
28758 int hpos = w->phys_cursor.hpos;
28759 int vpos = w->phys_cursor.vpos;
28760 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28761
28762 /* When the window is hscrolled, cursor hpos can legitimately be out
28763 of bounds, but we draw the cursor at the corresponding window
28764 margin in that case. */
28765 if (!row->reversed_p && hpos < 0)
28766 hpos = 0;
28767 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28768 hpos = row->used[TEXT_AREA] - 1;
28769
28770 return coords_in_mouse_face_p (w, hpos, vpos);
28771 }
28772
28773
28774 \f
28775 /* Find the glyph rows START_ROW and END_ROW of window W that display
28776 characters between buffer positions START_CHARPOS and END_CHARPOS
28777 (excluding END_CHARPOS). DISP_STRING is a display string that
28778 covers these buffer positions. This is similar to
28779 row_containing_pos, but is more accurate when bidi reordering makes
28780 buffer positions change non-linearly with glyph rows. */
28781 static void
28782 rows_from_pos_range (struct window *w,
28783 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28784 Lisp_Object disp_string,
28785 struct glyph_row **start, struct glyph_row **end)
28786 {
28787 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28788 int last_y = window_text_bottom_y (w);
28789 struct glyph_row *row;
28790
28791 *start = NULL;
28792 *end = NULL;
28793
28794 while (!first->enabled_p
28795 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28796 first++;
28797
28798 /* Find the START row. */
28799 for (row = first;
28800 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28801 row++)
28802 {
28803 /* A row can potentially be the START row if the range of the
28804 characters it displays intersects the range
28805 [START_CHARPOS..END_CHARPOS). */
28806 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28807 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28808 /* See the commentary in row_containing_pos, for the
28809 explanation of the complicated way to check whether
28810 some position is beyond the end of the characters
28811 displayed by a row. */
28812 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28813 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28814 && !row->ends_at_zv_p
28815 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28816 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28817 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28818 && !row->ends_at_zv_p
28819 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28820 {
28821 /* Found a candidate row. Now make sure at least one of the
28822 glyphs it displays has a charpos from the range
28823 [START_CHARPOS..END_CHARPOS).
28824
28825 This is not obvious because bidi reordering could make
28826 buffer positions of a row be 1,2,3,102,101,100, and if we
28827 want to highlight characters in [50..60), we don't want
28828 this row, even though [50..60) does intersect [1..103),
28829 the range of character positions given by the row's start
28830 and end positions. */
28831 struct glyph *g = row->glyphs[TEXT_AREA];
28832 struct glyph *e = g + row->used[TEXT_AREA];
28833
28834 while (g < e)
28835 {
28836 if (((BUFFERP (g->object) || NILP (g->object))
28837 && start_charpos <= g->charpos && g->charpos < end_charpos)
28838 /* A glyph that comes from DISP_STRING is by
28839 definition to be highlighted. */
28840 || EQ (g->object, disp_string))
28841 *start = row;
28842 g++;
28843 }
28844 if (*start)
28845 break;
28846 }
28847 }
28848
28849 /* Find the END row. */
28850 if (!*start
28851 /* If the last row is partially visible, start looking for END
28852 from that row, instead of starting from FIRST. */
28853 && !(row->enabled_p
28854 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28855 row = first;
28856 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28857 {
28858 struct glyph_row *next = row + 1;
28859 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28860
28861 if (!next->enabled_p
28862 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28863 /* The first row >= START whose range of displayed characters
28864 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28865 is the row END + 1. */
28866 || (start_charpos < next_start
28867 && end_charpos < next_start)
28868 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28869 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28870 && !next->ends_at_zv_p
28871 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28872 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28873 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28874 && !next->ends_at_zv_p
28875 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28876 {
28877 *end = row;
28878 break;
28879 }
28880 else
28881 {
28882 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28883 but none of the characters it displays are in the range, it is
28884 also END + 1. */
28885 struct glyph *g = next->glyphs[TEXT_AREA];
28886 struct glyph *s = g;
28887 struct glyph *e = g + next->used[TEXT_AREA];
28888
28889 while (g < e)
28890 {
28891 if (((BUFFERP (g->object) || NILP (g->object))
28892 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28893 /* If the buffer position of the first glyph in
28894 the row is equal to END_CHARPOS, it means
28895 the last character to be highlighted is the
28896 newline of ROW, and we must consider NEXT as
28897 END, not END+1. */
28898 || (((!next->reversed_p && g == s)
28899 || (next->reversed_p && g == e - 1))
28900 && (g->charpos == end_charpos
28901 /* Special case for when NEXT is an
28902 empty line at ZV. */
28903 || (g->charpos == -1
28904 && !row->ends_at_zv_p
28905 && next_start == end_charpos)))))
28906 /* A glyph that comes from DISP_STRING is by
28907 definition to be highlighted. */
28908 || EQ (g->object, disp_string))
28909 break;
28910 g++;
28911 }
28912 if (g == e)
28913 {
28914 *end = row;
28915 break;
28916 }
28917 /* The first row that ends at ZV must be the last to be
28918 highlighted. */
28919 else if (next->ends_at_zv_p)
28920 {
28921 *end = next;
28922 break;
28923 }
28924 }
28925 }
28926 }
28927
28928 /* This function sets the mouse_face_* elements of HLINFO, assuming
28929 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28930 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28931 for the overlay or run of text properties specifying the mouse
28932 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28933 before-string and after-string that must also be highlighted.
28934 DISP_STRING, if non-nil, is a display string that may cover some
28935 or all of the highlighted text. */
28936
28937 static void
28938 mouse_face_from_buffer_pos (Lisp_Object window,
28939 Mouse_HLInfo *hlinfo,
28940 ptrdiff_t mouse_charpos,
28941 ptrdiff_t start_charpos,
28942 ptrdiff_t end_charpos,
28943 Lisp_Object before_string,
28944 Lisp_Object after_string,
28945 Lisp_Object disp_string)
28946 {
28947 struct window *w = XWINDOW (window);
28948 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28949 struct glyph_row *r1, *r2;
28950 struct glyph *glyph, *end;
28951 ptrdiff_t ignore, pos;
28952 int x;
28953
28954 eassert (NILP (disp_string) || STRINGP (disp_string));
28955 eassert (NILP (before_string) || STRINGP (before_string));
28956 eassert (NILP (after_string) || STRINGP (after_string));
28957
28958 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28959 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28960 if (r1 == NULL)
28961 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28962 /* If the before-string or display-string contains newlines,
28963 rows_from_pos_range skips to its last row. Move back. */
28964 if (!NILP (before_string) || !NILP (disp_string))
28965 {
28966 struct glyph_row *prev;
28967 while ((prev = r1 - 1, prev >= first)
28968 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28969 && prev->used[TEXT_AREA] > 0)
28970 {
28971 struct glyph *beg = prev->glyphs[TEXT_AREA];
28972 glyph = beg + prev->used[TEXT_AREA];
28973 while (--glyph >= beg && NILP (glyph->object));
28974 if (glyph < beg
28975 || !(EQ (glyph->object, before_string)
28976 || EQ (glyph->object, disp_string)))
28977 break;
28978 r1 = prev;
28979 }
28980 }
28981 if (r2 == NULL)
28982 {
28983 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28984 hlinfo->mouse_face_past_end = true;
28985 }
28986 else if (!NILP (after_string))
28987 {
28988 /* If the after-string has newlines, advance to its last row. */
28989 struct glyph_row *next;
28990 struct glyph_row *last
28991 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28992
28993 for (next = r2 + 1;
28994 next <= last
28995 && next->used[TEXT_AREA] > 0
28996 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28997 ++next)
28998 r2 = next;
28999 }
29000 /* The rest of the display engine assumes that mouse_face_beg_row is
29001 either above mouse_face_end_row or identical to it. But with
29002 bidi-reordered continued lines, the row for START_CHARPOS could
29003 be below the row for END_CHARPOS. If so, swap the rows and store
29004 them in correct order. */
29005 if (r1->y > r2->y)
29006 {
29007 struct glyph_row *tem = r2;
29008
29009 r2 = r1;
29010 r1 = tem;
29011 }
29012
29013 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
29014 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
29015
29016 /* For a bidi-reordered row, the positions of BEFORE_STRING,
29017 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
29018 could be anywhere in the row and in any order. The strategy
29019 below is to find the leftmost and the rightmost glyph that
29020 belongs to either of these 3 strings, or whose position is
29021 between START_CHARPOS and END_CHARPOS, and highlight all the
29022 glyphs between those two. This may cover more than just the text
29023 between START_CHARPOS and END_CHARPOS if the range of characters
29024 strides the bidi level boundary, e.g. if the beginning is in R2L
29025 text while the end is in L2R text or vice versa. */
29026 if (!r1->reversed_p)
29027 {
29028 /* This row is in a left to right paragraph. Scan it left to
29029 right. */
29030 glyph = r1->glyphs[TEXT_AREA];
29031 end = glyph + r1->used[TEXT_AREA];
29032 x = r1->x;
29033
29034 /* Skip truncation glyphs at the start of the glyph row. */
29035 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29036 for (; glyph < end
29037 && NILP (glyph->object)
29038 && glyph->charpos < 0;
29039 ++glyph)
29040 x += glyph->pixel_width;
29041
29042 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29043 or DISP_STRING, and the first glyph from buffer whose
29044 position is between START_CHARPOS and END_CHARPOS. */
29045 for (; glyph < end
29046 && !NILP (glyph->object)
29047 && !EQ (glyph->object, disp_string)
29048 && !(BUFFERP (glyph->object)
29049 && (glyph->charpos >= start_charpos
29050 && glyph->charpos < end_charpos));
29051 ++glyph)
29052 {
29053 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29054 are present at buffer positions between START_CHARPOS and
29055 END_CHARPOS, or if they come from an overlay. */
29056 if (EQ (glyph->object, before_string))
29057 {
29058 pos = string_buffer_position (before_string,
29059 start_charpos);
29060 /* If pos == 0, it means before_string came from an
29061 overlay, not from a buffer position. */
29062 if (!pos || (pos >= start_charpos && pos < end_charpos))
29063 break;
29064 }
29065 else if (EQ (glyph->object, after_string))
29066 {
29067 pos = string_buffer_position (after_string, end_charpos);
29068 if (!pos || (pos >= start_charpos && pos < end_charpos))
29069 break;
29070 }
29071 x += glyph->pixel_width;
29072 }
29073 hlinfo->mouse_face_beg_x = x;
29074 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29075 }
29076 else
29077 {
29078 /* This row is in a right to left paragraph. Scan it right to
29079 left. */
29080 struct glyph *g;
29081
29082 end = r1->glyphs[TEXT_AREA] - 1;
29083 glyph = end + r1->used[TEXT_AREA];
29084
29085 /* Skip truncation glyphs at the start of the glyph row. */
29086 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29087 for (; glyph > end
29088 && NILP (glyph->object)
29089 && glyph->charpos < 0;
29090 --glyph)
29091 ;
29092
29093 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29094 or DISP_STRING, and the first glyph from buffer whose
29095 position is between START_CHARPOS and END_CHARPOS. */
29096 for (; glyph > end
29097 && !NILP (glyph->object)
29098 && !EQ (glyph->object, disp_string)
29099 && !(BUFFERP (glyph->object)
29100 && (glyph->charpos >= start_charpos
29101 && glyph->charpos < end_charpos));
29102 --glyph)
29103 {
29104 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29105 are present at buffer positions between START_CHARPOS and
29106 END_CHARPOS, or if they come from an overlay. */
29107 if (EQ (glyph->object, before_string))
29108 {
29109 pos = string_buffer_position (before_string, start_charpos);
29110 /* If pos == 0, it means before_string came from an
29111 overlay, not from a buffer position. */
29112 if (!pos || (pos >= start_charpos && pos < end_charpos))
29113 break;
29114 }
29115 else if (EQ (glyph->object, after_string))
29116 {
29117 pos = string_buffer_position (after_string, end_charpos);
29118 if (!pos || (pos >= start_charpos && pos < end_charpos))
29119 break;
29120 }
29121 }
29122
29123 glyph++; /* first glyph to the right of the highlighted area */
29124 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
29125 x += g->pixel_width;
29126 hlinfo->mouse_face_beg_x = x;
29127 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29128 }
29129
29130 /* If the highlight ends in a different row, compute GLYPH and END
29131 for the end row. Otherwise, reuse the values computed above for
29132 the row where the highlight begins. */
29133 if (r2 != r1)
29134 {
29135 if (!r2->reversed_p)
29136 {
29137 glyph = r2->glyphs[TEXT_AREA];
29138 end = glyph + r2->used[TEXT_AREA];
29139 x = r2->x;
29140 }
29141 else
29142 {
29143 end = r2->glyphs[TEXT_AREA] - 1;
29144 glyph = end + r2->used[TEXT_AREA];
29145 }
29146 }
29147
29148 if (!r2->reversed_p)
29149 {
29150 /* Skip truncation and continuation glyphs near the end of the
29151 row, and also blanks and stretch glyphs inserted by
29152 extend_face_to_end_of_line. */
29153 while (end > glyph
29154 && NILP ((end - 1)->object))
29155 --end;
29156 /* Scan the rest of the glyph row from the end, looking for the
29157 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29158 DISP_STRING, or whose position is between START_CHARPOS
29159 and END_CHARPOS */
29160 for (--end;
29161 end > glyph
29162 && !NILP (end->object)
29163 && !EQ (end->object, disp_string)
29164 && !(BUFFERP (end->object)
29165 && (end->charpos >= start_charpos
29166 && end->charpos < end_charpos));
29167 --end)
29168 {
29169 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29170 are present at buffer positions between START_CHARPOS and
29171 END_CHARPOS, or if they come from an overlay. */
29172 if (EQ (end->object, before_string))
29173 {
29174 pos = string_buffer_position (before_string, start_charpos);
29175 if (!pos || (pos >= start_charpos && pos < end_charpos))
29176 break;
29177 }
29178 else if (EQ (end->object, after_string))
29179 {
29180 pos = string_buffer_position (after_string, end_charpos);
29181 if (!pos || (pos >= start_charpos && pos < end_charpos))
29182 break;
29183 }
29184 }
29185 /* Find the X coordinate of the last glyph to be highlighted. */
29186 for (; glyph <= end; ++glyph)
29187 x += glyph->pixel_width;
29188
29189 hlinfo->mouse_face_end_x = x;
29190 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
29191 }
29192 else
29193 {
29194 /* Skip truncation and continuation glyphs near the end of the
29195 row, and also blanks and stretch glyphs inserted by
29196 extend_face_to_end_of_line. */
29197 x = r2->x;
29198 end++;
29199 while (end < glyph
29200 && NILP (end->object))
29201 {
29202 x += end->pixel_width;
29203 ++end;
29204 }
29205 /* Scan the rest of the glyph row from the end, looking for the
29206 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29207 DISP_STRING, or whose position is between START_CHARPOS
29208 and END_CHARPOS */
29209 for ( ;
29210 end < glyph
29211 && !NILP (end->object)
29212 && !EQ (end->object, disp_string)
29213 && !(BUFFERP (end->object)
29214 && (end->charpos >= start_charpos
29215 && end->charpos < end_charpos));
29216 ++end)
29217 {
29218 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29219 are present at buffer positions between START_CHARPOS and
29220 END_CHARPOS, or if they come from an overlay. */
29221 if (EQ (end->object, before_string))
29222 {
29223 pos = string_buffer_position (before_string, start_charpos);
29224 if (!pos || (pos >= start_charpos && pos < end_charpos))
29225 break;
29226 }
29227 else if (EQ (end->object, after_string))
29228 {
29229 pos = string_buffer_position (after_string, end_charpos);
29230 if (!pos || (pos >= start_charpos && pos < end_charpos))
29231 break;
29232 }
29233 x += end->pixel_width;
29234 }
29235 /* If we exited the above loop because we arrived at the last
29236 glyph of the row, and its buffer position is still not in
29237 range, it means the last character in range is the preceding
29238 newline. Bump the end column and x values to get past the
29239 last glyph. */
29240 if (end == glyph
29241 && BUFFERP (end->object)
29242 && (end->charpos < start_charpos
29243 || end->charpos >= end_charpos))
29244 {
29245 x += end->pixel_width;
29246 ++end;
29247 }
29248 hlinfo->mouse_face_end_x = x;
29249 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
29250 }
29251
29252 hlinfo->mouse_face_window = window;
29253 hlinfo->mouse_face_face_id
29254 = face_at_buffer_position (w, mouse_charpos, &ignore,
29255 mouse_charpos + 1,
29256 !hlinfo->mouse_face_hidden, -1);
29257 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29258 }
29259
29260 /* The following function is not used anymore (replaced with
29261 mouse_face_from_string_pos), but I leave it here for the time
29262 being, in case someone would. */
29263
29264 #if false /* not used */
29265
29266 /* Find the position of the glyph for position POS in OBJECT in
29267 window W's current matrix, and return in *X, *Y the pixel
29268 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
29269
29270 RIGHT_P means return the position of the right edge of the glyph.
29271 !RIGHT_P means return the left edge position.
29272
29273 If no glyph for POS exists in the matrix, return the position of
29274 the glyph with the next smaller position that is in the matrix, if
29275 RIGHT_P is false. If RIGHT_P, and no glyph for POS
29276 exists in the matrix, return the position of the glyph with the
29277 next larger position in OBJECT.
29278
29279 Value is true if a glyph was found. */
29280
29281 static bool
29282 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
29283 int *hpos, int *vpos, int *x, int *y, bool right_p)
29284 {
29285 int yb = window_text_bottom_y (w);
29286 struct glyph_row *r;
29287 struct glyph *best_glyph = NULL;
29288 struct glyph_row *best_row = NULL;
29289 int best_x = 0;
29290
29291 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29292 r->enabled_p && r->y < yb;
29293 ++r)
29294 {
29295 struct glyph *g = r->glyphs[TEXT_AREA];
29296 struct glyph *e = g + r->used[TEXT_AREA];
29297 int gx;
29298
29299 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29300 if (EQ (g->object, object))
29301 {
29302 if (g->charpos == pos)
29303 {
29304 best_glyph = g;
29305 best_x = gx;
29306 best_row = r;
29307 goto found;
29308 }
29309 else if (best_glyph == NULL
29310 || ((eabs (g->charpos - pos)
29311 < eabs (best_glyph->charpos - pos))
29312 && (right_p
29313 ? g->charpos < pos
29314 : g->charpos > pos)))
29315 {
29316 best_glyph = g;
29317 best_x = gx;
29318 best_row = r;
29319 }
29320 }
29321 }
29322
29323 found:
29324
29325 if (best_glyph)
29326 {
29327 *x = best_x;
29328 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
29329
29330 if (right_p)
29331 {
29332 *x += best_glyph->pixel_width;
29333 ++*hpos;
29334 }
29335
29336 *y = best_row->y;
29337 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
29338 }
29339
29340 return best_glyph != NULL;
29341 }
29342 #endif /* not used */
29343
29344 /* Find the positions of the first and the last glyphs in window W's
29345 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29346 (assumed to be a string), and return in HLINFO's mouse_face_*
29347 members the pixel and column/row coordinates of those glyphs. */
29348
29349 static void
29350 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29351 Lisp_Object object,
29352 ptrdiff_t startpos, ptrdiff_t endpos)
29353 {
29354 int yb = window_text_bottom_y (w);
29355 struct glyph_row *r;
29356 struct glyph *g, *e;
29357 int gx;
29358 bool found = false;
29359
29360 /* Find the glyph row with at least one position in the range
29361 [STARTPOS..ENDPOS), and the first glyph in that row whose
29362 position belongs to that range. */
29363 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29364 r->enabled_p && r->y < yb;
29365 ++r)
29366 {
29367 if (!r->reversed_p)
29368 {
29369 g = r->glyphs[TEXT_AREA];
29370 e = g + r->used[TEXT_AREA];
29371 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29372 if (EQ (g->object, object)
29373 && startpos <= g->charpos && g->charpos < endpos)
29374 {
29375 hlinfo->mouse_face_beg_row
29376 = MATRIX_ROW_VPOS (r, w->current_matrix);
29377 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29378 hlinfo->mouse_face_beg_x = gx;
29379 found = true;
29380 break;
29381 }
29382 }
29383 else
29384 {
29385 struct glyph *g1;
29386
29387 e = r->glyphs[TEXT_AREA];
29388 g = e + r->used[TEXT_AREA];
29389 for ( ; g > e; --g)
29390 if (EQ ((g-1)->object, object)
29391 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29392 {
29393 hlinfo->mouse_face_beg_row
29394 = MATRIX_ROW_VPOS (r, w->current_matrix);
29395 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29396 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29397 gx += g1->pixel_width;
29398 hlinfo->mouse_face_beg_x = gx;
29399 found = true;
29400 break;
29401 }
29402 }
29403 if (found)
29404 break;
29405 }
29406
29407 if (!found)
29408 return;
29409
29410 /* Starting with the next row, look for the first row which does NOT
29411 include any glyphs whose positions are in the range. */
29412 for (++r; r->enabled_p && r->y < yb; ++r)
29413 {
29414 g = r->glyphs[TEXT_AREA];
29415 e = g + r->used[TEXT_AREA];
29416 found = false;
29417 for ( ; g < e; ++g)
29418 if (EQ (g->object, object)
29419 && startpos <= g->charpos && g->charpos < endpos)
29420 {
29421 found = true;
29422 break;
29423 }
29424 if (!found)
29425 break;
29426 }
29427
29428 /* The highlighted region ends on the previous row. */
29429 r--;
29430
29431 /* Set the end row. */
29432 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29433
29434 /* Compute and set the end column and the end column's horizontal
29435 pixel coordinate. */
29436 if (!r->reversed_p)
29437 {
29438 g = r->glyphs[TEXT_AREA];
29439 e = g + r->used[TEXT_AREA];
29440 for ( ; e > g; --e)
29441 if (EQ ((e-1)->object, object)
29442 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29443 break;
29444 hlinfo->mouse_face_end_col = e - g;
29445
29446 for (gx = r->x; g < e; ++g)
29447 gx += g->pixel_width;
29448 hlinfo->mouse_face_end_x = gx;
29449 }
29450 else
29451 {
29452 e = r->glyphs[TEXT_AREA];
29453 g = e + r->used[TEXT_AREA];
29454 for (gx = r->x ; e < g; ++e)
29455 {
29456 if (EQ (e->object, object)
29457 && startpos <= e->charpos && e->charpos < endpos)
29458 break;
29459 gx += e->pixel_width;
29460 }
29461 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29462 hlinfo->mouse_face_end_x = gx;
29463 }
29464 }
29465
29466 #ifdef HAVE_WINDOW_SYSTEM
29467
29468 /* See if position X, Y is within a hot-spot of an image. */
29469
29470 static bool
29471 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29472 {
29473 if (!CONSP (hot_spot))
29474 return false;
29475
29476 if (EQ (XCAR (hot_spot), Qrect))
29477 {
29478 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29479 Lisp_Object rect = XCDR (hot_spot);
29480 Lisp_Object tem;
29481 if (!CONSP (rect))
29482 return false;
29483 if (!CONSP (XCAR (rect)))
29484 return false;
29485 if (!CONSP (XCDR (rect)))
29486 return false;
29487 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29488 return false;
29489 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29490 return false;
29491 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29492 return false;
29493 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29494 return false;
29495 return true;
29496 }
29497 else if (EQ (XCAR (hot_spot), Qcircle))
29498 {
29499 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29500 Lisp_Object circ = XCDR (hot_spot);
29501 Lisp_Object lr, lx0, ly0;
29502 if (CONSP (circ)
29503 && CONSP (XCAR (circ))
29504 && (lr = XCDR (circ), NUMBERP (lr))
29505 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29506 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29507 {
29508 double r = XFLOATINT (lr);
29509 double dx = XINT (lx0) - x;
29510 double dy = XINT (ly0) - y;
29511 return (dx * dx + dy * dy <= r * r);
29512 }
29513 }
29514 else if (EQ (XCAR (hot_spot), Qpoly))
29515 {
29516 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29517 if (VECTORP (XCDR (hot_spot)))
29518 {
29519 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29520 Lisp_Object *poly = v->contents;
29521 ptrdiff_t n = v->header.size;
29522 ptrdiff_t i;
29523 bool inside = false;
29524 Lisp_Object lx, ly;
29525 int x0, y0;
29526
29527 /* Need an even number of coordinates, and at least 3 edges. */
29528 if (n < 6 || n & 1)
29529 return false;
29530
29531 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29532 If count is odd, we are inside polygon. Pixels on edges
29533 may or may not be included depending on actual geometry of the
29534 polygon. */
29535 if ((lx = poly[n-2], !INTEGERP (lx))
29536 || (ly = poly[n-1], !INTEGERP (lx)))
29537 return false;
29538 x0 = XINT (lx), y0 = XINT (ly);
29539 for (i = 0; i < n; i += 2)
29540 {
29541 int x1 = x0, y1 = y0;
29542 if ((lx = poly[i], !INTEGERP (lx))
29543 || (ly = poly[i+1], !INTEGERP (ly)))
29544 return false;
29545 x0 = XINT (lx), y0 = XINT (ly);
29546
29547 /* Does this segment cross the X line? */
29548 if (x0 >= x)
29549 {
29550 if (x1 >= x)
29551 continue;
29552 }
29553 else if (x1 < x)
29554 continue;
29555 if (y > y0 && y > y1)
29556 continue;
29557 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29558 inside = !inside;
29559 }
29560 return inside;
29561 }
29562 }
29563 return false;
29564 }
29565
29566 Lisp_Object
29567 find_hot_spot (Lisp_Object map, int x, int y)
29568 {
29569 while (CONSP (map))
29570 {
29571 if (CONSP (XCAR (map))
29572 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29573 return XCAR (map);
29574 map = XCDR (map);
29575 }
29576
29577 return Qnil;
29578 }
29579
29580 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29581 3, 3, 0,
29582 doc: /* Lookup in image map MAP coordinates X and Y.
29583 An image map is an alist where each element has the format (AREA ID PLIST).
29584 An AREA is specified as either a rectangle, a circle, or a polygon:
29585 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29586 pixel coordinates of the upper left and bottom right corners.
29587 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29588 and the radius of the circle; r may be a float or integer.
29589 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29590 vector describes one corner in the polygon.
29591 Returns the alist element for the first matching AREA in MAP. */)
29592 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29593 {
29594 if (NILP (map))
29595 return Qnil;
29596
29597 CHECK_NUMBER (x);
29598 CHECK_NUMBER (y);
29599
29600 return find_hot_spot (map,
29601 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29602 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29603 }
29604
29605
29606 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29607 static void
29608 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29609 {
29610 /* Do not change cursor shape while dragging mouse. */
29611 if (EQ (do_mouse_tracking, Qdragging))
29612 return;
29613
29614 if (!NILP (pointer))
29615 {
29616 if (EQ (pointer, Qarrow))
29617 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29618 else if (EQ (pointer, Qhand))
29619 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29620 else if (EQ (pointer, Qtext))
29621 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29622 else if (EQ (pointer, intern ("hdrag")))
29623 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29624 else if (EQ (pointer, intern ("nhdrag")))
29625 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29626 #ifdef HAVE_X_WINDOWS
29627 else if (EQ (pointer, intern ("vdrag")))
29628 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29629 #endif
29630 else if (EQ (pointer, intern ("hourglass")))
29631 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29632 else if (EQ (pointer, Qmodeline))
29633 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29634 else
29635 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29636 }
29637
29638 if (cursor != No_Cursor)
29639 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29640 }
29641
29642 #endif /* HAVE_WINDOW_SYSTEM */
29643
29644 /* Take proper action when mouse has moved to the mode or header line
29645 or marginal area AREA of window W, x-position X and y-position Y.
29646 X is relative to the start of the text display area of W, so the
29647 width of bitmap areas and scroll bars must be subtracted to get a
29648 position relative to the start of the mode line. */
29649
29650 static void
29651 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29652 enum window_part area)
29653 {
29654 struct window *w = XWINDOW (window);
29655 struct frame *f = XFRAME (w->frame);
29656 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29657 #ifdef HAVE_WINDOW_SYSTEM
29658 Display_Info *dpyinfo;
29659 #endif
29660 Cursor cursor = No_Cursor;
29661 Lisp_Object pointer = Qnil;
29662 int dx, dy, width, height;
29663 ptrdiff_t charpos;
29664 Lisp_Object string, object = Qnil;
29665 Lisp_Object pos IF_LINT (= Qnil), help;
29666
29667 Lisp_Object mouse_face;
29668 int original_x_pixel = x;
29669 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29670 struct glyph_row *row IF_LINT (= 0);
29671
29672 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29673 {
29674 int x0;
29675 struct glyph *end;
29676
29677 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29678 returns them in row/column units! */
29679 string = mode_line_string (w, area, &x, &y, &charpos,
29680 &object, &dx, &dy, &width, &height);
29681
29682 row = (area == ON_MODE_LINE
29683 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29684 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29685
29686 /* Find the glyph under the mouse pointer. */
29687 if (row->mode_line_p && row->enabled_p)
29688 {
29689 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29690 end = glyph + row->used[TEXT_AREA];
29691
29692 for (x0 = original_x_pixel;
29693 glyph < end && x0 >= glyph->pixel_width;
29694 ++glyph)
29695 x0 -= glyph->pixel_width;
29696
29697 if (glyph >= end)
29698 glyph = NULL;
29699 }
29700 }
29701 else
29702 {
29703 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29704 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29705 returns them in row/column units! */
29706 string = marginal_area_string (w, area, &x, &y, &charpos,
29707 &object, &dx, &dy, &width, &height);
29708 }
29709
29710 help = Qnil;
29711
29712 #ifdef HAVE_WINDOW_SYSTEM
29713 if (IMAGEP (object))
29714 {
29715 Lisp_Object image_map, hotspot;
29716 if ((image_map = Fplist_get (XCDR (object), QCmap),
29717 !NILP (image_map))
29718 && (hotspot = find_hot_spot (image_map, dx, dy),
29719 CONSP (hotspot))
29720 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29721 {
29722 Lisp_Object plist;
29723
29724 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29725 If so, we could look for mouse-enter, mouse-leave
29726 properties in PLIST (and do something...). */
29727 hotspot = XCDR (hotspot);
29728 if (CONSP (hotspot)
29729 && (plist = XCAR (hotspot), CONSP (plist)))
29730 {
29731 pointer = Fplist_get (plist, Qpointer);
29732 if (NILP (pointer))
29733 pointer = Qhand;
29734 help = Fplist_get (plist, Qhelp_echo);
29735 if (!NILP (help))
29736 {
29737 help_echo_string = help;
29738 XSETWINDOW (help_echo_window, w);
29739 help_echo_object = w->contents;
29740 help_echo_pos = charpos;
29741 }
29742 }
29743 }
29744 if (NILP (pointer))
29745 pointer = Fplist_get (XCDR (object), QCpointer);
29746 }
29747 #endif /* HAVE_WINDOW_SYSTEM */
29748
29749 if (STRINGP (string))
29750 pos = make_number (charpos);
29751
29752 /* Set the help text and mouse pointer. If the mouse is on a part
29753 of the mode line without any text (e.g. past the right edge of
29754 the mode line text), use the default help text and pointer. */
29755 if (STRINGP (string) || area == ON_MODE_LINE)
29756 {
29757 /* Arrange to display the help by setting the global variables
29758 help_echo_string, help_echo_object, and help_echo_pos. */
29759 if (NILP (help))
29760 {
29761 if (STRINGP (string))
29762 help = Fget_text_property (pos, Qhelp_echo, string);
29763
29764 if (!NILP (help))
29765 {
29766 help_echo_string = help;
29767 XSETWINDOW (help_echo_window, w);
29768 help_echo_object = string;
29769 help_echo_pos = charpos;
29770 }
29771 else if (area == ON_MODE_LINE)
29772 {
29773 Lisp_Object default_help
29774 = buffer_local_value (Qmode_line_default_help_echo,
29775 w->contents);
29776
29777 if (STRINGP (default_help))
29778 {
29779 help_echo_string = default_help;
29780 XSETWINDOW (help_echo_window, w);
29781 help_echo_object = Qnil;
29782 help_echo_pos = -1;
29783 }
29784 }
29785 }
29786
29787 #ifdef HAVE_WINDOW_SYSTEM
29788 /* Change the mouse pointer according to what is under it. */
29789 if (FRAME_WINDOW_P (f))
29790 {
29791 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29792 || minibuf_level
29793 || NILP (Vresize_mini_windows));
29794
29795 dpyinfo = FRAME_DISPLAY_INFO (f);
29796 if (STRINGP (string))
29797 {
29798 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29799
29800 if (NILP (pointer))
29801 pointer = Fget_text_property (pos, Qpointer, string);
29802
29803 /* Change the mouse pointer according to what is under X/Y. */
29804 if (NILP (pointer)
29805 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29806 {
29807 Lisp_Object map;
29808 map = Fget_text_property (pos, Qlocal_map, string);
29809 if (!KEYMAPP (map))
29810 map = Fget_text_property (pos, Qkeymap, string);
29811 if (!KEYMAPP (map) && draggable)
29812 cursor = dpyinfo->vertical_scroll_bar_cursor;
29813 }
29814 }
29815 else if (draggable)
29816 /* Default mode-line pointer. */
29817 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29818 }
29819 #endif
29820 }
29821
29822 /* Change the mouse face according to what is under X/Y. */
29823 bool mouse_face_shown = false;
29824 if (STRINGP (string))
29825 {
29826 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29827 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29828 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29829 && glyph)
29830 {
29831 Lisp_Object b, e;
29832
29833 struct glyph * tmp_glyph;
29834
29835 int gpos;
29836 int gseq_length;
29837 int total_pixel_width;
29838 ptrdiff_t begpos, endpos, ignore;
29839
29840 int vpos, hpos;
29841
29842 b = Fprevious_single_property_change (make_number (charpos + 1),
29843 Qmouse_face, string, Qnil);
29844 if (NILP (b))
29845 begpos = 0;
29846 else
29847 begpos = XINT (b);
29848
29849 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29850 if (NILP (e))
29851 endpos = SCHARS (string);
29852 else
29853 endpos = XINT (e);
29854
29855 /* Calculate the glyph position GPOS of GLYPH in the
29856 displayed string, relative to the beginning of the
29857 highlighted part of the string.
29858
29859 Note: GPOS is different from CHARPOS. CHARPOS is the
29860 position of GLYPH in the internal string object. A mode
29861 line string format has structures which are converted to
29862 a flattened string by the Emacs Lisp interpreter. The
29863 internal string is an element of those structures. The
29864 displayed string is the flattened string. */
29865 tmp_glyph = row_start_glyph;
29866 while (tmp_glyph < glyph
29867 && (!(EQ (tmp_glyph->object, glyph->object)
29868 && begpos <= tmp_glyph->charpos
29869 && tmp_glyph->charpos < endpos)))
29870 tmp_glyph++;
29871 gpos = glyph - tmp_glyph;
29872
29873 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29874 the highlighted part of the displayed string to which
29875 GLYPH belongs. Note: GSEQ_LENGTH is different from
29876 SCHARS (STRING), because the latter returns the length of
29877 the internal string. */
29878 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29879 tmp_glyph > glyph
29880 && (!(EQ (tmp_glyph->object, glyph->object)
29881 && begpos <= tmp_glyph->charpos
29882 && tmp_glyph->charpos < endpos));
29883 tmp_glyph--)
29884 ;
29885 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29886
29887 /* Calculate the total pixel width of all the glyphs between
29888 the beginning of the highlighted area and GLYPH. */
29889 total_pixel_width = 0;
29890 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29891 total_pixel_width += tmp_glyph->pixel_width;
29892
29893 /* Pre calculation of re-rendering position. Note: X is in
29894 column units here, after the call to mode_line_string or
29895 marginal_area_string. */
29896 hpos = x - gpos;
29897 vpos = (area == ON_MODE_LINE
29898 ? (w->current_matrix)->nrows - 1
29899 : 0);
29900
29901 /* If GLYPH's position is included in the region that is
29902 already drawn in mouse face, we have nothing to do. */
29903 if ( EQ (window, hlinfo->mouse_face_window)
29904 && (!row->reversed_p
29905 ? (hlinfo->mouse_face_beg_col <= hpos
29906 && hpos < hlinfo->mouse_face_end_col)
29907 /* In R2L rows we swap BEG and END, see below. */
29908 : (hlinfo->mouse_face_end_col <= hpos
29909 && hpos < hlinfo->mouse_face_beg_col))
29910 && hlinfo->mouse_face_beg_row == vpos )
29911 return;
29912
29913 if (clear_mouse_face (hlinfo))
29914 cursor = No_Cursor;
29915
29916 if (!row->reversed_p)
29917 {
29918 hlinfo->mouse_face_beg_col = hpos;
29919 hlinfo->mouse_face_beg_x = original_x_pixel
29920 - (total_pixel_width + dx);
29921 hlinfo->mouse_face_end_col = hpos + gseq_length;
29922 hlinfo->mouse_face_end_x = 0;
29923 }
29924 else
29925 {
29926 /* In R2L rows, show_mouse_face expects BEG and END
29927 coordinates to be swapped. */
29928 hlinfo->mouse_face_end_col = hpos;
29929 hlinfo->mouse_face_end_x = original_x_pixel
29930 - (total_pixel_width + dx);
29931 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29932 hlinfo->mouse_face_beg_x = 0;
29933 }
29934
29935 hlinfo->mouse_face_beg_row = vpos;
29936 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29937 hlinfo->mouse_face_past_end = false;
29938 hlinfo->mouse_face_window = window;
29939
29940 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29941 charpos,
29942 0, &ignore,
29943 glyph->face_id,
29944 true);
29945 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29946 mouse_face_shown = true;
29947
29948 if (NILP (pointer))
29949 pointer = Qhand;
29950 }
29951 }
29952
29953 /* If mouse-face doesn't need to be shown, clear any existing
29954 mouse-face. */
29955 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
29956 clear_mouse_face (hlinfo);
29957
29958 #ifdef HAVE_WINDOW_SYSTEM
29959 if (FRAME_WINDOW_P (f))
29960 define_frame_cursor1 (f, cursor, pointer);
29961 #endif
29962 }
29963
29964
29965 /* EXPORT:
29966 Take proper action when the mouse has moved to position X, Y on
29967 frame F with regards to highlighting portions of display that have
29968 mouse-face properties. Also de-highlight portions of display where
29969 the mouse was before, set the mouse pointer shape as appropriate
29970 for the mouse coordinates, and activate help echo (tooltips).
29971 X and Y can be negative or out of range. */
29972
29973 void
29974 note_mouse_highlight (struct frame *f, int x, int y)
29975 {
29976 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29977 enum window_part part = ON_NOTHING;
29978 Lisp_Object window;
29979 struct window *w;
29980 Cursor cursor = No_Cursor;
29981 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29982 struct buffer *b;
29983
29984 /* When a menu is active, don't highlight because this looks odd. */
29985 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29986 if (popup_activated ())
29987 return;
29988 #endif
29989
29990 if (!f->glyphs_initialized_p
29991 || f->pointer_invisible)
29992 return;
29993
29994 hlinfo->mouse_face_mouse_x = x;
29995 hlinfo->mouse_face_mouse_y = y;
29996 hlinfo->mouse_face_mouse_frame = f;
29997
29998 if (hlinfo->mouse_face_defer)
29999 return;
30000
30001 /* Which window is that in? */
30002 window = window_from_coordinates (f, x, y, &part, true);
30003
30004 /* If displaying active text in another window, clear that. */
30005 if (! EQ (window, hlinfo->mouse_face_window)
30006 /* Also clear if we move out of text area in same window. */
30007 || (!NILP (hlinfo->mouse_face_window)
30008 && !NILP (window)
30009 && part != ON_TEXT
30010 && part != ON_MODE_LINE
30011 && part != ON_HEADER_LINE))
30012 clear_mouse_face (hlinfo);
30013
30014 /* Not on a window -> return. */
30015 if (!WINDOWP (window))
30016 return;
30017
30018 /* Reset help_echo_string. It will get recomputed below. */
30019 help_echo_string = Qnil;
30020
30021 /* Convert to window-relative pixel coordinates. */
30022 w = XWINDOW (window);
30023 frame_to_window_pixel_xy (w, &x, &y);
30024
30025 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
30026 /* Handle tool-bar window differently since it doesn't display a
30027 buffer. */
30028 if (EQ (window, f->tool_bar_window))
30029 {
30030 note_tool_bar_highlight (f, x, y);
30031 return;
30032 }
30033 #endif
30034
30035 /* Mouse is on the mode, header line or margin? */
30036 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
30037 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30038 {
30039 note_mode_line_or_margin_highlight (window, x, y, part);
30040
30041 #ifdef HAVE_WINDOW_SYSTEM
30042 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30043 {
30044 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30045 /* Show non-text cursor (Bug#16647). */
30046 goto set_cursor;
30047 }
30048 else
30049 #endif
30050 return;
30051 }
30052
30053 #ifdef HAVE_WINDOW_SYSTEM
30054 if (part == ON_VERTICAL_BORDER)
30055 {
30056 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30057 help_echo_string = build_string ("drag-mouse-1: resize");
30058 }
30059 else if (part == ON_RIGHT_DIVIDER)
30060 {
30061 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30062 help_echo_string = build_string ("drag-mouse-1: resize");
30063 }
30064 else if (part == ON_BOTTOM_DIVIDER)
30065 if (! WINDOW_BOTTOMMOST_P (w)
30066 || minibuf_level
30067 || NILP (Vresize_mini_windows))
30068 {
30069 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30070 help_echo_string = build_string ("drag-mouse-1: resize");
30071 }
30072 else
30073 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30074 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
30075 || part == ON_VERTICAL_SCROLL_BAR
30076 || part == ON_HORIZONTAL_SCROLL_BAR)
30077 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30078 else
30079 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30080 #endif
30081
30082 /* Are we in a window whose display is up to date?
30083 And verify the buffer's text has not changed. */
30084 b = XBUFFER (w->contents);
30085 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
30086 {
30087 int hpos, vpos, dx, dy, area = LAST_AREA;
30088 ptrdiff_t pos;
30089 struct glyph *glyph;
30090 Lisp_Object object;
30091 Lisp_Object mouse_face = Qnil, position;
30092 Lisp_Object *overlay_vec = NULL;
30093 ptrdiff_t i, noverlays;
30094 struct buffer *obuf;
30095 ptrdiff_t obegv, ozv;
30096 bool same_region;
30097
30098 /* Find the glyph under X/Y. */
30099 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
30100
30101 #ifdef HAVE_WINDOW_SYSTEM
30102 /* Look for :pointer property on image. */
30103 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
30104 {
30105 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
30106 if (img != NULL && IMAGEP (img->spec))
30107 {
30108 Lisp_Object image_map, hotspot;
30109 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
30110 !NILP (image_map))
30111 && (hotspot = find_hot_spot (image_map,
30112 glyph->slice.img.x + dx,
30113 glyph->slice.img.y + dy),
30114 CONSP (hotspot))
30115 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30116 {
30117 Lisp_Object plist;
30118
30119 /* Could check XCAR (hotspot) to see if we enter/leave
30120 this hot-spot.
30121 If so, we could look for mouse-enter, mouse-leave
30122 properties in PLIST (and do something...). */
30123 hotspot = XCDR (hotspot);
30124 if (CONSP (hotspot)
30125 && (plist = XCAR (hotspot), CONSP (plist)))
30126 {
30127 pointer = Fplist_get (plist, Qpointer);
30128 if (NILP (pointer))
30129 pointer = Qhand;
30130 help_echo_string = Fplist_get (plist, Qhelp_echo);
30131 if (!NILP (help_echo_string))
30132 {
30133 help_echo_window = window;
30134 help_echo_object = glyph->object;
30135 help_echo_pos = glyph->charpos;
30136 }
30137 }
30138 }
30139 if (NILP (pointer))
30140 pointer = Fplist_get (XCDR (img->spec), QCpointer);
30141 }
30142 }
30143 #endif /* HAVE_WINDOW_SYSTEM */
30144
30145 /* Clear mouse face if X/Y not over text. */
30146 if (glyph == NULL
30147 || area != TEXT_AREA
30148 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
30149 /* Glyph's OBJECT is nil for glyphs inserted by the
30150 display engine for its internal purposes, like truncation
30151 and continuation glyphs and blanks beyond the end of
30152 line's text on text terminals. If we are over such a
30153 glyph, we are not over any text. */
30154 || NILP (glyph->object)
30155 /* R2L rows have a stretch glyph at their front, which
30156 stands for no text, whereas L2R rows have no glyphs at
30157 all beyond the end of text. Treat such stretch glyphs
30158 like we do with NULL glyphs in L2R rows. */
30159 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
30160 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
30161 && glyph->type == STRETCH_GLYPH
30162 && glyph->avoid_cursor_p))
30163 {
30164 if (clear_mouse_face (hlinfo))
30165 cursor = No_Cursor;
30166 #ifdef HAVE_WINDOW_SYSTEM
30167 if (FRAME_WINDOW_P (f) && NILP (pointer))
30168 {
30169 if (area != TEXT_AREA)
30170 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30171 else
30172 pointer = Vvoid_text_area_pointer;
30173 }
30174 #endif
30175 goto set_cursor;
30176 }
30177
30178 pos = glyph->charpos;
30179 object = glyph->object;
30180 if (!STRINGP (object) && !BUFFERP (object))
30181 goto set_cursor;
30182
30183 /* If we get an out-of-range value, return now; avoid an error. */
30184 if (BUFFERP (object) && pos > BUF_Z (b))
30185 goto set_cursor;
30186
30187 /* Make the window's buffer temporarily current for
30188 overlays_at and compute_char_face. */
30189 obuf = current_buffer;
30190 current_buffer = b;
30191 obegv = BEGV;
30192 ozv = ZV;
30193 BEGV = BEG;
30194 ZV = Z;
30195
30196 /* Is this char mouse-active or does it have help-echo? */
30197 position = make_number (pos);
30198
30199 USE_SAFE_ALLOCA;
30200
30201 if (BUFFERP (object))
30202 {
30203 /* Put all the overlays we want in a vector in overlay_vec. */
30204 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
30205 /* Sort overlays into increasing priority order. */
30206 noverlays = sort_overlays (overlay_vec, noverlays, w);
30207 }
30208 else
30209 noverlays = 0;
30210
30211 if (NILP (Vmouse_highlight))
30212 {
30213 clear_mouse_face (hlinfo);
30214 goto check_help_echo;
30215 }
30216
30217 same_region = coords_in_mouse_face_p (w, hpos, vpos);
30218
30219 if (same_region)
30220 cursor = No_Cursor;
30221
30222 /* Check mouse-face highlighting. */
30223 if (! same_region
30224 /* If there exists an overlay with mouse-face overlapping
30225 the one we are currently highlighting, we have to
30226 check if we enter the overlapping overlay, and then
30227 highlight only that. */
30228 || (OVERLAYP (hlinfo->mouse_face_overlay)
30229 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
30230 {
30231 /* Find the highest priority overlay with a mouse-face. */
30232 Lisp_Object overlay = Qnil;
30233 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
30234 {
30235 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
30236 if (!NILP (mouse_face))
30237 overlay = overlay_vec[i];
30238 }
30239
30240 /* If we're highlighting the same overlay as before, there's
30241 no need to do that again. */
30242 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
30243 goto check_help_echo;
30244 hlinfo->mouse_face_overlay = overlay;
30245
30246 /* Clear the display of the old active region, if any. */
30247 if (clear_mouse_face (hlinfo))
30248 cursor = No_Cursor;
30249
30250 /* If no overlay applies, get a text property. */
30251 if (NILP (overlay))
30252 mouse_face = Fget_text_property (position, Qmouse_face, object);
30253
30254 /* Next, compute the bounds of the mouse highlighting and
30255 display it. */
30256 if (!NILP (mouse_face) && STRINGP (object))
30257 {
30258 /* The mouse-highlighting comes from a display string
30259 with a mouse-face. */
30260 Lisp_Object s, e;
30261 ptrdiff_t ignore;
30262
30263 s = Fprevious_single_property_change
30264 (make_number (pos + 1), Qmouse_face, object, Qnil);
30265 e = Fnext_single_property_change
30266 (position, Qmouse_face, object, Qnil);
30267 if (NILP (s))
30268 s = make_number (0);
30269 if (NILP (e))
30270 e = make_number (SCHARS (object));
30271 mouse_face_from_string_pos (w, hlinfo, object,
30272 XINT (s), XINT (e));
30273 hlinfo->mouse_face_past_end = false;
30274 hlinfo->mouse_face_window = window;
30275 hlinfo->mouse_face_face_id
30276 = face_at_string_position (w, object, pos, 0, &ignore,
30277 glyph->face_id, true);
30278 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30279 cursor = No_Cursor;
30280 }
30281 else
30282 {
30283 /* The mouse-highlighting, if any, comes from an overlay
30284 or text property in the buffer. */
30285 Lisp_Object buffer IF_LINT (= Qnil);
30286 Lisp_Object disp_string IF_LINT (= Qnil);
30287
30288 if (STRINGP (object))
30289 {
30290 /* If we are on a display string with no mouse-face,
30291 check if the text under it has one. */
30292 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
30293 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30294 pos = string_buffer_position (object, start);
30295 if (pos > 0)
30296 {
30297 mouse_face = get_char_property_and_overlay
30298 (make_number (pos), Qmouse_face, w->contents, &overlay);
30299 buffer = w->contents;
30300 disp_string = object;
30301 }
30302 }
30303 else
30304 {
30305 buffer = object;
30306 disp_string = Qnil;
30307 }
30308
30309 if (!NILP (mouse_face))
30310 {
30311 Lisp_Object before, after;
30312 Lisp_Object before_string, after_string;
30313 /* To correctly find the limits of mouse highlight
30314 in a bidi-reordered buffer, we must not use the
30315 optimization of limiting the search in
30316 previous-single-property-change and
30317 next-single-property-change, because
30318 rows_from_pos_range needs the real start and end
30319 positions to DTRT in this case. That's because
30320 the first row visible in a window does not
30321 necessarily display the character whose position
30322 is the smallest. */
30323 Lisp_Object lim1
30324 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30325 ? Fmarker_position (w->start)
30326 : Qnil;
30327 Lisp_Object lim2
30328 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30329 ? make_number (BUF_Z (XBUFFER (buffer))
30330 - w->window_end_pos)
30331 : Qnil;
30332
30333 if (NILP (overlay))
30334 {
30335 /* Handle the text property case. */
30336 before = Fprevious_single_property_change
30337 (make_number (pos + 1), Qmouse_face, buffer, lim1);
30338 after = Fnext_single_property_change
30339 (make_number (pos), Qmouse_face, buffer, lim2);
30340 before_string = after_string = Qnil;
30341 }
30342 else
30343 {
30344 /* Handle the overlay case. */
30345 before = Foverlay_start (overlay);
30346 after = Foverlay_end (overlay);
30347 before_string = Foverlay_get (overlay, Qbefore_string);
30348 after_string = Foverlay_get (overlay, Qafter_string);
30349
30350 if (!STRINGP (before_string)) before_string = Qnil;
30351 if (!STRINGP (after_string)) after_string = Qnil;
30352 }
30353
30354 mouse_face_from_buffer_pos (window, hlinfo, pos,
30355 NILP (before)
30356 ? 1
30357 : XFASTINT (before),
30358 NILP (after)
30359 ? BUF_Z (XBUFFER (buffer))
30360 : XFASTINT (after),
30361 before_string, after_string,
30362 disp_string);
30363 cursor = No_Cursor;
30364 }
30365 }
30366 }
30367
30368 check_help_echo:
30369
30370 /* Look for a `help-echo' property. */
30371 if (NILP (help_echo_string)) {
30372 Lisp_Object help, overlay;
30373
30374 /* Check overlays first. */
30375 help = overlay = Qnil;
30376 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30377 {
30378 overlay = overlay_vec[i];
30379 help = Foverlay_get (overlay, Qhelp_echo);
30380 }
30381
30382 if (!NILP (help))
30383 {
30384 help_echo_string = help;
30385 help_echo_window = window;
30386 help_echo_object = overlay;
30387 help_echo_pos = pos;
30388 }
30389 else
30390 {
30391 Lisp_Object obj = glyph->object;
30392 ptrdiff_t charpos = glyph->charpos;
30393
30394 /* Try text properties. */
30395 if (STRINGP (obj)
30396 && charpos >= 0
30397 && charpos < SCHARS (obj))
30398 {
30399 help = Fget_text_property (make_number (charpos),
30400 Qhelp_echo, obj);
30401 if (NILP (help))
30402 {
30403 /* If the string itself doesn't specify a help-echo,
30404 see if the buffer text ``under'' it does. */
30405 struct glyph_row *r
30406 = MATRIX_ROW (w->current_matrix, vpos);
30407 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30408 ptrdiff_t p = string_buffer_position (obj, start);
30409 if (p > 0)
30410 {
30411 help = Fget_char_property (make_number (p),
30412 Qhelp_echo, w->contents);
30413 if (!NILP (help))
30414 {
30415 charpos = p;
30416 obj = w->contents;
30417 }
30418 }
30419 }
30420 }
30421 else if (BUFFERP (obj)
30422 && charpos >= BEGV
30423 && charpos < ZV)
30424 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30425 obj);
30426
30427 if (!NILP (help))
30428 {
30429 help_echo_string = help;
30430 help_echo_window = window;
30431 help_echo_object = obj;
30432 help_echo_pos = charpos;
30433 }
30434 }
30435 }
30436
30437 #ifdef HAVE_WINDOW_SYSTEM
30438 /* Look for a `pointer' property. */
30439 if (FRAME_WINDOW_P (f) && NILP (pointer))
30440 {
30441 /* Check overlays first. */
30442 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30443 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30444
30445 if (NILP (pointer))
30446 {
30447 Lisp_Object obj = glyph->object;
30448 ptrdiff_t charpos = glyph->charpos;
30449
30450 /* Try text properties. */
30451 if (STRINGP (obj)
30452 && charpos >= 0
30453 && charpos < SCHARS (obj))
30454 {
30455 pointer = Fget_text_property (make_number (charpos),
30456 Qpointer, obj);
30457 if (NILP (pointer))
30458 {
30459 /* If the string itself doesn't specify a pointer,
30460 see if the buffer text ``under'' it does. */
30461 struct glyph_row *r
30462 = MATRIX_ROW (w->current_matrix, vpos);
30463 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30464 ptrdiff_t p = string_buffer_position (obj, start);
30465 if (p > 0)
30466 pointer = Fget_char_property (make_number (p),
30467 Qpointer, w->contents);
30468 }
30469 }
30470 else if (BUFFERP (obj)
30471 && charpos >= BEGV
30472 && charpos < ZV)
30473 pointer = Fget_text_property (make_number (charpos),
30474 Qpointer, obj);
30475 }
30476 }
30477 #endif /* HAVE_WINDOW_SYSTEM */
30478
30479 BEGV = obegv;
30480 ZV = ozv;
30481 current_buffer = obuf;
30482 SAFE_FREE ();
30483 }
30484
30485 set_cursor:
30486
30487 #ifdef HAVE_WINDOW_SYSTEM
30488 if (FRAME_WINDOW_P (f))
30489 define_frame_cursor1 (f, cursor, pointer);
30490 #else
30491 /* This is here to prevent a compiler error, about "label at end of
30492 compound statement". */
30493 return;
30494 #endif
30495 }
30496
30497
30498 /* EXPORT for RIF:
30499 Clear any mouse-face on window W. This function is part of the
30500 redisplay interface, and is called from try_window_id and similar
30501 functions to ensure the mouse-highlight is off. */
30502
30503 void
30504 x_clear_window_mouse_face (struct window *w)
30505 {
30506 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30507 Lisp_Object window;
30508
30509 block_input ();
30510 XSETWINDOW (window, w);
30511 if (EQ (window, hlinfo->mouse_face_window))
30512 clear_mouse_face (hlinfo);
30513 unblock_input ();
30514 }
30515
30516
30517 /* EXPORT:
30518 Just discard the mouse face information for frame F, if any.
30519 This is used when the size of F is changed. */
30520
30521 void
30522 cancel_mouse_face (struct frame *f)
30523 {
30524 Lisp_Object window;
30525 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30526
30527 window = hlinfo->mouse_face_window;
30528 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30529 reset_mouse_highlight (hlinfo);
30530 }
30531
30532
30533 \f
30534 /***********************************************************************
30535 Exposure Events
30536 ***********************************************************************/
30537
30538 #ifdef HAVE_WINDOW_SYSTEM
30539
30540 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30541 which intersects rectangle R. R is in window-relative coordinates. */
30542
30543 static void
30544 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30545 enum glyph_row_area area)
30546 {
30547 struct glyph *first = row->glyphs[area];
30548 struct glyph *end = row->glyphs[area] + row->used[area];
30549 struct glyph *last;
30550 int first_x, start_x, x;
30551
30552 if (area == TEXT_AREA && row->fill_line_p)
30553 /* If row extends face to end of line write the whole line. */
30554 draw_glyphs (w, 0, row, area,
30555 0, row->used[area],
30556 DRAW_NORMAL_TEXT, 0);
30557 else
30558 {
30559 /* Set START_X to the window-relative start position for drawing glyphs of
30560 AREA. The first glyph of the text area can be partially visible.
30561 The first glyphs of other areas cannot. */
30562 start_x = window_box_left_offset (w, area);
30563 x = start_x;
30564 if (area == TEXT_AREA)
30565 x += row->x;
30566
30567 /* Find the first glyph that must be redrawn. */
30568 while (first < end
30569 && x + first->pixel_width < r->x)
30570 {
30571 x += first->pixel_width;
30572 ++first;
30573 }
30574
30575 /* Find the last one. */
30576 last = first;
30577 first_x = x;
30578 /* Use a signed int intermediate value to avoid catastrophic
30579 failures due to comparison between signed and unsigned, when
30580 x is negative (can happen for wide images that are hscrolled). */
30581 int r_end = r->x + r->width;
30582 while (last < end && x < r_end)
30583 {
30584 x += last->pixel_width;
30585 ++last;
30586 }
30587
30588 /* Repaint. */
30589 if (last > first)
30590 draw_glyphs (w, first_x - start_x, row, area,
30591 first - row->glyphs[area], last - row->glyphs[area],
30592 DRAW_NORMAL_TEXT, 0);
30593 }
30594 }
30595
30596
30597 /* Redraw the parts of the glyph row ROW on window W intersecting
30598 rectangle R. R is in window-relative coordinates. Value is
30599 true if mouse-face was overwritten. */
30600
30601 static bool
30602 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30603 {
30604 eassert (row->enabled_p);
30605
30606 if (row->mode_line_p || w->pseudo_window_p)
30607 draw_glyphs (w, 0, row, TEXT_AREA,
30608 0, row->used[TEXT_AREA],
30609 DRAW_NORMAL_TEXT, 0);
30610 else
30611 {
30612 if (row->used[LEFT_MARGIN_AREA])
30613 expose_area (w, row, r, LEFT_MARGIN_AREA);
30614 if (row->used[TEXT_AREA])
30615 expose_area (w, row, r, TEXT_AREA);
30616 if (row->used[RIGHT_MARGIN_AREA])
30617 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30618 draw_row_fringe_bitmaps (w, row);
30619 }
30620
30621 return row->mouse_face_p;
30622 }
30623
30624
30625 /* Redraw those parts of glyphs rows during expose event handling that
30626 overlap other rows. Redrawing of an exposed line writes over parts
30627 of lines overlapping that exposed line; this function fixes that.
30628
30629 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30630 row in W's current matrix that is exposed and overlaps other rows.
30631 LAST_OVERLAPPING_ROW is the last such row. */
30632
30633 static void
30634 expose_overlaps (struct window *w,
30635 struct glyph_row *first_overlapping_row,
30636 struct glyph_row *last_overlapping_row,
30637 XRectangle *r)
30638 {
30639 struct glyph_row *row;
30640
30641 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30642 if (row->overlapping_p)
30643 {
30644 eassert (row->enabled_p && !row->mode_line_p);
30645
30646 row->clip = r;
30647 if (row->used[LEFT_MARGIN_AREA])
30648 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30649
30650 if (row->used[TEXT_AREA])
30651 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30652
30653 if (row->used[RIGHT_MARGIN_AREA])
30654 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30655 row->clip = NULL;
30656 }
30657 }
30658
30659
30660 /* Return true if W's cursor intersects rectangle R. */
30661
30662 static bool
30663 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30664 {
30665 XRectangle cr, result;
30666 struct glyph *cursor_glyph;
30667 struct glyph_row *row;
30668
30669 if (w->phys_cursor.vpos >= 0
30670 && w->phys_cursor.vpos < w->current_matrix->nrows
30671 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30672 row->enabled_p)
30673 && row->cursor_in_fringe_p)
30674 {
30675 /* Cursor is in the fringe. */
30676 cr.x = window_box_right_offset (w,
30677 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30678 ? RIGHT_MARGIN_AREA
30679 : TEXT_AREA));
30680 cr.y = row->y;
30681 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30682 cr.height = row->height;
30683 return x_intersect_rectangles (&cr, r, &result);
30684 }
30685
30686 cursor_glyph = get_phys_cursor_glyph (w);
30687 if (cursor_glyph)
30688 {
30689 /* r is relative to W's box, but w->phys_cursor.x is relative
30690 to left edge of W's TEXT area. Adjust it. */
30691 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30692 cr.y = w->phys_cursor.y;
30693 cr.width = cursor_glyph->pixel_width;
30694 cr.height = w->phys_cursor_height;
30695 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30696 I assume the effect is the same -- and this is portable. */
30697 return x_intersect_rectangles (&cr, r, &result);
30698 }
30699 /* If we don't understand the format, pretend we're not in the hot-spot. */
30700 return false;
30701 }
30702
30703
30704 /* EXPORT:
30705 Draw a vertical window border to the right of window W if W doesn't
30706 have vertical scroll bars. */
30707
30708 void
30709 x_draw_vertical_border (struct window *w)
30710 {
30711 struct frame *f = XFRAME (WINDOW_FRAME (w));
30712
30713 /* We could do better, if we knew what type of scroll-bar the adjacent
30714 windows (on either side) have... But we don't :-(
30715 However, I think this works ok. ++KFS 2003-04-25 */
30716
30717 /* Redraw borders between horizontally adjacent windows. Don't
30718 do it for frames with vertical scroll bars because either the
30719 right scroll bar of a window, or the left scroll bar of its
30720 neighbor will suffice as a border. */
30721 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30722 return;
30723
30724 /* Note: It is necessary to redraw both the left and the right
30725 borders, for when only this single window W is being
30726 redisplayed. */
30727 if (!WINDOW_RIGHTMOST_P (w)
30728 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30729 {
30730 int x0, x1, y0, y1;
30731
30732 window_box_edges (w, &x0, &y0, &x1, &y1);
30733 y1 -= 1;
30734
30735 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30736 x1 -= 1;
30737
30738 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30739 }
30740
30741 if (!WINDOW_LEFTMOST_P (w)
30742 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30743 {
30744 int x0, x1, y0, y1;
30745
30746 window_box_edges (w, &x0, &y0, &x1, &y1);
30747 y1 -= 1;
30748
30749 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30750 x0 -= 1;
30751
30752 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30753 }
30754 }
30755
30756
30757 /* Draw window dividers for window W. */
30758
30759 void
30760 x_draw_right_divider (struct window *w)
30761 {
30762 struct frame *f = WINDOW_XFRAME (w);
30763
30764 if (w->mini || w->pseudo_window_p)
30765 return;
30766 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30767 {
30768 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30769 int x1 = WINDOW_RIGHT_EDGE_X (w);
30770 int y0 = WINDOW_TOP_EDGE_Y (w);
30771 /* The bottom divider prevails. */
30772 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30773
30774 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30775 }
30776 }
30777
30778 static void
30779 x_draw_bottom_divider (struct window *w)
30780 {
30781 struct frame *f = XFRAME (WINDOW_FRAME (w));
30782
30783 if (w->mini || w->pseudo_window_p)
30784 return;
30785 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30786 {
30787 int x0 = WINDOW_LEFT_EDGE_X (w);
30788 int x1 = WINDOW_RIGHT_EDGE_X (w);
30789 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30790 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30791
30792 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30793 }
30794 }
30795
30796 /* Redraw the part of window W intersection rectangle FR. Pixel
30797 coordinates in FR are frame-relative. Call this function with
30798 input blocked. Value is true if the exposure overwrites
30799 mouse-face. */
30800
30801 static bool
30802 expose_window (struct window *w, XRectangle *fr)
30803 {
30804 struct frame *f = XFRAME (w->frame);
30805 XRectangle wr, r;
30806 bool mouse_face_overwritten_p = false;
30807
30808 /* If window is not yet fully initialized, do nothing. This can
30809 happen when toolkit scroll bars are used and a window is split.
30810 Reconfiguring the scroll bar will generate an expose for a newly
30811 created window. */
30812 if (w->current_matrix == NULL)
30813 return false;
30814
30815 /* When we're currently updating the window, display and current
30816 matrix usually don't agree. Arrange for a thorough display
30817 later. */
30818 if (w->must_be_updated_p)
30819 {
30820 SET_FRAME_GARBAGED (f);
30821 return false;
30822 }
30823
30824 /* Frame-relative pixel rectangle of W. */
30825 wr.x = WINDOW_LEFT_EDGE_X (w);
30826 wr.y = WINDOW_TOP_EDGE_Y (w);
30827 wr.width = WINDOW_PIXEL_WIDTH (w);
30828 wr.height = WINDOW_PIXEL_HEIGHT (w);
30829
30830 if (x_intersect_rectangles (fr, &wr, &r))
30831 {
30832 int yb = window_text_bottom_y (w);
30833 struct glyph_row *row;
30834 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30835
30836 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30837 r.x, r.y, r.width, r.height));
30838
30839 /* Convert to window coordinates. */
30840 r.x -= WINDOW_LEFT_EDGE_X (w);
30841 r.y -= WINDOW_TOP_EDGE_Y (w);
30842
30843 /* Turn off the cursor. */
30844 bool cursor_cleared_p = (!w->pseudo_window_p
30845 && phys_cursor_in_rect_p (w, &r));
30846 if (cursor_cleared_p)
30847 x_clear_cursor (w);
30848
30849 /* If the row containing the cursor extends face to end of line,
30850 then expose_area might overwrite the cursor outside the
30851 rectangle and thus notice_overwritten_cursor might clear
30852 w->phys_cursor_on_p. We remember the original value and
30853 check later if it is changed. */
30854 bool phys_cursor_on_p = w->phys_cursor_on_p;
30855
30856 /* Use a signed int intermediate value to avoid catastrophic
30857 failures due to comparison between signed and unsigned, when
30858 y0 or y1 is negative (can happen for tall images). */
30859 int r_bottom = r.y + r.height;
30860
30861 /* Update lines intersecting rectangle R. */
30862 first_overlapping_row = last_overlapping_row = NULL;
30863 for (row = w->current_matrix->rows;
30864 row->enabled_p;
30865 ++row)
30866 {
30867 int y0 = row->y;
30868 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30869
30870 if ((y0 >= r.y && y0 < r_bottom)
30871 || (y1 > r.y && y1 < r_bottom)
30872 || (r.y >= y0 && r.y < y1)
30873 || (r_bottom > y0 && r_bottom < y1))
30874 {
30875 /* A header line may be overlapping, but there is no need
30876 to fix overlapping areas for them. KFS 2005-02-12 */
30877 if (row->overlapping_p && !row->mode_line_p)
30878 {
30879 if (first_overlapping_row == NULL)
30880 first_overlapping_row = row;
30881 last_overlapping_row = row;
30882 }
30883
30884 row->clip = fr;
30885 if (expose_line (w, row, &r))
30886 mouse_face_overwritten_p = true;
30887 row->clip = NULL;
30888 }
30889 else if (row->overlapping_p)
30890 {
30891 /* We must redraw a row overlapping the exposed area. */
30892 if (y0 < r.y
30893 ? y0 + row->phys_height > r.y
30894 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30895 {
30896 if (first_overlapping_row == NULL)
30897 first_overlapping_row = row;
30898 last_overlapping_row = row;
30899 }
30900 }
30901
30902 if (y1 >= yb)
30903 break;
30904 }
30905
30906 /* Display the mode line if there is one. */
30907 if (WINDOW_WANTS_MODELINE_P (w)
30908 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30909 row->enabled_p)
30910 && row->y < r_bottom)
30911 {
30912 if (expose_line (w, row, &r))
30913 mouse_face_overwritten_p = true;
30914 }
30915
30916 if (!w->pseudo_window_p)
30917 {
30918 /* Fix the display of overlapping rows. */
30919 if (first_overlapping_row)
30920 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30921 fr);
30922
30923 /* Draw border between windows. */
30924 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30925 x_draw_right_divider (w);
30926 else
30927 x_draw_vertical_border (w);
30928
30929 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30930 x_draw_bottom_divider (w);
30931
30932 /* Turn the cursor on again. */
30933 if (cursor_cleared_p
30934 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30935 update_window_cursor (w, true);
30936 }
30937 }
30938
30939 return mouse_face_overwritten_p;
30940 }
30941
30942
30943
30944 /* Redraw (parts) of all windows in the window tree rooted at W that
30945 intersect R. R contains frame pixel coordinates. Value is
30946 true if the exposure overwrites mouse-face. */
30947
30948 static bool
30949 expose_window_tree (struct window *w, XRectangle *r)
30950 {
30951 struct frame *f = XFRAME (w->frame);
30952 bool mouse_face_overwritten_p = false;
30953
30954 while (w && !FRAME_GARBAGED_P (f))
30955 {
30956 mouse_face_overwritten_p
30957 |= (WINDOWP (w->contents)
30958 ? expose_window_tree (XWINDOW (w->contents), r)
30959 : expose_window (w, r));
30960
30961 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30962 }
30963
30964 return mouse_face_overwritten_p;
30965 }
30966
30967
30968 /* EXPORT:
30969 Redisplay an exposed area of frame F. X and Y are the upper-left
30970 corner of the exposed rectangle. W and H are width and height of
30971 the exposed area. All are pixel values. W or H zero means redraw
30972 the entire frame. */
30973
30974 void
30975 expose_frame (struct frame *f, int x, int y, int w, int h)
30976 {
30977 XRectangle r;
30978 bool mouse_face_overwritten_p = false;
30979
30980 TRACE ((stderr, "expose_frame "));
30981
30982 /* No need to redraw if frame will be redrawn soon. */
30983 if (FRAME_GARBAGED_P (f))
30984 {
30985 TRACE ((stderr, " garbaged\n"));
30986 return;
30987 }
30988
30989 /* If basic faces haven't been realized yet, there is no point in
30990 trying to redraw anything. This can happen when we get an expose
30991 event while Emacs is starting, e.g. by moving another window. */
30992 if (FRAME_FACE_CACHE (f) == NULL
30993 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30994 {
30995 TRACE ((stderr, " no faces\n"));
30996 return;
30997 }
30998
30999 if (w == 0 || h == 0)
31000 {
31001 r.x = r.y = 0;
31002 r.width = FRAME_TEXT_WIDTH (f);
31003 r.height = FRAME_TEXT_HEIGHT (f);
31004 }
31005 else
31006 {
31007 r.x = x;
31008 r.y = y;
31009 r.width = w;
31010 r.height = h;
31011 }
31012
31013 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
31014 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
31015
31016 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
31017 if (WINDOWP (f->tool_bar_window))
31018 mouse_face_overwritten_p
31019 |= expose_window (XWINDOW (f->tool_bar_window), &r);
31020 #endif
31021
31022 #ifdef HAVE_X_WINDOWS
31023 #ifndef MSDOS
31024 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
31025 if (WINDOWP (f->menu_bar_window))
31026 mouse_face_overwritten_p
31027 |= expose_window (XWINDOW (f->menu_bar_window), &r);
31028 #endif /* not USE_X_TOOLKIT and not USE_GTK */
31029 #endif
31030 #endif
31031
31032 /* Some window managers support a focus-follows-mouse style with
31033 delayed raising of frames. Imagine a partially obscured frame,
31034 and moving the mouse into partially obscured mouse-face on that
31035 frame. The visible part of the mouse-face will be highlighted,
31036 then the WM raises the obscured frame. With at least one WM, KDE
31037 2.1, Emacs is not getting any event for the raising of the frame
31038 (even tried with SubstructureRedirectMask), only Expose events.
31039 These expose events will draw text normally, i.e. not
31040 highlighted. Which means we must redo the highlight here.
31041 Subsume it under ``we love X''. --gerd 2001-08-15 */
31042 /* Included in Windows version because Windows most likely does not
31043 do the right thing if any third party tool offers
31044 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
31045 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
31046 {
31047 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31048 if (f == hlinfo->mouse_face_mouse_frame)
31049 {
31050 int mouse_x = hlinfo->mouse_face_mouse_x;
31051 int mouse_y = hlinfo->mouse_face_mouse_y;
31052 clear_mouse_face (hlinfo);
31053 note_mouse_highlight (f, mouse_x, mouse_y);
31054 }
31055 }
31056 }
31057
31058
31059 /* EXPORT:
31060 Determine the intersection of two rectangles R1 and R2. Return
31061 the intersection in *RESULT. Value is true if RESULT is not
31062 empty. */
31063
31064 bool
31065 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
31066 {
31067 XRectangle *left, *right;
31068 XRectangle *upper, *lower;
31069 bool intersection_p = false;
31070
31071 /* Rearrange so that R1 is the left-most rectangle. */
31072 if (r1->x < r2->x)
31073 left = r1, right = r2;
31074 else
31075 left = r2, right = r1;
31076
31077 /* X0 of the intersection is right.x0, if this is inside R1,
31078 otherwise there is no intersection. */
31079 if (right->x <= left->x + left->width)
31080 {
31081 result->x = right->x;
31082
31083 /* The right end of the intersection is the minimum of
31084 the right ends of left and right. */
31085 result->width = (min (left->x + left->width, right->x + right->width)
31086 - result->x);
31087
31088 /* Same game for Y. */
31089 if (r1->y < r2->y)
31090 upper = r1, lower = r2;
31091 else
31092 upper = r2, lower = r1;
31093
31094 /* The upper end of the intersection is lower.y0, if this is inside
31095 of upper. Otherwise, there is no intersection. */
31096 if (lower->y <= upper->y + upper->height)
31097 {
31098 result->y = lower->y;
31099
31100 /* The lower end of the intersection is the minimum of the lower
31101 ends of upper and lower. */
31102 result->height = (min (lower->y + lower->height,
31103 upper->y + upper->height)
31104 - result->y);
31105 intersection_p = true;
31106 }
31107 }
31108
31109 return intersection_p;
31110 }
31111
31112 #endif /* HAVE_WINDOW_SYSTEM */
31113
31114 \f
31115 /***********************************************************************
31116 Initialization
31117 ***********************************************************************/
31118
31119 void
31120 syms_of_xdisp (void)
31121 {
31122 Vwith_echo_area_save_vector = Qnil;
31123 staticpro (&Vwith_echo_area_save_vector);
31124
31125 Vmessage_stack = Qnil;
31126 staticpro (&Vmessage_stack);
31127
31128 /* Non-nil means don't actually do any redisplay. */
31129 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
31130
31131 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
31132
31133 DEFVAR_BOOL("inhibit-message", inhibit_message,
31134 doc: /* Non-nil means calls to `message' are not displayed.
31135 They are still logged to the *Messages* buffer. */);
31136 inhibit_message = 0;
31137
31138 message_dolog_marker1 = Fmake_marker ();
31139 staticpro (&message_dolog_marker1);
31140 message_dolog_marker2 = Fmake_marker ();
31141 staticpro (&message_dolog_marker2);
31142 message_dolog_marker3 = Fmake_marker ();
31143 staticpro (&message_dolog_marker3);
31144
31145 #ifdef GLYPH_DEBUG
31146 defsubr (&Sdump_frame_glyph_matrix);
31147 defsubr (&Sdump_glyph_matrix);
31148 defsubr (&Sdump_glyph_row);
31149 defsubr (&Sdump_tool_bar_row);
31150 defsubr (&Strace_redisplay);
31151 defsubr (&Strace_to_stderr);
31152 #endif
31153 #ifdef HAVE_WINDOW_SYSTEM
31154 defsubr (&Stool_bar_height);
31155 defsubr (&Slookup_image_map);
31156 #endif
31157 defsubr (&Sline_pixel_height);
31158 defsubr (&Sformat_mode_line);
31159 defsubr (&Sinvisible_p);
31160 defsubr (&Scurrent_bidi_paragraph_direction);
31161 defsubr (&Swindow_text_pixel_size);
31162 defsubr (&Smove_point_visually);
31163 defsubr (&Sbidi_find_overridden_directionality);
31164
31165 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
31166 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
31167 DEFSYM (Qoverriding_local_map, "overriding-local-map");
31168 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
31169 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
31170 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
31171 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
31172 DEFSYM (Qeval, "eval");
31173 DEFSYM (QCdata, ":data");
31174
31175 /* Names of text properties relevant for redisplay. */
31176 DEFSYM (Qdisplay, "display");
31177 DEFSYM (Qspace_width, "space-width");
31178 DEFSYM (Qraise, "raise");
31179 DEFSYM (Qslice, "slice");
31180 DEFSYM (Qspace, "space");
31181 DEFSYM (Qmargin, "margin");
31182 DEFSYM (Qpointer, "pointer");
31183 DEFSYM (Qleft_margin, "left-margin");
31184 DEFSYM (Qright_margin, "right-margin");
31185 DEFSYM (Qcenter, "center");
31186 DEFSYM (Qline_height, "line-height");
31187 DEFSYM (QCalign_to, ":align-to");
31188 DEFSYM (QCrelative_width, ":relative-width");
31189 DEFSYM (QCrelative_height, ":relative-height");
31190 DEFSYM (QCeval, ":eval");
31191 DEFSYM (QCpropertize, ":propertize");
31192 DEFSYM (QCfile, ":file");
31193 DEFSYM (Qfontified, "fontified");
31194 DEFSYM (Qfontification_functions, "fontification-functions");
31195
31196 /* Name of the face used to highlight trailing whitespace. */
31197 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
31198
31199 /* Name and number of the face used to highlight escape glyphs. */
31200 DEFSYM (Qescape_glyph, "escape-glyph");
31201
31202 /* Name and number of the face used to highlight non-breaking spaces. */
31203 DEFSYM (Qnobreak_space, "nobreak-space");
31204
31205 /* The symbol 'image' which is the car of the lists used to represent
31206 images in Lisp. Also a tool bar style. */
31207 DEFSYM (Qimage, "image");
31208
31209 /* Tool bar styles. */
31210 DEFSYM (Qtext, "text");
31211 DEFSYM (Qboth, "both");
31212 DEFSYM (Qboth_horiz, "both-horiz");
31213 DEFSYM (Qtext_image_horiz, "text-image-horiz");
31214
31215 /* The image map types. */
31216 DEFSYM (QCmap, ":map");
31217 DEFSYM (QCpointer, ":pointer");
31218 DEFSYM (Qrect, "rect");
31219 DEFSYM (Qcircle, "circle");
31220 DEFSYM (Qpoly, "poly");
31221
31222 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
31223
31224 DEFSYM (Qgrow_only, "grow-only");
31225 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
31226 DEFSYM (Qposition, "position");
31227 DEFSYM (Qbuffer_position, "buffer-position");
31228 DEFSYM (Qobject, "object");
31229
31230 /* Cursor shapes. */
31231 DEFSYM (Qbar, "bar");
31232 DEFSYM (Qhbar, "hbar");
31233 DEFSYM (Qbox, "box");
31234 DEFSYM (Qhollow, "hollow");
31235
31236 /* Pointer shapes. */
31237 DEFSYM (Qhand, "hand");
31238 DEFSYM (Qarrow, "arrow");
31239 /* also Qtext */
31240
31241 DEFSYM (Qdragging, "dragging");
31242
31243 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
31244
31245 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
31246 staticpro (&list_of_error);
31247
31248 /* Values of those variables at last redisplay are stored as
31249 properties on 'overlay-arrow-position' symbol. However, if
31250 Voverlay_arrow_position is a marker, last-arrow-position is its
31251 numerical position. */
31252 DEFSYM (Qlast_arrow_position, "last-arrow-position");
31253 DEFSYM (Qlast_arrow_string, "last-arrow-string");
31254
31255 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
31256 properties on a symbol in overlay-arrow-variable-list. */
31257 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
31258 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
31259
31260 echo_buffer[0] = echo_buffer[1] = Qnil;
31261 staticpro (&echo_buffer[0]);
31262 staticpro (&echo_buffer[1]);
31263
31264 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
31265 staticpro (&echo_area_buffer[0]);
31266 staticpro (&echo_area_buffer[1]);
31267
31268 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
31269 staticpro (&Vmessages_buffer_name);
31270
31271 mode_line_proptrans_alist = Qnil;
31272 staticpro (&mode_line_proptrans_alist);
31273 mode_line_string_list = Qnil;
31274 staticpro (&mode_line_string_list);
31275 mode_line_string_face = Qnil;
31276 staticpro (&mode_line_string_face);
31277 mode_line_string_face_prop = Qnil;
31278 staticpro (&mode_line_string_face_prop);
31279 Vmode_line_unwind_vector = Qnil;
31280 staticpro (&Vmode_line_unwind_vector);
31281
31282 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
31283
31284 help_echo_string = Qnil;
31285 staticpro (&help_echo_string);
31286 help_echo_object = Qnil;
31287 staticpro (&help_echo_object);
31288 help_echo_window = Qnil;
31289 staticpro (&help_echo_window);
31290 previous_help_echo_string = Qnil;
31291 staticpro (&previous_help_echo_string);
31292 help_echo_pos = -1;
31293
31294 DEFSYM (Qright_to_left, "right-to-left");
31295 DEFSYM (Qleft_to_right, "left-to-right");
31296 defsubr (&Sbidi_resolved_levels);
31297
31298 #ifdef HAVE_WINDOW_SYSTEM
31299 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
31300 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
31301 For example, if a block cursor is over a tab, it will be drawn as
31302 wide as that tab on the display. */);
31303 x_stretch_cursor_p = 0;
31304 #endif
31305
31306 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
31307 doc: /* Non-nil means highlight trailing whitespace.
31308 The face used for trailing whitespace is `trailing-whitespace'. */);
31309 Vshow_trailing_whitespace = Qnil;
31310
31311 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
31312 doc: /* Control highlighting of non-ASCII space and hyphen chars.
31313 If the value is t, Emacs highlights non-ASCII chars which have the
31314 same appearance as an ASCII space or hyphen, using the `nobreak-space'
31315 or `escape-glyph' face respectively.
31316
31317 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
31318 U+2011 (non-breaking hyphen) are affected.
31319
31320 Any other non-nil value means to display these characters as a escape
31321 glyph followed by an ordinary space or hyphen.
31322
31323 A value of nil means no special handling of these characters. */);
31324 Vnobreak_char_display = Qt;
31325
31326 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
31327 doc: /* The pointer shape to show in void text areas.
31328 A value of nil means to show the text pointer. Other options are
31329 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
31330 `hourglass'. */);
31331 Vvoid_text_area_pointer = Qarrow;
31332
31333 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
31334 doc: /* Non-nil means don't actually do any redisplay.
31335 This is used for internal purposes. */);
31336 Vinhibit_redisplay = Qnil;
31337
31338 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
31339 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
31340 Vglobal_mode_string = Qnil;
31341
31342 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
31343 doc: /* Marker for where to display an arrow on top of the buffer text.
31344 This must be the beginning of a line in order to work.
31345 See also `overlay-arrow-string'. */);
31346 Voverlay_arrow_position = Qnil;
31347
31348 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
31349 doc: /* String to display as an arrow in non-window frames.
31350 See also `overlay-arrow-position'. */);
31351 Voverlay_arrow_string = build_pure_c_string ("=>");
31352
31353 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31354 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31355 The symbols on this list are examined during redisplay to determine
31356 where to display overlay arrows. */);
31357 Voverlay_arrow_variable_list
31358 = list1 (intern_c_string ("overlay-arrow-position"));
31359
31360 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31361 doc: /* The number of lines to try scrolling a window by when point moves out.
31362 If that fails to bring point back on frame, point is centered instead.
31363 If this is zero, point is always centered after it moves off frame.
31364 If you want scrolling to always be a line at a time, you should set
31365 `scroll-conservatively' to a large value rather than set this to 1. */);
31366
31367 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31368 doc: /* Scroll up to this many lines, to bring point back on screen.
31369 If point moves off-screen, redisplay will scroll by up to
31370 `scroll-conservatively' lines in order to bring point just barely
31371 onto the screen again. If that cannot be done, then redisplay
31372 recenters point as usual.
31373
31374 If the value is greater than 100, redisplay will never recenter point,
31375 but will always scroll just enough text to bring point into view, even
31376 if you move far away.
31377
31378 A value of zero means always recenter point if it moves off screen. */);
31379 scroll_conservatively = 0;
31380
31381 DEFVAR_INT ("scroll-margin", scroll_margin,
31382 doc: /* Number of lines of margin at the top and bottom of a window.
31383 Recenter the window whenever point gets within this many lines
31384 of the top or bottom of the window. */);
31385 scroll_margin = 0;
31386
31387 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31388 doc: /* Pixels per inch value for non-window system displays.
31389 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31390 Vdisplay_pixels_per_inch = make_float (72.0);
31391
31392 #ifdef GLYPH_DEBUG
31393 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31394 #endif
31395
31396 DEFVAR_LISP ("truncate-partial-width-windows",
31397 Vtruncate_partial_width_windows,
31398 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31399 For an integer value, truncate lines in each window narrower than the
31400 full frame width, provided the window width is less than that integer;
31401 otherwise, respect the value of `truncate-lines'.
31402
31403 For any other non-nil value, truncate lines in all windows that do
31404 not span the full frame width.
31405
31406 A value of nil means to respect the value of `truncate-lines'.
31407
31408 If `word-wrap' is enabled, you might want to reduce this. */);
31409 Vtruncate_partial_width_windows = make_number (50);
31410
31411 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31412 doc: /* Maximum buffer size for which line number should be displayed.
31413 If the buffer is bigger than this, the line number does not appear
31414 in the mode line. A value of nil means no limit. */);
31415 Vline_number_display_limit = Qnil;
31416
31417 DEFVAR_INT ("line-number-display-limit-width",
31418 line_number_display_limit_width,
31419 doc: /* Maximum line width (in characters) for line number display.
31420 If the average length of the lines near point is bigger than this, then the
31421 line number may be omitted from the mode line. */);
31422 line_number_display_limit_width = 200;
31423
31424 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31425 doc: /* Non-nil means highlight region even in nonselected windows. */);
31426 highlight_nonselected_windows = false;
31427
31428 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31429 doc: /* Non-nil if more than one frame is visible on this display.
31430 Minibuffer-only frames don't count, but iconified frames do.
31431 This variable is not guaranteed to be accurate except while processing
31432 `frame-title-format' and `icon-title-format'. */);
31433
31434 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31435 doc: /* Template for displaying the title bar of visible frames.
31436 (Assuming the window manager supports this feature.)
31437
31438 This variable has the same structure as `mode-line-format', except that
31439 the %c and %l constructs are ignored. It is used only on frames for
31440 which no explicit name has been set (see `modify-frame-parameters'). */);
31441
31442 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31443 doc: /* Template for displaying the title bar of an iconified frame.
31444 (Assuming the window manager supports this feature.)
31445 This variable has the same structure as `mode-line-format' (which see),
31446 and is used only on frames for which no explicit name has been set
31447 (see `modify-frame-parameters'). */);
31448 Vicon_title_format
31449 = Vframe_title_format
31450 = listn (CONSTYPE_PURE, 3,
31451 intern_c_string ("multiple-frames"),
31452 build_pure_c_string ("%b"),
31453 listn (CONSTYPE_PURE, 4,
31454 empty_unibyte_string,
31455 intern_c_string ("invocation-name"),
31456 build_pure_c_string ("@"),
31457 intern_c_string ("system-name")));
31458
31459 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31460 doc: /* Maximum number of lines to keep in the message log buffer.
31461 If nil, disable message logging. If t, log messages but don't truncate
31462 the buffer when it becomes large. */);
31463 Vmessage_log_max = make_number (1000);
31464
31465 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
31466 doc: /* Functions called during redisplay, if window sizes have changed.
31467 The value should be a list of functions that take one argument.
31468 During the first part of redisplay, for each frame, if any of its windows
31469 have changed size since the last redisplay, or have been split or deleted,
31470 all the functions in the list are called, with the frame as argument.
31471 If redisplay decides to resize the minibuffer window, it calls these
31472 functions on behalf of that as well. */);
31473 Vwindow_size_change_functions = Qnil;
31474
31475 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31476 doc: /* List of functions to call before redisplaying a window with scrolling.
31477 Each function is called with two arguments, the window and its new
31478 display-start position.
31479 These functions are called whenever the `window-start' marker is modified,
31480 either to point into another buffer (e.g. via `set-window-buffer') or another
31481 place in the same buffer.
31482 Note that the value of `window-end' is not valid when these functions are
31483 called.
31484
31485 Warning: Do not use this feature to alter the way the window
31486 is scrolled. It is not designed for that, and such use probably won't
31487 work. */);
31488 Vwindow_scroll_functions = Qnil;
31489
31490 DEFVAR_LISP ("window-text-change-functions",
31491 Vwindow_text_change_functions,
31492 doc: /* Functions to call in redisplay when text in the window might change. */);
31493 Vwindow_text_change_functions = Qnil;
31494
31495 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31496 doc: /* Functions called when redisplay of a window reaches the end trigger.
31497 Each function is called with two arguments, the window and the end trigger value.
31498 See `set-window-redisplay-end-trigger'. */);
31499 Vredisplay_end_trigger_functions = Qnil;
31500
31501 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31502 doc: /* Non-nil means autoselect window with mouse pointer.
31503 If nil, do not autoselect windows.
31504 A positive number means delay autoselection by that many seconds: a
31505 window is autoselected only after the mouse has remained in that
31506 window for the duration of the delay.
31507 A negative number has a similar effect, but causes windows to be
31508 autoselected only after the mouse has stopped moving. (Because of
31509 the way Emacs compares mouse events, you will occasionally wait twice
31510 that time before the window gets selected.)
31511 Any other value means to autoselect window instantaneously when the
31512 mouse pointer enters it.
31513
31514 Autoselection selects the minibuffer only if it is active, and never
31515 unselects the minibuffer if it is active.
31516
31517 When customizing this variable make sure that the actual value of
31518 `focus-follows-mouse' matches the behavior of your window manager. */);
31519 Vmouse_autoselect_window = Qnil;
31520
31521 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31522 doc: /* Non-nil means automatically resize tool-bars.
31523 This dynamically changes the tool-bar's height to the minimum height
31524 that is needed to make all tool-bar items visible.
31525 If value is `grow-only', the tool-bar's height is only increased
31526 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31527 Vauto_resize_tool_bars = Qt;
31528
31529 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31530 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31531 auto_raise_tool_bar_buttons_p = true;
31532
31533 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31534 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31535 make_cursor_line_fully_visible_p = true;
31536
31537 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31538 doc: /* Border below tool-bar in pixels.
31539 If an integer, use it as the height of the border.
31540 If it is one of `internal-border-width' or `border-width', use the
31541 value of the corresponding frame parameter.
31542 Otherwise, no border is added below the tool-bar. */);
31543 Vtool_bar_border = Qinternal_border_width;
31544
31545 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31546 doc: /* Margin around tool-bar buttons in pixels.
31547 If an integer, use that for both horizontal and vertical margins.
31548 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31549 HORZ specifying the horizontal margin, and VERT specifying the
31550 vertical margin. */);
31551 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31552
31553 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31554 doc: /* Relief thickness of tool-bar buttons. */);
31555 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31556
31557 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31558 doc: /* Tool bar style to use.
31559 It can be one of
31560 image - show images only
31561 text - show text only
31562 both - show both, text below image
31563 both-horiz - show text to the right of the image
31564 text-image-horiz - show text to the left of the image
31565 any other - use system default or image if no system default.
31566
31567 This variable only affects the GTK+ toolkit version of Emacs. */);
31568 Vtool_bar_style = Qnil;
31569
31570 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31571 doc: /* Maximum number of characters a label can have to be shown.
31572 The tool bar style must also show labels for this to have any effect, see
31573 `tool-bar-style'. */);
31574 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31575
31576 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31577 doc: /* List of functions to call to fontify regions of text.
31578 Each function is called with one argument POS. Functions must
31579 fontify a region starting at POS in the current buffer, and give
31580 fontified regions the property `fontified'. */);
31581 Vfontification_functions = Qnil;
31582 Fmake_variable_buffer_local (Qfontification_functions);
31583
31584 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31585 unibyte_display_via_language_environment,
31586 doc: /* Non-nil means display unibyte text according to language environment.
31587 Specifically, this means that raw bytes in the range 160-255 decimal
31588 are displayed by converting them to the equivalent multibyte characters
31589 according to the current language environment. As a result, they are
31590 displayed according to the current fontset.
31591
31592 Note that this variable affects only how these bytes are displayed,
31593 but does not change the fact they are interpreted as raw bytes. */);
31594 unibyte_display_via_language_environment = false;
31595
31596 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31597 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31598 If a float, it specifies a fraction of the mini-window frame's height.
31599 If an integer, it specifies a number of lines. */);
31600 Vmax_mini_window_height = make_float (0.25);
31601
31602 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31603 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31604 A value of nil means don't automatically resize mini-windows.
31605 A value of t means resize them to fit the text displayed in them.
31606 A value of `grow-only', the default, means let mini-windows grow only;
31607 they return to their normal size when the minibuffer is closed, or the
31608 echo area becomes empty. */);
31609 Vresize_mini_windows = Qgrow_only;
31610
31611 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31612 doc: /* Alist specifying how to blink the cursor off.
31613 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31614 `cursor-type' frame-parameter or variable equals ON-STATE,
31615 comparing using `equal', Emacs uses OFF-STATE to specify
31616 how to blink it off. ON-STATE and OFF-STATE are values for
31617 the `cursor-type' frame parameter.
31618
31619 If a frame's ON-STATE has no entry in this list,
31620 the frame's other specifications determine how to blink the cursor off. */);
31621 Vblink_cursor_alist = Qnil;
31622
31623 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31624 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31625 If non-nil, windows are automatically scrolled horizontally to make
31626 point visible. */);
31627 automatic_hscrolling_p = true;
31628 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31629
31630 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31631 doc: /* How many columns away from the window edge point is allowed to get
31632 before automatic hscrolling will horizontally scroll the window. */);
31633 hscroll_margin = 5;
31634
31635 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31636 doc: /* How many columns to scroll the window when point gets too close to the edge.
31637 When point is less than `hscroll-margin' columns from the window
31638 edge, automatic hscrolling will scroll the window by the amount of columns
31639 determined by this variable. If its value is a positive integer, scroll that
31640 many columns. If it's a positive floating-point number, it specifies the
31641 fraction of the window's width to scroll. If it's nil or zero, point will be
31642 centered horizontally after the scroll. Any other value, including negative
31643 numbers, are treated as if the value were zero.
31644
31645 Automatic hscrolling always moves point outside the scroll margin, so if
31646 point was more than scroll step columns inside the margin, the window will
31647 scroll more than the value given by the scroll step.
31648
31649 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31650 and `scroll-right' overrides this variable's effect. */);
31651 Vhscroll_step = make_number (0);
31652
31653 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31654 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31655 Bind this around calls to `message' to let it take effect. */);
31656 message_truncate_lines = false;
31657
31658 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31659 doc: /* Normal hook run to update the menu bar definitions.
31660 Redisplay runs this hook before it redisplays the menu bar.
31661 This is used to update menus such as Buffers, whose contents depend on
31662 various data. */);
31663 Vmenu_bar_update_hook = Qnil;
31664
31665 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31666 doc: /* Frame for which we are updating a menu.
31667 The enable predicate for a menu binding should check this variable. */);
31668 Vmenu_updating_frame = Qnil;
31669
31670 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31671 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31672 inhibit_menubar_update = false;
31673
31674 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31675 doc: /* Prefix prepended to all continuation lines at display time.
31676 The value may be a string, an image, or a stretch-glyph; it is
31677 interpreted in the same way as the value of a `display' text property.
31678
31679 This variable is overridden by any `wrap-prefix' text or overlay
31680 property.
31681
31682 To add a prefix to non-continuation lines, use `line-prefix'. */);
31683 Vwrap_prefix = Qnil;
31684 DEFSYM (Qwrap_prefix, "wrap-prefix");
31685 Fmake_variable_buffer_local (Qwrap_prefix);
31686
31687 DEFVAR_LISP ("line-prefix", Vline_prefix,
31688 doc: /* Prefix prepended to all non-continuation lines at display time.
31689 The value may be a string, an image, or a stretch-glyph; it is
31690 interpreted in the same way as the value of a `display' text property.
31691
31692 This variable is overridden by any `line-prefix' text or overlay
31693 property.
31694
31695 To add a prefix to continuation lines, use `wrap-prefix'. */);
31696 Vline_prefix = Qnil;
31697 DEFSYM (Qline_prefix, "line-prefix");
31698 Fmake_variable_buffer_local (Qline_prefix);
31699
31700 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31701 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31702 inhibit_eval_during_redisplay = false;
31703
31704 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31705 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31706 inhibit_free_realized_faces = false;
31707
31708 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31709 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31710 Intended for use during debugging and for testing bidi display;
31711 see biditest.el in the test suite. */);
31712 inhibit_bidi_mirroring = false;
31713
31714 #ifdef GLYPH_DEBUG
31715 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31716 doc: /* Inhibit try_window_id display optimization. */);
31717 inhibit_try_window_id = false;
31718
31719 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31720 doc: /* Inhibit try_window_reusing display optimization. */);
31721 inhibit_try_window_reusing = false;
31722
31723 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31724 doc: /* Inhibit try_cursor_movement display optimization. */);
31725 inhibit_try_cursor_movement = false;
31726 #endif /* GLYPH_DEBUG */
31727
31728 DEFVAR_INT ("overline-margin", overline_margin,
31729 doc: /* Space between overline and text, in pixels.
31730 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31731 margin to the character height. */);
31732 overline_margin = 2;
31733
31734 DEFVAR_INT ("underline-minimum-offset",
31735 underline_minimum_offset,
31736 doc: /* Minimum distance between baseline and underline.
31737 This can improve legibility of underlined text at small font sizes,
31738 particularly when using variable `x-use-underline-position-properties'
31739 with fonts that specify an UNDERLINE_POSITION relatively close to the
31740 baseline. The default value is 1. */);
31741 underline_minimum_offset = 1;
31742
31743 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31744 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31745 This feature only works when on a window system that can change
31746 cursor shapes. */);
31747 display_hourglass_p = true;
31748
31749 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31750 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31751 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31752
31753 #ifdef HAVE_WINDOW_SYSTEM
31754 hourglass_atimer = NULL;
31755 hourglass_shown_p = false;
31756 #endif /* HAVE_WINDOW_SYSTEM */
31757
31758 /* Name of the face used to display glyphless characters. */
31759 DEFSYM (Qglyphless_char, "glyphless-char");
31760
31761 /* Method symbols for Vglyphless_char_display. */
31762 DEFSYM (Qhex_code, "hex-code");
31763 DEFSYM (Qempty_box, "empty-box");
31764 DEFSYM (Qthin_space, "thin-space");
31765 DEFSYM (Qzero_width, "zero-width");
31766
31767 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31768 doc: /* Function run just before redisplay.
31769 It is called with one argument, which is the set of windows that are to
31770 be redisplayed. This set can be nil (meaning, only the selected window),
31771 or t (meaning all windows). */);
31772 Vpre_redisplay_function = intern ("ignore");
31773
31774 /* Symbol for the purpose of Vglyphless_char_display. */
31775 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31776 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31777
31778 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31779 doc: /* Char-table defining glyphless characters.
31780 Each element, if non-nil, should be one of the following:
31781 an ASCII acronym string: display this string in a box
31782 `hex-code': display the hexadecimal code of a character in a box
31783 `empty-box': display as an empty box
31784 `thin-space': display as 1-pixel width space
31785 `zero-width': don't display
31786 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31787 display method for graphical terminals and text terminals respectively.
31788 GRAPHICAL and TEXT should each have one of the values listed above.
31789
31790 The char-table has one extra slot to control the display of a character for
31791 which no font is found. This slot only takes effect on graphical terminals.
31792 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31793 `thin-space'. The default is `empty-box'.
31794
31795 If a character has a non-nil entry in an active display table, the
31796 display table takes effect; in this case, Emacs does not consult
31797 `glyphless-char-display' at all. */);
31798 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31799 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31800 Qempty_box);
31801
31802 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31803 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31804 Vdebug_on_message = Qnil;
31805
31806 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31807 doc: /* */);
31808 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
31809
31810 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31811 doc: /* */);
31812 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
31813
31814 DEFVAR_LISP ("redisplay--variables", Vredisplay__variables,
31815 doc: /* A hash-table of variables changing which triggers a thorough redisplay. */);
31816 Vredisplay__variables = Qnil;
31817 }
31818
31819
31820 /* Initialize this module when Emacs starts. */
31821
31822 void
31823 init_xdisp (void)
31824 {
31825 CHARPOS (this_line_start_pos) = 0;
31826
31827 if (!noninteractive)
31828 {
31829 struct window *m = XWINDOW (minibuf_window);
31830 Lisp_Object frame = m->frame;
31831 struct frame *f = XFRAME (frame);
31832 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31833 struct window *r = XWINDOW (root);
31834 int i;
31835
31836 echo_area_window = minibuf_window;
31837
31838 r->top_line = FRAME_TOP_MARGIN (f);
31839 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31840 r->total_cols = FRAME_COLS (f);
31841 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31842 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31843 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31844
31845 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31846 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31847 m->total_cols = FRAME_COLS (f);
31848 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31849 m->total_lines = 1;
31850 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31851
31852 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31853 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31854 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31855
31856 /* The default ellipsis glyphs `...'. */
31857 for (i = 0; i < 3; ++i)
31858 default_invis_vector[i] = make_number ('.');
31859 }
31860
31861 {
31862 /* Allocate the buffer for frame titles.
31863 Also used for `format-mode-line'. */
31864 int size = 100;
31865 mode_line_noprop_buf = xmalloc (size);
31866 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31867 mode_line_noprop_ptr = mode_line_noprop_buf;
31868 mode_line_target = MODE_LINE_DISPLAY;
31869 }
31870
31871 help_echo_showing_p = false;
31872 }
31873
31874 #ifdef HAVE_WINDOW_SYSTEM
31875
31876 /* Platform-independent portion of hourglass implementation. */
31877
31878 /* Timer function of hourglass_atimer. */
31879
31880 static void
31881 show_hourglass (struct atimer *timer)
31882 {
31883 /* The timer implementation will cancel this timer automatically
31884 after this function has run. Set hourglass_atimer to null
31885 so that we know the timer doesn't have to be canceled. */
31886 hourglass_atimer = NULL;
31887
31888 if (!hourglass_shown_p)
31889 {
31890 Lisp_Object tail, frame;
31891
31892 block_input ();
31893
31894 FOR_EACH_FRAME (tail, frame)
31895 {
31896 struct frame *f = XFRAME (frame);
31897
31898 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31899 && FRAME_RIF (f)->show_hourglass)
31900 FRAME_RIF (f)->show_hourglass (f);
31901 }
31902
31903 hourglass_shown_p = true;
31904 unblock_input ();
31905 }
31906 }
31907
31908 /* Cancel a currently active hourglass timer, and start a new one. */
31909
31910 void
31911 start_hourglass (void)
31912 {
31913 struct timespec delay;
31914
31915 cancel_hourglass ();
31916
31917 if (INTEGERP (Vhourglass_delay)
31918 && XINT (Vhourglass_delay) > 0)
31919 delay = make_timespec (min (XINT (Vhourglass_delay),
31920 TYPE_MAXIMUM (time_t)),
31921 0);
31922 else if (FLOATP (Vhourglass_delay)
31923 && XFLOAT_DATA (Vhourglass_delay) > 0)
31924 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31925 else
31926 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31927
31928 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31929 show_hourglass, NULL);
31930 }
31931
31932 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31933 shown. */
31934
31935 void
31936 cancel_hourglass (void)
31937 {
31938 if (hourglass_atimer)
31939 {
31940 cancel_atimer (hourglass_atimer);
31941 hourglass_atimer = NULL;
31942 }
31943
31944 if (hourglass_shown_p)
31945 {
31946 Lisp_Object tail, frame;
31947
31948 block_input ();
31949
31950 FOR_EACH_FRAME (tail, frame)
31951 {
31952 struct frame *f = XFRAME (frame);
31953
31954 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31955 && FRAME_RIF (f)->hide_hourglass)
31956 FRAME_RIF (f)->hide_hourglass (f);
31957 #ifdef HAVE_NTGUI
31958 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31959 else if (!FRAME_W32_P (f))
31960 w32_arrow_cursor ();
31961 #endif
31962 }
31963
31964 hourglass_shown_p = false;
31965 unblock_input ();
31966 }
31967 }
31968
31969 #endif /* HAVE_WINDOW_SYSTEM */