]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Expunge "allow" + infinitive from source and doc, part 2.
[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 #include "xwidget.h"
318 #ifdef HAVE_WINDOW_SYSTEM
319 #include TERM_HEADER
320 #endif /* HAVE_WINDOW_SYSTEM */
321
322 #ifndef FRAME_X_OUTPUT
323 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
324 #endif
325
326 #define INFINITY 10000000
327
328 /* Holds the list (error). */
329 static Lisp_Object list_of_error;
330
331 #ifdef HAVE_WINDOW_SYSTEM
332
333 /* Test if overflow newline into fringe. Called with iterator IT
334 at or past right window margin, and with IT->current_x set. */
335
336 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
337 (!NILP (Voverflow_newline_into_fringe) \
338 && FRAME_WINDOW_P ((IT)->f) \
339 && ((IT)->bidi_it.paragraph_dir == R2L \
340 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
341 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
342 && (IT)->current_x == (IT)->last_visible_x)
343
344 #else /* !HAVE_WINDOW_SYSTEM */
345 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
346 #endif /* HAVE_WINDOW_SYSTEM */
347
348 /* Test if the display element loaded in IT, or the underlying buffer
349 or string character, is a space or a TAB character. This is used
350 to determine where word wrapping can occur. */
351
352 #define IT_DISPLAYING_WHITESPACE(it) \
353 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
354 || ((STRINGP (it->string) \
355 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
356 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
357 || (it->s \
358 && (it->s[IT_BYTEPOS (*it)] == ' ' \
359 || it->s[IT_BYTEPOS (*it)] == '\t')) \
360 || (IT_BYTEPOS (*it) < ZV_BYTE \
361 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
362 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
363
364 /* True means print newline to stdout before next mini-buffer message. */
365
366 bool noninteractive_need_newline;
367
368 /* True means print newline to message log before next message. */
369
370 static bool message_log_need_newline;
371
372 /* Three markers that message_dolog uses.
373 It could allocate them itself, but that causes trouble
374 in handling memory-full errors. */
375 static Lisp_Object message_dolog_marker1;
376 static Lisp_Object message_dolog_marker2;
377 static Lisp_Object message_dolog_marker3;
378 \f
379 /* The buffer position of the first character appearing entirely or
380 partially on the line of the selected window which contains the
381 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
382 redisplay optimization in redisplay_internal. */
383
384 static struct text_pos this_line_start_pos;
385
386 /* Number of characters past the end of the line above, including the
387 terminating newline. */
388
389 static struct text_pos this_line_end_pos;
390
391 /* The vertical positions and the height of this line. */
392
393 static int this_line_vpos;
394 static int this_line_y;
395 static int this_line_pixel_height;
396
397 /* X position at which this display line starts. Usually zero;
398 negative if first character is partially visible. */
399
400 static int this_line_start_x;
401
402 /* The smallest character position seen by move_it_* functions as they
403 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
404 hscrolled lines, see display_line. */
405
406 static struct text_pos this_line_min_pos;
407
408 /* Buffer that this_line_.* variables are referring to. */
409
410 static struct buffer *this_line_buffer;
411
412 /* True if an overlay arrow has been displayed in this window. */
413
414 static bool overlay_arrow_seen;
415
416 /* Vector containing glyphs for an ellipsis `...'. */
417
418 static Lisp_Object default_invis_vector[3];
419
420 /* This is the window where the echo area message was displayed. It
421 is always a mini-buffer window, but it may not be the same window
422 currently active as a mini-buffer. */
423
424 Lisp_Object echo_area_window;
425
426 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
427 pushes the current message and the value of
428 message_enable_multibyte on the stack, the function restore_message
429 pops the stack and displays MESSAGE again. */
430
431 static Lisp_Object Vmessage_stack;
432
433 /* True means multibyte characters were enabled when the echo area
434 message was specified. */
435
436 static bool message_enable_multibyte;
437
438 /* At each redisplay cycle, we should refresh everything there is to refresh.
439 To do that efficiently, we use many optimizations that try to make sure we
440 don't waste too much time updating things that haven't changed.
441 The coarsest such optimization is that, in the most common cases, we only
442 look at the selected-window.
443
444 To know whether other windows should be considered for redisplay, we use the
445 variable windows_or_buffers_changed: as long as it is 0, it means that we
446 have not noticed anything that should require updating anything else than
447 the selected-window. If it is set to REDISPLAY_SOME, it means that since
448 last redisplay, some changes have been made which could impact other
449 windows. To know which ones need redisplay, every buffer, window, and frame
450 has a `redisplay' bit, which (if true) means that this object needs to be
451 redisplayed. If windows_or_buffers_changed is 0, we know there's no point
452 looking for those `redisplay' bits (actually, there might be some such bits
453 set, but then only on objects which aren't displayed anyway).
454
455 OTOH if it's non-zero we wil have to loop through all windows and then check
456 the `redisplay' bit of the corresponding window, frame, and buffer, in order
457 to decide whether that window needs attention or not. Note that we can't
458 just look at the frame's redisplay bit to decide that the whole frame can be
459 skipped, since even if the frame's redisplay bit is unset, some of its
460 windows's redisplay bits may be set.
461
462 Mostly for historical reasons, windows_or_buffers_changed can also take
463 other non-zero values. In that case, the precise value doesn't matter (it
464 encodes the cause of the setting but is only used for debugging purposes),
465 and what it means is that we shouldn't pay attention to any `redisplay' bits
466 and we should simply try and redisplay every window out there. */
467
468 int windows_or_buffers_changed;
469
470 /* Nonzero if we should redraw the mode lines on the next redisplay.
471 Similarly to `windows_or_buffers_changed', If it has value REDISPLAY_SOME,
472 then only redisplay the mode lines in those buffers/windows/frames where the
473 `redisplay' bit has been set.
474 For any other value, redisplay all mode lines (the number used is then only
475 used to track down the cause for this full-redisplay).
476
477 Since the frame title uses the same %-constructs as the mode line
478 (except %c and %l), if this variable is non-zero, we also consider
479 redisplaying the title of each frame, see x_consider_frame_title.
480
481 The `redisplay' bits are the same as those used for
482 windows_or_buffers_changed, and setting windows_or_buffers_changed also
483 causes recomputation of the mode lines of all those windows. IOW this
484 variable only has an effect if windows_or_buffers_changed is zero, in which
485 case we should only need to redisplay the mode-line of those objects with
486 a `redisplay' bit set but not the window's text content (tho we may still
487 need to refresh the text content of the selected-window). */
488
489 int update_mode_lines;
490
491 /* True after display_mode_line if %l was used and it displayed a
492 line number. */
493
494 static bool line_number_displayed;
495
496 /* The name of the *Messages* buffer, a string. */
497
498 static Lisp_Object Vmessages_buffer_name;
499
500 /* Current, index 0, and last displayed echo area message. Either
501 buffers from echo_buffers, or nil to indicate no message. */
502
503 Lisp_Object echo_area_buffer[2];
504
505 /* The buffers referenced from echo_area_buffer. */
506
507 static Lisp_Object echo_buffer[2];
508
509 /* A vector saved used in with_area_buffer to reduce consing. */
510
511 static Lisp_Object Vwith_echo_area_save_vector;
512
513 /* True means display_echo_area should display the last echo area
514 message again. Set by redisplay_preserve_echo_area. */
515
516 static bool display_last_displayed_message_p;
517
518 /* True if echo area is being used by print; false if being used by
519 message. */
520
521 static bool message_buf_print;
522
523 /* Set to true in clear_message to make redisplay_internal aware
524 of an emptied echo area. */
525
526 static bool message_cleared_p;
527
528 /* A scratch glyph row with contents used for generating truncation
529 glyphs. Also used in direct_output_for_insert. */
530
531 #define MAX_SCRATCH_GLYPHS 100
532 static struct glyph_row scratch_glyph_row;
533 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
534
535 /* Ascent and height of the last line processed by move_it_to. */
536
537 static int last_height;
538
539 /* True if there's a help-echo in the echo area. */
540
541 bool help_echo_showing_p;
542
543 /* The maximum distance to look ahead for text properties. Values
544 that are too small let us call compute_char_face and similar
545 functions too often which is expensive. Values that are too large
546 let us call compute_char_face and alike too often because we
547 might not be interested in text properties that far away. */
548
549 #define TEXT_PROP_DISTANCE_LIMIT 100
550
551 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
552 iterator state and later restore it. This is needed because the
553 bidi iterator on bidi.c keeps a stacked cache of its states, which
554 is really a singleton. When we use scratch iterator objects to
555 move around the buffer, we can cause the bidi cache to be pushed or
556 popped, and therefore we need to restore the cache state when we
557 return to the original iterator. */
558 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
559 do { \
560 if (CACHE) \
561 bidi_unshelve_cache (CACHE, true); \
562 ITCOPY = ITORIG; \
563 CACHE = bidi_shelve_cache (); \
564 } while (false)
565
566 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
567 do { \
568 if (pITORIG != pITCOPY) \
569 *(pITORIG) = *(pITCOPY); \
570 bidi_unshelve_cache (CACHE, false); \
571 CACHE = NULL; \
572 } while (false)
573
574 /* Functions to mark elements as needing redisplay. */
575 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
576
577 void
578 redisplay_other_windows (void)
579 {
580 if (!windows_or_buffers_changed)
581 windows_or_buffers_changed = REDISPLAY_SOME;
582 }
583
584 void
585 wset_redisplay (struct window *w)
586 {
587 /* Beware: selected_window can be nil during early stages. */
588 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
589 redisplay_other_windows ();
590 w->redisplay = true;
591 }
592
593 void
594 fset_redisplay (struct frame *f)
595 {
596 redisplay_other_windows ();
597 f->redisplay = true;
598 }
599
600 void
601 bset_redisplay (struct buffer *b)
602 {
603 int count = buffer_window_count (b);
604 if (count > 0)
605 {
606 /* ... it's visible in other window than selected, */
607 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
608 redisplay_other_windows ();
609 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
610 so that if we later set windows_or_buffers_changed, this buffer will
611 not be omitted. */
612 b->text->redisplay = true;
613 }
614 }
615
616 void
617 bset_update_mode_line (struct buffer *b)
618 {
619 if (!update_mode_lines)
620 update_mode_lines = REDISPLAY_SOME;
621 b->text->redisplay = true;
622 }
623
624 void
625 maybe_set_redisplay (Lisp_Object symbol)
626 {
627 if (HASH_TABLE_P (Vredisplay__variables)
628 && hash_lookup (XHASH_TABLE (Vredisplay__variables), symbol, NULL) >= 0)
629 {
630 bset_update_mode_line (current_buffer);
631 current_buffer->prevent_redisplay_optimizations_p = true;
632 }
633 }
634
635 #ifdef GLYPH_DEBUG
636
637 /* True means print traces of redisplay if compiled with
638 GLYPH_DEBUG defined. */
639
640 bool trace_redisplay_p;
641
642 #endif /* GLYPH_DEBUG */
643
644 #ifdef DEBUG_TRACE_MOVE
645 /* True means trace with TRACE_MOVE to stderr. */
646 static bool trace_move;
647
648 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
649 #else
650 #define TRACE_MOVE(x) (void) 0
651 #endif
652
653 /* Buffer being redisplayed -- for redisplay_window_error. */
654
655 static struct buffer *displayed_buffer;
656
657 /* Value returned from text property handlers (see below). */
658
659 enum prop_handled
660 {
661 HANDLED_NORMALLY,
662 HANDLED_RECOMPUTE_PROPS,
663 HANDLED_OVERLAY_STRING_CONSUMED,
664 HANDLED_RETURN
665 };
666
667 /* A description of text properties that redisplay is interested
668 in. */
669
670 struct props
671 {
672 /* The symbol index of the name of the property. */
673 short name;
674
675 /* A unique index for the property. */
676 enum prop_idx idx;
677
678 /* A handler function called to set up iterator IT from the property
679 at IT's current position. Value is used to steer handle_stop. */
680 enum prop_handled (*handler) (struct it *it);
681 };
682
683 static enum prop_handled handle_face_prop (struct it *);
684 static enum prop_handled handle_invisible_prop (struct it *);
685 static enum prop_handled handle_display_prop (struct it *);
686 static enum prop_handled handle_composition_prop (struct it *);
687 static enum prop_handled handle_overlay_change (struct it *);
688 static enum prop_handled handle_fontified_prop (struct it *);
689
690 /* Properties handled by iterators. */
691
692 static struct props it_props[] =
693 {
694 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
695 /* Handle `face' before `display' because some sub-properties of
696 `display' need to know the face. */
697 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
698 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
699 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
700 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
701 {0, 0, NULL}
702 };
703
704 /* Value is the position described by X. If X is a marker, value is
705 the marker_position of X. Otherwise, value is X. */
706
707 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
708
709 /* Enumeration returned by some move_it_.* functions internally. */
710
711 enum move_it_result
712 {
713 /* Not used. Undefined value. */
714 MOVE_UNDEFINED,
715
716 /* Move ended at the requested buffer position or ZV. */
717 MOVE_POS_MATCH_OR_ZV,
718
719 /* Move ended at the requested X pixel position. */
720 MOVE_X_REACHED,
721
722 /* Move within a line ended at the end of a line that must be
723 continued. */
724 MOVE_LINE_CONTINUED,
725
726 /* Move within a line ended at the end of a line that would
727 be displayed truncated. */
728 MOVE_LINE_TRUNCATED,
729
730 /* Move within a line ended at a line end. */
731 MOVE_NEWLINE_OR_CR
732 };
733
734 /* This counter is used to clear the face cache every once in a while
735 in redisplay_internal. It is incremented for each redisplay.
736 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
737 cleared. */
738
739 #define CLEAR_FACE_CACHE_COUNT 500
740 static int clear_face_cache_count;
741
742 /* Similarly for the image cache. */
743
744 #ifdef HAVE_WINDOW_SYSTEM
745 #define CLEAR_IMAGE_CACHE_COUNT 101
746 static int clear_image_cache_count;
747
748 /* Null glyph slice */
749 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
750 #endif
751
752 /* True while redisplay_internal is in progress. */
753
754 bool redisplaying_p;
755
756 /* If a string, XTread_socket generates an event to display that string.
757 (The display is done in read_char.) */
758
759 Lisp_Object help_echo_string;
760 Lisp_Object help_echo_window;
761 Lisp_Object help_echo_object;
762 ptrdiff_t help_echo_pos;
763
764 /* Temporary variable for XTread_socket. */
765
766 Lisp_Object previous_help_echo_string;
767
768 /* Platform-independent portion of hourglass implementation. */
769
770 #ifdef HAVE_WINDOW_SYSTEM
771
772 /* True means an hourglass cursor is currently shown. */
773 static bool hourglass_shown_p;
774
775 /* If non-null, an asynchronous timer that, when it expires, displays
776 an hourglass cursor on all frames. */
777 static struct atimer *hourglass_atimer;
778
779 #endif /* HAVE_WINDOW_SYSTEM */
780
781 /* Default number of seconds to wait before displaying an hourglass
782 cursor. */
783 #define DEFAULT_HOURGLASS_DELAY 1
784
785 #ifdef HAVE_WINDOW_SYSTEM
786
787 /* Default pixel width of `thin-space' display method. */
788 #define THIN_SPACE_WIDTH 1
789
790 #endif /* HAVE_WINDOW_SYSTEM */
791
792 /* Function prototypes. */
793
794 static void setup_for_ellipsis (struct it *, int);
795 static void set_iterator_to_next (struct it *, bool);
796 static void mark_window_display_accurate_1 (struct window *, bool);
797 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
798 static bool cursor_row_p (struct glyph_row *);
799 static int redisplay_mode_lines (Lisp_Object, bool);
800
801 static void handle_line_prefix (struct it *);
802
803 static void handle_stop_backwards (struct it *, ptrdiff_t);
804 static void unwind_with_echo_area_buffer (Lisp_Object);
805 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
806 static bool current_message_1 (ptrdiff_t, Lisp_Object);
807 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
808 static void set_message (Lisp_Object);
809 static bool set_message_1 (ptrdiff_t, Lisp_Object);
810 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
811 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
812 static void unwind_redisplay (void);
813 static void extend_face_to_end_of_line (struct it *);
814 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
815 static void push_it (struct it *, struct text_pos *);
816 static void iterate_out_of_display_property (struct it *);
817 static void pop_it (struct it *);
818 static void redisplay_internal (void);
819 static void echo_area_display (bool);
820 static void redisplay_windows (Lisp_Object);
821 static void redisplay_window (Lisp_Object, bool);
822 static Lisp_Object redisplay_window_error (Lisp_Object);
823 static Lisp_Object redisplay_window_0 (Lisp_Object);
824 static Lisp_Object redisplay_window_1 (Lisp_Object);
825 static bool set_cursor_from_row (struct window *, struct glyph_row *,
826 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
827 int, int);
828 static bool update_menu_bar (struct frame *, bool, bool);
829 static bool try_window_reusing_current_matrix (struct window *);
830 static int try_window_id (struct window *);
831 static bool display_line (struct it *);
832 static int display_mode_lines (struct window *);
833 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
834 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
835 Lisp_Object, bool);
836 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
837 Lisp_Object);
838 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
839 static void display_menu_bar (struct window *);
840 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
841 ptrdiff_t *);
842 static int display_string (const char *, Lisp_Object, Lisp_Object,
843 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
844 static void compute_line_metrics (struct it *);
845 static void run_redisplay_end_trigger_hook (struct it *);
846 static bool get_overlay_strings (struct it *, ptrdiff_t);
847 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
848 static void next_overlay_string (struct it *);
849 static void reseat (struct it *, struct text_pos, bool);
850 static void reseat_1 (struct it *, struct text_pos, bool);
851 static bool next_element_from_display_vector (struct it *);
852 static bool next_element_from_string (struct it *);
853 static bool next_element_from_c_string (struct it *);
854 static bool next_element_from_buffer (struct it *);
855 static bool next_element_from_composition (struct it *);
856 static bool next_element_from_image (struct it *);
857 static bool next_element_from_stretch (struct it *);
858 static bool next_element_from_xwidget (struct it *);
859 static void load_overlay_strings (struct it *, ptrdiff_t);
860 static bool get_next_display_element (struct it *);
861 static enum move_it_result
862 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
863 enum move_operation_enum);
864 static void get_visually_first_element (struct it *);
865 static void compute_stop_pos (struct it *);
866 static int face_before_or_after_it_pos (struct it *, bool);
867 static ptrdiff_t next_overlay_change (ptrdiff_t);
868 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
869 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
870 static int handle_single_display_spec (struct it *, Lisp_Object,
871 Lisp_Object, Lisp_Object,
872 struct text_pos *, ptrdiff_t, int, bool);
873 static int underlying_face_id (struct it *);
874
875 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
876 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
877
878 #ifdef HAVE_WINDOW_SYSTEM
879
880 static void update_tool_bar (struct frame *, bool);
881 static void x_draw_bottom_divider (struct window *w);
882 static void notice_overwritten_cursor (struct window *,
883 enum glyph_row_area,
884 int, int, int, int);
885 static int normal_char_height (struct font *, int);
886 static void normal_char_ascent_descent (struct font *, int, int *, int *);
887
888 static void append_stretch_glyph (struct it *, Lisp_Object,
889 int, int, int);
890
891 static Lisp_Object get_it_property (struct it *, Lisp_Object);
892 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
893 struct font *, int, bool);
894
895 #endif /* HAVE_WINDOW_SYSTEM */
896
897 static void produce_special_glyphs (struct it *, enum display_element_type);
898 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
899 static bool coords_in_mouse_face_p (struct window *, int, int);
900
901
902 \f
903 /***********************************************************************
904 Window display dimensions
905 ***********************************************************************/
906
907 /* Return the bottom boundary y-position for text lines in window W.
908 This is the first y position at which a line cannot start.
909 It is relative to the top of the window.
910
911 This is the height of W minus the height of a mode line, if any. */
912
913 int
914 window_text_bottom_y (struct window *w)
915 {
916 int height = WINDOW_PIXEL_HEIGHT (w);
917
918 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
919
920 if (WINDOW_WANTS_MODELINE_P (w))
921 height -= CURRENT_MODE_LINE_HEIGHT (w);
922
923 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
924
925 return height;
926 }
927
928 /* Return the pixel width of display area AREA of window W.
929 ANY_AREA means return the total width of W, not including
930 fringes to the left and right of the window. */
931
932 int
933 window_box_width (struct window *w, enum glyph_row_area area)
934 {
935 int width = w->pixel_width;
936
937 if (!w->pseudo_window_p)
938 {
939 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
940 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
941
942 if (area == TEXT_AREA)
943 width -= (WINDOW_MARGINS_WIDTH (w)
944 + WINDOW_FRINGES_WIDTH (w));
945 else if (area == LEFT_MARGIN_AREA)
946 width = WINDOW_LEFT_MARGIN_WIDTH (w);
947 else if (area == RIGHT_MARGIN_AREA)
948 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
949 }
950
951 /* With wide margins, fringes, etc. we might end up with a negative
952 width, correct that here. */
953 return max (0, width);
954 }
955
956
957 /* Return the pixel height of the display area of window W, not
958 including mode lines of W, if any. */
959
960 int
961 window_box_height (struct window *w)
962 {
963 struct frame *f = XFRAME (w->frame);
964 int height = WINDOW_PIXEL_HEIGHT (w);
965
966 eassert (height >= 0);
967
968 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
969 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
970
971 /* Note: the code below that determines the mode-line/header-line
972 height is essentially the same as that contained in the macro
973 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
974 the appropriate glyph row has its `mode_line_p' flag set,
975 and if it doesn't, uses estimate_mode_line_height instead. */
976
977 if (WINDOW_WANTS_MODELINE_P (w))
978 {
979 struct glyph_row *ml_row
980 = (w->current_matrix && w->current_matrix->rows
981 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
982 : 0);
983 if (ml_row && ml_row->mode_line_p)
984 height -= ml_row->height;
985 else
986 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
987 }
988
989 if (WINDOW_WANTS_HEADER_LINE_P (w))
990 {
991 struct glyph_row *hl_row
992 = (w->current_matrix && w->current_matrix->rows
993 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
994 : 0);
995 if (hl_row && hl_row->mode_line_p)
996 height -= hl_row->height;
997 else
998 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
999 }
1000
1001 /* With a very small font and a mode-line that's taller than
1002 default, we might end up with a negative height. */
1003 return max (0, height);
1004 }
1005
1006 /* Return the window-relative coordinate of the left edge of display
1007 area AREA of window W. ANY_AREA means return the left edge of the
1008 whole window, to the right of the left fringe of W. */
1009
1010 int
1011 window_box_left_offset (struct window *w, enum glyph_row_area area)
1012 {
1013 int x;
1014
1015 if (w->pseudo_window_p)
1016 return 0;
1017
1018 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1019
1020 if (area == TEXT_AREA)
1021 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1022 + window_box_width (w, LEFT_MARGIN_AREA));
1023 else if (area == RIGHT_MARGIN_AREA)
1024 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1025 + window_box_width (w, LEFT_MARGIN_AREA)
1026 + window_box_width (w, TEXT_AREA)
1027 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1028 ? 0
1029 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1030 else if (area == LEFT_MARGIN_AREA
1031 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1032 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1033
1034 /* Don't return more than the window's pixel width. */
1035 return min (x, w->pixel_width);
1036 }
1037
1038
1039 /* Return the window-relative coordinate of the right edge of display
1040 area AREA of window W. ANY_AREA means return the right edge of the
1041 whole window, to the left of the right fringe of W. */
1042
1043 static int
1044 window_box_right_offset (struct window *w, enum glyph_row_area area)
1045 {
1046 /* Don't return more than the window's pixel width. */
1047 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1048 w->pixel_width);
1049 }
1050
1051 /* Return the frame-relative coordinate of the left edge of display
1052 area AREA of window W. ANY_AREA means return the left edge of the
1053 whole window, to the right of the left fringe of W. */
1054
1055 int
1056 window_box_left (struct window *w, enum glyph_row_area area)
1057 {
1058 struct frame *f = XFRAME (w->frame);
1059 int x;
1060
1061 if (w->pseudo_window_p)
1062 return FRAME_INTERNAL_BORDER_WIDTH (f);
1063
1064 x = (WINDOW_LEFT_EDGE_X (w)
1065 + window_box_left_offset (w, area));
1066
1067 return x;
1068 }
1069
1070
1071 /* Return the frame-relative coordinate of the right edge of display
1072 area AREA of window W. ANY_AREA means return the right edge of the
1073 whole window, to the left of the right fringe of W. */
1074
1075 int
1076 window_box_right (struct window *w, enum glyph_row_area area)
1077 {
1078 return window_box_left (w, area) + window_box_width (w, area);
1079 }
1080
1081 /* Get the bounding box of the display area AREA of window W, without
1082 mode lines, in frame-relative coordinates. ANY_AREA means the
1083 whole window, not including the left and right fringes of
1084 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1085 coordinates of the upper-left corner of the box. Return in
1086 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1087
1088 void
1089 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1090 int *box_y, int *box_width, int *box_height)
1091 {
1092 if (box_width)
1093 *box_width = window_box_width (w, area);
1094 if (box_height)
1095 *box_height = window_box_height (w);
1096 if (box_x)
1097 *box_x = window_box_left (w, area);
1098 if (box_y)
1099 {
1100 *box_y = WINDOW_TOP_EDGE_Y (w);
1101 if (WINDOW_WANTS_HEADER_LINE_P (w))
1102 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1103 }
1104 }
1105
1106 #ifdef HAVE_WINDOW_SYSTEM
1107
1108 /* Get the bounding box of the display area AREA of window W, without
1109 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1110 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1111 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1112 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1113 box. */
1114
1115 static void
1116 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1117 int *bottom_right_x, int *bottom_right_y)
1118 {
1119 window_box (w, ANY_AREA, top_left_x, top_left_y,
1120 bottom_right_x, bottom_right_y);
1121 *bottom_right_x += *top_left_x;
1122 *bottom_right_y += *top_left_y;
1123 }
1124
1125 #endif /* HAVE_WINDOW_SYSTEM */
1126
1127 /***********************************************************************
1128 Utilities
1129 ***********************************************************************/
1130
1131 /* Return the bottom y-position of the line the iterator IT is in.
1132 This can modify IT's settings. */
1133
1134 int
1135 line_bottom_y (struct it *it)
1136 {
1137 int line_height = it->max_ascent + it->max_descent;
1138 int line_top_y = it->current_y;
1139
1140 if (line_height == 0)
1141 {
1142 if (last_height)
1143 line_height = last_height;
1144 else if (IT_CHARPOS (*it) < ZV)
1145 {
1146 move_it_by_lines (it, 1);
1147 line_height = (it->max_ascent || it->max_descent
1148 ? it->max_ascent + it->max_descent
1149 : last_height);
1150 }
1151 else
1152 {
1153 struct glyph_row *row = it->glyph_row;
1154
1155 /* Use the default character height. */
1156 it->glyph_row = NULL;
1157 it->what = IT_CHARACTER;
1158 it->c = ' ';
1159 it->len = 1;
1160 PRODUCE_GLYPHS (it);
1161 line_height = it->ascent + it->descent;
1162 it->glyph_row = row;
1163 }
1164 }
1165
1166 return line_top_y + line_height;
1167 }
1168
1169 DEFUN ("line-pixel-height", Fline_pixel_height,
1170 Sline_pixel_height, 0, 0, 0,
1171 doc: /* Return height in pixels of text line in the selected window.
1172
1173 Value is the height in pixels of the line at point. */)
1174 (void)
1175 {
1176 struct it it;
1177 struct text_pos pt;
1178 struct window *w = XWINDOW (selected_window);
1179 struct buffer *old_buffer = NULL;
1180 Lisp_Object result;
1181
1182 if (XBUFFER (w->contents) != current_buffer)
1183 {
1184 old_buffer = current_buffer;
1185 set_buffer_internal_1 (XBUFFER (w->contents));
1186 }
1187 SET_TEXT_POS (pt, PT, PT_BYTE);
1188 start_display (&it, w, pt);
1189 it.vpos = it.current_y = 0;
1190 last_height = 0;
1191 result = make_number (line_bottom_y (&it));
1192 if (old_buffer)
1193 set_buffer_internal_1 (old_buffer);
1194
1195 return result;
1196 }
1197
1198 /* Return the default pixel height of text lines in window W. The
1199 value is the canonical height of the W frame's default font, plus
1200 any extra space required by the line-spacing variable or frame
1201 parameter.
1202
1203 Implementation note: this ignores any line-spacing text properties
1204 put on the newline characters. This is because those properties
1205 only affect the _screen_ line ending in the newline (i.e., in a
1206 continued line, only the last screen line will be affected), which
1207 means only a small number of lines in a buffer can ever use this
1208 feature. Since this function is used to compute the default pixel
1209 equivalent of text lines in a window, we can safely ignore those
1210 few lines. For the same reasons, we ignore the line-height
1211 properties. */
1212 int
1213 default_line_pixel_height (struct window *w)
1214 {
1215 struct frame *f = WINDOW_XFRAME (w);
1216 int height = FRAME_LINE_HEIGHT (f);
1217
1218 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1219 {
1220 struct buffer *b = XBUFFER (w->contents);
1221 Lisp_Object val = BVAR (b, extra_line_spacing);
1222
1223 if (NILP (val))
1224 val = BVAR (&buffer_defaults, extra_line_spacing);
1225 if (!NILP (val))
1226 {
1227 if (RANGED_INTEGERP (0, val, INT_MAX))
1228 height += XFASTINT (val);
1229 else if (FLOATP (val))
1230 {
1231 int addon = XFLOAT_DATA (val) * height + 0.5;
1232
1233 if (addon >= 0)
1234 height += addon;
1235 }
1236 }
1237 else
1238 height += f->extra_line_spacing;
1239 }
1240
1241 return height;
1242 }
1243
1244 /* Subroutine of pos_visible_p below. Extracts a display string, if
1245 any, from the display spec given as its argument. */
1246 static Lisp_Object
1247 string_from_display_spec (Lisp_Object spec)
1248 {
1249 if (CONSP (spec))
1250 {
1251 while (CONSP (spec))
1252 {
1253 if (STRINGP (XCAR (spec)))
1254 return XCAR (spec);
1255 spec = XCDR (spec);
1256 }
1257 }
1258 else if (VECTORP (spec))
1259 {
1260 ptrdiff_t i;
1261
1262 for (i = 0; i < ASIZE (spec); i++)
1263 {
1264 if (STRINGP (AREF (spec, i)))
1265 return AREF (spec, i);
1266 }
1267 return Qnil;
1268 }
1269
1270 return spec;
1271 }
1272
1273
1274 /* Limit insanely large values of W->hscroll on frame F to the largest
1275 value that will still prevent first_visible_x and last_visible_x of
1276 'struct it' from overflowing an int. */
1277 static int
1278 window_hscroll_limited (struct window *w, struct frame *f)
1279 {
1280 ptrdiff_t window_hscroll = w->hscroll;
1281 int window_text_width = window_box_width (w, TEXT_AREA);
1282 int colwidth = FRAME_COLUMN_WIDTH (f);
1283
1284 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1285 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1286
1287 return window_hscroll;
1288 }
1289
1290 /* Return true if position CHARPOS is visible in window W.
1291 CHARPOS < 0 means return info about WINDOW_END position.
1292 If visible, set *X and *Y to pixel coordinates of top left corner.
1293 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1294 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1295
1296 bool
1297 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1298 int *rtop, int *rbot, int *rowh, int *vpos)
1299 {
1300 struct it it;
1301 void *itdata = bidi_shelve_cache ();
1302 struct text_pos top;
1303 bool visible_p = false;
1304 struct buffer *old_buffer = NULL;
1305 bool r2l = false;
1306
1307 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1308 return visible_p;
1309
1310 if (XBUFFER (w->contents) != current_buffer)
1311 {
1312 old_buffer = current_buffer;
1313 set_buffer_internal_1 (XBUFFER (w->contents));
1314 }
1315
1316 SET_TEXT_POS_FROM_MARKER (top, w->start);
1317 /* Scrolling a minibuffer window via scroll bar when the echo area
1318 shows long text sometimes resets the minibuffer contents behind
1319 our backs. */
1320 if (CHARPOS (top) > ZV)
1321 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1322
1323 /* Compute exact mode line heights. */
1324 if (WINDOW_WANTS_MODELINE_P (w))
1325 w->mode_line_height
1326 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1327 BVAR (current_buffer, mode_line_format));
1328
1329 if (WINDOW_WANTS_HEADER_LINE_P (w))
1330 w->header_line_height
1331 = display_mode_line (w, HEADER_LINE_FACE_ID,
1332 BVAR (current_buffer, header_line_format));
1333
1334 start_display (&it, w, top);
1335 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1336 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1337
1338 if (charpos >= 0
1339 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1340 && IT_CHARPOS (it) >= charpos)
1341 /* When scanning backwards under bidi iteration, move_it_to
1342 stops at or _before_ CHARPOS, because it stops at or to
1343 the _right_ of the character at CHARPOS. */
1344 || (it.bidi_p && it.bidi_it.scan_dir == -1
1345 && IT_CHARPOS (it) <= charpos)))
1346 {
1347 /* We have reached CHARPOS, or passed it. How the call to
1348 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1349 or covered by a display property, move_it_to stops at the end
1350 of the invisible text, to the right of CHARPOS. (ii) If
1351 CHARPOS is in a display vector, move_it_to stops on its last
1352 glyph. */
1353 int top_x = it.current_x;
1354 int top_y = it.current_y;
1355 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1356 int bottom_y;
1357 struct it save_it;
1358 void *save_it_data = NULL;
1359
1360 /* Calling line_bottom_y may change it.method, it.position, etc. */
1361 SAVE_IT (save_it, it, save_it_data);
1362 last_height = 0;
1363 bottom_y = line_bottom_y (&it);
1364 if (top_y < window_top_y)
1365 visible_p = bottom_y > window_top_y;
1366 else if (top_y < it.last_visible_y)
1367 visible_p = true;
1368 if (bottom_y >= it.last_visible_y
1369 && it.bidi_p && it.bidi_it.scan_dir == -1
1370 && IT_CHARPOS (it) < charpos)
1371 {
1372 /* When the last line of the window is scanned backwards
1373 under bidi iteration, we could be duped into thinking
1374 that we have passed CHARPOS, when in fact move_it_to
1375 simply stopped short of CHARPOS because it reached
1376 last_visible_y. To see if that's what happened, we call
1377 move_it_to again with a slightly larger vertical limit,
1378 and see if it actually moved vertically; if it did, we
1379 didn't really reach CHARPOS, which is beyond window end. */
1380 /* Why 10? because we don't know how many canonical lines
1381 will the height of the next line(s) be. So we guess. */
1382 int ten_more_lines = 10 * default_line_pixel_height (w);
1383
1384 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1385 MOVE_TO_POS | MOVE_TO_Y);
1386 if (it.current_y > top_y)
1387 visible_p = false;
1388
1389 }
1390 RESTORE_IT (&it, &save_it, save_it_data);
1391 if (visible_p)
1392 {
1393 if (it.method == GET_FROM_DISPLAY_VECTOR)
1394 {
1395 /* We stopped on the last glyph of a display vector.
1396 Try and recompute. Hack alert! */
1397 if (charpos < 2 || top.charpos >= charpos)
1398 top_x = it.glyph_row->x;
1399 else
1400 {
1401 struct it it2, it2_prev;
1402 /* The idea is to get to the previous buffer
1403 position, consume the character there, and use
1404 the pixel coordinates we get after that. But if
1405 the previous buffer position is also displayed
1406 from a display vector, we need to consume all of
1407 the glyphs from that display vector. */
1408 start_display (&it2, w, top);
1409 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1410 /* If we didn't get to CHARPOS - 1, there's some
1411 replacing display property at that position, and
1412 we stopped after it. That is exactly the place
1413 whose coordinates we want. */
1414 if (IT_CHARPOS (it2) != charpos - 1)
1415 it2_prev = it2;
1416 else
1417 {
1418 /* Iterate until we get out of the display
1419 vector that displays the character at
1420 CHARPOS - 1. */
1421 do {
1422 get_next_display_element (&it2);
1423 PRODUCE_GLYPHS (&it2);
1424 it2_prev = it2;
1425 set_iterator_to_next (&it2, true);
1426 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1427 && IT_CHARPOS (it2) < charpos);
1428 }
1429 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1430 || it2_prev.current_x > it2_prev.last_visible_x)
1431 top_x = it.glyph_row->x;
1432 else
1433 {
1434 top_x = it2_prev.current_x;
1435 top_y = it2_prev.current_y;
1436 }
1437 }
1438 }
1439 else if (IT_CHARPOS (it) != charpos)
1440 {
1441 Lisp_Object cpos = make_number (charpos);
1442 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1443 Lisp_Object string = string_from_display_spec (spec);
1444 struct text_pos tpos;
1445 bool newline_in_string
1446 = (STRINGP (string)
1447 && memchr (SDATA (string), '\n', SBYTES (string)));
1448
1449 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1450 bool replacing_spec_p
1451 = (!NILP (spec)
1452 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1453 charpos, FRAME_WINDOW_P (it.f)));
1454 /* The tricky code below is needed because there's a
1455 discrepancy between move_it_to and how we set cursor
1456 when PT is at the beginning of a portion of text
1457 covered by a display property or an overlay with a
1458 display property, or the display line ends in a
1459 newline from a display string. move_it_to will stop
1460 _after_ such display strings, whereas
1461 set_cursor_from_row conspires with cursor_row_p to
1462 place the cursor on the first glyph produced from the
1463 display string. */
1464
1465 /* We have overshoot PT because it is covered by a
1466 display property that replaces the text it covers.
1467 If the string includes embedded newlines, we are also
1468 in the wrong display line. Backtrack to the correct
1469 line, where the display property begins. */
1470 if (replacing_spec_p)
1471 {
1472 Lisp_Object startpos, endpos;
1473 EMACS_INT start, end;
1474 struct it it3;
1475
1476 /* Find the first and the last buffer positions
1477 covered by the display string. */
1478 endpos =
1479 Fnext_single_char_property_change (cpos, Qdisplay,
1480 Qnil, Qnil);
1481 startpos =
1482 Fprevious_single_char_property_change (endpos, Qdisplay,
1483 Qnil, Qnil);
1484 start = XFASTINT (startpos);
1485 end = XFASTINT (endpos);
1486 /* Move to the last buffer position before the
1487 display property. */
1488 start_display (&it3, w, top);
1489 if (start > CHARPOS (top))
1490 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1491 /* Move forward one more line if the position before
1492 the display string is a newline or if it is the
1493 rightmost character on a line that is
1494 continued or word-wrapped. */
1495 if (it3.method == GET_FROM_BUFFER
1496 && (it3.c == '\n'
1497 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1498 move_it_by_lines (&it3, 1);
1499 else if (move_it_in_display_line_to (&it3, -1,
1500 it3.current_x
1501 + it3.pixel_width,
1502 MOVE_TO_X)
1503 == MOVE_LINE_CONTINUED)
1504 {
1505 move_it_by_lines (&it3, 1);
1506 /* When we are under word-wrap, the #$@%!
1507 move_it_by_lines moves 2 lines, so we need to
1508 fix that up. */
1509 if (it3.line_wrap == WORD_WRAP)
1510 move_it_by_lines (&it3, -1);
1511 }
1512
1513 /* Record the vertical coordinate of the display
1514 line where we wound up. */
1515 top_y = it3.current_y;
1516 if (it3.bidi_p)
1517 {
1518 /* When characters are reordered for display,
1519 the character displayed to the left of the
1520 display string could be _after_ the display
1521 property in the logical order. Use the
1522 smallest vertical position of these two. */
1523 start_display (&it3, w, top);
1524 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1525 if (it3.current_y < top_y)
1526 top_y = it3.current_y;
1527 }
1528 /* Move from the top of the window to the beginning
1529 of the display line where the display string
1530 begins. */
1531 start_display (&it3, w, top);
1532 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1533 /* If it3_moved stays false after the 'while' loop
1534 below, that means we already were at a newline
1535 before the loop (e.g., the display string begins
1536 with a newline), so we don't need to (and cannot)
1537 inspect the glyphs of it3.glyph_row, because
1538 PRODUCE_GLYPHS will not produce anything for a
1539 newline, and thus it3.glyph_row stays at its
1540 stale content it got at top of the window. */
1541 bool it3_moved = false;
1542 /* Finally, advance the iterator until we hit the
1543 first display element whose character position is
1544 CHARPOS, or until the first newline from the
1545 display string, which signals the end of the
1546 display line. */
1547 while (get_next_display_element (&it3))
1548 {
1549 PRODUCE_GLYPHS (&it3);
1550 if (IT_CHARPOS (it3) == charpos
1551 || ITERATOR_AT_END_OF_LINE_P (&it3))
1552 break;
1553 it3_moved = true;
1554 set_iterator_to_next (&it3, false);
1555 }
1556 top_x = it3.current_x - it3.pixel_width;
1557 /* Normally, we would exit the above loop because we
1558 found the display element whose character
1559 position is CHARPOS. For the contingency that we
1560 didn't, and stopped at the first newline from the
1561 display string, move back over the glyphs
1562 produced from the string, until we find the
1563 rightmost glyph not from the string. */
1564 if (it3_moved
1565 && newline_in_string
1566 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1567 {
1568 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1569 + it3.glyph_row->used[TEXT_AREA];
1570
1571 while (EQ ((g - 1)->object, string))
1572 {
1573 --g;
1574 top_x -= g->pixel_width;
1575 }
1576 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1577 + it3.glyph_row->used[TEXT_AREA]);
1578 }
1579 }
1580 }
1581
1582 *x = top_x;
1583 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1584 *rtop = max (0, window_top_y - top_y);
1585 *rbot = max (0, bottom_y - it.last_visible_y);
1586 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1587 - max (top_y, window_top_y)));
1588 *vpos = it.vpos;
1589 if (it.bidi_it.paragraph_dir == R2L)
1590 r2l = true;
1591 }
1592 }
1593 else
1594 {
1595 /* Either we were asked to provide info about WINDOW_END, or
1596 CHARPOS is in the partially visible glyph row at end of
1597 window. */
1598 struct it it2;
1599 void *it2data = NULL;
1600
1601 SAVE_IT (it2, it, it2data);
1602 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1603 move_it_by_lines (&it, 1);
1604 if (charpos < IT_CHARPOS (it)
1605 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1606 {
1607 visible_p = true;
1608 RESTORE_IT (&it2, &it2, it2data);
1609 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1610 *x = it2.current_x;
1611 *y = it2.current_y + it2.max_ascent - it2.ascent;
1612 *rtop = max (0, -it2.current_y);
1613 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1614 - it.last_visible_y));
1615 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1616 it.last_visible_y)
1617 - max (it2.current_y,
1618 WINDOW_HEADER_LINE_HEIGHT (w))));
1619 *vpos = it2.vpos;
1620 if (it2.bidi_it.paragraph_dir == R2L)
1621 r2l = true;
1622 }
1623 else
1624 bidi_unshelve_cache (it2data, true);
1625 }
1626 bidi_unshelve_cache (itdata, false);
1627
1628 if (old_buffer)
1629 set_buffer_internal_1 (old_buffer);
1630
1631 if (visible_p)
1632 {
1633 if (w->hscroll > 0)
1634 *x -=
1635 window_hscroll_limited (w, WINDOW_XFRAME (w))
1636 * WINDOW_FRAME_COLUMN_WIDTH (w);
1637 /* For lines in an R2L paragraph, we need to mirror the X pixel
1638 coordinate wrt the text area. For the reasons, see the
1639 commentary in buffer_posn_from_coords and the explanation of
1640 the geometry used by the move_it_* functions at the end of
1641 the large commentary near the beginning of this file. */
1642 if (r2l)
1643 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1644 }
1645
1646 #if false
1647 /* Debugging code. */
1648 if (visible_p)
1649 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1650 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1651 else
1652 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1653 #endif
1654
1655 return visible_p;
1656 }
1657
1658
1659 /* Return the next character from STR. Return in *LEN the length of
1660 the character. This is like STRING_CHAR_AND_LENGTH but never
1661 returns an invalid character. If we find one, we return a `?', but
1662 with the length of the invalid character. */
1663
1664 static int
1665 string_char_and_length (const unsigned char *str, int *len)
1666 {
1667 int c;
1668
1669 c = STRING_CHAR_AND_LENGTH (str, *len);
1670 if (!CHAR_VALID_P (c))
1671 /* We may not change the length here because other places in Emacs
1672 don't use this function, i.e. they silently accept invalid
1673 characters. */
1674 c = '?';
1675
1676 return c;
1677 }
1678
1679
1680
1681 /* Given a position POS containing a valid character and byte position
1682 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1683
1684 static struct text_pos
1685 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1686 {
1687 eassert (STRINGP (string) && nchars >= 0);
1688
1689 if (STRING_MULTIBYTE (string))
1690 {
1691 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1692 int len;
1693
1694 while (nchars--)
1695 {
1696 string_char_and_length (p, &len);
1697 p += len;
1698 CHARPOS (pos) += 1;
1699 BYTEPOS (pos) += len;
1700 }
1701 }
1702 else
1703 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1704
1705 return pos;
1706 }
1707
1708
1709 /* Value is the text position, i.e. character and byte position,
1710 for character position CHARPOS in STRING. */
1711
1712 static struct text_pos
1713 string_pos (ptrdiff_t charpos, Lisp_Object string)
1714 {
1715 struct text_pos pos;
1716 eassert (STRINGP (string));
1717 eassert (charpos >= 0);
1718 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1719 return pos;
1720 }
1721
1722
1723 /* Value is a text position, i.e. character and byte position, for
1724 character position CHARPOS in C string S. MULTIBYTE_P
1725 means recognize multibyte characters. */
1726
1727 static struct text_pos
1728 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1729 {
1730 struct text_pos pos;
1731
1732 eassert (s != NULL);
1733 eassert (charpos >= 0);
1734
1735 if (multibyte_p)
1736 {
1737 int len;
1738
1739 SET_TEXT_POS (pos, 0, 0);
1740 while (charpos--)
1741 {
1742 string_char_and_length ((const unsigned char *) s, &len);
1743 s += len;
1744 CHARPOS (pos) += 1;
1745 BYTEPOS (pos) += len;
1746 }
1747 }
1748 else
1749 SET_TEXT_POS (pos, charpos, charpos);
1750
1751 return pos;
1752 }
1753
1754
1755 /* Value is the number of characters in C string S. MULTIBYTE_P
1756 means recognize multibyte characters. */
1757
1758 static ptrdiff_t
1759 number_of_chars (const char *s, bool multibyte_p)
1760 {
1761 ptrdiff_t nchars;
1762
1763 if (multibyte_p)
1764 {
1765 ptrdiff_t rest = strlen (s);
1766 int len;
1767 const unsigned char *p = (const unsigned char *) s;
1768
1769 for (nchars = 0; rest > 0; ++nchars)
1770 {
1771 string_char_and_length (p, &len);
1772 rest -= len, p += len;
1773 }
1774 }
1775 else
1776 nchars = strlen (s);
1777
1778 return nchars;
1779 }
1780
1781
1782 /* Compute byte position NEWPOS->bytepos corresponding to
1783 NEWPOS->charpos. POS is a known position in string STRING.
1784 NEWPOS->charpos must be >= POS.charpos. */
1785
1786 static void
1787 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1788 {
1789 eassert (STRINGP (string));
1790 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1791
1792 if (STRING_MULTIBYTE (string))
1793 *newpos = string_pos_nchars_ahead (pos, string,
1794 CHARPOS (*newpos) - CHARPOS (pos));
1795 else
1796 BYTEPOS (*newpos) = CHARPOS (*newpos);
1797 }
1798
1799 /* EXPORT:
1800 Return an estimation of the pixel height of mode or header lines on
1801 frame F. FACE_ID specifies what line's height to estimate. */
1802
1803 int
1804 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1805 {
1806 #ifdef HAVE_WINDOW_SYSTEM
1807 if (FRAME_WINDOW_P (f))
1808 {
1809 int height = FONT_HEIGHT (FRAME_FONT (f));
1810
1811 /* This function is called so early when Emacs starts that the face
1812 cache and mode line face are not yet initialized. */
1813 if (FRAME_FACE_CACHE (f))
1814 {
1815 struct face *face = FACE_FROM_ID (f, face_id);
1816 if (face)
1817 {
1818 if (face->font)
1819 height = normal_char_height (face->font, -1);
1820 if (face->box_line_width > 0)
1821 height += 2 * face->box_line_width;
1822 }
1823 }
1824
1825 return height;
1826 }
1827 #endif
1828
1829 return 1;
1830 }
1831
1832 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1833 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1834 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1835 not force the value into range. */
1836
1837 void
1838 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1839 NativeRectangle *bounds, bool noclip)
1840 {
1841
1842 #ifdef HAVE_WINDOW_SYSTEM
1843 if (FRAME_WINDOW_P (f))
1844 {
1845 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1846 even for negative values. */
1847 if (pix_x < 0)
1848 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1849 if (pix_y < 0)
1850 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1851
1852 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1853 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1854
1855 if (bounds)
1856 STORE_NATIVE_RECT (*bounds,
1857 FRAME_COL_TO_PIXEL_X (f, pix_x),
1858 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1859 FRAME_COLUMN_WIDTH (f) - 1,
1860 FRAME_LINE_HEIGHT (f) - 1);
1861
1862 /* PXW: Should we clip pixels before converting to columns/lines? */
1863 if (!noclip)
1864 {
1865 if (pix_x < 0)
1866 pix_x = 0;
1867 else if (pix_x > FRAME_TOTAL_COLS (f))
1868 pix_x = FRAME_TOTAL_COLS (f);
1869
1870 if (pix_y < 0)
1871 pix_y = 0;
1872 else if (pix_y > FRAME_TOTAL_LINES (f))
1873 pix_y = FRAME_TOTAL_LINES (f);
1874 }
1875 }
1876 #endif
1877
1878 *x = pix_x;
1879 *y = pix_y;
1880 }
1881
1882
1883 /* Find the glyph under window-relative coordinates X/Y in window W.
1884 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1885 strings. Return in *HPOS and *VPOS the row and column number of
1886 the glyph found. Return in *AREA the glyph area containing X.
1887 Value is a pointer to the glyph found or null if X/Y is not on
1888 text, or we can't tell because W's current matrix is not up to
1889 date. */
1890
1891 static struct glyph *
1892 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1893 int *dx, int *dy, int *area)
1894 {
1895 struct glyph *glyph, *end;
1896 struct glyph_row *row = NULL;
1897 int x0, i;
1898
1899 /* Find row containing Y. Give up if some row is not enabled. */
1900 for (i = 0; i < w->current_matrix->nrows; ++i)
1901 {
1902 row = MATRIX_ROW (w->current_matrix, i);
1903 if (!row->enabled_p)
1904 return NULL;
1905 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1906 break;
1907 }
1908
1909 *vpos = i;
1910 *hpos = 0;
1911
1912 /* Give up if Y is not in the window. */
1913 if (i == w->current_matrix->nrows)
1914 return NULL;
1915
1916 /* Get the glyph area containing X. */
1917 if (w->pseudo_window_p)
1918 {
1919 *area = TEXT_AREA;
1920 x0 = 0;
1921 }
1922 else
1923 {
1924 if (x < window_box_left_offset (w, TEXT_AREA))
1925 {
1926 *area = LEFT_MARGIN_AREA;
1927 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1928 }
1929 else if (x < window_box_right_offset (w, TEXT_AREA))
1930 {
1931 *area = TEXT_AREA;
1932 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1933 }
1934 else
1935 {
1936 *area = RIGHT_MARGIN_AREA;
1937 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1938 }
1939 }
1940
1941 /* Find glyph containing X. */
1942 glyph = row->glyphs[*area];
1943 end = glyph + row->used[*area];
1944 x -= x0;
1945 while (glyph < end && x >= glyph->pixel_width)
1946 {
1947 x -= glyph->pixel_width;
1948 ++glyph;
1949 }
1950
1951 if (glyph == end)
1952 return NULL;
1953
1954 if (dx)
1955 {
1956 *dx = x;
1957 *dy = y - (row->y + row->ascent - glyph->ascent);
1958 }
1959
1960 *hpos = glyph - row->glyphs[*area];
1961 return glyph;
1962 }
1963
1964 /* Convert frame-relative x/y to coordinates relative to window W.
1965 Takes pseudo-windows into account. */
1966
1967 static void
1968 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1969 {
1970 if (w->pseudo_window_p)
1971 {
1972 /* A pseudo-window is always full-width, and starts at the
1973 left edge of the frame, plus a frame border. */
1974 struct frame *f = XFRAME (w->frame);
1975 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1976 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1977 }
1978 else
1979 {
1980 *x -= WINDOW_LEFT_EDGE_X (w);
1981 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1982 }
1983 }
1984
1985 #ifdef HAVE_WINDOW_SYSTEM
1986
1987 /* EXPORT:
1988 Return in RECTS[] at most N clipping rectangles for glyph string S.
1989 Return the number of stored rectangles. */
1990
1991 int
1992 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1993 {
1994 XRectangle r;
1995
1996 if (n <= 0)
1997 return 0;
1998
1999 if (s->row->full_width_p)
2000 {
2001 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2002 r.x = WINDOW_LEFT_EDGE_X (s->w);
2003 if (s->row->mode_line_p)
2004 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2005 else
2006 r.width = WINDOW_PIXEL_WIDTH (s->w);
2007
2008 /* Unless displaying a mode or menu bar line, which are always
2009 fully visible, clip to the visible part of the row. */
2010 if (s->w->pseudo_window_p)
2011 r.height = s->row->visible_height;
2012 else
2013 r.height = s->height;
2014 }
2015 else
2016 {
2017 /* This is a text line that may be partially visible. */
2018 r.x = window_box_left (s->w, s->area);
2019 r.width = window_box_width (s->w, s->area);
2020 r.height = s->row->visible_height;
2021 }
2022
2023 if (s->clip_head)
2024 if (r.x < s->clip_head->x)
2025 {
2026 if (r.width >= s->clip_head->x - r.x)
2027 r.width -= s->clip_head->x - r.x;
2028 else
2029 r.width = 0;
2030 r.x = s->clip_head->x;
2031 }
2032 if (s->clip_tail)
2033 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2034 {
2035 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2036 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2037 else
2038 r.width = 0;
2039 }
2040
2041 /* If S draws overlapping rows, it's sufficient to use the top and
2042 bottom of the window for clipping because this glyph string
2043 intentionally draws over other lines. */
2044 if (s->for_overlaps)
2045 {
2046 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2047 r.height = window_text_bottom_y (s->w) - r.y;
2048
2049 /* Alas, the above simple strategy does not work for the
2050 environments with anti-aliased text: if the same text is
2051 drawn onto the same place multiple times, it gets thicker.
2052 If the overlap we are processing is for the erased cursor, we
2053 take the intersection with the rectangle of the cursor. */
2054 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2055 {
2056 XRectangle rc, r_save = r;
2057
2058 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2059 rc.y = s->w->phys_cursor.y;
2060 rc.width = s->w->phys_cursor_width;
2061 rc.height = s->w->phys_cursor_height;
2062
2063 x_intersect_rectangles (&r_save, &rc, &r);
2064 }
2065 }
2066 else
2067 {
2068 /* Don't use S->y for clipping because it doesn't take partially
2069 visible lines into account. For example, it can be negative for
2070 partially visible lines at the top of a window. */
2071 if (!s->row->full_width_p
2072 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2073 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2074 else
2075 r.y = max (0, s->row->y);
2076 }
2077
2078 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2079
2080 /* If drawing the cursor, don't let glyph draw outside its
2081 advertised boundaries. Cleartype does this under some circumstances. */
2082 if (s->hl == DRAW_CURSOR)
2083 {
2084 struct glyph *glyph = s->first_glyph;
2085 int height, max_y;
2086
2087 if (s->x > r.x)
2088 {
2089 if (r.width >= s->x - r.x)
2090 r.width -= s->x - r.x;
2091 else /* R2L hscrolled row with cursor outside text area */
2092 r.width = 0;
2093 r.x = s->x;
2094 }
2095 r.width = min (r.width, glyph->pixel_width);
2096
2097 /* If r.y is below window bottom, ensure that we still see a cursor. */
2098 height = min (glyph->ascent + glyph->descent,
2099 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2100 max_y = window_text_bottom_y (s->w) - height;
2101 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2102 if (s->ybase - glyph->ascent > max_y)
2103 {
2104 r.y = max_y;
2105 r.height = height;
2106 }
2107 else
2108 {
2109 /* Don't draw cursor glyph taller than our actual glyph. */
2110 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2111 if (height < r.height)
2112 {
2113 max_y = r.y + r.height;
2114 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2115 r.height = min (max_y - r.y, height);
2116 }
2117 }
2118 }
2119
2120 if (s->row->clip)
2121 {
2122 XRectangle r_save = r;
2123
2124 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2125 r.width = 0;
2126 }
2127
2128 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2129 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2130 {
2131 #ifdef CONVERT_FROM_XRECT
2132 CONVERT_FROM_XRECT (r, *rects);
2133 #else
2134 *rects = r;
2135 #endif
2136 return 1;
2137 }
2138 else
2139 {
2140 /* If we are processing overlapping and allowed to return
2141 multiple clipping rectangles, we exclude the row of the glyph
2142 string from the clipping rectangle. This is to avoid drawing
2143 the same text on the environment with anti-aliasing. */
2144 #ifdef CONVERT_FROM_XRECT
2145 XRectangle rs[2];
2146 #else
2147 XRectangle *rs = rects;
2148 #endif
2149 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2150
2151 if (s->for_overlaps & OVERLAPS_PRED)
2152 {
2153 rs[i] = r;
2154 if (r.y + r.height > row_y)
2155 {
2156 if (r.y < row_y)
2157 rs[i].height = row_y - r.y;
2158 else
2159 rs[i].height = 0;
2160 }
2161 i++;
2162 }
2163 if (s->for_overlaps & OVERLAPS_SUCC)
2164 {
2165 rs[i] = r;
2166 if (r.y < row_y + s->row->visible_height)
2167 {
2168 if (r.y + r.height > row_y + s->row->visible_height)
2169 {
2170 rs[i].y = row_y + s->row->visible_height;
2171 rs[i].height = r.y + r.height - rs[i].y;
2172 }
2173 else
2174 rs[i].height = 0;
2175 }
2176 i++;
2177 }
2178
2179 n = i;
2180 #ifdef CONVERT_FROM_XRECT
2181 for (i = 0; i < n; i++)
2182 CONVERT_FROM_XRECT (rs[i], rects[i]);
2183 #endif
2184 return n;
2185 }
2186 }
2187
2188 /* EXPORT:
2189 Return in *NR the clipping rectangle for glyph string S. */
2190
2191 void
2192 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2193 {
2194 get_glyph_string_clip_rects (s, nr, 1);
2195 }
2196
2197
2198 /* EXPORT:
2199 Return the position and height of the phys cursor in window W.
2200 Set w->phys_cursor_width to width of phys cursor.
2201 */
2202
2203 void
2204 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2205 struct glyph *glyph, int *xp, int *yp, int *heightp)
2206 {
2207 struct frame *f = XFRAME (WINDOW_FRAME (w));
2208 int x, y, wd, h, h0, y0, ascent;
2209
2210 /* Compute the width of the rectangle to draw. If on a stretch
2211 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2212 rectangle as wide as the glyph, but use a canonical character
2213 width instead. */
2214 wd = glyph->pixel_width;
2215
2216 x = w->phys_cursor.x;
2217 if (x < 0)
2218 {
2219 wd += x;
2220 x = 0;
2221 }
2222
2223 if (glyph->type == STRETCH_GLYPH
2224 && !x_stretch_cursor_p)
2225 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2226 w->phys_cursor_width = wd;
2227
2228 /* Don't let the hollow cursor glyph descend below the glyph row's
2229 ascent value, lest the hollow cursor looks funny. */
2230 y = w->phys_cursor.y;
2231 ascent = row->ascent;
2232 if (row->ascent < glyph->ascent)
2233 {
2234 y =- glyph->ascent - row->ascent;
2235 ascent = glyph->ascent;
2236 }
2237
2238 /* If y is below window bottom, ensure that we still see a cursor. */
2239 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2240
2241 h = max (h0, ascent + glyph->descent);
2242 h0 = min (h0, ascent + glyph->descent);
2243
2244 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2245 if (y < y0)
2246 {
2247 h = max (h - (y0 - y) + 1, h0);
2248 y = y0 - 1;
2249 }
2250 else
2251 {
2252 y0 = window_text_bottom_y (w) - h0;
2253 if (y > y0)
2254 {
2255 h += y - y0;
2256 y = y0;
2257 }
2258 }
2259
2260 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2261 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2262 *heightp = h;
2263 }
2264
2265 /*
2266 * Remember which glyph the mouse is over.
2267 */
2268
2269 void
2270 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2271 {
2272 Lisp_Object window;
2273 struct window *w;
2274 struct glyph_row *r, *gr, *end_row;
2275 enum window_part part;
2276 enum glyph_row_area area;
2277 int x, y, width, height;
2278
2279 /* Try to determine frame pixel position and size of the glyph under
2280 frame pixel coordinates X/Y on frame F. */
2281
2282 if (window_resize_pixelwise)
2283 {
2284 width = height = 1;
2285 goto virtual_glyph;
2286 }
2287 else if (!f->glyphs_initialized_p
2288 || (window = window_from_coordinates (f, gx, gy, &part, false),
2289 NILP (window)))
2290 {
2291 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2292 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2293 goto virtual_glyph;
2294 }
2295
2296 w = XWINDOW (window);
2297 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2298 height = WINDOW_FRAME_LINE_HEIGHT (w);
2299
2300 x = window_relative_x_coord (w, part, gx);
2301 y = gy - WINDOW_TOP_EDGE_Y (w);
2302
2303 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2304 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2305
2306 if (w->pseudo_window_p)
2307 {
2308 area = TEXT_AREA;
2309 part = ON_MODE_LINE; /* Don't adjust margin. */
2310 goto text_glyph;
2311 }
2312
2313 switch (part)
2314 {
2315 case ON_LEFT_MARGIN:
2316 area = LEFT_MARGIN_AREA;
2317 goto text_glyph;
2318
2319 case ON_RIGHT_MARGIN:
2320 area = RIGHT_MARGIN_AREA;
2321 goto text_glyph;
2322
2323 case ON_HEADER_LINE:
2324 case ON_MODE_LINE:
2325 gr = (part == ON_HEADER_LINE
2326 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2327 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2328 gy = gr->y;
2329 area = TEXT_AREA;
2330 goto text_glyph_row_found;
2331
2332 case ON_TEXT:
2333 area = TEXT_AREA;
2334
2335 text_glyph:
2336 gr = 0; gy = 0;
2337 for (; r <= end_row && r->enabled_p; ++r)
2338 if (r->y + r->height > y)
2339 {
2340 gr = r; gy = r->y;
2341 break;
2342 }
2343
2344 text_glyph_row_found:
2345 if (gr && gy <= y)
2346 {
2347 struct glyph *g = gr->glyphs[area];
2348 struct glyph *end = g + gr->used[area];
2349
2350 height = gr->height;
2351 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2352 if (gx + g->pixel_width > x)
2353 break;
2354
2355 if (g < end)
2356 {
2357 if (g->type == IMAGE_GLYPH)
2358 {
2359 /* Don't remember when mouse is over image, as
2360 image may have hot-spots. */
2361 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2362 return;
2363 }
2364 width = g->pixel_width;
2365 }
2366 else
2367 {
2368 /* Use nominal char spacing at end of line. */
2369 x -= gx;
2370 gx += (x / width) * width;
2371 }
2372
2373 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2374 {
2375 gx += window_box_left_offset (w, area);
2376 /* Don't expand over the modeline to make sure the vertical
2377 drag cursor is shown early enough. */
2378 height = min (height,
2379 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2380 }
2381 }
2382 else
2383 {
2384 /* Use nominal line height at end of window. */
2385 gx = (x / width) * width;
2386 y -= gy;
2387 gy += (y / height) * height;
2388 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2389 /* See comment above. */
2390 height = min (height,
2391 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2392 }
2393 break;
2394
2395 case ON_LEFT_FRINGE:
2396 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2397 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2398 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2399 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2400 goto row_glyph;
2401
2402 case ON_RIGHT_FRINGE:
2403 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2404 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2405 : window_box_right_offset (w, TEXT_AREA));
2406 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2407 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2408 && !WINDOW_RIGHTMOST_P (w))
2409 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2410 /* Make sure the vertical border can get her own glyph to the
2411 right of the one we build here. */
2412 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2413 else
2414 width = WINDOW_PIXEL_WIDTH (w) - gx;
2415 else
2416 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2417
2418 goto row_glyph;
2419
2420 case ON_VERTICAL_BORDER:
2421 gx = WINDOW_PIXEL_WIDTH (w) - width;
2422 goto row_glyph;
2423
2424 case ON_VERTICAL_SCROLL_BAR:
2425 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2426 ? 0
2427 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2428 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2429 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2430 : 0)));
2431 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2432
2433 row_glyph:
2434 gr = 0, gy = 0;
2435 for (; r <= end_row && r->enabled_p; ++r)
2436 if (r->y + r->height > y)
2437 {
2438 gr = r; gy = r->y;
2439 break;
2440 }
2441
2442 if (gr && gy <= y)
2443 height = gr->height;
2444 else
2445 {
2446 /* Use nominal line height at end of window. */
2447 y -= gy;
2448 gy += (y / height) * height;
2449 }
2450 break;
2451
2452 case ON_RIGHT_DIVIDER:
2453 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2454 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2455 gy = 0;
2456 /* The bottom divider prevails. */
2457 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2458 goto add_edge;
2459
2460 case ON_BOTTOM_DIVIDER:
2461 gx = 0;
2462 width = WINDOW_PIXEL_WIDTH (w);
2463 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2464 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2465 goto add_edge;
2466
2467 default:
2468 ;
2469 virtual_glyph:
2470 /* If there is no glyph under the mouse, then we divide the screen
2471 into a grid of the smallest glyph in the frame, and use that
2472 as our "glyph". */
2473
2474 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2475 round down even for negative values. */
2476 if (gx < 0)
2477 gx -= width - 1;
2478 if (gy < 0)
2479 gy -= height - 1;
2480
2481 gx = (gx / width) * width;
2482 gy = (gy / height) * height;
2483
2484 goto store_rect;
2485 }
2486
2487 add_edge:
2488 gx += WINDOW_LEFT_EDGE_X (w);
2489 gy += WINDOW_TOP_EDGE_Y (w);
2490
2491 store_rect:
2492 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2493
2494 /* Visible feedback for debugging. */
2495 #if false && defined HAVE_X_WINDOWS
2496 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2497 f->output_data.x->normal_gc,
2498 gx, gy, width, height);
2499 #endif
2500 }
2501
2502
2503 #endif /* HAVE_WINDOW_SYSTEM */
2504
2505 static void
2506 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2507 {
2508 eassert (w);
2509 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2510 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2511 w->window_end_vpos
2512 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2513 }
2514
2515 /***********************************************************************
2516 Lisp form evaluation
2517 ***********************************************************************/
2518
2519 /* Error handler for safe_eval and safe_call. */
2520
2521 static Lisp_Object
2522 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2523 {
2524 add_to_log ("Error during redisplay: %S signaled %S",
2525 Flist (nargs, args), arg);
2526 return Qnil;
2527 }
2528
2529 /* Call function FUNC with the rest of NARGS - 1 arguments
2530 following. Return the result, or nil if something went
2531 wrong. Prevent redisplay during the evaluation. */
2532
2533 static Lisp_Object
2534 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2535 {
2536 Lisp_Object val;
2537
2538 if (inhibit_eval_during_redisplay)
2539 val = Qnil;
2540 else
2541 {
2542 ptrdiff_t i;
2543 ptrdiff_t count = SPECPDL_INDEX ();
2544 Lisp_Object *args;
2545 USE_SAFE_ALLOCA;
2546 SAFE_ALLOCA_LISP (args, nargs);
2547
2548 args[0] = func;
2549 for (i = 1; i < nargs; i++)
2550 args[i] = va_arg (ap, Lisp_Object);
2551
2552 specbind (Qinhibit_redisplay, Qt);
2553 if (inhibit_quit)
2554 specbind (Qinhibit_quit, Qt);
2555 /* Use Qt to ensure debugger does not run,
2556 so there is no possibility of wanting to redisplay. */
2557 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2558 safe_eval_handler);
2559 SAFE_FREE ();
2560 val = unbind_to (count, val);
2561 }
2562
2563 return val;
2564 }
2565
2566 Lisp_Object
2567 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2568 {
2569 Lisp_Object retval;
2570 va_list ap;
2571
2572 va_start (ap, func);
2573 retval = safe__call (false, nargs, func, ap);
2574 va_end (ap);
2575 return retval;
2576 }
2577
2578 /* Call function FN with one argument ARG.
2579 Return the result, or nil if something went wrong. */
2580
2581 Lisp_Object
2582 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2583 {
2584 return safe_call (2, fn, arg);
2585 }
2586
2587 static Lisp_Object
2588 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2589 {
2590 Lisp_Object retval;
2591 va_list ap;
2592
2593 va_start (ap, fn);
2594 retval = safe__call (inhibit_quit, 2, fn, ap);
2595 va_end (ap);
2596 return retval;
2597 }
2598
2599 Lisp_Object
2600 safe_eval (Lisp_Object sexpr)
2601 {
2602 return safe__call1 (false, Qeval, sexpr);
2603 }
2604
2605 static Lisp_Object
2606 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2607 {
2608 return safe__call1 (inhibit_quit, Qeval, sexpr);
2609 }
2610
2611 /* Call function FN with two arguments ARG1 and ARG2.
2612 Return the result, or nil if something went wrong. */
2613
2614 Lisp_Object
2615 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2616 {
2617 return safe_call (3, fn, arg1, arg2);
2618 }
2619
2620
2621 \f
2622 /***********************************************************************
2623 Debugging
2624 ***********************************************************************/
2625
2626 /* Define CHECK_IT to perform sanity checks on iterators.
2627 This is for debugging. It is too slow to do unconditionally. */
2628
2629 static void
2630 CHECK_IT (struct it *it)
2631 {
2632 #if false
2633 if (it->method == GET_FROM_STRING)
2634 {
2635 eassert (STRINGP (it->string));
2636 eassert (IT_STRING_CHARPOS (*it) >= 0);
2637 }
2638 else
2639 {
2640 eassert (IT_STRING_CHARPOS (*it) < 0);
2641 if (it->method == GET_FROM_BUFFER)
2642 {
2643 /* Check that character and byte positions agree. */
2644 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2645 }
2646 }
2647
2648 if (it->dpvec)
2649 eassert (it->current.dpvec_index >= 0);
2650 else
2651 eassert (it->current.dpvec_index < 0);
2652 #endif
2653 }
2654
2655
2656 /* Check that the window end of window W is what we expect it
2657 to be---the last row in the current matrix displaying text. */
2658
2659 static void
2660 CHECK_WINDOW_END (struct window *w)
2661 {
2662 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2663 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2664 {
2665 struct glyph_row *row;
2666 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2667 !row->enabled_p
2668 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2669 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2670 }
2671 #endif
2672 }
2673
2674 /***********************************************************************
2675 Iterator initialization
2676 ***********************************************************************/
2677
2678 /* Initialize IT for displaying current_buffer in window W, starting
2679 at character position CHARPOS. CHARPOS < 0 means that no buffer
2680 position is specified which is useful when the iterator is assigned
2681 a position later. BYTEPOS is the byte position corresponding to
2682 CHARPOS.
2683
2684 If ROW is not null, calls to produce_glyphs with IT as parameter
2685 will produce glyphs in that row.
2686
2687 BASE_FACE_ID is the id of a base face to use. It must be one of
2688 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2689 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2690 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2691
2692 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2693 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2694 will be initialized to use the corresponding mode line glyph row of
2695 the desired matrix of W. */
2696
2697 void
2698 init_iterator (struct it *it, struct window *w,
2699 ptrdiff_t charpos, ptrdiff_t bytepos,
2700 struct glyph_row *row, enum face_id base_face_id)
2701 {
2702 enum face_id remapped_base_face_id = base_face_id;
2703
2704 /* Some precondition checks. */
2705 eassert (w != NULL && it != NULL);
2706 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2707 && charpos <= ZV));
2708
2709 /* If face attributes have been changed since the last redisplay,
2710 free realized faces now because they depend on face definitions
2711 that might have changed. Don't free faces while there might be
2712 desired matrices pending which reference these faces. */
2713 if (!inhibit_free_realized_faces)
2714 {
2715 if (face_change)
2716 {
2717 face_change = false;
2718 free_all_realized_faces (Qnil);
2719 }
2720 else if (XFRAME (w->frame)->face_change)
2721 {
2722 XFRAME (w->frame)->face_change = 0;
2723 free_all_realized_faces (w->frame);
2724 }
2725 }
2726
2727 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2728 if (! NILP (Vface_remapping_alist))
2729 remapped_base_face_id
2730 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2731
2732 /* Use one of the mode line rows of W's desired matrix if
2733 appropriate. */
2734 if (row == NULL)
2735 {
2736 if (base_face_id == MODE_LINE_FACE_ID
2737 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2738 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2739 else if (base_face_id == HEADER_LINE_FACE_ID)
2740 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2741 }
2742
2743 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2744 Other parts of redisplay rely on that. */
2745 memclear (it, sizeof *it);
2746 it->current.overlay_string_index = -1;
2747 it->current.dpvec_index = -1;
2748 it->base_face_id = remapped_base_face_id;
2749 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2750 it->paragraph_embedding = L2R;
2751 it->bidi_it.w = w;
2752
2753 /* The window in which we iterate over current_buffer: */
2754 XSETWINDOW (it->window, w);
2755 it->w = w;
2756 it->f = XFRAME (w->frame);
2757
2758 it->cmp_it.id = -1;
2759
2760 /* Extra space between lines (on window systems only). */
2761 if (base_face_id == DEFAULT_FACE_ID
2762 && FRAME_WINDOW_P (it->f))
2763 {
2764 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2765 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2766 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2767 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2768 * FRAME_LINE_HEIGHT (it->f));
2769 else if (it->f->extra_line_spacing > 0)
2770 it->extra_line_spacing = it->f->extra_line_spacing;
2771 }
2772
2773 /* If realized faces have been removed, e.g. because of face
2774 attribute changes of named faces, recompute them. When running
2775 in batch mode, the face cache of the initial frame is null. If
2776 we happen to get called, make a dummy face cache. */
2777 if (FRAME_FACE_CACHE (it->f) == NULL)
2778 init_frame_faces (it->f);
2779 if (FRAME_FACE_CACHE (it->f)->used == 0)
2780 recompute_basic_faces (it->f);
2781
2782 it->override_ascent = -1;
2783
2784 /* Are control characters displayed as `^C'? */
2785 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2786
2787 /* -1 means everything between a CR and the following line end
2788 is invisible. >0 means lines indented more than this value are
2789 invisible. */
2790 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2791 ? (clip_to_bounds
2792 (-1, XINT (BVAR (current_buffer, selective_display)),
2793 PTRDIFF_MAX))
2794 : (!NILP (BVAR (current_buffer, selective_display))
2795 ? -1 : 0));
2796 it->selective_display_ellipsis_p
2797 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2798
2799 /* Display table to use. */
2800 it->dp = window_display_table (w);
2801
2802 /* Are multibyte characters enabled in current_buffer? */
2803 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2804
2805 /* Get the position at which the redisplay_end_trigger hook should
2806 be run, if it is to be run at all. */
2807 if (MARKERP (w->redisplay_end_trigger)
2808 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2809 it->redisplay_end_trigger_charpos
2810 = marker_position (w->redisplay_end_trigger);
2811 else if (INTEGERP (w->redisplay_end_trigger))
2812 it->redisplay_end_trigger_charpos
2813 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2814 PTRDIFF_MAX);
2815
2816 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2817
2818 /* Are lines in the display truncated? */
2819 if (TRUNCATE != 0)
2820 it->line_wrap = TRUNCATE;
2821 if (base_face_id == DEFAULT_FACE_ID
2822 && !it->w->hscroll
2823 && (WINDOW_FULL_WIDTH_P (it->w)
2824 || NILP (Vtruncate_partial_width_windows)
2825 || (INTEGERP (Vtruncate_partial_width_windows)
2826 /* PXW: Shall we do something about this? */
2827 && (XINT (Vtruncate_partial_width_windows)
2828 <= WINDOW_TOTAL_COLS (it->w))))
2829 && NILP (BVAR (current_buffer, truncate_lines)))
2830 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2831 ? WINDOW_WRAP : WORD_WRAP;
2832
2833 /* Get dimensions of truncation and continuation glyphs. These are
2834 displayed as fringe bitmaps under X, but we need them for such
2835 frames when the fringes are turned off. But leave the dimensions
2836 zero for tooltip frames, as these glyphs look ugly there and also
2837 sabotage calculations of tooltip dimensions in x-show-tip. */
2838 #ifdef HAVE_WINDOW_SYSTEM
2839 if (!(FRAME_WINDOW_P (it->f)
2840 && FRAMEP (tip_frame)
2841 && it->f == XFRAME (tip_frame)))
2842 #endif
2843 {
2844 if (it->line_wrap == TRUNCATE)
2845 {
2846 /* We will need the truncation glyph. */
2847 eassert (it->glyph_row == NULL);
2848 produce_special_glyphs (it, IT_TRUNCATION);
2849 it->truncation_pixel_width = it->pixel_width;
2850 }
2851 else
2852 {
2853 /* We will need the continuation glyph. */
2854 eassert (it->glyph_row == NULL);
2855 produce_special_glyphs (it, IT_CONTINUATION);
2856 it->continuation_pixel_width = it->pixel_width;
2857 }
2858 }
2859
2860 /* Reset these values to zero because the produce_special_glyphs
2861 above has changed them. */
2862 it->pixel_width = it->ascent = it->descent = 0;
2863 it->phys_ascent = it->phys_descent = 0;
2864
2865 /* Set this after getting the dimensions of truncation and
2866 continuation glyphs, so that we don't produce glyphs when calling
2867 produce_special_glyphs, above. */
2868 it->glyph_row = row;
2869 it->area = TEXT_AREA;
2870
2871 /* Get the dimensions of the display area. The display area
2872 consists of the visible window area plus a horizontally scrolled
2873 part to the left of the window. All x-values are relative to the
2874 start of this total display area. */
2875 if (base_face_id != DEFAULT_FACE_ID)
2876 {
2877 /* Mode lines, menu bar in terminal frames. */
2878 it->first_visible_x = 0;
2879 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2880 }
2881 else
2882 {
2883 it->first_visible_x
2884 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2885 it->last_visible_x = (it->first_visible_x
2886 + window_box_width (w, TEXT_AREA));
2887
2888 /* If we truncate lines, leave room for the truncation glyph(s) at
2889 the right margin. Otherwise, leave room for the continuation
2890 glyph(s). Done only if the window has no right fringe. */
2891 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2892 {
2893 if (it->line_wrap == TRUNCATE)
2894 it->last_visible_x -= it->truncation_pixel_width;
2895 else
2896 it->last_visible_x -= it->continuation_pixel_width;
2897 }
2898
2899 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2900 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2901 }
2902
2903 /* Leave room for a border glyph. */
2904 if (!FRAME_WINDOW_P (it->f)
2905 && !WINDOW_RIGHTMOST_P (it->w))
2906 it->last_visible_x -= 1;
2907
2908 it->last_visible_y = window_text_bottom_y (w);
2909
2910 /* For mode lines and alike, arrange for the first glyph having a
2911 left box line if the face specifies a box. */
2912 if (base_face_id != DEFAULT_FACE_ID)
2913 {
2914 struct face *face;
2915
2916 it->face_id = remapped_base_face_id;
2917
2918 /* If we have a boxed mode line, make the first character appear
2919 with a left box line. */
2920 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2921 if (face && face->box != FACE_NO_BOX)
2922 it->start_of_box_run_p = true;
2923 }
2924
2925 /* If a buffer position was specified, set the iterator there,
2926 getting overlays and face properties from that position. */
2927 if (charpos >= BUF_BEG (current_buffer))
2928 {
2929 it->stop_charpos = charpos;
2930 it->end_charpos = ZV;
2931 eassert (charpos == BYTE_TO_CHAR (bytepos));
2932 IT_CHARPOS (*it) = charpos;
2933 IT_BYTEPOS (*it) = bytepos;
2934
2935 /* We will rely on `reseat' to set this up properly, via
2936 handle_face_prop. */
2937 it->face_id = it->base_face_id;
2938
2939 it->start = it->current;
2940 /* Do we need to reorder bidirectional text? Not if this is a
2941 unibyte buffer: by definition, none of the single-byte
2942 characters are strong R2L, so no reordering is needed. And
2943 bidi.c doesn't support unibyte buffers anyway. Also, don't
2944 reorder while we are loading loadup.el, since the tables of
2945 character properties needed for reordering are not yet
2946 available. */
2947 it->bidi_p =
2948 NILP (Vpurify_flag)
2949 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2950 && it->multibyte_p;
2951
2952 /* If we are to reorder bidirectional text, init the bidi
2953 iterator. */
2954 if (it->bidi_p)
2955 {
2956 /* Since we don't know at this point whether there will be
2957 any R2L lines in the window, we reserve space for
2958 truncation/continuation glyphs even if only the left
2959 fringe is absent. */
2960 if (base_face_id == DEFAULT_FACE_ID
2961 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2962 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2963 {
2964 if (it->line_wrap == TRUNCATE)
2965 it->last_visible_x -= it->truncation_pixel_width;
2966 else
2967 it->last_visible_x -= it->continuation_pixel_width;
2968 }
2969 /* Note the paragraph direction that this buffer wants to
2970 use. */
2971 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2972 Qleft_to_right))
2973 it->paragraph_embedding = L2R;
2974 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2975 Qright_to_left))
2976 it->paragraph_embedding = R2L;
2977 else
2978 it->paragraph_embedding = NEUTRAL_DIR;
2979 bidi_unshelve_cache (NULL, false);
2980 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2981 &it->bidi_it);
2982 }
2983
2984 /* Compute faces etc. */
2985 reseat (it, it->current.pos, true);
2986 }
2987
2988 CHECK_IT (it);
2989 }
2990
2991
2992 /* Initialize IT for the display of window W with window start POS. */
2993
2994 void
2995 start_display (struct it *it, struct window *w, struct text_pos pos)
2996 {
2997 struct glyph_row *row;
2998 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
2999
3000 row = w->desired_matrix->rows + first_vpos;
3001 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3002 it->first_vpos = first_vpos;
3003
3004 /* Don't reseat to previous visible line start if current start
3005 position is in a string or image. */
3006 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3007 {
3008 int first_y = it->current_y;
3009
3010 /* If window start is not at a line start, skip forward to POS to
3011 get the correct continuation lines width. */
3012 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3013 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3014 if (!start_at_line_beg_p)
3015 {
3016 int new_x;
3017
3018 reseat_at_previous_visible_line_start (it);
3019 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3020
3021 new_x = it->current_x + it->pixel_width;
3022
3023 /* If lines are continued, this line may end in the middle
3024 of a multi-glyph character (e.g. a control character
3025 displayed as \003, or in the middle of an overlay
3026 string). In this case move_it_to above will not have
3027 taken us to the start of the continuation line but to the
3028 end of the continued line. */
3029 if (it->current_x > 0
3030 && it->line_wrap != TRUNCATE /* Lines are continued. */
3031 && (/* And glyph doesn't fit on the line. */
3032 new_x > it->last_visible_x
3033 /* Or it fits exactly and we're on a window
3034 system frame. */
3035 || (new_x == it->last_visible_x
3036 && FRAME_WINDOW_P (it->f)
3037 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3038 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3039 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3040 {
3041 if ((it->current.dpvec_index >= 0
3042 || it->current.overlay_string_index >= 0)
3043 /* If we are on a newline from a display vector or
3044 overlay string, then we are already at the end of
3045 a screen line; no need to go to the next line in
3046 that case, as this line is not really continued.
3047 (If we do go to the next line, C-e will not DTRT.) */
3048 && it->c != '\n')
3049 {
3050 set_iterator_to_next (it, true);
3051 move_it_in_display_line_to (it, -1, -1, 0);
3052 }
3053
3054 it->continuation_lines_width += it->current_x;
3055 }
3056 /* If the character at POS is displayed via a display
3057 vector, move_it_to above stops at the final glyph of
3058 IT->dpvec. To make the caller redisplay that character
3059 again (a.k.a. start at POS), we need to reset the
3060 dpvec_index to the beginning of IT->dpvec. */
3061 else if (it->current.dpvec_index >= 0)
3062 it->current.dpvec_index = 0;
3063
3064 /* We're starting a new display line, not affected by the
3065 height of the continued line, so clear the appropriate
3066 fields in the iterator structure. */
3067 it->max_ascent = it->max_descent = 0;
3068 it->max_phys_ascent = it->max_phys_descent = 0;
3069
3070 it->current_y = first_y;
3071 it->vpos = 0;
3072 it->current_x = it->hpos = 0;
3073 }
3074 }
3075 }
3076
3077
3078 /* Return true if POS is a position in ellipses displayed for invisible
3079 text. W is the window we display, for text property lookup. */
3080
3081 static bool
3082 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3083 {
3084 Lisp_Object prop, window;
3085 bool ellipses_p = false;
3086 ptrdiff_t charpos = CHARPOS (pos->pos);
3087
3088 /* If POS specifies a position in a display vector, this might
3089 be for an ellipsis displayed for invisible text. We won't
3090 get the iterator set up for delivering that ellipsis unless
3091 we make sure that it gets aware of the invisible text. */
3092 if (pos->dpvec_index >= 0
3093 && pos->overlay_string_index < 0
3094 && CHARPOS (pos->string_pos) < 0
3095 && charpos > BEGV
3096 && (XSETWINDOW (window, w),
3097 prop = Fget_char_property (make_number (charpos),
3098 Qinvisible, window),
3099 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3100 {
3101 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3102 window);
3103 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3104 }
3105
3106 return ellipses_p;
3107 }
3108
3109
3110 /* Initialize IT for stepping through current_buffer in window W,
3111 starting at position POS that includes overlay string and display
3112 vector/ control character translation position information. Value
3113 is false if there are overlay strings with newlines at POS. */
3114
3115 static bool
3116 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3117 {
3118 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3119 int i;
3120 bool overlay_strings_with_newlines = false;
3121
3122 /* If POS specifies a position in a display vector, this might
3123 be for an ellipsis displayed for invisible text. We won't
3124 get the iterator set up for delivering that ellipsis unless
3125 we make sure that it gets aware of the invisible text. */
3126 if (in_ellipses_for_invisible_text_p (pos, w))
3127 {
3128 --charpos;
3129 bytepos = 0;
3130 }
3131
3132 /* Keep in mind: the call to reseat in init_iterator skips invisible
3133 text, so we might end up at a position different from POS. This
3134 is only a problem when POS is a row start after a newline and an
3135 overlay starts there with an after-string, and the overlay has an
3136 invisible property. Since we don't skip invisible text in
3137 display_line and elsewhere immediately after consuming the
3138 newline before the row start, such a POS will not be in a string,
3139 but the call to init_iterator below will move us to the
3140 after-string. */
3141 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3142
3143 /* This only scans the current chunk -- it should scan all chunks.
3144 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3145 to 16 in 22.1 to make this a lesser problem. */
3146 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3147 {
3148 const char *s = SSDATA (it->overlay_strings[i]);
3149 const char *e = s + SBYTES (it->overlay_strings[i]);
3150
3151 while (s < e && *s != '\n')
3152 ++s;
3153
3154 if (s < e)
3155 {
3156 overlay_strings_with_newlines = true;
3157 break;
3158 }
3159 }
3160
3161 /* If position is within an overlay string, set up IT to the right
3162 overlay string. */
3163 if (pos->overlay_string_index >= 0)
3164 {
3165 int relative_index;
3166
3167 /* If the first overlay string happens to have a `display'
3168 property for an image, the iterator will be set up for that
3169 image, and we have to undo that setup first before we can
3170 correct the overlay string index. */
3171 if (it->method == GET_FROM_IMAGE)
3172 pop_it (it);
3173
3174 /* We already have the first chunk of overlay strings in
3175 IT->overlay_strings. Load more until the one for
3176 pos->overlay_string_index is in IT->overlay_strings. */
3177 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3178 {
3179 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3180 it->current.overlay_string_index = 0;
3181 while (n--)
3182 {
3183 load_overlay_strings (it, 0);
3184 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3185 }
3186 }
3187
3188 it->current.overlay_string_index = pos->overlay_string_index;
3189 relative_index = (it->current.overlay_string_index
3190 % OVERLAY_STRING_CHUNK_SIZE);
3191 it->string = it->overlay_strings[relative_index];
3192 eassert (STRINGP (it->string));
3193 it->current.string_pos = pos->string_pos;
3194 it->method = GET_FROM_STRING;
3195 it->end_charpos = SCHARS (it->string);
3196 /* Set up the bidi iterator for this overlay string. */
3197 if (it->bidi_p)
3198 {
3199 it->bidi_it.string.lstring = it->string;
3200 it->bidi_it.string.s = NULL;
3201 it->bidi_it.string.schars = SCHARS (it->string);
3202 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3203 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3204 it->bidi_it.string.unibyte = !it->multibyte_p;
3205 it->bidi_it.w = it->w;
3206 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3207 FRAME_WINDOW_P (it->f), &it->bidi_it);
3208
3209 /* Synchronize the state of the bidi iterator with
3210 pos->string_pos. For any string position other than
3211 zero, this will be done automagically when we resume
3212 iteration over the string and get_visually_first_element
3213 is called. But if string_pos is zero, and the string is
3214 to be reordered for display, we need to resync manually,
3215 since it could be that the iteration state recorded in
3216 pos ended at string_pos of 0 moving backwards in string. */
3217 if (CHARPOS (pos->string_pos) == 0)
3218 {
3219 get_visually_first_element (it);
3220 if (IT_STRING_CHARPOS (*it) != 0)
3221 do {
3222 /* Paranoia. */
3223 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3224 bidi_move_to_visually_next (&it->bidi_it);
3225 } while (it->bidi_it.charpos != 0);
3226 }
3227 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3228 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3229 }
3230 }
3231
3232 if (CHARPOS (pos->string_pos) >= 0)
3233 {
3234 /* Recorded position is not in an overlay string, but in another
3235 string. This can only be a string from a `display' property.
3236 IT should already be filled with that string. */
3237 it->current.string_pos = pos->string_pos;
3238 eassert (STRINGP (it->string));
3239 if (it->bidi_p)
3240 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3241 FRAME_WINDOW_P (it->f), &it->bidi_it);
3242 }
3243
3244 /* Restore position in display vector translations, control
3245 character translations or ellipses. */
3246 if (pos->dpvec_index >= 0)
3247 {
3248 if (it->dpvec == NULL)
3249 get_next_display_element (it);
3250 eassert (it->dpvec && it->current.dpvec_index == 0);
3251 it->current.dpvec_index = pos->dpvec_index;
3252 }
3253
3254 CHECK_IT (it);
3255 return !overlay_strings_with_newlines;
3256 }
3257
3258
3259 /* Initialize IT for stepping through current_buffer in window W
3260 starting at ROW->start. */
3261
3262 static void
3263 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3264 {
3265 init_from_display_pos (it, w, &row->start);
3266 it->start = row->start;
3267 it->continuation_lines_width = row->continuation_lines_width;
3268 CHECK_IT (it);
3269 }
3270
3271
3272 /* Initialize IT for stepping through current_buffer in window W
3273 starting in the line following ROW, i.e. starting at ROW->end.
3274 Value is false if there are overlay strings with newlines at ROW's
3275 end position. */
3276
3277 static bool
3278 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3279 {
3280 bool success = false;
3281
3282 if (init_from_display_pos (it, w, &row->end))
3283 {
3284 if (row->continued_p)
3285 it->continuation_lines_width
3286 = row->continuation_lines_width + row->pixel_width;
3287 CHECK_IT (it);
3288 success = true;
3289 }
3290
3291 return success;
3292 }
3293
3294
3295
3296 \f
3297 /***********************************************************************
3298 Text properties
3299 ***********************************************************************/
3300
3301 /* Called when IT reaches IT->stop_charpos. Handle text property and
3302 overlay changes. Set IT->stop_charpos to the next position where
3303 to stop. */
3304
3305 static void
3306 handle_stop (struct it *it)
3307 {
3308 enum prop_handled handled;
3309 bool handle_overlay_change_p;
3310 struct props *p;
3311
3312 it->dpvec = NULL;
3313 it->current.dpvec_index = -1;
3314 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3315 it->ellipsis_p = false;
3316
3317 /* Use face of preceding text for ellipsis (if invisible) */
3318 if (it->selective_display_ellipsis_p)
3319 it->saved_face_id = it->face_id;
3320
3321 /* Here's the description of the semantics of, and the logic behind,
3322 the various HANDLED_* statuses:
3323
3324 HANDLED_NORMALLY means the handler did its job, and the loop
3325 should proceed to calling the next handler in order.
3326
3327 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3328 change in the properties and overlays at current position, so the
3329 loop should be restarted, to re-invoke the handlers that were
3330 already called. This happens when fontification-functions were
3331 called by handle_fontified_prop, and actually fontified
3332 something. Another case where HANDLED_RECOMPUTE_PROPS is
3333 returned is when we discover overlay strings that need to be
3334 displayed right away. The loop below will continue for as long
3335 as the status is HANDLED_RECOMPUTE_PROPS.
3336
3337 HANDLED_RETURN means return immediately to the caller, to
3338 continue iteration without calling any further handlers. This is
3339 used when we need to act on some property right away, for example
3340 when we need to display the ellipsis or a replacing display
3341 property, such as display string or image.
3342
3343 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3344 consumed, and the handler switched to the next overlay string.
3345 This signals the loop below to refrain from looking for more
3346 overlays before all the overlay strings of the current overlay
3347 are processed.
3348
3349 Some of the handlers called by the loop push the iterator state
3350 onto the stack (see 'push_it'), and arrange for the iteration to
3351 continue with another object, such as an image, a display string,
3352 or an overlay string. In most such cases, it->stop_charpos is
3353 set to the first character of the string, so that when the
3354 iteration resumes, this function will immediately be called
3355 again, to examine the properties at the beginning of the string.
3356
3357 When a display or overlay string is exhausted, the iterator state
3358 is popped (see 'pop_it'), and iteration continues with the
3359 previous object. Again, in many such cases this function is
3360 called again to find the next position where properties might
3361 change. */
3362
3363 do
3364 {
3365 handled = HANDLED_NORMALLY;
3366
3367 /* Call text property handlers. */
3368 for (p = it_props; p->handler; ++p)
3369 {
3370 handled = p->handler (it);
3371
3372 if (handled == HANDLED_RECOMPUTE_PROPS)
3373 break;
3374 else if (handled == HANDLED_RETURN)
3375 {
3376 /* We still want to show before and after strings from
3377 overlays even if the actual buffer text is replaced. */
3378 if (!handle_overlay_change_p
3379 || it->sp > 1
3380 /* Don't call get_overlay_strings_1 if we already
3381 have overlay strings loaded, because doing so
3382 will load them again and push the iterator state
3383 onto the stack one more time, which is not
3384 expected by the rest of the code that processes
3385 overlay strings. */
3386 || (it->current.overlay_string_index < 0
3387 && !get_overlay_strings_1 (it, 0, false)))
3388 {
3389 if (it->ellipsis_p)
3390 setup_for_ellipsis (it, 0);
3391 /* When handling a display spec, we might load an
3392 empty string. In that case, discard it here. We
3393 used to discard it in handle_single_display_spec,
3394 but that causes get_overlay_strings_1, above, to
3395 ignore overlay strings that we must check. */
3396 if (STRINGP (it->string) && !SCHARS (it->string))
3397 pop_it (it);
3398 return;
3399 }
3400 else if (STRINGP (it->string) && !SCHARS (it->string))
3401 pop_it (it);
3402 else
3403 {
3404 it->string_from_display_prop_p = false;
3405 it->from_disp_prop_p = false;
3406 handle_overlay_change_p = false;
3407 }
3408 handled = HANDLED_RECOMPUTE_PROPS;
3409 break;
3410 }
3411 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3412 handle_overlay_change_p = false;
3413 }
3414
3415 if (handled != HANDLED_RECOMPUTE_PROPS)
3416 {
3417 /* Don't check for overlay strings below when set to deliver
3418 characters from a display vector. */
3419 if (it->method == GET_FROM_DISPLAY_VECTOR)
3420 handle_overlay_change_p = false;
3421
3422 /* Handle overlay changes.
3423 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3424 if it finds overlays. */
3425 if (handle_overlay_change_p)
3426 handled = handle_overlay_change (it);
3427 }
3428
3429 if (it->ellipsis_p)
3430 {
3431 setup_for_ellipsis (it, 0);
3432 break;
3433 }
3434 }
3435 while (handled == HANDLED_RECOMPUTE_PROPS);
3436
3437 /* Determine where to stop next. */
3438 if (handled == HANDLED_NORMALLY)
3439 compute_stop_pos (it);
3440 }
3441
3442
3443 /* Compute IT->stop_charpos from text property and overlay change
3444 information for IT's current position. */
3445
3446 static void
3447 compute_stop_pos (struct it *it)
3448 {
3449 register INTERVAL iv, next_iv;
3450 Lisp_Object object, limit, position;
3451 ptrdiff_t charpos, bytepos;
3452
3453 if (STRINGP (it->string))
3454 {
3455 /* Strings are usually short, so don't limit the search for
3456 properties. */
3457 it->stop_charpos = it->end_charpos;
3458 object = it->string;
3459 limit = Qnil;
3460 charpos = IT_STRING_CHARPOS (*it);
3461 bytepos = IT_STRING_BYTEPOS (*it);
3462 }
3463 else
3464 {
3465 ptrdiff_t pos;
3466
3467 /* If end_charpos is out of range for some reason, such as a
3468 misbehaving display function, rationalize it (Bug#5984). */
3469 if (it->end_charpos > ZV)
3470 it->end_charpos = ZV;
3471 it->stop_charpos = it->end_charpos;
3472
3473 /* If next overlay change is in front of the current stop pos
3474 (which is IT->end_charpos), stop there. Note: value of
3475 next_overlay_change is point-max if no overlay change
3476 follows. */
3477 charpos = IT_CHARPOS (*it);
3478 bytepos = IT_BYTEPOS (*it);
3479 pos = next_overlay_change (charpos);
3480 if (pos < it->stop_charpos)
3481 it->stop_charpos = pos;
3482
3483 /* Set up variables for computing the stop position from text
3484 property changes. */
3485 XSETBUFFER (object, current_buffer);
3486 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3487 }
3488
3489 /* Get the interval containing IT's position. Value is a null
3490 interval if there isn't such an interval. */
3491 position = make_number (charpos);
3492 iv = validate_interval_range (object, &position, &position, false);
3493 if (iv)
3494 {
3495 Lisp_Object values_here[LAST_PROP_IDX];
3496 struct props *p;
3497
3498 /* Get properties here. */
3499 for (p = it_props; p->handler; ++p)
3500 values_here[p->idx] = textget (iv->plist,
3501 builtin_lisp_symbol (p->name));
3502
3503 /* Look for an interval following iv that has different
3504 properties. */
3505 for (next_iv = next_interval (iv);
3506 (next_iv
3507 && (NILP (limit)
3508 || XFASTINT (limit) > next_iv->position));
3509 next_iv = next_interval (next_iv))
3510 {
3511 for (p = it_props; p->handler; ++p)
3512 {
3513 Lisp_Object new_value = textget (next_iv->plist,
3514 builtin_lisp_symbol (p->name));
3515 if (!EQ (values_here[p->idx], new_value))
3516 break;
3517 }
3518
3519 if (p->handler)
3520 break;
3521 }
3522
3523 if (next_iv)
3524 {
3525 if (INTEGERP (limit)
3526 && next_iv->position >= XFASTINT (limit))
3527 /* No text property change up to limit. */
3528 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3529 else
3530 /* Text properties change in next_iv. */
3531 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3532 }
3533 }
3534
3535 if (it->cmp_it.id < 0)
3536 {
3537 ptrdiff_t stoppos = it->end_charpos;
3538
3539 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3540 stoppos = -1;
3541 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3542 stoppos, it->string);
3543 }
3544
3545 eassert (STRINGP (it->string)
3546 || (it->stop_charpos >= BEGV
3547 && it->stop_charpos >= IT_CHARPOS (*it)));
3548 }
3549
3550
3551 /* Return the position of the next overlay change after POS in
3552 current_buffer. Value is point-max if no overlay change
3553 follows. This is like `next-overlay-change' but doesn't use
3554 xmalloc. */
3555
3556 static ptrdiff_t
3557 next_overlay_change (ptrdiff_t pos)
3558 {
3559 ptrdiff_t i, noverlays;
3560 ptrdiff_t endpos;
3561 Lisp_Object *overlays;
3562 USE_SAFE_ALLOCA;
3563
3564 /* Get all overlays at the given position. */
3565 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3566
3567 /* If any of these overlays ends before endpos,
3568 use its ending point instead. */
3569 for (i = 0; i < noverlays; ++i)
3570 {
3571 Lisp_Object oend;
3572 ptrdiff_t oendpos;
3573
3574 oend = OVERLAY_END (overlays[i]);
3575 oendpos = OVERLAY_POSITION (oend);
3576 endpos = min (endpos, oendpos);
3577 }
3578
3579 SAFE_FREE ();
3580 return endpos;
3581 }
3582
3583 /* How many characters forward to search for a display property or
3584 display string. Searching too far forward makes the bidi display
3585 sluggish, especially in small windows. */
3586 #define MAX_DISP_SCAN 250
3587
3588 /* Return the character position of a display string at or after
3589 position specified by POSITION. If no display string exists at or
3590 after POSITION, return ZV. A display string is either an overlay
3591 with `display' property whose value is a string, or a `display'
3592 text property whose value is a string. STRING is data about the
3593 string to iterate; if STRING->lstring is nil, we are iterating a
3594 buffer. FRAME_WINDOW_P is true when we are displaying a window
3595 on a GUI frame. DISP_PROP is set to zero if we searched
3596 MAX_DISP_SCAN characters forward without finding any display
3597 strings, non-zero otherwise. It is set to 2 if the display string
3598 uses any kind of `(space ...)' spec that will produce a stretch of
3599 white space in the text area. */
3600 ptrdiff_t
3601 compute_display_string_pos (struct text_pos *position,
3602 struct bidi_string_data *string,
3603 struct window *w,
3604 bool frame_window_p, int *disp_prop)
3605 {
3606 /* OBJECT = nil means current buffer. */
3607 Lisp_Object object, object1;
3608 Lisp_Object pos, spec, limpos;
3609 bool string_p = string && (STRINGP (string->lstring) || string->s);
3610 ptrdiff_t eob = string_p ? string->schars : ZV;
3611 ptrdiff_t begb = string_p ? 0 : BEGV;
3612 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3613 ptrdiff_t lim =
3614 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3615 struct text_pos tpos;
3616 int rv = 0;
3617
3618 if (string && STRINGP (string->lstring))
3619 object1 = object = string->lstring;
3620 else if (w && !string_p)
3621 {
3622 XSETWINDOW (object, w);
3623 object1 = Qnil;
3624 }
3625 else
3626 object1 = object = Qnil;
3627
3628 *disp_prop = 1;
3629
3630 if (charpos >= eob
3631 /* We don't support display properties whose values are strings
3632 that have display string properties. */
3633 || string->from_disp_str
3634 /* C strings cannot have display properties. */
3635 || (string->s && !STRINGP (object)))
3636 {
3637 *disp_prop = 0;
3638 return eob;
3639 }
3640
3641 /* If the character at CHARPOS is where the display string begins,
3642 return CHARPOS. */
3643 pos = make_number (charpos);
3644 if (STRINGP (object))
3645 bufpos = string->bufpos;
3646 else
3647 bufpos = charpos;
3648 tpos = *position;
3649 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3650 && (charpos <= begb
3651 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3652 object),
3653 spec))
3654 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3655 frame_window_p)))
3656 {
3657 if (rv == 2)
3658 *disp_prop = 2;
3659 return charpos;
3660 }
3661
3662 /* Look forward for the first character with a `display' property
3663 that will replace the underlying text when displayed. */
3664 limpos = make_number (lim);
3665 do {
3666 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3667 CHARPOS (tpos) = XFASTINT (pos);
3668 if (CHARPOS (tpos) >= lim)
3669 {
3670 *disp_prop = 0;
3671 break;
3672 }
3673 if (STRINGP (object))
3674 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3675 else
3676 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3677 spec = Fget_char_property (pos, Qdisplay, object);
3678 if (!STRINGP (object))
3679 bufpos = CHARPOS (tpos);
3680 } while (NILP (spec)
3681 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3682 bufpos, frame_window_p)));
3683 if (rv == 2)
3684 *disp_prop = 2;
3685
3686 return CHARPOS (tpos);
3687 }
3688
3689 /* Return the character position of the end of the display string that
3690 started at CHARPOS. If there's no display string at CHARPOS,
3691 return -1. A display string is either an overlay with `display'
3692 property whose value is a string or a `display' text property whose
3693 value is a string. */
3694 ptrdiff_t
3695 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3696 {
3697 /* OBJECT = nil means current buffer. */
3698 Lisp_Object object =
3699 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3700 Lisp_Object pos = make_number (charpos);
3701 ptrdiff_t eob =
3702 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3703
3704 if (charpos >= eob || (string->s && !STRINGP (object)))
3705 return eob;
3706
3707 /* It could happen that the display property or overlay was removed
3708 since we found it in compute_display_string_pos above. One way
3709 this can happen is if JIT font-lock was called (through
3710 handle_fontified_prop), and jit-lock-functions remove text
3711 properties or overlays from the portion of buffer that includes
3712 CHARPOS. Muse mode is known to do that, for example. In this
3713 case, we return -1 to the caller, to signal that no display
3714 string is actually present at CHARPOS. See bidi_fetch_char for
3715 how this is handled.
3716
3717 An alternative would be to never look for display properties past
3718 it->stop_charpos. But neither compute_display_string_pos nor
3719 bidi_fetch_char that calls it know or care where the next
3720 stop_charpos is. */
3721 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3722 return -1;
3723
3724 /* Look forward for the first character where the `display' property
3725 changes. */
3726 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3727
3728 return XFASTINT (pos);
3729 }
3730
3731
3732 \f
3733 /***********************************************************************
3734 Fontification
3735 ***********************************************************************/
3736
3737 /* Handle changes in the `fontified' property of the current buffer by
3738 calling hook functions from Qfontification_functions to fontify
3739 regions of text. */
3740
3741 static enum prop_handled
3742 handle_fontified_prop (struct it *it)
3743 {
3744 Lisp_Object prop, pos;
3745 enum prop_handled handled = HANDLED_NORMALLY;
3746
3747 if (!NILP (Vmemory_full))
3748 return handled;
3749
3750 /* Get the value of the `fontified' property at IT's current buffer
3751 position. (The `fontified' property doesn't have a special
3752 meaning in strings.) If the value is nil, call functions from
3753 Qfontification_functions. */
3754 if (!STRINGP (it->string)
3755 && it->s == NULL
3756 && !NILP (Vfontification_functions)
3757 && !NILP (Vrun_hooks)
3758 && (pos = make_number (IT_CHARPOS (*it)),
3759 prop = Fget_char_property (pos, Qfontified, Qnil),
3760 /* Ignore the special cased nil value always present at EOB since
3761 no amount of fontifying will be able to change it. */
3762 NILP (prop) && IT_CHARPOS (*it) < Z))
3763 {
3764 ptrdiff_t count = SPECPDL_INDEX ();
3765 Lisp_Object val;
3766 struct buffer *obuf = current_buffer;
3767 ptrdiff_t begv = BEGV, zv = ZV;
3768 bool old_clip_changed = current_buffer->clip_changed;
3769
3770 val = Vfontification_functions;
3771 specbind (Qfontification_functions, Qnil);
3772
3773 eassert (it->end_charpos == ZV);
3774
3775 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3776 safe_call1 (val, pos);
3777 else
3778 {
3779 Lisp_Object fns, fn;
3780
3781 fns = Qnil;
3782
3783 for (; CONSP (val); val = XCDR (val))
3784 {
3785 fn = XCAR (val);
3786
3787 if (EQ (fn, Qt))
3788 {
3789 /* A value of t indicates this hook has a local
3790 binding; it means to run the global binding too.
3791 In a global value, t should not occur. If it
3792 does, we must ignore it to avoid an endless
3793 loop. */
3794 for (fns = Fdefault_value (Qfontification_functions);
3795 CONSP (fns);
3796 fns = XCDR (fns))
3797 {
3798 fn = XCAR (fns);
3799 if (!EQ (fn, Qt))
3800 safe_call1 (fn, pos);
3801 }
3802 }
3803 else
3804 safe_call1 (fn, pos);
3805 }
3806 }
3807
3808 unbind_to (count, Qnil);
3809
3810 /* Fontification functions routinely call `save-restriction'.
3811 Normally, this tags clip_changed, which can confuse redisplay
3812 (see discussion in Bug#6671). Since we don't perform any
3813 special handling of fontification changes in the case where
3814 `save-restriction' isn't called, there's no point doing so in
3815 this case either. So, if the buffer's restrictions are
3816 actually left unchanged, reset clip_changed. */
3817 if (obuf == current_buffer)
3818 {
3819 if (begv == BEGV && zv == ZV)
3820 current_buffer->clip_changed = old_clip_changed;
3821 }
3822 /* There isn't much we can reasonably do to protect against
3823 misbehaving fontification, but here's a fig leaf. */
3824 else if (BUFFER_LIVE_P (obuf))
3825 set_buffer_internal_1 (obuf);
3826
3827 /* The fontification code may have added/removed text.
3828 It could do even a lot worse, but let's at least protect against
3829 the most obvious case where only the text past `pos' gets changed',
3830 as is/was done in grep.el where some escapes sequences are turned
3831 into face properties (bug#7876). */
3832 it->end_charpos = ZV;
3833
3834 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3835 something. This avoids an endless loop if they failed to
3836 fontify the text for which reason ever. */
3837 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3838 handled = HANDLED_RECOMPUTE_PROPS;
3839 }
3840
3841 return handled;
3842 }
3843
3844
3845 \f
3846 /***********************************************************************
3847 Faces
3848 ***********************************************************************/
3849
3850 /* Set up iterator IT from face properties at its current position.
3851 Called from handle_stop. */
3852
3853 static enum prop_handled
3854 handle_face_prop (struct it *it)
3855 {
3856 int new_face_id;
3857 ptrdiff_t next_stop;
3858
3859 if (!STRINGP (it->string))
3860 {
3861 new_face_id
3862 = face_at_buffer_position (it->w,
3863 IT_CHARPOS (*it),
3864 &next_stop,
3865 (IT_CHARPOS (*it)
3866 + TEXT_PROP_DISTANCE_LIMIT),
3867 false, it->base_face_id);
3868
3869 /* Is this a start of a run of characters with box face?
3870 Caveat: this can be called for a freshly initialized
3871 iterator; face_id is -1 in this case. We know that the new
3872 face will not change until limit, i.e. if the new face has a
3873 box, all characters up to limit will have one. But, as
3874 usual, we don't know whether limit is really the end. */
3875 if (new_face_id != it->face_id)
3876 {
3877 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3878 /* If it->face_id is -1, old_face below will be NULL, see
3879 the definition of FACE_FROM_ID. This will happen if this
3880 is the initial call that gets the face. */
3881 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3882
3883 /* If the value of face_id of the iterator is -1, we have to
3884 look in front of IT's position and see whether there is a
3885 face there that's different from new_face_id. */
3886 if (!old_face && IT_CHARPOS (*it) > BEG)
3887 {
3888 int prev_face_id = face_before_it_pos (it);
3889
3890 old_face = FACE_FROM_ID (it->f, prev_face_id);
3891 }
3892
3893 /* If the new face has a box, but the old face does not,
3894 this is the start of a run of characters with box face,
3895 i.e. this character has a shadow on the left side. */
3896 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3897 && (old_face == NULL || !old_face->box));
3898 it->face_box_p = new_face->box != FACE_NO_BOX;
3899 }
3900 }
3901 else
3902 {
3903 int base_face_id;
3904 ptrdiff_t bufpos;
3905 int i;
3906 Lisp_Object from_overlay
3907 = (it->current.overlay_string_index >= 0
3908 ? it->string_overlays[it->current.overlay_string_index
3909 % OVERLAY_STRING_CHUNK_SIZE]
3910 : Qnil);
3911
3912 /* See if we got to this string directly or indirectly from
3913 an overlay property. That includes the before-string or
3914 after-string of an overlay, strings in display properties
3915 provided by an overlay, their text properties, etc.
3916
3917 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3918 if (! NILP (from_overlay))
3919 for (i = it->sp - 1; i >= 0; i--)
3920 {
3921 if (it->stack[i].current.overlay_string_index >= 0)
3922 from_overlay
3923 = it->string_overlays[it->stack[i].current.overlay_string_index
3924 % OVERLAY_STRING_CHUNK_SIZE];
3925 else if (! NILP (it->stack[i].from_overlay))
3926 from_overlay = it->stack[i].from_overlay;
3927
3928 if (!NILP (from_overlay))
3929 break;
3930 }
3931
3932 if (! NILP (from_overlay))
3933 {
3934 bufpos = IT_CHARPOS (*it);
3935 /* For a string from an overlay, the base face depends
3936 only on text properties and ignores overlays. */
3937 base_face_id
3938 = face_for_overlay_string (it->w,
3939 IT_CHARPOS (*it),
3940 &next_stop,
3941 (IT_CHARPOS (*it)
3942 + TEXT_PROP_DISTANCE_LIMIT),
3943 false,
3944 from_overlay);
3945 }
3946 else
3947 {
3948 bufpos = 0;
3949
3950 /* For strings from a `display' property, use the face at
3951 IT's current buffer position as the base face to merge
3952 with, so that overlay strings appear in the same face as
3953 surrounding text, unless they specify their own faces.
3954 For strings from wrap-prefix and line-prefix properties,
3955 use the default face, possibly remapped via
3956 Vface_remapping_alist. */
3957 /* Note that the fact that we use the face at _buffer_
3958 position means that a 'display' property on an overlay
3959 string will not inherit the face of that overlay string,
3960 but will instead revert to the face of buffer text
3961 covered by the overlay. This is visible, e.g., when the
3962 overlay specifies a box face, but neither the buffer nor
3963 the display string do. This sounds like a design bug,
3964 but Emacs always did that since v21.1, so changing that
3965 might be a big deal. */
3966 base_face_id = it->string_from_prefix_prop_p
3967 ? (!NILP (Vface_remapping_alist)
3968 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3969 : DEFAULT_FACE_ID)
3970 : underlying_face_id (it);
3971 }
3972
3973 new_face_id = face_at_string_position (it->w,
3974 it->string,
3975 IT_STRING_CHARPOS (*it),
3976 bufpos,
3977 &next_stop,
3978 base_face_id, false);
3979
3980 /* Is this a start of a run of characters with box? Caveat:
3981 this can be called for a freshly allocated iterator; face_id
3982 is -1 is this case. We know that the new face will not
3983 change until the next check pos, i.e. if the new face has a
3984 box, all characters up to that position will have a
3985 box. But, as usual, we don't know whether that position
3986 is really the end. */
3987 if (new_face_id != it->face_id)
3988 {
3989 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3990 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3991
3992 /* If new face has a box but old face hasn't, this is the
3993 start of a run of characters with box, i.e. it has a
3994 shadow on the left side. */
3995 it->start_of_box_run_p
3996 = new_face->box && (old_face == NULL || !old_face->box);
3997 it->face_box_p = new_face->box != FACE_NO_BOX;
3998 }
3999 }
4000
4001 it->face_id = new_face_id;
4002 return HANDLED_NORMALLY;
4003 }
4004
4005
4006 /* Return the ID of the face ``underlying'' IT's current position,
4007 which is in a string. If the iterator is associated with a
4008 buffer, return the face at IT's current buffer position.
4009 Otherwise, use the iterator's base_face_id. */
4010
4011 static int
4012 underlying_face_id (struct it *it)
4013 {
4014 int face_id = it->base_face_id, i;
4015
4016 eassert (STRINGP (it->string));
4017
4018 for (i = it->sp - 1; i >= 0; --i)
4019 if (NILP (it->stack[i].string))
4020 face_id = it->stack[i].face_id;
4021
4022 return face_id;
4023 }
4024
4025
4026 /* Compute the face one character before or after the current position
4027 of IT, in the visual order. BEFORE_P means get the face
4028 in front (to the left in L2R paragraphs, to the right in R2L
4029 paragraphs) of IT's screen position. Value is the ID of the face. */
4030
4031 static int
4032 face_before_or_after_it_pos (struct it *it, bool before_p)
4033 {
4034 int face_id, limit;
4035 ptrdiff_t next_check_charpos;
4036 struct it it_copy;
4037 void *it_copy_data = NULL;
4038
4039 eassert (it->s == NULL);
4040
4041 if (STRINGP (it->string))
4042 {
4043 ptrdiff_t bufpos, charpos;
4044 int base_face_id;
4045
4046 /* No face change past the end of the string (for the case
4047 we are padding with spaces). No face change before the
4048 string start. */
4049 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4050 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4051 return it->face_id;
4052
4053 if (!it->bidi_p)
4054 {
4055 /* Set charpos to the position before or after IT's current
4056 position, in the logical order, which in the non-bidi
4057 case is the same as the visual order. */
4058 if (before_p)
4059 charpos = IT_STRING_CHARPOS (*it) - 1;
4060 else if (it->what == IT_COMPOSITION)
4061 /* For composition, we must check the character after the
4062 composition. */
4063 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4064 else
4065 charpos = IT_STRING_CHARPOS (*it) + 1;
4066 }
4067 else
4068 {
4069 if (before_p)
4070 {
4071 /* With bidi iteration, the character before the current
4072 in the visual order cannot be found by simple
4073 iteration, because "reverse" reordering is not
4074 supported. Instead, we need to start from the string
4075 beginning and go all the way to the current string
4076 position, remembering the previous position. */
4077 /* Ignore face changes before the first visible
4078 character on this display line. */
4079 if (it->current_x <= it->first_visible_x)
4080 return it->face_id;
4081 SAVE_IT (it_copy, *it, it_copy_data);
4082 IT_STRING_CHARPOS (it_copy) = 0;
4083 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4084
4085 do
4086 {
4087 charpos = IT_STRING_CHARPOS (it_copy);
4088 if (charpos >= SCHARS (it->string))
4089 break;
4090 bidi_move_to_visually_next (&it_copy.bidi_it);
4091 }
4092 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4093
4094 RESTORE_IT (it, it, it_copy_data);
4095 }
4096 else
4097 {
4098 /* Set charpos to the string position of the character
4099 that comes after IT's current position in the visual
4100 order. */
4101 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4102
4103 it_copy = *it;
4104 while (n--)
4105 bidi_move_to_visually_next (&it_copy.bidi_it);
4106
4107 charpos = it_copy.bidi_it.charpos;
4108 }
4109 }
4110 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4111
4112 if (it->current.overlay_string_index >= 0)
4113 bufpos = IT_CHARPOS (*it);
4114 else
4115 bufpos = 0;
4116
4117 base_face_id = underlying_face_id (it);
4118
4119 /* Get the face for ASCII, or unibyte. */
4120 face_id = face_at_string_position (it->w,
4121 it->string,
4122 charpos,
4123 bufpos,
4124 &next_check_charpos,
4125 base_face_id, false);
4126
4127 /* Correct the face for charsets different from ASCII. Do it
4128 for the multibyte case only. The face returned above is
4129 suitable for unibyte text if IT->string is unibyte. */
4130 if (STRING_MULTIBYTE (it->string))
4131 {
4132 struct text_pos pos1 = string_pos (charpos, it->string);
4133 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4134 int c, len;
4135 struct face *face = FACE_FROM_ID (it->f, face_id);
4136
4137 c = string_char_and_length (p, &len);
4138 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4139 }
4140 }
4141 else
4142 {
4143 struct text_pos pos;
4144
4145 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4146 || (IT_CHARPOS (*it) <= BEGV && before_p))
4147 return it->face_id;
4148
4149 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4150 pos = it->current.pos;
4151
4152 if (!it->bidi_p)
4153 {
4154 if (before_p)
4155 DEC_TEXT_POS (pos, it->multibyte_p);
4156 else
4157 {
4158 if (it->what == IT_COMPOSITION)
4159 {
4160 /* For composition, we must check the position after
4161 the composition. */
4162 pos.charpos += it->cmp_it.nchars;
4163 pos.bytepos += it->len;
4164 }
4165 else
4166 INC_TEXT_POS (pos, it->multibyte_p);
4167 }
4168 }
4169 else
4170 {
4171 if (before_p)
4172 {
4173 int current_x;
4174
4175 /* With bidi iteration, the character before the current
4176 in the visual order cannot be found by simple
4177 iteration, because "reverse" reordering is not
4178 supported. Instead, we need to use the move_it_*
4179 family of functions, and move to the previous
4180 character starting from the beginning of the visual
4181 line. */
4182 /* Ignore face changes before the first visible
4183 character on this display line. */
4184 if (it->current_x <= it->first_visible_x)
4185 return it->face_id;
4186 SAVE_IT (it_copy, *it, it_copy_data);
4187 /* Implementation note: Since move_it_in_display_line
4188 works in the iterator geometry, and thinks the first
4189 character is always the leftmost, even in R2L lines,
4190 we don't need to distinguish between the R2L and L2R
4191 cases here. */
4192 current_x = it_copy.current_x;
4193 move_it_vertically_backward (&it_copy, 0);
4194 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4195 pos = it_copy.current.pos;
4196 RESTORE_IT (it, it, it_copy_data);
4197 }
4198 else
4199 {
4200 /* Set charpos to the buffer position of the character
4201 that comes after IT's current position in the visual
4202 order. */
4203 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4204
4205 it_copy = *it;
4206 while (n--)
4207 bidi_move_to_visually_next (&it_copy.bidi_it);
4208
4209 SET_TEXT_POS (pos,
4210 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4211 }
4212 }
4213 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4214
4215 /* Determine face for CHARSET_ASCII, or unibyte. */
4216 face_id = face_at_buffer_position (it->w,
4217 CHARPOS (pos),
4218 &next_check_charpos,
4219 limit, false, -1);
4220
4221 /* Correct the face for charsets different from ASCII. Do it
4222 for the multibyte case only. The face returned above is
4223 suitable for unibyte text if current_buffer is unibyte. */
4224 if (it->multibyte_p)
4225 {
4226 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4227 struct face *face = FACE_FROM_ID (it->f, face_id);
4228 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4229 }
4230 }
4231
4232 return face_id;
4233 }
4234
4235
4236 \f
4237 /***********************************************************************
4238 Invisible text
4239 ***********************************************************************/
4240
4241 /* Set up iterator IT from invisible properties at its current
4242 position. Called from handle_stop. */
4243
4244 static enum prop_handled
4245 handle_invisible_prop (struct it *it)
4246 {
4247 enum prop_handled handled = HANDLED_NORMALLY;
4248 int invis;
4249 Lisp_Object prop;
4250
4251 if (STRINGP (it->string))
4252 {
4253 Lisp_Object end_charpos, limit;
4254
4255 /* Get the value of the invisible text property at the
4256 current position. Value will be nil if there is no such
4257 property. */
4258 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4259 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4260 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4261
4262 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4263 {
4264 /* Record whether we have to display an ellipsis for the
4265 invisible text. */
4266 bool display_ellipsis_p = (invis == 2);
4267 ptrdiff_t len, endpos;
4268
4269 handled = HANDLED_RECOMPUTE_PROPS;
4270
4271 /* Get the position at which the next visible text can be
4272 found in IT->string, if any. */
4273 endpos = len = SCHARS (it->string);
4274 XSETINT (limit, len);
4275 do
4276 {
4277 end_charpos
4278 = Fnext_single_property_change (end_charpos, Qinvisible,
4279 it->string, limit);
4280 /* Since LIMIT is always an integer, so should be the
4281 value returned by Fnext_single_property_change. */
4282 eassert (INTEGERP (end_charpos));
4283 if (INTEGERP (end_charpos))
4284 {
4285 endpos = XFASTINT (end_charpos);
4286 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4287 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4288 if (invis == 2)
4289 display_ellipsis_p = true;
4290 }
4291 else /* Should never happen; but if it does, exit the loop. */
4292 endpos = len;
4293 }
4294 while (invis != 0 && endpos < len);
4295
4296 if (display_ellipsis_p)
4297 it->ellipsis_p = true;
4298
4299 if (endpos < len)
4300 {
4301 /* Text at END_CHARPOS is visible. Move IT there. */
4302 struct text_pos old;
4303 ptrdiff_t oldpos;
4304
4305 old = it->current.string_pos;
4306 oldpos = CHARPOS (old);
4307 if (it->bidi_p)
4308 {
4309 if (it->bidi_it.first_elt
4310 && it->bidi_it.charpos < SCHARS (it->string))
4311 bidi_paragraph_init (it->paragraph_embedding,
4312 &it->bidi_it, true);
4313 /* Bidi-iterate out of the invisible text. */
4314 do
4315 {
4316 bidi_move_to_visually_next (&it->bidi_it);
4317 }
4318 while (oldpos <= it->bidi_it.charpos
4319 && it->bidi_it.charpos < endpos);
4320
4321 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4322 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4323 if (IT_CHARPOS (*it) >= endpos)
4324 it->prev_stop = endpos;
4325 }
4326 else
4327 {
4328 IT_STRING_CHARPOS (*it) = endpos;
4329 compute_string_pos (&it->current.string_pos, old, it->string);
4330 }
4331 }
4332 else
4333 {
4334 /* The rest of the string is invisible. If this is an
4335 overlay string, proceed with the next overlay string
4336 or whatever comes and return a character from there. */
4337 if (it->current.overlay_string_index >= 0
4338 && !display_ellipsis_p)
4339 {
4340 next_overlay_string (it);
4341 /* Don't check for overlay strings when we just
4342 finished processing them. */
4343 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4344 }
4345 else
4346 {
4347 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4348 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4349 }
4350 }
4351 }
4352 }
4353 else
4354 {
4355 ptrdiff_t newpos, next_stop, start_charpos, tem;
4356 Lisp_Object pos, overlay;
4357
4358 /* First of all, is there invisible text at this position? */
4359 tem = start_charpos = IT_CHARPOS (*it);
4360 pos = make_number (tem);
4361 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4362 &overlay);
4363 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4364
4365 /* If we are on invisible text, skip over it. */
4366 if (invis != 0 && start_charpos < it->end_charpos)
4367 {
4368 /* Record whether we have to display an ellipsis for the
4369 invisible text. */
4370 bool display_ellipsis_p = invis == 2;
4371
4372 handled = HANDLED_RECOMPUTE_PROPS;
4373
4374 /* Loop skipping over invisible text. The loop is left at
4375 ZV or with IT on the first char being visible again. */
4376 do
4377 {
4378 /* Try to skip some invisible text. Return value is the
4379 position reached which can be equal to where we start
4380 if there is nothing invisible there. This skips both
4381 over invisible text properties and overlays with
4382 invisible property. */
4383 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4384
4385 /* If we skipped nothing at all we weren't at invisible
4386 text in the first place. If everything to the end of
4387 the buffer was skipped, end the loop. */
4388 if (newpos == tem || newpos >= ZV)
4389 invis = 0;
4390 else
4391 {
4392 /* We skipped some characters but not necessarily
4393 all there are. Check if we ended up on visible
4394 text. Fget_char_property returns the property of
4395 the char before the given position, i.e. if we
4396 get invis = 0, this means that the char at
4397 newpos is visible. */
4398 pos = make_number (newpos);
4399 prop = Fget_char_property (pos, Qinvisible, it->window);
4400 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4401 }
4402
4403 /* If we ended up on invisible text, proceed to
4404 skip starting with next_stop. */
4405 if (invis != 0)
4406 tem = next_stop;
4407
4408 /* If there are adjacent invisible texts, don't lose the
4409 second one's ellipsis. */
4410 if (invis == 2)
4411 display_ellipsis_p = true;
4412 }
4413 while (invis != 0);
4414
4415 /* The position newpos is now either ZV or on visible text. */
4416 if (it->bidi_p)
4417 {
4418 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4419 bool on_newline
4420 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4421 bool after_newline
4422 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4423
4424 /* If the invisible text ends on a newline or on a
4425 character after a newline, we can avoid the costly,
4426 character by character, bidi iteration to NEWPOS, and
4427 instead simply reseat the iterator there. That's
4428 because all bidi reordering information is tossed at
4429 the newline. This is a big win for modes that hide
4430 complete lines, like Outline, Org, etc. */
4431 if (on_newline || after_newline)
4432 {
4433 struct text_pos tpos;
4434 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4435
4436 SET_TEXT_POS (tpos, newpos, bpos);
4437 reseat_1 (it, tpos, false);
4438 /* If we reseat on a newline/ZV, we need to prep the
4439 bidi iterator for advancing to the next character
4440 after the newline/EOB, keeping the current paragraph
4441 direction (so that PRODUCE_GLYPHS does TRT wrt
4442 prepending/appending glyphs to a glyph row). */
4443 if (on_newline)
4444 {
4445 it->bidi_it.first_elt = false;
4446 it->bidi_it.paragraph_dir = pdir;
4447 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4448 it->bidi_it.nchars = 1;
4449 it->bidi_it.ch_len = 1;
4450 }
4451 }
4452 else /* Must use the slow method. */
4453 {
4454 /* With bidi iteration, the region of invisible text
4455 could start and/or end in the middle of a
4456 non-base embedding level. Therefore, we need to
4457 skip invisible text using the bidi iterator,
4458 starting at IT's current position, until we find
4459 ourselves outside of the invisible text.
4460 Skipping invisible text _after_ bidi iteration
4461 avoids affecting the visual order of the
4462 displayed text when invisible properties are
4463 added or removed. */
4464 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4465 {
4466 /* If we were `reseat'ed to a new paragraph,
4467 determine the paragraph base direction. We
4468 need to do it now because
4469 next_element_from_buffer may not have a
4470 chance to do it, if we are going to skip any
4471 text at the beginning, which resets the
4472 FIRST_ELT flag. */
4473 bidi_paragraph_init (it->paragraph_embedding,
4474 &it->bidi_it, true);
4475 }
4476 do
4477 {
4478 bidi_move_to_visually_next (&it->bidi_it);
4479 }
4480 while (it->stop_charpos <= it->bidi_it.charpos
4481 && it->bidi_it.charpos < newpos);
4482 IT_CHARPOS (*it) = it->bidi_it.charpos;
4483 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4484 /* If we overstepped NEWPOS, record its position in
4485 the iterator, so that we skip invisible text if
4486 later the bidi iteration lands us in the
4487 invisible region again. */
4488 if (IT_CHARPOS (*it) >= newpos)
4489 it->prev_stop = newpos;
4490 }
4491 }
4492 else
4493 {
4494 IT_CHARPOS (*it) = newpos;
4495 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4496 }
4497
4498 if (display_ellipsis_p)
4499 {
4500 /* Make sure that the glyphs of the ellipsis will get
4501 correct `charpos' values. If we would not update
4502 it->position here, the glyphs would belong to the
4503 last visible character _before_ the invisible
4504 text, which confuses `set_cursor_from_row'.
4505
4506 We use the last invisible position instead of the
4507 first because this way the cursor is always drawn on
4508 the first "." of the ellipsis, whenever PT is inside
4509 the invisible text. Otherwise the cursor would be
4510 placed _after_ the ellipsis when the point is after the
4511 first invisible character. */
4512 if (!STRINGP (it->object))
4513 {
4514 it->position.charpos = newpos - 1;
4515 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4516 }
4517 }
4518
4519 /* If there are before-strings at the start of invisible
4520 text, and the text is invisible because of a text
4521 property, arrange to show before-strings because 20.x did
4522 it that way. (If the text is invisible because of an
4523 overlay property instead of a text property, this is
4524 already handled in the overlay code.) */
4525 if (NILP (overlay)
4526 && get_overlay_strings (it, it->stop_charpos))
4527 {
4528 handled = HANDLED_RECOMPUTE_PROPS;
4529 if (it->sp > 0)
4530 {
4531 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4532 /* The call to get_overlay_strings above recomputes
4533 it->stop_charpos, but it only considers changes
4534 in properties and overlays beyond iterator's
4535 current position. This causes us to miss changes
4536 that happen exactly where the invisible property
4537 ended. So we play it safe here and force the
4538 iterator to check for potential stop positions
4539 immediately after the invisible text. Note that
4540 if get_overlay_strings returns true, it
4541 normally also pushed the iterator stack, so we
4542 need to update the stop position in the slot
4543 below the current one. */
4544 it->stack[it->sp - 1].stop_charpos
4545 = CHARPOS (it->stack[it->sp - 1].current.pos);
4546 }
4547 }
4548 else if (display_ellipsis_p)
4549 {
4550 it->ellipsis_p = true;
4551 /* Let the ellipsis display before
4552 considering any properties of the following char.
4553 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4554 handled = HANDLED_RETURN;
4555 }
4556 }
4557 }
4558
4559 return handled;
4560 }
4561
4562
4563 /* Make iterator IT return `...' next.
4564 Replaces LEN characters from buffer. */
4565
4566 static void
4567 setup_for_ellipsis (struct it *it, int len)
4568 {
4569 /* Use the display table definition for `...'. Invalid glyphs
4570 will be handled by the method returning elements from dpvec. */
4571 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4572 {
4573 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4574 it->dpvec = v->contents;
4575 it->dpend = v->contents + v->header.size;
4576 }
4577 else
4578 {
4579 /* Default `...'. */
4580 it->dpvec = default_invis_vector;
4581 it->dpend = default_invis_vector + 3;
4582 }
4583
4584 it->dpvec_char_len = len;
4585 it->current.dpvec_index = 0;
4586 it->dpvec_face_id = -1;
4587
4588 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4589 face as the preceding text. IT->saved_face_id was set in
4590 handle_stop to the face of the preceding character, and will be
4591 different from IT->face_id only if the invisible text skipped in
4592 handle_invisible_prop has some non-default face on its first
4593 character. We thus ignore the face of the invisible text when we
4594 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4595 if (it->saved_face_id >= 0)
4596 it->face_id = it->saved_face_id;
4597
4598 /* If the ellipsis represents buffer text, it means we advanced in
4599 the buffer, so we should no longer ignore overlay strings. */
4600 if (it->method == GET_FROM_BUFFER)
4601 it->ignore_overlay_strings_at_pos_p = false;
4602
4603 it->method = GET_FROM_DISPLAY_VECTOR;
4604 it->ellipsis_p = true;
4605 }
4606
4607
4608 \f
4609 /***********************************************************************
4610 'display' property
4611 ***********************************************************************/
4612
4613 /* Set up iterator IT from `display' property at its current position.
4614 Called from handle_stop.
4615 We return HANDLED_RETURN if some part of the display property
4616 overrides the display of the buffer text itself.
4617 Otherwise we return HANDLED_NORMALLY. */
4618
4619 static enum prop_handled
4620 handle_display_prop (struct it *it)
4621 {
4622 Lisp_Object propval, object, overlay;
4623 struct text_pos *position;
4624 ptrdiff_t bufpos;
4625 /* Nonzero if some property replaces the display of the text itself. */
4626 int display_replaced = 0;
4627
4628 if (STRINGP (it->string))
4629 {
4630 object = it->string;
4631 position = &it->current.string_pos;
4632 bufpos = CHARPOS (it->current.pos);
4633 }
4634 else
4635 {
4636 XSETWINDOW (object, it->w);
4637 position = &it->current.pos;
4638 bufpos = CHARPOS (*position);
4639 }
4640
4641 /* Reset those iterator values set from display property values. */
4642 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4643 it->space_width = Qnil;
4644 it->font_height = Qnil;
4645 it->voffset = 0;
4646
4647 /* We don't support recursive `display' properties, i.e. string
4648 values that have a string `display' property, that have a string
4649 `display' property etc. */
4650 if (!it->string_from_display_prop_p)
4651 it->area = TEXT_AREA;
4652
4653 propval = get_char_property_and_overlay (make_number (position->charpos),
4654 Qdisplay, object, &overlay);
4655 if (NILP (propval))
4656 return HANDLED_NORMALLY;
4657 /* Now OVERLAY is the overlay that gave us this property, or nil
4658 if it was a text property. */
4659
4660 if (!STRINGP (it->string))
4661 object = it->w->contents;
4662
4663 display_replaced = handle_display_spec (it, propval, object, overlay,
4664 position, bufpos,
4665 FRAME_WINDOW_P (it->f));
4666 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4667 }
4668
4669 /* Subroutine of handle_display_prop. Returns non-zero if the display
4670 specification in SPEC is a replacing specification, i.e. it would
4671 replace the text covered by `display' property with something else,
4672 such as an image or a display string. If SPEC includes any kind or
4673 `(space ...) specification, the value is 2; this is used by
4674 compute_display_string_pos, which see.
4675
4676 See handle_single_display_spec for documentation of arguments.
4677 FRAME_WINDOW_P is true if the window being redisplayed is on a
4678 GUI frame; this argument is used only if IT is NULL, see below.
4679
4680 IT can be NULL, if this is called by the bidi reordering code
4681 through compute_display_string_pos, which see. In that case, this
4682 function only examines SPEC, but does not otherwise "handle" it, in
4683 the sense that it doesn't set up members of IT from the display
4684 spec. */
4685 static int
4686 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4687 Lisp_Object overlay, struct text_pos *position,
4688 ptrdiff_t bufpos, bool frame_window_p)
4689 {
4690 int replacing = 0;
4691
4692 if (CONSP (spec)
4693 /* Simple specifications. */
4694 && !EQ (XCAR (spec), Qimage)
4695 #ifdef HAVE_XWIDGETS
4696 && !EQ (XCAR (spec), Qxwidget)
4697 #endif
4698 && !EQ (XCAR (spec), Qspace)
4699 && !EQ (XCAR (spec), Qwhen)
4700 && !EQ (XCAR (spec), Qslice)
4701 && !EQ (XCAR (spec), Qspace_width)
4702 && !EQ (XCAR (spec), Qheight)
4703 && !EQ (XCAR (spec), Qraise)
4704 /* Marginal area specifications. */
4705 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4706 && !EQ (XCAR (spec), Qleft_fringe)
4707 && !EQ (XCAR (spec), Qright_fringe)
4708 && !NILP (XCAR (spec)))
4709 {
4710 for (; CONSP (spec); spec = XCDR (spec))
4711 {
4712 int rv = handle_single_display_spec (it, XCAR (spec), object,
4713 overlay, position, bufpos,
4714 replacing, frame_window_p);
4715 if (rv != 0)
4716 {
4717 replacing = rv;
4718 /* If some text in a string is replaced, `position' no
4719 longer points to the position of `object'. */
4720 if (!it || STRINGP (object))
4721 break;
4722 }
4723 }
4724 }
4725 else if (VECTORP (spec))
4726 {
4727 ptrdiff_t i;
4728 for (i = 0; i < ASIZE (spec); ++i)
4729 {
4730 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4731 overlay, position, bufpos,
4732 replacing, frame_window_p);
4733 if (rv != 0)
4734 {
4735 replacing = rv;
4736 /* If some text in a string is replaced, `position' no
4737 longer points to the position of `object'. */
4738 if (!it || STRINGP (object))
4739 break;
4740 }
4741 }
4742 }
4743 else
4744 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4745 bufpos, 0, frame_window_p);
4746 return replacing;
4747 }
4748
4749 /* Value is the position of the end of the `display' property starting
4750 at START_POS in OBJECT. */
4751
4752 static struct text_pos
4753 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4754 {
4755 Lisp_Object end;
4756 struct text_pos end_pos;
4757
4758 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4759 Qdisplay, object, Qnil);
4760 CHARPOS (end_pos) = XFASTINT (end);
4761 if (STRINGP (object))
4762 compute_string_pos (&end_pos, start_pos, it->string);
4763 else
4764 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4765
4766 return end_pos;
4767 }
4768
4769
4770 /* Set up IT from a single `display' property specification SPEC. OBJECT
4771 is the object in which the `display' property was found. *POSITION
4772 is the position in OBJECT at which the `display' property was found.
4773 BUFPOS is the buffer position of OBJECT (different from POSITION if
4774 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4775 previously saw a display specification which already replaced text
4776 display with something else, for example an image; we ignore such
4777 properties after the first one has been processed.
4778
4779 OVERLAY is the overlay this `display' property came from,
4780 or nil if it was a text property.
4781
4782 If SPEC is a `space' or `image' specification, and in some other
4783 cases too, set *POSITION to the position where the `display'
4784 property ends.
4785
4786 If IT is NULL, only examine the property specification in SPEC, but
4787 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4788 is intended to be displayed in a window on a GUI frame.
4789
4790 Value is non-zero if something was found which replaces the display
4791 of buffer or string text. */
4792
4793 static int
4794 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4795 Lisp_Object overlay, struct text_pos *position,
4796 ptrdiff_t bufpos, int display_replaced,
4797 bool frame_window_p)
4798 {
4799 Lisp_Object form;
4800 Lisp_Object location, value;
4801 struct text_pos start_pos = *position;
4802
4803 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4804 If the result is non-nil, use VALUE instead of SPEC. */
4805 form = Qt;
4806 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4807 {
4808 spec = XCDR (spec);
4809 if (!CONSP (spec))
4810 return 0;
4811 form = XCAR (spec);
4812 spec = XCDR (spec);
4813 }
4814
4815 if (!NILP (form) && !EQ (form, Qt))
4816 {
4817 ptrdiff_t count = SPECPDL_INDEX ();
4818
4819 /* Bind `object' to the object having the `display' property, a
4820 buffer or string. Bind `position' to the position in the
4821 object where the property was found, and `buffer-position'
4822 to the current position in the buffer. */
4823
4824 if (NILP (object))
4825 XSETBUFFER (object, current_buffer);
4826 specbind (Qobject, object);
4827 specbind (Qposition, make_number (CHARPOS (*position)));
4828 specbind (Qbuffer_position, make_number (bufpos));
4829 form = safe_eval (form);
4830 unbind_to (count, Qnil);
4831 }
4832
4833 if (NILP (form))
4834 return 0;
4835
4836 /* Handle `(height HEIGHT)' specifications. */
4837 if (CONSP (spec)
4838 && EQ (XCAR (spec), Qheight)
4839 && CONSP (XCDR (spec)))
4840 {
4841 if (it)
4842 {
4843 if (!FRAME_WINDOW_P (it->f))
4844 return 0;
4845
4846 it->font_height = XCAR (XCDR (spec));
4847 if (!NILP (it->font_height))
4848 {
4849 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4850 int new_height = -1;
4851
4852 if (CONSP (it->font_height)
4853 && (EQ (XCAR (it->font_height), Qplus)
4854 || EQ (XCAR (it->font_height), Qminus))
4855 && CONSP (XCDR (it->font_height))
4856 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4857 {
4858 /* `(+ N)' or `(- N)' where N is an integer. */
4859 int steps = XINT (XCAR (XCDR (it->font_height)));
4860 if (EQ (XCAR (it->font_height), Qplus))
4861 steps = - steps;
4862 it->face_id = smaller_face (it->f, it->face_id, steps);
4863 }
4864 else if (FUNCTIONP (it->font_height))
4865 {
4866 /* Call function with current height as argument.
4867 Value is the new height. */
4868 Lisp_Object height;
4869 height = safe_call1 (it->font_height,
4870 face->lface[LFACE_HEIGHT_INDEX]);
4871 if (NUMBERP (height))
4872 new_height = XFLOATINT (height);
4873 }
4874 else if (NUMBERP (it->font_height))
4875 {
4876 /* Value is a multiple of the canonical char height. */
4877 struct face *f;
4878
4879 f = FACE_FROM_ID (it->f,
4880 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4881 new_height = (XFLOATINT (it->font_height)
4882 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4883 }
4884 else
4885 {
4886 /* Evaluate IT->font_height with `height' bound to the
4887 current specified height to get the new height. */
4888 ptrdiff_t count = SPECPDL_INDEX ();
4889
4890 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4891 value = safe_eval (it->font_height);
4892 unbind_to (count, Qnil);
4893
4894 if (NUMBERP (value))
4895 new_height = XFLOATINT (value);
4896 }
4897
4898 if (new_height > 0)
4899 it->face_id = face_with_height (it->f, it->face_id, new_height);
4900 }
4901 }
4902
4903 return 0;
4904 }
4905
4906 /* Handle `(space-width WIDTH)'. */
4907 if (CONSP (spec)
4908 && EQ (XCAR (spec), Qspace_width)
4909 && CONSP (XCDR (spec)))
4910 {
4911 if (it)
4912 {
4913 if (!FRAME_WINDOW_P (it->f))
4914 return 0;
4915
4916 value = XCAR (XCDR (spec));
4917 if (NUMBERP (value) && XFLOATINT (value) > 0)
4918 it->space_width = value;
4919 }
4920
4921 return 0;
4922 }
4923
4924 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4925 if (CONSP (spec)
4926 && EQ (XCAR (spec), Qslice))
4927 {
4928 Lisp_Object tem;
4929
4930 if (it)
4931 {
4932 if (!FRAME_WINDOW_P (it->f))
4933 return 0;
4934
4935 if (tem = XCDR (spec), CONSP (tem))
4936 {
4937 it->slice.x = XCAR (tem);
4938 if (tem = XCDR (tem), CONSP (tem))
4939 {
4940 it->slice.y = XCAR (tem);
4941 if (tem = XCDR (tem), CONSP (tem))
4942 {
4943 it->slice.width = XCAR (tem);
4944 if (tem = XCDR (tem), CONSP (tem))
4945 it->slice.height = XCAR (tem);
4946 }
4947 }
4948 }
4949 }
4950
4951 return 0;
4952 }
4953
4954 /* Handle `(raise FACTOR)'. */
4955 if (CONSP (spec)
4956 && EQ (XCAR (spec), Qraise)
4957 && CONSP (XCDR (spec)))
4958 {
4959 if (it)
4960 {
4961 if (!FRAME_WINDOW_P (it->f))
4962 return 0;
4963
4964 #ifdef HAVE_WINDOW_SYSTEM
4965 value = XCAR (XCDR (spec));
4966 if (NUMBERP (value))
4967 {
4968 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4969 it->voffset = - (XFLOATINT (value)
4970 * (normal_char_height (face->font, -1)));
4971 }
4972 #endif /* HAVE_WINDOW_SYSTEM */
4973 }
4974
4975 return 0;
4976 }
4977
4978 /* Don't handle the other kinds of display specifications
4979 inside a string that we got from a `display' property. */
4980 if (it && it->string_from_display_prop_p)
4981 return 0;
4982
4983 /* Characters having this form of property are not displayed, so
4984 we have to find the end of the property. */
4985 if (it)
4986 {
4987 start_pos = *position;
4988 *position = display_prop_end (it, object, start_pos);
4989 /* If the display property comes from an overlay, don't consider
4990 any potential stop_charpos values before the end of that
4991 overlay. Since display_prop_end will happily find another
4992 'display' property coming from some other overlay or text
4993 property on buffer positions before this overlay's end, we
4994 need to ignore them, or else we risk displaying this
4995 overlay's display string/image twice. */
4996 if (!NILP (overlay))
4997 {
4998 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4999
5000 if (ovendpos > CHARPOS (*position))
5001 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5002 }
5003 }
5004 value = Qnil;
5005
5006 /* Stop the scan at that end position--we assume that all
5007 text properties change there. */
5008 if (it)
5009 it->stop_charpos = position->charpos;
5010
5011 /* Handle `(left-fringe BITMAP [FACE])'
5012 and `(right-fringe BITMAP [FACE])'. */
5013 if (CONSP (spec)
5014 && (EQ (XCAR (spec), Qleft_fringe)
5015 || EQ (XCAR (spec), Qright_fringe))
5016 && CONSP (XCDR (spec)))
5017 {
5018 int fringe_bitmap;
5019
5020 if (it)
5021 {
5022 if (!FRAME_WINDOW_P (it->f))
5023 /* If we return here, POSITION has been advanced
5024 across the text with this property. */
5025 {
5026 /* Synchronize the bidi iterator with POSITION. This is
5027 needed because we are not going to push the iterator
5028 on behalf of this display property, so there will be
5029 no pop_it call to do this synchronization for us. */
5030 if (it->bidi_p)
5031 {
5032 it->position = *position;
5033 iterate_out_of_display_property (it);
5034 *position = it->position;
5035 }
5036 return 1;
5037 }
5038 }
5039 else if (!frame_window_p)
5040 return 1;
5041
5042 #ifdef HAVE_WINDOW_SYSTEM
5043 value = XCAR (XCDR (spec));
5044 if (!SYMBOLP (value)
5045 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
5046 /* If we return here, POSITION has been advanced
5047 across the text with this property. */
5048 {
5049 if (it && it->bidi_p)
5050 {
5051 it->position = *position;
5052 iterate_out_of_display_property (it);
5053 *position = it->position;
5054 }
5055 return 1;
5056 }
5057
5058 if (it)
5059 {
5060 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5061
5062 if (CONSP (XCDR (XCDR (spec))))
5063 {
5064 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5065 int face_id2 = lookup_derived_face (it->f, face_name,
5066 FRINGE_FACE_ID, false);
5067 if (face_id2 >= 0)
5068 face_id = face_id2;
5069 }
5070
5071 /* Save current settings of IT so that we can restore them
5072 when we are finished with the glyph property value. */
5073 push_it (it, position);
5074
5075 it->area = TEXT_AREA;
5076 it->what = IT_IMAGE;
5077 it->image_id = -1; /* no image */
5078 it->position = start_pos;
5079 it->object = NILP (object) ? it->w->contents : object;
5080 it->method = GET_FROM_IMAGE;
5081 it->from_overlay = Qnil;
5082 it->face_id = face_id;
5083 it->from_disp_prop_p = true;
5084
5085 /* Say that we haven't consumed the characters with
5086 `display' property yet. The call to pop_it in
5087 set_iterator_to_next will clean this up. */
5088 *position = start_pos;
5089
5090 if (EQ (XCAR (spec), Qleft_fringe))
5091 {
5092 it->left_user_fringe_bitmap = fringe_bitmap;
5093 it->left_user_fringe_face_id = face_id;
5094 }
5095 else
5096 {
5097 it->right_user_fringe_bitmap = fringe_bitmap;
5098 it->right_user_fringe_face_id = face_id;
5099 }
5100 }
5101 #endif /* HAVE_WINDOW_SYSTEM */
5102 return 1;
5103 }
5104
5105 /* Prepare to handle `((margin left-margin) ...)',
5106 `((margin right-margin) ...)' and `((margin nil) ...)'
5107 prefixes for display specifications. */
5108 location = Qunbound;
5109 if (CONSP (spec) && CONSP (XCAR (spec)))
5110 {
5111 Lisp_Object tem;
5112
5113 value = XCDR (spec);
5114 if (CONSP (value))
5115 value = XCAR (value);
5116
5117 tem = XCAR (spec);
5118 if (EQ (XCAR (tem), Qmargin)
5119 && (tem = XCDR (tem),
5120 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5121 (NILP (tem)
5122 || EQ (tem, Qleft_margin)
5123 || EQ (tem, Qright_margin))))
5124 location = tem;
5125 }
5126
5127 if (EQ (location, Qunbound))
5128 {
5129 location = Qnil;
5130 value = spec;
5131 }
5132
5133 /* After this point, VALUE is the property after any
5134 margin prefix has been stripped. It must be a string,
5135 an image specification, or `(space ...)'.
5136
5137 LOCATION specifies where to display: `left-margin',
5138 `right-margin' or nil. */
5139
5140 bool valid_p = (STRINGP (value)
5141 #ifdef HAVE_WINDOW_SYSTEM
5142 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5143 && valid_image_p (value))
5144 #endif /* not HAVE_WINDOW_SYSTEM */
5145 || (CONSP (value) && EQ (XCAR (value), Qspace))
5146 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5147 && valid_xwidget_spec_p (value)));
5148
5149 if (valid_p && display_replaced == 0)
5150 {
5151 int retval = 1;
5152
5153 if (!it)
5154 {
5155 /* Callers need to know whether the display spec is any kind
5156 of `(space ...)' spec that is about to affect text-area
5157 display. */
5158 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5159 retval = 2;
5160 return retval;
5161 }
5162
5163 /* Save current settings of IT so that we can restore them
5164 when we are finished with the glyph property value. */
5165 push_it (it, position);
5166 it->from_overlay = overlay;
5167 it->from_disp_prop_p = true;
5168
5169 if (NILP (location))
5170 it->area = TEXT_AREA;
5171 else if (EQ (location, Qleft_margin))
5172 it->area = LEFT_MARGIN_AREA;
5173 else
5174 it->area = RIGHT_MARGIN_AREA;
5175
5176 if (STRINGP (value))
5177 {
5178 it->string = value;
5179 it->multibyte_p = STRING_MULTIBYTE (it->string);
5180 it->current.overlay_string_index = -1;
5181 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5182 it->end_charpos = it->string_nchars = SCHARS (it->string);
5183 it->method = GET_FROM_STRING;
5184 it->stop_charpos = 0;
5185 it->prev_stop = 0;
5186 it->base_level_stop = 0;
5187 it->string_from_display_prop_p = true;
5188 /* Say that we haven't consumed the characters with
5189 `display' property yet. The call to pop_it in
5190 set_iterator_to_next will clean this up. */
5191 if (BUFFERP (object))
5192 *position = start_pos;
5193
5194 /* Force paragraph direction to be that of the parent
5195 object. If the parent object's paragraph direction is
5196 not yet determined, default to L2R. */
5197 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5198 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5199 else
5200 it->paragraph_embedding = L2R;
5201
5202 /* Set up the bidi iterator for this display string. */
5203 if (it->bidi_p)
5204 {
5205 it->bidi_it.string.lstring = it->string;
5206 it->bidi_it.string.s = NULL;
5207 it->bidi_it.string.schars = it->end_charpos;
5208 it->bidi_it.string.bufpos = bufpos;
5209 it->bidi_it.string.from_disp_str = true;
5210 it->bidi_it.string.unibyte = !it->multibyte_p;
5211 it->bidi_it.w = it->w;
5212 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5213 }
5214 }
5215 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5216 {
5217 it->method = GET_FROM_STRETCH;
5218 it->object = value;
5219 *position = it->position = start_pos;
5220 retval = 1 + (it->area == TEXT_AREA);
5221 }
5222 else if (valid_xwidget_spec_p (value))
5223 {
5224 it->what = IT_XWIDGET;
5225 it->method = GET_FROM_XWIDGET;
5226 it->position = start_pos;
5227 it->object = NILP (object) ? it->w->contents : object;
5228 *position = start_pos;
5229 it->xwidget = lookup_xwidget (value);
5230 }
5231 #ifdef HAVE_WINDOW_SYSTEM
5232 else
5233 {
5234 it->what = IT_IMAGE;
5235 it->image_id = lookup_image (it->f, value);
5236 it->position = start_pos;
5237 it->object = NILP (object) ? it->w->contents : object;
5238 it->method = GET_FROM_IMAGE;
5239
5240 /* Say that we haven't consumed the characters with
5241 `display' property yet. The call to pop_it in
5242 set_iterator_to_next will clean this up. */
5243 *position = start_pos;
5244 }
5245 #endif /* HAVE_WINDOW_SYSTEM */
5246
5247 return retval;
5248 }
5249
5250 /* Invalid property or property not supported. Restore
5251 POSITION to what it was before. */
5252 *position = start_pos;
5253 return 0;
5254 }
5255
5256 /* Check if PROP is a display property value whose text should be
5257 treated as intangible. OVERLAY is the overlay from which PROP
5258 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5259 specify the buffer position covered by PROP. */
5260
5261 bool
5262 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5263 ptrdiff_t charpos, ptrdiff_t bytepos)
5264 {
5265 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5266 struct text_pos position;
5267
5268 SET_TEXT_POS (position, charpos, bytepos);
5269 return (handle_display_spec (NULL, prop, Qnil, overlay,
5270 &position, charpos, frame_window_p)
5271 != 0);
5272 }
5273
5274
5275 /* Return true if PROP is a display sub-property value containing STRING.
5276
5277 Implementation note: this and the following function are really
5278 special cases of handle_display_spec and
5279 handle_single_display_spec, and should ideally use the same code.
5280 Until they do, these two pairs must be consistent and must be
5281 modified in sync. */
5282
5283 static bool
5284 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5285 {
5286 if (EQ (string, prop))
5287 return true;
5288
5289 /* Skip over `when FORM'. */
5290 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5291 {
5292 prop = XCDR (prop);
5293 if (!CONSP (prop))
5294 return false;
5295 /* Actually, the condition following `when' should be eval'ed,
5296 like handle_single_display_spec does, and we should return
5297 false if it evaluates to nil. However, this function is
5298 called only when the buffer was already displayed and some
5299 glyph in the glyph matrix was found to come from a display
5300 string. Therefore, the condition was already evaluated, and
5301 the result was non-nil, otherwise the display string wouldn't
5302 have been displayed and we would have never been called for
5303 this property. Thus, we can skip the evaluation and assume
5304 its result is non-nil. */
5305 prop = XCDR (prop);
5306 }
5307
5308 if (CONSP (prop))
5309 /* Skip over `margin LOCATION'. */
5310 if (EQ (XCAR (prop), Qmargin))
5311 {
5312 prop = XCDR (prop);
5313 if (!CONSP (prop))
5314 return false;
5315
5316 prop = XCDR (prop);
5317 if (!CONSP (prop))
5318 return false;
5319 }
5320
5321 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5322 }
5323
5324
5325 /* Return true if STRING appears in the `display' property PROP. */
5326
5327 static bool
5328 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5329 {
5330 if (CONSP (prop)
5331 && !EQ (XCAR (prop), Qwhen)
5332 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5333 {
5334 /* A list of sub-properties. */
5335 while (CONSP (prop))
5336 {
5337 if (single_display_spec_string_p (XCAR (prop), string))
5338 return true;
5339 prop = XCDR (prop);
5340 }
5341 }
5342 else if (VECTORP (prop))
5343 {
5344 /* A vector of sub-properties. */
5345 ptrdiff_t i;
5346 for (i = 0; i < ASIZE (prop); ++i)
5347 if (single_display_spec_string_p (AREF (prop, i), string))
5348 return true;
5349 }
5350 else
5351 return single_display_spec_string_p (prop, string);
5352
5353 return false;
5354 }
5355
5356 /* Look for STRING in overlays and text properties in the current
5357 buffer, between character positions FROM and TO (excluding TO).
5358 BACK_P means look back (in this case, TO is supposed to be
5359 less than FROM).
5360 Value is the first character position where STRING was found, or
5361 zero if it wasn't found before hitting TO.
5362
5363 This function may only use code that doesn't eval because it is
5364 called asynchronously from note_mouse_highlight. */
5365
5366 static ptrdiff_t
5367 string_buffer_position_lim (Lisp_Object string,
5368 ptrdiff_t from, ptrdiff_t to, bool back_p)
5369 {
5370 Lisp_Object limit, prop, pos;
5371 bool found = false;
5372
5373 pos = make_number (max (from, BEGV));
5374
5375 if (!back_p) /* looking forward */
5376 {
5377 limit = make_number (min (to, ZV));
5378 while (!found && !EQ (pos, limit))
5379 {
5380 prop = Fget_char_property (pos, Qdisplay, Qnil);
5381 if (!NILP (prop) && display_prop_string_p (prop, string))
5382 found = true;
5383 else
5384 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5385 limit);
5386 }
5387 }
5388 else /* looking back */
5389 {
5390 limit = make_number (max (to, BEGV));
5391 while (!found && !EQ (pos, limit))
5392 {
5393 prop = Fget_char_property (pos, Qdisplay, Qnil);
5394 if (!NILP (prop) && display_prop_string_p (prop, string))
5395 found = true;
5396 else
5397 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5398 limit);
5399 }
5400 }
5401
5402 return found ? XINT (pos) : 0;
5403 }
5404
5405 /* Determine which buffer position in current buffer STRING comes from.
5406 AROUND_CHARPOS is an approximate position where it could come from.
5407 Value is the buffer position or 0 if it couldn't be determined.
5408
5409 This function is necessary because we don't record buffer positions
5410 in glyphs generated from strings (to keep struct glyph small).
5411 This function may only use code that doesn't eval because it is
5412 called asynchronously from note_mouse_highlight. */
5413
5414 static ptrdiff_t
5415 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5416 {
5417 const int MAX_DISTANCE = 1000;
5418 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5419 around_charpos + MAX_DISTANCE,
5420 false);
5421
5422 if (!found)
5423 found = string_buffer_position_lim (string, around_charpos,
5424 around_charpos - MAX_DISTANCE, true);
5425 return found;
5426 }
5427
5428
5429 \f
5430 /***********************************************************************
5431 `composition' property
5432 ***********************************************************************/
5433
5434 /* Set up iterator IT from `composition' property at its current
5435 position. Called from handle_stop. */
5436
5437 static enum prop_handled
5438 handle_composition_prop (struct it *it)
5439 {
5440 Lisp_Object prop, string;
5441 ptrdiff_t pos, pos_byte, start, end;
5442
5443 if (STRINGP (it->string))
5444 {
5445 unsigned char *s;
5446
5447 pos = IT_STRING_CHARPOS (*it);
5448 pos_byte = IT_STRING_BYTEPOS (*it);
5449 string = it->string;
5450 s = SDATA (string) + pos_byte;
5451 it->c = STRING_CHAR (s);
5452 }
5453 else
5454 {
5455 pos = IT_CHARPOS (*it);
5456 pos_byte = IT_BYTEPOS (*it);
5457 string = Qnil;
5458 it->c = FETCH_CHAR (pos_byte);
5459 }
5460
5461 /* If there's a valid composition and point is not inside of the
5462 composition (in the case that the composition is from the current
5463 buffer), draw a glyph composed from the composition components. */
5464 if (find_composition (pos, -1, &start, &end, &prop, string)
5465 && composition_valid_p (start, end, prop)
5466 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5467 {
5468 if (start < pos)
5469 /* As we can't handle this situation (perhaps font-lock added
5470 a new composition), we just return here hoping that next
5471 redisplay will detect this composition much earlier. */
5472 return HANDLED_NORMALLY;
5473 if (start != pos)
5474 {
5475 if (STRINGP (it->string))
5476 pos_byte = string_char_to_byte (it->string, start);
5477 else
5478 pos_byte = CHAR_TO_BYTE (start);
5479 }
5480 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5481 prop, string);
5482
5483 if (it->cmp_it.id >= 0)
5484 {
5485 it->cmp_it.ch = -1;
5486 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5487 it->cmp_it.nglyphs = -1;
5488 }
5489 }
5490
5491 return HANDLED_NORMALLY;
5492 }
5493
5494
5495 \f
5496 /***********************************************************************
5497 Overlay strings
5498 ***********************************************************************/
5499
5500 /* The following structure is used to record overlay strings for
5501 later sorting in load_overlay_strings. */
5502
5503 struct overlay_entry
5504 {
5505 Lisp_Object overlay;
5506 Lisp_Object string;
5507 EMACS_INT priority;
5508 bool after_string_p;
5509 };
5510
5511
5512 /* Set up iterator IT from overlay strings at its current position.
5513 Called from handle_stop. */
5514
5515 static enum prop_handled
5516 handle_overlay_change (struct it *it)
5517 {
5518 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5519 return HANDLED_RECOMPUTE_PROPS;
5520 else
5521 return HANDLED_NORMALLY;
5522 }
5523
5524
5525 /* Set up the next overlay string for delivery by IT, if there is an
5526 overlay string to deliver. Called by set_iterator_to_next when the
5527 end of the current overlay string is reached. If there are more
5528 overlay strings to display, IT->string and
5529 IT->current.overlay_string_index are set appropriately here.
5530 Otherwise IT->string is set to nil. */
5531
5532 static void
5533 next_overlay_string (struct it *it)
5534 {
5535 ++it->current.overlay_string_index;
5536 if (it->current.overlay_string_index == it->n_overlay_strings)
5537 {
5538 /* No more overlay strings. Restore IT's settings to what
5539 they were before overlay strings were processed, and
5540 continue to deliver from current_buffer. */
5541
5542 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5543 pop_it (it);
5544 eassert (it->sp > 0
5545 || (NILP (it->string)
5546 && it->method == GET_FROM_BUFFER
5547 && it->stop_charpos >= BEGV
5548 && it->stop_charpos <= it->end_charpos));
5549 it->current.overlay_string_index = -1;
5550 it->n_overlay_strings = 0;
5551 /* If there's an empty display string on the stack, pop the
5552 stack, to resync the bidi iterator with IT's position. Such
5553 empty strings are pushed onto the stack in
5554 get_overlay_strings_1. */
5555 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5556 pop_it (it);
5557
5558 /* Since we've exhausted overlay strings at this buffer
5559 position, set the flag to ignore overlays until we move to
5560 another position. The flag is reset in
5561 next_element_from_buffer. */
5562 it->ignore_overlay_strings_at_pos_p = true;
5563
5564 /* If we're at the end of the buffer, record that we have
5565 processed the overlay strings there already, so that
5566 next_element_from_buffer doesn't try it again. */
5567 if (NILP (it->string)
5568 && IT_CHARPOS (*it) >= it->end_charpos
5569 && it->overlay_strings_charpos >= it->end_charpos)
5570 it->overlay_strings_at_end_processed_p = true;
5571 /* Note: we reset overlay_strings_charpos only here, to make
5572 sure the just-processed overlays were indeed at EOB.
5573 Otherwise, overlays on text with invisible text property,
5574 which are processed with IT's position past the invisible
5575 text, might fool us into thinking the overlays at EOB were
5576 already processed (linum-mode can cause this, for
5577 example). */
5578 it->overlay_strings_charpos = -1;
5579 }
5580 else
5581 {
5582 /* There are more overlay strings to process. If
5583 IT->current.overlay_string_index has advanced to a position
5584 where we must load IT->overlay_strings with more strings, do
5585 it. We must load at the IT->overlay_strings_charpos where
5586 IT->n_overlay_strings was originally computed; when invisible
5587 text is present, this might not be IT_CHARPOS (Bug#7016). */
5588 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5589
5590 if (it->current.overlay_string_index && i == 0)
5591 load_overlay_strings (it, it->overlay_strings_charpos);
5592
5593 /* Initialize IT to deliver display elements from the overlay
5594 string. */
5595 it->string = it->overlay_strings[i];
5596 it->multibyte_p = STRING_MULTIBYTE (it->string);
5597 SET_TEXT_POS (it->current.string_pos, 0, 0);
5598 it->method = GET_FROM_STRING;
5599 it->stop_charpos = 0;
5600 it->end_charpos = SCHARS (it->string);
5601 if (it->cmp_it.stop_pos >= 0)
5602 it->cmp_it.stop_pos = 0;
5603 it->prev_stop = 0;
5604 it->base_level_stop = 0;
5605
5606 /* Set up the bidi iterator for this overlay string. */
5607 if (it->bidi_p)
5608 {
5609 it->bidi_it.string.lstring = it->string;
5610 it->bidi_it.string.s = NULL;
5611 it->bidi_it.string.schars = SCHARS (it->string);
5612 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5613 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5614 it->bidi_it.string.unibyte = !it->multibyte_p;
5615 it->bidi_it.w = it->w;
5616 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5617 }
5618 }
5619
5620 CHECK_IT (it);
5621 }
5622
5623
5624 /* Compare two overlay_entry structures E1 and E2. Used as a
5625 comparison function for qsort in load_overlay_strings. Overlay
5626 strings for the same position are sorted so that
5627
5628 1. All after-strings come in front of before-strings, except
5629 when they come from the same overlay.
5630
5631 2. Within after-strings, strings are sorted so that overlay strings
5632 from overlays with higher priorities come first.
5633
5634 2. Within before-strings, strings are sorted so that overlay
5635 strings from overlays with higher priorities come last.
5636
5637 Value is analogous to strcmp. */
5638
5639
5640 static int
5641 compare_overlay_entries (const void *e1, const void *e2)
5642 {
5643 struct overlay_entry const *entry1 = e1;
5644 struct overlay_entry const *entry2 = e2;
5645 int result;
5646
5647 if (entry1->after_string_p != entry2->after_string_p)
5648 {
5649 /* Let after-strings appear in front of before-strings if
5650 they come from different overlays. */
5651 if (EQ (entry1->overlay, entry2->overlay))
5652 result = entry1->after_string_p ? 1 : -1;
5653 else
5654 result = entry1->after_string_p ? -1 : 1;
5655 }
5656 else if (entry1->priority != entry2->priority)
5657 {
5658 if (entry1->after_string_p)
5659 /* After-strings sorted in order of decreasing priority. */
5660 result = entry2->priority < entry1->priority ? -1 : 1;
5661 else
5662 /* Before-strings sorted in order of increasing priority. */
5663 result = entry1->priority < entry2->priority ? -1 : 1;
5664 }
5665 else
5666 result = 0;
5667
5668 return result;
5669 }
5670
5671
5672 /* Load the vector IT->overlay_strings with overlay strings from IT's
5673 current buffer position, or from CHARPOS if that is > 0. Set
5674 IT->n_overlays to the total number of overlay strings found.
5675
5676 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5677 a time. On entry into load_overlay_strings,
5678 IT->current.overlay_string_index gives the number of overlay
5679 strings that have already been loaded by previous calls to this
5680 function.
5681
5682 IT->add_overlay_start contains an additional overlay start
5683 position to consider for taking overlay strings from, if non-zero.
5684 This position comes into play when the overlay has an `invisible'
5685 property, and both before and after-strings. When we've skipped to
5686 the end of the overlay, because of its `invisible' property, we
5687 nevertheless want its before-string to appear.
5688 IT->add_overlay_start will contain the overlay start position
5689 in this case.
5690
5691 Overlay strings are sorted so that after-string strings come in
5692 front of before-string strings. Within before and after-strings,
5693 strings are sorted by overlay priority. See also function
5694 compare_overlay_entries. */
5695
5696 static void
5697 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5698 {
5699 Lisp_Object overlay, window, str, invisible;
5700 struct Lisp_Overlay *ov;
5701 ptrdiff_t start, end;
5702 ptrdiff_t n = 0, i, j;
5703 int invis;
5704 struct overlay_entry entriesbuf[20];
5705 ptrdiff_t size = ARRAYELTS (entriesbuf);
5706 struct overlay_entry *entries = entriesbuf;
5707 USE_SAFE_ALLOCA;
5708
5709 if (charpos <= 0)
5710 charpos = IT_CHARPOS (*it);
5711
5712 /* Append the overlay string STRING of overlay OVERLAY to vector
5713 `entries' which has size `size' and currently contains `n'
5714 elements. AFTER_P means STRING is an after-string of
5715 OVERLAY. */
5716 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5717 do \
5718 { \
5719 Lisp_Object priority; \
5720 \
5721 if (n == size) \
5722 { \
5723 struct overlay_entry *old = entries; \
5724 SAFE_NALLOCA (entries, 2, size); \
5725 memcpy (entries, old, size * sizeof *entries); \
5726 size *= 2; \
5727 } \
5728 \
5729 entries[n].string = (STRING); \
5730 entries[n].overlay = (OVERLAY); \
5731 priority = Foverlay_get ((OVERLAY), Qpriority); \
5732 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5733 entries[n].after_string_p = (AFTER_P); \
5734 ++n; \
5735 } \
5736 while (false)
5737
5738 /* Process overlay before the overlay center. */
5739 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5740 {
5741 XSETMISC (overlay, ov);
5742 eassert (OVERLAYP (overlay));
5743 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5744 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5745
5746 if (end < charpos)
5747 break;
5748
5749 /* Skip this overlay if it doesn't start or end at IT's current
5750 position. */
5751 if (end != charpos && start != charpos)
5752 continue;
5753
5754 /* Skip this overlay if it doesn't apply to IT->w. */
5755 window = Foverlay_get (overlay, Qwindow);
5756 if (WINDOWP (window) && XWINDOW (window) != it->w)
5757 continue;
5758
5759 /* If the text ``under'' the overlay is invisible, both before-
5760 and after-strings from this overlay are visible; start and
5761 end position are indistinguishable. */
5762 invisible = Foverlay_get (overlay, Qinvisible);
5763 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5764
5765 /* If overlay has a non-empty before-string, record it. */
5766 if ((start == charpos || (end == charpos && invis != 0))
5767 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5768 && SCHARS (str))
5769 RECORD_OVERLAY_STRING (overlay, str, false);
5770
5771 /* If overlay has a non-empty after-string, record it. */
5772 if ((end == charpos || (start == charpos && invis != 0))
5773 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5774 && SCHARS (str))
5775 RECORD_OVERLAY_STRING (overlay, str, true);
5776 }
5777
5778 /* Process overlays after the overlay center. */
5779 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5780 {
5781 XSETMISC (overlay, ov);
5782 eassert (OVERLAYP (overlay));
5783 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5784 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5785
5786 if (start > charpos)
5787 break;
5788
5789 /* Skip this overlay if it doesn't start or end at IT's current
5790 position. */
5791 if (end != charpos && start != charpos)
5792 continue;
5793
5794 /* Skip this overlay if it doesn't apply to IT->w. */
5795 window = Foverlay_get (overlay, Qwindow);
5796 if (WINDOWP (window) && XWINDOW (window) != it->w)
5797 continue;
5798
5799 /* If the text ``under'' the overlay is invisible, it has a zero
5800 dimension, and both before- and after-strings apply. */
5801 invisible = Foverlay_get (overlay, Qinvisible);
5802 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5803
5804 /* If overlay has a non-empty before-string, record it. */
5805 if ((start == charpos || (end == charpos && invis != 0))
5806 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5807 && SCHARS (str))
5808 RECORD_OVERLAY_STRING (overlay, str, false);
5809
5810 /* If overlay has a non-empty after-string, record it. */
5811 if ((end == charpos || (start == charpos && invis != 0))
5812 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5813 && SCHARS (str))
5814 RECORD_OVERLAY_STRING (overlay, str, true);
5815 }
5816
5817 #undef RECORD_OVERLAY_STRING
5818
5819 /* Sort entries. */
5820 if (n > 1)
5821 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5822
5823 /* Record number of overlay strings, and where we computed it. */
5824 it->n_overlay_strings = n;
5825 it->overlay_strings_charpos = charpos;
5826
5827 /* IT->current.overlay_string_index is the number of overlay strings
5828 that have already been consumed by IT. Copy some of the
5829 remaining overlay strings to IT->overlay_strings. */
5830 i = 0;
5831 j = it->current.overlay_string_index;
5832 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5833 {
5834 it->overlay_strings[i] = entries[j].string;
5835 it->string_overlays[i++] = entries[j++].overlay;
5836 }
5837
5838 CHECK_IT (it);
5839 SAFE_FREE ();
5840 }
5841
5842
5843 /* Get the first chunk of overlay strings at IT's current buffer
5844 position, or at CHARPOS if that is > 0. Value is true if at
5845 least one overlay string was found. */
5846
5847 static bool
5848 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5849 {
5850 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5851 process. This fills IT->overlay_strings with strings, and sets
5852 IT->n_overlay_strings to the total number of strings to process.
5853 IT->pos.overlay_string_index has to be set temporarily to zero
5854 because load_overlay_strings needs this; it must be set to -1
5855 when no overlay strings are found because a zero value would
5856 indicate a position in the first overlay string. */
5857 it->current.overlay_string_index = 0;
5858 load_overlay_strings (it, charpos);
5859
5860 /* If we found overlay strings, set up IT to deliver display
5861 elements from the first one. Otherwise set up IT to deliver
5862 from current_buffer. */
5863 if (it->n_overlay_strings)
5864 {
5865 /* Make sure we know settings in current_buffer, so that we can
5866 restore meaningful values when we're done with the overlay
5867 strings. */
5868 if (compute_stop_p)
5869 compute_stop_pos (it);
5870 eassert (it->face_id >= 0);
5871
5872 /* Save IT's settings. They are restored after all overlay
5873 strings have been processed. */
5874 eassert (!compute_stop_p || it->sp == 0);
5875
5876 /* When called from handle_stop, there might be an empty display
5877 string loaded. In that case, don't bother saving it. But
5878 don't use this optimization with the bidi iterator, since we
5879 need the corresponding pop_it call to resync the bidi
5880 iterator's position with IT's position, after we are done
5881 with the overlay strings. (The corresponding call to pop_it
5882 in case of an empty display string is in
5883 next_overlay_string.) */
5884 if (!(!it->bidi_p
5885 && STRINGP (it->string) && !SCHARS (it->string)))
5886 push_it (it, NULL);
5887
5888 /* Set up IT to deliver display elements from the first overlay
5889 string. */
5890 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5891 it->string = it->overlay_strings[0];
5892 it->from_overlay = Qnil;
5893 it->stop_charpos = 0;
5894 eassert (STRINGP (it->string));
5895 it->end_charpos = SCHARS (it->string);
5896 it->prev_stop = 0;
5897 it->base_level_stop = 0;
5898 it->multibyte_p = STRING_MULTIBYTE (it->string);
5899 it->method = GET_FROM_STRING;
5900 it->from_disp_prop_p = 0;
5901
5902 /* Force paragraph direction to be that of the parent
5903 buffer. */
5904 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5905 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5906 else
5907 it->paragraph_embedding = L2R;
5908
5909 /* Set up the bidi iterator for this overlay string. */
5910 if (it->bidi_p)
5911 {
5912 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5913
5914 it->bidi_it.string.lstring = it->string;
5915 it->bidi_it.string.s = NULL;
5916 it->bidi_it.string.schars = SCHARS (it->string);
5917 it->bidi_it.string.bufpos = pos;
5918 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5919 it->bidi_it.string.unibyte = !it->multibyte_p;
5920 it->bidi_it.w = it->w;
5921 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5922 }
5923 return true;
5924 }
5925
5926 it->current.overlay_string_index = -1;
5927 return false;
5928 }
5929
5930 static bool
5931 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5932 {
5933 it->string = Qnil;
5934 it->method = GET_FROM_BUFFER;
5935
5936 get_overlay_strings_1 (it, charpos, true);
5937
5938 CHECK_IT (it);
5939
5940 /* Value is true if we found at least one overlay string. */
5941 return STRINGP (it->string);
5942 }
5943
5944
5945 \f
5946 /***********************************************************************
5947 Saving and restoring state
5948 ***********************************************************************/
5949
5950 /* Save current settings of IT on IT->stack. Called, for example,
5951 before setting up IT for an overlay string, to be able to restore
5952 IT's settings to what they were after the overlay string has been
5953 processed. If POSITION is non-NULL, it is the position to save on
5954 the stack instead of IT->position. */
5955
5956 static void
5957 push_it (struct it *it, struct text_pos *position)
5958 {
5959 struct iterator_stack_entry *p;
5960
5961 eassert (it->sp < IT_STACK_SIZE);
5962 p = it->stack + it->sp;
5963
5964 p->stop_charpos = it->stop_charpos;
5965 p->prev_stop = it->prev_stop;
5966 p->base_level_stop = it->base_level_stop;
5967 p->cmp_it = it->cmp_it;
5968 eassert (it->face_id >= 0);
5969 p->face_id = it->face_id;
5970 p->string = it->string;
5971 p->method = it->method;
5972 p->from_overlay = it->from_overlay;
5973 switch (p->method)
5974 {
5975 case GET_FROM_IMAGE:
5976 p->u.image.object = it->object;
5977 p->u.image.image_id = it->image_id;
5978 p->u.image.slice = it->slice;
5979 break;
5980 case GET_FROM_STRETCH:
5981 p->u.stretch.object = it->object;
5982 break;
5983 case GET_FROM_XWIDGET:
5984 p->u.xwidget.object = it->object;
5985 break;
5986 case GET_FROM_BUFFER:
5987 case GET_FROM_DISPLAY_VECTOR:
5988 case GET_FROM_STRING:
5989 case GET_FROM_C_STRING:
5990 break;
5991 default:
5992 emacs_abort ();
5993 }
5994 p->position = position ? *position : it->position;
5995 p->current = it->current;
5996 p->end_charpos = it->end_charpos;
5997 p->string_nchars = it->string_nchars;
5998 p->area = it->area;
5999 p->multibyte_p = it->multibyte_p;
6000 p->avoid_cursor_p = it->avoid_cursor_p;
6001 p->space_width = it->space_width;
6002 p->font_height = it->font_height;
6003 p->voffset = it->voffset;
6004 p->string_from_display_prop_p = it->string_from_display_prop_p;
6005 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6006 p->display_ellipsis_p = false;
6007 p->line_wrap = it->line_wrap;
6008 p->bidi_p = it->bidi_p;
6009 p->paragraph_embedding = it->paragraph_embedding;
6010 p->from_disp_prop_p = it->from_disp_prop_p;
6011 ++it->sp;
6012
6013 /* Save the state of the bidi iterator as well. */
6014 if (it->bidi_p)
6015 bidi_push_it (&it->bidi_it);
6016 }
6017
6018 static void
6019 iterate_out_of_display_property (struct it *it)
6020 {
6021 bool buffer_p = !STRINGP (it->string);
6022 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6023 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6024
6025 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6026
6027 /* Maybe initialize paragraph direction. If we are at the beginning
6028 of a new paragraph, next_element_from_buffer may not have a
6029 chance to do that. */
6030 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6031 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6032 /* prev_stop can be zero, so check against BEGV as well. */
6033 while (it->bidi_it.charpos >= bob
6034 && it->prev_stop <= it->bidi_it.charpos
6035 && it->bidi_it.charpos < CHARPOS (it->position)
6036 && it->bidi_it.charpos < eob)
6037 bidi_move_to_visually_next (&it->bidi_it);
6038 /* Record the stop_pos we just crossed, for when we cross it
6039 back, maybe. */
6040 if (it->bidi_it.charpos > CHARPOS (it->position))
6041 it->prev_stop = CHARPOS (it->position);
6042 /* If we ended up not where pop_it put us, resync IT's
6043 positional members with the bidi iterator. */
6044 if (it->bidi_it.charpos != CHARPOS (it->position))
6045 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6046 if (buffer_p)
6047 it->current.pos = it->position;
6048 else
6049 it->current.string_pos = it->position;
6050 }
6051
6052 /* Restore IT's settings from IT->stack. Called, for example, when no
6053 more overlay strings must be processed, and we return to delivering
6054 display elements from a buffer, or when the end of a string from a
6055 `display' property is reached and we return to delivering display
6056 elements from an overlay string, or from a buffer. */
6057
6058 static void
6059 pop_it (struct it *it)
6060 {
6061 struct iterator_stack_entry *p;
6062 bool from_display_prop = it->from_disp_prop_p;
6063 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6064
6065 eassert (it->sp > 0);
6066 --it->sp;
6067 p = it->stack + it->sp;
6068 it->stop_charpos = p->stop_charpos;
6069 it->prev_stop = p->prev_stop;
6070 it->base_level_stop = p->base_level_stop;
6071 it->cmp_it = p->cmp_it;
6072 it->face_id = p->face_id;
6073 it->current = p->current;
6074 it->position = p->position;
6075 it->string = p->string;
6076 it->from_overlay = p->from_overlay;
6077 if (NILP (it->string))
6078 SET_TEXT_POS (it->current.string_pos, -1, -1);
6079 it->method = p->method;
6080 switch (it->method)
6081 {
6082 case GET_FROM_IMAGE:
6083 it->image_id = p->u.image.image_id;
6084 it->object = p->u.image.object;
6085 it->slice = p->u.image.slice;
6086 break;
6087 case GET_FROM_XWIDGET:
6088 it->object = p->u.xwidget.object;
6089 break;
6090 case GET_FROM_STRETCH:
6091 it->object = p->u.stretch.object;
6092 break;
6093 case GET_FROM_BUFFER:
6094 it->object = it->w->contents;
6095 break;
6096 case GET_FROM_STRING:
6097 {
6098 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6099
6100 /* Restore the face_box_p flag, since it could have been
6101 overwritten by the face of the object that we just finished
6102 displaying. */
6103 if (face)
6104 it->face_box_p = face->box != FACE_NO_BOX;
6105 it->object = it->string;
6106 }
6107 break;
6108 case GET_FROM_DISPLAY_VECTOR:
6109 if (it->s)
6110 it->method = GET_FROM_C_STRING;
6111 else if (STRINGP (it->string))
6112 it->method = GET_FROM_STRING;
6113 else
6114 {
6115 it->method = GET_FROM_BUFFER;
6116 it->object = it->w->contents;
6117 }
6118 break;
6119 case GET_FROM_C_STRING:
6120 break;
6121 default:
6122 emacs_abort ();
6123 }
6124 it->end_charpos = p->end_charpos;
6125 it->string_nchars = p->string_nchars;
6126 it->area = p->area;
6127 it->multibyte_p = p->multibyte_p;
6128 it->avoid_cursor_p = p->avoid_cursor_p;
6129 it->space_width = p->space_width;
6130 it->font_height = p->font_height;
6131 it->voffset = p->voffset;
6132 it->string_from_display_prop_p = p->string_from_display_prop_p;
6133 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6134 it->line_wrap = p->line_wrap;
6135 it->bidi_p = p->bidi_p;
6136 it->paragraph_embedding = p->paragraph_embedding;
6137 it->from_disp_prop_p = p->from_disp_prop_p;
6138 if (it->bidi_p)
6139 {
6140 bidi_pop_it (&it->bidi_it);
6141 /* Bidi-iterate until we get out of the portion of text, if any,
6142 covered by a `display' text property or by an overlay with
6143 `display' property. (We cannot just jump there, because the
6144 internal coherency of the bidi iterator state can not be
6145 preserved across such jumps.) We also must determine the
6146 paragraph base direction if the overlay we just processed is
6147 at the beginning of a new paragraph. */
6148 if (from_display_prop
6149 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6150 iterate_out_of_display_property (it);
6151
6152 eassert ((BUFFERP (it->object)
6153 && IT_CHARPOS (*it) == it->bidi_it.charpos
6154 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6155 || (STRINGP (it->object)
6156 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6157 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6158 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6159 }
6160 /* If we move the iterator over text covered by a display property
6161 to a new buffer position, any info about previously seen overlays
6162 is no longer valid. */
6163 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6164 it->ignore_overlay_strings_at_pos_p = false;
6165 }
6166
6167
6168 \f
6169 /***********************************************************************
6170 Moving over lines
6171 ***********************************************************************/
6172
6173 /* Set IT's current position to the previous line start. */
6174
6175 static void
6176 back_to_previous_line_start (struct it *it)
6177 {
6178 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6179
6180 DEC_BOTH (cp, bp);
6181 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6182 }
6183
6184
6185 /* Move IT to the next line start.
6186
6187 Value is true if a newline was found. Set *SKIPPED_P to true if
6188 we skipped over part of the text (as opposed to moving the iterator
6189 continuously over the text). Otherwise, don't change the value
6190 of *SKIPPED_P.
6191
6192 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6193 iterator on the newline, if it was found.
6194
6195 Newlines may come from buffer text, overlay strings, or strings
6196 displayed via the `display' property. That's the reason we can't
6197 simply use find_newline_no_quit.
6198
6199 Note that this function may not skip over invisible text that is so
6200 because of text properties and immediately follows a newline. If
6201 it would, function reseat_at_next_visible_line_start, when called
6202 from set_iterator_to_next, would effectively make invisible
6203 characters following a newline part of the wrong glyph row, which
6204 leads to wrong cursor motion. */
6205
6206 static bool
6207 forward_to_next_line_start (struct it *it, bool *skipped_p,
6208 struct bidi_it *bidi_it_prev)
6209 {
6210 ptrdiff_t old_selective;
6211 bool newline_found_p = false;
6212 int n;
6213 const int MAX_NEWLINE_DISTANCE = 500;
6214
6215 /* If already on a newline, just consume it to avoid unintended
6216 skipping over invisible text below. */
6217 if (it->what == IT_CHARACTER
6218 && it->c == '\n'
6219 && CHARPOS (it->position) == IT_CHARPOS (*it))
6220 {
6221 if (it->bidi_p && bidi_it_prev)
6222 *bidi_it_prev = it->bidi_it;
6223 set_iterator_to_next (it, false);
6224 it->c = 0;
6225 return true;
6226 }
6227
6228 /* Don't handle selective display in the following. It's (a)
6229 unnecessary because it's done by the caller, and (b) leads to an
6230 infinite recursion because next_element_from_ellipsis indirectly
6231 calls this function. */
6232 old_selective = it->selective;
6233 it->selective = 0;
6234
6235 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6236 from buffer text. */
6237 for (n = 0;
6238 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6239 n += !STRINGP (it->string))
6240 {
6241 if (!get_next_display_element (it))
6242 return false;
6243 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6244 if (newline_found_p && it->bidi_p && bidi_it_prev)
6245 *bidi_it_prev = it->bidi_it;
6246 set_iterator_to_next (it, false);
6247 }
6248
6249 /* If we didn't find a newline near enough, see if we can use a
6250 short-cut. */
6251 if (!newline_found_p)
6252 {
6253 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6254 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6255 1, &bytepos);
6256 Lisp_Object pos;
6257
6258 eassert (!STRINGP (it->string));
6259
6260 /* If there isn't any `display' property in sight, and no
6261 overlays, we can just use the position of the newline in
6262 buffer text. */
6263 if (it->stop_charpos >= limit
6264 || ((pos = Fnext_single_property_change (make_number (start),
6265 Qdisplay, Qnil,
6266 make_number (limit)),
6267 NILP (pos))
6268 && next_overlay_change (start) == ZV))
6269 {
6270 if (!it->bidi_p)
6271 {
6272 IT_CHARPOS (*it) = limit;
6273 IT_BYTEPOS (*it) = bytepos;
6274 }
6275 else
6276 {
6277 struct bidi_it bprev;
6278
6279 /* Help bidi.c avoid expensive searches for display
6280 properties and overlays, by telling it that there are
6281 none up to `limit'. */
6282 if (it->bidi_it.disp_pos < limit)
6283 {
6284 it->bidi_it.disp_pos = limit;
6285 it->bidi_it.disp_prop = 0;
6286 }
6287 do {
6288 bprev = it->bidi_it;
6289 bidi_move_to_visually_next (&it->bidi_it);
6290 } while (it->bidi_it.charpos != limit);
6291 IT_CHARPOS (*it) = limit;
6292 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6293 if (bidi_it_prev)
6294 *bidi_it_prev = bprev;
6295 }
6296 *skipped_p = newline_found_p = true;
6297 }
6298 else
6299 {
6300 while (get_next_display_element (it)
6301 && !newline_found_p)
6302 {
6303 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6304 if (newline_found_p && it->bidi_p && bidi_it_prev)
6305 *bidi_it_prev = it->bidi_it;
6306 set_iterator_to_next (it, false);
6307 }
6308 }
6309 }
6310
6311 it->selective = old_selective;
6312 return newline_found_p;
6313 }
6314
6315
6316 /* Set IT's current position to the previous visible line start. Skip
6317 invisible text that is so either due to text properties or due to
6318 selective display. Caution: this does not change IT->current_x and
6319 IT->hpos. */
6320
6321 static void
6322 back_to_previous_visible_line_start (struct it *it)
6323 {
6324 while (IT_CHARPOS (*it) > BEGV)
6325 {
6326 back_to_previous_line_start (it);
6327
6328 if (IT_CHARPOS (*it) <= BEGV)
6329 break;
6330
6331 /* If selective > 0, then lines indented more than its value are
6332 invisible. */
6333 if (it->selective > 0
6334 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6335 it->selective))
6336 continue;
6337
6338 /* Check the newline before point for invisibility. */
6339 {
6340 Lisp_Object prop;
6341 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6342 Qinvisible, it->window);
6343 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6344 continue;
6345 }
6346
6347 if (IT_CHARPOS (*it) <= BEGV)
6348 break;
6349
6350 {
6351 struct it it2;
6352 void *it2data = NULL;
6353 ptrdiff_t pos;
6354 ptrdiff_t beg, end;
6355 Lisp_Object val, overlay;
6356
6357 SAVE_IT (it2, *it, it2data);
6358
6359 /* If newline is part of a composition, continue from start of composition */
6360 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6361 && beg < IT_CHARPOS (*it))
6362 goto replaced;
6363
6364 /* If newline is replaced by a display property, find start of overlay
6365 or interval and continue search from that point. */
6366 pos = --IT_CHARPOS (it2);
6367 --IT_BYTEPOS (it2);
6368 it2.sp = 0;
6369 bidi_unshelve_cache (NULL, false);
6370 it2.string_from_display_prop_p = false;
6371 it2.from_disp_prop_p = false;
6372 if (handle_display_prop (&it2) == HANDLED_RETURN
6373 && !NILP (val = get_char_property_and_overlay
6374 (make_number (pos), Qdisplay, Qnil, &overlay))
6375 && (OVERLAYP (overlay)
6376 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6377 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6378 {
6379 RESTORE_IT (it, it, it2data);
6380 goto replaced;
6381 }
6382
6383 /* Newline is not replaced by anything -- so we are done. */
6384 RESTORE_IT (it, it, it2data);
6385 break;
6386
6387 replaced:
6388 if (beg < BEGV)
6389 beg = BEGV;
6390 IT_CHARPOS (*it) = beg;
6391 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6392 }
6393 }
6394
6395 it->continuation_lines_width = 0;
6396
6397 eassert (IT_CHARPOS (*it) >= BEGV);
6398 eassert (IT_CHARPOS (*it) == BEGV
6399 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6400 CHECK_IT (it);
6401 }
6402
6403
6404 /* Reseat iterator IT at the previous visible line start. Skip
6405 invisible text that is so either due to text properties or due to
6406 selective display. At the end, update IT's overlay information,
6407 face information etc. */
6408
6409 void
6410 reseat_at_previous_visible_line_start (struct it *it)
6411 {
6412 back_to_previous_visible_line_start (it);
6413 reseat (it, it->current.pos, true);
6414 CHECK_IT (it);
6415 }
6416
6417
6418 /* Reseat iterator IT on the next visible line start in the current
6419 buffer. ON_NEWLINE_P means position IT on the newline
6420 preceding the line start. Skip over invisible text that is so
6421 because of selective display. Compute faces, overlays etc at the
6422 new position. Note that this function does not skip over text that
6423 is invisible because of text properties. */
6424
6425 static void
6426 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6427 {
6428 bool skipped_p = false;
6429 struct bidi_it bidi_it_prev;
6430 bool newline_found_p
6431 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6432
6433 /* Skip over lines that are invisible because they are indented
6434 more than the value of IT->selective. */
6435 if (it->selective > 0)
6436 while (IT_CHARPOS (*it) < ZV
6437 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6438 it->selective))
6439 {
6440 eassert (IT_BYTEPOS (*it) == BEGV
6441 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6442 newline_found_p =
6443 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6444 }
6445
6446 /* Position on the newline if that's what's requested. */
6447 if (on_newline_p && newline_found_p)
6448 {
6449 if (STRINGP (it->string))
6450 {
6451 if (IT_STRING_CHARPOS (*it) > 0)
6452 {
6453 if (!it->bidi_p)
6454 {
6455 --IT_STRING_CHARPOS (*it);
6456 --IT_STRING_BYTEPOS (*it);
6457 }
6458 else
6459 {
6460 /* We need to restore the bidi iterator to the state
6461 it had on the newline, and resync the IT's
6462 position with that. */
6463 it->bidi_it = bidi_it_prev;
6464 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6465 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6466 }
6467 }
6468 }
6469 else if (IT_CHARPOS (*it) > BEGV)
6470 {
6471 if (!it->bidi_p)
6472 {
6473 --IT_CHARPOS (*it);
6474 --IT_BYTEPOS (*it);
6475 }
6476 else
6477 {
6478 /* We need to restore the bidi iterator to the state it
6479 had on the newline and resync IT with that. */
6480 it->bidi_it = bidi_it_prev;
6481 IT_CHARPOS (*it) = it->bidi_it.charpos;
6482 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6483 }
6484 reseat (it, it->current.pos, false);
6485 }
6486 }
6487 else if (skipped_p)
6488 reseat (it, it->current.pos, false);
6489
6490 CHECK_IT (it);
6491 }
6492
6493
6494 \f
6495 /***********************************************************************
6496 Changing an iterator's position
6497 ***********************************************************************/
6498
6499 /* Change IT's current position to POS in current_buffer.
6500 If FORCE_P, always check for text properties at the new position.
6501 Otherwise, text properties are only looked up if POS >=
6502 IT->check_charpos of a property. */
6503
6504 static void
6505 reseat (struct it *it, struct text_pos pos, bool force_p)
6506 {
6507 ptrdiff_t original_pos = IT_CHARPOS (*it);
6508
6509 reseat_1 (it, pos, false);
6510
6511 /* Determine where to check text properties. Avoid doing it
6512 where possible because text property lookup is very expensive. */
6513 if (force_p
6514 || CHARPOS (pos) > it->stop_charpos
6515 || CHARPOS (pos) < original_pos)
6516 {
6517 if (it->bidi_p)
6518 {
6519 /* For bidi iteration, we need to prime prev_stop and
6520 base_level_stop with our best estimations. */
6521 /* Implementation note: Of course, POS is not necessarily a
6522 stop position, so assigning prev_pos to it is a lie; we
6523 should have called compute_stop_backwards. However, if
6524 the current buffer does not include any R2L characters,
6525 that call would be a waste of cycles, because the
6526 iterator will never move back, and thus never cross this
6527 "fake" stop position. So we delay that backward search
6528 until the time we really need it, in next_element_from_buffer. */
6529 if (CHARPOS (pos) != it->prev_stop)
6530 it->prev_stop = CHARPOS (pos);
6531 if (CHARPOS (pos) < it->base_level_stop)
6532 it->base_level_stop = 0; /* meaning it's unknown */
6533 handle_stop (it);
6534 }
6535 else
6536 {
6537 handle_stop (it);
6538 it->prev_stop = it->base_level_stop = 0;
6539 }
6540
6541 }
6542
6543 CHECK_IT (it);
6544 }
6545
6546
6547 /* Change IT's buffer position to POS. SET_STOP_P means set
6548 IT->stop_pos to POS, also. */
6549
6550 static void
6551 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6552 {
6553 /* Don't call this function when scanning a C string. */
6554 eassert (it->s == NULL);
6555
6556 /* POS must be a reasonable value. */
6557 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6558
6559 it->current.pos = it->position = pos;
6560 it->end_charpos = ZV;
6561 it->dpvec = NULL;
6562 it->current.dpvec_index = -1;
6563 it->current.overlay_string_index = -1;
6564 IT_STRING_CHARPOS (*it) = -1;
6565 IT_STRING_BYTEPOS (*it) = -1;
6566 it->string = Qnil;
6567 it->method = GET_FROM_BUFFER;
6568 it->object = it->w->contents;
6569 it->area = TEXT_AREA;
6570 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6571 it->sp = 0;
6572 it->string_from_display_prop_p = false;
6573 it->string_from_prefix_prop_p = false;
6574
6575 it->from_disp_prop_p = false;
6576 it->face_before_selective_p = false;
6577 if (it->bidi_p)
6578 {
6579 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6580 &it->bidi_it);
6581 bidi_unshelve_cache (NULL, false);
6582 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6583 it->bidi_it.string.s = NULL;
6584 it->bidi_it.string.lstring = Qnil;
6585 it->bidi_it.string.bufpos = 0;
6586 it->bidi_it.string.from_disp_str = false;
6587 it->bidi_it.string.unibyte = false;
6588 it->bidi_it.w = it->w;
6589 }
6590
6591 if (set_stop_p)
6592 {
6593 it->stop_charpos = CHARPOS (pos);
6594 it->base_level_stop = CHARPOS (pos);
6595 }
6596 /* This make the information stored in it->cmp_it invalidate. */
6597 it->cmp_it.id = -1;
6598 }
6599
6600
6601 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6602 If S is non-null, it is a C string to iterate over. Otherwise,
6603 STRING gives a Lisp string to iterate over.
6604
6605 If PRECISION > 0, don't return more then PRECISION number of
6606 characters from the string.
6607
6608 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6609 characters have been returned. FIELD_WIDTH < 0 means an infinite
6610 field width.
6611
6612 MULTIBYTE = 0 means disable processing of multibyte characters,
6613 MULTIBYTE > 0 means enable it,
6614 MULTIBYTE < 0 means use IT->multibyte_p.
6615
6616 IT must be initialized via a prior call to init_iterator before
6617 calling this function. */
6618
6619 static void
6620 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6621 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6622 int multibyte)
6623 {
6624 /* No text property checks performed by default, but see below. */
6625 it->stop_charpos = -1;
6626
6627 /* Set iterator position and end position. */
6628 memset (&it->current, 0, sizeof it->current);
6629 it->current.overlay_string_index = -1;
6630 it->current.dpvec_index = -1;
6631 eassert (charpos >= 0);
6632
6633 /* If STRING is specified, use its multibyteness, otherwise use the
6634 setting of MULTIBYTE, if specified. */
6635 if (multibyte >= 0)
6636 it->multibyte_p = multibyte > 0;
6637
6638 /* Bidirectional reordering of strings is controlled by the default
6639 value of bidi-display-reordering. Don't try to reorder while
6640 loading loadup.el, as the necessary character property tables are
6641 not yet available. */
6642 it->bidi_p =
6643 NILP (Vpurify_flag)
6644 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6645
6646 if (s == NULL)
6647 {
6648 eassert (STRINGP (string));
6649 it->string = string;
6650 it->s = NULL;
6651 it->end_charpos = it->string_nchars = SCHARS (string);
6652 it->method = GET_FROM_STRING;
6653 it->current.string_pos = string_pos (charpos, string);
6654
6655 if (it->bidi_p)
6656 {
6657 it->bidi_it.string.lstring = string;
6658 it->bidi_it.string.s = NULL;
6659 it->bidi_it.string.schars = it->end_charpos;
6660 it->bidi_it.string.bufpos = 0;
6661 it->bidi_it.string.from_disp_str = false;
6662 it->bidi_it.string.unibyte = !it->multibyte_p;
6663 it->bidi_it.w = it->w;
6664 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6665 FRAME_WINDOW_P (it->f), &it->bidi_it);
6666 }
6667 }
6668 else
6669 {
6670 it->s = (const unsigned char *) s;
6671 it->string = Qnil;
6672
6673 /* Note that we use IT->current.pos, not it->current.string_pos,
6674 for displaying C strings. */
6675 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6676 if (it->multibyte_p)
6677 {
6678 it->current.pos = c_string_pos (charpos, s, true);
6679 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6680 }
6681 else
6682 {
6683 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6684 it->end_charpos = it->string_nchars = strlen (s);
6685 }
6686
6687 if (it->bidi_p)
6688 {
6689 it->bidi_it.string.lstring = Qnil;
6690 it->bidi_it.string.s = (const unsigned char *) s;
6691 it->bidi_it.string.schars = it->end_charpos;
6692 it->bidi_it.string.bufpos = 0;
6693 it->bidi_it.string.from_disp_str = false;
6694 it->bidi_it.string.unibyte = !it->multibyte_p;
6695 it->bidi_it.w = it->w;
6696 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6697 &it->bidi_it);
6698 }
6699 it->method = GET_FROM_C_STRING;
6700 }
6701
6702 /* PRECISION > 0 means don't return more than PRECISION characters
6703 from the string. */
6704 if (precision > 0 && it->end_charpos - charpos > precision)
6705 {
6706 it->end_charpos = it->string_nchars = charpos + precision;
6707 if (it->bidi_p)
6708 it->bidi_it.string.schars = it->end_charpos;
6709 }
6710
6711 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6712 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6713 FIELD_WIDTH < 0 means infinite field width. This is useful for
6714 padding with `-' at the end of a mode line. */
6715 if (field_width < 0)
6716 field_width = INFINITY;
6717 /* Implementation note: We deliberately don't enlarge
6718 it->bidi_it.string.schars here to fit it->end_charpos, because
6719 the bidi iterator cannot produce characters out of thin air. */
6720 if (field_width > it->end_charpos - charpos)
6721 it->end_charpos = charpos + field_width;
6722
6723 /* Use the standard display table for displaying strings. */
6724 if (DISP_TABLE_P (Vstandard_display_table))
6725 it->dp = XCHAR_TABLE (Vstandard_display_table);
6726
6727 it->stop_charpos = charpos;
6728 it->prev_stop = charpos;
6729 it->base_level_stop = 0;
6730 if (it->bidi_p)
6731 {
6732 it->bidi_it.first_elt = true;
6733 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6734 it->bidi_it.disp_pos = -1;
6735 }
6736 if (s == NULL && it->multibyte_p)
6737 {
6738 ptrdiff_t endpos = SCHARS (it->string);
6739 if (endpos > it->end_charpos)
6740 endpos = it->end_charpos;
6741 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6742 it->string);
6743 }
6744 CHECK_IT (it);
6745 }
6746
6747
6748 \f
6749 /***********************************************************************
6750 Iteration
6751 ***********************************************************************/
6752
6753 /* Map enum it_method value to corresponding next_element_from_* function. */
6754
6755 typedef bool (*next_element_function) (struct it *);
6756
6757 static next_element_function const get_next_element[NUM_IT_METHODS] =
6758 {
6759 next_element_from_buffer,
6760 next_element_from_display_vector,
6761 next_element_from_string,
6762 next_element_from_c_string,
6763 next_element_from_image,
6764 next_element_from_stretch,
6765 next_element_from_xwidget,
6766 };
6767
6768 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6769
6770
6771 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6772 (possibly with the following characters). */
6773
6774 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6775 ((IT)->cmp_it.id >= 0 \
6776 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6777 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6778 END_CHARPOS, (IT)->w, \
6779 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6780 (IT)->string)))
6781
6782
6783 /* Lookup the char-table Vglyphless_char_display for character C (-1
6784 if we want information for no-font case), and return the display
6785 method symbol. By side-effect, update it->what and
6786 it->glyphless_method. This function is called from
6787 get_next_display_element for each character element, and from
6788 x_produce_glyphs when no suitable font was found. */
6789
6790 Lisp_Object
6791 lookup_glyphless_char_display (int c, struct it *it)
6792 {
6793 Lisp_Object glyphless_method = Qnil;
6794
6795 if (CHAR_TABLE_P (Vglyphless_char_display)
6796 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6797 {
6798 if (c >= 0)
6799 {
6800 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6801 if (CONSP (glyphless_method))
6802 glyphless_method = FRAME_WINDOW_P (it->f)
6803 ? XCAR (glyphless_method)
6804 : XCDR (glyphless_method);
6805 }
6806 else
6807 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6808 }
6809
6810 retry:
6811 if (NILP (glyphless_method))
6812 {
6813 if (c >= 0)
6814 /* The default is to display the character by a proper font. */
6815 return Qnil;
6816 /* The default for the no-font case is to display an empty box. */
6817 glyphless_method = Qempty_box;
6818 }
6819 if (EQ (glyphless_method, Qzero_width))
6820 {
6821 if (c >= 0)
6822 return glyphless_method;
6823 /* This method can't be used for the no-font case. */
6824 glyphless_method = Qempty_box;
6825 }
6826 if (EQ (glyphless_method, Qthin_space))
6827 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6828 else if (EQ (glyphless_method, Qempty_box))
6829 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6830 else if (EQ (glyphless_method, Qhex_code))
6831 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6832 else if (STRINGP (glyphless_method))
6833 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6834 else
6835 {
6836 /* Invalid value. We use the default method. */
6837 glyphless_method = Qnil;
6838 goto retry;
6839 }
6840 it->what = IT_GLYPHLESS;
6841 return glyphless_method;
6842 }
6843
6844 /* Merge escape glyph face and cache the result. */
6845
6846 static struct frame *last_escape_glyph_frame = NULL;
6847 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6848 static int last_escape_glyph_merged_face_id = 0;
6849
6850 static int
6851 merge_escape_glyph_face (struct it *it)
6852 {
6853 int face_id;
6854
6855 if (it->f == last_escape_glyph_frame
6856 && it->face_id == last_escape_glyph_face_id)
6857 face_id = last_escape_glyph_merged_face_id;
6858 else
6859 {
6860 /* Merge the `escape-glyph' face into the current face. */
6861 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6862 last_escape_glyph_frame = it->f;
6863 last_escape_glyph_face_id = it->face_id;
6864 last_escape_glyph_merged_face_id = face_id;
6865 }
6866 return face_id;
6867 }
6868
6869 /* Likewise for glyphless glyph face. */
6870
6871 static struct frame *last_glyphless_glyph_frame = NULL;
6872 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6873 static int last_glyphless_glyph_merged_face_id = 0;
6874
6875 int
6876 merge_glyphless_glyph_face (struct it *it)
6877 {
6878 int face_id;
6879
6880 if (it->f == last_glyphless_glyph_frame
6881 && it->face_id == last_glyphless_glyph_face_id)
6882 face_id = last_glyphless_glyph_merged_face_id;
6883 else
6884 {
6885 /* Merge the `glyphless-char' face into the current face. */
6886 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6887 last_glyphless_glyph_frame = it->f;
6888 last_glyphless_glyph_face_id = it->face_id;
6889 last_glyphless_glyph_merged_face_id = face_id;
6890 }
6891 return face_id;
6892 }
6893
6894 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6895 be called before redisplaying windows, and when the frame's face
6896 cache is freed. */
6897 void
6898 forget_escape_and_glyphless_faces (void)
6899 {
6900 last_escape_glyph_frame = NULL;
6901 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6902 last_glyphless_glyph_frame = NULL;
6903 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6904 }
6905
6906 /* Load IT's display element fields with information about the next
6907 display element from the current position of IT. Value is false if
6908 end of buffer (or C string) is reached. */
6909
6910 static bool
6911 get_next_display_element (struct it *it)
6912 {
6913 /* True means that we found a display element. False means that
6914 we hit the end of what we iterate over. Performance note: the
6915 function pointer `method' used here turns out to be faster than
6916 using a sequence of if-statements. */
6917 bool success_p;
6918
6919 get_next:
6920 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6921
6922 if (it->what == IT_CHARACTER)
6923 {
6924 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6925 and only if (a) the resolved directionality of that character
6926 is R..." */
6927 /* FIXME: Do we need an exception for characters from display
6928 tables? */
6929 if (it->bidi_p && it->bidi_it.type == STRONG_R
6930 && !inhibit_bidi_mirroring)
6931 it->c = bidi_mirror_char (it->c);
6932 /* Map via display table or translate control characters.
6933 IT->c, IT->len etc. have been set to the next character by
6934 the function call above. If we have a display table, and it
6935 contains an entry for IT->c, translate it. Don't do this if
6936 IT->c itself comes from a display table, otherwise we could
6937 end up in an infinite recursion. (An alternative could be to
6938 count the recursion depth of this function and signal an
6939 error when a certain maximum depth is reached.) Is it worth
6940 it? */
6941 if (success_p && it->dpvec == NULL)
6942 {
6943 Lisp_Object dv;
6944 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6945 bool nonascii_space_p = false;
6946 bool nonascii_hyphen_p = false;
6947 int c = it->c; /* This is the character to display. */
6948
6949 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6950 {
6951 eassert (SINGLE_BYTE_CHAR_P (c));
6952 if (unibyte_display_via_language_environment)
6953 {
6954 c = DECODE_CHAR (unibyte, c);
6955 if (c < 0)
6956 c = BYTE8_TO_CHAR (it->c);
6957 }
6958 else
6959 c = BYTE8_TO_CHAR (it->c);
6960 }
6961
6962 if (it->dp
6963 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6964 VECTORP (dv)))
6965 {
6966 struct Lisp_Vector *v = XVECTOR (dv);
6967
6968 /* Return the first character from the display table
6969 entry, if not empty. If empty, don't display the
6970 current character. */
6971 if (v->header.size)
6972 {
6973 it->dpvec_char_len = it->len;
6974 it->dpvec = v->contents;
6975 it->dpend = v->contents + v->header.size;
6976 it->current.dpvec_index = 0;
6977 it->dpvec_face_id = -1;
6978 it->saved_face_id = it->face_id;
6979 it->method = GET_FROM_DISPLAY_VECTOR;
6980 it->ellipsis_p = false;
6981 }
6982 else
6983 {
6984 set_iterator_to_next (it, false);
6985 }
6986 goto get_next;
6987 }
6988
6989 if (! NILP (lookup_glyphless_char_display (c, it)))
6990 {
6991 if (it->what == IT_GLYPHLESS)
6992 goto done;
6993 /* Don't display this character. */
6994 set_iterator_to_next (it, false);
6995 goto get_next;
6996 }
6997
6998 /* If `nobreak-char-display' is non-nil, we display
6999 non-ASCII spaces and hyphens specially. */
7000 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7001 {
7002 if (c == NO_BREAK_SPACE)
7003 nonascii_space_p = true;
7004 else if (c == SOFT_HYPHEN || c == HYPHEN
7005 || c == NON_BREAKING_HYPHEN)
7006 nonascii_hyphen_p = true;
7007 }
7008
7009 /* Translate control characters into `\003' or `^C' form.
7010 Control characters coming from a display table entry are
7011 currently not translated because we use IT->dpvec to hold
7012 the translation. This could easily be changed but I
7013 don't believe that it is worth doing.
7014
7015 The characters handled by `nobreak-char-display' must be
7016 translated too.
7017
7018 Non-printable characters and raw-byte characters are also
7019 translated to octal form. */
7020 if (((c < ' ' || c == 127) /* ASCII control chars. */
7021 ? (it->area != TEXT_AREA
7022 /* In mode line, treat \n, \t like other crl chars. */
7023 || (c != '\t'
7024 && it->glyph_row
7025 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7026 || (c != '\n' && c != '\t'))
7027 : (nonascii_space_p
7028 || nonascii_hyphen_p
7029 || CHAR_BYTE8_P (c)
7030 || ! CHAR_PRINTABLE_P (c))))
7031 {
7032 /* C is a control character, non-ASCII space/hyphen,
7033 raw-byte, or a non-printable character which must be
7034 displayed either as '\003' or as `^C' where the '\\'
7035 and '^' can be defined in the display table. Fill
7036 IT->ctl_chars with glyphs for what we have to
7037 display. Then, set IT->dpvec to these glyphs. */
7038 Lisp_Object gc;
7039 int ctl_len;
7040 int face_id;
7041 int lface_id = 0;
7042 int escape_glyph;
7043
7044 /* Handle control characters with ^. */
7045
7046 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7047 {
7048 int g;
7049
7050 g = '^'; /* default glyph for Control */
7051 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7052 if (it->dp
7053 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7054 {
7055 g = GLYPH_CODE_CHAR (gc);
7056 lface_id = GLYPH_CODE_FACE (gc);
7057 }
7058
7059 face_id = (lface_id
7060 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7061 : merge_escape_glyph_face (it));
7062
7063 XSETINT (it->ctl_chars[0], g);
7064 XSETINT (it->ctl_chars[1], c ^ 0100);
7065 ctl_len = 2;
7066 goto display_control;
7067 }
7068
7069 /* Handle non-ascii space in the mode where it only gets
7070 highlighting. */
7071
7072 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7073 {
7074 /* Merge `nobreak-space' into the current face. */
7075 face_id = merge_faces (it->f, Qnobreak_space, 0,
7076 it->face_id);
7077 XSETINT (it->ctl_chars[0], ' ');
7078 ctl_len = 1;
7079 goto display_control;
7080 }
7081
7082 /* Handle sequences that start with the "escape glyph". */
7083
7084 /* the default escape glyph is \. */
7085 escape_glyph = '\\';
7086
7087 if (it->dp
7088 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7089 {
7090 escape_glyph = GLYPH_CODE_CHAR (gc);
7091 lface_id = GLYPH_CODE_FACE (gc);
7092 }
7093
7094 face_id = (lface_id
7095 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7096 : merge_escape_glyph_face (it));
7097
7098 /* Draw non-ASCII hyphen with just highlighting: */
7099
7100 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7101 {
7102 XSETINT (it->ctl_chars[0], '-');
7103 ctl_len = 1;
7104 goto display_control;
7105 }
7106
7107 /* Draw non-ASCII space/hyphen with escape glyph: */
7108
7109 if (nonascii_space_p || nonascii_hyphen_p)
7110 {
7111 XSETINT (it->ctl_chars[0], escape_glyph);
7112 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7113 ctl_len = 2;
7114 goto display_control;
7115 }
7116
7117 {
7118 char str[10];
7119 int len, i;
7120
7121 if (CHAR_BYTE8_P (c))
7122 /* Display \200 instead of \17777600. */
7123 c = CHAR_TO_BYTE8 (c);
7124 len = sprintf (str, "%03o", c + 0u);
7125
7126 XSETINT (it->ctl_chars[0], escape_glyph);
7127 for (i = 0; i < len; i++)
7128 XSETINT (it->ctl_chars[i + 1], str[i]);
7129 ctl_len = len + 1;
7130 }
7131
7132 display_control:
7133 /* Set up IT->dpvec and return first character from it. */
7134 it->dpvec_char_len = it->len;
7135 it->dpvec = it->ctl_chars;
7136 it->dpend = it->dpvec + ctl_len;
7137 it->current.dpvec_index = 0;
7138 it->dpvec_face_id = face_id;
7139 it->saved_face_id = it->face_id;
7140 it->method = GET_FROM_DISPLAY_VECTOR;
7141 it->ellipsis_p = false;
7142 goto get_next;
7143 }
7144 it->char_to_display = c;
7145 }
7146 else if (success_p)
7147 {
7148 it->char_to_display = it->c;
7149 }
7150 }
7151
7152 #ifdef HAVE_WINDOW_SYSTEM
7153 /* Adjust face id for a multibyte character. There are no multibyte
7154 character in unibyte text. */
7155 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7156 && it->multibyte_p
7157 && success_p
7158 && FRAME_WINDOW_P (it->f))
7159 {
7160 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7161
7162 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7163 {
7164 /* Automatic composition with glyph-string. */
7165 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7166
7167 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7168 }
7169 else
7170 {
7171 ptrdiff_t pos = (it->s ? -1
7172 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7173 : IT_CHARPOS (*it));
7174 int c;
7175
7176 if (it->what == IT_CHARACTER)
7177 c = it->char_to_display;
7178 else
7179 {
7180 struct composition *cmp = composition_table[it->cmp_it.id];
7181 int i;
7182
7183 c = ' ';
7184 for (i = 0; i < cmp->glyph_len; i++)
7185 /* TAB in a composition means display glyphs with
7186 padding space on the left or right. */
7187 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7188 break;
7189 }
7190 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7191 }
7192 }
7193 #endif /* HAVE_WINDOW_SYSTEM */
7194
7195 done:
7196 /* Is this character the last one of a run of characters with
7197 box? If yes, set IT->end_of_box_run_p to true. */
7198 if (it->face_box_p
7199 && it->s == NULL)
7200 {
7201 if (it->method == GET_FROM_STRING && it->sp)
7202 {
7203 int face_id = underlying_face_id (it);
7204 struct face *face = FACE_FROM_ID (it->f, face_id);
7205
7206 if (face)
7207 {
7208 if (face->box == FACE_NO_BOX)
7209 {
7210 /* If the box comes from face properties in a
7211 display string, check faces in that string. */
7212 int string_face_id = face_after_it_pos (it);
7213 it->end_of_box_run_p
7214 = (FACE_FROM_ID (it->f, string_face_id)->box
7215 == FACE_NO_BOX);
7216 }
7217 /* Otherwise, the box comes from the underlying face.
7218 If this is the last string character displayed, check
7219 the next buffer location. */
7220 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7221 /* n_overlay_strings is unreliable unless
7222 overlay_string_index is non-negative. */
7223 && ((it->current.overlay_string_index >= 0
7224 && (it->current.overlay_string_index
7225 == it->n_overlay_strings - 1))
7226 /* A string from display property. */
7227 || it->from_disp_prop_p))
7228 {
7229 ptrdiff_t ignore;
7230 int next_face_id;
7231 struct text_pos pos = it->current.pos;
7232
7233 /* For a string from a display property, the next
7234 buffer position is stored in the 'position'
7235 member of the iteration stack slot below the
7236 current one, see handle_single_display_spec. By
7237 contrast, it->current.pos was is not yet updated
7238 to point to that buffer position; that will
7239 happen in pop_it, after we finish displaying the
7240 current string. Note that we already checked
7241 above that it->sp is positive, so subtracting one
7242 from it is safe. */
7243 if (it->from_disp_prop_p)
7244 pos = (it->stack + it->sp - 1)->position;
7245 else
7246 INC_TEXT_POS (pos, it->multibyte_p);
7247
7248 if (CHARPOS (pos) >= ZV)
7249 it->end_of_box_run_p = true;
7250 else
7251 {
7252 next_face_id = face_at_buffer_position
7253 (it->w, CHARPOS (pos), &ignore,
7254 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, false, -1);
7255 it->end_of_box_run_p
7256 = (FACE_FROM_ID (it->f, next_face_id)->box
7257 == FACE_NO_BOX);
7258 }
7259 }
7260 }
7261 }
7262 /* next_element_from_display_vector sets this flag according to
7263 faces of the display vector glyphs, see there. */
7264 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7265 {
7266 int face_id = face_after_it_pos (it);
7267 it->end_of_box_run_p
7268 = (face_id != it->face_id
7269 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7270 }
7271 }
7272 /* If we reached the end of the object we've been iterating (e.g., a
7273 display string or an overlay string), and there's something on
7274 IT->stack, proceed with what's on the stack. It doesn't make
7275 sense to return false if there's unprocessed stuff on the stack,
7276 because otherwise that stuff will never be displayed. */
7277 if (!success_p && it->sp > 0)
7278 {
7279 set_iterator_to_next (it, false);
7280 success_p = get_next_display_element (it);
7281 }
7282
7283 /* Value is false if end of buffer or string reached. */
7284 return success_p;
7285 }
7286
7287
7288 /* Move IT to the next display element.
7289
7290 RESEAT_P means if called on a newline in buffer text,
7291 skip to the next visible line start.
7292
7293 Functions get_next_display_element and set_iterator_to_next are
7294 separate because I find this arrangement easier to handle than a
7295 get_next_display_element function that also increments IT's
7296 position. The way it is we can first look at an iterator's current
7297 display element, decide whether it fits on a line, and if it does,
7298 increment the iterator position. The other way around we probably
7299 would either need a flag indicating whether the iterator has to be
7300 incremented the next time, or we would have to implement a
7301 decrement position function which would not be easy to write. */
7302
7303 void
7304 set_iterator_to_next (struct it *it, bool reseat_p)
7305 {
7306 /* Reset flags indicating start and end of a sequence of characters
7307 with box. Reset them at the start of this function because
7308 moving the iterator to a new position might set them. */
7309 it->start_of_box_run_p = it->end_of_box_run_p = false;
7310
7311 switch (it->method)
7312 {
7313 case GET_FROM_BUFFER:
7314 /* The current display element of IT is a character from
7315 current_buffer. Advance in the buffer, and maybe skip over
7316 invisible lines that are so because of selective display. */
7317 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7318 reseat_at_next_visible_line_start (it, false);
7319 else if (it->cmp_it.id >= 0)
7320 {
7321 /* We are currently getting glyphs from a composition. */
7322 if (! it->bidi_p)
7323 {
7324 IT_CHARPOS (*it) += it->cmp_it.nchars;
7325 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7326 }
7327 else
7328 {
7329 int i;
7330
7331 /* Update IT's char/byte positions to point to the first
7332 character of the next grapheme cluster, or to the
7333 character visually after the current composition. */
7334 for (i = 0; i < it->cmp_it.nchars; i++)
7335 bidi_move_to_visually_next (&it->bidi_it);
7336 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7337 IT_CHARPOS (*it) = it->bidi_it.charpos;
7338 }
7339
7340 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7341 && it->cmp_it.to < it->cmp_it.nglyphs)
7342 {
7343 /* Composition created while scanning forward. Proceed
7344 to the next grapheme cluster. */
7345 it->cmp_it.from = it->cmp_it.to;
7346 }
7347 else if ((it->bidi_p && it->cmp_it.reversed_p)
7348 && it->cmp_it.from > 0)
7349 {
7350 /* Composition created while scanning backward. Proceed
7351 to the previous grapheme cluster. */
7352 it->cmp_it.to = it->cmp_it.from;
7353 }
7354 else
7355 {
7356 /* No more grapheme clusters in this composition.
7357 Find the next stop position. */
7358 ptrdiff_t stop = it->end_charpos;
7359
7360 if (it->bidi_it.scan_dir < 0)
7361 /* Now we are scanning backward and don't know
7362 where to stop. */
7363 stop = -1;
7364 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7365 IT_BYTEPOS (*it), stop, Qnil);
7366 }
7367 }
7368 else
7369 {
7370 eassert (it->len != 0);
7371
7372 if (!it->bidi_p)
7373 {
7374 IT_BYTEPOS (*it) += it->len;
7375 IT_CHARPOS (*it) += 1;
7376 }
7377 else
7378 {
7379 int prev_scan_dir = it->bidi_it.scan_dir;
7380 /* If this is a new paragraph, determine its base
7381 direction (a.k.a. its base embedding level). */
7382 if (it->bidi_it.new_paragraph)
7383 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7384 false);
7385 bidi_move_to_visually_next (&it->bidi_it);
7386 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7387 IT_CHARPOS (*it) = it->bidi_it.charpos;
7388 if (prev_scan_dir != it->bidi_it.scan_dir)
7389 {
7390 /* As the scan direction was changed, we must
7391 re-compute the stop position for composition. */
7392 ptrdiff_t stop = it->end_charpos;
7393 if (it->bidi_it.scan_dir < 0)
7394 stop = -1;
7395 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7396 IT_BYTEPOS (*it), stop, Qnil);
7397 }
7398 }
7399 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7400 }
7401 break;
7402
7403 case GET_FROM_C_STRING:
7404 /* Current display element of IT is from a C string. */
7405 if (!it->bidi_p
7406 /* If the string position is beyond string's end, it means
7407 next_element_from_c_string is padding the string with
7408 blanks, in which case we bypass the bidi iterator,
7409 because it cannot deal with such virtual characters. */
7410 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7411 {
7412 IT_BYTEPOS (*it) += it->len;
7413 IT_CHARPOS (*it) += 1;
7414 }
7415 else
7416 {
7417 bidi_move_to_visually_next (&it->bidi_it);
7418 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7419 IT_CHARPOS (*it) = it->bidi_it.charpos;
7420 }
7421 break;
7422
7423 case GET_FROM_DISPLAY_VECTOR:
7424 /* Current display element of IT is from a display table entry.
7425 Advance in the display table definition. Reset it to null if
7426 end reached, and continue with characters from buffers/
7427 strings. */
7428 ++it->current.dpvec_index;
7429
7430 /* Restore face of the iterator to what they were before the
7431 display vector entry (these entries may contain faces). */
7432 it->face_id = it->saved_face_id;
7433
7434 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7435 {
7436 bool recheck_faces = it->ellipsis_p;
7437
7438 if (it->s)
7439 it->method = GET_FROM_C_STRING;
7440 else if (STRINGP (it->string))
7441 it->method = GET_FROM_STRING;
7442 else
7443 {
7444 it->method = GET_FROM_BUFFER;
7445 it->object = it->w->contents;
7446 }
7447
7448 it->dpvec = NULL;
7449 it->current.dpvec_index = -1;
7450
7451 /* Skip over characters which were displayed via IT->dpvec. */
7452 if (it->dpvec_char_len < 0)
7453 reseat_at_next_visible_line_start (it, true);
7454 else if (it->dpvec_char_len > 0)
7455 {
7456 it->len = it->dpvec_char_len;
7457 set_iterator_to_next (it, reseat_p);
7458 }
7459
7460 /* Maybe recheck faces after display vector. */
7461 if (recheck_faces)
7462 {
7463 if (it->method == GET_FROM_STRING)
7464 it->stop_charpos = IT_STRING_CHARPOS (*it);
7465 else
7466 it->stop_charpos = IT_CHARPOS (*it);
7467 }
7468 }
7469 break;
7470
7471 case GET_FROM_STRING:
7472 /* Current display element is a character from a Lisp string. */
7473 eassert (it->s == NULL && STRINGP (it->string));
7474 /* Don't advance past string end. These conditions are true
7475 when set_iterator_to_next is called at the end of
7476 get_next_display_element, in which case the Lisp string is
7477 already exhausted, and all we want is pop the iterator
7478 stack. */
7479 if (it->current.overlay_string_index >= 0)
7480 {
7481 /* This is an overlay string, so there's no padding with
7482 spaces, and the number of characters in the string is
7483 where the string ends. */
7484 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7485 goto consider_string_end;
7486 }
7487 else
7488 {
7489 /* Not an overlay string. There could be padding, so test
7490 against it->end_charpos. */
7491 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7492 goto consider_string_end;
7493 }
7494 if (it->cmp_it.id >= 0)
7495 {
7496 /* We are delivering display elements from a composition.
7497 Update the string position past the grapheme cluster
7498 we've just processed. */
7499 if (! it->bidi_p)
7500 {
7501 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7502 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7503 }
7504 else
7505 {
7506 int i;
7507
7508 for (i = 0; i < it->cmp_it.nchars; i++)
7509 bidi_move_to_visually_next (&it->bidi_it);
7510 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7511 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7512 }
7513
7514 /* Did we exhaust all the grapheme clusters of this
7515 composition? */
7516 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7517 && (it->cmp_it.to < it->cmp_it.nglyphs))
7518 {
7519 /* Not all the grapheme clusters were processed yet;
7520 advance to the next cluster. */
7521 it->cmp_it.from = it->cmp_it.to;
7522 }
7523 else if ((it->bidi_p && it->cmp_it.reversed_p)
7524 && it->cmp_it.from > 0)
7525 {
7526 /* Likewise: advance to the next cluster, but going in
7527 the reverse direction. */
7528 it->cmp_it.to = it->cmp_it.from;
7529 }
7530 else
7531 {
7532 /* This composition was fully processed; find the next
7533 candidate place for checking for composed
7534 characters. */
7535 /* Always limit string searches to the string length;
7536 any padding spaces are not part of the string, and
7537 there cannot be any compositions in that padding. */
7538 ptrdiff_t stop = SCHARS (it->string);
7539
7540 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7541 stop = -1;
7542 else if (it->end_charpos < stop)
7543 {
7544 /* Cf. PRECISION in reseat_to_string: we might be
7545 limited in how many of the string characters we
7546 need to deliver. */
7547 stop = it->end_charpos;
7548 }
7549 composition_compute_stop_pos (&it->cmp_it,
7550 IT_STRING_CHARPOS (*it),
7551 IT_STRING_BYTEPOS (*it), stop,
7552 it->string);
7553 }
7554 }
7555 else
7556 {
7557 if (!it->bidi_p
7558 /* If the string position is beyond string's end, it
7559 means next_element_from_string is padding the string
7560 with blanks, in which case we bypass the bidi
7561 iterator, because it cannot deal with such virtual
7562 characters. */
7563 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7564 {
7565 IT_STRING_BYTEPOS (*it) += it->len;
7566 IT_STRING_CHARPOS (*it) += 1;
7567 }
7568 else
7569 {
7570 int prev_scan_dir = it->bidi_it.scan_dir;
7571
7572 bidi_move_to_visually_next (&it->bidi_it);
7573 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7574 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7575 /* If the scan direction changes, we may need to update
7576 the place where to check for composed characters. */
7577 if (prev_scan_dir != it->bidi_it.scan_dir)
7578 {
7579 ptrdiff_t stop = SCHARS (it->string);
7580
7581 if (it->bidi_it.scan_dir < 0)
7582 stop = -1;
7583 else if (it->end_charpos < stop)
7584 stop = it->end_charpos;
7585
7586 composition_compute_stop_pos (&it->cmp_it,
7587 IT_STRING_CHARPOS (*it),
7588 IT_STRING_BYTEPOS (*it), stop,
7589 it->string);
7590 }
7591 }
7592 }
7593
7594 consider_string_end:
7595
7596 if (it->current.overlay_string_index >= 0)
7597 {
7598 /* IT->string is an overlay string. Advance to the
7599 next, if there is one. */
7600 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7601 {
7602 it->ellipsis_p = false;
7603 next_overlay_string (it);
7604 if (it->ellipsis_p)
7605 setup_for_ellipsis (it, 0);
7606 }
7607 }
7608 else
7609 {
7610 /* IT->string is not an overlay string. If we reached
7611 its end, and there is something on IT->stack, proceed
7612 with what is on the stack. This can be either another
7613 string, this time an overlay string, or a buffer. */
7614 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7615 && it->sp > 0)
7616 {
7617 pop_it (it);
7618 if (it->method == GET_FROM_STRING)
7619 goto consider_string_end;
7620 }
7621 }
7622 break;
7623
7624 case GET_FROM_IMAGE:
7625 case GET_FROM_STRETCH:
7626 case GET_FROM_XWIDGET:
7627
7628 /* The position etc with which we have to proceed are on
7629 the stack. The position may be at the end of a string,
7630 if the `display' property takes up the whole string. */
7631 eassert (it->sp > 0);
7632 pop_it (it);
7633 if (it->method == GET_FROM_STRING)
7634 goto consider_string_end;
7635 break;
7636
7637 default:
7638 /* There are no other methods defined, so this should be a bug. */
7639 emacs_abort ();
7640 }
7641
7642 eassert (it->method != GET_FROM_STRING
7643 || (STRINGP (it->string)
7644 && IT_STRING_CHARPOS (*it) >= 0));
7645 }
7646
7647 /* Load IT's display element fields with information about the next
7648 display element which comes from a display table entry or from the
7649 result of translating a control character to one of the forms `^C'
7650 or `\003'.
7651
7652 IT->dpvec holds the glyphs to return as characters.
7653 IT->saved_face_id holds the face id before the display vector--it
7654 is restored into IT->face_id in set_iterator_to_next. */
7655
7656 static bool
7657 next_element_from_display_vector (struct it *it)
7658 {
7659 Lisp_Object gc;
7660 int prev_face_id = it->face_id;
7661 int next_face_id;
7662
7663 /* Precondition. */
7664 eassert (it->dpvec && it->current.dpvec_index >= 0);
7665
7666 it->face_id = it->saved_face_id;
7667
7668 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7669 That seemed totally bogus - so I changed it... */
7670 gc = it->dpvec[it->current.dpvec_index];
7671
7672 if (GLYPH_CODE_P (gc))
7673 {
7674 struct face *this_face, *prev_face, *next_face;
7675
7676 it->c = GLYPH_CODE_CHAR (gc);
7677 it->len = CHAR_BYTES (it->c);
7678
7679 /* The entry may contain a face id to use. Such a face id is
7680 the id of a Lisp face, not a realized face. A face id of
7681 zero means no face is specified. */
7682 if (it->dpvec_face_id >= 0)
7683 it->face_id = it->dpvec_face_id;
7684 else
7685 {
7686 int lface_id = GLYPH_CODE_FACE (gc);
7687 if (lface_id > 0)
7688 it->face_id = merge_faces (it->f, Qt, lface_id,
7689 it->saved_face_id);
7690 }
7691
7692 /* Glyphs in the display vector could have the box face, so we
7693 need to set the related flags in the iterator, as
7694 appropriate. */
7695 this_face = FACE_FROM_ID (it->f, it->face_id);
7696 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7697
7698 /* Is this character the first character of a box-face run? */
7699 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7700 && (!prev_face
7701 || prev_face->box == FACE_NO_BOX));
7702
7703 /* For the last character of the box-face run, we need to look
7704 either at the next glyph from the display vector, or at the
7705 face we saw before the display vector. */
7706 next_face_id = it->saved_face_id;
7707 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7708 {
7709 if (it->dpvec_face_id >= 0)
7710 next_face_id = it->dpvec_face_id;
7711 else
7712 {
7713 int lface_id =
7714 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7715
7716 if (lface_id > 0)
7717 next_face_id = merge_faces (it->f, Qt, lface_id,
7718 it->saved_face_id);
7719 }
7720 }
7721 next_face = FACE_FROM_ID (it->f, next_face_id);
7722 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7723 && (!next_face
7724 || next_face->box == FACE_NO_BOX));
7725 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7726 }
7727 else
7728 /* Display table entry is invalid. Return a space. */
7729 it->c = ' ', it->len = 1;
7730
7731 /* Don't change position and object of the iterator here. They are
7732 still the values of the character that had this display table
7733 entry or was translated, and that's what we want. */
7734 it->what = IT_CHARACTER;
7735 return true;
7736 }
7737
7738 /* Get the first element of string/buffer in the visual order, after
7739 being reseated to a new position in a string or a buffer. */
7740 static void
7741 get_visually_first_element (struct it *it)
7742 {
7743 bool string_p = STRINGP (it->string) || it->s;
7744 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7745 ptrdiff_t bob = (string_p ? 0 : BEGV);
7746
7747 if (STRINGP (it->string))
7748 {
7749 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7750 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7751 }
7752 else
7753 {
7754 it->bidi_it.charpos = IT_CHARPOS (*it);
7755 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7756 }
7757
7758 if (it->bidi_it.charpos == eob)
7759 {
7760 /* Nothing to do, but reset the FIRST_ELT flag, like
7761 bidi_paragraph_init does, because we are not going to
7762 call it. */
7763 it->bidi_it.first_elt = false;
7764 }
7765 else if (it->bidi_it.charpos == bob
7766 || (!string_p
7767 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7768 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7769 {
7770 /* If we are at the beginning of a line/string, we can produce
7771 the next element right away. */
7772 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7773 bidi_move_to_visually_next (&it->bidi_it);
7774 }
7775 else
7776 {
7777 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7778
7779 /* We need to prime the bidi iterator starting at the line's or
7780 string's beginning, before we will be able to produce the
7781 next element. */
7782 if (string_p)
7783 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7784 else
7785 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7786 IT_BYTEPOS (*it), -1,
7787 &it->bidi_it.bytepos);
7788 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7789 do
7790 {
7791 /* Now return to buffer/string position where we were asked
7792 to get the next display element, and produce that. */
7793 bidi_move_to_visually_next (&it->bidi_it);
7794 }
7795 while (it->bidi_it.bytepos != orig_bytepos
7796 && it->bidi_it.charpos < eob);
7797 }
7798
7799 /* Adjust IT's position information to where we ended up. */
7800 if (STRINGP (it->string))
7801 {
7802 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7803 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7804 }
7805 else
7806 {
7807 IT_CHARPOS (*it) = it->bidi_it.charpos;
7808 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7809 }
7810
7811 if (STRINGP (it->string) || !it->s)
7812 {
7813 ptrdiff_t stop, charpos, bytepos;
7814
7815 if (STRINGP (it->string))
7816 {
7817 eassert (!it->s);
7818 stop = SCHARS (it->string);
7819 if (stop > it->end_charpos)
7820 stop = it->end_charpos;
7821 charpos = IT_STRING_CHARPOS (*it);
7822 bytepos = IT_STRING_BYTEPOS (*it);
7823 }
7824 else
7825 {
7826 stop = it->end_charpos;
7827 charpos = IT_CHARPOS (*it);
7828 bytepos = IT_BYTEPOS (*it);
7829 }
7830 if (it->bidi_it.scan_dir < 0)
7831 stop = -1;
7832 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7833 it->string);
7834 }
7835 }
7836
7837 /* Load IT with the next display element from Lisp string IT->string.
7838 IT->current.string_pos is the current position within the string.
7839 If IT->current.overlay_string_index >= 0, the Lisp string is an
7840 overlay string. */
7841
7842 static bool
7843 next_element_from_string (struct it *it)
7844 {
7845 struct text_pos position;
7846
7847 eassert (STRINGP (it->string));
7848 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7849 eassert (IT_STRING_CHARPOS (*it) >= 0);
7850 position = it->current.string_pos;
7851
7852 /* With bidi reordering, the character to display might not be the
7853 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7854 that we were reseat()ed to a new string, whose paragraph
7855 direction is not known. */
7856 if (it->bidi_p && it->bidi_it.first_elt)
7857 {
7858 get_visually_first_element (it);
7859 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7860 }
7861
7862 /* Time to check for invisible text? */
7863 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7864 {
7865 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7866 {
7867 if (!(!it->bidi_p
7868 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7869 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7870 {
7871 /* With bidi non-linear iteration, we could find
7872 ourselves far beyond the last computed stop_charpos,
7873 with several other stop positions in between that we
7874 missed. Scan them all now, in buffer's logical
7875 order, until we find and handle the last stop_charpos
7876 that precedes our current position. */
7877 handle_stop_backwards (it, it->stop_charpos);
7878 return GET_NEXT_DISPLAY_ELEMENT (it);
7879 }
7880 else
7881 {
7882 if (it->bidi_p)
7883 {
7884 /* Take note of the stop position we just moved
7885 across, for when we will move back across it. */
7886 it->prev_stop = it->stop_charpos;
7887 /* If we are at base paragraph embedding level, take
7888 note of the last stop position seen at this
7889 level. */
7890 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7891 it->base_level_stop = it->stop_charpos;
7892 }
7893 handle_stop (it);
7894
7895 /* Since a handler may have changed IT->method, we must
7896 recurse here. */
7897 return GET_NEXT_DISPLAY_ELEMENT (it);
7898 }
7899 }
7900 else if (it->bidi_p
7901 /* If we are before prev_stop, we may have overstepped
7902 on our way backwards a stop_pos, and if so, we need
7903 to handle that stop_pos. */
7904 && IT_STRING_CHARPOS (*it) < it->prev_stop
7905 /* We can sometimes back up for reasons that have nothing
7906 to do with bidi reordering. E.g., compositions. The
7907 code below is only needed when we are above the base
7908 embedding level, so test for that explicitly. */
7909 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7910 {
7911 /* If we lost track of base_level_stop, we have no better
7912 place for handle_stop_backwards to start from than string
7913 beginning. This happens, e.g., when we were reseated to
7914 the previous screenful of text by vertical-motion. */
7915 if (it->base_level_stop <= 0
7916 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7917 it->base_level_stop = 0;
7918 handle_stop_backwards (it, it->base_level_stop);
7919 return GET_NEXT_DISPLAY_ELEMENT (it);
7920 }
7921 }
7922
7923 if (it->current.overlay_string_index >= 0)
7924 {
7925 /* Get the next character from an overlay string. In overlay
7926 strings, there is no field width or padding with spaces to
7927 do. */
7928 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7929 {
7930 it->what = IT_EOB;
7931 return false;
7932 }
7933 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7934 IT_STRING_BYTEPOS (*it),
7935 it->bidi_it.scan_dir < 0
7936 ? -1
7937 : SCHARS (it->string))
7938 && next_element_from_composition (it))
7939 {
7940 return true;
7941 }
7942 else if (STRING_MULTIBYTE (it->string))
7943 {
7944 const unsigned char *s = (SDATA (it->string)
7945 + IT_STRING_BYTEPOS (*it));
7946 it->c = string_char_and_length (s, &it->len);
7947 }
7948 else
7949 {
7950 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7951 it->len = 1;
7952 }
7953 }
7954 else
7955 {
7956 /* Get the next character from a Lisp string that is not an
7957 overlay string. Such strings come from the mode line, for
7958 example. We may have to pad with spaces, or truncate the
7959 string. See also next_element_from_c_string. */
7960 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7961 {
7962 it->what = IT_EOB;
7963 return false;
7964 }
7965 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7966 {
7967 /* Pad with spaces. */
7968 it->c = ' ', it->len = 1;
7969 CHARPOS (position) = BYTEPOS (position) = -1;
7970 }
7971 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7972 IT_STRING_BYTEPOS (*it),
7973 it->bidi_it.scan_dir < 0
7974 ? -1
7975 : it->string_nchars)
7976 && next_element_from_composition (it))
7977 {
7978 return true;
7979 }
7980 else if (STRING_MULTIBYTE (it->string))
7981 {
7982 const unsigned char *s = (SDATA (it->string)
7983 + IT_STRING_BYTEPOS (*it));
7984 it->c = string_char_and_length (s, &it->len);
7985 }
7986 else
7987 {
7988 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7989 it->len = 1;
7990 }
7991 }
7992
7993 /* Record what we have and where it came from. */
7994 it->what = IT_CHARACTER;
7995 it->object = it->string;
7996 it->position = position;
7997 return true;
7998 }
7999
8000
8001 /* Load IT with next display element from C string IT->s.
8002 IT->string_nchars is the maximum number of characters to return
8003 from the string. IT->end_charpos may be greater than
8004 IT->string_nchars when this function is called, in which case we
8005 may have to return padding spaces. Value is false if end of string
8006 reached, including padding spaces. */
8007
8008 static bool
8009 next_element_from_c_string (struct it *it)
8010 {
8011 bool success_p = true;
8012
8013 eassert (it->s);
8014 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8015 it->what = IT_CHARACTER;
8016 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8017 it->object = make_number (0);
8018
8019 /* With bidi reordering, the character to display might not be the
8020 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8021 we were reseated to a new string, whose paragraph direction is
8022 not known. */
8023 if (it->bidi_p && it->bidi_it.first_elt)
8024 get_visually_first_element (it);
8025
8026 /* IT's position can be greater than IT->string_nchars in case a
8027 field width or precision has been specified when the iterator was
8028 initialized. */
8029 if (IT_CHARPOS (*it) >= it->end_charpos)
8030 {
8031 /* End of the game. */
8032 it->what = IT_EOB;
8033 success_p = false;
8034 }
8035 else if (IT_CHARPOS (*it) >= it->string_nchars)
8036 {
8037 /* Pad with spaces. */
8038 it->c = ' ', it->len = 1;
8039 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8040 }
8041 else if (it->multibyte_p)
8042 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8043 else
8044 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8045
8046 return success_p;
8047 }
8048
8049
8050 /* Set up IT to return characters from an ellipsis, if appropriate.
8051 The definition of the ellipsis glyphs may come from a display table
8052 entry. This function fills IT with the first glyph from the
8053 ellipsis if an ellipsis is to be displayed. */
8054
8055 static bool
8056 next_element_from_ellipsis (struct it *it)
8057 {
8058 if (it->selective_display_ellipsis_p)
8059 setup_for_ellipsis (it, it->len);
8060 else
8061 {
8062 /* The face at the current position may be different from the
8063 face we find after the invisible text. Remember what it
8064 was in IT->saved_face_id, and signal that it's there by
8065 setting face_before_selective_p. */
8066 it->saved_face_id = it->face_id;
8067 it->method = GET_FROM_BUFFER;
8068 it->object = it->w->contents;
8069 reseat_at_next_visible_line_start (it, true);
8070 it->face_before_selective_p = true;
8071 }
8072
8073 return GET_NEXT_DISPLAY_ELEMENT (it);
8074 }
8075
8076
8077 /* Deliver an image display element. The iterator IT is already
8078 filled with image information (done in handle_display_prop). Value
8079 is always true. */
8080
8081
8082 static bool
8083 next_element_from_image (struct it *it)
8084 {
8085 it->what = IT_IMAGE;
8086 return true;
8087 }
8088
8089 static bool
8090 next_element_from_xwidget (struct it *it)
8091 {
8092 it->what = IT_XWIDGET;
8093 return true;
8094 }
8095
8096
8097 /* Fill iterator IT with next display element from a stretch glyph
8098 property. IT->object is the value of the text property. Value is
8099 always true. */
8100
8101 static bool
8102 next_element_from_stretch (struct it *it)
8103 {
8104 it->what = IT_STRETCH;
8105 return true;
8106 }
8107
8108 /* Scan backwards from IT's current position until we find a stop
8109 position, or until BEGV. This is called when we find ourself
8110 before both the last known prev_stop and base_level_stop while
8111 reordering bidirectional text. */
8112
8113 static void
8114 compute_stop_pos_backwards (struct it *it)
8115 {
8116 const int SCAN_BACK_LIMIT = 1000;
8117 struct text_pos pos;
8118 struct display_pos save_current = it->current;
8119 struct text_pos save_position = it->position;
8120 ptrdiff_t charpos = IT_CHARPOS (*it);
8121 ptrdiff_t where_we_are = charpos;
8122 ptrdiff_t save_stop_pos = it->stop_charpos;
8123 ptrdiff_t save_end_pos = it->end_charpos;
8124
8125 eassert (NILP (it->string) && !it->s);
8126 eassert (it->bidi_p);
8127 it->bidi_p = false;
8128 do
8129 {
8130 it->end_charpos = min (charpos + 1, ZV);
8131 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8132 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8133 reseat_1 (it, pos, false);
8134 compute_stop_pos (it);
8135 /* We must advance forward, right? */
8136 if (it->stop_charpos <= charpos)
8137 emacs_abort ();
8138 }
8139 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8140
8141 if (it->stop_charpos <= where_we_are)
8142 it->prev_stop = it->stop_charpos;
8143 else
8144 it->prev_stop = BEGV;
8145 it->bidi_p = true;
8146 it->current = save_current;
8147 it->position = save_position;
8148 it->stop_charpos = save_stop_pos;
8149 it->end_charpos = save_end_pos;
8150 }
8151
8152 /* Scan forward from CHARPOS in the current buffer/string, until we
8153 find a stop position > current IT's position. Then handle the stop
8154 position before that. This is called when we bump into a stop
8155 position while reordering bidirectional text. CHARPOS should be
8156 the last previously processed stop_pos (or BEGV/0, if none were
8157 processed yet) whose position is less that IT's current
8158 position. */
8159
8160 static void
8161 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8162 {
8163 bool bufp = !STRINGP (it->string);
8164 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8165 struct display_pos save_current = it->current;
8166 struct text_pos save_position = it->position;
8167 struct text_pos pos1;
8168 ptrdiff_t next_stop;
8169
8170 /* Scan in strict logical order. */
8171 eassert (it->bidi_p);
8172 it->bidi_p = false;
8173 do
8174 {
8175 it->prev_stop = charpos;
8176 if (bufp)
8177 {
8178 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8179 reseat_1 (it, pos1, false);
8180 }
8181 else
8182 it->current.string_pos = string_pos (charpos, it->string);
8183 compute_stop_pos (it);
8184 /* We must advance forward, right? */
8185 if (it->stop_charpos <= it->prev_stop)
8186 emacs_abort ();
8187 charpos = it->stop_charpos;
8188 }
8189 while (charpos <= where_we_are);
8190
8191 it->bidi_p = true;
8192 it->current = save_current;
8193 it->position = save_position;
8194 next_stop = it->stop_charpos;
8195 it->stop_charpos = it->prev_stop;
8196 handle_stop (it);
8197 it->stop_charpos = next_stop;
8198 }
8199
8200 /* Load IT with the next display element from current_buffer. Value
8201 is false if end of buffer reached. IT->stop_charpos is the next
8202 position at which to stop and check for text properties or buffer
8203 end. */
8204
8205 static bool
8206 next_element_from_buffer (struct it *it)
8207 {
8208 bool success_p = true;
8209
8210 eassert (IT_CHARPOS (*it) >= BEGV);
8211 eassert (NILP (it->string) && !it->s);
8212 eassert (!it->bidi_p
8213 || (EQ (it->bidi_it.string.lstring, Qnil)
8214 && it->bidi_it.string.s == NULL));
8215
8216 /* With bidi reordering, the character to display might not be the
8217 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8218 we were reseat()ed to a new buffer position, which is potentially
8219 a different paragraph. */
8220 if (it->bidi_p && it->bidi_it.first_elt)
8221 {
8222 get_visually_first_element (it);
8223 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8224 }
8225
8226 if (IT_CHARPOS (*it) >= it->stop_charpos)
8227 {
8228 if (IT_CHARPOS (*it) >= it->end_charpos)
8229 {
8230 bool overlay_strings_follow_p;
8231
8232 /* End of the game, except when overlay strings follow that
8233 haven't been returned yet. */
8234 if (it->overlay_strings_at_end_processed_p)
8235 overlay_strings_follow_p = false;
8236 else
8237 {
8238 it->overlay_strings_at_end_processed_p = true;
8239 overlay_strings_follow_p = get_overlay_strings (it, 0);
8240 }
8241
8242 if (overlay_strings_follow_p)
8243 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8244 else
8245 {
8246 it->what = IT_EOB;
8247 it->position = it->current.pos;
8248 success_p = false;
8249 }
8250 }
8251 else if (!(!it->bidi_p
8252 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8253 || IT_CHARPOS (*it) == it->stop_charpos))
8254 {
8255 /* With bidi non-linear iteration, we could find ourselves
8256 far beyond the last computed stop_charpos, with several
8257 other stop positions in between that we missed. Scan
8258 them all now, in buffer's logical order, until we find
8259 and handle the last stop_charpos that precedes our
8260 current position. */
8261 handle_stop_backwards (it, it->stop_charpos);
8262 it->ignore_overlay_strings_at_pos_p = false;
8263 return GET_NEXT_DISPLAY_ELEMENT (it);
8264 }
8265 else
8266 {
8267 if (it->bidi_p)
8268 {
8269 /* Take note of the stop position we just moved across,
8270 for when we will move back across it. */
8271 it->prev_stop = it->stop_charpos;
8272 /* If we are at base paragraph embedding level, take
8273 note of the last stop position seen at this
8274 level. */
8275 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8276 it->base_level_stop = it->stop_charpos;
8277 }
8278 handle_stop (it);
8279 it->ignore_overlay_strings_at_pos_p = false;
8280 return GET_NEXT_DISPLAY_ELEMENT (it);
8281 }
8282 }
8283 else if (it->bidi_p
8284 /* If we are before prev_stop, we may have overstepped on
8285 our way backwards a stop_pos, and if so, we need to
8286 handle that stop_pos. */
8287 && IT_CHARPOS (*it) < it->prev_stop
8288 /* We can sometimes back up for reasons that have nothing
8289 to do with bidi reordering. E.g., compositions. The
8290 code below is only needed when we are above the base
8291 embedding level, so test for that explicitly. */
8292 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8293 {
8294 if (it->base_level_stop <= 0
8295 || IT_CHARPOS (*it) < it->base_level_stop)
8296 {
8297 /* If we lost track of base_level_stop, we need to find
8298 prev_stop by looking backwards. This happens, e.g., when
8299 we were reseated to the previous screenful of text by
8300 vertical-motion. */
8301 it->base_level_stop = BEGV;
8302 compute_stop_pos_backwards (it);
8303 handle_stop_backwards (it, it->prev_stop);
8304 }
8305 else
8306 handle_stop_backwards (it, it->base_level_stop);
8307 it->ignore_overlay_strings_at_pos_p = false;
8308 return GET_NEXT_DISPLAY_ELEMENT (it);
8309 }
8310 else
8311 {
8312 /* No face changes, overlays etc. in sight, so just return a
8313 character from current_buffer. */
8314 unsigned char *p;
8315 ptrdiff_t stop;
8316
8317 /* We moved to the next buffer position, so any info about
8318 previously seen overlays is no longer valid. */
8319 it->ignore_overlay_strings_at_pos_p = false;
8320
8321 /* Maybe run the redisplay end trigger hook. Performance note:
8322 This doesn't seem to cost measurable time. */
8323 if (it->redisplay_end_trigger_charpos
8324 && it->glyph_row
8325 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8326 run_redisplay_end_trigger_hook (it);
8327
8328 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8329 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8330 stop)
8331 && next_element_from_composition (it))
8332 {
8333 return true;
8334 }
8335
8336 /* Get the next character, maybe multibyte. */
8337 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8338 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8339 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8340 else
8341 it->c = *p, it->len = 1;
8342
8343 /* Record what we have and where it came from. */
8344 it->what = IT_CHARACTER;
8345 it->object = it->w->contents;
8346 it->position = it->current.pos;
8347
8348 /* Normally we return the character found above, except when we
8349 really want to return an ellipsis for selective display. */
8350 if (it->selective)
8351 {
8352 if (it->c == '\n')
8353 {
8354 /* A value of selective > 0 means hide lines indented more
8355 than that number of columns. */
8356 if (it->selective > 0
8357 && IT_CHARPOS (*it) + 1 < ZV
8358 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8359 IT_BYTEPOS (*it) + 1,
8360 it->selective))
8361 {
8362 success_p = next_element_from_ellipsis (it);
8363 it->dpvec_char_len = -1;
8364 }
8365 }
8366 else if (it->c == '\r' && it->selective == -1)
8367 {
8368 /* A value of selective == -1 means that everything from the
8369 CR to the end of the line is invisible, with maybe an
8370 ellipsis displayed for it. */
8371 success_p = next_element_from_ellipsis (it);
8372 it->dpvec_char_len = -1;
8373 }
8374 }
8375 }
8376
8377 /* Value is false if end of buffer reached. */
8378 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8379 return success_p;
8380 }
8381
8382
8383 /* Run the redisplay end trigger hook for IT. */
8384
8385 static void
8386 run_redisplay_end_trigger_hook (struct it *it)
8387 {
8388 /* IT->glyph_row should be non-null, i.e. we should be actually
8389 displaying something, or otherwise we should not run the hook. */
8390 eassert (it->glyph_row);
8391
8392 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8393 it->redisplay_end_trigger_charpos = 0;
8394
8395 /* Since we are *trying* to run these functions, don't try to run
8396 them again, even if they get an error. */
8397 wset_redisplay_end_trigger (it->w, Qnil);
8398 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8399 make_number (charpos));
8400
8401 /* Notice if it changed the face of the character we are on. */
8402 handle_face_prop (it);
8403 }
8404
8405
8406 /* Deliver a composition display element. Unlike the other
8407 next_element_from_XXX, this function is not registered in the array
8408 get_next_element[]. It is called from next_element_from_buffer and
8409 next_element_from_string when necessary. */
8410
8411 static bool
8412 next_element_from_composition (struct it *it)
8413 {
8414 it->what = IT_COMPOSITION;
8415 it->len = it->cmp_it.nbytes;
8416 if (STRINGP (it->string))
8417 {
8418 if (it->c < 0)
8419 {
8420 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8421 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8422 return false;
8423 }
8424 it->position = it->current.string_pos;
8425 it->object = it->string;
8426 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8427 IT_STRING_BYTEPOS (*it), it->string);
8428 }
8429 else
8430 {
8431 if (it->c < 0)
8432 {
8433 IT_CHARPOS (*it) += it->cmp_it.nchars;
8434 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8435 if (it->bidi_p)
8436 {
8437 if (it->bidi_it.new_paragraph)
8438 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8439 false);
8440 /* Resync the bidi iterator with IT's new position.
8441 FIXME: this doesn't support bidirectional text. */
8442 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8443 bidi_move_to_visually_next (&it->bidi_it);
8444 }
8445 return false;
8446 }
8447 it->position = it->current.pos;
8448 it->object = it->w->contents;
8449 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8450 IT_BYTEPOS (*it), Qnil);
8451 }
8452 return true;
8453 }
8454
8455
8456 \f
8457 /***********************************************************************
8458 Moving an iterator without producing glyphs
8459 ***********************************************************************/
8460
8461 /* Check if iterator is at a position corresponding to a valid buffer
8462 position after some move_it_ call. */
8463
8464 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8465 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8466
8467
8468 /* Move iterator IT to a specified buffer or X position within one
8469 line on the display without producing glyphs.
8470
8471 OP should be a bit mask including some or all of these bits:
8472 MOVE_TO_X: Stop upon reaching x-position TO_X.
8473 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8474 Regardless of OP's value, stop upon reaching the end of the display line.
8475
8476 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8477 This means, in particular, that TO_X includes window's horizontal
8478 scroll amount.
8479
8480 The return value has several possible values that
8481 say what condition caused the scan to stop:
8482
8483 MOVE_POS_MATCH_OR_ZV
8484 - when TO_POS or ZV was reached.
8485
8486 MOVE_X_REACHED
8487 -when TO_X was reached before TO_POS or ZV were reached.
8488
8489 MOVE_LINE_CONTINUED
8490 - when we reached the end of the display area and the line must
8491 be continued.
8492
8493 MOVE_LINE_TRUNCATED
8494 - when we reached the end of the display area and the line is
8495 truncated.
8496
8497 MOVE_NEWLINE_OR_CR
8498 - when we stopped at a line end, i.e. a newline or a CR and selective
8499 display is on. */
8500
8501 static enum move_it_result
8502 move_it_in_display_line_to (struct it *it,
8503 ptrdiff_t to_charpos, int to_x,
8504 enum move_operation_enum op)
8505 {
8506 enum move_it_result result = MOVE_UNDEFINED;
8507 struct glyph_row *saved_glyph_row;
8508 struct it wrap_it, atpos_it, atx_it, ppos_it;
8509 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8510 void *ppos_data = NULL;
8511 bool may_wrap = false;
8512 enum it_method prev_method = it->method;
8513 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8514 bool saw_smaller_pos = prev_pos < to_charpos;
8515
8516 /* Don't produce glyphs in produce_glyphs. */
8517 saved_glyph_row = it->glyph_row;
8518 it->glyph_row = NULL;
8519
8520 /* Use wrap_it to save a copy of IT wherever a word wrap could
8521 occur. Use atpos_it to save a copy of IT at the desired buffer
8522 position, if found, so that we can scan ahead and check if the
8523 word later overshoots the window edge. Use atx_it similarly, for
8524 pixel positions. */
8525 wrap_it.sp = -1;
8526 atpos_it.sp = -1;
8527 atx_it.sp = -1;
8528
8529 /* Use ppos_it under bidi reordering to save a copy of IT for the
8530 initial position. We restore that position in IT when we have
8531 scanned the entire display line without finding a match for
8532 TO_CHARPOS and all the character positions are greater than
8533 TO_CHARPOS. We then restart the scan from the initial position,
8534 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8535 the closest to TO_CHARPOS. */
8536 if (it->bidi_p)
8537 {
8538 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8539 {
8540 SAVE_IT (ppos_it, *it, ppos_data);
8541 closest_pos = IT_CHARPOS (*it);
8542 }
8543 else
8544 closest_pos = ZV;
8545 }
8546
8547 #define BUFFER_POS_REACHED_P() \
8548 ((op & MOVE_TO_POS) != 0 \
8549 && BUFFERP (it->object) \
8550 && (IT_CHARPOS (*it) == to_charpos \
8551 || ((!it->bidi_p \
8552 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8553 && IT_CHARPOS (*it) > to_charpos) \
8554 || (it->what == IT_COMPOSITION \
8555 && ((IT_CHARPOS (*it) > to_charpos \
8556 && to_charpos >= it->cmp_it.charpos) \
8557 || (IT_CHARPOS (*it) < to_charpos \
8558 && to_charpos <= it->cmp_it.charpos)))) \
8559 && (it->method == GET_FROM_BUFFER \
8560 || (it->method == GET_FROM_DISPLAY_VECTOR \
8561 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8562
8563 /* If there's a line-/wrap-prefix, handle it. */
8564 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8565 && it->current_y < it->last_visible_y)
8566 handle_line_prefix (it);
8567
8568 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8569 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8570
8571 while (true)
8572 {
8573 int x, i, ascent = 0, descent = 0;
8574
8575 /* Utility macro to reset an iterator with x, ascent, and descent. */
8576 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8577 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8578 (IT)->max_descent = descent)
8579
8580 /* Stop if we move beyond TO_CHARPOS (after an image or a
8581 display string or stretch glyph). */
8582 if ((op & MOVE_TO_POS) != 0
8583 && BUFFERP (it->object)
8584 && it->method == GET_FROM_BUFFER
8585 && (((!it->bidi_p
8586 /* When the iterator is at base embedding level, we
8587 are guaranteed that characters are delivered for
8588 display in strictly increasing order of their
8589 buffer positions. */
8590 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8591 && IT_CHARPOS (*it) > to_charpos)
8592 || (it->bidi_p
8593 && (prev_method == GET_FROM_IMAGE
8594 || prev_method == GET_FROM_STRETCH
8595 || prev_method == GET_FROM_STRING)
8596 /* Passed TO_CHARPOS from left to right. */
8597 && ((prev_pos < to_charpos
8598 && IT_CHARPOS (*it) > to_charpos)
8599 /* Passed TO_CHARPOS from right to left. */
8600 || (prev_pos > to_charpos
8601 && IT_CHARPOS (*it) < to_charpos)))))
8602 {
8603 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8604 {
8605 result = MOVE_POS_MATCH_OR_ZV;
8606 break;
8607 }
8608 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8609 /* If wrap_it is valid, the current position might be in a
8610 word that is wrapped. So, save the iterator in
8611 atpos_it and continue to see if wrapping happens. */
8612 SAVE_IT (atpos_it, *it, atpos_data);
8613 }
8614
8615 /* Stop when ZV reached.
8616 We used to stop here when TO_CHARPOS reached as well, but that is
8617 too soon if this glyph does not fit on this line. So we handle it
8618 explicitly below. */
8619 if (!get_next_display_element (it))
8620 {
8621 result = MOVE_POS_MATCH_OR_ZV;
8622 break;
8623 }
8624
8625 if (it->line_wrap == TRUNCATE)
8626 {
8627 if (BUFFER_POS_REACHED_P ())
8628 {
8629 result = MOVE_POS_MATCH_OR_ZV;
8630 break;
8631 }
8632 }
8633 else
8634 {
8635 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8636 {
8637 if (IT_DISPLAYING_WHITESPACE (it))
8638 may_wrap = true;
8639 else if (may_wrap)
8640 {
8641 /* We have reached a glyph that follows one or more
8642 whitespace characters. If the position is
8643 already found, we are done. */
8644 if (atpos_it.sp >= 0)
8645 {
8646 RESTORE_IT (it, &atpos_it, atpos_data);
8647 result = MOVE_POS_MATCH_OR_ZV;
8648 goto done;
8649 }
8650 if (atx_it.sp >= 0)
8651 {
8652 RESTORE_IT (it, &atx_it, atx_data);
8653 result = MOVE_X_REACHED;
8654 goto done;
8655 }
8656 /* Otherwise, we can wrap here. */
8657 SAVE_IT (wrap_it, *it, wrap_data);
8658 may_wrap = false;
8659 }
8660 }
8661 }
8662
8663 /* Remember the line height for the current line, in case
8664 the next element doesn't fit on the line. */
8665 ascent = it->max_ascent;
8666 descent = it->max_descent;
8667
8668 /* The call to produce_glyphs will get the metrics of the
8669 display element IT is loaded with. Record the x-position
8670 before this display element, in case it doesn't fit on the
8671 line. */
8672 x = it->current_x;
8673
8674 PRODUCE_GLYPHS (it);
8675
8676 if (it->area != TEXT_AREA)
8677 {
8678 prev_method = it->method;
8679 if (it->method == GET_FROM_BUFFER)
8680 prev_pos = IT_CHARPOS (*it);
8681 set_iterator_to_next (it, true);
8682 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8683 SET_TEXT_POS (this_line_min_pos,
8684 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8685 if (it->bidi_p
8686 && (op & MOVE_TO_POS)
8687 && IT_CHARPOS (*it) > to_charpos
8688 && IT_CHARPOS (*it) < closest_pos)
8689 closest_pos = IT_CHARPOS (*it);
8690 continue;
8691 }
8692
8693 /* The number of glyphs we get back in IT->nglyphs will normally
8694 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8695 character on a terminal frame, or (iii) a line end. For the
8696 second case, IT->nglyphs - 1 padding glyphs will be present.
8697 (On X frames, there is only one glyph produced for a
8698 composite character.)
8699
8700 The behavior implemented below means, for continuation lines,
8701 that as many spaces of a TAB as fit on the current line are
8702 displayed there. For terminal frames, as many glyphs of a
8703 multi-glyph character are displayed in the current line, too.
8704 This is what the old redisplay code did, and we keep it that
8705 way. Under X, the whole shape of a complex character must
8706 fit on the line or it will be completely displayed in the
8707 next line.
8708
8709 Note that both for tabs and padding glyphs, all glyphs have
8710 the same width. */
8711 if (it->nglyphs)
8712 {
8713 /* More than one glyph or glyph doesn't fit on line. All
8714 glyphs have the same width. */
8715 int single_glyph_width = it->pixel_width / it->nglyphs;
8716 int new_x;
8717 int x_before_this_char = x;
8718 int hpos_before_this_char = it->hpos;
8719
8720 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8721 {
8722 new_x = x + single_glyph_width;
8723
8724 /* We want to leave anything reaching TO_X to the caller. */
8725 if ((op & MOVE_TO_X) && new_x > to_x)
8726 {
8727 if (BUFFER_POS_REACHED_P ())
8728 {
8729 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8730 goto buffer_pos_reached;
8731 if (atpos_it.sp < 0)
8732 {
8733 SAVE_IT (atpos_it, *it, atpos_data);
8734 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8735 }
8736 }
8737 else
8738 {
8739 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8740 {
8741 it->current_x = x;
8742 result = MOVE_X_REACHED;
8743 break;
8744 }
8745 if (atx_it.sp < 0)
8746 {
8747 SAVE_IT (atx_it, *it, atx_data);
8748 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8749 }
8750 }
8751 }
8752
8753 if (/* Lines are continued. */
8754 it->line_wrap != TRUNCATE
8755 && (/* And glyph doesn't fit on the line. */
8756 new_x > it->last_visible_x
8757 /* Or it fits exactly and we're on a window
8758 system frame. */
8759 || (new_x == it->last_visible_x
8760 && FRAME_WINDOW_P (it->f)
8761 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8762 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8763 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8764 {
8765 if (/* IT->hpos == 0 means the very first glyph
8766 doesn't fit on the line, e.g. a wide image. */
8767 it->hpos == 0
8768 || (new_x == it->last_visible_x
8769 && FRAME_WINDOW_P (it->f)))
8770 {
8771 ++it->hpos;
8772 it->current_x = new_x;
8773
8774 /* The character's last glyph just barely fits
8775 in this row. */
8776 if (i == it->nglyphs - 1)
8777 {
8778 /* If this is the destination position,
8779 return a position *before* it in this row,
8780 now that we know it fits in this row. */
8781 if (BUFFER_POS_REACHED_P ())
8782 {
8783 if (it->line_wrap != WORD_WRAP
8784 || wrap_it.sp < 0
8785 /* If we've just found whitespace to
8786 wrap, effectively ignore the
8787 previous wrap point -- it is no
8788 longer relevant, but we won't
8789 have an opportunity to update it,
8790 since we've reached the edge of
8791 this screen line. */
8792 || (may_wrap
8793 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8794 {
8795 it->hpos = hpos_before_this_char;
8796 it->current_x = x_before_this_char;
8797 result = MOVE_POS_MATCH_OR_ZV;
8798 break;
8799 }
8800 if (it->line_wrap == WORD_WRAP
8801 && atpos_it.sp < 0)
8802 {
8803 SAVE_IT (atpos_it, *it, atpos_data);
8804 atpos_it.current_x = x_before_this_char;
8805 atpos_it.hpos = hpos_before_this_char;
8806 }
8807 }
8808
8809 prev_method = it->method;
8810 if (it->method == GET_FROM_BUFFER)
8811 prev_pos = IT_CHARPOS (*it);
8812 set_iterator_to_next (it, true);
8813 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8814 SET_TEXT_POS (this_line_min_pos,
8815 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8816 /* On graphical terminals, newlines may
8817 "overflow" into the fringe if
8818 overflow-newline-into-fringe is non-nil.
8819 On text terminals, and on graphical
8820 terminals with no right margin, newlines
8821 may overflow into the last glyph on the
8822 display line.*/
8823 if (!FRAME_WINDOW_P (it->f)
8824 || ((it->bidi_p
8825 && it->bidi_it.paragraph_dir == R2L)
8826 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8827 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8828 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8829 {
8830 if (!get_next_display_element (it))
8831 {
8832 result = MOVE_POS_MATCH_OR_ZV;
8833 break;
8834 }
8835 if (BUFFER_POS_REACHED_P ())
8836 {
8837 if (ITERATOR_AT_END_OF_LINE_P (it))
8838 result = MOVE_POS_MATCH_OR_ZV;
8839 else
8840 result = MOVE_LINE_CONTINUED;
8841 break;
8842 }
8843 if (ITERATOR_AT_END_OF_LINE_P (it)
8844 && (it->line_wrap != WORD_WRAP
8845 || wrap_it.sp < 0
8846 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8847 {
8848 result = MOVE_NEWLINE_OR_CR;
8849 break;
8850 }
8851 }
8852 }
8853 }
8854 else
8855 IT_RESET_X_ASCENT_DESCENT (it);
8856
8857 /* If the screen line ends with whitespace, and we
8858 are under word-wrap, don't use wrap_it: it is no
8859 longer relevant, but we won't have an opportunity
8860 to update it, since we are done with this screen
8861 line. */
8862 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8863 {
8864 /* If we've found TO_X, go back there, as we now
8865 know the last word fits on this screen line. */
8866 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8867 && atx_it.sp >= 0)
8868 {
8869 RESTORE_IT (it, &atx_it, atx_data);
8870 atpos_it.sp = -1;
8871 atx_it.sp = -1;
8872 result = MOVE_X_REACHED;
8873 break;
8874 }
8875 }
8876 else if (wrap_it.sp >= 0)
8877 {
8878 RESTORE_IT (it, &wrap_it, wrap_data);
8879 atpos_it.sp = -1;
8880 atx_it.sp = -1;
8881 }
8882
8883 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8884 IT_CHARPOS (*it)));
8885 result = MOVE_LINE_CONTINUED;
8886 break;
8887 }
8888
8889 if (BUFFER_POS_REACHED_P ())
8890 {
8891 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8892 goto buffer_pos_reached;
8893 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8894 {
8895 SAVE_IT (atpos_it, *it, atpos_data);
8896 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8897 }
8898 }
8899
8900 if (new_x > it->first_visible_x)
8901 {
8902 /* Glyph is visible. Increment number of glyphs that
8903 would be displayed. */
8904 ++it->hpos;
8905 }
8906 }
8907
8908 if (result != MOVE_UNDEFINED)
8909 break;
8910 }
8911 else if (BUFFER_POS_REACHED_P ())
8912 {
8913 buffer_pos_reached:
8914 IT_RESET_X_ASCENT_DESCENT (it);
8915 result = MOVE_POS_MATCH_OR_ZV;
8916 break;
8917 }
8918 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8919 {
8920 /* Stop when TO_X specified and reached. This check is
8921 necessary here because of lines consisting of a line end,
8922 only. The line end will not produce any glyphs and we
8923 would never get MOVE_X_REACHED. */
8924 eassert (it->nglyphs == 0);
8925 result = MOVE_X_REACHED;
8926 break;
8927 }
8928
8929 /* Is this a line end? If yes, we're done. */
8930 if (ITERATOR_AT_END_OF_LINE_P (it))
8931 {
8932 /* If we are past TO_CHARPOS, but never saw any character
8933 positions smaller than TO_CHARPOS, return
8934 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8935 did. */
8936 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8937 {
8938 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8939 {
8940 if (closest_pos < ZV)
8941 {
8942 RESTORE_IT (it, &ppos_it, ppos_data);
8943 /* Don't recurse if closest_pos is equal to
8944 to_charpos, since we have just tried that. */
8945 if (closest_pos != to_charpos)
8946 move_it_in_display_line_to (it, closest_pos, -1,
8947 MOVE_TO_POS);
8948 result = MOVE_POS_MATCH_OR_ZV;
8949 }
8950 else
8951 goto buffer_pos_reached;
8952 }
8953 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8954 && IT_CHARPOS (*it) > to_charpos)
8955 goto buffer_pos_reached;
8956 else
8957 result = MOVE_NEWLINE_OR_CR;
8958 }
8959 else
8960 result = MOVE_NEWLINE_OR_CR;
8961 break;
8962 }
8963
8964 prev_method = it->method;
8965 if (it->method == GET_FROM_BUFFER)
8966 prev_pos = IT_CHARPOS (*it);
8967 /* The current display element has been consumed. Advance
8968 to the next. */
8969 set_iterator_to_next (it, true);
8970 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8971 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8972 if (IT_CHARPOS (*it) < to_charpos)
8973 saw_smaller_pos = true;
8974 if (it->bidi_p
8975 && (op & MOVE_TO_POS)
8976 && IT_CHARPOS (*it) >= to_charpos
8977 && IT_CHARPOS (*it) < closest_pos)
8978 closest_pos = IT_CHARPOS (*it);
8979
8980 /* Stop if lines are truncated and IT's current x-position is
8981 past the right edge of the window now. */
8982 if (it->line_wrap == TRUNCATE
8983 && it->current_x >= it->last_visible_x)
8984 {
8985 if (!FRAME_WINDOW_P (it->f)
8986 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8987 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8988 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8989 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8990 {
8991 bool at_eob_p = false;
8992
8993 if ((at_eob_p = !get_next_display_element (it))
8994 || BUFFER_POS_REACHED_P ()
8995 /* If we are past TO_CHARPOS, but never saw any
8996 character positions smaller than TO_CHARPOS,
8997 return MOVE_POS_MATCH_OR_ZV, like the
8998 unidirectional display did. */
8999 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9000 && !saw_smaller_pos
9001 && IT_CHARPOS (*it) > to_charpos))
9002 {
9003 if (it->bidi_p
9004 && !BUFFER_POS_REACHED_P ()
9005 && !at_eob_p && closest_pos < ZV)
9006 {
9007 RESTORE_IT (it, &ppos_it, ppos_data);
9008 if (closest_pos != to_charpos)
9009 move_it_in_display_line_to (it, closest_pos, -1,
9010 MOVE_TO_POS);
9011 }
9012 result = MOVE_POS_MATCH_OR_ZV;
9013 break;
9014 }
9015 if (ITERATOR_AT_END_OF_LINE_P (it))
9016 {
9017 result = MOVE_NEWLINE_OR_CR;
9018 break;
9019 }
9020 }
9021 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9022 && !saw_smaller_pos
9023 && IT_CHARPOS (*it) > to_charpos)
9024 {
9025 if (closest_pos < ZV)
9026 {
9027 RESTORE_IT (it, &ppos_it, ppos_data);
9028 if (closest_pos != to_charpos)
9029 move_it_in_display_line_to (it, closest_pos, -1,
9030 MOVE_TO_POS);
9031 }
9032 result = MOVE_POS_MATCH_OR_ZV;
9033 break;
9034 }
9035 result = MOVE_LINE_TRUNCATED;
9036 break;
9037 }
9038 #undef IT_RESET_X_ASCENT_DESCENT
9039 }
9040
9041 #undef BUFFER_POS_REACHED_P
9042
9043 /* If we scanned beyond to_pos and didn't find a point to wrap at,
9044 restore the saved iterator. */
9045 if (atpos_it.sp >= 0)
9046 RESTORE_IT (it, &atpos_it, atpos_data);
9047 else if (atx_it.sp >= 0)
9048 RESTORE_IT (it, &atx_it, atx_data);
9049
9050 done:
9051
9052 if (atpos_data)
9053 bidi_unshelve_cache (atpos_data, true);
9054 if (atx_data)
9055 bidi_unshelve_cache (atx_data, true);
9056 if (wrap_data)
9057 bidi_unshelve_cache (wrap_data, true);
9058 if (ppos_data)
9059 bidi_unshelve_cache (ppos_data, true);
9060
9061 /* Restore the iterator settings altered at the beginning of this
9062 function. */
9063 it->glyph_row = saved_glyph_row;
9064 return result;
9065 }
9066
9067 /* For external use. */
9068 void
9069 move_it_in_display_line (struct it *it,
9070 ptrdiff_t to_charpos, int to_x,
9071 enum move_operation_enum op)
9072 {
9073 if (it->line_wrap == WORD_WRAP
9074 && (op & MOVE_TO_X))
9075 {
9076 struct it save_it;
9077 void *save_data = NULL;
9078 int skip;
9079
9080 SAVE_IT (save_it, *it, save_data);
9081 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9082 /* When word-wrap is on, TO_X may lie past the end
9083 of a wrapped line. Then it->current is the
9084 character on the next line, so backtrack to the
9085 space before the wrap point. */
9086 if (skip == MOVE_LINE_CONTINUED)
9087 {
9088 int prev_x = max (it->current_x - 1, 0);
9089 RESTORE_IT (it, &save_it, save_data);
9090 move_it_in_display_line_to
9091 (it, -1, prev_x, MOVE_TO_X);
9092 }
9093 else
9094 bidi_unshelve_cache (save_data, true);
9095 }
9096 else
9097 move_it_in_display_line_to (it, to_charpos, to_x, op);
9098 }
9099
9100
9101 /* Move IT forward until it satisfies one or more of the criteria in
9102 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9103
9104 OP is a bit-mask that specifies where to stop, and in particular,
9105 which of those four position arguments makes a difference. See the
9106 description of enum move_operation_enum.
9107
9108 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9109 screen line, this function will set IT to the next position that is
9110 displayed to the right of TO_CHARPOS on the screen.
9111
9112 Return the maximum pixel length of any line scanned but never more
9113 than it.last_visible_x. */
9114
9115 int
9116 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9117 {
9118 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9119 int line_height, line_start_x = 0, reached = 0;
9120 int max_current_x = 0;
9121 void *backup_data = NULL;
9122
9123 for (;;)
9124 {
9125 if (op & MOVE_TO_VPOS)
9126 {
9127 /* If no TO_CHARPOS and no TO_X specified, stop at the
9128 start of the line TO_VPOS. */
9129 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9130 {
9131 if (it->vpos == to_vpos)
9132 {
9133 reached = 1;
9134 break;
9135 }
9136 else
9137 skip = move_it_in_display_line_to (it, -1, -1, 0);
9138 }
9139 else
9140 {
9141 /* TO_VPOS >= 0 means stop at TO_X in the line at
9142 TO_VPOS, or at TO_POS, whichever comes first. */
9143 if (it->vpos == to_vpos)
9144 {
9145 reached = 2;
9146 break;
9147 }
9148
9149 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9150
9151 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9152 {
9153 reached = 3;
9154 break;
9155 }
9156 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9157 {
9158 /* We have reached TO_X but not in the line we want. */
9159 skip = move_it_in_display_line_to (it, to_charpos,
9160 -1, MOVE_TO_POS);
9161 if (skip == MOVE_POS_MATCH_OR_ZV)
9162 {
9163 reached = 4;
9164 break;
9165 }
9166 }
9167 }
9168 }
9169 else if (op & MOVE_TO_Y)
9170 {
9171 struct it it_backup;
9172
9173 if (it->line_wrap == WORD_WRAP)
9174 SAVE_IT (it_backup, *it, backup_data);
9175
9176 /* TO_Y specified means stop at TO_X in the line containing
9177 TO_Y---or at TO_CHARPOS if this is reached first. The
9178 problem is that we can't really tell whether the line
9179 contains TO_Y before we have completely scanned it, and
9180 this may skip past TO_X. What we do is to first scan to
9181 TO_X.
9182
9183 If TO_X is not specified, use a TO_X of zero. The reason
9184 is to make the outcome of this function more predictable.
9185 If we didn't use TO_X == 0, we would stop at the end of
9186 the line which is probably not what a caller would expect
9187 to happen. */
9188 skip = move_it_in_display_line_to
9189 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9190 (MOVE_TO_X | (op & MOVE_TO_POS)));
9191
9192 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9193 if (skip == MOVE_POS_MATCH_OR_ZV)
9194 reached = 5;
9195 else if (skip == MOVE_X_REACHED)
9196 {
9197 /* If TO_X was reached, we want to know whether TO_Y is
9198 in the line. We know this is the case if the already
9199 scanned glyphs make the line tall enough. Otherwise,
9200 we must check by scanning the rest of the line. */
9201 line_height = it->max_ascent + it->max_descent;
9202 if (to_y >= it->current_y
9203 && to_y < it->current_y + line_height)
9204 {
9205 reached = 6;
9206 break;
9207 }
9208 SAVE_IT (it_backup, *it, backup_data);
9209 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9210 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9211 op & MOVE_TO_POS);
9212 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9213 line_height = it->max_ascent + it->max_descent;
9214 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9215
9216 if (to_y >= it->current_y
9217 && to_y < it->current_y + line_height)
9218 {
9219 /* If TO_Y is in this line and TO_X was reached
9220 above, we scanned too far. We have to restore
9221 IT's settings to the ones before skipping. But
9222 keep the more accurate values of max_ascent and
9223 max_descent we've found while skipping the rest
9224 of the line, for the sake of callers, such as
9225 pos_visible_p, that need to know the line
9226 height. */
9227 int max_ascent = it->max_ascent;
9228 int max_descent = it->max_descent;
9229
9230 RESTORE_IT (it, &it_backup, backup_data);
9231 it->max_ascent = max_ascent;
9232 it->max_descent = max_descent;
9233 reached = 6;
9234 }
9235 else
9236 {
9237 skip = skip2;
9238 if (skip == MOVE_POS_MATCH_OR_ZV)
9239 reached = 7;
9240 }
9241 }
9242 else
9243 {
9244 /* Check whether TO_Y is in this line. */
9245 line_height = it->max_ascent + it->max_descent;
9246 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9247
9248 if (to_y >= it->current_y
9249 && to_y < it->current_y + line_height)
9250 {
9251 if (to_y > it->current_y)
9252 max_current_x = max (it->current_x, max_current_x);
9253
9254 /* When word-wrap is on, TO_X may lie past the end
9255 of a wrapped line. Then it->current is the
9256 character on the next line, so backtrack to the
9257 space before the wrap point. */
9258 if (skip == MOVE_LINE_CONTINUED
9259 && it->line_wrap == WORD_WRAP)
9260 {
9261 int prev_x = max (it->current_x - 1, 0);
9262 RESTORE_IT (it, &it_backup, backup_data);
9263 skip = move_it_in_display_line_to
9264 (it, -1, prev_x, MOVE_TO_X);
9265 }
9266
9267 reached = 6;
9268 }
9269 }
9270
9271 if (reached)
9272 {
9273 max_current_x = max (it->current_x, max_current_x);
9274 break;
9275 }
9276 }
9277 else if (BUFFERP (it->object)
9278 && (it->method == GET_FROM_BUFFER
9279 || it->method == GET_FROM_STRETCH)
9280 && IT_CHARPOS (*it) >= to_charpos
9281 /* Under bidi iteration, a call to set_iterator_to_next
9282 can scan far beyond to_charpos if the initial
9283 portion of the next line needs to be reordered. In
9284 that case, give move_it_in_display_line_to another
9285 chance below. */
9286 && !(it->bidi_p
9287 && it->bidi_it.scan_dir == -1))
9288 skip = MOVE_POS_MATCH_OR_ZV;
9289 else
9290 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9291
9292 switch (skip)
9293 {
9294 case MOVE_POS_MATCH_OR_ZV:
9295 max_current_x = max (it->current_x, max_current_x);
9296 reached = 8;
9297 goto out;
9298
9299 case MOVE_NEWLINE_OR_CR:
9300 max_current_x = max (it->current_x, max_current_x);
9301 set_iterator_to_next (it, true);
9302 it->continuation_lines_width = 0;
9303 break;
9304
9305 case MOVE_LINE_TRUNCATED:
9306 max_current_x = it->last_visible_x;
9307 it->continuation_lines_width = 0;
9308 reseat_at_next_visible_line_start (it, false);
9309 if ((op & MOVE_TO_POS) != 0
9310 && IT_CHARPOS (*it) > to_charpos)
9311 {
9312 reached = 9;
9313 goto out;
9314 }
9315 break;
9316
9317 case MOVE_LINE_CONTINUED:
9318 max_current_x = it->last_visible_x;
9319 /* For continued lines ending in a tab, some of the glyphs
9320 associated with the tab are displayed on the current
9321 line. Since it->current_x does not include these glyphs,
9322 we use it->last_visible_x instead. */
9323 if (it->c == '\t')
9324 {
9325 it->continuation_lines_width += it->last_visible_x;
9326 /* When moving by vpos, ensure that the iterator really
9327 advances to the next line (bug#847, bug#969). Fixme:
9328 do we need to do this in other circumstances? */
9329 if (it->current_x != it->last_visible_x
9330 && (op & MOVE_TO_VPOS)
9331 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9332 {
9333 line_start_x = it->current_x + it->pixel_width
9334 - it->last_visible_x;
9335 if (FRAME_WINDOW_P (it->f))
9336 {
9337 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9338 struct font *face_font = face->font;
9339
9340 /* When display_line produces a continued line
9341 that ends in a TAB, it skips a tab stop that
9342 is closer than the font's space character
9343 width (see x_produce_glyphs where it produces
9344 the stretch glyph which represents a TAB).
9345 We need to reproduce the same logic here. */
9346 eassert (face_font);
9347 if (face_font)
9348 {
9349 if (line_start_x < face_font->space_width)
9350 line_start_x
9351 += it->tab_width * face_font->space_width;
9352 }
9353 }
9354 set_iterator_to_next (it, false);
9355 }
9356 }
9357 else
9358 it->continuation_lines_width += it->current_x;
9359 break;
9360
9361 default:
9362 emacs_abort ();
9363 }
9364
9365 /* Reset/increment for the next run. */
9366 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9367 it->current_x = line_start_x;
9368 line_start_x = 0;
9369 it->hpos = 0;
9370 it->current_y += it->max_ascent + it->max_descent;
9371 ++it->vpos;
9372 last_height = it->max_ascent + it->max_descent;
9373 it->max_ascent = it->max_descent = 0;
9374 }
9375
9376 out:
9377
9378 /* On text terminals, we may stop at the end of a line in the middle
9379 of a multi-character glyph. If the glyph itself is continued,
9380 i.e. it is actually displayed on the next line, don't treat this
9381 stopping point as valid; move to the next line instead (unless
9382 that brings us offscreen). */
9383 if (!FRAME_WINDOW_P (it->f)
9384 && op & MOVE_TO_POS
9385 && IT_CHARPOS (*it) == to_charpos
9386 && it->what == IT_CHARACTER
9387 && it->nglyphs > 1
9388 && it->line_wrap == WINDOW_WRAP
9389 && it->current_x == it->last_visible_x - 1
9390 && it->c != '\n'
9391 && it->c != '\t'
9392 && it->w->window_end_valid
9393 && it->vpos < it->w->window_end_vpos)
9394 {
9395 it->continuation_lines_width += it->current_x;
9396 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9397 it->current_y += it->max_ascent + it->max_descent;
9398 ++it->vpos;
9399 last_height = it->max_ascent + it->max_descent;
9400 }
9401
9402 if (backup_data)
9403 bidi_unshelve_cache (backup_data, true);
9404
9405 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9406
9407 return max_current_x;
9408 }
9409
9410
9411 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9412
9413 If DY > 0, move IT backward at least that many pixels. DY = 0
9414 means move IT backward to the preceding line start or BEGV. This
9415 function may move over more than DY pixels if IT->current_y - DY
9416 ends up in the middle of a line; in this case IT->current_y will be
9417 set to the top of the line moved to. */
9418
9419 void
9420 move_it_vertically_backward (struct it *it, int dy)
9421 {
9422 int nlines, h;
9423 struct it it2, it3;
9424 void *it2data = NULL, *it3data = NULL;
9425 ptrdiff_t start_pos;
9426 int nchars_per_row
9427 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9428 ptrdiff_t pos_limit;
9429
9430 move_further_back:
9431 eassert (dy >= 0);
9432
9433 start_pos = IT_CHARPOS (*it);
9434
9435 /* Estimate how many newlines we must move back. */
9436 nlines = max (1, dy / default_line_pixel_height (it->w));
9437 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9438 pos_limit = BEGV;
9439 else
9440 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9441
9442 /* Set the iterator's position that many lines back. But don't go
9443 back more than NLINES full screen lines -- this wins a day with
9444 buffers which have very long lines. */
9445 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9446 back_to_previous_visible_line_start (it);
9447
9448 /* Reseat the iterator here. When moving backward, we don't want
9449 reseat to skip forward over invisible text, set up the iterator
9450 to deliver from overlay strings at the new position etc. So,
9451 use reseat_1 here. */
9452 reseat_1 (it, it->current.pos, true);
9453
9454 /* We are now surely at a line start. */
9455 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9456 reordering is in effect. */
9457 it->continuation_lines_width = 0;
9458
9459 /* Move forward and see what y-distance we moved. First move to the
9460 start of the next line so that we get its height. We need this
9461 height to be able to tell whether we reached the specified
9462 y-distance. */
9463 SAVE_IT (it2, *it, it2data);
9464 it2.max_ascent = it2.max_descent = 0;
9465 do
9466 {
9467 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9468 MOVE_TO_POS | MOVE_TO_VPOS);
9469 }
9470 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9471 /* If we are in a display string which starts at START_POS,
9472 and that display string includes a newline, and we are
9473 right after that newline (i.e. at the beginning of a
9474 display line), exit the loop, because otherwise we will
9475 infloop, since move_it_to will see that it is already at
9476 START_POS and will not move. */
9477 || (it2.method == GET_FROM_STRING
9478 && IT_CHARPOS (it2) == start_pos
9479 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9480 eassert (IT_CHARPOS (*it) >= BEGV);
9481 SAVE_IT (it3, it2, it3data);
9482
9483 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9484 eassert (IT_CHARPOS (*it) >= BEGV);
9485 /* H is the actual vertical distance from the position in *IT
9486 and the starting position. */
9487 h = it2.current_y - it->current_y;
9488 /* NLINES is the distance in number of lines. */
9489 nlines = it2.vpos - it->vpos;
9490
9491 /* Correct IT's y and vpos position
9492 so that they are relative to the starting point. */
9493 it->vpos -= nlines;
9494 it->current_y -= h;
9495
9496 if (dy == 0)
9497 {
9498 /* DY == 0 means move to the start of the screen line. The
9499 value of nlines is > 0 if continuation lines were involved,
9500 or if the original IT position was at start of a line. */
9501 RESTORE_IT (it, it, it2data);
9502 if (nlines > 0)
9503 move_it_by_lines (it, nlines);
9504 /* The above code moves us to some position NLINES down,
9505 usually to its first glyph (leftmost in an L2R line), but
9506 that's not necessarily the start of the line, under bidi
9507 reordering. We want to get to the character position
9508 that is immediately after the newline of the previous
9509 line. */
9510 if (it->bidi_p
9511 && !it->continuation_lines_width
9512 && !STRINGP (it->string)
9513 && IT_CHARPOS (*it) > BEGV
9514 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9515 {
9516 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9517
9518 DEC_BOTH (cp, bp);
9519 cp = find_newline_no_quit (cp, bp, -1, NULL);
9520 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9521 }
9522 bidi_unshelve_cache (it3data, true);
9523 }
9524 else
9525 {
9526 /* The y-position we try to reach, relative to *IT.
9527 Note that H has been subtracted in front of the if-statement. */
9528 int target_y = it->current_y + h - dy;
9529 int y0 = it3.current_y;
9530 int y1;
9531 int line_height;
9532
9533 RESTORE_IT (&it3, &it3, it3data);
9534 y1 = line_bottom_y (&it3);
9535 line_height = y1 - y0;
9536 RESTORE_IT (it, it, it2data);
9537 /* If we did not reach target_y, try to move further backward if
9538 we can. If we moved too far backward, try to move forward. */
9539 if (target_y < it->current_y
9540 /* This is heuristic. In a window that's 3 lines high, with
9541 a line height of 13 pixels each, recentering with point
9542 on the bottom line will try to move -39/2 = 19 pixels
9543 backward. Try to avoid moving into the first line. */
9544 && (it->current_y - target_y
9545 > min (window_box_height (it->w), line_height * 2 / 3))
9546 && IT_CHARPOS (*it) > BEGV)
9547 {
9548 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9549 target_y - it->current_y));
9550 dy = it->current_y - target_y;
9551 goto move_further_back;
9552 }
9553 else if (target_y >= it->current_y + line_height
9554 && IT_CHARPOS (*it) < ZV)
9555 {
9556 /* Should move forward by at least one line, maybe more.
9557
9558 Note: Calling move_it_by_lines can be expensive on
9559 terminal frames, where compute_motion is used (via
9560 vmotion) to do the job, when there are very long lines
9561 and truncate-lines is nil. That's the reason for
9562 treating terminal frames specially here. */
9563
9564 if (!FRAME_WINDOW_P (it->f))
9565 move_it_vertically (it, target_y - it->current_y);
9566 else
9567 {
9568 do
9569 {
9570 move_it_by_lines (it, 1);
9571 }
9572 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9573 }
9574 }
9575 }
9576 }
9577
9578
9579 /* Move IT by a specified amount of pixel lines DY. DY negative means
9580 move backwards. DY = 0 means move to start of screen line. At the
9581 end, IT will be on the start of a screen line. */
9582
9583 void
9584 move_it_vertically (struct it *it, int dy)
9585 {
9586 if (dy <= 0)
9587 move_it_vertically_backward (it, -dy);
9588 else
9589 {
9590 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9591 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9592 MOVE_TO_POS | MOVE_TO_Y);
9593 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9594
9595 /* If buffer ends in ZV without a newline, move to the start of
9596 the line to satisfy the post-condition. */
9597 if (IT_CHARPOS (*it) == ZV
9598 && ZV > BEGV
9599 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9600 move_it_by_lines (it, 0);
9601 }
9602 }
9603
9604
9605 /* Move iterator IT past the end of the text line it is in. */
9606
9607 void
9608 move_it_past_eol (struct it *it)
9609 {
9610 enum move_it_result rc;
9611
9612 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9613 if (rc == MOVE_NEWLINE_OR_CR)
9614 set_iterator_to_next (it, false);
9615 }
9616
9617
9618 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9619 negative means move up. DVPOS == 0 means move to the start of the
9620 screen line.
9621
9622 Optimization idea: If we would know that IT->f doesn't use
9623 a face with proportional font, we could be faster for
9624 truncate-lines nil. */
9625
9626 void
9627 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9628 {
9629
9630 /* The commented-out optimization uses vmotion on terminals. This
9631 gives bad results, because elements like it->what, on which
9632 callers such as pos_visible_p rely, aren't updated. */
9633 /* struct position pos;
9634 if (!FRAME_WINDOW_P (it->f))
9635 {
9636 struct text_pos textpos;
9637
9638 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9639 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9640 reseat (it, textpos, true);
9641 it->vpos += pos.vpos;
9642 it->current_y += pos.vpos;
9643 }
9644 else */
9645
9646 if (dvpos == 0)
9647 {
9648 /* DVPOS == 0 means move to the start of the screen line. */
9649 move_it_vertically_backward (it, 0);
9650 /* Let next call to line_bottom_y calculate real line height. */
9651 last_height = 0;
9652 }
9653 else if (dvpos > 0)
9654 {
9655 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9656 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9657 {
9658 /* Only move to the next buffer position if we ended up in a
9659 string from display property, not in an overlay string
9660 (before-string or after-string). That is because the
9661 latter don't conceal the underlying buffer position, so
9662 we can ask to move the iterator to the exact position we
9663 are interested in. Note that, even if we are already at
9664 IT_CHARPOS (*it), the call below is not a no-op, as it
9665 will detect that we are at the end of the string, pop the
9666 iterator, and compute it->current_x and it->hpos
9667 correctly. */
9668 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9669 -1, -1, -1, MOVE_TO_POS);
9670 }
9671 }
9672 else
9673 {
9674 struct it it2;
9675 void *it2data = NULL;
9676 ptrdiff_t start_charpos, i;
9677 int nchars_per_row
9678 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9679 bool hit_pos_limit = false;
9680 ptrdiff_t pos_limit;
9681
9682 /* Start at the beginning of the screen line containing IT's
9683 position. This may actually move vertically backwards,
9684 in case of overlays, so adjust dvpos accordingly. */
9685 dvpos += it->vpos;
9686 move_it_vertically_backward (it, 0);
9687 dvpos -= it->vpos;
9688
9689 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9690 screen lines, and reseat the iterator there. */
9691 start_charpos = IT_CHARPOS (*it);
9692 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9693 pos_limit = BEGV;
9694 else
9695 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9696
9697 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9698 back_to_previous_visible_line_start (it);
9699 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9700 hit_pos_limit = true;
9701 reseat (it, it->current.pos, true);
9702
9703 /* Move further back if we end up in a string or an image. */
9704 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9705 {
9706 /* First try to move to start of display line. */
9707 dvpos += it->vpos;
9708 move_it_vertically_backward (it, 0);
9709 dvpos -= it->vpos;
9710 if (IT_POS_VALID_AFTER_MOVE_P (it))
9711 break;
9712 /* If start of line is still in string or image,
9713 move further back. */
9714 back_to_previous_visible_line_start (it);
9715 reseat (it, it->current.pos, true);
9716 dvpos--;
9717 }
9718
9719 it->current_x = it->hpos = 0;
9720
9721 /* Above call may have moved too far if continuation lines
9722 are involved. Scan forward and see if it did. */
9723 SAVE_IT (it2, *it, it2data);
9724 it2.vpos = it2.current_y = 0;
9725 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9726 it->vpos -= it2.vpos;
9727 it->current_y -= it2.current_y;
9728 it->current_x = it->hpos = 0;
9729
9730 /* If we moved too far back, move IT some lines forward. */
9731 if (it2.vpos > -dvpos)
9732 {
9733 int delta = it2.vpos + dvpos;
9734
9735 RESTORE_IT (&it2, &it2, it2data);
9736 SAVE_IT (it2, *it, it2data);
9737 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9738 /* Move back again if we got too far ahead. */
9739 if (IT_CHARPOS (*it) >= start_charpos)
9740 RESTORE_IT (it, &it2, it2data);
9741 else
9742 bidi_unshelve_cache (it2data, true);
9743 }
9744 else if (hit_pos_limit && pos_limit > BEGV
9745 && dvpos < 0 && it2.vpos < -dvpos)
9746 {
9747 /* If we hit the limit, but still didn't make it far enough
9748 back, that means there's a display string with a newline
9749 covering a large chunk of text, and that caused
9750 back_to_previous_visible_line_start try to go too far.
9751 Punish those who commit such atrocities by going back
9752 until we've reached DVPOS, after lifting the limit, which
9753 could make it slow for very long lines. "If it hurts,
9754 don't do that!" */
9755 dvpos += it2.vpos;
9756 RESTORE_IT (it, it, it2data);
9757 for (i = -dvpos; i > 0; --i)
9758 {
9759 back_to_previous_visible_line_start (it);
9760 it->vpos--;
9761 }
9762 reseat_1 (it, it->current.pos, true);
9763 }
9764 else
9765 RESTORE_IT (it, it, it2data);
9766 }
9767 }
9768
9769 /* Return true if IT points into the middle of a display vector. */
9770
9771 bool
9772 in_display_vector_p (struct it *it)
9773 {
9774 return (it->method == GET_FROM_DISPLAY_VECTOR
9775 && it->current.dpvec_index > 0
9776 && it->dpvec + it->current.dpvec_index != it->dpend);
9777 }
9778
9779 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9780 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9781 WINDOW must be a live window and defaults to the selected one. The
9782 return value is a cons of the maximum pixel-width of any text line and
9783 the maximum pixel-height of all text lines.
9784
9785 The optional argument FROM, if non-nil, specifies the first text
9786 position and defaults to the minimum accessible position of the buffer.
9787 If FROM is t, use the minimum accessible position that is not a newline
9788 character. TO, if non-nil, specifies the last text position and
9789 defaults to the maximum accessible position of the buffer. If TO is t,
9790 use the maximum accessible position that is not a newline character.
9791
9792 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9793 width that can be returned. X-LIMIT nil or omitted, means to use the
9794 pixel-width of WINDOW's body; use this if you do not intend to change
9795 the width of WINDOW. Use the maximum width WINDOW may assume if you
9796 intend to change WINDOW's width. In any case, text whose x-coordinate
9797 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9798 can take some time, it's always a good idea to make this argument as
9799 small as possible; in particular, if the buffer contains long lines that
9800 shall be truncated anyway.
9801
9802 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9803 height that can be returned. Text lines whose y-coordinate is beyond
9804 Y-LIMIT are ignored. Since calculating the text height of a large
9805 buffer can take some time, it makes sense to specify this argument if
9806 the size of the buffer is unknown.
9807
9808 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9809 include the height of the mode- or header-line of WINDOW in the return
9810 value. If it is either the symbol `mode-line' or `header-line', include
9811 only the height of that line, if present, in the return value. If t,
9812 include the height of both, if present, in the return value. */)
9813 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9814 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9815 {
9816 struct window *w = decode_live_window (window);
9817 Lisp_Object buffer = w->contents;
9818 struct buffer *b;
9819 struct it it;
9820 struct buffer *old_b = NULL;
9821 ptrdiff_t start, end, pos;
9822 struct text_pos startp;
9823 void *itdata = NULL;
9824 int c, max_y = -1, x = 0, y = 0;
9825
9826 CHECK_BUFFER (buffer);
9827 b = XBUFFER (buffer);
9828
9829 if (b != current_buffer)
9830 {
9831 old_b = current_buffer;
9832 set_buffer_internal (b);
9833 }
9834
9835 if (NILP (from))
9836 start = BEGV;
9837 else if (EQ (from, Qt))
9838 {
9839 start = pos = BEGV;
9840 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9841 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9842 start = pos;
9843 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9844 start = pos;
9845 }
9846 else
9847 {
9848 CHECK_NUMBER_COERCE_MARKER (from);
9849 start = min (max (XINT (from), BEGV), ZV);
9850 }
9851
9852 if (NILP (to))
9853 end = ZV;
9854 else if (EQ (to, Qt))
9855 {
9856 end = pos = ZV;
9857 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9858 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9859 end = pos;
9860 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9861 end = pos;
9862 }
9863 else
9864 {
9865 CHECK_NUMBER_COERCE_MARKER (to);
9866 end = max (start, min (XINT (to), ZV));
9867 }
9868
9869 if (!NILP (y_limit))
9870 {
9871 CHECK_NUMBER (y_limit);
9872 max_y = min (XINT (y_limit), INT_MAX);
9873 }
9874
9875 itdata = bidi_shelve_cache ();
9876 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9877 start_display (&it, w, startp);
9878
9879 if (NILP (x_limit))
9880 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9881 else
9882 {
9883 CHECK_NUMBER (x_limit);
9884 it.last_visible_x = min (XINT (x_limit), INFINITY);
9885 /* Actually, we never want move_it_to stop at to_x. But to make
9886 sure that move_it_in_display_line_to always moves far enough,
9887 we set it to INT_MAX and specify MOVE_TO_X. */
9888 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9889 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9890 }
9891
9892 y = it.current_y + it.max_ascent + it.max_descent;
9893
9894 if (!EQ (mode_and_header_line, Qheader_line)
9895 && !EQ (mode_and_header_line, Qt))
9896 /* Do not count the header-line which was counted automatically by
9897 start_display. */
9898 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9899
9900 if (EQ (mode_and_header_line, Qmode_line)
9901 || EQ (mode_and_header_line, Qt))
9902 /* Do count the mode-line which is not included automatically by
9903 start_display. */
9904 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9905
9906 bidi_unshelve_cache (itdata, false);
9907
9908 if (old_b)
9909 set_buffer_internal (old_b);
9910
9911 return Fcons (make_number (x), make_number (y));
9912 }
9913 \f
9914 /***********************************************************************
9915 Messages
9916 ***********************************************************************/
9917
9918 /* Return the number of arguments the format string FORMAT needs. */
9919
9920 static ptrdiff_t
9921 format_nargs (char const *format)
9922 {
9923 ptrdiff_t nargs = 0;
9924 for (char const *p = format; (p = strchr (p, '%')); p++)
9925 if (p[1] == '%')
9926 p++;
9927 else
9928 nargs++;
9929 return nargs;
9930 }
9931
9932 /* Add a message with format string FORMAT and formatted arguments
9933 to *Messages*. */
9934
9935 void
9936 add_to_log (const char *format, ...)
9937 {
9938 va_list ap;
9939 va_start (ap, format);
9940 vadd_to_log (format, ap);
9941 va_end (ap);
9942 }
9943
9944 void
9945 vadd_to_log (char const *format, va_list ap)
9946 {
9947 ptrdiff_t form_nargs = format_nargs (format);
9948 ptrdiff_t nargs = 1 + form_nargs;
9949 Lisp_Object args[10];
9950 eassert (nargs <= ARRAYELTS (args));
9951 AUTO_STRING (args0, format);
9952 args[0] = args0;
9953 for (ptrdiff_t i = 1; i <= nargs; i++)
9954 args[i] = va_arg (ap, Lisp_Object);
9955 Lisp_Object msg = Qnil;
9956 msg = Fformat_message (nargs, args);
9957
9958 ptrdiff_t len = SBYTES (msg) + 1;
9959 USE_SAFE_ALLOCA;
9960 char *buffer = SAFE_ALLOCA (len);
9961 memcpy (buffer, SDATA (msg), len);
9962
9963 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
9964 SAFE_FREE ();
9965 }
9966
9967
9968 /* Output a newline in the *Messages* buffer if "needs" one. */
9969
9970 void
9971 message_log_maybe_newline (void)
9972 {
9973 if (message_log_need_newline)
9974 message_dolog ("", 0, true, false);
9975 }
9976
9977
9978 /* Add a string M of length NBYTES to the message log, optionally
9979 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9980 true, means interpret the contents of M as multibyte. This
9981 function calls low-level routines in order to bypass text property
9982 hooks, etc. which might not be safe to run.
9983
9984 This may GC (insert may run before/after change hooks),
9985 so the buffer M must NOT point to a Lisp string. */
9986
9987 void
9988 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9989 {
9990 const unsigned char *msg = (const unsigned char *) m;
9991
9992 if (!NILP (Vmemory_full))
9993 return;
9994
9995 if (!NILP (Vmessage_log_max))
9996 {
9997 struct buffer *oldbuf;
9998 Lisp_Object oldpoint, oldbegv, oldzv;
9999 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10000 ptrdiff_t point_at_end = 0;
10001 ptrdiff_t zv_at_end = 0;
10002 Lisp_Object old_deactivate_mark;
10003
10004 old_deactivate_mark = Vdeactivate_mark;
10005 oldbuf = current_buffer;
10006
10007 /* Ensure the Messages buffer exists, and switch to it.
10008 If we created it, set the major-mode. */
10009 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10010 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10011 if (newbuffer
10012 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10013 call0 (intern ("messages-buffer-mode"));
10014
10015 bset_undo_list (current_buffer, Qt);
10016 bset_cache_long_scans (current_buffer, Qnil);
10017
10018 oldpoint = message_dolog_marker1;
10019 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10020 oldbegv = message_dolog_marker2;
10021 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10022 oldzv = message_dolog_marker3;
10023 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10024
10025 if (PT == Z)
10026 point_at_end = 1;
10027 if (ZV == Z)
10028 zv_at_end = 1;
10029
10030 BEGV = BEG;
10031 BEGV_BYTE = BEG_BYTE;
10032 ZV = Z;
10033 ZV_BYTE = Z_BYTE;
10034 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10035
10036 /* Insert the string--maybe converting multibyte to single byte
10037 or vice versa, so that all the text fits the buffer. */
10038 if (multibyte
10039 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10040 {
10041 ptrdiff_t i;
10042 int c, char_bytes;
10043 char work[1];
10044
10045 /* Convert a multibyte string to single-byte
10046 for the *Message* buffer. */
10047 for (i = 0; i < nbytes; i += char_bytes)
10048 {
10049 c = string_char_and_length (msg + i, &char_bytes);
10050 work[0] = CHAR_TO_BYTE8 (c);
10051 insert_1_both (work, 1, 1, true, false, false);
10052 }
10053 }
10054 else if (! multibyte
10055 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10056 {
10057 ptrdiff_t i;
10058 int c, char_bytes;
10059 unsigned char str[MAX_MULTIBYTE_LENGTH];
10060 /* Convert a single-byte string to multibyte
10061 for the *Message* buffer. */
10062 for (i = 0; i < nbytes; i++)
10063 {
10064 c = msg[i];
10065 MAKE_CHAR_MULTIBYTE (c);
10066 char_bytes = CHAR_STRING (c, str);
10067 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10068 }
10069 }
10070 else if (nbytes)
10071 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10072 true, false, false);
10073
10074 if (nlflag)
10075 {
10076 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10077 printmax_t dups;
10078
10079 insert_1_both ("\n", 1, 1, true, false, false);
10080
10081 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10082 this_bol = PT;
10083 this_bol_byte = PT_BYTE;
10084
10085 /* See if this line duplicates the previous one.
10086 If so, combine duplicates. */
10087 if (this_bol > BEG)
10088 {
10089 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10090 prev_bol = PT;
10091 prev_bol_byte = PT_BYTE;
10092
10093 dups = message_log_check_duplicate (prev_bol_byte,
10094 this_bol_byte);
10095 if (dups)
10096 {
10097 del_range_both (prev_bol, prev_bol_byte,
10098 this_bol, this_bol_byte, false);
10099 if (dups > 1)
10100 {
10101 char dupstr[sizeof " [ times]"
10102 + INT_STRLEN_BOUND (printmax_t)];
10103
10104 /* If you change this format, don't forget to also
10105 change message_log_check_duplicate. */
10106 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10107 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10108 insert_1_both (dupstr, duplen, duplen,
10109 true, false, true);
10110 }
10111 }
10112 }
10113
10114 /* If we have more than the desired maximum number of lines
10115 in the *Messages* buffer now, delete the oldest ones.
10116 This is safe because we don't have undo in this buffer. */
10117
10118 if (NATNUMP (Vmessage_log_max))
10119 {
10120 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10121 -XFASTINT (Vmessage_log_max) - 1, false);
10122 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10123 }
10124 }
10125 BEGV = marker_position (oldbegv);
10126 BEGV_BYTE = marker_byte_position (oldbegv);
10127
10128 if (zv_at_end)
10129 {
10130 ZV = Z;
10131 ZV_BYTE = Z_BYTE;
10132 }
10133 else
10134 {
10135 ZV = marker_position (oldzv);
10136 ZV_BYTE = marker_byte_position (oldzv);
10137 }
10138
10139 if (point_at_end)
10140 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10141 else
10142 /* We can't do Fgoto_char (oldpoint) because it will run some
10143 Lisp code. */
10144 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10145 marker_byte_position (oldpoint));
10146
10147 unchain_marker (XMARKER (oldpoint));
10148 unchain_marker (XMARKER (oldbegv));
10149 unchain_marker (XMARKER (oldzv));
10150
10151 /* We called insert_1_both above with its 5th argument (PREPARE)
10152 false, which prevents insert_1_both from calling
10153 prepare_to_modify_buffer, which in turns prevents us from
10154 incrementing windows_or_buffers_changed even if *Messages* is
10155 shown in some window. So we must manually set
10156 windows_or_buffers_changed here to make up for that. */
10157 windows_or_buffers_changed = old_windows_or_buffers_changed;
10158 bset_redisplay (current_buffer);
10159
10160 set_buffer_internal (oldbuf);
10161
10162 message_log_need_newline = !nlflag;
10163 Vdeactivate_mark = old_deactivate_mark;
10164 }
10165 }
10166
10167
10168 /* We are at the end of the buffer after just having inserted a newline.
10169 (Note: We depend on the fact we won't be crossing the gap.)
10170 Check to see if the most recent message looks a lot like the previous one.
10171 Return 0 if different, 1 if the new one should just replace it, or a
10172 value N > 1 if we should also append " [N times]". */
10173
10174 static intmax_t
10175 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10176 {
10177 ptrdiff_t i;
10178 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10179 bool seen_dots = false;
10180 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10181 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10182
10183 for (i = 0; i < len; i++)
10184 {
10185 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10186 seen_dots = true;
10187 if (p1[i] != p2[i])
10188 return seen_dots;
10189 }
10190 p1 += len;
10191 if (*p1 == '\n')
10192 return 2;
10193 if (*p1++ == ' ' && *p1++ == '[')
10194 {
10195 char *pend;
10196 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10197 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10198 return n + 1;
10199 }
10200 return 0;
10201 }
10202 \f
10203
10204 /* Display an echo area message M with a specified length of NBYTES
10205 bytes. The string may include null characters. If M is not a
10206 string, clear out any existing message, and let the mini-buffer
10207 text show through.
10208
10209 This function cancels echoing. */
10210
10211 void
10212 message3 (Lisp_Object m)
10213 {
10214 clear_message (true, true);
10215 cancel_echoing ();
10216
10217 /* First flush out any partial line written with print. */
10218 message_log_maybe_newline ();
10219 if (STRINGP (m))
10220 {
10221 ptrdiff_t nbytes = SBYTES (m);
10222 bool multibyte = STRING_MULTIBYTE (m);
10223 char *buffer;
10224 USE_SAFE_ALLOCA;
10225 SAFE_ALLOCA_STRING (buffer, m);
10226 message_dolog (buffer, nbytes, true, multibyte);
10227 SAFE_FREE ();
10228 }
10229 if (! inhibit_message)
10230 message3_nolog (m);
10231 }
10232
10233 /* Log the message M to stderr. Log an empty line if M is not a string. */
10234
10235 static void
10236 message_to_stderr (Lisp_Object m)
10237 {
10238 if (noninteractive_need_newline)
10239 {
10240 noninteractive_need_newline = false;
10241 fputc ('\n', stderr);
10242 }
10243 if (STRINGP (m))
10244 {
10245 Lisp_Object coding_system = Vlocale_coding_system;
10246 Lisp_Object s;
10247
10248 if (!NILP (Vcoding_system_for_write))
10249 coding_system = Vcoding_system_for_write;
10250 if (!NILP (coding_system))
10251 s = code_convert_string_norecord (m, coding_system, true);
10252 else
10253 s = m;
10254
10255 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10256 }
10257 if (!cursor_in_echo_area)
10258 fputc ('\n', stderr);
10259 fflush (stderr);
10260 }
10261
10262 /* The non-logging version of message3.
10263 This does not cancel echoing, because it is used for echoing.
10264 Perhaps we need to make a separate function for echoing
10265 and make this cancel echoing. */
10266
10267 void
10268 message3_nolog (Lisp_Object m)
10269 {
10270 struct frame *sf = SELECTED_FRAME ();
10271
10272 if (FRAME_INITIAL_P (sf))
10273 message_to_stderr (m);
10274 /* Error messages get reported properly by cmd_error, so this must be just an
10275 informative message; if the frame hasn't really been initialized yet, just
10276 toss it. */
10277 else if (INTERACTIVE && sf->glyphs_initialized_p)
10278 {
10279 /* Get the frame containing the mini-buffer
10280 that the selected frame is using. */
10281 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10282 Lisp_Object frame = XWINDOW (mini_window)->frame;
10283 struct frame *f = XFRAME (frame);
10284
10285 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10286 Fmake_frame_visible (frame);
10287
10288 if (STRINGP (m) && SCHARS (m) > 0)
10289 {
10290 set_message (m);
10291 if (minibuffer_auto_raise)
10292 Fraise_frame (frame);
10293 /* Assume we are not echoing.
10294 (If we are, echo_now will override this.) */
10295 echo_message_buffer = Qnil;
10296 }
10297 else
10298 clear_message (true, true);
10299
10300 do_pending_window_change (false);
10301 echo_area_display (true);
10302 do_pending_window_change (false);
10303 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10304 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10305 }
10306 }
10307
10308
10309 /* Display a null-terminated echo area message M. If M is 0, clear
10310 out any existing message, and let the mini-buffer text show through.
10311
10312 The buffer M must continue to exist until after the echo area gets
10313 cleared or some other message gets displayed there. Do not pass
10314 text that is stored in a Lisp string. Do not pass text in a buffer
10315 that was alloca'd. */
10316
10317 void
10318 message1 (const char *m)
10319 {
10320 message3 (m ? build_unibyte_string (m) : Qnil);
10321 }
10322
10323
10324 /* The non-logging counterpart of message1. */
10325
10326 void
10327 message1_nolog (const char *m)
10328 {
10329 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10330 }
10331
10332 /* Display a message M which contains a single %s
10333 which gets replaced with STRING. */
10334
10335 void
10336 message_with_string (const char *m, Lisp_Object string, bool log)
10337 {
10338 CHECK_STRING (string);
10339
10340 bool need_message;
10341 if (noninteractive)
10342 need_message = !!m;
10343 else if (!INTERACTIVE)
10344 need_message = false;
10345 else
10346 {
10347 /* The frame whose minibuffer we're going to display the message on.
10348 It may be larger than the selected frame, so we need
10349 to use its buffer, not the selected frame's buffer. */
10350 Lisp_Object mini_window;
10351 struct frame *f, *sf = SELECTED_FRAME ();
10352
10353 /* Get the frame containing the minibuffer
10354 that the selected frame is using. */
10355 mini_window = FRAME_MINIBUF_WINDOW (sf);
10356 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10357
10358 /* Error messages get reported properly by cmd_error, so this must be
10359 just an informative message; if the frame hasn't really been
10360 initialized yet, just toss it. */
10361 need_message = f->glyphs_initialized_p;
10362 }
10363
10364 if (need_message)
10365 {
10366 AUTO_STRING (fmt, m);
10367 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10368
10369 if (noninteractive)
10370 message_to_stderr (msg);
10371 else
10372 {
10373 if (log)
10374 message3 (msg);
10375 else
10376 message3_nolog (msg);
10377
10378 /* Print should start at the beginning of the message
10379 buffer next time. */
10380 message_buf_print = false;
10381 }
10382 }
10383 }
10384
10385
10386 /* Dump an informative message to the minibuf. If M is 0, clear out
10387 any existing message, and let the mini-buffer text show through.
10388
10389 The message must be safe ASCII and the format must not contain ` or
10390 '. If your message and format do not fit into this category,
10391 convert your arguments to Lisp objects and use Fmessage instead. */
10392
10393 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10394 vmessage (const char *m, va_list ap)
10395 {
10396 if (noninteractive)
10397 {
10398 if (m)
10399 {
10400 if (noninteractive_need_newline)
10401 putc ('\n', stderr);
10402 noninteractive_need_newline = false;
10403 vfprintf (stderr, m, ap);
10404 if (!cursor_in_echo_area)
10405 fprintf (stderr, "\n");
10406 fflush (stderr);
10407 }
10408 }
10409 else if (INTERACTIVE)
10410 {
10411 /* The frame whose mini-buffer we're going to display the message
10412 on. It may be larger than the selected frame, so we need to
10413 use its buffer, not the selected frame's buffer. */
10414 Lisp_Object mini_window;
10415 struct frame *f, *sf = SELECTED_FRAME ();
10416
10417 /* Get the frame containing the mini-buffer
10418 that the selected frame is using. */
10419 mini_window = FRAME_MINIBUF_WINDOW (sf);
10420 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10421
10422 /* Error messages get reported properly by cmd_error, so this must be
10423 just an informative message; if the frame hasn't really been
10424 initialized yet, just toss it. */
10425 if (f->glyphs_initialized_p)
10426 {
10427 if (m)
10428 {
10429 ptrdiff_t len;
10430 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10431 USE_SAFE_ALLOCA;
10432 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10433
10434 len = doprnt (message_buf, maxsize, m, 0, ap);
10435
10436 message3 (make_string (message_buf, len));
10437 SAFE_FREE ();
10438 }
10439 else
10440 message1 (0);
10441
10442 /* Print should start at the beginning of the message
10443 buffer next time. */
10444 message_buf_print = false;
10445 }
10446 }
10447 }
10448
10449 void
10450 message (const char *m, ...)
10451 {
10452 va_list ap;
10453 va_start (ap, m);
10454 vmessage (m, ap);
10455 va_end (ap);
10456 }
10457
10458
10459 /* Display the current message in the current mini-buffer. This is
10460 only called from error handlers in process.c, and is not time
10461 critical. */
10462
10463 void
10464 update_echo_area (void)
10465 {
10466 if (!NILP (echo_area_buffer[0]))
10467 {
10468 Lisp_Object string;
10469 string = Fcurrent_message ();
10470 message3 (string);
10471 }
10472 }
10473
10474
10475 /* Make sure echo area buffers in `echo_buffers' are live.
10476 If they aren't, make new ones. */
10477
10478 static void
10479 ensure_echo_area_buffers (void)
10480 {
10481 int i;
10482
10483 for (i = 0; i < 2; ++i)
10484 if (!BUFFERP (echo_buffer[i])
10485 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10486 {
10487 char name[30];
10488 Lisp_Object old_buffer;
10489 int j;
10490
10491 old_buffer = echo_buffer[i];
10492 echo_buffer[i] = Fget_buffer_create
10493 (make_formatted_string (name, " *Echo Area %d*", i));
10494 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10495 /* to force word wrap in echo area -
10496 it was decided to postpone this*/
10497 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10498
10499 for (j = 0; j < 2; ++j)
10500 if (EQ (old_buffer, echo_area_buffer[j]))
10501 echo_area_buffer[j] = echo_buffer[i];
10502 }
10503 }
10504
10505
10506 /* Call FN with args A1..A2 with either the current or last displayed
10507 echo_area_buffer as current buffer.
10508
10509 WHICH zero means use the current message buffer
10510 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10511 from echo_buffer[] and clear it.
10512
10513 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10514 suitable buffer from echo_buffer[] and clear it.
10515
10516 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10517 that the current message becomes the last displayed one, make
10518 choose a suitable buffer for echo_area_buffer[0], and clear it.
10519
10520 Value is what FN returns. */
10521
10522 static bool
10523 with_echo_area_buffer (struct window *w, int which,
10524 bool (*fn) (ptrdiff_t, Lisp_Object),
10525 ptrdiff_t a1, Lisp_Object a2)
10526 {
10527 Lisp_Object buffer;
10528 bool this_one, the_other, clear_buffer_p, rc;
10529 ptrdiff_t count = SPECPDL_INDEX ();
10530
10531 /* If buffers aren't live, make new ones. */
10532 ensure_echo_area_buffers ();
10533
10534 clear_buffer_p = false;
10535
10536 if (which == 0)
10537 this_one = false, the_other = true;
10538 else if (which > 0)
10539 this_one = true, the_other = false;
10540 else
10541 {
10542 this_one = false, the_other = true;
10543 clear_buffer_p = true;
10544
10545 /* We need a fresh one in case the current echo buffer equals
10546 the one containing the last displayed echo area message. */
10547 if (!NILP (echo_area_buffer[this_one])
10548 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10549 echo_area_buffer[this_one] = Qnil;
10550 }
10551
10552 /* Choose a suitable buffer from echo_buffer[] is we don't
10553 have one. */
10554 if (NILP (echo_area_buffer[this_one]))
10555 {
10556 echo_area_buffer[this_one]
10557 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10558 ? echo_buffer[the_other]
10559 : echo_buffer[this_one]);
10560 clear_buffer_p = true;
10561 }
10562
10563 buffer = echo_area_buffer[this_one];
10564
10565 /* Don't get confused by reusing the buffer used for echoing
10566 for a different purpose. */
10567 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10568 cancel_echoing ();
10569
10570 record_unwind_protect (unwind_with_echo_area_buffer,
10571 with_echo_area_buffer_unwind_data (w));
10572
10573 /* Make the echo area buffer current. Note that for display
10574 purposes, it is not necessary that the displayed window's buffer
10575 == current_buffer, except for text property lookup. So, let's
10576 only set that buffer temporarily here without doing a full
10577 Fset_window_buffer. We must also change w->pointm, though,
10578 because otherwise an assertions in unshow_buffer fails, and Emacs
10579 aborts. */
10580 set_buffer_internal_1 (XBUFFER (buffer));
10581 if (w)
10582 {
10583 wset_buffer (w, buffer);
10584 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10585 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10586 }
10587
10588 bset_undo_list (current_buffer, Qt);
10589 bset_read_only (current_buffer, Qnil);
10590 specbind (Qinhibit_read_only, Qt);
10591 specbind (Qinhibit_modification_hooks, Qt);
10592
10593 if (clear_buffer_p && Z > BEG)
10594 del_range (BEG, Z);
10595
10596 eassert (BEGV >= BEG);
10597 eassert (ZV <= Z && ZV >= BEGV);
10598
10599 rc = fn (a1, a2);
10600
10601 eassert (BEGV >= BEG);
10602 eassert (ZV <= Z && ZV >= BEGV);
10603
10604 unbind_to (count, Qnil);
10605 return rc;
10606 }
10607
10608
10609 /* Save state that should be preserved around the call to the function
10610 FN called in with_echo_area_buffer. */
10611
10612 static Lisp_Object
10613 with_echo_area_buffer_unwind_data (struct window *w)
10614 {
10615 int i = 0;
10616 Lisp_Object vector, tmp;
10617
10618 /* Reduce consing by keeping one vector in
10619 Vwith_echo_area_save_vector. */
10620 vector = Vwith_echo_area_save_vector;
10621 Vwith_echo_area_save_vector = Qnil;
10622
10623 if (NILP (vector))
10624 vector = Fmake_vector (make_number (11), Qnil);
10625
10626 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10627 ASET (vector, i, Vdeactivate_mark); ++i;
10628 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10629
10630 if (w)
10631 {
10632 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10633 ASET (vector, i, w->contents); ++i;
10634 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10635 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10636 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10637 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10638 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10639 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10640 }
10641 else
10642 {
10643 int end = i + 8;
10644 for (; i < end; ++i)
10645 ASET (vector, i, Qnil);
10646 }
10647
10648 eassert (i == ASIZE (vector));
10649 return vector;
10650 }
10651
10652
10653 /* Restore global state from VECTOR which was created by
10654 with_echo_area_buffer_unwind_data. */
10655
10656 static void
10657 unwind_with_echo_area_buffer (Lisp_Object vector)
10658 {
10659 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10660 Vdeactivate_mark = AREF (vector, 1);
10661 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10662
10663 if (WINDOWP (AREF (vector, 3)))
10664 {
10665 struct window *w;
10666 Lisp_Object buffer;
10667
10668 w = XWINDOW (AREF (vector, 3));
10669 buffer = AREF (vector, 4);
10670
10671 wset_buffer (w, buffer);
10672 set_marker_both (w->pointm, buffer,
10673 XFASTINT (AREF (vector, 5)),
10674 XFASTINT (AREF (vector, 6)));
10675 set_marker_both (w->old_pointm, buffer,
10676 XFASTINT (AREF (vector, 7)),
10677 XFASTINT (AREF (vector, 8)));
10678 set_marker_both (w->start, buffer,
10679 XFASTINT (AREF (vector, 9)),
10680 XFASTINT (AREF (vector, 10)));
10681 }
10682
10683 Vwith_echo_area_save_vector = vector;
10684 }
10685
10686
10687 /* Set up the echo area for use by print functions. MULTIBYTE_P
10688 means we will print multibyte. */
10689
10690 void
10691 setup_echo_area_for_printing (bool multibyte_p)
10692 {
10693 /* If we can't find an echo area any more, exit. */
10694 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10695 Fkill_emacs (Qnil);
10696
10697 ensure_echo_area_buffers ();
10698
10699 if (!message_buf_print)
10700 {
10701 /* A message has been output since the last time we printed.
10702 Choose a fresh echo area buffer. */
10703 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10704 echo_area_buffer[0] = echo_buffer[1];
10705 else
10706 echo_area_buffer[0] = echo_buffer[0];
10707
10708 /* Switch to that buffer and clear it. */
10709 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10710 bset_truncate_lines (current_buffer, Qnil);
10711
10712 if (Z > BEG)
10713 {
10714 ptrdiff_t count = SPECPDL_INDEX ();
10715 specbind (Qinhibit_read_only, Qt);
10716 /* Note that undo recording is always disabled. */
10717 del_range (BEG, Z);
10718 unbind_to (count, Qnil);
10719 }
10720 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10721
10722 /* Set up the buffer for the multibyteness we need. */
10723 if (multibyte_p
10724 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10725 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10726
10727 /* Raise the frame containing the echo area. */
10728 if (minibuffer_auto_raise)
10729 {
10730 struct frame *sf = SELECTED_FRAME ();
10731 Lisp_Object mini_window;
10732 mini_window = FRAME_MINIBUF_WINDOW (sf);
10733 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10734 }
10735
10736 message_log_maybe_newline ();
10737 message_buf_print = true;
10738 }
10739 else
10740 {
10741 if (NILP (echo_area_buffer[0]))
10742 {
10743 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10744 echo_area_buffer[0] = echo_buffer[1];
10745 else
10746 echo_area_buffer[0] = echo_buffer[0];
10747 }
10748
10749 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10750 {
10751 /* Someone switched buffers between print requests. */
10752 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10753 bset_truncate_lines (current_buffer, Qnil);
10754 }
10755 }
10756 }
10757
10758
10759 /* Display an echo area message in window W. Value is true if W's
10760 height is changed. If display_last_displayed_message_p,
10761 display the message that was last displayed, otherwise
10762 display the current message. */
10763
10764 static bool
10765 display_echo_area (struct window *w)
10766 {
10767 bool no_message_p, window_height_changed_p;
10768
10769 /* Temporarily disable garbage collections while displaying the echo
10770 area. This is done because a GC can print a message itself.
10771 That message would modify the echo area buffer's contents while a
10772 redisplay of the buffer is going on, and seriously confuse
10773 redisplay. */
10774 ptrdiff_t count = inhibit_garbage_collection ();
10775
10776 /* If there is no message, we must call display_echo_area_1
10777 nevertheless because it resizes the window. But we will have to
10778 reset the echo_area_buffer in question to nil at the end because
10779 with_echo_area_buffer will sets it to an empty buffer. */
10780 bool i = display_last_displayed_message_p;
10781 /* According to the C99, C11 and C++11 standards, the integral value
10782 of a "bool" is always 0 or 1, so this array access is safe here,
10783 if oddly typed. */
10784 no_message_p = NILP (echo_area_buffer[i]);
10785
10786 window_height_changed_p
10787 = with_echo_area_buffer (w, display_last_displayed_message_p,
10788 display_echo_area_1,
10789 (intptr_t) w, Qnil);
10790
10791 if (no_message_p)
10792 echo_area_buffer[i] = Qnil;
10793
10794 unbind_to (count, Qnil);
10795 return window_height_changed_p;
10796 }
10797
10798
10799 /* Helper for display_echo_area. Display the current buffer which
10800 contains the current echo area message in window W, a mini-window,
10801 a pointer to which is passed in A1. A2..A4 are currently not used.
10802 Change the height of W so that all of the message is displayed.
10803 Value is true if height of W was changed. */
10804
10805 static bool
10806 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10807 {
10808 intptr_t i1 = a1;
10809 struct window *w = (struct window *) i1;
10810 Lisp_Object window;
10811 struct text_pos start;
10812
10813 /* We are about to enter redisplay without going through
10814 redisplay_internal, so we need to forget these faces by hand
10815 here. */
10816 forget_escape_and_glyphless_faces ();
10817
10818 /* Do this before displaying, so that we have a large enough glyph
10819 matrix for the display. If we can't get enough space for the
10820 whole text, display the last N lines. That works by setting w->start. */
10821 bool window_height_changed_p = resize_mini_window (w, false);
10822
10823 /* Use the starting position chosen by resize_mini_window. */
10824 SET_TEXT_POS_FROM_MARKER (start, w->start);
10825
10826 /* Display. */
10827 clear_glyph_matrix (w->desired_matrix);
10828 XSETWINDOW (window, w);
10829 try_window (window, start, 0);
10830
10831 return window_height_changed_p;
10832 }
10833
10834
10835 /* Resize the echo area window to exactly the size needed for the
10836 currently displayed message, if there is one. If a mini-buffer
10837 is active, don't shrink it. */
10838
10839 void
10840 resize_echo_area_exactly (void)
10841 {
10842 if (BUFFERP (echo_area_buffer[0])
10843 && WINDOWP (echo_area_window))
10844 {
10845 struct window *w = XWINDOW (echo_area_window);
10846 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10847 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10848 (intptr_t) w, resize_exactly);
10849 if (resized_p)
10850 {
10851 windows_or_buffers_changed = 42;
10852 update_mode_lines = 30;
10853 redisplay_internal ();
10854 }
10855 }
10856 }
10857
10858
10859 /* Callback function for with_echo_area_buffer, when used from
10860 resize_echo_area_exactly. A1 contains a pointer to the window to
10861 resize, EXACTLY non-nil means resize the mini-window exactly to the
10862 size of the text displayed. A3 and A4 are not used. Value is what
10863 resize_mini_window returns. */
10864
10865 static bool
10866 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10867 {
10868 intptr_t i1 = a1;
10869 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10870 }
10871
10872
10873 /* Resize mini-window W to fit the size of its contents. EXACT_P
10874 means size the window exactly to the size needed. Otherwise, it's
10875 only enlarged until W's buffer is empty.
10876
10877 Set W->start to the right place to begin display. If the whole
10878 contents fit, start at the beginning. Otherwise, start so as
10879 to make the end of the contents appear. This is particularly
10880 important for y-or-n-p, but seems desirable generally.
10881
10882 Value is true if the window height has been changed. */
10883
10884 bool
10885 resize_mini_window (struct window *w, bool exact_p)
10886 {
10887 struct frame *f = XFRAME (w->frame);
10888 bool window_height_changed_p = false;
10889
10890 eassert (MINI_WINDOW_P (w));
10891
10892 /* By default, start display at the beginning. */
10893 set_marker_both (w->start, w->contents,
10894 BUF_BEGV (XBUFFER (w->contents)),
10895 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10896
10897 /* Don't resize windows while redisplaying a window; it would
10898 confuse redisplay functions when the size of the window they are
10899 displaying changes from under them. Such a resizing can happen,
10900 for instance, when which-func prints a long message while
10901 we are running fontification-functions. We're running these
10902 functions with safe_call which binds inhibit-redisplay to t. */
10903 if (!NILP (Vinhibit_redisplay))
10904 return false;
10905
10906 /* Nil means don't try to resize. */
10907 if (NILP (Vresize_mini_windows)
10908 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10909 return false;
10910
10911 if (!FRAME_MINIBUF_ONLY_P (f))
10912 {
10913 struct it it;
10914 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10915 + WINDOW_PIXEL_HEIGHT (w));
10916 int unit = FRAME_LINE_HEIGHT (f);
10917 int height, max_height;
10918 struct text_pos start;
10919 struct buffer *old_current_buffer = NULL;
10920
10921 if (current_buffer != XBUFFER (w->contents))
10922 {
10923 old_current_buffer = current_buffer;
10924 set_buffer_internal (XBUFFER (w->contents));
10925 }
10926
10927 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10928
10929 /* Compute the max. number of lines specified by the user. */
10930 if (FLOATP (Vmax_mini_window_height))
10931 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10932 else if (INTEGERP (Vmax_mini_window_height))
10933 max_height = XINT (Vmax_mini_window_height) * unit;
10934 else
10935 max_height = total_height / 4;
10936
10937 /* Correct that max. height if it's bogus. */
10938 max_height = clip_to_bounds (unit, max_height, total_height);
10939
10940 /* Find out the height of the text in the window. */
10941 if (it.line_wrap == TRUNCATE)
10942 height = unit;
10943 else
10944 {
10945 last_height = 0;
10946 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10947 if (it.max_ascent == 0 && it.max_descent == 0)
10948 height = it.current_y + last_height;
10949 else
10950 height = it.current_y + it.max_ascent + it.max_descent;
10951 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10952 }
10953
10954 /* Compute a suitable window start. */
10955 if (height > max_height)
10956 {
10957 height = (max_height / unit) * unit;
10958 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10959 move_it_vertically_backward (&it, height - unit);
10960 start = it.current.pos;
10961 }
10962 else
10963 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10964 SET_MARKER_FROM_TEXT_POS (w->start, start);
10965
10966 if (EQ (Vresize_mini_windows, Qgrow_only))
10967 {
10968 /* Let it grow only, until we display an empty message, in which
10969 case the window shrinks again. */
10970 if (height > WINDOW_PIXEL_HEIGHT (w))
10971 {
10972 int old_height = WINDOW_PIXEL_HEIGHT (w);
10973
10974 FRAME_WINDOWS_FROZEN (f) = true;
10975 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10976 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10977 }
10978 else if (height < WINDOW_PIXEL_HEIGHT (w)
10979 && (exact_p || BEGV == ZV))
10980 {
10981 int old_height = WINDOW_PIXEL_HEIGHT (w);
10982
10983 FRAME_WINDOWS_FROZEN (f) = false;
10984 shrink_mini_window (w, true);
10985 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10986 }
10987 }
10988 else
10989 {
10990 /* Always resize to exact size needed. */
10991 if (height > WINDOW_PIXEL_HEIGHT (w))
10992 {
10993 int old_height = WINDOW_PIXEL_HEIGHT (w);
10994
10995 FRAME_WINDOWS_FROZEN (f) = true;
10996 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10997 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10998 }
10999 else if (height < WINDOW_PIXEL_HEIGHT (w))
11000 {
11001 int old_height = WINDOW_PIXEL_HEIGHT (w);
11002
11003 FRAME_WINDOWS_FROZEN (f) = false;
11004 shrink_mini_window (w, true);
11005
11006 if (height)
11007 {
11008 FRAME_WINDOWS_FROZEN (f) = true;
11009 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11010 }
11011
11012 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11013 }
11014 }
11015
11016 if (old_current_buffer)
11017 set_buffer_internal (old_current_buffer);
11018 }
11019
11020 return window_height_changed_p;
11021 }
11022
11023
11024 /* Value is the current message, a string, or nil if there is no
11025 current message. */
11026
11027 Lisp_Object
11028 current_message (void)
11029 {
11030 Lisp_Object msg;
11031
11032 if (!BUFFERP (echo_area_buffer[0]))
11033 msg = Qnil;
11034 else
11035 {
11036 with_echo_area_buffer (0, 0, current_message_1,
11037 (intptr_t) &msg, Qnil);
11038 if (NILP (msg))
11039 echo_area_buffer[0] = Qnil;
11040 }
11041
11042 return msg;
11043 }
11044
11045
11046 static bool
11047 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11048 {
11049 intptr_t i1 = a1;
11050 Lisp_Object *msg = (Lisp_Object *) i1;
11051
11052 if (Z > BEG)
11053 *msg = make_buffer_string (BEG, Z, true);
11054 else
11055 *msg = Qnil;
11056 return false;
11057 }
11058
11059
11060 /* Push the current message on Vmessage_stack for later restoration
11061 by restore_message. Value is true if the current message isn't
11062 empty. This is a relatively infrequent operation, so it's not
11063 worth optimizing. */
11064
11065 bool
11066 push_message (void)
11067 {
11068 Lisp_Object msg = current_message ();
11069 Vmessage_stack = Fcons (msg, Vmessage_stack);
11070 return STRINGP (msg);
11071 }
11072
11073
11074 /* Restore message display from the top of Vmessage_stack. */
11075
11076 void
11077 restore_message (void)
11078 {
11079 eassert (CONSP (Vmessage_stack));
11080 message3_nolog (XCAR (Vmessage_stack));
11081 }
11082
11083
11084 /* Handler for unwind-protect calling pop_message. */
11085
11086 void
11087 pop_message_unwind (void)
11088 {
11089 /* Pop the top-most entry off Vmessage_stack. */
11090 eassert (CONSP (Vmessage_stack));
11091 Vmessage_stack = XCDR (Vmessage_stack);
11092 }
11093
11094
11095 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11096 exits. If the stack is not empty, we have a missing pop_message
11097 somewhere. */
11098
11099 void
11100 check_message_stack (void)
11101 {
11102 if (!NILP (Vmessage_stack))
11103 emacs_abort ();
11104 }
11105
11106
11107 /* Truncate to NCHARS what will be displayed in the echo area the next
11108 time we display it---but don't redisplay it now. */
11109
11110 void
11111 truncate_echo_area (ptrdiff_t nchars)
11112 {
11113 if (nchars == 0)
11114 echo_area_buffer[0] = Qnil;
11115 else if (!noninteractive
11116 && INTERACTIVE
11117 && !NILP (echo_area_buffer[0]))
11118 {
11119 struct frame *sf = SELECTED_FRAME ();
11120 /* Error messages get reported properly by cmd_error, so this must be
11121 just an informative message; if the frame hasn't really been
11122 initialized yet, just toss it. */
11123 if (sf->glyphs_initialized_p)
11124 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11125 }
11126 }
11127
11128
11129 /* Helper function for truncate_echo_area. Truncate the current
11130 message to at most NCHARS characters. */
11131
11132 static bool
11133 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11134 {
11135 if (BEG + nchars < Z)
11136 del_range (BEG + nchars, Z);
11137 if (Z == BEG)
11138 echo_area_buffer[0] = Qnil;
11139 return false;
11140 }
11141
11142 /* Set the current message to STRING. */
11143
11144 static void
11145 set_message (Lisp_Object string)
11146 {
11147 eassert (STRINGP (string));
11148
11149 message_enable_multibyte = STRING_MULTIBYTE (string);
11150
11151 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11152 message_buf_print = false;
11153 help_echo_showing_p = false;
11154
11155 if (STRINGP (Vdebug_on_message)
11156 && STRINGP (string)
11157 && fast_string_match (Vdebug_on_message, string) >= 0)
11158 call_debugger (list2 (Qerror, string));
11159 }
11160
11161
11162 /* Helper function for set_message. First argument is ignored and second
11163 argument has the same meaning as for set_message.
11164 This function is called with the echo area buffer being current. */
11165
11166 static bool
11167 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11168 {
11169 eassert (STRINGP (string));
11170
11171 /* Change multibyteness of the echo buffer appropriately. */
11172 if (message_enable_multibyte
11173 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11174 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11175
11176 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11177 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11178 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11179
11180 /* Insert new message at BEG. */
11181 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11182
11183 /* This function takes care of single/multibyte conversion.
11184 We just have to ensure that the echo area buffer has the right
11185 setting of enable_multibyte_characters. */
11186 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11187
11188 return false;
11189 }
11190
11191
11192 /* Clear messages. CURRENT_P means clear the current message.
11193 LAST_DISPLAYED_P means clear the message last displayed. */
11194
11195 void
11196 clear_message (bool current_p, bool last_displayed_p)
11197 {
11198 if (current_p)
11199 {
11200 echo_area_buffer[0] = Qnil;
11201 message_cleared_p = true;
11202 }
11203
11204 if (last_displayed_p)
11205 echo_area_buffer[1] = Qnil;
11206
11207 message_buf_print = false;
11208 }
11209
11210 /* Clear garbaged frames.
11211
11212 This function is used where the old redisplay called
11213 redraw_garbaged_frames which in turn called redraw_frame which in
11214 turn called clear_frame. The call to clear_frame was a source of
11215 flickering. I believe a clear_frame is not necessary. It should
11216 suffice in the new redisplay to invalidate all current matrices,
11217 and ensure a complete redisplay of all windows. */
11218
11219 static void
11220 clear_garbaged_frames (void)
11221 {
11222 if (frame_garbaged)
11223 {
11224 Lisp_Object tail, frame;
11225
11226 FOR_EACH_FRAME (tail, frame)
11227 {
11228 struct frame *f = XFRAME (frame);
11229
11230 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11231 {
11232 if (f->resized_p)
11233 redraw_frame (f);
11234 else
11235 clear_current_matrices (f);
11236 fset_redisplay (f);
11237 f->garbaged = false;
11238 f->resized_p = false;
11239 }
11240 }
11241
11242 frame_garbaged = false;
11243 }
11244 }
11245
11246
11247 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11248 selected_frame. */
11249
11250 static void
11251 echo_area_display (bool update_frame_p)
11252 {
11253 Lisp_Object mini_window;
11254 struct window *w;
11255 struct frame *f;
11256 bool window_height_changed_p = false;
11257 struct frame *sf = SELECTED_FRAME ();
11258
11259 mini_window = FRAME_MINIBUF_WINDOW (sf);
11260 w = XWINDOW (mini_window);
11261 f = XFRAME (WINDOW_FRAME (w));
11262
11263 /* Don't display if frame is invisible or not yet initialized. */
11264 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11265 return;
11266
11267 #ifdef HAVE_WINDOW_SYSTEM
11268 /* When Emacs starts, selected_frame may be the initial terminal
11269 frame. If we let this through, a message would be displayed on
11270 the terminal. */
11271 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11272 return;
11273 #endif /* HAVE_WINDOW_SYSTEM */
11274
11275 /* Redraw garbaged frames. */
11276 clear_garbaged_frames ();
11277
11278 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11279 {
11280 echo_area_window = mini_window;
11281 window_height_changed_p = display_echo_area (w);
11282 w->must_be_updated_p = true;
11283
11284 /* Update the display, unless called from redisplay_internal.
11285 Also don't update the screen during redisplay itself. The
11286 update will happen at the end of redisplay, and an update
11287 here could cause confusion. */
11288 if (update_frame_p && !redisplaying_p)
11289 {
11290 int n = 0;
11291
11292 /* If the display update has been interrupted by pending
11293 input, update mode lines in the frame. Due to the
11294 pending input, it might have been that redisplay hasn't
11295 been called, so that mode lines above the echo area are
11296 garbaged. This looks odd, so we prevent it here. */
11297 if (!display_completed)
11298 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11299
11300 if (window_height_changed_p
11301 /* Don't do this if Emacs is shutting down. Redisplay
11302 needs to run hooks. */
11303 && !NILP (Vrun_hooks))
11304 {
11305 /* Must update other windows. Likewise as in other
11306 cases, don't let this update be interrupted by
11307 pending input. */
11308 ptrdiff_t count = SPECPDL_INDEX ();
11309 specbind (Qredisplay_dont_pause, Qt);
11310 fset_redisplay (f);
11311 redisplay_internal ();
11312 unbind_to (count, Qnil);
11313 }
11314 else if (FRAME_WINDOW_P (f) && n == 0)
11315 {
11316 /* Window configuration is the same as before.
11317 Can do with a display update of the echo area,
11318 unless we displayed some mode lines. */
11319 update_single_window (w);
11320 flush_frame (f);
11321 }
11322 else
11323 update_frame (f, true, true);
11324
11325 /* If cursor is in the echo area, make sure that the next
11326 redisplay displays the minibuffer, so that the cursor will
11327 be replaced with what the minibuffer wants. */
11328 if (cursor_in_echo_area)
11329 wset_redisplay (XWINDOW (mini_window));
11330 }
11331 }
11332 else if (!EQ (mini_window, selected_window))
11333 wset_redisplay (XWINDOW (mini_window));
11334
11335 /* Last displayed message is now the current message. */
11336 echo_area_buffer[1] = echo_area_buffer[0];
11337 /* Inform read_char that we're not echoing. */
11338 echo_message_buffer = Qnil;
11339
11340 /* Prevent redisplay optimization in redisplay_internal by resetting
11341 this_line_start_pos. This is done because the mini-buffer now
11342 displays the message instead of its buffer text. */
11343 if (EQ (mini_window, selected_window))
11344 CHARPOS (this_line_start_pos) = 0;
11345
11346 if (window_height_changed_p)
11347 {
11348 fset_redisplay (f);
11349
11350 /* If window configuration was changed, frames may have been
11351 marked garbaged. Clear them or we will experience
11352 surprises wrt scrolling.
11353 FIXME: How/why/when? */
11354 clear_garbaged_frames ();
11355 }
11356 }
11357
11358 /* True if W's buffer was changed but not saved. */
11359
11360 static bool
11361 window_buffer_changed (struct window *w)
11362 {
11363 struct buffer *b = XBUFFER (w->contents);
11364
11365 eassert (BUFFER_LIVE_P (b));
11366
11367 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11368 }
11369
11370 /* True if W has %c in its mode line and mode line should be updated. */
11371
11372 static bool
11373 mode_line_update_needed (struct window *w)
11374 {
11375 return (w->column_number_displayed != -1
11376 && !(PT == w->last_point && !window_outdated (w))
11377 && (w->column_number_displayed != current_column ()));
11378 }
11379
11380 /* True if window start of W is frozen and may not be changed during
11381 redisplay. */
11382
11383 static bool
11384 window_frozen_p (struct window *w)
11385 {
11386 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11387 {
11388 Lisp_Object window;
11389
11390 XSETWINDOW (window, w);
11391 if (MINI_WINDOW_P (w))
11392 return false;
11393 else if (EQ (window, selected_window))
11394 return false;
11395 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11396 && EQ (window, Vminibuf_scroll_window))
11397 /* This special window can't be frozen too. */
11398 return false;
11399 else
11400 return true;
11401 }
11402 return false;
11403 }
11404
11405 /***********************************************************************
11406 Mode Lines and Frame Titles
11407 ***********************************************************************/
11408
11409 /* A buffer for constructing non-propertized mode-line strings and
11410 frame titles in it; allocated from the heap in init_xdisp and
11411 resized as needed in store_mode_line_noprop_char. */
11412
11413 static char *mode_line_noprop_buf;
11414
11415 /* The buffer's end, and a current output position in it. */
11416
11417 static char *mode_line_noprop_buf_end;
11418 static char *mode_line_noprop_ptr;
11419
11420 #define MODE_LINE_NOPROP_LEN(start) \
11421 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11422
11423 static enum {
11424 MODE_LINE_DISPLAY = 0,
11425 MODE_LINE_TITLE,
11426 MODE_LINE_NOPROP,
11427 MODE_LINE_STRING
11428 } mode_line_target;
11429
11430 /* Alist that caches the results of :propertize.
11431 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11432 static Lisp_Object mode_line_proptrans_alist;
11433
11434 /* List of strings making up the mode-line. */
11435 static Lisp_Object mode_line_string_list;
11436
11437 /* Base face property when building propertized mode line string. */
11438 static Lisp_Object mode_line_string_face;
11439 static Lisp_Object mode_line_string_face_prop;
11440
11441
11442 /* Unwind data for mode line strings */
11443
11444 static Lisp_Object Vmode_line_unwind_vector;
11445
11446 static Lisp_Object
11447 format_mode_line_unwind_data (struct frame *target_frame,
11448 struct buffer *obuf,
11449 Lisp_Object owin,
11450 bool save_proptrans)
11451 {
11452 Lisp_Object vector, tmp;
11453
11454 /* Reduce consing by keeping one vector in
11455 Vwith_echo_area_save_vector. */
11456 vector = Vmode_line_unwind_vector;
11457 Vmode_line_unwind_vector = Qnil;
11458
11459 if (NILP (vector))
11460 vector = Fmake_vector (make_number (10), Qnil);
11461
11462 ASET (vector, 0, make_number (mode_line_target));
11463 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11464 ASET (vector, 2, mode_line_string_list);
11465 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11466 ASET (vector, 4, mode_line_string_face);
11467 ASET (vector, 5, mode_line_string_face_prop);
11468
11469 if (obuf)
11470 XSETBUFFER (tmp, obuf);
11471 else
11472 tmp = Qnil;
11473 ASET (vector, 6, tmp);
11474 ASET (vector, 7, owin);
11475 if (target_frame)
11476 {
11477 /* Similarly to `with-selected-window', if the operation selects
11478 a window on another frame, we must restore that frame's
11479 selected window, and (for a tty) the top-frame. */
11480 ASET (vector, 8, target_frame->selected_window);
11481 if (FRAME_TERMCAP_P (target_frame))
11482 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11483 }
11484
11485 return vector;
11486 }
11487
11488 static void
11489 unwind_format_mode_line (Lisp_Object vector)
11490 {
11491 Lisp_Object old_window = AREF (vector, 7);
11492 Lisp_Object target_frame_window = AREF (vector, 8);
11493 Lisp_Object old_top_frame = AREF (vector, 9);
11494
11495 mode_line_target = XINT (AREF (vector, 0));
11496 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11497 mode_line_string_list = AREF (vector, 2);
11498 if (! EQ (AREF (vector, 3), Qt))
11499 mode_line_proptrans_alist = AREF (vector, 3);
11500 mode_line_string_face = AREF (vector, 4);
11501 mode_line_string_face_prop = AREF (vector, 5);
11502
11503 /* Select window before buffer, since it may change the buffer. */
11504 if (!NILP (old_window))
11505 {
11506 /* If the operation that we are unwinding had selected a window
11507 on a different frame, reset its frame-selected-window. For a
11508 text terminal, reset its top-frame if necessary. */
11509 if (!NILP (target_frame_window))
11510 {
11511 Lisp_Object frame
11512 = WINDOW_FRAME (XWINDOW (target_frame_window));
11513
11514 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11515 Fselect_window (target_frame_window, Qt);
11516
11517 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11518 Fselect_frame (old_top_frame, Qt);
11519 }
11520
11521 Fselect_window (old_window, Qt);
11522 }
11523
11524 if (!NILP (AREF (vector, 6)))
11525 {
11526 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11527 ASET (vector, 6, Qnil);
11528 }
11529
11530 Vmode_line_unwind_vector = vector;
11531 }
11532
11533
11534 /* Store a single character C for the frame title in mode_line_noprop_buf.
11535 Re-allocate mode_line_noprop_buf if necessary. */
11536
11537 static void
11538 store_mode_line_noprop_char (char c)
11539 {
11540 /* If output position has reached the end of the allocated buffer,
11541 increase the buffer's size. */
11542 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11543 {
11544 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11545 ptrdiff_t size = len;
11546 mode_line_noprop_buf =
11547 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11548 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11549 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11550 }
11551
11552 *mode_line_noprop_ptr++ = c;
11553 }
11554
11555
11556 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11557 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11558 characters that yield more columns than PRECISION; PRECISION <= 0
11559 means copy the whole string. Pad with spaces until FIELD_WIDTH
11560 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11561 pad. Called from display_mode_element when it is used to build a
11562 frame title. */
11563
11564 static int
11565 store_mode_line_noprop (const char *string, int field_width, int precision)
11566 {
11567 const unsigned char *str = (const unsigned char *) string;
11568 int n = 0;
11569 ptrdiff_t dummy, nbytes;
11570
11571 /* Copy at most PRECISION chars from STR. */
11572 nbytes = strlen (string);
11573 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11574 while (nbytes--)
11575 store_mode_line_noprop_char (*str++);
11576
11577 /* Fill up with spaces until FIELD_WIDTH reached. */
11578 while (field_width > 0
11579 && n < field_width)
11580 {
11581 store_mode_line_noprop_char (' ');
11582 ++n;
11583 }
11584
11585 return n;
11586 }
11587
11588 /***********************************************************************
11589 Frame Titles
11590 ***********************************************************************/
11591
11592 #ifdef HAVE_WINDOW_SYSTEM
11593
11594 /* Set the title of FRAME, if it has changed. The title format is
11595 Vicon_title_format if FRAME is iconified, otherwise it is
11596 frame_title_format. */
11597
11598 static void
11599 x_consider_frame_title (Lisp_Object frame)
11600 {
11601 struct frame *f = XFRAME (frame);
11602
11603 if ((FRAME_WINDOW_P (f)
11604 || FRAME_MINIBUF_ONLY_P (f)
11605 || f->explicit_name)
11606 && NILP (Fframe_parameter (frame, Qtooltip)))
11607 {
11608 /* Do we have more than one visible frame on this X display? */
11609 Lisp_Object tail, other_frame, fmt;
11610 ptrdiff_t title_start;
11611 char *title;
11612 ptrdiff_t len;
11613 struct it it;
11614 ptrdiff_t count = SPECPDL_INDEX ();
11615
11616 FOR_EACH_FRAME (tail, other_frame)
11617 {
11618 struct frame *tf = XFRAME (other_frame);
11619
11620 if (tf != f
11621 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11622 && !FRAME_MINIBUF_ONLY_P (tf)
11623 && !EQ (other_frame, tip_frame)
11624 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11625 break;
11626 }
11627
11628 /* Set global variable indicating that multiple frames exist. */
11629 multiple_frames = CONSP (tail);
11630
11631 /* Switch to the buffer of selected window of the frame. Set up
11632 mode_line_target so that display_mode_element will output into
11633 mode_line_noprop_buf; then display the title. */
11634 record_unwind_protect (unwind_format_mode_line,
11635 format_mode_line_unwind_data
11636 (f, current_buffer, selected_window, false));
11637
11638 Fselect_window (f->selected_window, Qt);
11639 set_buffer_internal_1
11640 (XBUFFER (XWINDOW (f->selected_window)->contents));
11641 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11642
11643 mode_line_target = MODE_LINE_TITLE;
11644 title_start = MODE_LINE_NOPROP_LEN (0);
11645 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11646 NULL, DEFAULT_FACE_ID);
11647 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11648 len = MODE_LINE_NOPROP_LEN (title_start);
11649 title = mode_line_noprop_buf + title_start;
11650 unbind_to (count, Qnil);
11651
11652 /* Set the title only if it's changed. This avoids consing in
11653 the common case where it hasn't. (If it turns out that we've
11654 already wasted too much time by walking through the list with
11655 display_mode_element, then we might need to optimize at a
11656 higher level than this.) */
11657 if (! STRINGP (f->name)
11658 || SBYTES (f->name) != len
11659 || memcmp (title, SDATA (f->name), len) != 0)
11660 x_implicitly_set_name (f, make_string (title, len), Qnil);
11661 }
11662 }
11663
11664 #endif /* not HAVE_WINDOW_SYSTEM */
11665
11666 \f
11667 /***********************************************************************
11668 Menu Bars
11669 ***********************************************************************/
11670
11671 /* True if we will not redisplay all visible windows. */
11672 #define REDISPLAY_SOME_P() \
11673 ((windows_or_buffers_changed == 0 \
11674 || windows_or_buffers_changed == REDISPLAY_SOME) \
11675 && (update_mode_lines == 0 \
11676 || update_mode_lines == REDISPLAY_SOME))
11677
11678 /* Prepare for redisplay by updating menu-bar item lists when
11679 appropriate. This can call eval. */
11680
11681 static void
11682 prepare_menu_bars (void)
11683 {
11684 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11685 bool some_windows = REDISPLAY_SOME_P ();
11686 Lisp_Object tooltip_frame;
11687
11688 #ifdef HAVE_WINDOW_SYSTEM
11689 tooltip_frame = tip_frame;
11690 #else
11691 tooltip_frame = Qnil;
11692 #endif
11693
11694 if (FUNCTIONP (Vpre_redisplay_function))
11695 {
11696 Lisp_Object windows = all_windows ? Qt : Qnil;
11697 if (all_windows && some_windows)
11698 {
11699 Lisp_Object ws = window_list ();
11700 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11701 {
11702 Lisp_Object this = XCAR (ws);
11703 struct window *w = XWINDOW (this);
11704 if (w->redisplay
11705 || XFRAME (w->frame)->redisplay
11706 || XBUFFER (w->contents)->text->redisplay)
11707 {
11708 windows = Fcons (this, windows);
11709 }
11710 }
11711 }
11712 safe__call1 (true, Vpre_redisplay_function, windows);
11713 }
11714
11715 /* Update all frame titles based on their buffer names, etc. We do
11716 this before the menu bars so that the buffer-menu will show the
11717 up-to-date frame titles. */
11718 #ifdef HAVE_WINDOW_SYSTEM
11719 if (all_windows)
11720 {
11721 Lisp_Object tail, frame;
11722
11723 FOR_EACH_FRAME (tail, frame)
11724 {
11725 struct frame *f = XFRAME (frame);
11726 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11727 if (some_windows
11728 && !f->redisplay
11729 && !w->redisplay
11730 && !XBUFFER (w->contents)->text->redisplay)
11731 continue;
11732
11733 if (!EQ (frame, tooltip_frame)
11734 && (FRAME_ICONIFIED_P (f)
11735 || FRAME_VISIBLE_P (f) == 1
11736 /* Exclude TTY frames that are obscured because they
11737 are not the top frame on their console. This is
11738 because x_consider_frame_title actually switches
11739 to the frame, which for TTY frames means it is
11740 marked as garbaged, and will be completely
11741 redrawn on the next redisplay cycle. This causes
11742 TTY frames to be completely redrawn, when there
11743 are more than one of them, even though nothing
11744 should be changed on display. */
11745 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11746 x_consider_frame_title (frame);
11747 }
11748 }
11749 #endif /* HAVE_WINDOW_SYSTEM */
11750
11751 /* Update the menu bar item lists, if appropriate. This has to be
11752 done before any actual redisplay or generation of display lines. */
11753
11754 if (all_windows)
11755 {
11756 Lisp_Object tail, frame;
11757 ptrdiff_t count = SPECPDL_INDEX ();
11758 /* True means that update_menu_bar has run its hooks
11759 so any further calls to update_menu_bar shouldn't do so again. */
11760 bool menu_bar_hooks_run = false;
11761
11762 record_unwind_save_match_data ();
11763
11764 FOR_EACH_FRAME (tail, frame)
11765 {
11766 struct frame *f = XFRAME (frame);
11767 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11768
11769 /* Ignore tooltip frame. */
11770 if (EQ (frame, tooltip_frame))
11771 continue;
11772
11773 if (some_windows
11774 && !f->redisplay
11775 && !w->redisplay
11776 && !XBUFFER (w->contents)->text->redisplay)
11777 continue;
11778
11779 /* If a window on this frame changed size, report that to
11780 the user and clear the size-change flag. */
11781 if (FRAME_WINDOW_SIZES_CHANGED (f))
11782 {
11783 Lisp_Object functions;
11784
11785 /* Clear flag first in case we get an error below. */
11786 FRAME_WINDOW_SIZES_CHANGED (f) = false;
11787 functions = Vwindow_size_change_functions;
11788
11789 while (CONSP (functions))
11790 {
11791 if (!EQ (XCAR (functions), Qt))
11792 call1 (XCAR (functions), frame);
11793 functions = XCDR (functions);
11794 }
11795 }
11796
11797 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11798 #ifdef HAVE_WINDOW_SYSTEM
11799 update_tool_bar (f, false);
11800 #endif
11801 }
11802
11803 unbind_to (count, Qnil);
11804 }
11805 else
11806 {
11807 struct frame *sf = SELECTED_FRAME ();
11808 update_menu_bar (sf, true, false);
11809 #ifdef HAVE_WINDOW_SYSTEM
11810 update_tool_bar (sf, true);
11811 #endif
11812 }
11813 }
11814
11815
11816 /* Update the menu bar item list for frame F. This has to be done
11817 before we start to fill in any display lines, because it can call
11818 eval.
11819
11820 If SAVE_MATCH_DATA, we must save and restore it here.
11821
11822 If HOOKS_RUN, a previous call to update_menu_bar
11823 already ran the menu bar hooks for this redisplay, so there
11824 is no need to run them again. The return value is the
11825 updated value of this flag, to pass to the next call. */
11826
11827 static bool
11828 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11829 {
11830 Lisp_Object window;
11831 struct window *w;
11832
11833 /* If called recursively during a menu update, do nothing. This can
11834 happen when, for instance, an activate-menubar-hook causes a
11835 redisplay. */
11836 if (inhibit_menubar_update)
11837 return hooks_run;
11838
11839 window = FRAME_SELECTED_WINDOW (f);
11840 w = XWINDOW (window);
11841
11842 if (FRAME_WINDOW_P (f)
11843 ?
11844 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11845 || defined (HAVE_NS) || defined (USE_GTK)
11846 FRAME_EXTERNAL_MENU_BAR (f)
11847 #else
11848 FRAME_MENU_BAR_LINES (f) > 0
11849 #endif
11850 : FRAME_MENU_BAR_LINES (f) > 0)
11851 {
11852 /* If the user has switched buffers or windows, we need to
11853 recompute to reflect the new bindings. But we'll
11854 recompute when update_mode_lines is set too; that means
11855 that people can use force-mode-line-update to request
11856 that the menu bar be recomputed. The adverse effect on
11857 the rest of the redisplay algorithm is about the same as
11858 windows_or_buffers_changed anyway. */
11859 if (windows_or_buffers_changed
11860 /* This used to test w->update_mode_line, but we believe
11861 there is no need to recompute the menu in that case. */
11862 || update_mode_lines
11863 || window_buffer_changed (w))
11864 {
11865 struct buffer *prev = current_buffer;
11866 ptrdiff_t count = SPECPDL_INDEX ();
11867
11868 specbind (Qinhibit_menubar_update, Qt);
11869
11870 set_buffer_internal_1 (XBUFFER (w->contents));
11871 if (save_match_data)
11872 record_unwind_save_match_data ();
11873 if (NILP (Voverriding_local_map_menu_flag))
11874 {
11875 specbind (Qoverriding_terminal_local_map, Qnil);
11876 specbind (Qoverriding_local_map, Qnil);
11877 }
11878
11879 if (!hooks_run)
11880 {
11881 /* Run the Lucid hook. */
11882 safe_run_hooks (Qactivate_menubar_hook);
11883
11884 /* If it has changed current-menubar from previous value,
11885 really recompute the menu-bar from the value. */
11886 if (! NILP (Vlucid_menu_bar_dirty_flag))
11887 call0 (Qrecompute_lucid_menubar);
11888
11889 safe_run_hooks (Qmenu_bar_update_hook);
11890
11891 hooks_run = true;
11892 }
11893
11894 XSETFRAME (Vmenu_updating_frame, f);
11895 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11896
11897 /* Redisplay the menu bar in case we changed it. */
11898 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11899 || defined (HAVE_NS) || defined (USE_GTK)
11900 if (FRAME_WINDOW_P (f))
11901 {
11902 #if defined (HAVE_NS)
11903 /* All frames on Mac OS share the same menubar. So only
11904 the selected frame should be allowed to set it. */
11905 if (f == SELECTED_FRAME ())
11906 #endif
11907 set_frame_menubar (f, false, false);
11908 }
11909 else
11910 /* On a terminal screen, the menu bar is an ordinary screen
11911 line, and this makes it get updated. */
11912 w->update_mode_line = true;
11913 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11914 /* In the non-toolkit version, the menu bar is an ordinary screen
11915 line, and this makes it get updated. */
11916 w->update_mode_line = true;
11917 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11918
11919 unbind_to (count, Qnil);
11920 set_buffer_internal_1 (prev);
11921 }
11922 }
11923
11924 return hooks_run;
11925 }
11926
11927 /***********************************************************************
11928 Tool-bars
11929 ***********************************************************************/
11930
11931 #ifdef HAVE_WINDOW_SYSTEM
11932
11933 /* Select `frame' temporarily without running all the code in
11934 do_switch_frame.
11935 FIXME: Maybe do_switch_frame should be trimmed down similarly
11936 when `norecord' is set. */
11937 static void
11938 fast_set_selected_frame (Lisp_Object frame)
11939 {
11940 if (!EQ (selected_frame, frame))
11941 {
11942 selected_frame = frame;
11943 selected_window = XFRAME (frame)->selected_window;
11944 }
11945 }
11946
11947 /* Update the tool-bar item list for frame F. This has to be done
11948 before we start to fill in any display lines. Called from
11949 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
11950 and restore it here. */
11951
11952 static void
11953 update_tool_bar (struct frame *f, bool save_match_data)
11954 {
11955 #if defined (USE_GTK) || defined (HAVE_NS)
11956 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11957 #else
11958 bool do_update = (WINDOWP (f->tool_bar_window)
11959 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11960 #endif
11961
11962 if (do_update)
11963 {
11964 Lisp_Object window;
11965 struct window *w;
11966
11967 window = FRAME_SELECTED_WINDOW (f);
11968 w = XWINDOW (window);
11969
11970 /* If the user has switched buffers or windows, we need to
11971 recompute to reflect the new bindings. But we'll
11972 recompute when update_mode_lines is set too; that means
11973 that people can use force-mode-line-update to request
11974 that the menu bar be recomputed. The adverse effect on
11975 the rest of the redisplay algorithm is about the same as
11976 windows_or_buffers_changed anyway. */
11977 if (windows_or_buffers_changed
11978 || w->update_mode_line
11979 || update_mode_lines
11980 || window_buffer_changed (w))
11981 {
11982 struct buffer *prev = current_buffer;
11983 ptrdiff_t count = SPECPDL_INDEX ();
11984 Lisp_Object frame, new_tool_bar;
11985 int new_n_tool_bar;
11986
11987 /* Set current_buffer to the buffer of the selected
11988 window of the frame, so that we get the right local
11989 keymaps. */
11990 set_buffer_internal_1 (XBUFFER (w->contents));
11991
11992 /* Save match data, if we must. */
11993 if (save_match_data)
11994 record_unwind_save_match_data ();
11995
11996 /* Make sure that we don't accidentally use bogus keymaps. */
11997 if (NILP (Voverriding_local_map_menu_flag))
11998 {
11999 specbind (Qoverriding_terminal_local_map, Qnil);
12000 specbind (Qoverriding_local_map, Qnil);
12001 }
12002
12003 /* We must temporarily set the selected frame to this frame
12004 before calling tool_bar_items, because the calculation of
12005 the tool-bar keymap uses the selected frame (see
12006 `tool-bar-make-keymap' in tool-bar.el). */
12007 eassert (EQ (selected_window,
12008 /* Since we only explicitly preserve selected_frame,
12009 check that selected_window would be redundant. */
12010 XFRAME (selected_frame)->selected_window));
12011 record_unwind_protect (fast_set_selected_frame, selected_frame);
12012 XSETFRAME (frame, f);
12013 fast_set_selected_frame (frame);
12014
12015 /* Build desired tool-bar items from keymaps. */
12016 new_tool_bar
12017 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12018 &new_n_tool_bar);
12019
12020 /* Redisplay the tool-bar if we changed it. */
12021 if (new_n_tool_bar != f->n_tool_bar_items
12022 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12023 {
12024 /* Redisplay that happens asynchronously due to an expose event
12025 may access f->tool_bar_items. Make sure we update both
12026 variables within BLOCK_INPUT so no such event interrupts. */
12027 block_input ();
12028 fset_tool_bar_items (f, new_tool_bar);
12029 f->n_tool_bar_items = new_n_tool_bar;
12030 w->update_mode_line = true;
12031 unblock_input ();
12032 }
12033
12034 unbind_to (count, Qnil);
12035 set_buffer_internal_1 (prev);
12036 }
12037 }
12038 }
12039
12040 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12041
12042 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12043 F's desired tool-bar contents. F->tool_bar_items must have
12044 been set up previously by calling prepare_menu_bars. */
12045
12046 static void
12047 build_desired_tool_bar_string (struct frame *f)
12048 {
12049 int i, size, size_needed;
12050 Lisp_Object image, plist;
12051
12052 image = plist = Qnil;
12053
12054 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12055 Otherwise, make a new string. */
12056
12057 /* The size of the string we might be able to reuse. */
12058 size = (STRINGP (f->desired_tool_bar_string)
12059 ? SCHARS (f->desired_tool_bar_string)
12060 : 0);
12061
12062 /* We need one space in the string for each image. */
12063 size_needed = f->n_tool_bar_items;
12064
12065 /* Reuse f->desired_tool_bar_string, if possible. */
12066 if (size < size_needed || NILP (f->desired_tool_bar_string))
12067 fset_desired_tool_bar_string
12068 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12069 else
12070 {
12071 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12072 Fremove_text_properties (make_number (0), make_number (size),
12073 props, f->desired_tool_bar_string);
12074 }
12075
12076 /* Put a `display' property on the string for the images to display,
12077 put a `menu_item' property on tool-bar items with a value that
12078 is the index of the item in F's tool-bar item vector. */
12079 for (i = 0; i < f->n_tool_bar_items; ++i)
12080 {
12081 #define PROP(IDX) \
12082 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12083
12084 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12085 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12086 int hmargin, vmargin, relief, idx, end;
12087
12088 /* If image is a vector, choose the image according to the
12089 button state. */
12090 image = PROP (TOOL_BAR_ITEM_IMAGES);
12091 if (VECTORP (image))
12092 {
12093 if (enabled_p)
12094 idx = (selected_p
12095 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12096 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12097 else
12098 idx = (selected_p
12099 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12100 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12101
12102 eassert (ASIZE (image) >= idx);
12103 image = AREF (image, idx);
12104 }
12105 else
12106 idx = -1;
12107
12108 /* Ignore invalid image specifications. */
12109 if (!valid_image_p (image))
12110 continue;
12111
12112 /* Display the tool-bar button pressed, or depressed. */
12113 plist = Fcopy_sequence (XCDR (image));
12114
12115 /* Compute margin and relief to draw. */
12116 relief = (tool_bar_button_relief >= 0
12117 ? tool_bar_button_relief
12118 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12119 hmargin = vmargin = relief;
12120
12121 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12122 INT_MAX - max (hmargin, vmargin)))
12123 {
12124 hmargin += XFASTINT (Vtool_bar_button_margin);
12125 vmargin += XFASTINT (Vtool_bar_button_margin);
12126 }
12127 else if (CONSP (Vtool_bar_button_margin))
12128 {
12129 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12130 INT_MAX - hmargin))
12131 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12132
12133 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12134 INT_MAX - vmargin))
12135 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12136 }
12137
12138 if (auto_raise_tool_bar_buttons_p)
12139 {
12140 /* Add a `:relief' property to the image spec if the item is
12141 selected. */
12142 if (selected_p)
12143 {
12144 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12145 hmargin -= relief;
12146 vmargin -= relief;
12147 }
12148 }
12149 else
12150 {
12151 /* If image is selected, display it pressed, i.e. with a
12152 negative relief. If it's not selected, display it with a
12153 raised relief. */
12154 plist = Fplist_put (plist, QCrelief,
12155 (selected_p
12156 ? make_number (-relief)
12157 : make_number (relief)));
12158 hmargin -= relief;
12159 vmargin -= relief;
12160 }
12161
12162 /* Put a margin around the image. */
12163 if (hmargin || vmargin)
12164 {
12165 if (hmargin == vmargin)
12166 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12167 else
12168 plist = Fplist_put (plist, QCmargin,
12169 Fcons (make_number (hmargin),
12170 make_number (vmargin)));
12171 }
12172
12173 /* If button is not enabled, and we don't have special images
12174 for the disabled state, make the image appear disabled by
12175 applying an appropriate algorithm to it. */
12176 if (!enabled_p && idx < 0)
12177 plist = Fplist_put (plist, QCconversion, Qdisabled);
12178
12179 /* Put a `display' text property on the string for the image to
12180 display. Put a `menu-item' property on the string that gives
12181 the start of this item's properties in the tool-bar items
12182 vector. */
12183 image = Fcons (Qimage, plist);
12184 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12185 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12186
12187 /* Let the last image hide all remaining spaces in the tool bar
12188 string. The string can be longer than needed when we reuse a
12189 previous string. */
12190 if (i + 1 == f->n_tool_bar_items)
12191 end = SCHARS (f->desired_tool_bar_string);
12192 else
12193 end = i + 1;
12194 Fadd_text_properties (make_number (i), make_number (end),
12195 props, f->desired_tool_bar_string);
12196 #undef PROP
12197 }
12198 }
12199
12200
12201 /* Display one line of the tool-bar of frame IT->f.
12202
12203 HEIGHT specifies the desired height of the tool-bar line.
12204 If the actual height of the glyph row is less than HEIGHT, the
12205 row's height is increased to HEIGHT, and the icons are centered
12206 vertically in the new height.
12207
12208 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12209 count a final empty row in case the tool-bar width exactly matches
12210 the window width.
12211 */
12212
12213 static void
12214 display_tool_bar_line (struct it *it, int height)
12215 {
12216 struct glyph_row *row = it->glyph_row;
12217 int max_x = it->last_visible_x;
12218 struct glyph *last;
12219
12220 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12221 clear_glyph_row (row);
12222 row->enabled_p = true;
12223 row->y = it->current_y;
12224
12225 /* Note that this isn't made use of if the face hasn't a box,
12226 so there's no need to check the face here. */
12227 it->start_of_box_run_p = true;
12228
12229 while (it->current_x < max_x)
12230 {
12231 int x, n_glyphs_before, i, nglyphs;
12232 struct it it_before;
12233
12234 /* Get the next display element. */
12235 if (!get_next_display_element (it))
12236 {
12237 /* Don't count empty row if we are counting needed tool-bar lines. */
12238 if (height < 0 && !it->hpos)
12239 return;
12240 break;
12241 }
12242
12243 /* Produce glyphs. */
12244 n_glyphs_before = row->used[TEXT_AREA];
12245 it_before = *it;
12246
12247 PRODUCE_GLYPHS (it);
12248
12249 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12250 i = 0;
12251 x = it_before.current_x;
12252 while (i < nglyphs)
12253 {
12254 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12255
12256 if (x + glyph->pixel_width > max_x)
12257 {
12258 /* Glyph doesn't fit on line. Backtrack. */
12259 row->used[TEXT_AREA] = n_glyphs_before;
12260 *it = it_before;
12261 /* If this is the only glyph on this line, it will never fit on the
12262 tool-bar, so skip it. But ensure there is at least one glyph,
12263 so we don't accidentally disable the tool-bar. */
12264 if (n_glyphs_before == 0
12265 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12266 break;
12267 goto out;
12268 }
12269
12270 ++it->hpos;
12271 x += glyph->pixel_width;
12272 ++i;
12273 }
12274
12275 /* Stop at line end. */
12276 if (ITERATOR_AT_END_OF_LINE_P (it))
12277 break;
12278
12279 set_iterator_to_next (it, true);
12280 }
12281
12282 out:;
12283
12284 row->displays_text_p = row->used[TEXT_AREA] != 0;
12285
12286 /* Use default face for the border below the tool bar.
12287
12288 FIXME: When auto-resize-tool-bars is grow-only, there is
12289 no additional border below the possibly empty tool-bar lines.
12290 So to make the extra empty lines look "normal", we have to
12291 use the tool-bar face for the border too. */
12292 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12293 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12294 it->face_id = DEFAULT_FACE_ID;
12295
12296 extend_face_to_end_of_line (it);
12297 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12298 last->right_box_line_p = true;
12299 if (last == row->glyphs[TEXT_AREA])
12300 last->left_box_line_p = true;
12301
12302 /* Make line the desired height and center it vertically. */
12303 if ((height -= it->max_ascent + it->max_descent) > 0)
12304 {
12305 /* Don't add more than one line height. */
12306 height %= FRAME_LINE_HEIGHT (it->f);
12307 it->max_ascent += height / 2;
12308 it->max_descent += (height + 1) / 2;
12309 }
12310
12311 compute_line_metrics (it);
12312
12313 /* If line is empty, make it occupy the rest of the tool-bar. */
12314 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12315 {
12316 row->height = row->phys_height = it->last_visible_y - row->y;
12317 row->visible_height = row->height;
12318 row->ascent = row->phys_ascent = 0;
12319 row->extra_line_spacing = 0;
12320 }
12321
12322 row->full_width_p = true;
12323 row->continued_p = false;
12324 row->truncated_on_left_p = false;
12325 row->truncated_on_right_p = false;
12326
12327 it->current_x = it->hpos = 0;
12328 it->current_y += row->height;
12329 ++it->vpos;
12330 ++it->glyph_row;
12331 }
12332
12333
12334 /* Value is the number of pixels needed to make all tool-bar items of
12335 frame F visible. The actual number of glyph rows needed is
12336 returned in *N_ROWS if non-NULL. */
12337 static int
12338 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12339 {
12340 struct window *w = XWINDOW (f->tool_bar_window);
12341 struct it it;
12342 /* tool_bar_height is called from redisplay_tool_bar after building
12343 the desired matrix, so use (unused) mode-line row as temporary row to
12344 avoid destroying the first tool-bar row. */
12345 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12346
12347 /* Initialize an iterator for iteration over
12348 F->desired_tool_bar_string in the tool-bar window of frame F. */
12349 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12350 temp_row->reversed_p = false;
12351 it.first_visible_x = 0;
12352 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12353 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12354 it.paragraph_embedding = L2R;
12355
12356 while (!ITERATOR_AT_END_P (&it))
12357 {
12358 clear_glyph_row (temp_row);
12359 it.glyph_row = temp_row;
12360 display_tool_bar_line (&it, -1);
12361 }
12362 clear_glyph_row (temp_row);
12363
12364 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12365 if (n_rows)
12366 *n_rows = it.vpos > 0 ? it.vpos : -1;
12367
12368 if (pixelwise)
12369 return it.current_y;
12370 else
12371 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12372 }
12373
12374 #endif /* !USE_GTK && !HAVE_NS */
12375
12376 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12377 0, 2, 0,
12378 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12379 If FRAME is nil or omitted, use the selected frame. Optional argument
12380 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12381 (Lisp_Object frame, Lisp_Object pixelwise)
12382 {
12383 int height = 0;
12384
12385 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12386 struct frame *f = decode_any_frame (frame);
12387
12388 if (WINDOWP (f->tool_bar_window)
12389 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12390 {
12391 update_tool_bar (f, true);
12392 if (f->n_tool_bar_items)
12393 {
12394 build_desired_tool_bar_string (f);
12395 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12396 }
12397 }
12398 #endif
12399
12400 return make_number (height);
12401 }
12402
12403
12404 /* Display the tool-bar of frame F. Value is true if tool-bar's
12405 height should be changed. */
12406 static bool
12407 redisplay_tool_bar (struct frame *f)
12408 {
12409 f->tool_bar_redisplayed = true;
12410 #if defined (USE_GTK) || defined (HAVE_NS)
12411
12412 if (FRAME_EXTERNAL_TOOL_BAR (f))
12413 update_frame_tool_bar (f);
12414 return false;
12415
12416 #else /* !USE_GTK && !HAVE_NS */
12417
12418 struct window *w;
12419 struct it it;
12420 struct glyph_row *row;
12421
12422 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12423 do anything. This means you must start with tool-bar-lines
12424 non-zero to get the auto-sizing effect. Or in other words, you
12425 can turn off tool-bars by specifying tool-bar-lines zero. */
12426 if (!WINDOWP (f->tool_bar_window)
12427 || (w = XWINDOW (f->tool_bar_window),
12428 WINDOW_TOTAL_LINES (w) == 0))
12429 return false;
12430
12431 /* Set up an iterator for the tool-bar window. */
12432 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12433 it.first_visible_x = 0;
12434 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12435 row = it.glyph_row;
12436 row->reversed_p = false;
12437
12438 /* Build a string that represents the contents of the tool-bar. */
12439 build_desired_tool_bar_string (f);
12440 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12441 /* FIXME: This should be controlled by a user option. But it
12442 doesn't make sense to have an R2L tool bar if the menu bar cannot
12443 be drawn also R2L, and making the menu bar R2L is tricky due
12444 toolkit-specific code that implements it. If an R2L tool bar is
12445 ever supported, display_tool_bar_line should also be augmented to
12446 call unproduce_glyphs like display_line and display_string
12447 do. */
12448 it.paragraph_embedding = L2R;
12449
12450 if (f->n_tool_bar_rows == 0)
12451 {
12452 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12453
12454 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12455 {
12456 x_change_tool_bar_height (f, new_height);
12457 frame_default_tool_bar_height = new_height;
12458 /* Always do that now. */
12459 clear_glyph_matrix (w->desired_matrix);
12460 f->fonts_changed = true;
12461 return true;
12462 }
12463 }
12464
12465 /* Display as many lines as needed to display all tool-bar items. */
12466
12467 if (f->n_tool_bar_rows > 0)
12468 {
12469 int border, rows, height, extra;
12470
12471 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12472 border = XINT (Vtool_bar_border);
12473 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12474 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12475 else if (EQ (Vtool_bar_border, Qborder_width))
12476 border = f->border_width;
12477 else
12478 border = 0;
12479 if (border < 0)
12480 border = 0;
12481
12482 rows = f->n_tool_bar_rows;
12483 height = max (1, (it.last_visible_y - border) / rows);
12484 extra = it.last_visible_y - border - height * rows;
12485
12486 while (it.current_y < it.last_visible_y)
12487 {
12488 int h = 0;
12489 if (extra > 0 && rows-- > 0)
12490 {
12491 h = (extra + rows - 1) / rows;
12492 extra -= h;
12493 }
12494 display_tool_bar_line (&it, height + h);
12495 }
12496 }
12497 else
12498 {
12499 while (it.current_y < it.last_visible_y)
12500 display_tool_bar_line (&it, 0);
12501 }
12502
12503 /* It doesn't make much sense to try scrolling in the tool-bar
12504 window, so don't do it. */
12505 w->desired_matrix->no_scrolling_p = true;
12506 w->must_be_updated_p = true;
12507
12508 if (!NILP (Vauto_resize_tool_bars))
12509 {
12510 bool change_height_p = true;
12511
12512 /* If we couldn't display everything, change the tool-bar's
12513 height if there is room for more. */
12514 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12515 change_height_p = true;
12516
12517 /* We subtract 1 because display_tool_bar_line advances the
12518 glyph_row pointer before returning to its caller. We want to
12519 examine the last glyph row produced by
12520 display_tool_bar_line. */
12521 row = it.glyph_row - 1;
12522
12523 /* If there are blank lines at the end, except for a partially
12524 visible blank line at the end that is smaller than
12525 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12526 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12527 && row->height >= FRAME_LINE_HEIGHT (f))
12528 change_height_p = true;
12529
12530 /* If row displays tool-bar items, but is partially visible,
12531 change the tool-bar's height. */
12532 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12533 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12534 change_height_p = true;
12535
12536 /* Resize windows as needed by changing the `tool-bar-lines'
12537 frame parameter. */
12538 if (change_height_p)
12539 {
12540 int nrows;
12541 int new_height = tool_bar_height (f, &nrows, true);
12542
12543 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12544 && !f->minimize_tool_bar_window_p)
12545 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12546 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12547 f->minimize_tool_bar_window_p = false;
12548
12549 if (change_height_p)
12550 {
12551 x_change_tool_bar_height (f, new_height);
12552 frame_default_tool_bar_height = new_height;
12553 clear_glyph_matrix (w->desired_matrix);
12554 f->n_tool_bar_rows = nrows;
12555 f->fonts_changed = true;
12556
12557 return true;
12558 }
12559 }
12560 }
12561
12562 f->minimize_tool_bar_window_p = false;
12563 return false;
12564
12565 #endif /* USE_GTK || HAVE_NS */
12566 }
12567
12568 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12569
12570 /* Get information about the tool-bar item which is displayed in GLYPH
12571 on frame F. Return in *PROP_IDX the index where tool-bar item
12572 properties start in F->tool_bar_items. Value is false if
12573 GLYPH doesn't display a tool-bar item. */
12574
12575 static bool
12576 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12577 {
12578 Lisp_Object prop;
12579 int charpos;
12580
12581 /* This function can be called asynchronously, which means we must
12582 exclude any possibility that Fget_text_property signals an
12583 error. */
12584 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12585 charpos = max (0, charpos);
12586
12587 /* Get the text property `menu-item' at pos. The value of that
12588 property is the start index of this item's properties in
12589 F->tool_bar_items. */
12590 prop = Fget_text_property (make_number (charpos),
12591 Qmenu_item, f->current_tool_bar_string);
12592 if (! INTEGERP (prop))
12593 return false;
12594 *prop_idx = XINT (prop);
12595 return true;
12596 }
12597
12598 \f
12599 /* Get information about the tool-bar item at position X/Y on frame F.
12600 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12601 the current matrix of the tool-bar window of F, or NULL if not
12602 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12603 item in F->tool_bar_items. Value is
12604
12605 -1 if X/Y is not on a tool-bar item
12606 0 if X/Y is on the same item that was highlighted before.
12607 1 otherwise. */
12608
12609 static int
12610 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12611 int *hpos, int *vpos, int *prop_idx)
12612 {
12613 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12614 struct window *w = XWINDOW (f->tool_bar_window);
12615 int area;
12616
12617 /* Find the glyph under X/Y. */
12618 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12619 if (*glyph == NULL)
12620 return -1;
12621
12622 /* Get the start of this tool-bar item's properties in
12623 f->tool_bar_items. */
12624 if (!tool_bar_item_info (f, *glyph, prop_idx))
12625 return -1;
12626
12627 /* Is mouse on the highlighted item? */
12628 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12629 && *vpos >= hlinfo->mouse_face_beg_row
12630 && *vpos <= hlinfo->mouse_face_end_row
12631 && (*vpos > hlinfo->mouse_face_beg_row
12632 || *hpos >= hlinfo->mouse_face_beg_col)
12633 && (*vpos < hlinfo->mouse_face_end_row
12634 || *hpos < hlinfo->mouse_face_end_col
12635 || hlinfo->mouse_face_past_end))
12636 return 0;
12637
12638 return 1;
12639 }
12640
12641
12642 /* EXPORT:
12643 Handle mouse button event on the tool-bar of frame F, at
12644 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12645 false for button release. MODIFIERS is event modifiers for button
12646 release. */
12647
12648 void
12649 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12650 int modifiers)
12651 {
12652 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12653 struct window *w = XWINDOW (f->tool_bar_window);
12654 int hpos, vpos, prop_idx;
12655 struct glyph *glyph;
12656 Lisp_Object enabled_p;
12657 int ts;
12658
12659 /* If not on the highlighted tool-bar item, and mouse-highlight is
12660 non-nil, return. This is so we generate the tool-bar button
12661 click only when the mouse button is released on the same item as
12662 where it was pressed. However, when mouse-highlight is disabled,
12663 generate the click when the button is released regardless of the
12664 highlight, since tool-bar items are not highlighted in that
12665 case. */
12666 frame_to_window_pixel_xy (w, &x, &y);
12667 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12668 if (ts == -1
12669 || (ts != 0 && !NILP (Vmouse_highlight)))
12670 return;
12671
12672 /* When mouse-highlight is off, generate the click for the item
12673 where the button was pressed, disregarding where it was
12674 released. */
12675 if (NILP (Vmouse_highlight) && !down_p)
12676 prop_idx = f->last_tool_bar_item;
12677
12678 /* If item is disabled, do nothing. */
12679 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12680 if (NILP (enabled_p))
12681 return;
12682
12683 if (down_p)
12684 {
12685 /* Show item in pressed state. */
12686 if (!NILP (Vmouse_highlight))
12687 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12688 f->last_tool_bar_item = prop_idx;
12689 }
12690 else
12691 {
12692 Lisp_Object key, frame;
12693 struct input_event event;
12694 EVENT_INIT (event);
12695
12696 /* Show item in released state. */
12697 if (!NILP (Vmouse_highlight))
12698 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12699
12700 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12701
12702 XSETFRAME (frame, f);
12703 event.kind = TOOL_BAR_EVENT;
12704 event.frame_or_window = frame;
12705 event.arg = frame;
12706 kbd_buffer_store_event (&event);
12707
12708 event.kind = TOOL_BAR_EVENT;
12709 event.frame_or_window = frame;
12710 event.arg = key;
12711 event.modifiers = modifiers;
12712 kbd_buffer_store_event (&event);
12713 f->last_tool_bar_item = -1;
12714 }
12715 }
12716
12717
12718 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12719 tool-bar window-relative coordinates X/Y. Called from
12720 note_mouse_highlight. */
12721
12722 static void
12723 note_tool_bar_highlight (struct frame *f, int x, int y)
12724 {
12725 Lisp_Object window = f->tool_bar_window;
12726 struct window *w = XWINDOW (window);
12727 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12728 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12729 int hpos, vpos;
12730 struct glyph *glyph;
12731 struct glyph_row *row;
12732 int i;
12733 Lisp_Object enabled_p;
12734 int prop_idx;
12735 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12736 bool mouse_down_p;
12737 int rc;
12738
12739 /* Function note_mouse_highlight is called with negative X/Y
12740 values when mouse moves outside of the frame. */
12741 if (x <= 0 || y <= 0)
12742 {
12743 clear_mouse_face (hlinfo);
12744 return;
12745 }
12746
12747 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12748 if (rc < 0)
12749 {
12750 /* Not on tool-bar item. */
12751 clear_mouse_face (hlinfo);
12752 return;
12753 }
12754 else if (rc == 0)
12755 /* On same tool-bar item as before. */
12756 goto set_help_echo;
12757
12758 clear_mouse_face (hlinfo);
12759
12760 /* Mouse is down, but on different tool-bar item? */
12761 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12762 && f == dpyinfo->last_mouse_frame);
12763
12764 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12765 return;
12766
12767 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12768
12769 /* If tool-bar item is not enabled, don't highlight it. */
12770 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12771 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12772 {
12773 /* Compute the x-position of the glyph. In front and past the
12774 image is a space. We include this in the highlighted area. */
12775 row = MATRIX_ROW (w->current_matrix, vpos);
12776 for (i = x = 0; i < hpos; ++i)
12777 x += row->glyphs[TEXT_AREA][i].pixel_width;
12778
12779 /* Record this as the current active region. */
12780 hlinfo->mouse_face_beg_col = hpos;
12781 hlinfo->mouse_face_beg_row = vpos;
12782 hlinfo->mouse_face_beg_x = x;
12783 hlinfo->mouse_face_past_end = false;
12784
12785 hlinfo->mouse_face_end_col = hpos + 1;
12786 hlinfo->mouse_face_end_row = vpos;
12787 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12788 hlinfo->mouse_face_window = window;
12789 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12790
12791 /* Display it as active. */
12792 show_mouse_face (hlinfo, draw);
12793 }
12794
12795 set_help_echo:
12796
12797 /* Set help_echo_string to a help string to display for this tool-bar item.
12798 XTread_socket does the rest. */
12799 help_echo_object = help_echo_window = Qnil;
12800 help_echo_pos = -1;
12801 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12802 if (NILP (help_echo_string))
12803 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12804 }
12805
12806 #endif /* !USE_GTK && !HAVE_NS */
12807
12808 #endif /* HAVE_WINDOW_SYSTEM */
12809
12810
12811 \f
12812 /************************************************************************
12813 Horizontal scrolling
12814 ************************************************************************/
12815
12816 /* For all leaf windows in the window tree rooted at WINDOW, set their
12817 hscroll value so that PT is (i) visible in the window, and (ii) so
12818 that it is not within a certain margin at the window's left and
12819 right border. Value is true if any window's hscroll has been
12820 changed. */
12821
12822 static bool
12823 hscroll_window_tree (Lisp_Object window)
12824 {
12825 bool hscrolled_p = false;
12826 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12827 int hscroll_step_abs = 0;
12828 double hscroll_step_rel = 0;
12829
12830 if (hscroll_relative_p)
12831 {
12832 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12833 if (hscroll_step_rel < 0)
12834 {
12835 hscroll_relative_p = false;
12836 hscroll_step_abs = 0;
12837 }
12838 }
12839 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12840 {
12841 hscroll_step_abs = XINT (Vhscroll_step);
12842 if (hscroll_step_abs < 0)
12843 hscroll_step_abs = 0;
12844 }
12845 else
12846 hscroll_step_abs = 0;
12847
12848 while (WINDOWP (window))
12849 {
12850 struct window *w = XWINDOW (window);
12851
12852 if (WINDOWP (w->contents))
12853 hscrolled_p |= hscroll_window_tree (w->contents);
12854 else if (w->cursor.vpos >= 0)
12855 {
12856 int h_margin;
12857 int text_area_width;
12858 struct glyph_row *cursor_row;
12859 struct glyph_row *bottom_row;
12860
12861 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12862 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12863 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12864 else
12865 cursor_row = bottom_row - 1;
12866
12867 if (!cursor_row->enabled_p)
12868 {
12869 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12870 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12871 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12872 else
12873 cursor_row = bottom_row - 1;
12874 }
12875 bool row_r2l_p = cursor_row->reversed_p;
12876
12877 text_area_width = window_box_width (w, TEXT_AREA);
12878
12879 /* Scroll when cursor is inside this scroll margin. */
12880 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12881
12882 /* If the position of this window's point has explicitly
12883 changed, no more suspend auto hscrolling. */
12884 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12885 w->suspend_auto_hscroll = false;
12886
12887 /* Remember window point. */
12888 Fset_marker (w->old_pointm,
12889 ((w == XWINDOW (selected_window))
12890 ? make_number (BUF_PT (XBUFFER (w->contents)))
12891 : Fmarker_position (w->pointm)),
12892 w->contents);
12893
12894 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12895 && !w->suspend_auto_hscroll
12896 /* In some pathological cases, like restoring a window
12897 configuration into a frame that is much smaller than
12898 the one from which the configuration was saved, we
12899 get glyph rows whose start and end have zero buffer
12900 positions, which we cannot handle below. Just skip
12901 such windows. */
12902 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12903 /* For left-to-right rows, hscroll when cursor is either
12904 (i) inside the right hscroll margin, or (ii) if it is
12905 inside the left margin and the window is already
12906 hscrolled. */
12907 && ((!row_r2l_p
12908 && ((w->hscroll && w->cursor.x <= h_margin)
12909 || (cursor_row->enabled_p
12910 && cursor_row->truncated_on_right_p
12911 && (w->cursor.x >= text_area_width - h_margin))))
12912 /* For right-to-left rows, the logic is similar,
12913 except that rules for scrolling to left and right
12914 are reversed. E.g., if cursor.x <= h_margin, we
12915 need to hscroll "to the right" unconditionally,
12916 and that will scroll the screen to the left so as
12917 to reveal the next portion of the row. */
12918 || (row_r2l_p
12919 && ((cursor_row->enabled_p
12920 /* FIXME: It is confusing to set the
12921 truncated_on_right_p flag when R2L rows
12922 are actually truncated on the left. */
12923 && cursor_row->truncated_on_right_p
12924 && w->cursor.x <= h_margin)
12925 || (w->hscroll
12926 && (w->cursor.x >= text_area_width - h_margin))))))
12927 {
12928 struct it it;
12929 ptrdiff_t hscroll;
12930 struct buffer *saved_current_buffer;
12931 ptrdiff_t pt;
12932 int wanted_x;
12933
12934 /* Find point in a display of infinite width. */
12935 saved_current_buffer = current_buffer;
12936 current_buffer = XBUFFER (w->contents);
12937
12938 if (w == XWINDOW (selected_window))
12939 pt = PT;
12940 else
12941 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12942
12943 /* Move iterator to pt starting at cursor_row->start in
12944 a line with infinite width. */
12945 init_to_row_start (&it, w, cursor_row);
12946 it.last_visible_x = INFINITY;
12947 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12948 current_buffer = saved_current_buffer;
12949
12950 /* Position cursor in window. */
12951 if (!hscroll_relative_p && hscroll_step_abs == 0)
12952 hscroll = max (0, (it.current_x
12953 - (ITERATOR_AT_END_OF_LINE_P (&it)
12954 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12955 : (text_area_width / 2))))
12956 / FRAME_COLUMN_WIDTH (it.f);
12957 else if ((!row_r2l_p
12958 && w->cursor.x >= text_area_width - h_margin)
12959 || (row_r2l_p && w->cursor.x <= h_margin))
12960 {
12961 if (hscroll_relative_p)
12962 wanted_x = text_area_width * (1 - hscroll_step_rel)
12963 - h_margin;
12964 else
12965 wanted_x = text_area_width
12966 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12967 - h_margin;
12968 hscroll
12969 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12970 }
12971 else
12972 {
12973 if (hscroll_relative_p)
12974 wanted_x = text_area_width * hscroll_step_rel
12975 + h_margin;
12976 else
12977 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12978 + h_margin;
12979 hscroll
12980 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12981 }
12982 hscroll = max (hscroll, w->min_hscroll);
12983
12984 /* Don't prevent redisplay optimizations if hscroll
12985 hasn't changed, as it will unnecessarily slow down
12986 redisplay. */
12987 if (w->hscroll != hscroll)
12988 {
12989 struct buffer *b = XBUFFER (w->contents);
12990 b->prevent_redisplay_optimizations_p = true;
12991 w->hscroll = hscroll;
12992 hscrolled_p = true;
12993 }
12994 }
12995 }
12996
12997 window = w->next;
12998 }
12999
13000 /* Value is true if hscroll of any leaf window has been changed. */
13001 return hscrolled_p;
13002 }
13003
13004
13005 /* Set hscroll so that cursor is visible and not inside horizontal
13006 scroll margins for all windows in the tree rooted at WINDOW. See
13007 also hscroll_window_tree above. Value is true if any window's
13008 hscroll has been changed. If it has, desired matrices on the frame
13009 of WINDOW are cleared. */
13010
13011 static bool
13012 hscroll_windows (Lisp_Object window)
13013 {
13014 bool hscrolled_p = hscroll_window_tree (window);
13015 if (hscrolled_p)
13016 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13017 return hscrolled_p;
13018 }
13019
13020
13021 \f
13022 /************************************************************************
13023 Redisplay
13024 ************************************************************************/
13025
13026 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13027 This is sometimes handy to have in a debugger session. */
13028
13029 #ifdef GLYPH_DEBUG
13030
13031 /* First and last unchanged row for try_window_id. */
13032
13033 static int debug_first_unchanged_at_end_vpos;
13034 static int debug_last_unchanged_at_beg_vpos;
13035
13036 /* Delta vpos and y. */
13037
13038 static int debug_dvpos, debug_dy;
13039
13040 /* Delta in characters and bytes for try_window_id. */
13041
13042 static ptrdiff_t debug_delta, debug_delta_bytes;
13043
13044 /* Values of window_end_pos and window_end_vpos at the end of
13045 try_window_id. */
13046
13047 static ptrdiff_t debug_end_vpos;
13048
13049 /* Append a string to W->desired_matrix->method. FMT is a printf
13050 format string. If trace_redisplay_p is true also printf the
13051 resulting string to stderr. */
13052
13053 static void debug_method_add (struct window *, char const *, ...)
13054 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13055
13056 static void
13057 debug_method_add (struct window *w, char const *fmt, ...)
13058 {
13059 void *ptr = w;
13060 char *method = w->desired_matrix->method;
13061 int len = strlen (method);
13062 int size = sizeof w->desired_matrix->method;
13063 int remaining = size - len - 1;
13064 va_list ap;
13065
13066 if (len && remaining)
13067 {
13068 method[len] = '|';
13069 --remaining, ++len;
13070 }
13071
13072 va_start (ap, fmt);
13073 vsnprintf (method + len, remaining + 1, fmt, ap);
13074 va_end (ap);
13075
13076 if (trace_redisplay_p)
13077 fprintf (stderr, "%p (%s): %s\n",
13078 ptr,
13079 ((BUFFERP (w->contents)
13080 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13081 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13082 : "no buffer"),
13083 method + len);
13084 }
13085
13086 #endif /* GLYPH_DEBUG */
13087
13088
13089 /* Value is true if all changes in window W, which displays
13090 current_buffer, are in the text between START and END. START is a
13091 buffer position, END is given as a distance from Z. Used in
13092 redisplay_internal for display optimization. */
13093
13094 static bool
13095 text_outside_line_unchanged_p (struct window *w,
13096 ptrdiff_t start, ptrdiff_t end)
13097 {
13098 bool unchanged_p = true;
13099
13100 /* If text or overlays have changed, see where. */
13101 if (window_outdated (w))
13102 {
13103 /* Gap in the line? */
13104 if (GPT < start || Z - GPT < end)
13105 unchanged_p = false;
13106
13107 /* Changes start in front of the line, or end after it? */
13108 if (unchanged_p
13109 && (BEG_UNCHANGED < start - 1
13110 || END_UNCHANGED < end))
13111 unchanged_p = false;
13112
13113 /* If selective display, can't optimize if changes start at the
13114 beginning of the line. */
13115 if (unchanged_p
13116 && INTEGERP (BVAR (current_buffer, selective_display))
13117 && XINT (BVAR (current_buffer, selective_display)) > 0
13118 && (BEG_UNCHANGED < start || GPT <= start))
13119 unchanged_p = false;
13120
13121 /* If there are overlays at the start or end of the line, these
13122 may have overlay strings with newlines in them. A change at
13123 START, for instance, may actually concern the display of such
13124 overlay strings as well, and they are displayed on different
13125 lines. So, quickly rule out this case. (For the future, it
13126 might be desirable to implement something more telling than
13127 just BEG/END_UNCHANGED.) */
13128 if (unchanged_p)
13129 {
13130 if (BEG + BEG_UNCHANGED == start
13131 && overlay_touches_p (start))
13132 unchanged_p = false;
13133 if (END_UNCHANGED == end
13134 && overlay_touches_p (Z - end))
13135 unchanged_p = false;
13136 }
13137
13138 /* Under bidi reordering, adding or deleting a character in the
13139 beginning of a paragraph, before the first strong directional
13140 character, can change the base direction of the paragraph (unless
13141 the buffer specifies a fixed paragraph direction), which will
13142 require redisplaying the whole paragraph. It might be worthwhile
13143 to find the paragraph limits and widen the range of redisplayed
13144 lines to that, but for now just give up this optimization. */
13145 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13146 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13147 unchanged_p = false;
13148 }
13149
13150 return unchanged_p;
13151 }
13152
13153
13154 /* Do a frame update, taking possible shortcuts into account. This is
13155 the main external entry point for redisplay.
13156
13157 If the last redisplay displayed an echo area message and that message
13158 is no longer requested, we clear the echo area or bring back the
13159 mini-buffer if that is in use. */
13160
13161 void
13162 redisplay (void)
13163 {
13164 redisplay_internal ();
13165 }
13166
13167
13168 static Lisp_Object
13169 overlay_arrow_string_or_property (Lisp_Object var)
13170 {
13171 Lisp_Object val;
13172
13173 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13174 return val;
13175
13176 return Voverlay_arrow_string;
13177 }
13178
13179 /* Return true if there are any overlay-arrows in current_buffer. */
13180 static bool
13181 overlay_arrow_in_current_buffer_p (void)
13182 {
13183 Lisp_Object vlist;
13184
13185 for (vlist = Voverlay_arrow_variable_list;
13186 CONSP (vlist);
13187 vlist = XCDR (vlist))
13188 {
13189 Lisp_Object var = XCAR (vlist);
13190 Lisp_Object val;
13191
13192 if (!SYMBOLP (var))
13193 continue;
13194 val = find_symbol_value (var);
13195 if (MARKERP (val)
13196 && current_buffer == XMARKER (val)->buffer)
13197 return true;
13198 }
13199 return false;
13200 }
13201
13202
13203 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13204 has changed. */
13205
13206 static bool
13207 overlay_arrows_changed_p (void)
13208 {
13209 Lisp_Object vlist;
13210
13211 for (vlist = Voverlay_arrow_variable_list;
13212 CONSP (vlist);
13213 vlist = XCDR (vlist))
13214 {
13215 Lisp_Object var = XCAR (vlist);
13216 Lisp_Object val, pstr;
13217
13218 if (!SYMBOLP (var))
13219 continue;
13220 val = find_symbol_value (var);
13221 if (!MARKERP (val))
13222 continue;
13223 if (! EQ (COERCE_MARKER (val),
13224 Fget (var, Qlast_arrow_position))
13225 || ! (pstr = overlay_arrow_string_or_property (var),
13226 EQ (pstr, Fget (var, Qlast_arrow_string))))
13227 return true;
13228 }
13229 return false;
13230 }
13231
13232 /* Mark overlay arrows to be updated on next redisplay. */
13233
13234 static void
13235 update_overlay_arrows (int up_to_date)
13236 {
13237 Lisp_Object vlist;
13238
13239 for (vlist = Voverlay_arrow_variable_list;
13240 CONSP (vlist);
13241 vlist = XCDR (vlist))
13242 {
13243 Lisp_Object var = XCAR (vlist);
13244
13245 if (!SYMBOLP (var))
13246 continue;
13247
13248 if (up_to_date > 0)
13249 {
13250 Lisp_Object val = find_symbol_value (var);
13251 Fput (var, Qlast_arrow_position,
13252 COERCE_MARKER (val));
13253 Fput (var, Qlast_arrow_string,
13254 overlay_arrow_string_or_property (var));
13255 }
13256 else if (up_to_date < 0
13257 || !NILP (Fget (var, Qlast_arrow_position)))
13258 {
13259 Fput (var, Qlast_arrow_position, Qt);
13260 Fput (var, Qlast_arrow_string, Qt);
13261 }
13262 }
13263 }
13264
13265
13266 /* Return overlay arrow string to display at row.
13267 Return integer (bitmap number) for arrow bitmap in left fringe.
13268 Return nil if no overlay arrow. */
13269
13270 static Lisp_Object
13271 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13272 {
13273 Lisp_Object vlist;
13274
13275 for (vlist = Voverlay_arrow_variable_list;
13276 CONSP (vlist);
13277 vlist = XCDR (vlist))
13278 {
13279 Lisp_Object var = XCAR (vlist);
13280 Lisp_Object val;
13281
13282 if (!SYMBOLP (var))
13283 continue;
13284
13285 val = find_symbol_value (var);
13286
13287 if (MARKERP (val)
13288 && current_buffer == XMARKER (val)->buffer
13289 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13290 {
13291 if (FRAME_WINDOW_P (it->f)
13292 /* FIXME: if ROW->reversed_p is set, this should test
13293 the right fringe, not the left one. */
13294 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13295 {
13296 #ifdef HAVE_WINDOW_SYSTEM
13297 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13298 {
13299 int fringe_bitmap = lookup_fringe_bitmap (val);
13300 if (fringe_bitmap != 0)
13301 return make_number (fringe_bitmap);
13302 }
13303 #endif
13304 return make_number (-1); /* Use default arrow bitmap. */
13305 }
13306 return overlay_arrow_string_or_property (var);
13307 }
13308 }
13309
13310 return Qnil;
13311 }
13312
13313 /* Return true if point moved out of or into a composition. Otherwise
13314 return false. PREV_BUF and PREV_PT are the last point buffer and
13315 position. BUF and PT are the current point buffer and position. */
13316
13317 static bool
13318 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13319 struct buffer *buf, ptrdiff_t pt)
13320 {
13321 ptrdiff_t start, end;
13322 Lisp_Object prop;
13323 Lisp_Object buffer;
13324
13325 XSETBUFFER (buffer, buf);
13326 /* Check a composition at the last point if point moved within the
13327 same buffer. */
13328 if (prev_buf == buf)
13329 {
13330 if (prev_pt == pt)
13331 /* Point didn't move. */
13332 return false;
13333
13334 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13335 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13336 && composition_valid_p (start, end, prop)
13337 && start < prev_pt && end > prev_pt)
13338 /* The last point was within the composition. Return true iff
13339 point moved out of the composition. */
13340 return (pt <= start || pt >= end);
13341 }
13342
13343 /* Check a composition at the current point. */
13344 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13345 && find_composition (pt, -1, &start, &end, &prop, buffer)
13346 && composition_valid_p (start, end, prop)
13347 && start < pt && end > pt);
13348 }
13349
13350 /* Reconsider the clip changes of buffer which is displayed in W. */
13351
13352 static void
13353 reconsider_clip_changes (struct window *w)
13354 {
13355 struct buffer *b = XBUFFER (w->contents);
13356
13357 if (b->clip_changed
13358 && w->window_end_valid
13359 && w->current_matrix->buffer == b
13360 && w->current_matrix->zv == BUF_ZV (b)
13361 && w->current_matrix->begv == BUF_BEGV (b))
13362 b->clip_changed = false;
13363
13364 /* If display wasn't paused, and W is not a tool bar window, see if
13365 point has been moved into or out of a composition. In that case,
13366 set b->clip_changed to force updating the screen. If
13367 b->clip_changed has already been set, skip this check. */
13368 if (!b->clip_changed && w->window_end_valid)
13369 {
13370 ptrdiff_t pt = (w == XWINDOW (selected_window)
13371 ? PT : marker_position (w->pointm));
13372
13373 if ((w->current_matrix->buffer != b || pt != w->last_point)
13374 && check_point_in_composition (w->current_matrix->buffer,
13375 w->last_point, b, pt))
13376 b->clip_changed = true;
13377 }
13378 }
13379
13380 static void
13381 propagate_buffer_redisplay (void)
13382 { /* Resetting b->text->redisplay is problematic!
13383 We can't just reset it in the case that some window that displays
13384 it has not been redisplayed; and such a window can stay
13385 unredisplayed for a long time if it's currently invisible.
13386 But we do want to reset it at the end of redisplay otherwise
13387 its displayed windows will keep being redisplayed over and over
13388 again.
13389 So we copy all b->text->redisplay flags up to their windows here,
13390 such that mark_window_display_accurate can safely reset
13391 b->text->redisplay. */
13392 Lisp_Object ws = window_list ();
13393 for (; CONSP (ws); ws = XCDR (ws))
13394 {
13395 struct window *thisw = XWINDOW (XCAR (ws));
13396 struct buffer *thisb = XBUFFER (thisw->contents);
13397 if (thisb->text->redisplay)
13398 thisw->redisplay = true;
13399 }
13400 }
13401
13402 #define STOP_POLLING \
13403 do { if (! polling_stopped_here) stop_polling (); \
13404 polling_stopped_here = true; } while (false)
13405
13406 #define RESUME_POLLING \
13407 do { if (polling_stopped_here) start_polling (); \
13408 polling_stopped_here = false; } while (false)
13409
13410
13411 /* Perhaps in the future avoid recentering windows if it
13412 is not necessary; currently that causes some problems. */
13413
13414 static void
13415 redisplay_internal (void)
13416 {
13417 struct window *w = XWINDOW (selected_window);
13418 struct window *sw;
13419 struct frame *fr;
13420 bool pending;
13421 bool must_finish = false, match_p;
13422 struct text_pos tlbufpos, tlendpos;
13423 int number_of_visible_frames;
13424 ptrdiff_t count;
13425 struct frame *sf;
13426 bool polling_stopped_here = false;
13427 Lisp_Object tail, frame;
13428
13429 /* True means redisplay has to consider all windows on all
13430 frames. False, only selected_window is considered. */
13431 bool consider_all_windows_p;
13432
13433 /* True means redisplay has to redisplay the miniwindow. */
13434 bool update_miniwindow_p = false;
13435
13436 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13437
13438 /* No redisplay if running in batch mode or frame is not yet fully
13439 initialized, or redisplay is explicitly turned off by setting
13440 Vinhibit_redisplay. */
13441 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13442 || !NILP (Vinhibit_redisplay))
13443 return;
13444
13445 /* Don't examine these until after testing Vinhibit_redisplay.
13446 When Emacs is shutting down, perhaps because its connection to
13447 X has dropped, we should not look at them at all. */
13448 fr = XFRAME (w->frame);
13449 sf = SELECTED_FRAME ();
13450
13451 if (!fr->glyphs_initialized_p)
13452 return;
13453
13454 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13455 if (popup_activated ())
13456 return;
13457 #endif
13458
13459 /* I don't think this happens but let's be paranoid. */
13460 if (redisplaying_p)
13461 return;
13462
13463 /* Record a function that clears redisplaying_p
13464 when we leave this function. */
13465 count = SPECPDL_INDEX ();
13466 record_unwind_protect_void (unwind_redisplay);
13467 redisplaying_p = true;
13468 specbind (Qinhibit_free_realized_faces, Qnil);
13469
13470 /* Record this function, so it appears on the profiler's backtraces. */
13471 record_in_backtrace (Qredisplay_internal, 0, 0);
13472
13473 FOR_EACH_FRAME (tail, frame)
13474 XFRAME (frame)->already_hscrolled_p = false;
13475
13476 retry:
13477 /* Remember the currently selected window. */
13478 sw = w;
13479
13480 pending = false;
13481 forget_escape_and_glyphless_faces ();
13482
13483 inhibit_free_realized_faces = false;
13484
13485 /* If face_change, init_iterator will free all realized faces, which
13486 includes the faces referenced from current matrices. So, we
13487 can't reuse current matrices in this case. */
13488 if (face_change)
13489 windows_or_buffers_changed = 47;
13490
13491 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13492 && FRAME_TTY (sf)->previous_frame != sf)
13493 {
13494 /* Since frames on a single ASCII terminal share the same
13495 display area, displaying a different frame means redisplay
13496 the whole thing. */
13497 SET_FRAME_GARBAGED (sf);
13498 #ifndef DOS_NT
13499 set_tty_color_mode (FRAME_TTY (sf), sf);
13500 #endif
13501 FRAME_TTY (sf)->previous_frame = sf;
13502 }
13503
13504 /* Set the visible flags for all frames. Do this before checking for
13505 resized or garbaged frames; they want to know if their frames are
13506 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13507 number_of_visible_frames = 0;
13508
13509 FOR_EACH_FRAME (tail, frame)
13510 {
13511 struct frame *f = XFRAME (frame);
13512
13513 if (FRAME_VISIBLE_P (f))
13514 {
13515 ++number_of_visible_frames;
13516 /* Adjust matrices for visible frames only. */
13517 if (f->fonts_changed)
13518 {
13519 adjust_frame_glyphs (f);
13520 /* Disable all redisplay optimizations for this frame.
13521 This is because adjust_frame_glyphs resets the
13522 enabled_p flag for all glyph rows of all windows, so
13523 many optimizations will fail anyway, and some might
13524 fail to test that flag and do bogus things as
13525 result. */
13526 SET_FRAME_GARBAGED (f);
13527 f->fonts_changed = false;
13528 }
13529 /* If cursor type has been changed on the frame
13530 other than selected, consider all frames. */
13531 if (f != sf && f->cursor_type_changed)
13532 fset_redisplay (f);
13533 }
13534 clear_desired_matrices (f);
13535 }
13536
13537 /* Notice any pending interrupt request to change frame size. */
13538 do_pending_window_change (true);
13539
13540 /* do_pending_window_change could change the selected_window due to
13541 frame resizing which makes the selected window too small. */
13542 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13543 sw = w;
13544
13545 /* Clear frames marked as garbaged. */
13546 clear_garbaged_frames ();
13547
13548 /* Build menubar and tool-bar items. */
13549 if (NILP (Vmemory_full))
13550 prepare_menu_bars ();
13551
13552 reconsider_clip_changes (w);
13553
13554 /* In most cases selected window displays current buffer. */
13555 match_p = XBUFFER (w->contents) == current_buffer;
13556 if (match_p)
13557 {
13558 /* Detect case that we need to write or remove a star in the mode line. */
13559 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13560 w->update_mode_line = true;
13561
13562 if (mode_line_update_needed (w))
13563 w->update_mode_line = true;
13564
13565 /* If reconsider_clip_changes above decided that the narrowing
13566 in the current buffer changed, make sure all other windows
13567 showing that buffer will be redisplayed. */
13568 if (current_buffer->clip_changed)
13569 bset_update_mode_line (current_buffer);
13570 }
13571
13572 /* Normally the message* functions will have already displayed and
13573 updated the echo area, but the frame may have been trashed, or
13574 the update may have been preempted, so display the echo area
13575 again here. Checking message_cleared_p captures the case that
13576 the echo area should be cleared. */
13577 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13578 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13579 || (message_cleared_p
13580 && minibuf_level == 0
13581 /* If the mini-window is currently selected, this means the
13582 echo-area doesn't show through. */
13583 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13584 {
13585 echo_area_display (false);
13586
13587 /* If echo_area_display resizes the mini-window, the redisplay and
13588 window_sizes_changed flags of the selected frame are set, but
13589 it's too late for the hooks in window-size-change-functions,
13590 which have been examined already in prepare_menu_bars. So in
13591 that case we call the hooks here only for the selected frame. */
13592 if (sf->redisplay && FRAME_WINDOW_SIZES_CHANGED (sf))
13593 {
13594 Lisp_Object functions;
13595 ptrdiff_t count1 = SPECPDL_INDEX ();
13596
13597 record_unwind_save_match_data ();
13598
13599 /* Clear flag first in case we get an error below. */
13600 FRAME_WINDOW_SIZES_CHANGED (sf) = false;
13601 functions = Vwindow_size_change_functions;
13602
13603 while (CONSP (functions))
13604 {
13605 if (!EQ (XCAR (functions), Qt))
13606 call1 (XCAR (functions), selected_frame);
13607 functions = XCDR (functions);
13608 }
13609
13610 unbind_to (count1, Qnil);
13611 }
13612
13613 if (message_cleared_p)
13614 update_miniwindow_p = true;
13615
13616 must_finish = true;
13617
13618 /* If we don't display the current message, don't clear the
13619 message_cleared_p flag, because, if we did, we wouldn't clear
13620 the echo area in the next redisplay which doesn't preserve
13621 the echo area. */
13622 if (!display_last_displayed_message_p)
13623 message_cleared_p = false;
13624 }
13625 else if (EQ (selected_window, minibuf_window)
13626 && (current_buffer->clip_changed || window_outdated (w))
13627 && resize_mini_window (w, false))
13628 {
13629 if (sf->redisplay)
13630 {
13631 Lisp_Object functions;
13632 ptrdiff_t count1 = SPECPDL_INDEX ();
13633
13634 record_unwind_save_match_data ();
13635
13636 /* Clear flag first in case we get an error below. */
13637 FRAME_WINDOW_SIZES_CHANGED (sf) = false;
13638 functions = Vwindow_size_change_functions;
13639
13640 while (CONSP (functions))
13641 {
13642 if (!EQ (XCAR (functions), Qt))
13643 call1 (XCAR (functions), selected_frame);
13644 functions = XCDR (functions);
13645 }
13646
13647 unbind_to (count1, Qnil);
13648 }
13649
13650 /* Resized active mini-window to fit the size of what it is
13651 showing if its contents might have changed. */
13652 must_finish = true;
13653
13654 /* If window configuration was changed, frames may have been
13655 marked garbaged. Clear them or we will experience
13656 surprises wrt scrolling. */
13657 clear_garbaged_frames ();
13658 }
13659
13660 if (windows_or_buffers_changed && !update_mode_lines)
13661 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13662 only the windows's contents needs to be refreshed, or whether the
13663 mode-lines also need a refresh. */
13664 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13665 ? REDISPLAY_SOME : 32);
13666
13667 /* If specs for an arrow have changed, do thorough redisplay
13668 to ensure we remove any arrow that should no longer exist. */
13669 if (overlay_arrows_changed_p ())
13670 /* Apparently, this is the only case where we update other windows,
13671 without updating other mode-lines. */
13672 windows_or_buffers_changed = 49;
13673
13674 consider_all_windows_p = (update_mode_lines
13675 || windows_or_buffers_changed);
13676
13677 #define AINC(a,i) \
13678 { \
13679 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13680 if (INTEGERP (entry)) \
13681 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13682 }
13683
13684 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13685 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13686
13687 /* Optimize the case that only the line containing the cursor in the
13688 selected window has changed. Variables starting with this_ are
13689 set in display_line and record information about the line
13690 containing the cursor. */
13691 tlbufpos = this_line_start_pos;
13692 tlendpos = this_line_end_pos;
13693 if (!consider_all_windows_p
13694 && CHARPOS (tlbufpos) > 0
13695 && !w->update_mode_line
13696 && !current_buffer->clip_changed
13697 && !current_buffer->prevent_redisplay_optimizations_p
13698 && FRAME_VISIBLE_P (XFRAME (w->frame))
13699 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13700 && !XFRAME (w->frame)->cursor_type_changed
13701 && !XFRAME (w->frame)->face_change
13702 /* Make sure recorded data applies to current buffer, etc. */
13703 && this_line_buffer == current_buffer
13704 && match_p
13705 && !w->force_start
13706 && !w->optional_new_start
13707 /* Point must be on the line that we have info recorded about. */
13708 && PT >= CHARPOS (tlbufpos)
13709 && PT <= Z - CHARPOS (tlendpos)
13710 /* All text outside that line, including its final newline,
13711 must be unchanged. */
13712 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13713 CHARPOS (tlendpos)))
13714 {
13715 if (CHARPOS (tlbufpos) > BEGV
13716 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13717 && (CHARPOS (tlbufpos) == ZV
13718 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13719 /* Former continuation line has disappeared by becoming empty. */
13720 goto cancel;
13721 else if (window_outdated (w) || MINI_WINDOW_P (w))
13722 {
13723 /* We have to handle the case of continuation around a
13724 wide-column character (see the comment in indent.c around
13725 line 1340).
13726
13727 For instance, in the following case:
13728
13729 -------- Insert --------
13730 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13731 J_I_ ==> J_I_ `^^' are cursors.
13732 ^^ ^^
13733 -------- --------
13734
13735 As we have to redraw the line above, we cannot use this
13736 optimization. */
13737
13738 struct it it;
13739 int line_height_before = this_line_pixel_height;
13740
13741 /* Note that start_display will handle the case that the
13742 line starting at tlbufpos is a continuation line. */
13743 start_display (&it, w, tlbufpos);
13744
13745 /* Implementation note: It this still necessary? */
13746 if (it.current_x != this_line_start_x)
13747 goto cancel;
13748
13749 TRACE ((stderr, "trying display optimization 1\n"));
13750 w->cursor.vpos = -1;
13751 overlay_arrow_seen = false;
13752 it.vpos = this_line_vpos;
13753 it.current_y = this_line_y;
13754 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13755 display_line (&it);
13756
13757 /* If line contains point, is not continued,
13758 and ends at same distance from eob as before, we win. */
13759 if (w->cursor.vpos >= 0
13760 /* Line is not continued, otherwise this_line_start_pos
13761 would have been set to 0 in display_line. */
13762 && CHARPOS (this_line_start_pos)
13763 /* Line ends as before. */
13764 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13765 /* Line has same height as before. Otherwise other lines
13766 would have to be shifted up or down. */
13767 && this_line_pixel_height == line_height_before)
13768 {
13769 /* If this is not the window's last line, we must adjust
13770 the charstarts of the lines below. */
13771 if (it.current_y < it.last_visible_y)
13772 {
13773 struct glyph_row *row
13774 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13775 ptrdiff_t delta, delta_bytes;
13776
13777 /* We used to distinguish between two cases here,
13778 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13779 when the line ends in a newline or the end of the
13780 buffer's accessible portion. But both cases did
13781 the same, so they were collapsed. */
13782 delta = (Z
13783 - CHARPOS (tlendpos)
13784 - MATRIX_ROW_START_CHARPOS (row));
13785 delta_bytes = (Z_BYTE
13786 - BYTEPOS (tlendpos)
13787 - MATRIX_ROW_START_BYTEPOS (row));
13788
13789 increment_matrix_positions (w->current_matrix,
13790 this_line_vpos + 1,
13791 w->current_matrix->nrows,
13792 delta, delta_bytes);
13793 }
13794
13795 /* If this row displays text now but previously didn't,
13796 or vice versa, w->window_end_vpos may have to be
13797 adjusted. */
13798 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13799 {
13800 if (w->window_end_vpos < this_line_vpos)
13801 w->window_end_vpos = this_line_vpos;
13802 }
13803 else if (w->window_end_vpos == this_line_vpos
13804 && this_line_vpos > 0)
13805 w->window_end_vpos = this_line_vpos - 1;
13806 w->window_end_valid = false;
13807
13808 /* Update hint: No need to try to scroll in update_window. */
13809 w->desired_matrix->no_scrolling_p = true;
13810
13811 #ifdef GLYPH_DEBUG
13812 *w->desired_matrix->method = 0;
13813 debug_method_add (w, "optimization 1");
13814 #endif
13815 #ifdef HAVE_WINDOW_SYSTEM
13816 update_window_fringes (w, false);
13817 #endif
13818 goto update;
13819 }
13820 else
13821 goto cancel;
13822 }
13823 else if (/* Cursor position hasn't changed. */
13824 PT == w->last_point
13825 /* Make sure the cursor was last displayed
13826 in this window. Otherwise we have to reposition it. */
13827
13828 /* PXW: Must be converted to pixels, probably. */
13829 && 0 <= w->cursor.vpos
13830 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13831 {
13832 if (!must_finish)
13833 {
13834 do_pending_window_change (true);
13835 /* If selected_window changed, redisplay again. */
13836 if (WINDOWP (selected_window)
13837 && (w = XWINDOW (selected_window)) != sw)
13838 goto retry;
13839
13840 /* We used to always goto end_of_redisplay here, but this
13841 isn't enough if we have a blinking cursor. */
13842 if (w->cursor_off_p == w->last_cursor_off_p)
13843 goto end_of_redisplay;
13844 }
13845 goto update;
13846 }
13847 /* If highlighting the region, or if the cursor is in the echo area,
13848 then we can't just move the cursor. */
13849 else if (NILP (Vshow_trailing_whitespace)
13850 && !cursor_in_echo_area)
13851 {
13852 struct it it;
13853 struct glyph_row *row;
13854
13855 /* Skip from tlbufpos to PT and see where it is. Note that
13856 PT may be in invisible text. If so, we will end at the
13857 next visible position. */
13858 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13859 NULL, DEFAULT_FACE_ID);
13860 it.current_x = this_line_start_x;
13861 it.current_y = this_line_y;
13862 it.vpos = this_line_vpos;
13863
13864 /* The call to move_it_to stops in front of PT, but
13865 moves over before-strings. */
13866 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13867
13868 if (it.vpos == this_line_vpos
13869 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13870 row->enabled_p))
13871 {
13872 eassert (this_line_vpos == it.vpos);
13873 eassert (this_line_y == it.current_y);
13874 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13875 #ifdef GLYPH_DEBUG
13876 *w->desired_matrix->method = 0;
13877 debug_method_add (w, "optimization 3");
13878 #endif
13879 goto update;
13880 }
13881 else
13882 goto cancel;
13883 }
13884
13885 cancel:
13886 /* Text changed drastically or point moved off of line. */
13887 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13888 }
13889
13890 CHARPOS (this_line_start_pos) = 0;
13891 ++clear_face_cache_count;
13892 #ifdef HAVE_WINDOW_SYSTEM
13893 ++clear_image_cache_count;
13894 #endif
13895
13896 /* Build desired matrices, and update the display. If
13897 consider_all_windows_p, do it for all windows on all frames that
13898 require redisplay, as specified by their 'redisplay' flag.
13899 Otherwise do it for selected_window, only. */
13900
13901 if (consider_all_windows_p)
13902 {
13903 FOR_EACH_FRAME (tail, frame)
13904 XFRAME (frame)->updated_p = false;
13905
13906 propagate_buffer_redisplay ();
13907
13908 FOR_EACH_FRAME (tail, frame)
13909 {
13910 struct frame *f = XFRAME (frame);
13911
13912 /* We don't have to do anything for unselected terminal
13913 frames. */
13914 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13915 && !EQ (FRAME_TTY (f)->top_frame, frame))
13916 continue;
13917
13918 retry_frame:
13919 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13920 {
13921 bool gcscrollbars
13922 /* Only GC scrollbars when we redisplay the whole frame. */
13923 = f->redisplay || !REDISPLAY_SOME_P ();
13924 bool f_redisplay_flag = f->redisplay;
13925 /* Mark all the scroll bars to be removed; we'll redeem
13926 the ones we want when we redisplay their windows. */
13927 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13928 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13929
13930 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13931 redisplay_windows (FRAME_ROOT_WINDOW (f));
13932 /* Remember that the invisible frames need to be redisplayed next
13933 time they're visible. */
13934 else if (!REDISPLAY_SOME_P ())
13935 f->redisplay = true;
13936
13937 /* The X error handler may have deleted that frame. */
13938 if (!FRAME_LIVE_P (f))
13939 continue;
13940
13941 /* Any scroll bars which redisplay_windows should have
13942 nuked should now go away. */
13943 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13944 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13945
13946 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13947 {
13948 /* If fonts changed on visible frame, display again. */
13949 if (f->fonts_changed)
13950 {
13951 adjust_frame_glyphs (f);
13952 /* Disable all redisplay optimizations for this
13953 frame. For the reasons, see the comment near
13954 the previous call to adjust_frame_glyphs above. */
13955 SET_FRAME_GARBAGED (f);
13956 f->fonts_changed = false;
13957 goto retry_frame;
13958 }
13959
13960 /* See if we have to hscroll. */
13961 if (!f->already_hscrolled_p)
13962 {
13963 f->already_hscrolled_p = true;
13964 if (hscroll_windows (f->root_window))
13965 goto retry_frame;
13966 }
13967
13968 /* If the frame's redisplay flag was not set before
13969 we went about redisplaying its windows, but it is
13970 set now, that means we employed some redisplay
13971 optimizations inside redisplay_windows, and
13972 bypassed producing some screen lines. But if
13973 f->redisplay is now set, it might mean the old
13974 faces are no longer valid (e.g., if redisplaying
13975 some window called some Lisp which defined a new
13976 face or redefined an existing face), so trying to
13977 use them in update_frame will segfault.
13978 Therefore, we must redisplay this frame. */
13979 if (!f_redisplay_flag && f->redisplay)
13980 goto retry_frame;
13981
13982 /* Prevent various kinds of signals during display
13983 update. stdio is not robust about handling
13984 signals, which can cause an apparent I/O error. */
13985 if (interrupt_input)
13986 unrequest_sigio ();
13987 STOP_POLLING;
13988
13989 pending |= update_frame (f, false, false);
13990 f->cursor_type_changed = false;
13991 f->updated_p = true;
13992 }
13993 }
13994 }
13995
13996 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13997
13998 if (!pending)
13999 {
14000 /* Do the mark_window_display_accurate after all windows have
14001 been redisplayed because this call resets flags in buffers
14002 which are needed for proper redisplay. */
14003 FOR_EACH_FRAME (tail, frame)
14004 {
14005 struct frame *f = XFRAME (frame);
14006 if (f->updated_p)
14007 {
14008 f->redisplay = false;
14009 mark_window_display_accurate (f->root_window, true);
14010 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14011 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14012 }
14013 }
14014 }
14015 }
14016 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14017 {
14018 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14019 struct frame *mini_frame;
14020
14021 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14022 /* Use list_of_error, not Qerror, so that
14023 we catch only errors and don't run the debugger. */
14024 internal_condition_case_1 (redisplay_window_1, selected_window,
14025 list_of_error,
14026 redisplay_window_error);
14027 if (update_miniwindow_p)
14028 internal_condition_case_1 (redisplay_window_1, mini_window,
14029 list_of_error,
14030 redisplay_window_error);
14031
14032 /* Compare desired and current matrices, perform output. */
14033
14034 update:
14035 /* If fonts changed, display again. Likewise if redisplay_window_1
14036 above caused some change (e.g., a change in faces) that requires
14037 considering the entire frame again. */
14038 if (sf->fonts_changed || sf->redisplay)
14039 {
14040 if (sf->redisplay)
14041 {
14042 /* Set this to force a more thorough redisplay.
14043 Otherwise, we might immediately loop back to the
14044 above "else-if" clause (since all the conditions that
14045 led here might still be true), and we will then
14046 infloop, because the selected-frame's redisplay flag
14047 is not (and cannot be) reset. */
14048 windows_or_buffers_changed = 50;
14049 }
14050 goto retry;
14051 }
14052
14053 /* Prevent freeing of realized faces, since desired matrices are
14054 pending that reference the faces we computed and cached. */
14055 inhibit_free_realized_faces = true;
14056
14057 /* Prevent various kinds of signals during display update.
14058 stdio is not robust about handling signals,
14059 which can cause an apparent I/O error. */
14060 if (interrupt_input)
14061 unrequest_sigio ();
14062 STOP_POLLING;
14063
14064 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14065 {
14066 if (hscroll_windows (selected_window))
14067 goto retry;
14068
14069 XWINDOW (selected_window)->must_be_updated_p = true;
14070 pending = update_frame (sf, false, false);
14071 sf->cursor_type_changed = false;
14072 }
14073
14074 /* We may have called echo_area_display at the top of this
14075 function. If the echo area is on another frame, that may
14076 have put text on a frame other than the selected one, so the
14077 above call to update_frame would not have caught it. Catch
14078 it here. */
14079 mini_window = FRAME_MINIBUF_WINDOW (sf);
14080 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14081
14082 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14083 {
14084 XWINDOW (mini_window)->must_be_updated_p = true;
14085 pending |= update_frame (mini_frame, false, false);
14086 mini_frame->cursor_type_changed = false;
14087 if (!pending && hscroll_windows (mini_window))
14088 goto retry;
14089 }
14090 }
14091
14092 /* If display was paused because of pending input, make sure we do a
14093 thorough update the next time. */
14094 if (pending)
14095 {
14096 /* Prevent the optimization at the beginning of
14097 redisplay_internal that tries a single-line update of the
14098 line containing the cursor in the selected window. */
14099 CHARPOS (this_line_start_pos) = 0;
14100
14101 /* Let the overlay arrow be updated the next time. */
14102 update_overlay_arrows (0);
14103
14104 /* If we pause after scrolling, some rows in the current
14105 matrices of some windows are not valid. */
14106 if (!WINDOW_FULL_WIDTH_P (w)
14107 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14108 update_mode_lines = 36;
14109 }
14110 else
14111 {
14112 if (!consider_all_windows_p)
14113 {
14114 /* This has already been done above if
14115 consider_all_windows_p is set. */
14116 if (XBUFFER (w->contents)->text->redisplay
14117 && buffer_window_count (XBUFFER (w->contents)) > 1)
14118 /* This can happen if b->text->redisplay was set during
14119 jit-lock. */
14120 propagate_buffer_redisplay ();
14121 mark_window_display_accurate_1 (w, true);
14122
14123 /* Say overlay arrows are up to date. */
14124 update_overlay_arrows (1);
14125
14126 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14127 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14128 }
14129
14130 update_mode_lines = 0;
14131 windows_or_buffers_changed = 0;
14132 }
14133
14134 /* Start SIGIO interrupts coming again. Having them off during the
14135 code above makes it less likely one will discard output, but not
14136 impossible, since there might be stuff in the system buffer here.
14137 But it is much hairier to try to do anything about that. */
14138 if (interrupt_input)
14139 request_sigio ();
14140 RESUME_POLLING;
14141
14142 /* If a frame has become visible which was not before, redisplay
14143 again, so that we display it. Expose events for such a frame
14144 (which it gets when becoming visible) don't call the parts of
14145 redisplay constructing glyphs, so simply exposing a frame won't
14146 display anything in this case. So, we have to display these
14147 frames here explicitly. */
14148 if (!pending)
14149 {
14150 int new_count = 0;
14151
14152 FOR_EACH_FRAME (tail, frame)
14153 {
14154 if (XFRAME (frame)->visible)
14155 new_count++;
14156 }
14157
14158 if (new_count != number_of_visible_frames)
14159 windows_or_buffers_changed = 52;
14160 }
14161
14162 /* Change frame size now if a change is pending. */
14163 do_pending_window_change (true);
14164
14165 /* If we just did a pending size change, or have additional
14166 visible frames, or selected_window changed, redisplay again. */
14167 if ((windows_or_buffers_changed && !pending)
14168 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14169 goto retry;
14170
14171 /* Clear the face and image caches.
14172
14173 We used to do this only if consider_all_windows_p. But the cache
14174 needs to be cleared if a timer creates images in the current
14175 buffer (e.g. the test case in Bug#6230). */
14176
14177 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14178 {
14179 clear_face_cache (false);
14180 clear_face_cache_count = 0;
14181 }
14182
14183 #ifdef HAVE_WINDOW_SYSTEM
14184 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14185 {
14186 clear_image_caches (Qnil);
14187 clear_image_cache_count = 0;
14188 }
14189 #endif /* HAVE_WINDOW_SYSTEM */
14190
14191 end_of_redisplay:
14192 #ifdef HAVE_NS
14193 ns_set_doc_edited ();
14194 #endif
14195 if (interrupt_input && interrupts_deferred)
14196 request_sigio ();
14197
14198 unbind_to (count, Qnil);
14199 RESUME_POLLING;
14200 }
14201
14202
14203 /* Redisplay, but leave alone any recent echo area message unless
14204 another message has been requested in its place.
14205
14206 This is useful in situations where you need to redisplay but no
14207 user action has occurred, making it inappropriate for the message
14208 area to be cleared. See tracking_off and
14209 wait_reading_process_output for examples of these situations.
14210
14211 FROM_WHERE is an integer saying from where this function was
14212 called. This is useful for debugging. */
14213
14214 void
14215 redisplay_preserve_echo_area (int from_where)
14216 {
14217 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14218
14219 if (!NILP (echo_area_buffer[1]))
14220 {
14221 /* We have a previously displayed message, but no current
14222 message. Redisplay the previous message. */
14223 display_last_displayed_message_p = true;
14224 redisplay_internal ();
14225 display_last_displayed_message_p = false;
14226 }
14227 else
14228 redisplay_internal ();
14229
14230 flush_frame (SELECTED_FRAME ());
14231 }
14232
14233
14234 /* Function registered with record_unwind_protect in redisplay_internal. */
14235
14236 static void
14237 unwind_redisplay (void)
14238 {
14239 redisplaying_p = false;
14240 }
14241
14242
14243 /* Mark the display of leaf window W as accurate or inaccurate.
14244 If ACCURATE_P, mark display of W as accurate.
14245 If !ACCURATE_P, arrange for W to be redisplayed the next
14246 time redisplay_internal is called. */
14247
14248 static void
14249 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14250 {
14251 struct buffer *b = XBUFFER (w->contents);
14252
14253 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14254 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14255 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14256
14257 if (accurate_p)
14258 {
14259 b->clip_changed = false;
14260 b->prevent_redisplay_optimizations_p = false;
14261 eassert (buffer_window_count (b) > 0);
14262 /* Resetting b->text->redisplay is problematic!
14263 In order to make it safer to do it here, redisplay_internal must
14264 have copied all b->text->redisplay to their respective windows. */
14265 b->text->redisplay = false;
14266
14267 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14268 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14269 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14270 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14271
14272 w->current_matrix->buffer = b;
14273 w->current_matrix->begv = BUF_BEGV (b);
14274 w->current_matrix->zv = BUF_ZV (b);
14275
14276 w->last_cursor_vpos = w->cursor.vpos;
14277 w->last_cursor_off_p = w->cursor_off_p;
14278
14279 if (w == XWINDOW (selected_window))
14280 w->last_point = BUF_PT (b);
14281 else
14282 w->last_point = marker_position (w->pointm);
14283
14284 w->window_end_valid = true;
14285 w->update_mode_line = false;
14286 }
14287
14288 w->redisplay = !accurate_p;
14289 }
14290
14291
14292 /* Mark the display of windows in the window tree rooted at WINDOW as
14293 accurate or inaccurate. If ACCURATE_P, mark display of
14294 windows as accurate. If !ACCURATE_P, arrange for windows to
14295 be redisplayed the next time redisplay_internal is called. */
14296
14297 void
14298 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14299 {
14300 struct window *w;
14301
14302 for (; !NILP (window); window = w->next)
14303 {
14304 w = XWINDOW (window);
14305 if (WINDOWP (w->contents))
14306 mark_window_display_accurate (w->contents, accurate_p);
14307 else
14308 mark_window_display_accurate_1 (w, accurate_p);
14309 }
14310
14311 if (accurate_p)
14312 update_overlay_arrows (1);
14313 else
14314 /* Force a thorough redisplay the next time by setting
14315 last_arrow_position and last_arrow_string to t, which is
14316 unequal to any useful value of Voverlay_arrow_... */
14317 update_overlay_arrows (-1);
14318 }
14319
14320
14321 /* Return value in display table DP (Lisp_Char_Table *) for character
14322 C. Since a display table doesn't have any parent, we don't have to
14323 follow parent. Do not call this function directly but use the
14324 macro DISP_CHAR_VECTOR. */
14325
14326 Lisp_Object
14327 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14328 {
14329 Lisp_Object val;
14330
14331 if (ASCII_CHAR_P (c))
14332 {
14333 val = dp->ascii;
14334 if (SUB_CHAR_TABLE_P (val))
14335 val = XSUB_CHAR_TABLE (val)->contents[c];
14336 }
14337 else
14338 {
14339 Lisp_Object table;
14340
14341 XSETCHAR_TABLE (table, dp);
14342 val = char_table_ref (table, c);
14343 }
14344 if (NILP (val))
14345 val = dp->defalt;
14346 return val;
14347 }
14348
14349
14350 \f
14351 /***********************************************************************
14352 Window Redisplay
14353 ***********************************************************************/
14354
14355 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14356
14357 static void
14358 redisplay_windows (Lisp_Object window)
14359 {
14360 while (!NILP (window))
14361 {
14362 struct window *w = XWINDOW (window);
14363
14364 if (WINDOWP (w->contents))
14365 redisplay_windows (w->contents);
14366 else if (BUFFERP (w->contents))
14367 {
14368 displayed_buffer = XBUFFER (w->contents);
14369 /* Use list_of_error, not Qerror, so that
14370 we catch only errors and don't run the debugger. */
14371 internal_condition_case_1 (redisplay_window_0, window,
14372 list_of_error,
14373 redisplay_window_error);
14374 }
14375
14376 window = w->next;
14377 }
14378 }
14379
14380 static Lisp_Object
14381 redisplay_window_error (Lisp_Object ignore)
14382 {
14383 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14384 return Qnil;
14385 }
14386
14387 static Lisp_Object
14388 redisplay_window_0 (Lisp_Object window)
14389 {
14390 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14391 redisplay_window (window, false);
14392 return Qnil;
14393 }
14394
14395 static Lisp_Object
14396 redisplay_window_1 (Lisp_Object window)
14397 {
14398 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14399 redisplay_window (window, true);
14400 return Qnil;
14401 }
14402 \f
14403
14404 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14405 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14406 which positions recorded in ROW differ from current buffer
14407 positions.
14408
14409 Return true iff cursor is on this row. */
14410
14411 static bool
14412 set_cursor_from_row (struct window *w, struct glyph_row *row,
14413 struct glyph_matrix *matrix,
14414 ptrdiff_t delta, ptrdiff_t delta_bytes,
14415 int dy, int dvpos)
14416 {
14417 struct glyph *glyph = row->glyphs[TEXT_AREA];
14418 struct glyph *end = glyph + row->used[TEXT_AREA];
14419 struct glyph *cursor = NULL;
14420 /* The last known character position in row. */
14421 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14422 int x = row->x;
14423 ptrdiff_t pt_old = PT - delta;
14424 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14425 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14426 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14427 /* A glyph beyond the edge of TEXT_AREA which we should never
14428 touch. */
14429 struct glyph *glyphs_end = end;
14430 /* True means we've found a match for cursor position, but that
14431 glyph has the avoid_cursor_p flag set. */
14432 bool match_with_avoid_cursor = false;
14433 /* True means we've seen at least one glyph that came from a
14434 display string. */
14435 bool string_seen = false;
14436 /* Largest and smallest buffer positions seen so far during scan of
14437 glyph row. */
14438 ptrdiff_t bpos_max = pos_before;
14439 ptrdiff_t bpos_min = pos_after;
14440 /* Last buffer position covered by an overlay string with an integer
14441 `cursor' property. */
14442 ptrdiff_t bpos_covered = 0;
14443 /* True means the display string on which to display the cursor
14444 comes from a text property, not from an overlay. */
14445 bool string_from_text_prop = false;
14446
14447 /* Don't even try doing anything if called for a mode-line or
14448 header-line row, since the rest of the code isn't prepared to
14449 deal with such calamities. */
14450 eassert (!row->mode_line_p);
14451 if (row->mode_line_p)
14452 return false;
14453
14454 /* Skip over glyphs not having an object at the start and the end of
14455 the row. These are special glyphs like truncation marks on
14456 terminal frames. */
14457 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14458 {
14459 if (!row->reversed_p)
14460 {
14461 while (glyph < end
14462 && NILP (glyph->object)
14463 && glyph->charpos < 0)
14464 {
14465 x += glyph->pixel_width;
14466 ++glyph;
14467 }
14468 while (end > glyph
14469 && NILP ((end - 1)->object)
14470 /* CHARPOS is zero for blanks and stretch glyphs
14471 inserted by extend_face_to_end_of_line. */
14472 && (end - 1)->charpos <= 0)
14473 --end;
14474 glyph_before = glyph - 1;
14475 glyph_after = end;
14476 }
14477 else
14478 {
14479 struct glyph *g;
14480
14481 /* If the glyph row is reversed, we need to process it from back
14482 to front, so swap the edge pointers. */
14483 glyphs_end = end = glyph - 1;
14484 glyph += row->used[TEXT_AREA] - 1;
14485
14486 while (glyph > end + 1
14487 && NILP (glyph->object)
14488 && glyph->charpos < 0)
14489 {
14490 --glyph;
14491 x -= glyph->pixel_width;
14492 }
14493 if (NILP (glyph->object) && glyph->charpos < 0)
14494 --glyph;
14495 /* By default, in reversed rows we put the cursor on the
14496 rightmost (first in the reading order) glyph. */
14497 for (g = end + 1; g < glyph; g++)
14498 x += g->pixel_width;
14499 while (end < glyph
14500 && NILP ((end + 1)->object)
14501 && (end + 1)->charpos <= 0)
14502 ++end;
14503 glyph_before = glyph + 1;
14504 glyph_after = end;
14505 }
14506 }
14507 else if (row->reversed_p)
14508 {
14509 /* In R2L rows that don't display text, put the cursor on the
14510 rightmost glyph. Case in point: an empty last line that is
14511 part of an R2L paragraph. */
14512 cursor = end - 1;
14513 /* Avoid placing the cursor on the last glyph of the row, where
14514 on terminal frames we hold the vertical border between
14515 adjacent windows. */
14516 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14517 && !WINDOW_RIGHTMOST_P (w)
14518 && cursor == row->glyphs[LAST_AREA] - 1)
14519 cursor--;
14520 x = -1; /* will be computed below, at label compute_x */
14521 }
14522
14523 /* Step 1: Try to find the glyph whose character position
14524 corresponds to point. If that's not possible, find 2 glyphs
14525 whose character positions are the closest to point, one before
14526 point, the other after it. */
14527 if (!row->reversed_p)
14528 while (/* not marched to end of glyph row */
14529 glyph < end
14530 /* glyph was not inserted by redisplay for internal purposes */
14531 && !NILP (glyph->object))
14532 {
14533 if (BUFFERP (glyph->object))
14534 {
14535 ptrdiff_t dpos = glyph->charpos - pt_old;
14536
14537 if (glyph->charpos > bpos_max)
14538 bpos_max = glyph->charpos;
14539 if (glyph->charpos < bpos_min)
14540 bpos_min = glyph->charpos;
14541 if (!glyph->avoid_cursor_p)
14542 {
14543 /* If we hit point, we've found the glyph on which to
14544 display the cursor. */
14545 if (dpos == 0)
14546 {
14547 match_with_avoid_cursor = false;
14548 break;
14549 }
14550 /* See if we've found a better approximation to
14551 POS_BEFORE or to POS_AFTER. */
14552 if (0 > dpos && dpos > pos_before - pt_old)
14553 {
14554 pos_before = glyph->charpos;
14555 glyph_before = glyph;
14556 }
14557 else if (0 < dpos && dpos < pos_after - pt_old)
14558 {
14559 pos_after = glyph->charpos;
14560 glyph_after = glyph;
14561 }
14562 }
14563 else if (dpos == 0)
14564 match_with_avoid_cursor = true;
14565 }
14566 else if (STRINGP (glyph->object))
14567 {
14568 Lisp_Object chprop;
14569 ptrdiff_t glyph_pos = glyph->charpos;
14570
14571 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14572 glyph->object);
14573 if (!NILP (chprop))
14574 {
14575 /* If the string came from a `display' text property,
14576 look up the buffer position of that property and
14577 use that position to update bpos_max, as if we
14578 actually saw such a position in one of the row's
14579 glyphs. This helps with supporting integer values
14580 of `cursor' property on the display string in
14581 situations where most or all of the row's buffer
14582 text is completely covered by display properties,
14583 so that no glyph with valid buffer positions is
14584 ever seen in the row. */
14585 ptrdiff_t prop_pos =
14586 string_buffer_position_lim (glyph->object, pos_before,
14587 pos_after, false);
14588
14589 if (prop_pos >= pos_before)
14590 bpos_max = prop_pos;
14591 }
14592 if (INTEGERP (chprop))
14593 {
14594 bpos_covered = bpos_max + XINT (chprop);
14595 /* If the `cursor' property covers buffer positions up
14596 to and including point, we should display cursor on
14597 this glyph. Note that, if a `cursor' property on one
14598 of the string's characters has an integer value, we
14599 will break out of the loop below _before_ we get to
14600 the position match above. IOW, integer values of
14601 the `cursor' property override the "exact match for
14602 point" strategy of positioning the cursor. */
14603 /* Implementation note: bpos_max == pt_old when, e.g.,
14604 we are in an empty line, where bpos_max is set to
14605 MATRIX_ROW_START_CHARPOS, see above. */
14606 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14607 {
14608 cursor = glyph;
14609 break;
14610 }
14611 }
14612
14613 string_seen = true;
14614 }
14615 x += glyph->pixel_width;
14616 ++glyph;
14617 }
14618 else if (glyph > end) /* row is reversed */
14619 while (!NILP (glyph->object))
14620 {
14621 if (BUFFERP (glyph->object))
14622 {
14623 ptrdiff_t dpos = glyph->charpos - pt_old;
14624
14625 if (glyph->charpos > bpos_max)
14626 bpos_max = glyph->charpos;
14627 if (glyph->charpos < bpos_min)
14628 bpos_min = glyph->charpos;
14629 if (!glyph->avoid_cursor_p)
14630 {
14631 if (dpos == 0)
14632 {
14633 match_with_avoid_cursor = false;
14634 break;
14635 }
14636 if (0 > dpos && dpos > pos_before - pt_old)
14637 {
14638 pos_before = glyph->charpos;
14639 glyph_before = glyph;
14640 }
14641 else if (0 < dpos && dpos < pos_after - pt_old)
14642 {
14643 pos_after = glyph->charpos;
14644 glyph_after = glyph;
14645 }
14646 }
14647 else if (dpos == 0)
14648 match_with_avoid_cursor = true;
14649 }
14650 else if (STRINGP (glyph->object))
14651 {
14652 Lisp_Object chprop;
14653 ptrdiff_t glyph_pos = glyph->charpos;
14654
14655 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14656 glyph->object);
14657 if (!NILP (chprop))
14658 {
14659 ptrdiff_t prop_pos =
14660 string_buffer_position_lim (glyph->object, pos_before,
14661 pos_after, false);
14662
14663 if (prop_pos >= pos_before)
14664 bpos_max = prop_pos;
14665 }
14666 if (INTEGERP (chprop))
14667 {
14668 bpos_covered = bpos_max + XINT (chprop);
14669 /* If the `cursor' property covers buffer positions up
14670 to and including point, we should display cursor on
14671 this glyph. */
14672 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14673 {
14674 cursor = glyph;
14675 break;
14676 }
14677 }
14678 string_seen = true;
14679 }
14680 --glyph;
14681 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14682 {
14683 x--; /* can't use any pixel_width */
14684 break;
14685 }
14686 x -= glyph->pixel_width;
14687 }
14688
14689 /* Step 2: If we didn't find an exact match for point, we need to
14690 look for a proper place to put the cursor among glyphs between
14691 GLYPH_BEFORE and GLYPH_AFTER. */
14692 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14693 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14694 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14695 {
14696 /* An empty line has a single glyph whose OBJECT is nil and
14697 whose CHARPOS is the position of a newline on that line.
14698 Note that on a TTY, there are more glyphs after that, which
14699 were produced by extend_face_to_end_of_line, but their
14700 CHARPOS is zero or negative. */
14701 bool empty_line_p =
14702 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14703 && NILP (glyph->object) && glyph->charpos > 0
14704 /* On a TTY, continued and truncated rows also have a glyph at
14705 their end whose OBJECT is nil and whose CHARPOS is
14706 positive (the continuation and truncation glyphs), but such
14707 rows are obviously not "empty". */
14708 && !(row->continued_p || row->truncated_on_right_p));
14709
14710 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14711 {
14712 ptrdiff_t ellipsis_pos;
14713
14714 /* Scan back over the ellipsis glyphs. */
14715 if (!row->reversed_p)
14716 {
14717 ellipsis_pos = (glyph - 1)->charpos;
14718 while (glyph > row->glyphs[TEXT_AREA]
14719 && (glyph - 1)->charpos == ellipsis_pos)
14720 glyph--, x -= glyph->pixel_width;
14721 /* That loop always goes one position too far, including
14722 the glyph before the ellipsis. So scan forward over
14723 that one. */
14724 x += glyph->pixel_width;
14725 glyph++;
14726 }
14727 else /* row is reversed */
14728 {
14729 ellipsis_pos = (glyph + 1)->charpos;
14730 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14731 && (glyph + 1)->charpos == ellipsis_pos)
14732 glyph++, x += glyph->pixel_width;
14733 x -= glyph->pixel_width;
14734 glyph--;
14735 }
14736 }
14737 else if (match_with_avoid_cursor)
14738 {
14739 cursor = glyph_after;
14740 x = -1;
14741 }
14742 else if (string_seen)
14743 {
14744 int incr = row->reversed_p ? -1 : +1;
14745
14746 /* Need to find the glyph that came out of a string which is
14747 present at point. That glyph is somewhere between
14748 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14749 positioned between POS_BEFORE and POS_AFTER in the
14750 buffer. */
14751 struct glyph *start, *stop;
14752 ptrdiff_t pos = pos_before;
14753
14754 x = -1;
14755
14756 /* If the row ends in a newline from a display string,
14757 reordering could have moved the glyphs belonging to the
14758 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14759 in this case we extend the search to the last glyph in
14760 the row that was not inserted by redisplay. */
14761 if (row->ends_in_newline_from_string_p)
14762 {
14763 glyph_after = end;
14764 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14765 }
14766
14767 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14768 correspond to POS_BEFORE and POS_AFTER, respectively. We
14769 need START and STOP in the order that corresponds to the
14770 row's direction as given by its reversed_p flag. If the
14771 directionality of characters between POS_BEFORE and
14772 POS_AFTER is the opposite of the row's base direction,
14773 these characters will have been reordered for display,
14774 and we need to reverse START and STOP. */
14775 if (!row->reversed_p)
14776 {
14777 start = min (glyph_before, glyph_after);
14778 stop = max (glyph_before, glyph_after);
14779 }
14780 else
14781 {
14782 start = max (glyph_before, glyph_after);
14783 stop = min (glyph_before, glyph_after);
14784 }
14785 for (glyph = start + incr;
14786 row->reversed_p ? glyph > stop : glyph < stop; )
14787 {
14788
14789 /* Any glyphs that come from the buffer are here because
14790 of bidi reordering. Skip them, and only pay
14791 attention to glyphs that came from some string. */
14792 if (STRINGP (glyph->object))
14793 {
14794 Lisp_Object str;
14795 ptrdiff_t tem;
14796 /* If the display property covers the newline, we
14797 need to search for it one position farther. */
14798 ptrdiff_t lim = pos_after
14799 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14800
14801 string_from_text_prop = false;
14802 str = glyph->object;
14803 tem = string_buffer_position_lim (str, pos, lim, false);
14804 if (tem == 0 /* from overlay */
14805 || pos <= tem)
14806 {
14807 /* If the string from which this glyph came is
14808 found in the buffer at point, or at position
14809 that is closer to point than pos_after, then
14810 we've found the glyph we've been looking for.
14811 If it comes from an overlay (tem == 0), and
14812 it has the `cursor' property on one of its
14813 glyphs, record that glyph as a candidate for
14814 displaying the cursor. (As in the
14815 unidirectional version, we will display the
14816 cursor on the last candidate we find.) */
14817 if (tem == 0
14818 || tem == pt_old
14819 || (tem - pt_old > 0 && tem < pos_after))
14820 {
14821 /* The glyphs from this string could have
14822 been reordered. Find the one with the
14823 smallest string position. Or there could
14824 be a character in the string with the
14825 `cursor' property, which means display
14826 cursor on that character's glyph. */
14827 ptrdiff_t strpos = glyph->charpos;
14828
14829 if (tem)
14830 {
14831 cursor = glyph;
14832 string_from_text_prop = true;
14833 }
14834 for ( ;
14835 (row->reversed_p ? glyph > stop : glyph < stop)
14836 && EQ (glyph->object, str);
14837 glyph += incr)
14838 {
14839 Lisp_Object cprop;
14840 ptrdiff_t gpos = glyph->charpos;
14841
14842 cprop = Fget_char_property (make_number (gpos),
14843 Qcursor,
14844 glyph->object);
14845 if (!NILP (cprop))
14846 {
14847 cursor = glyph;
14848 break;
14849 }
14850 if (tem && glyph->charpos < strpos)
14851 {
14852 strpos = glyph->charpos;
14853 cursor = glyph;
14854 }
14855 }
14856
14857 if (tem == pt_old
14858 || (tem - pt_old > 0 && tem < pos_after))
14859 goto compute_x;
14860 }
14861 if (tem)
14862 pos = tem + 1; /* don't find previous instances */
14863 }
14864 /* This string is not what we want; skip all of the
14865 glyphs that came from it. */
14866 while ((row->reversed_p ? glyph > stop : glyph < stop)
14867 && EQ (glyph->object, str))
14868 glyph += incr;
14869 }
14870 else
14871 glyph += incr;
14872 }
14873
14874 /* If we reached the end of the line, and END was from a string,
14875 the cursor is not on this line. */
14876 if (cursor == NULL
14877 && (row->reversed_p ? glyph <= end : glyph >= end)
14878 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14879 && STRINGP (end->object)
14880 && row->continued_p)
14881 return false;
14882 }
14883 /* A truncated row may not include PT among its character positions.
14884 Setting the cursor inside the scroll margin will trigger
14885 recalculation of hscroll in hscroll_window_tree. But if a
14886 display string covers point, defer to the string-handling
14887 code below to figure this out. */
14888 else if (row->truncated_on_left_p && pt_old < bpos_min)
14889 {
14890 cursor = glyph_before;
14891 x = -1;
14892 }
14893 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14894 /* Zero-width characters produce no glyphs. */
14895 || (!empty_line_p
14896 && (row->reversed_p
14897 ? glyph_after > glyphs_end
14898 : glyph_after < glyphs_end)))
14899 {
14900 cursor = glyph_after;
14901 x = -1;
14902 }
14903 }
14904
14905 compute_x:
14906 if (cursor != NULL)
14907 glyph = cursor;
14908 else if (glyph == glyphs_end
14909 && pos_before == pos_after
14910 && STRINGP ((row->reversed_p
14911 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14912 : row->glyphs[TEXT_AREA])->object))
14913 {
14914 /* If all the glyphs of this row came from strings, put the
14915 cursor on the first glyph of the row. This avoids having the
14916 cursor outside of the text area in this very rare and hard
14917 use case. */
14918 glyph =
14919 row->reversed_p
14920 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14921 : row->glyphs[TEXT_AREA];
14922 }
14923 if (x < 0)
14924 {
14925 struct glyph *g;
14926
14927 /* Need to compute x that corresponds to GLYPH. */
14928 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14929 {
14930 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14931 emacs_abort ();
14932 x += g->pixel_width;
14933 }
14934 }
14935
14936 /* ROW could be part of a continued line, which, under bidi
14937 reordering, might have other rows whose start and end charpos
14938 occlude point. Only set w->cursor if we found a better
14939 approximation to the cursor position than we have from previously
14940 examined candidate rows belonging to the same continued line. */
14941 if (/* We already have a candidate row. */
14942 w->cursor.vpos >= 0
14943 /* That candidate is not the row we are processing. */
14944 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14945 /* Make sure cursor.vpos specifies a row whose start and end
14946 charpos occlude point, and it is valid candidate for being a
14947 cursor-row. This is because some callers of this function
14948 leave cursor.vpos at the row where the cursor was displayed
14949 during the last redisplay cycle. */
14950 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14951 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14952 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14953 {
14954 struct glyph *g1
14955 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14956
14957 /* Don't consider glyphs that are outside TEXT_AREA. */
14958 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14959 return false;
14960 /* Keep the candidate whose buffer position is the closest to
14961 point or has the `cursor' property. */
14962 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14963 w->cursor.hpos >= 0
14964 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14965 && ((BUFFERP (g1->object)
14966 && (g1->charpos == pt_old /* An exact match always wins. */
14967 || (BUFFERP (glyph->object)
14968 && eabs (g1->charpos - pt_old)
14969 < eabs (glyph->charpos - pt_old))))
14970 /* Previous candidate is a glyph from a string that has
14971 a non-nil `cursor' property. */
14972 || (STRINGP (g1->object)
14973 && (!NILP (Fget_char_property (make_number (g1->charpos),
14974 Qcursor, g1->object))
14975 /* Previous candidate is from the same display
14976 string as this one, and the display string
14977 came from a text property. */
14978 || (EQ (g1->object, glyph->object)
14979 && string_from_text_prop)
14980 /* this candidate is from newline and its
14981 position is not an exact match */
14982 || (NILP (glyph->object)
14983 && glyph->charpos != pt_old)))))
14984 return false;
14985 /* If this candidate gives an exact match, use that. */
14986 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14987 /* If this candidate is a glyph created for the
14988 terminating newline of a line, and point is on that
14989 newline, it wins because it's an exact match. */
14990 || (!row->continued_p
14991 && NILP (glyph->object)
14992 && glyph->charpos == 0
14993 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14994 /* Otherwise, keep the candidate that comes from a row
14995 spanning less buffer positions. This may win when one or
14996 both candidate positions are on glyphs that came from
14997 display strings, for which we cannot compare buffer
14998 positions. */
14999 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15000 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15001 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15002 return false;
15003 }
15004 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15005 w->cursor.x = x;
15006 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15007 w->cursor.y = row->y + dy;
15008
15009 if (w == XWINDOW (selected_window))
15010 {
15011 if (!row->continued_p
15012 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15013 && row->x == 0)
15014 {
15015 this_line_buffer = XBUFFER (w->contents);
15016
15017 CHARPOS (this_line_start_pos)
15018 = MATRIX_ROW_START_CHARPOS (row) + delta;
15019 BYTEPOS (this_line_start_pos)
15020 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15021
15022 CHARPOS (this_line_end_pos)
15023 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15024 BYTEPOS (this_line_end_pos)
15025 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15026
15027 this_line_y = w->cursor.y;
15028 this_line_pixel_height = row->height;
15029 this_line_vpos = w->cursor.vpos;
15030 this_line_start_x = row->x;
15031 }
15032 else
15033 CHARPOS (this_line_start_pos) = 0;
15034 }
15035
15036 return true;
15037 }
15038
15039
15040 /* Run window scroll functions, if any, for WINDOW with new window
15041 start STARTP. Sets the window start of WINDOW to that position.
15042
15043 We assume that the window's buffer is really current. */
15044
15045 static struct text_pos
15046 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15047 {
15048 struct window *w = XWINDOW (window);
15049 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15050
15051 eassert (current_buffer == XBUFFER (w->contents));
15052
15053 if (!NILP (Vwindow_scroll_functions))
15054 {
15055 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15056 make_number (CHARPOS (startp)));
15057 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15058 /* In case the hook functions switch buffers. */
15059 set_buffer_internal (XBUFFER (w->contents));
15060 }
15061
15062 return startp;
15063 }
15064
15065
15066 /* Make sure the line containing the cursor is fully visible.
15067 A value of true means there is nothing to be done.
15068 (Either the line is fully visible, or it cannot be made so,
15069 or we cannot tell.)
15070
15071 If FORCE_P, return false even if partial visible cursor row
15072 is higher than window.
15073
15074 If CURRENT_MATRIX_P, use the information from the
15075 window's current glyph matrix; otherwise use the desired glyph
15076 matrix.
15077
15078 A value of false means the caller should do scrolling
15079 as if point had gone off the screen. */
15080
15081 static bool
15082 cursor_row_fully_visible_p (struct window *w, bool force_p,
15083 bool current_matrix_p)
15084 {
15085 struct glyph_matrix *matrix;
15086 struct glyph_row *row;
15087 int window_height;
15088
15089 if (!make_cursor_line_fully_visible_p)
15090 return true;
15091
15092 /* It's not always possible to find the cursor, e.g, when a window
15093 is full of overlay strings. Don't do anything in that case. */
15094 if (w->cursor.vpos < 0)
15095 return true;
15096
15097 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15098 row = MATRIX_ROW (matrix, w->cursor.vpos);
15099
15100 /* If the cursor row is not partially visible, there's nothing to do. */
15101 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15102 return true;
15103
15104 /* If the row the cursor is in is taller than the window's height,
15105 it's not clear what to do, so do nothing. */
15106 window_height = window_box_height (w);
15107 if (row->height >= window_height)
15108 {
15109 if (!force_p || MINI_WINDOW_P (w)
15110 || w->vscroll || w->cursor.vpos == 0)
15111 return true;
15112 }
15113 return false;
15114 }
15115
15116
15117 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15118 means only WINDOW is redisplayed in redisplay_internal.
15119 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15120 in redisplay_window to bring a partially visible line into view in
15121 the case that only the cursor has moved.
15122
15123 LAST_LINE_MISFIT should be true if we're scrolling because the
15124 last screen line's vertical height extends past the end of the screen.
15125
15126 Value is
15127
15128 1 if scrolling succeeded
15129
15130 0 if scrolling didn't find point.
15131
15132 -1 if new fonts have been loaded so that we must interrupt
15133 redisplay, adjust glyph matrices, and try again. */
15134
15135 enum
15136 {
15137 SCROLLING_SUCCESS,
15138 SCROLLING_FAILED,
15139 SCROLLING_NEED_LARGER_MATRICES
15140 };
15141
15142 /* If scroll-conservatively is more than this, never recenter.
15143
15144 If you change this, don't forget to update the doc string of
15145 `scroll-conservatively' and the Emacs manual. */
15146 #define SCROLL_LIMIT 100
15147
15148 static int
15149 try_scrolling (Lisp_Object window, bool just_this_one_p,
15150 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15151 bool temp_scroll_step, bool last_line_misfit)
15152 {
15153 struct window *w = XWINDOW (window);
15154 struct frame *f = XFRAME (w->frame);
15155 struct text_pos pos, startp;
15156 struct it it;
15157 int this_scroll_margin, scroll_max, rc, height;
15158 int dy = 0, amount_to_scroll = 0;
15159 bool scroll_down_p = false;
15160 int extra_scroll_margin_lines = last_line_misfit;
15161 Lisp_Object aggressive;
15162 /* We will never try scrolling more than this number of lines. */
15163 int scroll_limit = SCROLL_LIMIT;
15164 int frame_line_height = default_line_pixel_height (w);
15165 int window_total_lines
15166 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15167
15168 #ifdef GLYPH_DEBUG
15169 debug_method_add (w, "try_scrolling");
15170 #endif
15171
15172 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15173
15174 /* Compute scroll margin height in pixels. We scroll when point is
15175 within this distance from the top or bottom of the window. */
15176 if (scroll_margin > 0)
15177 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15178 * frame_line_height;
15179 else
15180 this_scroll_margin = 0;
15181
15182 /* Force arg_scroll_conservatively to have a reasonable value, to
15183 avoid scrolling too far away with slow move_it_* functions. Note
15184 that the user can supply scroll-conservatively equal to
15185 `most-positive-fixnum', which can be larger than INT_MAX. */
15186 if (arg_scroll_conservatively > scroll_limit)
15187 {
15188 arg_scroll_conservatively = scroll_limit + 1;
15189 scroll_max = scroll_limit * frame_line_height;
15190 }
15191 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15192 /* Compute how much we should try to scroll maximally to bring
15193 point into view. */
15194 scroll_max = (max (scroll_step,
15195 max (arg_scroll_conservatively, temp_scroll_step))
15196 * frame_line_height);
15197 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15198 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15199 /* We're trying to scroll because of aggressive scrolling but no
15200 scroll_step is set. Choose an arbitrary one. */
15201 scroll_max = 10 * frame_line_height;
15202 else
15203 scroll_max = 0;
15204
15205 too_near_end:
15206
15207 /* Decide whether to scroll down. */
15208 if (PT > CHARPOS (startp))
15209 {
15210 int scroll_margin_y;
15211
15212 /* Compute the pixel ypos of the scroll margin, then move IT to
15213 either that ypos or PT, whichever comes first. */
15214 start_display (&it, w, startp);
15215 scroll_margin_y = it.last_visible_y - this_scroll_margin
15216 - frame_line_height * extra_scroll_margin_lines;
15217 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15218 (MOVE_TO_POS | MOVE_TO_Y));
15219
15220 if (PT > CHARPOS (it.current.pos))
15221 {
15222 int y0 = line_bottom_y (&it);
15223 /* Compute how many pixels below window bottom to stop searching
15224 for PT. This avoids costly search for PT that is far away if
15225 the user limited scrolling by a small number of lines, but
15226 always finds PT if scroll_conservatively is set to a large
15227 number, such as most-positive-fixnum. */
15228 int slack = max (scroll_max, 10 * frame_line_height);
15229 int y_to_move = it.last_visible_y + slack;
15230
15231 /* Compute the distance from the scroll margin to PT or to
15232 the scroll limit, whichever comes first. This should
15233 include the height of the cursor line, to make that line
15234 fully visible. */
15235 move_it_to (&it, PT, -1, y_to_move,
15236 -1, MOVE_TO_POS | MOVE_TO_Y);
15237 dy = line_bottom_y (&it) - y0;
15238
15239 if (dy > scroll_max)
15240 return SCROLLING_FAILED;
15241
15242 if (dy > 0)
15243 scroll_down_p = true;
15244 }
15245 }
15246
15247 if (scroll_down_p)
15248 {
15249 /* Point is in or below the bottom scroll margin, so move the
15250 window start down. If scrolling conservatively, move it just
15251 enough down to make point visible. If scroll_step is set,
15252 move it down by scroll_step. */
15253 if (arg_scroll_conservatively)
15254 amount_to_scroll
15255 = min (max (dy, frame_line_height),
15256 frame_line_height * arg_scroll_conservatively);
15257 else if (scroll_step || temp_scroll_step)
15258 amount_to_scroll = scroll_max;
15259 else
15260 {
15261 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15262 height = WINDOW_BOX_TEXT_HEIGHT (w);
15263 if (NUMBERP (aggressive))
15264 {
15265 double float_amount = XFLOATINT (aggressive) * height;
15266 int aggressive_scroll = float_amount;
15267 if (aggressive_scroll == 0 && float_amount > 0)
15268 aggressive_scroll = 1;
15269 /* Don't let point enter the scroll margin near top of
15270 the window. This could happen if the value of
15271 scroll_up_aggressively is too large and there are
15272 non-zero margins, because scroll_up_aggressively
15273 means put point that fraction of window height
15274 _from_the_bottom_margin_. */
15275 if (aggressive_scroll + 2 * this_scroll_margin > height)
15276 aggressive_scroll = height - 2 * this_scroll_margin;
15277 amount_to_scroll = dy + aggressive_scroll;
15278 }
15279 }
15280
15281 if (amount_to_scroll <= 0)
15282 return SCROLLING_FAILED;
15283
15284 start_display (&it, w, startp);
15285 if (arg_scroll_conservatively <= scroll_limit)
15286 move_it_vertically (&it, amount_to_scroll);
15287 else
15288 {
15289 /* Extra precision for users who set scroll-conservatively
15290 to a large number: make sure the amount we scroll
15291 the window start is never less than amount_to_scroll,
15292 which was computed as distance from window bottom to
15293 point. This matters when lines at window top and lines
15294 below window bottom have different height. */
15295 struct it it1;
15296 void *it1data = NULL;
15297 /* We use a temporary it1 because line_bottom_y can modify
15298 its argument, if it moves one line down; see there. */
15299 int start_y;
15300
15301 SAVE_IT (it1, it, it1data);
15302 start_y = line_bottom_y (&it1);
15303 do {
15304 RESTORE_IT (&it, &it, it1data);
15305 move_it_by_lines (&it, 1);
15306 SAVE_IT (it1, it, it1data);
15307 } while (IT_CHARPOS (it) < ZV
15308 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15309 bidi_unshelve_cache (it1data, true);
15310 }
15311
15312 /* If STARTP is unchanged, move it down another screen line. */
15313 if (IT_CHARPOS (it) == CHARPOS (startp))
15314 move_it_by_lines (&it, 1);
15315 startp = it.current.pos;
15316 }
15317 else
15318 {
15319 struct text_pos scroll_margin_pos = startp;
15320 int y_offset = 0;
15321
15322 /* See if point is inside the scroll margin at the top of the
15323 window. */
15324 if (this_scroll_margin)
15325 {
15326 int y_start;
15327
15328 start_display (&it, w, startp);
15329 y_start = it.current_y;
15330 move_it_vertically (&it, this_scroll_margin);
15331 scroll_margin_pos = it.current.pos;
15332 /* If we didn't move enough before hitting ZV, request
15333 additional amount of scroll, to move point out of the
15334 scroll margin. */
15335 if (IT_CHARPOS (it) == ZV
15336 && it.current_y - y_start < this_scroll_margin)
15337 y_offset = this_scroll_margin - (it.current_y - y_start);
15338 }
15339
15340 if (PT < CHARPOS (scroll_margin_pos))
15341 {
15342 /* Point is in the scroll margin at the top of the window or
15343 above what is displayed in the window. */
15344 int y0, y_to_move;
15345
15346 /* Compute the vertical distance from PT to the scroll
15347 margin position. Move as far as scroll_max allows, or
15348 one screenful, or 10 screen lines, whichever is largest.
15349 Give up if distance is greater than scroll_max or if we
15350 didn't reach the scroll margin position. */
15351 SET_TEXT_POS (pos, PT, PT_BYTE);
15352 start_display (&it, w, pos);
15353 y0 = it.current_y;
15354 y_to_move = max (it.last_visible_y,
15355 max (scroll_max, 10 * frame_line_height));
15356 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15357 y_to_move, -1,
15358 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15359 dy = it.current_y - y0;
15360 if (dy > scroll_max
15361 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15362 return SCROLLING_FAILED;
15363
15364 /* Additional scroll for when ZV was too close to point. */
15365 dy += y_offset;
15366
15367 /* Compute new window start. */
15368 start_display (&it, w, startp);
15369
15370 if (arg_scroll_conservatively)
15371 amount_to_scroll = max (dy, frame_line_height
15372 * max (scroll_step, temp_scroll_step));
15373 else if (scroll_step || temp_scroll_step)
15374 amount_to_scroll = scroll_max;
15375 else
15376 {
15377 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15378 height = WINDOW_BOX_TEXT_HEIGHT (w);
15379 if (NUMBERP (aggressive))
15380 {
15381 double float_amount = XFLOATINT (aggressive) * height;
15382 int aggressive_scroll = float_amount;
15383 if (aggressive_scroll == 0 && float_amount > 0)
15384 aggressive_scroll = 1;
15385 /* Don't let point enter the scroll margin near
15386 bottom of the window, if the value of
15387 scroll_down_aggressively happens to be too
15388 large. */
15389 if (aggressive_scroll + 2 * this_scroll_margin > height)
15390 aggressive_scroll = height - 2 * this_scroll_margin;
15391 amount_to_scroll = dy + aggressive_scroll;
15392 }
15393 }
15394
15395 if (amount_to_scroll <= 0)
15396 return SCROLLING_FAILED;
15397
15398 move_it_vertically_backward (&it, amount_to_scroll);
15399 startp = it.current.pos;
15400 }
15401 }
15402
15403 /* Run window scroll functions. */
15404 startp = run_window_scroll_functions (window, startp);
15405
15406 /* Display the window. Give up if new fonts are loaded, or if point
15407 doesn't appear. */
15408 if (!try_window (window, startp, 0))
15409 rc = SCROLLING_NEED_LARGER_MATRICES;
15410 else if (w->cursor.vpos < 0)
15411 {
15412 clear_glyph_matrix (w->desired_matrix);
15413 rc = SCROLLING_FAILED;
15414 }
15415 else
15416 {
15417 /* Maybe forget recorded base line for line number display. */
15418 if (!just_this_one_p
15419 || current_buffer->clip_changed
15420 || BEG_UNCHANGED < CHARPOS (startp))
15421 w->base_line_number = 0;
15422
15423 /* If cursor ends up on a partially visible line,
15424 treat that as being off the bottom of the screen. */
15425 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15426 false)
15427 /* It's possible that the cursor is on the first line of the
15428 buffer, which is partially obscured due to a vscroll
15429 (Bug#7537). In that case, avoid looping forever. */
15430 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15431 {
15432 clear_glyph_matrix (w->desired_matrix);
15433 ++extra_scroll_margin_lines;
15434 goto too_near_end;
15435 }
15436 rc = SCROLLING_SUCCESS;
15437 }
15438
15439 return rc;
15440 }
15441
15442
15443 /* Compute a suitable window start for window W if display of W starts
15444 on a continuation line. Value is true if a new window start
15445 was computed.
15446
15447 The new window start will be computed, based on W's width, starting
15448 from the start of the continued line. It is the start of the
15449 screen line with the minimum distance from the old start W->start. */
15450
15451 static bool
15452 compute_window_start_on_continuation_line (struct window *w)
15453 {
15454 struct text_pos pos, start_pos;
15455 bool window_start_changed_p = false;
15456
15457 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15458
15459 /* If window start is on a continuation line... Window start may be
15460 < BEGV in case there's invisible text at the start of the
15461 buffer (M-x rmail, for example). */
15462 if (CHARPOS (start_pos) > BEGV
15463 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15464 {
15465 struct it it;
15466 struct glyph_row *row;
15467
15468 /* Handle the case that the window start is out of range. */
15469 if (CHARPOS (start_pos) < BEGV)
15470 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15471 else if (CHARPOS (start_pos) > ZV)
15472 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15473
15474 /* Find the start of the continued line. This should be fast
15475 because find_newline is fast (newline cache). */
15476 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15477 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15478 row, DEFAULT_FACE_ID);
15479 reseat_at_previous_visible_line_start (&it);
15480
15481 /* If the line start is "too far" away from the window start,
15482 say it takes too much time to compute a new window start. */
15483 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15484 /* PXW: Do we need upper bounds here? */
15485 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15486 {
15487 int min_distance, distance;
15488
15489 /* Move forward by display lines to find the new window
15490 start. If window width was enlarged, the new start can
15491 be expected to be > the old start. If window width was
15492 decreased, the new window start will be < the old start.
15493 So, we're looking for the display line start with the
15494 minimum distance from the old window start. */
15495 pos = it.current.pos;
15496 min_distance = INFINITY;
15497 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15498 distance < min_distance)
15499 {
15500 min_distance = distance;
15501 pos = it.current.pos;
15502 if (it.line_wrap == WORD_WRAP)
15503 {
15504 /* Under WORD_WRAP, move_it_by_lines is likely to
15505 overshoot and stop not at the first, but the
15506 second character from the left margin. So in
15507 that case, we need a more tight control on the X
15508 coordinate of the iterator than move_it_by_lines
15509 promises in its contract. The method is to first
15510 go to the last (rightmost) visible character of a
15511 line, then move to the leftmost character on the
15512 next line in a separate call. */
15513 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15514 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15515 move_it_to (&it, ZV, 0,
15516 it.current_y + it.max_ascent + it.max_descent, -1,
15517 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15518 }
15519 else
15520 move_it_by_lines (&it, 1);
15521 }
15522
15523 /* Set the window start there. */
15524 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15525 window_start_changed_p = true;
15526 }
15527 }
15528
15529 return window_start_changed_p;
15530 }
15531
15532
15533 /* Try cursor movement in case text has not changed in window WINDOW,
15534 with window start STARTP. Value is
15535
15536 CURSOR_MOVEMENT_SUCCESS if successful
15537
15538 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15539
15540 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15541 display. *SCROLL_STEP is set to true, under certain circumstances, if
15542 we want to scroll as if scroll-step were set to 1. See the code.
15543
15544 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15545 which case we have to abort this redisplay, and adjust matrices
15546 first. */
15547
15548 enum
15549 {
15550 CURSOR_MOVEMENT_SUCCESS,
15551 CURSOR_MOVEMENT_CANNOT_BE_USED,
15552 CURSOR_MOVEMENT_MUST_SCROLL,
15553 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15554 };
15555
15556 static int
15557 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15558 bool *scroll_step)
15559 {
15560 struct window *w = XWINDOW (window);
15561 struct frame *f = XFRAME (w->frame);
15562 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15563
15564 #ifdef GLYPH_DEBUG
15565 if (inhibit_try_cursor_movement)
15566 return rc;
15567 #endif
15568
15569 /* Previously, there was a check for Lisp integer in the
15570 if-statement below. Now, this field is converted to
15571 ptrdiff_t, thus zero means invalid position in a buffer. */
15572 eassert (w->last_point > 0);
15573 /* Likewise there was a check whether window_end_vpos is nil or larger
15574 than the window. Now window_end_vpos is int and so never nil, but
15575 let's leave eassert to check whether it fits in the window. */
15576 eassert (!w->window_end_valid
15577 || w->window_end_vpos < w->current_matrix->nrows);
15578
15579 /* Handle case where text has not changed, only point, and it has
15580 not moved off the frame. */
15581 if (/* Point may be in this window. */
15582 PT >= CHARPOS (startp)
15583 /* Selective display hasn't changed. */
15584 && !current_buffer->clip_changed
15585 /* Function force-mode-line-update is used to force a thorough
15586 redisplay. It sets either windows_or_buffers_changed or
15587 update_mode_lines. So don't take a shortcut here for these
15588 cases. */
15589 && !update_mode_lines
15590 && !windows_or_buffers_changed
15591 && !f->cursor_type_changed
15592 && NILP (Vshow_trailing_whitespace)
15593 /* This code is not used for mini-buffer for the sake of the case
15594 of redisplaying to replace an echo area message; since in
15595 that case the mini-buffer contents per se are usually
15596 unchanged. This code is of no real use in the mini-buffer
15597 since the handling of this_line_start_pos, etc., in redisplay
15598 handles the same cases. */
15599 && !EQ (window, minibuf_window)
15600 && (FRAME_WINDOW_P (f)
15601 || !overlay_arrow_in_current_buffer_p ()))
15602 {
15603 int this_scroll_margin, top_scroll_margin;
15604 struct glyph_row *row = NULL;
15605 int frame_line_height = default_line_pixel_height (w);
15606 int window_total_lines
15607 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15608
15609 #ifdef GLYPH_DEBUG
15610 debug_method_add (w, "cursor movement");
15611 #endif
15612
15613 /* Scroll if point within this distance from the top or bottom
15614 of the window. This is a pixel value. */
15615 if (scroll_margin > 0)
15616 {
15617 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15618 this_scroll_margin *= frame_line_height;
15619 }
15620 else
15621 this_scroll_margin = 0;
15622
15623 top_scroll_margin = this_scroll_margin;
15624 if (WINDOW_WANTS_HEADER_LINE_P (w))
15625 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15626
15627 /* Start with the row the cursor was displayed during the last
15628 not paused redisplay. Give up if that row is not valid. */
15629 if (w->last_cursor_vpos < 0
15630 || w->last_cursor_vpos >= w->current_matrix->nrows)
15631 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15632 else
15633 {
15634 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15635 if (row->mode_line_p)
15636 ++row;
15637 if (!row->enabled_p)
15638 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15639 }
15640
15641 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15642 {
15643 bool scroll_p = false, must_scroll = false;
15644 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15645
15646 if (PT > w->last_point)
15647 {
15648 /* Point has moved forward. */
15649 while (MATRIX_ROW_END_CHARPOS (row) < PT
15650 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15651 {
15652 eassert (row->enabled_p);
15653 ++row;
15654 }
15655
15656 /* If the end position of a row equals the start
15657 position of the next row, and PT is at that position,
15658 we would rather display cursor in the next line. */
15659 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15660 && MATRIX_ROW_END_CHARPOS (row) == PT
15661 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15662 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15663 && !cursor_row_p (row))
15664 ++row;
15665
15666 /* If within the scroll margin, scroll. Note that
15667 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15668 the next line would be drawn, and that
15669 this_scroll_margin can be zero. */
15670 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15671 || PT > MATRIX_ROW_END_CHARPOS (row)
15672 /* Line is completely visible last line in window
15673 and PT is to be set in the next line. */
15674 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15675 && PT == MATRIX_ROW_END_CHARPOS (row)
15676 && !row->ends_at_zv_p
15677 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15678 scroll_p = true;
15679 }
15680 else if (PT < w->last_point)
15681 {
15682 /* Cursor has to be moved backward. Note that PT >=
15683 CHARPOS (startp) because of the outer if-statement. */
15684 while (!row->mode_line_p
15685 && (MATRIX_ROW_START_CHARPOS (row) > PT
15686 || (MATRIX_ROW_START_CHARPOS (row) == PT
15687 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15688 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15689 row > w->current_matrix->rows
15690 && (row-1)->ends_in_newline_from_string_p))))
15691 && (row->y > top_scroll_margin
15692 || CHARPOS (startp) == BEGV))
15693 {
15694 eassert (row->enabled_p);
15695 --row;
15696 }
15697
15698 /* Consider the following case: Window starts at BEGV,
15699 there is invisible, intangible text at BEGV, so that
15700 display starts at some point START > BEGV. It can
15701 happen that we are called with PT somewhere between
15702 BEGV and START. Try to handle that case. */
15703 if (row < w->current_matrix->rows
15704 || row->mode_line_p)
15705 {
15706 row = w->current_matrix->rows;
15707 if (row->mode_line_p)
15708 ++row;
15709 }
15710
15711 /* Due to newlines in overlay strings, we may have to
15712 skip forward over overlay strings. */
15713 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15714 && MATRIX_ROW_END_CHARPOS (row) == PT
15715 && !cursor_row_p (row))
15716 ++row;
15717
15718 /* If within the scroll margin, scroll. */
15719 if (row->y < top_scroll_margin
15720 && CHARPOS (startp) != BEGV)
15721 scroll_p = true;
15722 }
15723 else
15724 {
15725 /* Cursor did not move. So don't scroll even if cursor line
15726 is partially visible, as it was so before. */
15727 rc = CURSOR_MOVEMENT_SUCCESS;
15728 }
15729
15730 if (PT < MATRIX_ROW_START_CHARPOS (row)
15731 || PT > MATRIX_ROW_END_CHARPOS (row))
15732 {
15733 /* if PT is not in the glyph row, give up. */
15734 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15735 must_scroll = true;
15736 }
15737 else if (rc != CURSOR_MOVEMENT_SUCCESS
15738 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15739 {
15740 struct glyph_row *row1;
15741
15742 /* If rows are bidi-reordered and point moved, back up
15743 until we find a row that does not belong to a
15744 continuation line. This is because we must consider
15745 all rows of a continued line as candidates for the
15746 new cursor positioning, since row start and end
15747 positions change non-linearly with vertical position
15748 in such rows. */
15749 /* FIXME: Revisit this when glyph ``spilling'' in
15750 continuation lines' rows is implemented for
15751 bidi-reordered rows. */
15752 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15753 MATRIX_ROW_CONTINUATION_LINE_P (row);
15754 --row)
15755 {
15756 /* If we hit the beginning of the displayed portion
15757 without finding the first row of a continued
15758 line, give up. */
15759 if (row <= row1)
15760 {
15761 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15762 break;
15763 }
15764 eassert (row->enabled_p);
15765 }
15766 }
15767 if (must_scroll)
15768 ;
15769 else if (rc != CURSOR_MOVEMENT_SUCCESS
15770 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15771 /* Make sure this isn't a header line by any chance, since
15772 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15773 && !row->mode_line_p
15774 && make_cursor_line_fully_visible_p)
15775 {
15776 if (PT == MATRIX_ROW_END_CHARPOS (row)
15777 && !row->ends_at_zv_p
15778 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15779 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15780 else if (row->height > window_box_height (w))
15781 {
15782 /* If we end up in a partially visible line, let's
15783 make it fully visible, except when it's taller
15784 than the window, in which case we can't do much
15785 about it. */
15786 *scroll_step = true;
15787 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15788 }
15789 else
15790 {
15791 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15792 if (!cursor_row_fully_visible_p (w, false, true))
15793 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15794 else
15795 rc = CURSOR_MOVEMENT_SUCCESS;
15796 }
15797 }
15798 else if (scroll_p)
15799 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15800 else if (rc != CURSOR_MOVEMENT_SUCCESS
15801 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15802 {
15803 /* With bidi-reordered rows, there could be more than
15804 one candidate row whose start and end positions
15805 occlude point. We need to let set_cursor_from_row
15806 find the best candidate. */
15807 /* FIXME: Revisit this when glyph ``spilling'' in
15808 continuation lines' rows is implemented for
15809 bidi-reordered rows. */
15810 bool rv = false;
15811
15812 do
15813 {
15814 bool at_zv_p = false, exact_match_p = false;
15815
15816 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15817 && PT <= MATRIX_ROW_END_CHARPOS (row)
15818 && cursor_row_p (row))
15819 rv |= set_cursor_from_row (w, row, w->current_matrix,
15820 0, 0, 0, 0);
15821 /* As soon as we've found the exact match for point,
15822 or the first suitable row whose ends_at_zv_p flag
15823 is set, we are done. */
15824 if (rv)
15825 {
15826 at_zv_p = MATRIX_ROW (w->current_matrix,
15827 w->cursor.vpos)->ends_at_zv_p;
15828 if (!at_zv_p
15829 && w->cursor.hpos >= 0
15830 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15831 w->cursor.vpos))
15832 {
15833 struct glyph_row *candidate =
15834 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15835 struct glyph *g =
15836 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15837 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15838
15839 exact_match_p =
15840 (BUFFERP (g->object) && g->charpos == PT)
15841 || (NILP (g->object)
15842 && (g->charpos == PT
15843 || (g->charpos == 0 && endpos - 1 == PT)));
15844 }
15845 if (at_zv_p || exact_match_p)
15846 {
15847 rc = CURSOR_MOVEMENT_SUCCESS;
15848 break;
15849 }
15850 }
15851 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15852 break;
15853 ++row;
15854 }
15855 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15856 || row->continued_p)
15857 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15858 || (MATRIX_ROW_START_CHARPOS (row) == PT
15859 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15860 /* If we didn't find any candidate rows, or exited the
15861 loop before all the candidates were examined, signal
15862 to the caller that this method failed. */
15863 if (rc != CURSOR_MOVEMENT_SUCCESS
15864 && !(rv
15865 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15866 && !row->continued_p))
15867 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15868 else if (rv)
15869 rc = CURSOR_MOVEMENT_SUCCESS;
15870 }
15871 else
15872 {
15873 do
15874 {
15875 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15876 {
15877 rc = CURSOR_MOVEMENT_SUCCESS;
15878 break;
15879 }
15880 ++row;
15881 }
15882 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15883 && MATRIX_ROW_START_CHARPOS (row) == PT
15884 && cursor_row_p (row));
15885 }
15886 }
15887 }
15888
15889 return rc;
15890 }
15891
15892
15893 void
15894 set_vertical_scroll_bar (struct window *w)
15895 {
15896 ptrdiff_t start, end, whole;
15897
15898 /* Calculate the start and end positions for the current window.
15899 At some point, it would be nice to choose between scrollbars
15900 which reflect the whole buffer size, with special markers
15901 indicating narrowing, and scrollbars which reflect only the
15902 visible region.
15903
15904 Note that mini-buffers sometimes aren't displaying any text. */
15905 if (!MINI_WINDOW_P (w)
15906 || (w == XWINDOW (minibuf_window)
15907 && NILP (echo_area_buffer[0])))
15908 {
15909 struct buffer *buf = XBUFFER (w->contents);
15910 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15911 start = marker_position (w->start) - BUF_BEGV (buf);
15912 /* I don't think this is guaranteed to be right. For the
15913 moment, we'll pretend it is. */
15914 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15915
15916 if (end < start)
15917 end = start;
15918 if (whole < (end - start))
15919 whole = end - start;
15920 }
15921 else
15922 start = end = whole = 0;
15923
15924 /* Indicate what this scroll bar ought to be displaying now. */
15925 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15926 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15927 (w, end - start, whole, start);
15928 }
15929
15930
15931 void
15932 set_horizontal_scroll_bar (struct window *w)
15933 {
15934 int start, end, whole, portion;
15935
15936 if (!MINI_WINDOW_P (w)
15937 || (w == XWINDOW (minibuf_window)
15938 && NILP (echo_area_buffer[0])))
15939 {
15940 struct buffer *b = XBUFFER (w->contents);
15941 struct buffer *old_buffer = NULL;
15942 struct it it;
15943 struct text_pos startp;
15944
15945 if (b != current_buffer)
15946 {
15947 old_buffer = current_buffer;
15948 set_buffer_internal (b);
15949 }
15950
15951 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15952 start_display (&it, w, startp);
15953 it.last_visible_x = INT_MAX;
15954 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15955 MOVE_TO_X | MOVE_TO_Y);
15956 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15957 window_box_height (w), -1,
15958 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15959
15960 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15961 end = start + window_box_width (w, TEXT_AREA);
15962 portion = end - start;
15963 /* After enlarging a horizontally scrolled window such that it
15964 gets at least as wide as the text it contains, make sure that
15965 the thumb doesn't fill the entire scroll bar so we can still
15966 drag it back to see the entire text. */
15967 whole = max (whole, end);
15968
15969 if (it.bidi_p)
15970 {
15971 Lisp_Object pdir;
15972
15973 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15974 if (EQ (pdir, Qright_to_left))
15975 {
15976 start = whole - end;
15977 end = start + portion;
15978 }
15979 }
15980
15981 if (old_buffer)
15982 set_buffer_internal (old_buffer);
15983 }
15984 else
15985 start = end = whole = portion = 0;
15986
15987 w->hscroll_whole = whole;
15988
15989 /* Indicate what this scroll bar ought to be displaying now. */
15990 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15991 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15992 (w, portion, whole, start);
15993 }
15994
15995
15996 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
15997 selected_window is redisplayed.
15998
15999 We can return without actually redisplaying the window if fonts has been
16000 changed on window's frame. In that case, redisplay_internal will retry.
16001
16002 As one of the important parts of redisplaying a window, we need to
16003 decide whether the previous window-start position (stored in the
16004 window's w->start marker position) is still valid, and if it isn't,
16005 recompute it. Some details about that:
16006
16007 . The previous window-start could be in a continuation line, in
16008 which case we need to recompute it when the window width
16009 changes. See compute_window_start_on_continuation_line and its
16010 call below.
16011
16012 . The text that changed since last redisplay could include the
16013 previous window-start position. In that case, we try to salvage
16014 what we can from the current glyph matrix by calling
16015 try_scrolling, which see.
16016
16017 . Some Emacs command could force us to use a specific window-start
16018 position by setting the window's force_start flag, or gently
16019 propose doing that by setting the window's optional_new_start
16020 flag. In these cases, we try using the specified start point if
16021 that succeeds (i.e. the window desired matrix is successfully
16022 recomputed, and point location is within the window). In case
16023 of optional_new_start, we first check if the specified start
16024 position is feasible, i.e. if it will allow point to be
16025 displayed in the window. If using the specified start point
16026 fails, e.g., if new fonts are needed to be loaded, we abort the
16027 redisplay cycle and leave it up to the next cycle to figure out
16028 things.
16029
16030 . Note that the window's force_start flag is sometimes set by
16031 redisplay itself, when it decides that the previous window start
16032 point is fine and should be kept. Search for "goto force_start"
16033 below to see the details. Like the values of window-start
16034 specified outside of redisplay, these internally-deduced values
16035 are tested for feasibility, and ignored if found to be
16036 unfeasible.
16037
16038 . Note that the function try_window, used to completely redisplay
16039 a window, accepts the window's start point as its argument.
16040 This is used several times in the redisplay code to control
16041 where the window start will be, according to user options such
16042 as scroll-conservatively, and also to ensure the screen line
16043 showing point will be fully (as opposed to partially) visible on
16044 display. */
16045
16046 static void
16047 redisplay_window (Lisp_Object window, bool just_this_one_p)
16048 {
16049 struct window *w = XWINDOW (window);
16050 struct frame *f = XFRAME (w->frame);
16051 struct buffer *buffer = XBUFFER (w->contents);
16052 struct buffer *old = current_buffer;
16053 struct text_pos lpoint, opoint, startp;
16054 bool update_mode_line;
16055 int tem;
16056 struct it it;
16057 /* Record it now because it's overwritten. */
16058 bool current_matrix_up_to_date_p = false;
16059 bool used_current_matrix_p = false;
16060 /* This is less strict than current_matrix_up_to_date_p.
16061 It indicates that the buffer contents and narrowing are unchanged. */
16062 bool buffer_unchanged_p = false;
16063 bool temp_scroll_step = false;
16064 ptrdiff_t count = SPECPDL_INDEX ();
16065 int rc;
16066 int centering_position = -1;
16067 bool last_line_misfit = false;
16068 ptrdiff_t beg_unchanged, end_unchanged;
16069 int frame_line_height;
16070
16071 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16072 opoint = lpoint;
16073
16074 #ifdef GLYPH_DEBUG
16075 *w->desired_matrix->method = 0;
16076 #endif
16077
16078 if (!just_this_one_p
16079 && REDISPLAY_SOME_P ()
16080 && !w->redisplay
16081 && !w->update_mode_line
16082 && !f->face_change
16083 && !f->redisplay
16084 && !buffer->text->redisplay
16085 && BUF_PT (buffer) == w->last_point)
16086 return;
16087
16088 /* Make sure that both W's markers are valid. */
16089 eassert (XMARKER (w->start)->buffer == buffer);
16090 eassert (XMARKER (w->pointm)->buffer == buffer);
16091
16092 /* We come here again if we need to run window-text-change-functions
16093 below. */
16094 restart:
16095 reconsider_clip_changes (w);
16096 frame_line_height = default_line_pixel_height (w);
16097
16098 /* Has the mode line to be updated? */
16099 update_mode_line = (w->update_mode_line
16100 || update_mode_lines
16101 || buffer->clip_changed
16102 || buffer->prevent_redisplay_optimizations_p);
16103
16104 if (!just_this_one_p)
16105 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16106 cleverly elsewhere. */
16107 w->must_be_updated_p = true;
16108
16109 if (MINI_WINDOW_P (w))
16110 {
16111 if (w == XWINDOW (echo_area_window)
16112 && !NILP (echo_area_buffer[0]))
16113 {
16114 if (update_mode_line)
16115 /* We may have to update a tty frame's menu bar or a
16116 tool-bar. Example `M-x C-h C-h C-g'. */
16117 goto finish_menu_bars;
16118 else
16119 /* We've already displayed the echo area glyphs in this window. */
16120 goto finish_scroll_bars;
16121 }
16122 else if ((w != XWINDOW (minibuf_window)
16123 || minibuf_level == 0)
16124 /* When buffer is nonempty, redisplay window normally. */
16125 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16126 /* Quail displays non-mini buffers in minibuffer window.
16127 In that case, redisplay the window normally. */
16128 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16129 {
16130 /* W is a mini-buffer window, but it's not active, so clear
16131 it. */
16132 int yb = window_text_bottom_y (w);
16133 struct glyph_row *row;
16134 int y;
16135
16136 for (y = 0, row = w->desired_matrix->rows;
16137 y < yb;
16138 y += row->height, ++row)
16139 blank_row (w, row, y);
16140 goto finish_scroll_bars;
16141 }
16142
16143 clear_glyph_matrix (w->desired_matrix);
16144 }
16145
16146 /* Otherwise set up data on this window; select its buffer and point
16147 value. */
16148 /* Really select the buffer, for the sake of buffer-local
16149 variables. */
16150 set_buffer_internal_1 (XBUFFER (w->contents));
16151
16152 current_matrix_up_to_date_p
16153 = (w->window_end_valid
16154 && !current_buffer->clip_changed
16155 && !current_buffer->prevent_redisplay_optimizations_p
16156 && !window_outdated (w));
16157
16158 /* Run the window-text-change-functions
16159 if it is possible that the text on the screen has changed
16160 (either due to modification of the text, or any other reason). */
16161 if (!current_matrix_up_to_date_p
16162 && !NILP (Vwindow_text_change_functions))
16163 {
16164 safe_run_hooks (Qwindow_text_change_functions);
16165 goto restart;
16166 }
16167
16168 beg_unchanged = BEG_UNCHANGED;
16169 end_unchanged = END_UNCHANGED;
16170
16171 SET_TEXT_POS (opoint, PT, PT_BYTE);
16172
16173 specbind (Qinhibit_point_motion_hooks, Qt);
16174
16175 buffer_unchanged_p
16176 = (w->window_end_valid
16177 && !current_buffer->clip_changed
16178 && !window_outdated (w));
16179
16180 /* When windows_or_buffers_changed is non-zero, we can't rely
16181 on the window end being valid, so set it to zero there. */
16182 if (windows_or_buffers_changed)
16183 {
16184 /* If window starts on a continuation line, maybe adjust the
16185 window start in case the window's width changed. */
16186 if (XMARKER (w->start)->buffer == current_buffer)
16187 compute_window_start_on_continuation_line (w);
16188
16189 w->window_end_valid = false;
16190 /* If so, we also can't rely on current matrix
16191 and should not fool try_cursor_movement below. */
16192 current_matrix_up_to_date_p = false;
16193 }
16194
16195 /* Some sanity checks. */
16196 CHECK_WINDOW_END (w);
16197 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16198 emacs_abort ();
16199 if (BYTEPOS (opoint) < CHARPOS (opoint))
16200 emacs_abort ();
16201
16202 if (mode_line_update_needed (w))
16203 update_mode_line = true;
16204
16205 /* Point refers normally to the selected window. For any other
16206 window, set up appropriate value. */
16207 if (!EQ (window, selected_window))
16208 {
16209 ptrdiff_t new_pt = marker_position (w->pointm);
16210 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16211
16212 if (new_pt < BEGV)
16213 {
16214 new_pt = BEGV;
16215 new_pt_byte = BEGV_BYTE;
16216 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16217 }
16218 else if (new_pt > (ZV - 1))
16219 {
16220 new_pt = ZV;
16221 new_pt_byte = ZV_BYTE;
16222 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16223 }
16224
16225 /* We don't use SET_PT so that the point-motion hooks don't run. */
16226 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16227 }
16228
16229 /* If any of the character widths specified in the display table
16230 have changed, invalidate the width run cache. It's true that
16231 this may be a bit late to catch such changes, but the rest of
16232 redisplay goes (non-fatally) haywire when the display table is
16233 changed, so why should we worry about doing any better? */
16234 if (current_buffer->width_run_cache
16235 || (current_buffer->base_buffer
16236 && current_buffer->base_buffer->width_run_cache))
16237 {
16238 struct Lisp_Char_Table *disptab = buffer_display_table ();
16239
16240 if (! disptab_matches_widthtab
16241 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16242 {
16243 struct buffer *buf = current_buffer;
16244
16245 if (buf->base_buffer)
16246 buf = buf->base_buffer;
16247 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16248 recompute_width_table (current_buffer, disptab);
16249 }
16250 }
16251
16252 /* If window-start is screwed up, choose a new one. */
16253 if (XMARKER (w->start)->buffer != current_buffer)
16254 goto recenter;
16255
16256 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16257
16258 /* If someone specified a new starting point but did not insist,
16259 check whether it can be used. */
16260 if ((w->optional_new_start || window_frozen_p (w))
16261 && CHARPOS (startp) >= BEGV
16262 && CHARPOS (startp) <= ZV)
16263 {
16264 ptrdiff_t it_charpos;
16265
16266 w->optional_new_start = false;
16267 start_display (&it, w, startp);
16268 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16269 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16270 /* Record IT's position now, since line_bottom_y might change
16271 that. */
16272 it_charpos = IT_CHARPOS (it);
16273 /* Make sure we set the force_start flag only if the cursor row
16274 will be fully visible. Otherwise, the code under force_start
16275 label below will try to move point back into view, which is
16276 not what the code which sets optional_new_start wants. */
16277 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16278 && !w->force_start)
16279 {
16280 if (it_charpos == PT)
16281 w->force_start = true;
16282 /* IT may overshoot PT if text at PT is invisible. */
16283 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16284 w->force_start = true;
16285 #ifdef GLYPH_DEBUG
16286 if (w->force_start)
16287 {
16288 if (window_frozen_p (w))
16289 debug_method_add (w, "set force_start from frozen window start");
16290 else
16291 debug_method_add (w, "set force_start from optional_new_start");
16292 }
16293 #endif
16294 }
16295 }
16296
16297 force_start:
16298
16299 /* Handle case where place to start displaying has been specified,
16300 unless the specified location is outside the accessible range. */
16301 if (w->force_start)
16302 {
16303 /* We set this later on if we have to adjust point. */
16304 int new_vpos = -1;
16305
16306 w->force_start = false;
16307 w->vscroll = 0;
16308 w->window_end_valid = false;
16309
16310 /* Forget any recorded base line for line number display. */
16311 if (!buffer_unchanged_p)
16312 w->base_line_number = 0;
16313
16314 /* Redisplay the mode line. Select the buffer properly for that.
16315 Also, run the hook window-scroll-functions
16316 because we have scrolled. */
16317 /* Note, we do this after clearing force_start because
16318 if there's an error, it is better to forget about force_start
16319 than to get into an infinite loop calling the hook functions
16320 and having them get more errors. */
16321 if (!update_mode_line
16322 || ! NILP (Vwindow_scroll_functions))
16323 {
16324 update_mode_line = true;
16325 w->update_mode_line = true;
16326 startp = run_window_scroll_functions (window, startp);
16327 }
16328
16329 if (CHARPOS (startp) < BEGV)
16330 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16331 else if (CHARPOS (startp) > ZV)
16332 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16333
16334 /* Redisplay, then check if cursor has been set during the
16335 redisplay. Give up if new fonts were loaded. */
16336 /* We used to issue a CHECK_MARGINS argument to try_window here,
16337 but this causes scrolling to fail when point begins inside
16338 the scroll margin (bug#148) -- cyd */
16339 if (!try_window (window, startp, 0))
16340 {
16341 w->force_start = true;
16342 clear_glyph_matrix (w->desired_matrix);
16343 goto need_larger_matrices;
16344 }
16345
16346 if (w->cursor.vpos < 0)
16347 {
16348 /* If point does not appear, try to move point so it does
16349 appear. The desired matrix has been built above, so we
16350 can use it here. First see if point is in invisible
16351 text, and if so, move it to the first visible buffer
16352 position past that. */
16353 struct glyph_row *r = NULL;
16354 Lisp_Object invprop =
16355 get_char_property_and_overlay (make_number (PT), Qinvisible,
16356 Qnil, NULL);
16357
16358 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16359 {
16360 ptrdiff_t alt_pt;
16361 Lisp_Object invprop_end =
16362 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16363 Qnil, Qnil);
16364
16365 if (NATNUMP (invprop_end))
16366 alt_pt = XFASTINT (invprop_end);
16367 else
16368 alt_pt = ZV;
16369 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16370 NULL, 0);
16371 }
16372 if (r)
16373 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16374 else /* Give up and just move to the middle of the window. */
16375 new_vpos = window_box_height (w) / 2;
16376 }
16377
16378 if (!cursor_row_fully_visible_p (w, false, false))
16379 {
16380 /* Point does appear, but on a line partly visible at end of window.
16381 Move it back to a fully-visible line. */
16382 new_vpos = window_box_height (w);
16383 /* But if window_box_height suggests a Y coordinate that is
16384 not less than we already have, that line will clearly not
16385 be fully visible, so give up and scroll the display.
16386 This can happen when the default face uses a font whose
16387 dimensions are different from the frame's default
16388 font. */
16389 if (new_vpos >= w->cursor.y)
16390 {
16391 w->cursor.vpos = -1;
16392 clear_glyph_matrix (w->desired_matrix);
16393 goto try_to_scroll;
16394 }
16395 }
16396 else if (w->cursor.vpos >= 0)
16397 {
16398 /* Some people insist on not letting point enter the scroll
16399 margin, even though this part handles windows that didn't
16400 scroll at all. */
16401 int window_total_lines
16402 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16403 int margin = min (scroll_margin, window_total_lines / 4);
16404 int pixel_margin = margin * frame_line_height;
16405 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16406
16407 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16408 below, which finds the row to move point to, advances by
16409 the Y coordinate of the _next_ row, see the definition of
16410 MATRIX_ROW_BOTTOM_Y. */
16411 if (w->cursor.vpos < margin + header_line)
16412 {
16413 w->cursor.vpos = -1;
16414 clear_glyph_matrix (w->desired_matrix);
16415 goto try_to_scroll;
16416 }
16417 else
16418 {
16419 int window_height = window_box_height (w);
16420
16421 if (header_line)
16422 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16423 if (w->cursor.y >= window_height - pixel_margin)
16424 {
16425 w->cursor.vpos = -1;
16426 clear_glyph_matrix (w->desired_matrix);
16427 goto try_to_scroll;
16428 }
16429 }
16430 }
16431
16432 /* If we need to move point for either of the above reasons,
16433 now actually do it. */
16434 if (new_vpos >= 0)
16435 {
16436 struct glyph_row *row;
16437
16438 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16439 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16440 ++row;
16441
16442 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16443 MATRIX_ROW_START_BYTEPOS (row));
16444
16445 if (w != XWINDOW (selected_window))
16446 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16447 else if (current_buffer == old)
16448 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16449
16450 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16451
16452 /* Re-run pre-redisplay-function so it can update the region
16453 according to the new position of point. */
16454 /* Other than the cursor, w's redisplay is done so we can set its
16455 redisplay to false. Also the buffer's redisplay can be set to
16456 false, since propagate_buffer_redisplay should have already
16457 propagated its info to `w' anyway. */
16458 w->redisplay = false;
16459 XBUFFER (w->contents)->text->redisplay = false;
16460 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16461
16462 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16463 {
16464 /* pre-redisplay-function made changes (e.g. move the region)
16465 that require another round of redisplay. */
16466 clear_glyph_matrix (w->desired_matrix);
16467 if (!try_window (window, startp, 0))
16468 goto need_larger_matrices;
16469 }
16470 }
16471 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16472 {
16473 clear_glyph_matrix (w->desired_matrix);
16474 goto try_to_scroll;
16475 }
16476
16477 #ifdef GLYPH_DEBUG
16478 debug_method_add (w, "forced window start");
16479 #endif
16480 goto done;
16481 }
16482
16483 /* Handle case where text has not changed, only point, and it has
16484 not moved off the frame, and we are not retrying after hscroll.
16485 (current_matrix_up_to_date_p is true when retrying.) */
16486 if (current_matrix_up_to_date_p
16487 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16488 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16489 {
16490 switch (rc)
16491 {
16492 case CURSOR_MOVEMENT_SUCCESS:
16493 used_current_matrix_p = true;
16494 goto done;
16495
16496 case CURSOR_MOVEMENT_MUST_SCROLL:
16497 goto try_to_scroll;
16498
16499 default:
16500 emacs_abort ();
16501 }
16502 }
16503 /* If current starting point was originally the beginning of a line
16504 but no longer is, find a new starting point. */
16505 else if (w->start_at_line_beg
16506 && !(CHARPOS (startp) <= BEGV
16507 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16508 {
16509 #ifdef GLYPH_DEBUG
16510 debug_method_add (w, "recenter 1");
16511 #endif
16512 goto recenter;
16513 }
16514
16515 /* Try scrolling with try_window_id. Value is > 0 if update has
16516 been done, it is -1 if we know that the same window start will
16517 not work. It is 0 if unsuccessful for some other reason. */
16518 else if ((tem = try_window_id (w)) != 0)
16519 {
16520 #ifdef GLYPH_DEBUG
16521 debug_method_add (w, "try_window_id %d", tem);
16522 #endif
16523
16524 if (f->fonts_changed)
16525 goto need_larger_matrices;
16526 if (tem > 0)
16527 goto done;
16528
16529 /* Otherwise try_window_id has returned -1 which means that we
16530 don't want the alternative below this comment to execute. */
16531 }
16532 else if (CHARPOS (startp) >= BEGV
16533 && CHARPOS (startp) <= ZV
16534 && PT >= CHARPOS (startp)
16535 && (CHARPOS (startp) < ZV
16536 /* Avoid starting at end of buffer. */
16537 || CHARPOS (startp) == BEGV
16538 || !window_outdated (w)))
16539 {
16540 int d1, d2, d5, d6;
16541 int rtop, rbot;
16542
16543 /* If first window line is a continuation line, and window start
16544 is inside the modified region, but the first change is before
16545 current window start, we must select a new window start.
16546
16547 However, if this is the result of a down-mouse event (e.g. by
16548 extending the mouse-drag-overlay), we don't want to select a
16549 new window start, since that would change the position under
16550 the mouse, resulting in an unwanted mouse-movement rather
16551 than a simple mouse-click. */
16552 if (!w->start_at_line_beg
16553 && NILP (do_mouse_tracking)
16554 && CHARPOS (startp) > BEGV
16555 && CHARPOS (startp) > BEG + beg_unchanged
16556 && CHARPOS (startp) <= Z - end_unchanged
16557 /* Even if w->start_at_line_beg is nil, a new window may
16558 start at a line_beg, since that's how set_buffer_window
16559 sets it. So, we need to check the return value of
16560 compute_window_start_on_continuation_line. (See also
16561 bug#197). */
16562 && XMARKER (w->start)->buffer == current_buffer
16563 && compute_window_start_on_continuation_line (w)
16564 /* It doesn't make sense to force the window start like we
16565 do at label force_start if it is already known that point
16566 will not be fully visible in the resulting window, because
16567 doing so will move point from its correct position
16568 instead of scrolling the window to bring point into view.
16569 See bug#9324. */
16570 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16571 /* A very tall row could need more than the window height,
16572 in which case we accept that it is partially visible. */
16573 && (rtop != 0) == (rbot != 0))
16574 {
16575 w->force_start = true;
16576 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16577 #ifdef GLYPH_DEBUG
16578 debug_method_add (w, "recomputed window start in continuation line");
16579 #endif
16580 goto force_start;
16581 }
16582
16583 #ifdef GLYPH_DEBUG
16584 debug_method_add (w, "same window start");
16585 #endif
16586
16587 /* Try to redisplay starting at same place as before.
16588 If point has not moved off frame, accept the results. */
16589 if (!current_matrix_up_to_date_p
16590 /* Don't use try_window_reusing_current_matrix in this case
16591 because a window scroll function can have changed the
16592 buffer. */
16593 || !NILP (Vwindow_scroll_functions)
16594 || MINI_WINDOW_P (w)
16595 || !(used_current_matrix_p
16596 = try_window_reusing_current_matrix (w)))
16597 {
16598 IF_DEBUG (debug_method_add (w, "1"));
16599 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16600 /* -1 means we need to scroll.
16601 0 means we need new matrices, but fonts_changed
16602 is set in that case, so we will detect it below. */
16603 goto try_to_scroll;
16604 }
16605
16606 if (f->fonts_changed)
16607 goto need_larger_matrices;
16608
16609 if (w->cursor.vpos >= 0)
16610 {
16611 if (!just_this_one_p
16612 || current_buffer->clip_changed
16613 || BEG_UNCHANGED < CHARPOS (startp))
16614 /* Forget any recorded base line for line number display. */
16615 w->base_line_number = 0;
16616
16617 if (!cursor_row_fully_visible_p (w, true, false))
16618 {
16619 clear_glyph_matrix (w->desired_matrix);
16620 last_line_misfit = true;
16621 }
16622 /* Drop through and scroll. */
16623 else
16624 goto done;
16625 }
16626 else
16627 clear_glyph_matrix (w->desired_matrix);
16628 }
16629
16630 try_to_scroll:
16631
16632 /* Redisplay the mode line. Select the buffer properly for that. */
16633 if (!update_mode_line)
16634 {
16635 update_mode_line = true;
16636 w->update_mode_line = true;
16637 }
16638
16639 /* Try to scroll by specified few lines. */
16640 if ((scroll_conservatively
16641 || emacs_scroll_step
16642 || temp_scroll_step
16643 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16644 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16645 && CHARPOS (startp) >= BEGV
16646 && CHARPOS (startp) <= ZV)
16647 {
16648 /* The function returns -1 if new fonts were loaded, 1 if
16649 successful, 0 if not successful. */
16650 int ss = try_scrolling (window, just_this_one_p,
16651 scroll_conservatively,
16652 emacs_scroll_step,
16653 temp_scroll_step, last_line_misfit);
16654 switch (ss)
16655 {
16656 case SCROLLING_SUCCESS:
16657 goto done;
16658
16659 case SCROLLING_NEED_LARGER_MATRICES:
16660 goto need_larger_matrices;
16661
16662 case SCROLLING_FAILED:
16663 break;
16664
16665 default:
16666 emacs_abort ();
16667 }
16668 }
16669
16670 /* Finally, just choose a place to start which positions point
16671 according to user preferences. */
16672
16673 recenter:
16674
16675 #ifdef GLYPH_DEBUG
16676 debug_method_add (w, "recenter");
16677 #endif
16678
16679 /* Forget any previously recorded base line for line number display. */
16680 if (!buffer_unchanged_p)
16681 w->base_line_number = 0;
16682
16683 /* Determine the window start relative to point. */
16684 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16685 it.current_y = it.last_visible_y;
16686 if (centering_position < 0)
16687 {
16688 int window_total_lines
16689 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16690 int margin
16691 = scroll_margin > 0
16692 ? min (scroll_margin, window_total_lines / 4)
16693 : 0;
16694 ptrdiff_t margin_pos = CHARPOS (startp);
16695 Lisp_Object aggressive;
16696 bool scrolling_up;
16697
16698 /* If there is a scroll margin at the top of the window, find
16699 its character position. */
16700 if (margin
16701 /* Cannot call start_display if startp is not in the
16702 accessible region of the buffer. This can happen when we
16703 have just switched to a different buffer and/or changed
16704 its restriction. In that case, startp is initialized to
16705 the character position 1 (BEGV) because we did not yet
16706 have chance to display the buffer even once. */
16707 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16708 {
16709 struct it it1;
16710 void *it1data = NULL;
16711
16712 SAVE_IT (it1, it, it1data);
16713 start_display (&it1, w, startp);
16714 move_it_vertically (&it1, margin * frame_line_height);
16715 margin_pos = IT_CHARPOS (it1);
16716 RESTORE_IT (&it, &it, it1data);
16717 }
16718 scrolling_up = PT > margin_pos;
16719 aggressive =
16720 scrolling_up
16721 ? BVAR (current_buffer, scroll_up_aggressively)
16722 : BVAR (current_buffer, scroll_down_aggressively);
16723
16724 if (!MINI_WINDOW_P (w)
16725 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16726 {
16727 int pt_offset = 0;
16728
16729 /* Setting scroll-conservatively overrides
16730 scroll-*-aggressively. */
16731 if (!scroll_conservatively && NUMBERP (aggressive))
16732 {
16733 double float_amount = XFLOATINT (aggressive);
16734
16735 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16736 if (pt_offset == 0 && float_amount > 0)
16737 pt_offset = 1;
16738 if (pt_offset && margin > 0)
16739 margin -= 1;
16740 }
16741 /* Compute how much to move the window start backward from
16742 point so that point will be displayed where the user
16743 wants it. */
16744 if (scrolling_up)
16745 {
16746 centering_position = it.last_visible_y;
16747 if (pt_offset)
16748 centering_position -= pt_offset;
16749 centering_position -=
16750 (frame_line_height * (1 + margin + last_line_misfit)
16751 + WINDOW_HEADER_LINE_HEIGHT (w));
16752 /* Don't let point enter the scroll margin near top of
16753 the window. */
16754 if (centering_position < margin * frame_line_height)
16755 centering_position = margin * frame_line_height;
16756 }
16757 else
16758 centering_position = margin * frame_line_height + pt_offset;
16759 }
16760 else
16761 /* Set the window start half the height of the window backward
16762 from point. */
16763 centering_position = window_box_height (w) / 2;
16764 }
16765 move_it_vertically_backward (&it, centering_position);
16766
16767 eassert (IT_CHARPOS (it) >= BEGV);
16768
16769 /* The function move_it_vertically_backward may move over more
16770 than the specified y-distance. If it->w is small, e.g. a
16771 mini-buffer window, we may end up in front of the window's
16772 display area. Start displaying at the start of the line
16773 containing PT in this case. */
16774 if (it.current_y <= 0)
16775 {
16776 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16777 move_it_vertically_backward (&it, 0);
16778 it.current_y = 0;
16779 }
16780
16781 it.current_x = it.hpos = 0;
16782
16783 /* Set the window start position here explicitly, to avoid an
16784 infinite loop in case the functions in window-scroll-functions
16785 get errors. */
16786 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16787
16788 /* Run scroll hooks. */
16789 startp = run_window_scroll_functions (window, it.current.pos);
16790
16791 /* Redisplay the window. */
16792 bool use_desired_matrix = false;
16793 if (!current_matrix_up_to_date_p
16794 || windows_or_buffers_changed
16795 || f->cursor_type_changed
16796 /* Don't use try_window_reusing_current_matrix in this case
16797 because it can have changed the buffer. */
16798 || !NILP (Vwindow_scroll_functions)
16799 || !just_this_one_p
16800 || MINI_WINDOW_P (w)
16801 || !(used_current_matrix_p
16802 = try_window_reusing_current_matrix (w)))
16803 use_desired_matrix = (try_window (window, startp, 0) == 1);
16804
16805 /* If new fonts have been loaded (due to fontsets), give up. We
16806 have to start a new redisplay since we need to re-adjust glyph
16807 matrices. */
16808 if (f->fonts_changed)
16809 goto need_larger_matrices;
16810
16811 /* If cursor did not appear assume that the middle of the window is
16812 in the first line of the window. Do it again with the next line.
16813 (Imagine a window of height 100, displaying two lines of height
16814 60. Moving back 50 from it->last_visible_y will end in the first
16815 line.) */
16816 if (w->cursor.vpos < 0)
16817 {
16818 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16819 {
16820 clear_glyph_matrix (w->desired_matrix);
16821 move_it_by_lines (&it, 1);
16822 try_window (window, it.current.pos, 0);
16823 }
16824 else if (PT < IT_CHARPOS (it))
16825 {
16826 clear_glyph_matrix (w->desired_matrix);
16827 move_it_by_lines (&it, -1);
16828 try_window (window, it.current.pos, 0);
16829 }
16830 else
16831 {
16832 /* Not much we can do about it. */
16833 }
16834 }
16835
16836 /* Consider the following case: Window starts at BEGV, there is
16837 invisible, intangible text at BEGV, so that display starts at
16838 some point START > BEGV. It can happen that we are called with
16839 PT somewhere between BEGV and START. Try to handle that case,
16840 and similar ones. */
16841 if (w->cursor.vpos < 0)
16842 {
16843 /* Prefer the desired matrix to the current matrix, if possible,
16844 in the fallback calculations below. This is because using
16845 the current matrix might completely goof, e.g. if its first
16846 row is after point. */
16847 struct glyph_matrix *matrix =
16848 use_desired_matrix ? w->desired_matrix : w->current_matrix;
16849 /* First, try locating the proper glyph row for PT. */
16850 struct glyph_row *row =
16851 row_containing_pos (w, PT, matrix->rows, NULL, 0);
16852
16853 /* Sometimes point is at the beginning of invisible text that is
16854 before the 1st character displayed in the row. In that case,
16855 row_containing_pos fails to find the row, because no glyphs
16856 with appropriate buffer positions are present in the row.
16857 Therefore, we next try to find the row which shows the 1st
16858 position after the invisible text. */
16859 if (!row)
16860 {
16861 Lisp_Object val =
16862 get_char_property_and_overlay (make_number (PT), Qinvisible,
16863 Qnil, NULL);
16864
16865 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
16866 {
16867 ptrdiff_t alt_pos;
16868 Lisp_Object invis_end =
16869 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16870 Qnil, Qnil);
16871
16872 if (NATNUMP (invis_end))
16873 alt_pos = XFASTINT (invis_end);
16874 else
16875 alt_pos = ZV;
16876 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
16877 }
16878 }
16879 /* Finally, fall back on the first row of the window after the
16880 header line (if any). This is slightly better than not
16881 displaying the cursor at all. */
16882 if (!row)
16883 {
16884 row = matrix->rows;
16885 if (row->mode_line_p)
16886 ++row;
16887 }
16888 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
16889 }
16890
16891 if (!cursor_row_fully_visible_p (w, false, false))
16892 {
16893 /* If vscroll is enabled, disable it and try again. */
16894 if (w->vscroll)
16895 {
16896 w->vscroll = 0;
16897 clear_glyph_matrix (w->desired_matrix);
16898 goto recenter;
16899 }
16900
16901 /* Users who set scroll-conservatively to a large number want
16902 point just above/below the scroll margin. If we ended up
16903 with point's row partially visible, move the window start to
16904 make that row fully visible and out of the margin. */
16905 if (scroll_conservatively > SCROLL_LIMIT)
16906 {
16907 int window_total_lines
16908 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16909 int margin =
16910 scroll_margin > 0
16911 ? min (scroll_margin, window_total_lines / 4)
16912 : 0;
16913 bool move_down = w->cursor.vpos >= window_total_lines / 2;
16914
16915 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16916 clear_glyph_matrix (w->desired_matrix);
16917 if (1 == try_window (window, it.current.pos,
16918 TRY_WINDOW_CHECK_MARGINS))
16919 goto done;
16920 }
16921
16922 /* If centering point failed to make the whole line visible,
16923 put point at the top instead. That has to make the whole line
16924 visible, if it can be done. */
16925 if (centering_position == 0)
16926 goto done;
16927
16928 clear_glyph_matrix (w->desired_matrix);
16929 centering_position = 0;
16930 goto recenter;
16931 }
16932
16933 done:
16934
16935 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16936 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16937 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16938
16939 /* Display the mode line, if we must. */
16940 if ((update_mode_line
16941 /* If window not full width, must redo its mode line
16942 if (a) the window to its side is being redone and
16943 (b) we do a frame-based redisplay. This is a consequence
16944 of how inverted lines are drawn in frame-based redisplay. */
16945 || (!just_this_one_p
16946 && !FRAME_WINDOW_P (f)
16947 && !WINDOW_FULL_WIDTH_P (w))
16948 /* Line number to display. */
16949 || w->base_line_pos > 0
16950 /* Column number is displayed and different from the one displayed. */
16951 || (w->column_number_displayed != -1
16952 && (w->column_number_displayed != current_column ())))
16953 /* This means that the window has a mode line. */
16954 && (WINDOW_WANTS_MODELINE_P (w)
16955 || WINDOW_WANTS_HEADER_LINE_P (w)))
16956 {
16957
16958 display_mode_lines (w);
16959
16960 /* If mode line height has changed, arrange for a thorough
16961 immediate redisplay using the correct mode line height. */
16962 if (WINDOW_WANTS_MODELINE_P (w)
16963 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16964 {
16965 f->fonts_changed = true;
16966 w->mode_line_height = -1;
16967 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16968 = DESIRED_MODE_LINE_HEIGHT (w);
16969 }
16970
16971 /* If header line height has changed, arrange for a thorough
16972 immediate redisplay using the correct header line height. */
16973 if (WINDOW_WANTS_HEADER_LINE_P (w)
16974 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16975 {
16976 f->fonts_changed = true;
16977 w->header_line_height = -1;
16978 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16979 = DESIRED_HEADER_LINE_HEIGHT (w);
16980 }
16981
16982 if (f->fonts_changed)
16983 goto need_larger_matrices;
16984 }
16985
16986 if (!line_number_displayed && w->base_line_pos != -1)
16987 {
16988 w->base_line_pos = 0;
16989 w->base_line_number = 0;
16990 }
16991
16992 finish_menu_bars:
16993
16994 /* When we reach a frame's selected window, redo the frame's menu
16995 bar and the frame's title. */
16996 if (update_mode_line
16997 && EQ (FRAME_SELECTED_WINDOW (f), window))
16998 {
16999 bool redisplay_menu_p;
17000
17001 if (FRAME_WINDOW_P (f))
17002 {
17003 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17004 || defined (HAVE_NS) || defined (USE_GTK)
17005 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17006 #else
17007 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17008 #endif
17009 }
17010 else
17011 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17012
17013 if (redisplay_menu_p)
17014 display_menu_bar (w);
17015
17016 #ifdef HAVE_WINDOW_SYSTEM
17017 if (FRAME_WINDOW_P (f))
17018 {
17019 #if defined (USE_GTK) || defined (HAVE_NS)
17020 if (FRAME_EXTERNAL_TOOL_BAR (f))
17021 redisplay_tool_bar (f);
17022 #else
17023 if (WINDOWP (f->tool_bar_window)
17024 && (FRAME_TOOL_BAR_LINES (f) > 0
17025 || !NILP (Vauto_resize_tool_bars))
17026 && redisplay_tool_bar (f))
17027 ignore_mouse_drag_p = true;
17028 #endif
17029 }
17030 x_consider_frame_title (w->frame);
17031 #endif
17032 }
17033
17034 #ifdef HAVE_WINDOW_SYSTEM
17035 if (FRAME_WINDOW_P (f)
17036 && update_window_fringes (w, (just_this_one_p
17037 || (!used_current_matrix_p && !overlay_arrow_seen)
17038 || w->pseudo_window_p)))
17039 {
17040 update_begin (f);
17041 block_input ();
17042 if (draw_window_fringes (w, true))
17043 {
17044 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17045 x_draw_right_divider (w);
17046 else
17047 x_draw_vertical_border (w);
17048 }
17049 unblock_input ();
17050 update_end (f);
17051 }
17052
17053 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17054 x_draw_bottom_divider (w);
17055 #endif /* HAVE_WINDOW_SYSTEM */
17056
17057 /* We go to this label, with fonts_changed set, if it is
17058 necessary to try again using larger glyph matrices.
17059 We have to redeem the scroll bar even in this case,
17060 because the loop in redisplay_internal expects that. */
17061 need_larger_matrices:
17062 ;
17063 finish_scroll_bars:
17064
17065 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17066 {
17067 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17068 /* Set the thumb's position and size. */
17069 set_vertical_scroll_bar (w);
17070
17071 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17072 /* Set the thumb's position and size. */
17073 set_horizontal_scroll_bar (w);
17074
17075 /* Note that we actually used the scroll bar attached to this
17076 window, so it shouldn't be deleted at the end of redisplay. */
17077 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17078 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17079 }
17080
17081 /* Restore current_buffer and value of point in it. The window
17082 update may have changed the buffer, so first make sure `opoint'
17083 is still valid (Bug#6177). */
17084 if (CHARPOS (opoint) < BEGV)
17085 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17086 else if (CHARPOS (opoint) > ZV)
17087 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17088 else
17089 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17090
17091 set_buffer_internal_1 (old);
17092 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17093 shorter. This can be caused by log truncation in *Messages*. */
17094 if (CHARPOS (lpoint) <= ZV)
17095 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17096
17097 unbind_to (count, Qnil);
17098 }
17099
17100
17101 /* Build the complete desired matrix of WINDOW with a window start
17102 buffer position POS.
17103
17104 Value is 1 if successful. It is zero if fonts were loaded during
17105 redisplay which makes re-adjusting glyph matrices necessary, and -1
17106 if point would appear in the scroll margins.
17107 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17108 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17109 set in FLAGS.) */
17110
17111 int
17112 try_window (Lisp_Object window, struct text_pos pos, int flags)
17113 {
17114 struct window *w = XWINDOW (window);
17115 struct it it;
17116 struct glyph_row *last_text_row = NULL;
17117 struct frame *f = XFRAME (w->frame);
17118 int frame_line_height = default_line_pixel_height (w);
17119
17120 /* Make POS the new window start. */
17121 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17122
17123 /* Mark cursor position as unknown. No overlay arrow seen. */
17124 w->cursor.vpos = -1;
17125 overlay_arrow_seen = false;
17126
17127 /* Initialize iterator and info to start at POS. */
17128 start_display (&it, w, pos);
17129 it.glyph_row->reversed_p = false;
17130
17131 /* Display all lines of W. */
17132 while (it.current_y < it.last_visible_y)
17133 {
17134 if (display_line (&it))
17135 last_text_row = it.glyph_row - 1;
17136 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17137 return 0;
17138 }
17139
17140 /* Don't let the cursor end in the scroll margins. */
17141 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17142 && !MINI_WINDOW_P (w))
17143 {
17144 int this_scroll_margin;
17145 int window_total_lines
17146 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17147
17148 if (scroll_margin > 0)
17149 {
17150 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
17151 this_scroll_margin *= frame_line_height;
17152 }
17153 else
17154 this_scroll_margin = 0;
17155
17156 if ((w->cursor.y >= 0 /* not vscrolled */
17157 && w->cursor.y < this_scroll_margin
17158 && CHARPOS (pos) > BEGV
17159 && IT_CHARPOS (it) < ZV)
17160 /* rms: considering make_cursor_line_fully_visible_p here
17161 seems to give wrong results. We don't want to recenter
17162 when the last line is partly visible, we want to allow
17163 that case to be handled in the usual way. */
17164 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
17165 {
17166 w->cursor.vpos = -1;
17167 clear_glyph_matrix (w->desired_matrix);
17168 return -1;
17169 }
17170 }
17171
17172 /* If bottom moved off end of frame, change mode line percentage. */
17173 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
17174 w->update_mode_line = true;
17175
17176 /* Set window_end_pos to the offset of the last character displayed
17177 on the window from the end of current_buffer. Set
17178 window_end_vpos to its row number. */
17179 if (last_text_row)
17180 {
17181 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17182 adjust_window_ends (w, last_text_row, false);
17183 eassert
17184 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17185 w->window_end_vpos)));
17186 }
17187 else
17188 {
17189 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17190 w->window_end_pos = Z - ZV;
17191 w->window_end_vpos = 0;
17192 }
17193
17194 /* But that is not valid info until redisplay finishes. */
17195 w->window_end_valid = false;
17196 return 1;
17197 }
17198
17199
17200 \f
17201 /************************************************************************
17202 Window redisplay reusing current matrix when buffer has not changed
17203 ************************************************************************/
17204
17205 /* Try redisplay of window W showing an unchanged buffer with a
17206 different window start than the last time it was displayed by
17207 reusing its current matrix. Value is true if successful.
17208 W->start is the new window start. */
17209
17210 static bool
17211 try_window_reusing_current_matrix (struct window *w)
17212 {
17213 struct frame *f = XFRAME (w->frame);
17214 struct glyph_row *bottom_row;
17215 struct it it;
17216 struct run run;
17217 struct text_pos start, new_start;
17218 int nrows_scrolled, i;
17219 struct glyph_row *last_text_row;
17220 struct glyph_row *last_reused_text_row;
17221 struct glyph_row *start_row;
17222 int start_vpos, min_y, max_y;
17223
17224 #ifdef GLYPH_DEBUG
17225 if (inhibit_try_window_reusing)
17226 return false;
17227 #endif
17228
17229 if (/* This function doesn't handle terminal frames. */
17230 !FRAME_WINDOW_P (f)
17231 /* Don't try to reuse the display if windows have been split
17232 or such. */
17233 || windows_or_buffers_changed
17234 || f->cursor_type_changed)
17235 return false;
17236
17237 /* Can't do this if showing trailing whitespace. */
17238 if (!NILP (Vshow_trailing_whitespace))
17239 return false;
17240
17241 /* If top-line visibility has changed, give up. */
17242 if (WINDOW_WANTS_HEADER_LINE_P (w)
17243 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17244 return false;
17245
17246 /* Give up if old or new display is scrolled vertically. We could
17247 make this function handle this, but right now it doesn't. */
17248 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17249 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17250 return false;
17251
17252 /* The variable new_start now holds the new window start. The old
17253 start `start' can be determined from the current matrix. */
17254 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17255 start = start_row->minpos;
17256 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17257
17258 /* Clear the desired matrix for the display below. */
17259 clear_glyph_matrix (w->desired_matrix);
17260
17261 if (CHARPOS (new_start) <= CHARPOS (start))
17262 {
17263 /* Don't use this method if the display starts with an ellipsis
17264 displayed for invisible text. It's not easy to handle that case
17265 below, and it's certainly not worth the effort since this is
17266 not a frequent case. */
17267 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17268 return false;
17269
17270 IF_DEBUG (debug_method_add (w, "twu1"));
17271
17272 /* Display up to a row that can be reused. The variable
17273 last_text_row is set to the last row displayed that displays
17274 text. Note that it.vpos == 0 if or if not there is a
17275 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17276 start_display (&it, w, new_start);
17277 w->cursor.vpos = -1;
17278 last_text_row = last_reused_text_row = NULL;
17279
17280 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17281 {
17282 /* If we have reached into the characters in the START row,
17283 that means the line boundaries have changed. So we
17284 can't start copying with the row START. Maybe it will
17285 work to start copying with the following row. */
17286 while (IT_CHARPOS (it) > CHARPOS (start))
17287 {
17288 /* Advance to the next row as the "start". */
17289 start_row++;
17290 start = start_row->minpos;
17291 /* If there are no more rows to try, or just one, give up. */
17292 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17293 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17294 || CHARPOS (start) == ZV)
17295 {
17296 clear_glyph_matrix (w->desired_matrix);
17297 return false;
17298 }
17299
17300 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17301 }
17302 /* If we have reached alignment, we can copy the rest of the
17303 rows. */
17304 if (IT_CHARPOS (it) == CHARPOS (start)
17305 /* Don't accept "alignment" inside a display vector,
17306 since start_row could have started in the middle of
17307 that same display vector (thus their character
17308 positions match), and we have no way of telling if
17309 that is the case. */
17310 && it.current.dpvec_index < 0)
17311 break;
17312
17313 it.glyph_row->reversed_p = false;
17314 if (display_line (&it))
17315 last_text_row = it.glyph_row - 1;
17316
17317 }
17318
17319 /* A value of current_y < last_visible_y means that we stopped
17320 at the previous window start, which in turn means that we
17321 have at least one reusable row. */
17322 if (it.current_y < it.last_visible_y)
17323 {
17324 struct glyph_row *row;
17325
17326 /* IT.vpos always starts from 0; it counts text lines. */
17327 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17328
17329 /* Find PT if not already found in the lines displayed. */
17330 if (w->cursor.vpos < 0)
17331 {
17332 int dy = it.current_y - start_row->y;
17333
17334 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17335 row = row_containing_pos (w, PT, row, NULL, dy);
17336 if (row)
17337 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17338 dy, nrows_scrolled);
17339 else
17340 {
17341 clear_glyph_matrix (w->desired_matrix);
17342 return false;
17343 }
17344 }
17345
17346 /* Scroll the display. Do it before the current matrix is
17347 changed. The problem here is that update has not yet
17348 run, i.e. part of the current matrix is not up to date.
17349 scroll_run_hook will clear the cursor, and use the
17350 current matrix to get the height of the row the cursor is
17351 in. */
17352 run.current_y = start_row->y;
17353 run.desired_y = it.current_y;
17354 run.height = it.last_visible_y - it.current_y;
17355
17356 if (run.height > 0 && run.current_y != run.desired_y)
17357 {
17358 update_begin (f);
17359 FRAME_RIF (f)->update_window_begin_hook (w);
17360 FRAME_RIF (f)->clear_window_mouse_face (w);
17361 FRAME_RIF (f)->scroll_run_hook (w, &run);
17362 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17363 update_end (f);
17364 }
17365
17366 /* Shift current matrix down by nrows_scrolled lines. */
17367 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17368 rotate_matrix (w->current_matrix,
17369 start_vpos,
17370 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17371 nrows_scrolled);
17372
17373 /* Disable lines that must be updated. */
17374 for (i = 0; i < nrows_scrolled; ++i)
17375 (start_row + i)->enabled_p = false;
17376
17377 /* Re-compute Y positions. */
17378 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17379 max_y = it.last_visible_y;
17380 for (row = start_row + nrows_scrolled;
17381 row < bottom_row;
17382 ++row)
17383 {
17384 row->y = it.current_y;
17385 row->visible_height = row->height;
17386
17387 if (row->y < min_y)
17388 row->visible_height -= min_y - row->y;
17389 if (row->y + row->height > max_y)
17390 row->visible_height -= row->y + row->height - max_y;
17391 if (row->fringe_bitmap_periodic_p)
17392 row->redraw_fringe_bitmaps_p = true;
17393
17394 it.current_y += row->height;
17395
17396 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17397 last_reused_text_row = row;
17398 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17399 break;
17400 }
17401
17402 /* Disable lines in the current matrix which are now
17403 below the window. */
17404 for (++row; row < bottom_row; ++row)
17405 row->enabled_p = row->mode_line_p = false;
17406 }
17407
17408 /* Update window_end_pos etc.; last_reused_text_row is the last
17409 reused row from the current matrix containing text, if any.
17410 The value of last_text_row is the last displayed line
17411 containing text. */
17412 if (last_reused_text_row)
17413 adjust_window_ends (w, last_reused_text_row, true);
17414 else if (last_text_row)
17415 adjust_window_ends (w, last_text_row, false);
17416 else
17417 {
17418 /* This window must be completely empty. */
17419 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17420 w->window_end_pos = Z - ZV;
17421 w->window_end_vpos = 0;
17422 }
17423 w->window_end_valid = false;
17424
17425 /* Update hint: don't try scrolling again in update_window. */
17426 w->desired_matrix->no_scrolling_p = true;
17427
17428 #ifdef GLYPH_DEBUG
17429 debug_method_add (w, "try_window_reusing_current_matrix 1");
17430 #endif
17431 return true;
17432 }
17433 else if (CHARPOS (new_start) > CHARPOS (start))
17434 {
17435 struct glyph_row *pt_row, *row;
17436 struct glyph_row *first_reusable_row;
17437 struct glyph_row *first_row_to_display;
17438 int dy;
17439 int yb = window_text_bottom_y (w);
17440
17441 /* Find the row starting at new_start, if there is one. Don't
17442 reuse a partially visible line at the end. */
17443 first_reusable_row = start_row;
17444 while (first_reusable_row->enabled_p
17445 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17446 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17447 < CHARPOS (new_start)))
17448 ++first_reusable_row;
17449
17450 /* Give up if there is no row to reuse. */
17451 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17452 || !first_reusable_row->enabled_p
17453 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17454 != CHARPOS (new_start)))
17455 return false;
17456
17457 /* We can reuse fully visible rows beginning with
17458 first_reusable_row to the end of the window. Set
17459 first_row_to_display to the first row that cannot be reused.
17460 Set pt_row to the row containing point, if there is any. */
17461 pt_row = NULL;
17462 for (first_row_to_display = first_reusable_row;
17463 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17464 ++first_row_to_display)
17465 {
17466 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17467 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17468 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17469 && first_row_to_display->ends_at_zv_p
17470 && pt_row == NULL)))
17471 pt_row = first_row_to_display;
17472 }
17473
17474 /* Start displaying at the start of first_row_to_display. */
17475 eassert (first_row_to_display->y < yb);
17476 init_to_row_start (&it, w, first_row_to_display);
17477
17478 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17479 - start_vpos);
17480 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17481 - nrows_scrolled);
17482 it.current_y = (first_row_to_display->y - first_reusable_row->y
17483 + WINDOW_HEADER_LINE_HEIGHT (w));
17484
17485 /* Display lines beginning with first_row_to_display in the
17486 desired matrix. Set last_text_row to the last row displayed
17487 that displays text. */
17488 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17489 if (pt_row == NULL)
17490 w->cursor.vpos = -1;
17491 last_text_row = NULL;
17492 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17493 if (display_line (&it))
17494 last_text_row = it.glyph_row - 1;
17495
17496 /* If point is in a reused row, adjust y and vpos of the cursor
17497 position. */
17498 if (pt_row)
17499 {
17500 w->cursor.vpos -= nrows_scrolled;
17501 w->cursor.y -= first_reusable_row->y - start_row->y;
17502 }
17503
17504 /* Give up if point isn't in a row displayed or reused. (This
17505 also handles the case where w->cursor.vpos < nrows_scrolled
17506 after the calls to display_line, which can happen with scroll
17507 margins. See bug#1295.) */
17508 if (w->cursor.vpos < 0)
17509 {
17510 clear_glyph_matrix (w->desired_matrix);
17511 return false;
17512 }
17513
17514 /* Scroll the display. */
17515 run.current_y = first_reusable_row->y;
17516 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17517 run.height = it.last_visible_y - run.current_y;
17518 dy = run.current_y - run.desired_y;
17519
17520 if (run.height)
17521 {
17522 update_begin (f);
17523 FRAME_RIF (f)->update_window_begin_hook (w);
17524 FRAME_RIF (f)->clear_window_mouse_face (w);
17525 FRAME_RIF (f)->scroll_run_hook (w, &run);
17526 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17527 update_end (f);
17528 }
17529
17530 /* Adjust Y positions of reused rows. */
17531 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17532 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17533 max_y = it.last_visible_y;
17534 for (row = first_reusable_row; row < first_row_to_display; ++row)
17535 {
17536 row->y -= dy;
17537 row->visible_height = row->height;
17538 if (row->y < min_y)
17539 row->visible_height -= min_y - row->y;
17540 if (row->y + row->height > max_y)
17541 row->visible_height -= row->y + row->height - max_y;
17542 if (row->fringe_bitmap_periodic_p)
17543 row->redraw_fringe_bitmaps_p = true;
17544 }
17545
17546 /* Scroll the current matrix. */
17547 eassert (nrows_scrolled > 0);
17548 rotate_matrix (w->current_matrix,
17549 start_vpos,
17550 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17551 -nrows_scrolled);
17552
17553 /* Disable rows not reused. */
17554 for (row -= nrows_scrolled; row < bottom_row; ++row)
17555 row->enabled_p = false;
17556
17557 /* Point may have moved to a different line, so we cannot assume that
17558 the previous cursor position is valid; locate the correct row. */
17559 if (pt_row)
17560 {
17561 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17562 row < bottom_row
17563 && PT >= MATRIX_ROW_END_CHARPOS (row)
17564 && !row->ends_at_zv_p;
17565 row++)
17566 {
17567 w->cursor.vpos++;
17568 w->cursor.y = row->y;
17569 }
17570 if (row < bottom_row)
17571 {
17572 /* Can't simply scan the row for point with
17573 bidi-reordered glyph rows. Let set_cursor_from_row
17574 figure out where to put the cursor, and if it fails,
17575 give up. */
17576 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17577 {
17578 if (!set_cursor_from_row (w, row, w->current_matrix,
17579 0, 0, 0, 0))
17580 {
17581 clear_glyph_matrix (w->desired_matrix);
17582 return false;
17583 }
17584 }
17585 else
17586 {
17587 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17588 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17589
17590 for (; glyph < end
17591 && (!BUFFERP (glyph->object)
17592 || glyph->charpos < PT);
17593 glyph++)
17594 {
17595 w->cursor.hpos++;
17596 w->cursor.x += glyph->pixel_width;
17597 }
17598 }
17599 }
17600 }
17601
17602 /* Adjust window end. A null value of last_text_row means that
17603 the window end is in reused rows which in turn means that
17604 only its vpos can have changed. */
17605 if (last_text_row)
17606 adjust_window_ends (w, last_text_row, false);
17607 else
17608 w->window_end_vpos -= nrows_scrolled;
17609
17610 w->window_end_valid = false;
17611 w->desired_matrix->no_scrolling_p = true;
17612
17613 #ifdef GLYPH_DEBUG
17614 debug_method_add (w, "try_window_reusing_current_matrix 2");
17615 #endif
17616 return true;
17617 }
17618
17619 return false;
17620 }
17621
17622
17623 \f
17624 /************************************************************************
17625 Window redisplay reusing current matrix when buffer has changed
17626 ************************************************************************/
17627
17628 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17629 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17630 ptrdiff_t *, ptrdiff_t *);
17631 static struct glyph_row *
17632 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17633 struct glyph_row *);
17634
17635
17636 /* Return the last row in MATRIX displaying text. If row START is
17637 non-null, start searching with that row. IT gives the dimensions
17638 of the display. Value is null if matrix is empty; otherwise it is
17639 a pointer to the row found. */
17640
17641 static struct glyph_row *
17642 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17643 struct glyph_row *start)
17644 {
17645 struct glyph_row *row, *row_found;
17646
17647 /* Set row_found to the last row in IT->w's current matrix
17648 displaying text. The loop looks funny but think of partially
17649 visible lines. */
17650 row_found = NULL;
17651 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17652 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17653 {
17654 eassert (row->enabled_p);
17655 row_found = row;
17656 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17657 break;
17658 ++row;
17659 }
17660
17661 return row_found;
17662 }
17663
17664
17665 /* Return the last row in the current matrix of W that is not affected
17666 by changes at the start of current_buffer that occurred since W's
17667 current matrix was built. Value is null if no such row exists.
17668
17669 BEG_UNCHANGED us the number of characters unchanged at the start of
17670 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17671 first changed character in current_buffer. Characters at positions <
17672 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17673 when the current matrix was built. */
17674
17675 static struct glyph_row *
17676 find_last_unchanged_at_beg_row (struct window *w)
17677 {
17678 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17679 struct glyph_row *row;
17680 struct glyph_row *row_found = NULL;
17681 int yb = window_text_bottom_y (w);
17682
17683 /* Find the last row displaying unchanged text. */
17684 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17685 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17686 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17687 ++row)
17688 {
17689 if (/* If row ends before first_changed_pos, it is unchanged,
17690 except in some case. */
17691 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17692 /* When row ends in ZV and we write at ZV it is not
17693 unchanged. */
17694 && !row->ends_at_zv_p
17695 /* When first_changed_pos is the end of a continued line,
17696 row is not unchanged because it may be no longer
17697 continued. */
17698 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17699 && (row->continued_p
17700 || row->exact_window_width_line_p))
17701 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17702 needs to be recomputed, so don't consider this row as
17703 unchanged. This happens when the last line was
17704 bidi-reordered and was killed immediately before this
17705 redisplay cycle. In that case, ROW->end stores the
17706 buffer position of the first visual-order character of
17707 the killed text, which is now beyond ZV. */
17708 && CHARPOS (row->end.pos) <= ZV)
17709 row_found = row;
17710
17711 /* Stop if last visible row. */
17712 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17713 break;
17714 }
17715
17716 return row_found;
17717 }
17718
17719
17720 /* Find the first glyph row in the current matrix of W that is not
17721 affected by changes at the end of current_buffer since the
17722 time W's current matrix was built.
17723
17724 Return in *DELTA the number of chars by which buffer positions in
17725 unchanged text at the end of current_buffer must be adjusted.
17726
17727 Return in *DELTA_BYTES the corresponding number of bytes.
17728
17729 Value is null if no such row exists, i.e. all rows are affected by
17730 changes. */
17731
17732 static struct glyph_row *
17733 find_first_unchanged_at_end_row (struct window *w,
17734 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17735 {
17736 struct glyph_row *row;
17737 struct glyph_row *row_found = NULL;
17738
17739 *delta = *delta_bytes = 0;
17740
17741 /* Display must not have been paused, otherwise the current matrix
17742 is not up to date. */
17743 eassert (w->window_end_valid);
17744
17745 /* A value of window_end_pos >= END_UNCHANGED means that the window
17746 end is in the range of changed text. If so, there is no
17747 unchanged row at the end of W's current matrix. */
17748 if (w->window_end_pos >= END_UNCHANGED)
17749 return NULL;
17750
17751 /* Set row to the last row in W's current matrix displaying text. */
17752 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17753
17754 /* If matrix is entirely empty, no unchanged row exists. */
17755 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17756 {
17757 /* The value of row is the last glyph row in the matrix having a
17758 meaningful buffer position in it. The end position of row
17759 corresponds to window_end_pos. This allows us to translate
17760 buffer positions in the current matrix to current buffer
17761 positions for characters not in changed text. */
17762 ptrdiff_t Z_old =
17763 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17764 ptrdiff_t Z_BYTE_old =
17765 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17766 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17767 struct glyph_row *first_text_row
17768 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17769
17770 *delta = Z - Z_old;
17771 *delta_bytes = Z_BYTE - Z_BYTE_old;
17772
17773 /* Set last_unchanged_pos to the buffer position of the last
17774 character in the buffer that has not been changed. Z is the
17775 index + 1 of the last character in current_buffer, i.e. by
17776 subtracting END_UNCHANGED we get the index of the last
17777 unchanged character, and we have to add BEG to get its buffer
17778 position. */
17779 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17780 last_unchanged_pos_old = last_unchanged_pos - *delta;
17781
17782 /* Search backward from ROW for a row displaying a line that
17783 starts at a minimum position >= last_unchanged_pos_old. */
17784 for (; row > first_text_row; --row)
17785 {
17786 /* This used to abort, but it can happen.
17787 It is ok to just stop the search instead here. KFS. */
17788 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17789 break;
17790
17791 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17792 row_found = row;
17793 }
17794 }
17795
17796 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17797
17798 return row_found;
17799 }
17800
17801
17802 /* Make sure that glyph rows in the current matrix of window W
17803 reference the same glyph memory as corresponding rows in the
17804 frame's frame matrix. This function is called after scrolling W's
17805 current matrix on a terminal frame in try_window_id and
17806 try_window_reusing_current_matrix. */
17807
17808 static void
17809 sync_frame_with_window_matrix_rows (struct window *w)
17810 {
17811 struct frame *f = XFRAME (w->frame);
17812 struct glyph_row *window_row, *window_row_end, *frame_row;
17813
17814 /* Preconditions: W must be a leaf window and full-width. Its frame
17815 must have a frame matrix. */
17816 eassert (BUFFERP (w->contents));
17817 eassert (WINDOW_FULL_WIDTH_P (w));
17818 eassert (!FRAME_WINDOW_P (f));
17819
17820 /* If W is a full-width window, glyph pointers in W's current matrix
17821 have, by definition, to be the same as glyph pointers in the
17822 corresponding frame matrix. Note that frame matrices have no
17823 marginal areas (see build_frame_matrix). */
17824 window_row = w->current_matrix->rows;
17825 window_row_end = window_row + w->current_matrix->nrows;
17826 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17827 while (window_row < window_row_end)
17828 {
17829 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17830 struct glyph *end = window_row->glyphs[LAST_AREA];
17831
17832 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17833 frame_row->glyphs[TEXT_AREA] = start;
17834 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17835 frame_row->glyphs[LAST_AREA] = end;
17836
17837 /* Disable frame rows whose corresponding window rows have
17838 been disabled in try_window_id. */
17839 if (!window_row->enabled_p)
17840 frame_row->enabled_p = false;
17841
17842 ++window_row, ++frame_row;
17843 }
17844 }
17845
17846
17847 /* Find the glyph row in window W containing CHARPOS. Consider all
17848 rows between START and END (not inclusive). END null means search
17849 all rows to the end of the display area of W. Value is the row
17850 containing CHARPOS or null. */
17851
17852 struct glyph_row *
17853 row_containing_pos (struct window *w, ptrdiff_t charpos,
17854 struct glyph_row *start, struct glyph_row *end, int dy)
17855 {
17856 struct glyph_row *row = start;
17857 struct glyph_row *best_row = NULL;
17858 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17859 int last_y;
17860
17861 /* If we happen to start on a header-line, skip that. */
17862 if (row->mode_line_p)
17863 ++row;
17864
17865 if ((end && row >= end) || !row->enabled_p)
17866 return NULL;
17867
17868 last_y = window_text_bottom_y (w) - dy;
17869
17870 while (true)
17871 {
17872 /* Give up if we have gone too far. */
17873 if ((end && row >= end) || !row->enabled_p)
17874 return NULL;
17875 /* This formerly returned if they were equal.
17876 I think that both quantities are of a "last plus one" type;
17877 if so, when they are equal, the row is within the screen. -- rms. */
17878 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17879 return NULL;
17880
17881 /* If it is in this row, return this row. */
17882 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17883 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17884 /* The end position of a row equals the start
17885 position of the next row. If CHARPOS is there, we
17886 would rather consider it displayed in the next
17887 line, except when this line ends in ZV. */
17888 && !row_for_charpos_p (row, charpos)))
17889 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17890 {
17891 struct glyph *g;
17892
17893 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17894 || (!best_row && !row->continued_p))
17895 return row;
17896 /* In bidi-reordered rows, there could be several rows whose
17897 edges surround CHARPOS, all of these rows belonging to
17898 the same continued line. We need to find the row which
17899 fits CHARPOS the best. */
17900 for (g = row->glyphs[TEXT_AREA];
17901 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17902 g++)
17903 {
17904 if (!STRINGP (g->object))
17905 {
17906 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17907 {
17908 mindif = eabs (g->charpos - charpos);
17909 best_row = row;
17910 /* Exact match always wins. */
17911 if (mindif == 0)
17912 return best_row;
17913 }
17914 }
17915 }
17916 }
17917 else if (best_row && !row->continued_p)
17918 return best_row;
17919 ++row;
17920 }
17921 }
17922
17923
17924 /* Try to redisplay window W by reusing its existing display. W's
17925 current matrix must be up to date when this function is called,
17926 i.e., window_end_valid must be true.
17927
17928 Value is
17929
17930 >= 1 if successful, i.e. display has been updated
17931 specifically:
17932 1 means the changes were in front of a newline that precedes
17933 the window start, and the whole current matrix was reused
17934 2 means the changes were after the last position displayed
17935 in the window, and the whole current matrix was reused
17936 3 means portions of the current matrix were reused, while
17937 some of the screen lines were redrawn
17938 -1 if redisplay with same window start is known not to succeed
17939 0 if otherwise unsuccessful
17940
17941 The following steps are performed:
17942
17943 1. Find the last row in the current matrix of W that is not
17944 affected by changes at the start of current_buffer. If no such row
17945 is found, give up.
17946
17947 2. Find the first row in W's current matrix that is not affected by
17948 changes at the end of current_buffer. Maybe there is no such row.
17949
17950 3. Display lines beginning with the row + 1 found in step 1 to the
17951 row found in step 2 or, if step 2 didn't find a row, to the end of
17952 the window.
17953
17954 4. If cursor is not known to appear on the window, give up.
17955
17956 5. If display stopped at the row found in step 2, scroll the
17957 display and current matrix as needed.
17958
17959 6. Maybe display some lines at the end of W, if we must. This can
17960 happen under various circumstances, like a partially visible line
17961 becoming fully visible, or because newly displayed lines are displayed
17962 in smaller font sizes.
17963
17964 7. Update W's window end information. */
17965
17966 static int
17967 try_window_id (struct window *w)
17968 {
17969 struct frame *f = XFRAME (w->frame);
17970 struct glyph_matrix *current_matrix = w->current_matrix;
17971 struct glyph_matrix *desired_matrix = w->desired_matrix;
17972 struct glyph_row *last_unchanged_at_beg_row;
17973 struct glyph_row *first_unchanged_at_end_row;
17974 struct glyph_row *row;
17975 struct glyph_row *bottom_row;
17976 int bottom_vpos;
17977 struct it it;
17978 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17979 int dvpos, dy;
17980 struct text_pos start_pos;
17981 struct run run;
17982 int first_unchanged_at_end_vpos = 0;
17983 struct glyph_row *last_text_row, *last_text_row_at_end;
17984 struct text_pos start;
17985 ptrdiff_t first_changed_charpos, last_changed_charpos;
17986
17987 #ifdef GLYPH_DEBUG
17988 if (inhibit_try_window_id)
17989 return 0;
17990 #endif
17991
17992 /* This is handy for debugging. */
17993 #if false
17994 #define GIVE_UP(X) \
17995 do { \
17996 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
17997 return 0; \
17998 } while (false)
17999 #else
18000 #define GIVE_UP(X) return 0
18001 #endif
18002
18003 SET_TEXT_POS_FROM_MARKER (start, w->start);
18004
18005 /* Don't use this for mini-windows because these can show
18006 messages and mini-buffers, and we don't handle that here. */
18007 if (MINI_WINDOW_P (w))
18008 GIVE_UP (1);
18009
18010 /* This flag is used to prevent redisplay optimizations. */
18011 if (windows_or_buffers_changed || f->cursor_type_changed)
18012 GIVE_UP (2);
18013
18014 /* This function's optimizations cannot be used if overlays have
18015 changed in the buffer displayed by the window, so give up if they
18016 have. */
18017 if (w->last_overlay_modified != OVERLAY_MODIFF)
18018 GIVE_UP (200);
18019
18020 /* Verify that narrowing has not changed.
18021 Also verify that we were not told to prevent redisplay optimizations.
18022 It would be nice to further
18023 reduce the number of cases where this prevents try_window_id. */
18024 if (current_buffer->clip_changed
18025 || current_buffer->prevent_redisplay_optimizations_p)
18026 GIVE_UP (3);
18027
18028 /* Window must either use window-based redisplay or be full width. */
18029 if (!FRAME_WINDOW_P (f)
18030 && (!FRAME_LINE_INS_DEL_OK (f)
18031 || !WINDOW_FULL_WIDTH_P (w)))
18032 GIVE_UP (4);
18033
18034 /* Give up if point is known NOT to appear in W. */
18035 if (PT < CHARPOS (start))
18036 GIVE_UP (5);
18037
18038 /* Another way to prevent redisplay optimizations. */
18039 if (w->last_modified == 0)
18040 GIVE_UP (6);
18041
18042 /* Verify that window is not hscrolled. */
18043 if (w->hscroll != 0)
18044 GIVE_UP (7);
18045
18046 /* Verify that display wasn't paused. */
18047 if (!w->window_end_valid)
18048 GIVE_UP (8);
18049
18050 /* Likewise if highlighting trailing whitespace. */
18051 if (!NILP (Vshow_trailing_whitespace))
18052 GIVE_UP (11);
18053
18054 /* Can't use this if overlay arrow position and/or string have
18055 changed. */
18056 if (overlay_arrows_changed_p ())
18057 GIVE_UP (12);
18058
18059 /* When word-wrap is on, adding a space to the first word of a
18060 wrapped line can change the wrap position, altering the line
18061 above it. It might be worthwhile to handle this more
18062 intelligently, but for now just redisplay from scratch. */
18063 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18064 GIVE_UP (21);
18065
18066 /* Under bidi reordering, adding or deleting a character in the
18067 beginning of a paragraph, before the first strong directional
18068 character, can change the base direction of the paragraph (unless
18069 the buffer specifies a fixed paragraph direction), which will
18070 require redisplaying the whole paragraph. It might be worthwhile
18071 to find the paragraph limits and widen the range of redisplayed
18072 lines to that, but for now just give up this optimization and
18073 redisplay from scratch. */
18074 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18075 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18076 GIVE_UP (22);
18077
18078 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18079 to that variable require thorough redisplay. */
18080 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18081 GIVE_UP (23);
18082
18083 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18084 only if buffer has really changed. The reason is that the gap is
18085 initially at Z for freshly visited files. The code below would
18086 set end_unchanged to 0 in that case. */
18087 if (MODIFF > SAVE_MODIFF
18088 /* This seems to happen sometimes after saving a buffer. */
18089 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18090 {
18091 if (GPT - BEG < BEG_UNCHANGED)
18092 BEG_UNCHANGED = GPT - BEG;
18093 if (Z - GPT < END_UNCHANGED)
18094 END_UNCHANGED = Z - GPT;
18095 }
18096
18097 /* The position of the first and last character that has been changed. */
18098 first_changed_charpos = BEG + BEG_UNCHANGED;
18099 last_changed_charpos = Z - END_UNCHANGED;
18100
18101 /* If window starts after a line end, and the last change is in
18102 front of that newline, then changes don't affect the display.
18103 This case happens with stealth-fontification. Note that although
18104 the display is unchanged, glyph positions in the matrix have to
18105 be adjusted, of course. */
18106 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18107 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18108 && ((last_changed_charpos < CHARPOS (start)
18109 && CHARPOS (start) == BEGV)
18110 || (last_changed_charpos < CHARPOS (start) - 1
18111 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18112 {
18113 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18114 struct glyph_row *r0;
18115
18116 /* Compute how many chars/bytes have been added to or removed
18117 from the buffer. */
18118 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18119 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18120 Z_delta = Z - Z_old;
18121 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18122
18123 /* Give up if PT is not in the window. Note that it already has
18124 been checked at the start of try_window_id that PT is not in
18125 front of the window start. */
18126 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18127 GIVE_UP (13);
18128
18129 /* If window start is unchanged, we can reuse the whole matrix
18130 as is, after adjusting glyph positions. No need to compute
18131 the window end again, since its offset from Z hasn't changed. */
18132 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18133 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18134 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18135 /* PT must not be in a partially visible line. */
18136 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18137 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18138 {
18139 /* Adjust positions in the glyph matrix. */
18140 if (Z_delta || Z_delta_bytes)
18141 {
18142 struct glyph_row *r1
18143 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18144 increment_matrix_positions (w->current_matrix,
18145 MATRIX_ROW_VPOS (r0, current_matrix),
18146 MATRIX_ROW_VPOS (r1, current_matrix),
18147 Z_delta, Z_delta_bytes);
18148 }
18149
18150 /* Set the cursor. */
18151 row = row_containing_pos (w, PT, r0, NULL, 0);
18152 if (row)
18153 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18154 return 1;
18155 }
18156 }
18157
18158 /* Handle the case that changes are all below what is displayed in
18159 the window, and that PT is in the window. This shortcut cannot
18160 be taken if ZV is visible in the window, and text has been added
18161 there that is visible in the window. */
18162 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18163 /* ZV is not visible in the window, or there are no
18164 changes at ZV, actually. */
18165 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18166 || first_changed_charpos == last_changed_charpos))
18167 {
18168 struct glyph_row *r0;
18169
18170 /* Give up if PT is not in the window. Note that it already has
18171 been checked at the start of try_window_id that PT is not in
18172 front of the window start. */
18173 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18174 GIVE_UP (14);
18175
18176 /* If window start is unchanged, we can reuse the whole matrix
18177 as is, without changing glyph positions since no text has
18178 been added/removed in front of the window end. */
18179 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18180 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18181 /* PT must not be in a partially visible line. */
18182 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18183 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18184 {
18185 /* We have to compute the window end anew since text
18186 could have been added/removed after it. */
18187 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18188 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18189
18190 /* Set the cursor. */
18191 row = row_containing_pos (w, PT, r0, NULL, 0);
18192 if (row)
18193 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18194 return 2;
18195 }
18196 }
18197
18198 /* Give up if window start is in the changed area.
18199
18200 The condition used to read
18201
18202 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18203
18204 but why that was tested escapes me at the moment. */
18205 if (CHARPOS (start) >= first_changed_charpos
18206 && CHARPOS (start) <= last_changed_charpos)
18207 GIVE_UP (15);
18208
18209 /* Check that window start agrees with the start of the first glyph
18210 row in its current matrix. Check this after we know the window
18211 start is not in changed text, otherwise positions would not be
18212 comparable. */
18213 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18214 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18215 GIVE_UP (16);
18216
18217 /* Give up if the window ends in strings. Overlay strings
18218 at the end are difficult to handle, so don't try. */
18219 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18220 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18221 GIVE_UP (20);
18222
18223 /* Compute the position at which we have to start displaying new
18224 lines. Some of the lines at the top of the window might be
18225 reusable because they are not displaying changed text. Find the
18226 last row in W's current matrix not affected by changes at the
18227 start of current_buffer. Value is null if changes start in the
18228 first line of window. */
18229 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18230 if (last_unchanged_at_beg_row)
18231 {
18232 /* Avoid starting to display in the middle of a character, a TAB
18233 for instance. This is easier than to set up the iterator
18234 exactly, and it's not a frequent case, so the additional
18235 effort wouldn't really pay off. */
18236 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18237 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18238 && last_unchanged_at_beg_row > w->current_matrix->rows)
18239 --last_unchanged_at_beg_row;
18240
18241 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18242 GIVE_UP (17);
18243
18244 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18245 GIVE_UP (18);
18246 start_pos = it.current.pos;
18247
18248 /* Start displaying new lines in the desired matrix at the same
18249 vpos we would use in the current matrix, i.e. below
18250 last_unchanged_at_beg_row. */
18251 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18252 current_matrix);
18253 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18254 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18255
18256 eassert (it.hpos == 0 && it.current_x == 0);
18257 }
18258 else
18259 {
18260 /* There are no reusable lines at the start of the window.
18261 Start displaying in the first text line. */
18262 start_display (&it, w, start);
18263 it.vpos = it.first_vpos;
18264 start_pos = it.current.pos;
18265 }
18266
18267 /* Find the first row that is not affected by changes at the end of
18268 the buffer. Value will be null if there is no unchanged row, in
18269 which case we must redisplay to the end of the window. delta
18270 will be set to the value by which buffer positions beginning with
18271 first_unchanged_at_end_row have to be adjusted due to text
18272 changes. */
18273 first_unchanged_at_end_row
18274 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18275 IF_DEBUG (debug_delta = delta);
18276 IF_DEBUG (debug_delta_bytes = delta_bytes);
18277
18278 /* Set stop_pos to the buffer position up to which we will have to
18279 display new lines. If first_unchanged_at_end_row != NULL, this
18280 is the buffer position of the start of the line displayed in that
18281 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18282 that we don't stop at a buffer position. */
18283 stop_pos = 0;
18284 if (first_unchanged_at_end_row)
18285 {
18286 eassert (last_unchanged_at_beg_row == NULL
18287 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18288
18289 /* If this is a continuation line, move forward to the next one
18290 that isn't. Changes in lines above affect this line.
18291 Caution: this may move first_unchanged_at_end_row to a row
18292 not displaying text. */
18293 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18294 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18295 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18296 < it.last_visible_y))
18297 ++first_unchanged_at_end_row;
18298
18299 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18300 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18301 >= it.last_visible_y))
18302 first_unchanged_at_end_row = NULL;
18303 else
18304 {
18305 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18306 + delta);
18307 first_unchanged_at_end_vpos
18308 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18309 eassert (stop_pos >= Z - END_UNCHANGED);
18310 }
18311 }
18312 else if (last_unchanged_at_beg_row == NULL)
18313 GIVE_UP (19);
18314
18315
18316 #ifdef GLYPH_DEBUG
18317
18318 /* Either there is no unchanged row at the end, or the one we have
18319 now displays text. This is a necessary condition for the window
18320 end pos calculation at the end of this function. */
18321 eassert (first_unchanged_at_end_row == NULL
18322 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18323
18324 debug_last_unchanged_at_beg_vpos
18325 = (last_unchanged_at_beg_row
18326 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18327 : -1);
18328 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18329
18330 #endif /* GLYPH_DEBUG */
18331
18332
18333 /* Display new lines. Set last_text_row to the last new line
18334 displayed which has text on it, i.e. might end up as being the
18335 line where the window_end_vpos is. */
18336 w->cursor.vpos = -1;
18337 last_text_row = NULL;
18338 overlay_arrow_seen = false;
18339 if (it.current_y < it.last_visible_y
18340 && !f->fonts_changed
18341 && (first_unchanged_at_end_row == NULL
18342 || IT_CHARPOS (it) < stop_pos))
18343 it.glyph_row->reversed_p = false;
18344 while (it.current_y < it.last_visible_y
18345 && !f->fonts_changed
18346 && (first_unchanged_at_end_row == NULL
18347 || IT_CHARPOS (it) < stop_pos))
18348 {
18349 if (display_line (&it))
18350 last_text_row = it.glyph_row - 1;
18351 }
18352
18353 if (f->fonts_changed)
18354 return -1;
18355
18356 /* The redisplay iterations in display_line above could have
18357 triggered font-lock, which could have done something that
18358 invalidates IT->w window's end-point information, on which we
18359 rely below. E.g., one package, which will remain unnamed, used
18360 to install a font-lock-fontify-region-function that called
18361 bury-buffer, whose side effect is to switch the buffer displayed
18362 by IT->w, and that predictably resets IT->w's window_end_valid
18363 flag, which we already tested at the entry to this function.
18364 Amply punish such packages/modes by giving up on this
18365 optimization in those cases. */
18366 if (!w->window_end_valid)
18367 {
18368 clear_glyph_matrix (w->desired_matrix);
18369 return -1;
18370 }
18371
18372 /* Compute differences in buffer positions, y-positions etc. for
18373 lines reused at the bottom of the window. Compute what we can
18374 scroll. */
18375 if (first_unchanged_at_end_row
18376 /* No lines reused because we displayed everything up to the
18377 bottom of the window. */
18378 && it.current_y < it.last_visible_y)
18379 {
18380 dvpos = (it.vpos
18381 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18382 current_matrix));
18383 dy = it.current_y - first_unchanged_at_end_row->y;
18384 run.current_y = first_unchanged_at_end_row->y;
18385 run.desired_y = run.current_y + dy;
18386 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18387 }
18388 else
18389 {
18390 delta = delta_bytes = dvpos = dy
18391 = run.current_y = run.desired_y = run.height = 0;
18392 first_unchanged_at_end_row = NULL;
18393 }
18394 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18395
18396
18397 /* Find the cursor if not already found. We have to decide whether
18398 PT will appear on this window (it sometimes doesn't, but this is
18399 not a very frequent case.) This decision has to be made before
18400 the current matrix is altered. A value of cursor.vpos < 0 means
18401 that PT is either in one of the lines beginning at
18402 first_unchanged_at_end_row or below the window. Don't care for
18403 lines that might be displayed later at the window end; as
18404 mentioned, this is not a frequent case. */
18405 if (w->cursor.vpos < 0)
18406 {
18407 /* Cursor in unchanged rows at the top? */
18408 if (PT < CHARPOS (start_pos)
18409 && last_unchanged_at_beg_row)
18410 {
18411 row = row_containing_pos (w, PT,
18412 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18413 last_unchanged_at_beg_row + 1, 0);
18414 if (row)
18415 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18416 }
18417
18418 /* Start from first_unchanged_at_end_row looking for PT. */
18419 else if (first_unchanged_at_end_row)
18420 {
18421 row = row_containing_pos (w, PT - delta,
18422 first_unchanged_at_end_row, NULL, 0);
18423 if (row)
18424 set_cursor_from_row (w, row, w->current_matrix, delta,
18425 delta_bytes, dy, dvpos);
18426 }
18427
18428 /* Give up if cursor was not found. */
18429 if (w->cursor.vpos < 0)
18430 {
18431 clear_glyph_matrix (w->desired_matrix);
18432 return -1;
18433 }
18434 }
18435
18436 /* Don't let the cursor end in the scroll margins. */
18437 {
18438 int this_scroll_margin, cursor_height;
18439 int frame_line_height = default_line_pixel_height (w);
18440 int window_total_lines
18441 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18442
18443 this_scroll_margin =
18444 max (0, min (scroll_margin, window_total_lines / 4));
18445 this_scroll_margin *= frame_line_height;
18446 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18447
18448 if ((w->cursor.y < this_scroll_margin
18449 && CHARPOS (start) > BEGV)
18450 /* Old redisplay didn't take scroll margin into account at the bottom,
18451 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18452 || (w->cursor.y + (make_cursor_line_fully_visible_p
18453 ? cursor_height + this_scroll_margin
18454 : 1)) > it.last_visible_y)
18455 {
18456 w->cursor.vpos = -1;
18457 clear_glyph_matrix (w->desired_matrix);
18458 return -1;
18459 }
18460 }
18461
18462 /* Scroll the display. Do it before changing the current matrix so
18463 that xterm.c doesn't get confused about where the cursor glyph is
18464 found. */
18465 if (dy && run.height)
18466 {
18467 update_begin (f);
18468
18469 if (FRAME_WINDOW_P (f))
18470 {
18471 FRAME_RIF (f)->update_window_begin_hook (w);
18472 FRAME_RIF (f)->clear_window_mouse_face (w);
18473 FRAME_RIF (f)->scroll_run_hook (w, &run);
18474 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18475 }
18476 else
18477 {
18478 /* Terminal frame. In this case, dvpos gives the number of
18479 lines to scroll by; dvpos < 0 means scroll up. */
18480 int from_vpos
18481 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18482 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18483 int end = (WINDOW_TOP_EDGE_LINE (w)
18484 + WINDOW_WANTS_HEADER_LINE_P (w)
18485 + window_internal_height (w));
18486
18487 #if defined (HAVE_GPM) || defined (MSDOS)
18488 x_clear_window_mouse_face (w);
18489 #endif
18490 /* Perform the operation on the screen. */
18491 if (dvpos > 0)
18492 {
18493 /* Scroll last_unchanged_at_beg_row to the end of the
18494 window down dvpos lines. */
18495 set_terminal_window (f, end);
18496
18497 /* On dumb terminals delete dvpos lines at the end
18498 before inserting dvpos empty lines. */
18499 if (!FRAME_SCROLL_REGION_OK (f))
18500 ins_del_lines (f, end - dvpos, -dvpos);
18501
18502 /* Insert dvpos empty lines in front of
18503 last_unchanged_at_beg_row. */
18504 ins_del_lines (f, from, dvpos);
18505 }
18506 else if (dvpos < 0)
18507 {
18508 /* Scroll up last_unchanged_at_beg_vpos to the end of
18509 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18510 set_terminal_window (f, end);
18511
18512 /* Delete dvpos lines in front of
18513 last_unchanged_at_beg_vpos. ins_del_lines will set
18514 the cursor to the given vpos and emit |dvpos| delete
18515 line sequences. */
18516 ins_del_lines (f, from + dvpos, dvpos);
18517
18518 /* On a dumb terminal insert dvpos empty lines at the
18519 end. */
18520 if (!FRAME_SCROLL_REGION_OK (f))
18521 ins_del_lines (f, end + dvpos, -dvpos);
18522 }
18523
18524 set_terminal_window (f, 0);
18525 }
18526
18527 update_end (f);
18528 }
18529
18530 /* Shift reused rows of the current matrix to the right position.
18531 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18532 text. */
18533 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18534 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18535 if (dvpos < 0)
18536 {
18537 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18538 bottom_vpos, dvpos);
18539 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18540 bottom_vpos);
18541 }
18542 else if (dvpos > 0)
18543 {
18544 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18545 bottom_vpos, dvpos);
18546 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18547 first_unchanged_at_end_vpos + dvpos);
18548 }
18549
18550 /* For frame-based redisplay, make sure that current frame and window
18551 matrix are in sync with respect to glyph memory. */
18552 if (!FRAME_WINDOW_P (f))
18553 sync_frame_with_window_matrix_rows (w);
18554
18555 /* Adjust buffer positions in reused rows. */
18556 if (delta || delta_bytes)
18557 increment_matrix_positions (current_matrix,
18558 first_unchanged_at_end_vpos + dvpos,
18559 bottom_vpos, delta, delta_bytes);
18560
18561 /* Adjust Y positions. */
18562 if (dy)
18563 shift_glyph_matrix (w, current_matrix,
18564 first_unchanged_at_end_vpos + dvpos,
18565 bottom_vpos, dy);
18566
18567 if (first_unchanged_at_end_row)
18568 {
18569 first_unchanged_at_end_row += dvpos;
18570 if (first_unchanged_at_end_row->y >= it.last_visible_y
18571 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18572 first_unchanged_at_end_row = NULL;
18573 }
18574
18575 /* If scrolling up, there may be some lines to display at the end of
18576 the window. */
18577 last_text_row_at_end = NULL;
18578 if (dy < 0)
18579 {
18580 /* Scrolling up can leave for example a partially visible line
18581 at the end of the window to be redisplayed. */
18582 /* Set last_row to the glyph row in the current matrix where the
18583 window end line is found. It has been moved up or down in
18584 the matrix by dvpos. */
18585 int last_vpos = w->window_end_vpos + dvpos;
18586 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18587
18588 /* If last_row is the window end line, it should display text. */
18589 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18590
18591 /* If window end line was partially visible before, begin
18592 displaying at that line. Otherwise begin displaying with the
18593 line following it. */
18594 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18595 {
18596 init_to_row_start (&it, w, last_row);
18597 it.vpos = last_vpos;
18598 it.current_y = last_row->y;
18599 }
18600 else
18601 {
18602 init_to_row_end (&it, w, last_row);
18603 it.vpos = 1 + last_vpos;
18604 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18605 ++last_row;
18606 }
18607
18608 /* We may start in a continuation line. If so, we have to
18609 get the right continuation_lines_width and current_x. */
18610 it.continuation_lines_width = last_row->continuation_lines_width;
18611 it.hpos = it.current_x = 0;
18612
18613 /* Display the rest of the lines at the window end. */
18614 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18615 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18616 {
18617 /* Is it always sure that the display agrees with lines in
18618 the current matrix? I don't think so, so we mark rows
18619 displayed invalid in the current matrix by setting their
18620 enabled_p flag to false. */
18621 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18622 if (display_line (&it))
18623 last_text_row_at_end = it.glyph_row - 1;
18624 }
18625 }
18626
18627 /* Update window_end_pos and window_end_vpos. */
18628 if (first_unchanged_at_end_row && !last_text_row_at_end)
18629 {
18630 /* Window end line if one of the preserved rows from the current
18631 matrix. Set row to the last row displaying text in current
18632 matrix starting at first_unchanged_at_end_row, after
18633 scrolling. */
18634 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18635 row = find_last_row_displaying_text (w->current_matrix, &it,
18636 first_unchanged_at_end_row);
18637 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18638 adjust_window_ends (w, row, true);
18639 eassert (w->window_end_bytepos >= 0);
18640 IF_DEBUG (debug_method_add (w, "A"));
18641 }
18642 else if (last_text_row_at_end)
18643 {
18644 adjust_window_ends (w, last_text_row_at_end, false);
18645 eassert (w->window_end_bytepos >= 0);
18646 IF_DEBUG (debug_method_add (w, "B"));
18647 }
18648 else if (last_text_row)
18649 {
18650 /* We have displayed either to the end of the window or at the
18651 end of the window, i.e. the last row with text is to be found
18652 in the desired matrix. */
18653 adjust_window_ends (w, last_text_row, false);
18654 eassert (w->window_end_bytepos >= 0);
18655 }
18656 else if (first_unchanged_at_end_row == NULL
18657 && last_text_row == NULL
18658 && last_text_row_at_end == NULL)
18659 {
18660 /* Displayed to end of window, but no line containing text was
18661 displayed. Lines were deleted at the end of the window. */
18662 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18663 int vpos = w->window_end_vpos;
18664 struct glyph_row *current_row = current_matrix->rows + vpos;
18665 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18666
18667 for (row = NULL;
18668 row == NULL && vpos >= first_vpos;
18669 --vpos, --current_row, --desired_row)
18670 {
18671 if (desired_row->enabled_p)
18672 {
18673 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18674 row = desired_row;
18675 }
18676 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18677 row = current_row;
18678 }
18679
18680 eassert (row != NULL);
18681 w->window_end_vpos = vpos + 1;
18682 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18683 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18684 eassert (w->window_end_bytepos >= 0);
18685 IF_DEBUG (debug_method_add (w, "C"));
18686 }
18687 else
18688 emacs_abort ();
18689
18690 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18691 debug_end_vpos = w->window_end_vpos));
18692
18693 /* Record that display has not been completed. */
18694 w->window_end_valid = false;
18695 w->desired_matrix->no_scrolling_p = true;
18696 return 3;
18697
18698 #undef GIVE_UP
18699 }
18700
18701
18702 \f
18703 /***********************************************************************
18704 More debugging support
18705 ***********************************************************************/
18706
18707 #ifdef GLYPH_DEBUG
18708
18709 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18710 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18711 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18712
18713
18714 /* Dump the contents of glyph matrix MATRIX on stderr.
18715
18716 GLYPHS 0 means don't show glyph contents.
18717 GLYPHS 1 means show glyphs in short form
18718 GLYPHS > 1 means show glyphs in long form. */
18719
18720 void
18721 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18722 {
18723 int i;
18724 for (i = 0; i < matrix->nrows; ++i)
18725 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18726 }
18727
18728
18729 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18730 the glyph row and area where the glyph comes from. */
18731
18732 void
18733 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18734 {
18735 if (glyph->type == CHAR_GLYPH
18736 || glyph->type == GLYPHLESS_GLYPH)
18737 {
18738 fprintf (stderr,
18739 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18740 glyph - row->glyphs[TEXT_AREA],
18741 (glyph->type == CHAR_GLYPH
18742 ? 'C'
18743 : 'G'),
18744 glyph->charpos,
18745 (BUFFERP (glyph->object)
18746 ? 'B'
18747 : (STRINGP (glyph->object)
18748 ? 'S'
18749 : (NILP (glyph->object)
18750 ? '0'
18751 : '-'))),
18752 glyph->pixel_width,
18753 glyph->u.ch,
18754 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18755 ? glyph->u.ch
18756 : '.'),
18757 glyph->face_id,
18758 glyph->left_box_line_p,
18759 glyph->right_box_line_p);
18760 }
18761 else if (glyph->type == STRETCH_GLYPH)
18762 {
18763 fprintf (stderr,
18764 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18765 glyph - row->glyphs[TEXT_AREA],
18766 'S',
18767 glyph->charpos,
18768 (BUFFERP (glyph->object)
18769 ? 'B'
18770 : (STRINGP (glyph->object)
18771 ? 'S'
18772 : (NILP (glyph->object)
18773 ? '0'
18774 : '-'))),
18775 glyph->pixel_width,
18776 0,
18777 ' ',
18778 glyph->face_id,
18779 glyph->left_box_line_p,
18780 glyph->right_box_line_p);
18781 }
18782 else if (glyph->type == IMAGE_GLYPH)
18783 {
18784 fprintf (stderr,
18785 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18786 glyph - row->glyphs[TEXT_AREA],
18787 'I',
18788 glyph->charpos,
18789 (BUFFERP (glyph->object)
18790 ? 'B'
18791 : (STRINGP (glyph->object)
18792 ? 'S'
18793 : (NILP (glyph->object)
18794 ? '0'
18795 : '-'))),
18796 glyph->pixel_width,
18797 glyph->u.img_id,
18798 '.',
18799 glyph->face_id,
18800 glyph->left_box_line_p,
18801 glyph->right_box_line_p);
18802 }
18803 else if (glyph->type == COMPOSITE_GLYPH)
18804 {
18805 fprintf (stderr,
18806 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18807 glyph - row->glyphs[TEXT_AREA],
18808 '+',
18809 glyph->charpos,
18810 (BUFFERP (glyph->object)
18811 ? 'B'
18812 : (STRINGP (glyph->object)
18813 ? 'S'
18814 : (NILP (glyph->object)
18815 ? '0'
18816 : '-'))),
18817 glyph->pixel_width,
18818 glyph->u.cmp.id);
18819 if (glyph->u.cmp.automatic)
18820 fprintf (stderr,
18821 "[%d-%d]",
18822 glyph->slice.cmp.from, glyph->slice.cmp.to);
18823 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18824 glyph->face_id,
18825 glyph->left_box_line_p,
18826 glyph->right_box_line_p);
18827 }
18828 else if (glyph->type == XWIDGET_GLYPH)
18829 {
18830 #ifndef HAVE_XWIDGETS
18831 eassume (false);
18832 #else
18833 fprintf (stderr,
18834 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18835 glyph - row->glyphs[TEXT_AREA],
18836 'X',
18837 glyph->charpos,
18838 (BUFFERP (glyph->object)
18839 ? 'B'
18840 : (STRINGP (glyph->object)
18841 ? 'S'
18842 : '-')),
18843 glyph->pixel_width,
18844 glyph->u.xwidget,
18845 '.',
18846 glyph->face_id,
18847 glyph->left_box_line_p,
18848 glyph->right_box_line_p);
18849 #endif
18850 }
18851 }
18852
18853
18854 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18855 GLYPHS 0 means don't show glyph contents.
18856 GLYPHS 1 means show glyphs in short form
18857 GLYPHS > 1 means show glyphs in long form. */
18858
18859 void
18860 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18861 {
18862 if (glyphs != 1)
18863 {
18864 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18865 fprintf (stderr, "==============================================================================\n");
18866
18867 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18868 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18869 vpos,
18870 MATRIX_ROW_START_CHARPOS (row),
18871 MATRIX_ROW_END_CHARPOS (row),
18872 row->used[TEXT_AREA],
18873 row->contains_overlapping_glyphs_p,
18874 row->enabled_p,
18875 row->truncated_on_left_p,
18876 row->truncated_on_right_p,
18877 row->continued_p,
18878 MATRIX_ROW_CONTINUATION_LINE_P (row),
18879 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18880 row->ends_at_zv_p,
18881 row->fill_line_p,
18882 row->ends_in_middle_of_char_p,
18883 row->starts_in_middle_of_char_p,
18884 row->mouse_face_p,
18885 row->x,
18886 row->y,
18887 row->pixel_width,
18888 row->height,
18889 row->visible_height,
18890 row->ascent,
18891 row->phys_ascent);
18892 /* The next 3 lines should align to "Start" in the header. */
18893 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18894 row->end.overlay_string_index,
18895 row->continuation_lines_width);
18896 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18897 CHARPOS (row->start.string_pos),
18898 CHARPOS (row->end.string_pos));
18899 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18900 row->end.dpvec_index);
18901 }
18902
18903 if (glyphs > 1)
18904 {
18905 int area;
18906
18907 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18908 {
18909 struct glyph *glyph = row->glyphs[area];
18910 struct glyph *glyph_end = glyph + row->used[area];
18911
18912 /* Glyph for a line end in text. */
18913 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18914 ++glyph_end;
18915
18916 if (glyph < glyph_end)
18917 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18918
18919 for (; glyph < glyph_end; ++glyph)
18920 dump_glyph (row, glyph, area);
18921 }
18922 }
18923 else if (glyphs == 1)
18924 {
18925 int area;
18926 char s[SHRT_MAX + 4];
18927
18928 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18929 {
18930 int i;
18931
18932 for (i = 0; i < row->used[area]; ++i)
18933 {
18934 struct glyph *glyph = row->glyphs[area] + i;
18935 if (i == row->used[area] - 1
18936 && area == TEXT_AREA
18937 && NILP (glyph->object)
18938 && glyph->type == CHAR_GLYPH
18939 && glyph->u.ch == ' ')
18940 {
18941 strcpy (&s[i], "[\\n]");
18942 i += 4;
18943 }
18944 else if (glyph->type == CHAR_GLYPH
18945 && glyph->u.ch < 0x80
18946 && glyph->u.ch >= ' ')
18947 s[i] = glyph->u.ch;
18948 else
18949 s[i] = '.';
18950 }
18951
18952 s[i] = '\0';
18953 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18954 }
18955 }
18956 }
18957
18958
18959 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18960 Sdump_glyph_matrix, 0, 1, "p",
18961 doc: /* Dump the current matrix of the selected window to stderr.
18962 Shows contents of glyph row structures. With non-nil
18963 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18964 glyphs in short form, otherwise show glyphs in long form.
18965
18966 Interactively, no argument means show glyphs in short form;
18967 with numeric argument, its value is passed as the GLYPHS flag. */)
18968 (Lisp_Object glyphs)
18969 {
18970 struct window *w = XWINDOW (selected_window);
18971 struct buffer *buffer = XBUFFER (w->contents);
18972
18973 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18974 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18975 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18976 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18977 fprintf (stderr, "=============================================\n");
18978 dump_glyph_matrix (w->current_matrix,
18979 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18980 return Qnil;
18981 }
18982
18983
18984 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18985 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18986 Only text-mode frames have frame glyph matrices. */)
18987 (void)
18988 {
18989 struct frame *f = XFRAME (selected_frame);
18990
18991 if (f->current_matrix)
18992 dump_glyph_matrix (f->current_matrix, 1);
18993 else
18994 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
18995 return Qnil;
18996 }
18997
18998
18999 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
19000 doc: /* Dump glyph row ROW to stderr.
19001 GLYPH 0 means don't dump glyphs.
19002 GLYPH 1 means dump glyphs in short form.
19003 GLYPH > 1 or omitted means dump glyphs in long form. */)
19004 (Lisp_Object row, Lisp_Object glyphs)
19005 {
19006 struct glyph_matrix *matrix;
19007 EMACS_INT vpos;
19008
19009 CHECK_NUMBER (row);
19010 matrix = XWINDOW (selected_window)->current_matrix;
19011 vpos = XINT (row);
19012 if (vpos >= 0 && vpos < matrix->nrows)
19013 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19014 vpos,
19015 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19016 return Qnil;
19017 }
19018
19019
19020 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
19021 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19022 GLYPH 0 means don't dump glyphs.
19023 GLYPH 1 means dump glyphs in short form.
19024 GLYPH > 1 or omitted means dump glyphs in long form.
19025
19026 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19027 do nothing. */)
19028 (Lisp_Object row, Lisp_Object glyphs)
19029 {
19030 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19031 struct frame *sf = SELECTED_FRAME ();
19032 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19033 EMACS_INT vpos;
19034
19035 CHECK_NUMBER (row);
19036 vpos = XINT (row);
19037 if (vpos >= 0 && vpos < m->nrows)
19038 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19039 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19040 #endif
19041 return Qnil;
19042 }
19043
19044
19045 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19046 doc: /* Toggle tracing of redisplay.
19047 With ARG, turn tracing on if and only if ARG is positive. */)
19048 (Lisp_Object arg)
19049 {
19050 if (NILP (arg))
19051 trace_redisplay_p = !trace_redisplay_p;
19052 else
19053 {
19054 arg = Fprefix_numeric_value (arg);
19055 trace_redisplay_p = XINT (arg) > 0;
19056 }
19057
19058 return Qnil;
19059 }
19060
19061
19062 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19063 doc: /* Like `format', but print result to stderr.
19064 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19065 (ptrdiff_t nargs, Lisp_Object *args)
19066 {
19067 Lisp_Object s = Fformat (nargs, args);
19068 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19069 return Qnil;
19070 }
19071
19072 #endif /* GLYPH_DEBUG */
19073
19074
19075 \f
19076 /***********************************************************************
19077 Building Desired Matrix Rows
19078 ***********************************************************************/
19079
19080 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19081 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19082
19083 static struct glyph_row *
19084 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19085 {
19086 struct frame *f = XFRAME (WINDOW_FRAME (w));
19087 struct buffer *buffer = XBUFFER (w->contents);
19088 struct buffer *old = current_buffer;
19089 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19090 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19091 const unsigned char *arrow_end = arrow_string + arrow_len;
19092 const unsigned char *p;
19093 struct it it;
19094 bool multibyte_p;
19095 int n_glyphs_before;
19096
19097 set_buffer_temp (buffer);
19098 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19099 scratch_glyph_row.reversed_p = false;
19100 it.glyph_row->used[TEXT_AREA] = 0;
19101 SET_TEXT_POS (it.position, 0, 0);
19102
19103 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19104 p = arrow_string;
19105 while (p < arrow_end)
19106 {
19107 Lisp_Object face, ilisp;
19108
19109 /* Get the next character. */
19110 if (multibyte_p)
19111 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19112 else
19113 {
19114 it.c = it.char_to_display = *p, it.len = 1;
19115 if (! ASCII_CHAR_P (it.c))
19116 it.char_to_display = BYTE8_TO_CHAR (it.c);
19117 }
19118 p += it.len;
19119
19120 /* Get its face. */
19121 ilisp = make_number (p - arrow_string);
19122 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19123 it.face_id = compute_char_face (f, it.char_to_display, face);
19124
19125 /* Compute its width, get its glyphs. */
19126 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19127 SET_TEXT_POS (it.position, -1, -1);
19128 PRODUCE_GLYPHS (&it);
19129
19130 /* If this character doesn't fit any more in the line, we have
19131 to remove some glyphs. */
19132 if (it.current_x > it.last_visible_x)
19133 {
19134 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19135 break;
19136 }
19137 }
19138
19139 set_buffer_temp (old);
19140 return it.glyph_row;
19141 }
19142
19143
19144 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19145 glyphs to insert is determined by produce_special_glyphs. */
19146
19147 static void
19148 insert_left_trunc_glyphs (struct it *it)
19149 {
19150 struct it truncate_it;
19151 struct glyph *from, *end, *to, *toend;
19152
19153 eassert (!FRAME_WINDOW_P (it->f)
19154 || (!it->glyph_row->reversed_p
19155 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19156 || (it->glyph_row->reversed_p
19157 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19158
19159 /* Get the truncation glyphs. */
19160 truncate_it = *it;
19161 truncate_it.current_x = 0;
19162 truncate_it.face_id = DEFAULT_FACE_ID;
19163 truncate_it.glyph_row = &scratch_glyph_row;
19164 truncate_it.area = TEXT_AREA;
19165 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19166 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19167 truncate_it.object = Qnil;
19168 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19169
19170 /* Overwrite glyphs from IT with truncation glyphs. */
19171 if (!it->glyph_row->reversed_p)
19172 {
19173 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19174
19175 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19176 end = from + tused;
19177 to = it->glyph_row->glyphs[TEXT_AREA];
19178 toend = to + it->glyph_row->used[TEXT_AREA];
19179 if (FRAME_WINDOW_P (it->f))
19180 {
19181 /* On GUI frames, when variable-size fonts are displayed,
19182 the truncation glyphs may need more pixels than the row's
19183 glyphs they overwrite. We overwrite more glyphs to free
19184 enough screen real estate, and enlarge the stretch glyph
19185 on the right (see display_line), if there is one, to
19186 preserve the screen position of the truncation glyphs on
19187 the right. */
19188 int w = 0;
19189 struct glyph *g = to;
19190 short used;
19191
19192 /* The first glyph could be partially visible, in which case
19193 it->glyph_row->x will be negative. But we want the left
19194 truncation glyphs to be aligned at the left margin of the
19195 window, so we override the x coordinate at which the row
19196 will begin. */
19197 it->glyph_row->x = 0;
19198 while (g < toend && w < it->truncation_pixel_width)
19199 {
19200 w += g->pixel_width;
19201 ++g;
19202 }
19203 if (g - to - tused > 0)
19204 {
19205 memmove (to + tused, g, (toend - g) * sizeof(*g));
19206 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19207 }
19208 used = it->glyph_row->used[TEXT_AREA];
19209 if (it->glyph_row->truncated_on_right_p
19210 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19211 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19212 == STRETCH_GLYPH)
19213 {
19214 int extra = w - it->truncation_pixel_width;
19215
19216 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19217 }
19218 }
19219
19220 while (from < end)
19221 *to++ = *from++;
19222
19223 /* There may be padding glyphs left over. Overwrite them too. */
19224 if (!FRAME_WINDOW_P (it->f))
19225 {
19226 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19227 {
19228 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19229 while (from < end)
19230 *to++ = *from++;
19231 }
19232 }
19233
19234 if (to > toend)
19235 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19236 }
19237 else
19238 {
19239 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19240
19241 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19242 that back to front. */
19243 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19244 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19245 toend = it->glyph_row->glyphs[TEXT_AREA];
19246 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19247 if (FRAME_WINDOW_P (it->f))
19248 {
19249 int w = 0;
19250 struct glyph *g = to;
19251
19252 while (g >= toend && w < it->truncation_pixel_width)
19253 {
19254 w += g->pixel_width;
19255 --g;
19256 }
19257 if (to - g - tused > 0)
19258 to = g + tused;
19259 if (it->glyph_row->truncated_on_right_p
19260 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19261 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19262 {
19263 int extra = w - it->truncation_pixel_width;
19264
19265 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19266 }
19267 }
19268
19269 while (from >= end && to >= toend)
19270 *to-- = *from--;
19271 if (!FRAME_WINDOW_P (it->f))
19272 {
19273 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19274 {
19275 from =
19276 truncate_it.glyph_row->glyphs[TEXT_AREA]
19277 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19278 while (from >= end && to >= toend)
19279 *to-- = *from--;
19280 }
19281 }
19282 if (from >= end)
19283 {
19284 /* Need to free some room before prepending additional
19285 glyphs. */
19286 int move_by = from - end + 1;
19287 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19288 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19289
19290 for ( ; g >= g0; g--)
19291 g[move_by] = *g;
19292 while (from >= end)
19293 *to-- = *from--;
19294 it->glyph_row->used[TEXT_AREA] += move_by;
19295 }
19296 }
19297 }
19298
19299 /* Compute the hash code for ROW. */
19300 unsigned
19301 row_hash (struct glyph_row *row)
19302 {
19303 int area, k;
19304 unsigned hashval = 0;
19305
19306 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19307 for (k = 0; k < row->used[area]; ++k)
19308 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19309 + row->glyphs[area][k].u.val
19310 + row->glyphs[area][k].face_id
19311 + row->glyphs[area][k].padding_p
19312 + (row->glyphs[area][k].type << 2));
19313
19314 return hashval;
19315 }
19316
19317 /* Compute the pixel height and width of IT->glyph_row.
19318
19319 Most of the time, ascent and height of a display line will be equal
19320 to the max_ascent and max_height values of the display iterator
19321 structure. This is not the case if
19322
19323 1. We hit ZV without displaying anything. In this case, max_ascent
19324 and max_height will be zero.
19325
19326 2. We have some glyphs that don't contribute to the line height.
19327 (The glyph row flag contributes_to_line_height_p is for future
19328 pixmap extensions).
19329
19330 The first case is easily covered by using default values because in
19331 these cases, the line height does not really matter, except that it
19332 must not be zero. */
19333
19334 static void
19335 compute_line_metrics (struct it *it)
19336 {
19337 struct glyph_row *row = it->glyph_row;
19338
19339 if (FRAME_WINDOW_P (it->f))
19340 {
19341 int i, min_y, max_y;
19342
19343 /* The line may consist of one space only, that was added to
19344 place the cursor on it. If so, the row's height hasn't been
19345 computed yet. */
19346 if (row->height == 0)
19347 {
19348 if (it->max_ascent + it->max_descent == 0)
19349 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19350 row->ascent = it->max_ascent;
19351 row->height = it->max_ascent + it->max_descent;
19352 row->phys_ascent = it->max_phys_ascent;
19353 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19354 row->extra_line_spacing = it->max_extra_line_spacing;
19355 }
19356
19357 /* Compute the width of this line. */
19358 row->pixel_width = row->x;
19359 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19360 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19361
19362 eassert (row->pixel_width >= 0);
19363 eassert (row->ascent >= 0 && row->height > 0);
19364
19365 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19366 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19367
19368 /* If first line's physical ascent is larger than its logical
19369 ascent, use the physical ascent, and make the row taller.
19370 This makes accented characters fully visible. */
19371 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19372 && row->phys_ascent > row->ascent)
19373 {
19374 row->height += row->phys_ascent - row->ascent;
19375 row->ascent = row->phys_ascent;
19376 }
19377
19378 /* Compute how much of the line is visible. */
19379 row->visible_height = row->height;
19380
19381 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19382 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19383
19384 if (row->y < min_y)
19385 row->visible_height -= min_y - row->y;
19386 if (row->y + row->height > max_y)
19387 row->visible_height -= row->y + row->height - max_y;
19388 }
19389 else
19390 {
19391 row->pixel_width = row->used[TEXT_AREA];
19392 if (row->continued_p)
19393 row->pixel_width -= it->continuation_pixel_width;
19394 else if (row->truncated_on_right_p)
19395 row->pixel_width -= it->truncation_pixel_width;
19396 row->ascent = row->phys_ascent = 0;
19397 row->height = row->phys_height = row->visible_height = 1;
19398 row->extra_line_spacing = 0;
19399 }
19400
19401 /* Compute a hash code for this row. */
19402 row->hash = row_hash (row);
19403
19404 it->max_ascent = it->max_descent = 0;
19405 it->max_phys_ascent = it->max_phys_descent = 0;
19406 }
19407
19408
19409 /* Append one space to the glyph row of iterator IT if doing a
19410 window-based redisplay. The space has the same face as
19411 IT->face_id. Value is true if a space was added.
19412
19413 This function is called to make sure that there is always one glyph
19414 at the end of a glyph row that the cursor can be set on under
19415 window-systems. (If there weren't such a glyph we would not know
19416 how wide and tall a box cursor should be displayed).
19417
19418 At the same time this space let's a nicely handle clearing to the
19419 end of the line if the row ends in italic text. */
19420
19421 static bool
19422 append_space_for_newline (struct it *it, bool default_face_p)
19423 {
19424 if (FRAME_WINDOW_P (it->f))
19425 {
19426 int n = it->glyph_row->used[TEXT_AREA];
19427
19428 if (it->glyph_row->glyphs[TEXT_AREA] + n
19429 < it->glyph_row->glyphs[1 + TEXT_AREA])
19430 {
19431 /* Save some values that must not be changed.
19432 Must save IT->c and IT->len because otherwise
19433 ITERATOR_AT_END_P wouldn't work anymore after
19434 append_space_for_newline has been called. */
19435 enum display_element_type saved_what = it->what;
19436 int saved_c = it->c, saved_len = it->len;
19437 int saved_char_to_display = it->char_to_display;
19438 int saved_x = it->current_x;
19439 int saved_face_id = it->face_id;
19440 bool saved_box_end = it->end_of_box_run_p;
19441 struct text_pos saved_pos;
19442 Lisp_Object saved_object;
19443 struct face *face;
19444 struct glyph *g;
19445
19446 saved_object = it->object;
19447 saved_pos = it->position;
19448
19449 it->what = IT_CHARACTER;
19450 memset (&it->position, 0, sizeof it->position);
19451 it->object = Qnil;
19452 it->c = it->char_to_display = ' ';
19453 it->len = 1;
19454
19455 /* If the default face was remapped, be sure to use the
19456 remapped face for the appended newline. */
19457 if (default_face_p)
19458 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19459 else if (it->face_before_selective_p)
19460 it->face_id = it->saved_face_id;
19461 face = FACE_FROM_ID (it->f, it->face_id);
19462 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19463 /* In R2L rows, we will prepend a stretch glyph that will
19464 have the end_of_box_run_p flag set for it, so there's no
19465 need for the appended newline glyph to have that flag
19466 set. */
19467 if (it->glyph_row->reversed_p
19468 /* But if the appended newline glyph goes all the way to
19469 the end of the row, there will be no stretch glyph,
19470 so leave the box flag set. */
19471 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19472 it->end_of_box_run_p = false;
19473
19474 PRODUCE_GLYPHS (it);
19475
19476 #ifdef HAVE_WINDOW_SYSTEM
19477 /* Make sure this space glyph has the right ascent and
19478 descent values, or else cursor at end of line will look
19479 funny, and height of empty lines will be incorrect. */
19480 g = it->glyph_row->glyphs[TEXT_AREA] + n;
19481 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19482 if (n == 0)
19483 {
19484 Lisp_Object height, total_height;
19485 int extra_line_spacing = it->extra_line_spacing;
19486 int boff = font->baseline_offset;
19487
19488 if (font->vertical_centering)
19489 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19490
19491 it->object = saved_object; /* get_it_property needs this */
19492 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19493 /* Must do a subset of line height processing from
19494 x_produce_glyph for newline characters. */
19495 height = get_it_property (it, Qline_height);
19496 if (CONSP (height)
19497 && CONSP (XCDR (height))
19498 && NILP (XCDR (XCDR (height))))
19499 {
19500 total_height = XCAR (XCDR (height));
19501 height = XCAR (height);
19502 }
19503 else
19504 total_height = Qnil;
19505 height = calc_line_height_property (it, height, font, boff, true);
19506
19507 if (it->override_ascent >= 0)
19508 {
19509 it->ascent = it->override_ascent;
19510 it->descent = it->override_descent;
19511 boff = it->override_boff;
19512 }
19513 if (EQ (height, Qt))
19514 extra_line_spacing = 0;
19515 else
19516 {
19517 Lisp_Object spacing;
19518
19519 it->phys_ascent = it->ascent;
19520 it->phys_descent = it->descent;
19521 if (!NILP (height)
19522 && XINT (height) > it->ascent + it->descent)
19523 it->ascent = XINT (height) - it->descent;
19524
19525 if (!NILP (total_height))
19526 spacing = calc_line_height_property (it, total_height, font,
19527 boff, false);
19528 else
19529 {
19530 spacing = get_it_property (it, Qline_spacing);
19531 spacing = calc_line_height_property (it, spacing, font,
19532 boff, false);
19533 }
19534 if (INTEGERP (spacing))
19535 {
19536 extra_line_spacing = XINT (spacing);
19537 if (!NILP (total_height))
19538 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19539 }
19540 }
19541 if (extra_line_spacing > 0)
19542 {
19543 it->descent += extra_line_spacing;
19544 if (extra_line_spacing > it->max_extra_line_spacing)
19545 it->max_extra_line_spacing = extra_line_spacing;
19546 }
19547 it->max_ascent = it->ascent;
19548 it->max_descent = it->descent;
19549 /* Make sure compute_line_metrics recomputes the row height. */
19550 it->glyph_row->height = 0;
19551 }
19552
19553 g->ascent = it->max_ascent;
19554 g->descent = it->max_descent;
19555 #endif
19556
19557 it->override_ascent = -1;
19558 it->constrain_row_ascent_descent_p = false;
19559 it->current_x = saved_x;
19560 it->object = saved_object;
19561 it->position = saved_pos;
19562 it->what = saved_what;
19563 it->face_id = saved_face_id;
19564 it->len = saved_len;
19565 it->c = saved_c;
19566 it->char_to_display = saved_char_to_display;
19567 it->end_of_box_run_p = saved_box_end;
19568 return true;
19569 }
19570 }
19571
19572 return false;
19573 }
19574
19575
19576 /* Extend the face of the last glyph in the text area of IT->glyph_row
19577 to the end of the display line. Called from display_line. If the
19578 glyph row is empty, add a space glyph to it so that we know the
19579 face to draw. Set the glyph row flag fill_line_p. If the glyph
19580 row is R2L, prepend a stretch glyph to cover the empty space to the
19581 left of the leftmost glyph. */
19582
19583 static void
19584 extend_face_to_end_of_line (struct it *it)
19585 {
19586 struct face *face, *default_face;
19587 struct frame *f = it->f;
19588
19589 /* If line is already filled, do nothing. Non window-system frames
19590 get a grace of one more ``pixel'' because their characters are
19591 1-``pixel'' wide, so they hit the equality too early. This grace
19592 is needed only for R2L rows that are not continued, to produce
19593 one extra blank where we could display the cursor. */
19594 if ((it->current_x >= it->last_visible_x
19595 + (!FRAME_WINDOW_P (f)
19596 && it->glyph_row->reversed_p
19597 && !it->glyph_row->continued_p))
19598 /* If the window has display margins, we will need to extend
19599 their face even if the text area is filled. */
19600 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19601 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19602 return;
19603
19604 /* The default face, possibly remapped. */
19605 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19606
19607 /* Face extension extends the background and box of IT->face_id
19608 to the end of the line. If the background equals the background
19609 of the frame, we don't have to do anything. */
19610 if (it->face_before_selective_p)
19611 face = FACE_FROM_ID (f, it->saved_face_id);
19612 else
19613 face = FACE_FROM_ID (f, it->face_id);
19614
19615 if (FRAME_WINDOW_P (f)
19616 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19617 && face->box == FACE_NO_BOX
19618 && face->background == FRAME_BACKGROUND_PIXEL (f)
19619 #ifdef HAVE_WINDOW_SYSTEM
19620 && !face->stipple
19621 #endif
19622 && !it->glyph_row->reversed_p)
19623 return;
19624
19625 /* Set the glyph row flag indicating that the face of the last glyph
19626 in the text area has to be drawn to the end of the text area. */
19627 it->glyph_row->fill_line_p = true;
19628
19629 /* If current character of IT is not ASCII, make sure we have the
19630 ASCII face. This will be automatically undone the next time
19631 get_next_display_element returns a multibyte character. Note
19632 that the character will always be single byte in unibyte
19633 text. */
19634 if (!ASCII_CHAR_P (it->c))
19635 {
19636 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19637 }
19638
19639 if (FRAME_WINDOW_P (f))
19640 {
19641 /* If the row is empty, add a space with the current face of IT,
19642 so that we know which face to draw. */
19643 if (it->glyph_row->used[TEXT_AREA] == 0)
19644 {
19645 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19646 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19647 it->glyph_row->used[TEXT_AREA] = 1;
19648 }
19649 /* Mode line and the header line don't have margins, and
19650 likewise the frame's tool-bar window, if there is any. */
19651 if (!(it->glyph_row->mode_line_p
19652 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19653 || (WINDOWP (f->tool_bar_window)
19654 && it->w == XWINDOW (f->tool_bar_window))
19655 #endif
19656 ))
19657 {
19658 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19659 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19660 {
19661 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19662 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19663 default_face->id;
19664 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19665 }
19666 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19667 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19668 {
19669 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19670 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19671 default_face->id;
19672 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19673 }
19674 }
19675 #ifdef HAVE_WINDOW_SYSTEM
19676 if (it->glyph_row->reversed_p)
19677 {
19678 /* Prepend a stretch glyph to the row, such that the
19679 rightmost glyph will be drawn flushed all the way to the
19680 right margin of the window. The stretch glyph that will
19681 occupy the empty space, if any, to the left of the
19682 glyphs. */
19683 struct font *font = face->font ? face->font : FRAME_FONT (f);
19684 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19685 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19686 struct glyph *g;
19687 int row_width, stretch_ascent, stretch_width;
19688 struct text_pos saved_pos;
19689 int saved_face_id;
19690 bool saved_avoid_cursor, saved_box_start;
19691
19692 for (row_width = 0, g = row_start; g < row_end; g++)
19693 row_width += g->pixel_width;
19694
19695 /* FIXME: There are various minor display glitches in R2L
19696 rows when only one of the fringes is missing. The
19697 strange condition below produces the least bad effect. */
19698 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19699 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19700 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19701 stretch_width = window_box_width (it->w, TEXT_AREA);
19702 else
19703 stretch_width = it->last_visible_x - it->first_visible_x;
19704 stretch_width -= row_width;
19705
19706 if (stretch_width > 0)
19707 {
19708 stretch_ascent =
19709 (((it->ascent + it->descent)
19710 * FONT_BASE (font)) / FONT_HEIGHT (font));
19711 saved_pos = it->position;
19712 memset (&it->position, 0, sizeof it->position);
19713 saved_avoid_cursor = it->avoid_cursor_p;
19714 it->avoid_cursor_p = true;
19715 saved_face_id = it->face_id;
19716 saved_box_start = it->start_of_box_run_p;
19717 /* The last row's stretch glyph should get the default
19718 face, to avoid painting the rest of the window with
19719 the region face, if the region ends at ZV. */
19720 if (it->glyph_row->ends_at_zv_p)
19721 it->face_id = default_face->id;
19722 else
19723 it->face_id = face->id;
19724 it->start_of_box_run_p = false;
19725 append_stretch_glyph (it, Qnil, stretch_width,
19726 it->ascent + it->descent, stretch_ascent);
19727 it->position = saved_pos;
19728 it->avoid_cursor_p = saved_avoid_cursor;
19729 it->face_id = saved_face_id;
19730 it->start_of_box_run_p = saved_box_start;
19731 }
19732 /* If stretch_width comes out negative, it means that the
19733 last glyph is only partially visible. In R2L rows, we
19734 want the leftmost glyph to be partially visible, so we
19735 need to give the row the corresponding left offset. */
19736 if (stretch_width < 0)
19737 it->glyph_row->x = stretch_width;
19738 }
19739 #endif /* HAVE_WINDOW_SYSTEM */
19740 }
19741 else
19742 {
19743 /* Save some values that must not be changed. */
19744 int saved_x = it->current_x;
19745 struct text_pos saved_pos;
19746 Lisp_Object saved_object;
19747 enum display_element_type saved_what = it->what;
19748 int saved_face_id = it->face_id;
19749
19750 saved_object = it->object;
19751 saved_pos = it->position;
19752
19753 it->what = IT_CHARACTER;
19754 memset (&it->position, 0, sizeof it->position);
19755 it->object = Qnil;
19756 it->c = it->char_to_display = ' ';
19757 it->len = 1;
19758
19759 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19760 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19761 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19762 && !it->glyph_row->mode_line_p
19763 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19764 {
19765 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19766 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19767
19768 for (it->current_x = 0; g < e; g++)
19769 it->current_x += g->pixel_width;
19770
19771 it->area = LEFT_MARGIN_AREA;
19772 it->face_id = default_face->id;
19773 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19774 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19775 {
19776 PRODUCE_GLYPHS (it);
19777 /* term.c:produce_glyphs advances it->current_x only for
19778 TEXT_AREA. */
19779 it->current_x += it->pixel_width;
19780 }
19781
19782 it->current_x = saved_x;
19783 it->area = TEXT_AREA;
19784 }
19785
19786 /* The last row's blank glyphs should get the default face, to
19787 avoid painting the rest of the window with the region face,
19788 if the region ends at ZV. */
19789 if (it->glyph_row->ends_at_zv_p)
19790 it->face_id = default_face->id;
19791 else
19792 it->face_id = face->id;
19793 PRODUCE_GLYPHS (it);
19794
19795 while (it->current_x <= it->last_visible_x)
19796 PRODUCE_GLYPHS (it);
19797
19798 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19799 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19800 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19801 && !it->glyph_row->mode_line_p
19802 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19803 {
19804 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19805 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19806
19807 for ( ; g < e; g++)
19808 it->current_x += g->pixel_width;
19809
19810 it->area = RIGHT_MARGIN_AREA;
19811 it->face_id = default_face->id;
19812 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19813 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19814 {
19815 PRODUCE_GLYPHS (it);
19816 it->current_x += it->pixel_width;
19817 }
19818
19819 it->area = TEXT_AREA;
19820 }
19821
19822 /* Don't count these blanks really. It would let us insert a left
19823 truncation glyph below and make us set the cursor on them, maybe. */
19824 it->current_x = saved_x;
19825 it->object = saved_object;
19826 it->position = saved_pos;
19827 it->what = saved_what;
19828 it->face_id = saved_face_id;
19829 }
19830 }
19831
19832
19833 /* Value is true if text starting at CHARPOS in current_buffer is
19834 trailing whitespace. */
19835
19836 static bool
19837 trailing_whitespace_p (ptrdiff_t charpos)
19838 {
19839 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19840 int c = 0;
19841
19842 while (bytepos < ZV_BYTE
19843 && (c = FETCH_CHAR (bytepos),
19844 c == ' ' || c == '\t'))
19845 ++bytepos;
19846
19847 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19848 {
19849 if (bytepos != PT_BYTE)
19850 return true;
19851 }
19852 return false;
19853 }
19854
19855
19856 /* Highlight trailing whitespace, if any, in ROW. */
19857
19858 static void
19859 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19860 {
19861 int used = row->used[TEXT_AREA];
19862
19863 if (used)
19864 {
19865 struct glyph *start = row->glyphs[TEXT_AREA];
19866 struct glyph *glyph = start + used - 1;
19867
19868 if (row->reversed_p)
19869 {
19870 /* Right-to-left rows need to be processed in the opposite
19871 direction, so swap the edge pointers. */
19872 glyph = start;
19873 start = row->glyphs[TEXT_AREA] + used - 1;
19874 }
19875
19876 /* Skip over glyphs inserted to display the cursor at the
19877 end of a line, for extending the face of the last glyph
19878 to the end of the line on terminals, and for truncation
19879 and continuation glyphs. */
19880 if (!row->reversed_p)
19881 {
19882 while (glyph >= start
19883 && glyph->type == CHAR_GLYPH
19884 && NILP (glyph->object))
19885 --glyph;
19886 }
19887 else
19888 {
19889 while (glyph <= start
19890 && glyph->type == CHAR_GLYPH
19891 && NILP (glyph->object))
19892 ++glyph;
19893 }
19894
19895 /* If last glyph is a space or stretch, and it's trailing
19896 whitespace, set the face of all trailing whitespace glyphs in
19897 IT->glyph_row to `trailing-whitespace'. */
19898 if ((row->reversed_p ? glyph <= start : glyph >= start)
19899 && BUFFERP (glyph->object)
19900 && (glyph->type == STRETCH_GLYPH
19901 || (glyph->type == CHAR_GLYPH
19902 && glyph->u.ch == ' '))
19903 && trailing_whitespace_p (glyph->charpos))
19904 {
19905 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
19906 if (face_id < 0)
19907 return;
19908
19909 if (!row->reversed_p)
19910 {
19911 while (glyph >= start
19912 && BUFFERP (glyph->object)
19913 && (glyph->type == STRETCH_GLYPH
19914 || (glyph->type == CHAR_GLYPH
19915 && glyph->u.ch == ' ')))
19916 (glyph--)->face_id = face_id;
19917 }
19918 else
19919 {
19920 while (glyph <= start
19921 && BUFFERP (glyph->object)
19922 && (glyph->type == STRETCH_GLYPH
19923 || (glyph->type == CHAR_GLYPH
19924 && glyph->u.ch == ' ')))
19925 (glyph++)->face_id = face_id;
19926 }
19927 }
19928 }
19929 }
19930
19931
19932 /* Value is true if glyph row ROW should be
19933 considered to hold the buffer position CHARPOS. */
19934
19935 static bool
19936 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19937 {
19938 bool result = true;
19939
19940 if (charpos == CHARPOS (row->end.pos)
19941 || charpos == MATRIX_ROW_END_CHARPOS (row))
19942 {
19943 /* Suppose the row ends on a string.
19944 Unless the row is continued, that means it ends on a newline
19945 in the string. If it's anything other than a display string
19946 (e.g., a before-string from an overlay), we don't want the
19947 cursor there. (This heuristic seems to give the optimal
19948 behavior for the various types of multi-line strings.)
19949 One exception: if the string has `cursor' property on one of
19950 its characters, we _do_ want the cursor there. */
19951 if (CHARPOS (row->end.string_pos) >= 0)
19952 {
19953 if (row->continued_p)
19954 result = true;
19955 else
19956 {
19957 /* Check for `display' property. */
19958 struct glyph *beg = row->glyphs[TEXT_AREA];
19959 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19960 struct glyph *glyph;
19961
19962 result = false;
19963 for (glyph = end; glyph >= beg; --glyph)
19964 if (STRINGP (glyph->object))
19965 {
19966 Lisp_Object prop
19967 = Fget_char_property (make_number (charpos),
19968 Qdisplay, Qnil);
19969 result =
19970 (!NILP (prop)
19971 && display_prop_string_p (prop, glyph->object));
19972 /* If there's a `cursor' property on one of the
19973 string's characters, this row is a cursor row,
19974 even though this is not a display string. */
19975 if (!result)
19976 {
19977 Lisp_Object s = glyph->object;
19978
19979 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19980 {
19981 ptrdiff_t gpos = glyph->charpos;
19982
19983 if (!NILP (Fget_char_property (make_number (gpos),
19984 Qcursor, s)))
19985 {
19986 result = true;
19987 break;
19988 }
19989 }
19990 }
19991 break;
19992 }
19993 }
19994 }
19995 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19996 {
19997 /* If the row ends in middle of a real character,
19998 and the line is continued, we want the cursor here.
19999 That's because CHARPOS (ROW->end.pos) would equal
20000 PT if PT is before the character. */
20001 if (!row->ends_in_ellipsis_p)
20002 result = row->continued_p;
20003 else
20004 /* If the row ends in an ellipsis, then
20005 CHARPOS (ROW->end.pos) will equal point after the
20006 invisible text. We want that position to be displayed
20007 after the ellipsis. */
20008 result = false;
20009 }
20010 /* If the row ends at ZV, display the cursor at the end of that
20011 row instead of at the start of the row below. */
20012 else
20013 result = row->ends_at_zv_p;
20014 }
20015
20016 return result;
20017 }
20018
20019 /* Value is true if glyph row ROW should be
20020 used to hold the cursor. */
20021
20022 static bool
20023 cursor_row_p (struct glyph_row *row)
20024 {
20025 return row_for_charpos_p (row, PT);
20026 }
20027
20028 \f
20029
20030 /* Push the property PROP so that it will be rendered at the current
20031 position in IT. Return true if PROP was successfully pushed, false
20032 otherwise. Called from handle_line_prefix to handle the
20033 `line-prefix' and `wrap-prefix' properties. */
20034
20035 static bool
20036 push_prefix_prop (struct it *it, Lisp_Object prop)
20037 {
20038 struct text_pos pos =
20039 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20040
20041 eassert (it->method == GET_FROM_BUFFER
20042 || it->method == GET_FROM_DISPLAY_VECTOR
20043 || it->method == GET_FROM_STRING
20044 || it->method == GET_FROM_IMAGE);
20045
20046 /* We need to save the current buffer/string position, so it will be
20047 restored by pop_it, because iterate_out_of_display_property
20048 depends on that being set correctly, but some situations leave
20049 it->position not yet set when this function is called. */
20050 push_it (it, &pos);
20051
20052 if (STRINGP (prop))
20053 {
20054 if (SCHARS (prop) == 0)
20055 {
20056 pop_it (it);
20057 return false;
20058 }
20059
20060 it->string = prop;
20061 it->string_from_prefix_prop_p = true;
20062 it->multibyte_p = STRING_MULTIBYTE (it->string);
20063 it->current.overlay_string_index = -1;
20064 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20065 it->end_charpos = it->string_nchars = SCHARS (it->string);
20066 it->method = GET_FROM_STRING;
20067 it->stop_charpos = 0;
20068 it->prev_stop = 0;
20069 it->base_level_stop = 0;
20070
20071 /* Force paragraph direction to be that of the parent
20072 buffer/string. */
20073 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20074 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20075 else
20076 it->paragraph_embedding = L2R;
20077
20078 /* Set up the bidi iterator for this display string. */
20079 if (it->bidi_p)
20080 {
20081 it->bidi_it.string.lstring = it->string;
20082 it->bidi_it.string.s = NULL;
20083 it->bidi_it.string.schars = it->end_charpos;
20084 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20085 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20086 it->bidi_it.string.unibyte = !it->multibyte_p;
20087 it->bidi_it.w = it->w;
20088 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20089 }
20090 }
20091 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20092 {
20093 it->method = GET_FROM_STRETCH;
20094 it->object = prop;
20095 }
20096 #ifdef HAVE_WINDOW_SYSTEM
20097 else if (IMAGEP (prop))
20098 {
20099 it->what = IT_IMAGE;
20100 it->image_id = lookup_image (it->f, prop);
20101 it->method = GET_FROM_IMAGE;
20102 }
20103 #endif /* HAVE_WINDOW_SYSTEM */
20104 else
20105 {
20106 pop_it (it); /* bogus display property, give up */
20107 return false;
20108 }
20109
20110 return true;
20111 }
20112
20113 /* Return the character-property PROP at the current position in IT. */
20114
20115 static Lisp_Object
20116 get_it_property (struct it *it, Lisp_Object prop)
20117 {
20118 Lisp_Object position, object = it->object;
20119
20120 if (STRINGP (object))
20121 position = make_number (IT_STRING_CHARPOS (*it));
20122 else if (BUFFERP (object))
20123 {
20124 position = make_number (IT_CHARPOS (*it));
20125 object = it->window;
20126 }
20127 else
20128 return Qnil;
20129
20130 return Fget_char_property (position, prop, object);
20131 }
20132
20133 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20134
20135 static void
20136 handle_line_prefix (struct it *it)
20137 {
20138 Lisp_Object prefix;
20139
20140 if (it->continuation_lines_width > 0)
20141 {
20142 prefix = get_it_property (it, Qwrap_prefix);
20143 if (NILP (prefix))
20144 prefix = Vwrap_prefix;
20145 }
20146 else
20147 {
20148 prefix = get_it_property (it, Qline_prefix);
20149 if (NILP (prefix))
20150 prefix = Vline_prefix;
20151 }
20152 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20153 {
20154 /* If the prefix is wider than the window, and we try to wrap
20155 it, it would acquire its own wrap prefix, and so on till the
20156 iterator stack overflows. So, don't wrap the prefix. */
20157 it->line_wrap = TRUNCATE;
20158 it->avoid_cursor_p = true;
20159 }
20160 }
20161
20162 \f
20163
20164 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20165 only for R2L lines from display_line and display_string, when they
20166 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20167 the line/string needs to be continued on the next glyph row. */
20168 static void
20169 unproduce_glyphs (struct it *it, int n)
20170 {
20171 struct glyph *glyph, *end;
20172
20173 eassert (it->glyph_row);
20174 eassert (it->glyph_row->reversed_p);
20175 eassert (it->area == TEXT_AREA);
20176 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20177
20178 if (n > it->glyph_row->used[TEXT_AREA])
20179 n = it->glyph_row->used[TEXT_AREA];
20180 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20181 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20182 for ( ; glyph < end; glyph++)
20183 glyph[-n] = *glyph;
20184 }
20185
20186 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20187 and ROW->maxpos. */
20188 static void
20189 find_row_edges (struct it *it, struct glyph_row *row,
20190 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20191 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20192 {
20193 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20194 lines' rows is implemented for bidi-reordered rows. */
20195
20196 /* ROW->minpos is the value of min_pos, the minimal buffer position
20197 we have in ROW, or ROW->start.pos if that is smaller. */
20198 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20199 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20200 else
20201 /* We didn't find buffer positions smaller than ROW->start, or
20202 didn't find _any_ valid buffer positions in any of the glyphs,
20203 so we must trust the iterator's computed positions. */
20204 row->minpos = row->start.pos;
20205 if (max_pos <= 0)
20206 {
20207 max_pos = CHARPOS (it->current.pos);
20208 max_bpos = BYTEPOS (it->current.pos);
20209 }
20210
20211 /* Here are the various use-cases for ending the row, and the
20212 corresponding values for ROW->maxpos:
20213
20214 Line ends in a newline from buffer eol_pos + 1
20215 Line is continued from buffer max_pos + 1
20216 Line is truncated on right it->current.pos
20217 Line ends in a newline from string max_pos + 1(*)
20218 (*) + 1 only when line ends in a forward scan
20219 Line is continued from string max_pos
20220 Line is continued from display vector max_pos
20221 Line is entirely from a string min_pos == max_pos
20222 Line is entirely from a display vector min_pos == max_pos
20223 Line that ends at ZV ZV
20224
20225 If you discover other use-cases, please add them here as
20226 appropriate. */
20227 if (row->ends_at_zv_p)
20228 row->maxpos = it->current.pos;
20229 else if (row->used[TEXT_AREA])
20230 {
20231 bool seen_this_string = false;
20232 struct glyph_row *r1 = row - 1;
20233
20234 /* Did we see the same display string on the previous row? */
20235 if (STRINGP (it->object)
20236 /* this is not the first row */
20237 && row > it->w->desired_matrix->rows
20238 /* previous row is not the header line */
20239 && !r1->mode_line_p
20240 /* previous row also ends in a newline from a string */
20241 && r1->ends_in_newline_from_string_p)
20242 {
20243 struct glyph *start, *end;
20244
20245 /* Search for the last glyph of the previous row that came
20246 from buffer or string. Depending on whether the row is
20247 L2R or R2L, we need to process it front to back or the
20248 other way round. */
20249 if (!r1->reversed_p)
20250 {
20251 start = r1->glyphs[TEXT_AREA];
20252 end = start + r1->used[TEXT_AREA];
20253 /* Glyphs inserted by redisplay have nil as their object. */
20254 while (end > start
20255 && NILP ((end - 1)->object)
20256 && (end - 1)->charpos <= 0)
20257 --end;
20258 if (end > start)
20259 {
20260 if (EQ ((end - 1)->object, it->object))
20261 seen_this_string = true;
20262 }
20263 else
20264 /* If all the glyphs of the previous row were inserted
20265 by redisplay, it means the previous row was
20266 produced from a single newline, which is only
20267 possible if that newline came from the same string
20268 as the one which produced this ROW. */
20269 seen_this_string = true;
20270 }
20271 else
20272 {
20273 end = r1->glyphs[TEXT_AREA] - 1;
20274 start = end + r1->used[TEXT_AREA];
20275 while (end < start
20276 && NILP ((end + 1)->object)
20277 && (end + 1)->charpos <= 0)
20278 ++end;
20279 if (end < start)
20280 {
20281 if (EQ ((end + 1)->object, it->object))
20282 seen_this_string = true;
20283 }
20284 else
20285 seen_this_string = true;
20286 }
20287 }
20288 /* Take note of each display string that covers a newline only
20289 once, the first time we see it. This is for when a display
20290 string includes more than one newline in it. */
20291 if (row->ends_in_newline_from_string_p && !seen_this_string)
20292 {
20293 /* If we were scanning the buffer forward when we displayed
20294 the string, we want to account for at least one buffer
20295 position that belongs to this row (position covered by
20296 the display string), so that cursor positioning will
20297 consider this row as a candidate when point is at the end
20298 of the visual line represented by this row. This is not
20299 required when scanning back, because max_pos will already
20300 have a much larger value. */
20301 if (CHARPOS (row->end.pos) > max_pos)
20302 INC_BOTH (max_pos, max_bpos);
20303 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20304 }
20305 else if (CHARPOS (it->eol_pos) > 0)
20306 SET_TEXT_POS (row->maxpos,
20307 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20308 else if (row->continued_p)
20309 {
20310 /* If max_pos is different from IT's current position, it
20311 means IT->method does not belong to the display element
20312 at max_pos. However, it also means that the display
20313 element at max_pos was displayed in its entirety on this
20314 line, which is equivalent to saying that the next line
20315 starts at the next buffer position. */
20316 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20317 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20318 else
20319 {
20320 INC_BOTH (max_pos, max_bpos);
20321 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20322 }
20323 }
20324 else if (row->truncated_on_right_p)
20325 /* display_line already called reseat_at_next_visible_line_start,
20326 which puts the iterator at the beginning of the next line, in
20327 the logical order. */
20328 row->maxpos = it->current.pos;
20329 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20330 /* A line that is entirely from a string/image/stretch... */
20331 row->maxpos = row->minpos;
20332 else
20333 emacs_abort ();
20334 }
20335 else
20336 row->maxpos = it->current.pos;
20337 }
20338
20339 /* Construct the glyph row IT->glyph_row in the desired matrix of
20340 IT->w from text at the current position of IT. See dispextern.h
20341 for an overview of struct it. Value is true if
20342 IT->glyph_row displays text, as opposed to a line displaying ZV
20343 only. */
20344
20345 static bool
20346 display_line (struct it *it)
20347 {
20348 struct glyph_row *row = it->glyph_row;
20349 Lisp_Object overlay_arrow_string;
20350 struct it wrap_it;
20351 void *wrap_data = NULL;
20352 bool may_wrap = false;
20353 int wrap_x IF_LINT (= 0);
20354 int wrap_row_used = -1;
20355 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20356 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20357 int wrap_row_extra_line_spacing IF_LINT (= 0);
20358 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20359 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20360 int cvpos;
20361 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20362 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20363 bool pending_handle_line_prefix = false;
20364
20365 /* We always start displaying at hpos zero even if hscrolled. */
20366 eassert (it->hpos == 0 && it->current_x == 0);
20367
20368 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20369 >= it->w->desired_matrix->nrows)
20370 {
20371 it->w->nrows_scale_factor++;
20372 it->f->fonts_changed = true;
20373 return false;
20374 }
20375
20376 /* Clear the result glyph row and enable it. */
20377 prepare_desired_row (it->w, row, false);
20378
20379 row->y = it->current_y;
20380 row->start = it->start;
20381 row->continuation_lines_width = it->continuation_lines_width;
20382 row->displays_text_p = true;
20383 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20384 it->starts_in_middle_of_char_p = false;
20385
20386 /* Arrange the overlays nicely for our purposes. Usually, we call
20387 display_line on only one line at a time, in which case this
20388 can't really hurt too much, or we call it on lines which appear
20389 one after another in the buffer, in which case all calls to
20390 recenter_overlay_lists but the first will be pretty cheap. */
20391 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20392
20393 /* Move over display elements that are not visible because we are
20394 hscrolled. This may stop at an x-position < IT->first_visible_x
20395 if the first glyph is partially visible or if we hit a line end. */
20396 if (it->current_x < it->first_visible_x)
20397 {
20398 enum move_it_result move_result;
20399
20400 this_line_min_pos = row->start.pos;
20401 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20402 MOVE_TO_POS | MOVE_TO_X);
20403 /* If we are under a large hscroll, move_it_in_display_line_to
20404 could hit the end of the line without reaching
20405 it->first_visible_x. Pretend that we did reach it. This is
20406 especially important on a TTY, where we will call
20407 extend_face_to_end_of_line, which needs to know how many
20408 blank glyphs to produce. */
20409 if (it->current_x < it->first_visible_x
20410 && (move_result == MOVE_NEWLINE_OR_CR
20411 || move_result == MOVE_POS_MATCH_OR_ZV))
20412 it->current_x = it->first_visible_x;
20413
20414 /* Record the smallest positions seen while we moved over
20415 display elements that are not visible. This is needed by
20416 redisplay_internal for optimizing the case where the cursor
20417 stays inside the same line. The rest of this function only
20418 considers positions that are actually displayed, so
20419 RECORD_MAX_MIN_POS will not otherwise record positions that
20420 are hscrolled to the left of the left edge of the window. */
20421 min_pos = CHARPOS (this_line_min_pos);
20422 min_bpos = BYTEPOS (this_line_min_pos);
20423 }
20424 else if (it->area == TEXT_AREA)
20425 {
20426 /* We only do this when not calling move_it_in_display_line_to
20427 above, because that function calls itself handle_line_prefix. */
20428 handle_line_prefix (it);
20429 }
20430 else
20431 {
20432 /* Line-prefix and wrap-prefix are always displayed in the text
20433 area. But if this is the first call to display_line after
20434 init_iterator, the iterator might have been set up to write
20435 into a marginal area, e.g. if the line begins with some
20436 display property that writes to the margins. So we need to
20437 wait with the call to handle_line_prefix until whatever
20438 writes to the margin has done its job. */
20439 pending_handle_line_prefix = true;
20440 }
20441
20442 /* Get the initial row height. This is either the height of the
20443 text hscrolled, if there is any, or zero. */
20444 row->ascent = it->max_ascent;
20445 row->height = it->max_ascent + it->max_descent;
20446 row->phys_ascent = it->max_phys_ascent;
20447 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20448 row->extra_line_spacing = it->max_extra_line_spacing;
20449
20450 /* Utility macro to record max and min buffer positions seen until now. */
20451 #define RECORD_MAX_MIN_POS(IT) \
20452 do \
20453 { \
20454 bool composition_p \
20455 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20456 ptrdiff_t current_pos = \
20457 composition_p ? (IT)->cmp_it.charpos \
20458 : IT_CHARPOS (*(IT)); \
20459 ptrdiff_t current_bpos = \
20460 composition_p ? CHAR_TO_BYTE (current_pos) \
20461 : IT_BYTEPOS (*(IT)); \
20462 if (current_pos < min_pos) \
20463 { \
20464 min_pos = current_pos; \
20465 min_bpos = current_bpos; \
20466 } \
20467 if (IT_CHARPOS (*it) > max_pos) \
20468 { \
20469 max_pos = IT_CHARPOS (*it); \
20470 max_bpos = IT_BYTEPOS (*it); \
20471 } \
20472 } \
20473 while (false)
20474
20475 /* Loop generating characters. The loop is left with IT on the next
20476 character to display. */
20477 while (true)
20478 {
20479 int n_glyphs_before, hpos_before, x_before;
20480 int x, nglyphs;
20481 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20482
20483 /* Retrieve the next thing to display. Value is false if end of
20484 buffer reached. */
20485 if (!get_next_display_element (it))
20486 {
20487 /* Maybe add a space at the end of this line that is used to
20488 display the cursor there under X. Set the charpos of the
20489 first glyph of blank lines not corresponding to any text
20490 to -1. */
20491 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20492 row->exact_window_width_line_p = true;
20493 else if ((append_space_for_newline (it, true)
20494 && row->used[TEXT_AREA] == 1)
20495 || row->used[TEXT_AREA] == 0)
20496 {
20497 row->glyphs[TEXT_AREA]->charpos = -1;
20498 row->displays_text_p = false;
20499
20500 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20501 && (!MINI_WINDOW_P (it->w)
20502 || (minibuf_level && EQ (it->window, minibuf_window))))
20503 row->indicate_empty_line_p = true;
20504 }
20505
20506 it->continuation_lines_width = 0;
20507 row->ends_at_zv_p = true;
20508 /* A row that displays right-to-left text must always have
20509 its last face extended all the way to the end of line,
20510 even if this row ends in ZV, because we still write to
20511 the screen left to right. We also need to extend the
20512 last face if the default face is remapped to some
20513 different face, otherwise the functions that clear
20514 portions of the screen will clear with the default face's
20515 background color. */
20516 if (row->reversed_p
20517 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20518 extend_face_to_end_of_line (it);
20519 break;
20520 }
20521
20522 /* Now, get the metrics of what we want to display. This also
20523 generates glyphs in `row' (which is IT->glyph_row). */
20524 n_glyphs_before = row->used[TEXT_AREA];
20525 x = it->current_x;
20526
20527 /* Remember the line height so far in case the next element doesn't
20528 fit on the line. */
20529 if (it->line_wrap != TRUNCATE)
20530 {
20531 ascent = it->max_ascent;
20532 descent = it->max_descent;
20533 phys_ascent = it->max_phys_ascent;
20534 phys_descent = it->max_phys_descent;
20535
20536 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20537 {
20538 if (IT_DISPLAYING_WHITESPACE (it))
20539 may_wrap = true;
20540 else if (may_wrap)
20541 {
20542 SAVE_IT (wrap_it, *it, wrap_data);
20543 wrap_x = x;
20544 wrap_row_used = row->used[TEXT_AREA];
20545 wrap_row_ascent = row->ascent;
20546 wrap_row_height = row->height;
20547 wrap_row_phys_ascent = row->phys_ascent;
20548 wrap_row_phys_height = row->phys_height;
20549 wrap_row_extra_line_spacing = row->extra_line_spacing;
20550 wrap_row_min_pos = min_pos;
20551 wrap_row_min_bpos = min_bpos;
20552 wrap_row_max_pos = max_pos;
20553 wrap_row_max_bpos = max_bpos;
20554 may_wrap = false;
20555 }
20556 }
20557 }
20558
20559 PRODUCE_GLYPHS (it);
20560
20561 /* If this display element was in marginal areas, continue with
20562 the next one. */
20563 if (it->area != TEXT_AREA)
20564 {
20565 row->ascent = max (row->ascent, it->max_ascent);
20566 row->height = max (row->height, it->max_ascent + it->max_descent);
20567 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20568 row->phys_height = max (row->phys_height,
20569 it->max_phys_ascent + it->max_phys_descent);
20570 row->extra_line_spacing = max (row->extra_line_spacing,
20571 it->max_extra_line_spacing);
20572 set_iterator_to_next (it, true);
20573 /* If we didn't handle the line/wrap prefix above, and the
20574 call to set_iterator_to_next just switched to TEXT_AREA,
20575 process the prefix now. */
20576 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20577 {
20578 pending_handle_line_prefix = false;
20579 handle_line_prefix (it);
20580 }
20581 continue;
20582 }
20583
20584 /* Does the display element fit on the line? If we truncate
20585 lines, we should draw past the right edge of the window. If
20586 we don't truncate, we want to stop so that we can display the
20587 continuation glyph before the right margin. If lines are
20588 continued, there are two possible strategies for characters
20589 resulting in more than 1 glyph (e.g. tabs): Display as many
20590 glyphs as possible in this line and leave the rest for the
20591 continuation line, or display the whole element in the next
20592 line. Original redisplay did the former, so we do it also. */
20593 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20594 hpos_before = it->hpos;
20595 x_before = x;
20596
20597 if (/* Not a newline. */
20598 nglyphs > 0
20599 /* Glyphs produced fit entirely in the line. */
20600 && it->current_x < it->last_visible_x)
20601 {
20602 it->hpos += nglyphs;
20603 row->ascent = max (row->ascent, it->max_ascent);
20604 row->height = max (row->height, it->max_ascent + it->max_descent);
20605 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20606 row->phys_height = max (row->phys_height,
20607 it->max_phys_ascent + it->max_phys_descent);
20608 row->extra_line_spacing = max (row->extra_line_spacing,
20609 it->max_extra_line_spacing);
20610 if (it->current_x - it->pixel_width < it->first_visible_x
20611 /* In R2L rows, we arrange in extend_face_to_end_of_line
20612 to add a right offset to the line, by a suitable
20613 change to the stretch glyph that is the leftmost
20614 glyph of the line. */
20615 && !row->reversed_p)
20616 row->x = x - it->first_visible_x;
20617 /* Record the maximum and minimum buffer positions seen so
20618 far in glyphs that will be displayed by this row. */
20619 if (it->bidi_p)
20620 RECORD_MAX_MIN_POS (it);
20621 }
20622 else
20623 {
20624 int i, new_x;
20625 struct glyph *glyph;
20626
20627 for (i = 0; i < nglyphs; ++i, x = new_x)
20628 {
20629 /* Identify the glyphs added by the last call to
20630 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20631 the previous glyphs. */
20632 if (!row->reversed_p)
20633 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20634 else
20635 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20636 new_x = x + glyph->pixel_width;
20637
20638 if (/* Lines are continued. */
20639 it->line_wrap != TRUNCATE
20640 && (/* Glyph doesn't fit on the line. */
20641 new_x > it->last_visible_x
20642 /* Or it fits exactly on a window system frame. */
20643 || (new_x == it->last_visible_x
20644 && FRAME_WINDOW_P (it->f)
20645 && (row->reversed_p
20646 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20647 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20648 {
20649 /* End of a continued line. */
20650
20651 if (it->hpos == 0
20652 || (new_x == it->last_visible_x
20653 && FRAME_WINDOW_P (it->f)
20654 && (row->reversed_p
20655 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20656 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20657 {
20658 /* Current glyph is the only one on the line or
20659 fits exactly on the line. We must continue
20660 the line because we can't draw the cursor
20661 after the glyph. */
20662 row->continued_p = true;
20663 it->current_x = new_x;
20664 it->continuation_lines_width += new_x;
20665 ++it->hpos;
20666 if (i == nglyphs - 1)
20667 {
20668 /* If line-wrap is on, check if a previous
20669 wrap point was found. */
20670 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20671 && wrap_row_used > 0
20672 /* Even if there is a previous wrap
20673 point, continue the line here as
20674 usual, if (i) the previous character
20675 was a space or tab AND (ii) the
20676 current character is not. */
20677 && (!may_wrap
20678 || IT_DISPLAYING_WHITESPACE (it)))
20679 goto back_to_wrap;
20680
20681 /* Record the maximum and minimum buffer
20682 positions seen so far in glyphs that will be
20683 displayed by this row. */
20684 if (it->bidi_p)
20685 RECORD_MAX_MIN_POS (it);
20686 set_iterator_to_next (it, true);
20687 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20688 {
20689 if (!get_next_display_element (it))
20690 {
20691 row->exact_window_width_line_p = true;
20692 it->continuation_lines_width = 0;
20693 row->continued_p = false;
20694 row->ends_at_zv_p = true;
20695 }
20696 else if (ITERATOR_AT_END_OF_LINE_P (it))
20697 {
20698 row->continued_p = false;
20699 row->exact_window_width_line_p = true;
20700 }
20701 /* If line-wrap is on, check if a
20702 previous wrap point was found. */
20703 else if (wrap_row_used > 0
20704 /* Even if there is a previous wrap
20705 point, continue the line here as
20706 usual, if (i) the previous character
20707 was a space or tab AND (ii) the
20708 current character is not. */
20709 && (!may_wrap
20710 || IT_DISPLAYING_WHITESPACE (it)))
20711 goto back_to_wrap;
20712
20713 }
20714 }
20715 else if (it->bidi_p)
20716 RECORD_MAX_MIN_POS (it);
20717 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20718 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20719 extend_face_to_end_of_line (it);
20720 }
20721 else if (CHAR_GLYPH_PADDING_P (*glyph)
20722 && !FRAME_WINDOW_P (it->f))
20723 {
20724 /* A padding glyph that doesn't fit on this line.
20725 This means the whole character doesn't fit
20726 on the line. */
20727 if (row->reversed_p)
20728 unproduce_glyphs (it, row->used[TEXT_AREA]
20729 - n_glyphs_before);
20730 row->used[TEXT_AREA] = n_glyphs_before;
20731
20732 /* Fill the rest of the row with continuation
20733 glyphs like in 20.x. */
20734 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20735 < row->glyphs[1 + TEXT_AREA])
20736 produce_special_glyphs (it, IT_CONTINUATION);
20737
20738 row->continued_p = true;
20739 it->current_x = x_before;
20740 it->continuation_lines_width += x_before;
20741
20742 /* Restore the height to what it was before the
20743 element not fitting on the line. */
20744 it->max_ascent = ascent;
20745 it->max_descent = descent;
20746 it->max_phys_ascent = phys_ascent;
20747 it->max_phys_descent = phys_descent;
20748 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20749 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20750 extend_face_to_end_of_line (it);
20751 }
20752 else if (wrap_row_used > 0)
20753 {
20754 back_to_wrap:
20755 if (row->reversed_p)
20756 unproduce_glyphs (it,
20757 row->used[TEXT_AREA] - wrap_row_used);
20758 RESTORE_IT (it, &wrap_it, wrap_data);
20759 it->continuation_lines_width += wrap_x;
20760 row->used[TEXT_AREA] = wrap_row_used;
20761 row->ascent = wrap_row_ascent;
20762 row->height = wrap_row_height;
20763 row->phys_ascent = wrap_row_phys_ascent;
20764 row->phys_height = wrap_row_phys_height;
20765 row->extra_line_spacing = wrap_row_extra_line_spacing;
20766 min_pos = wrap_row_min_pos;
20767 min_bpos = wrap_row_min_bpos;
20768 max_pos = wrap_row_max_pos;
20769 max_bpos = wrap_row_max_bpos;
20770 row->continued_p = true;
20771 row->ends_at_zv_p = false;
20772 row->exact_window_width_line_p = false;
20773 it->continuation_lines_width += x;
20774
20775 /* Make sure that a non-default face is extended
20776 up to the right margin of the window. */
20777 extend_face_to_end_of_line (it);
20778 }
20779 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20780 {
20781 /* A TAB that extends past the right edge of the
20782 window. This produces a single glyph on
20783 window system frames. We leave the glyph in
20784 this row and let it fill the row, but don't
20785 consume the TAB. */
20786 if ((row->reversed_p
20787 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20788 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20789 produce_special_glyphs (it, IT_CONTINUATION);
20790 it->continuation_lines_width += it->last_visible_x;
20791 row->ends_in_middle_of_char_p = true;
20792 row->continued_p = true;
20793 glyph->pixel_width = it->last_visible_x - x;
20794 it->starts_in_middle_of_char_p = true;
20795 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20796 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20797 extend_face_to_end_of_line (it);
20798 }
20799 else
20800 {
20801 /* Something other than a TAB that draws past
20802 the right edge of the window. Restore
20803 positions to values before the element. */
20804 if (row->reversed_p)
20805 unproduce_glyphs (it, row->used[TEXT_AREA]
20806 - (n_glyphs_before + i));
20807 row->used[TEXT_AREA] = n_glyphs_before + i;
20808
20809 /* Display continuation glyphs. */
20810 it->current_x = x_before;
20811 it->continuation_lines_width += x;
20812 if (!FRAME_WINDOW_P (it->f)
20813 || (row->reversed_p
20814 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20815 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20816 produce_special_glyphs (it, IT_CONTINUATION);
20817 row->continued_p = true;
20818
20819 extend_face_to_end_of_line (it);
20820
20821 if (nglyphs > 1 && i > 0)
20822 {
20823 row->ends_in_middle_of_char_p = true;
20824 it->starts_in_middle_of_char_p = true;
20825 }
20826
20827 /* Restore the height to what it was before the
20828 element not fitting on the line. */
20829 it->max_ascent = ascent;
20830 it->max_descent = descent;
20831 it->max_phys_ascent = phys_ascent;
20832 it->max_phys_descent = phys_descent;
20833 }
20834
20835 break;
20836 }
20837 else if (new_x > it->first_visible_x)
20838 {
20839 /* Increment number of glyphs actually displayed. */
20840 ++it->hpos;
20841
20842 /* Record the maximum and minimum buffer positions
20843 seen so far in glyphs that will be displayed by
20844 this row. */
20845 if (it->bidi_p)
20846 RECORD_MAX_MIN_POS (it);
20847
20848 if (x < it->first_visible_x && !row->reversed_p)
20849 /* Glyph is partially visible, i.e. row starts at
20850 negative X position. Don't do that in R2L
20851 rows, where we arrange to add a right offset to
20852 the line in extend_face_to_end_of_line, by a
20853 suitable change to the stretch glyph that is
20854 the leftmost glyph of the line. */
20855 row->x = x - it->first_visible_x;
20856 /* When the last glyph of an R2L row only fits
20857 partially on the line, we need to set row->x to a
20858 negative offset, so that the leftmost glyph is
20859 the one that is partially visible. But if we are
20860 going to produce the truncation glyph, this will
20861 be taken care of in produce_special_glyphs. */
20862 if (row->reversed_p
20863 && new_x > it->last_visible_x
20864 && !(it->line_wrap == TRUNCATE
20865 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20866 {
20867 eassert (FRAME_WINDOW_P (it->f));
20868 row->x = it->last_visible_x - new_x;
20869 }
20870 }
20871 else
20872 {
20873 /* Glyph is completely off the left margin of the
20874 window. This should not happen because of the
20875 move_it_in_display_line at the start of this
20876 function, unless the text display area of the
20877 window is empty. */
20878 eassert (it->first_visible_x <= it->last_visible_x);
20879 }
20880 }
20881 /* Even if this display element produced no glyphs at all,
20882 we want to record its position. */
20883 if (it->bidi_p && nglyphs == 0)
20884 RECORD_MAX_MIN_POS (it);
20885
20886 row->ascent = max (row->ascent, it->max_ascent);
20887 row->height = max (row->height, it->max_ascent + it->max_descent);
20888 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20889 row->phys_height = max (row->phys_height,
20890 it->max_phys_ascent + it->max_phys_descent);
20891 row->extra_line_spacing = max (row->extra_line_spacing,
20892 it->max_extra_line_spacing);
20893
20894 /* End of this display line if row is continued. */
20895 if (row->continued_p || row->ends_at_zv_p)
20896 break;
20897 }
20898
20899 at_end_of_line:
20900 /* Is this a line end? If yes, we're also done, after making
20901 sure that a non-default face is extended up to the right
20902 margin of the window. */
20903 if (ITERATOR_AT_END_OF_LINE_P (it))
20904 {
20905 int used_before = row->used[TEXT_AREA];
20906
20907 row->ends_in_newline_from_string_p = STRINGP (it->object);
20908
20909 /* Add a space at the end of the line that is used to
20910 display the cursor there. */
20911 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20912 append_space_for_newline (it, false);
20913
20914 /* Extend the face to the end of the line. */
20915 extend_face_to_end_of_line (it);
20916
20917 /* Make sure we have the position. */
20918 if (used_before == 0)
20919 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20920
20921 /* Record the position of the newline, for use in
20922 find_row_edges. */
20923 it->eol_pos = it->current.pos;
20924
20925 /* Consume the line end. This skips over invisible lines. */
20926 set_iterator_to_next (it, true);
20927 it->continuation_lines_width = 0;
20928 break;
20929 }
20930
20931 /* Proceed with next display element. Note that this skips
20932 over lines invisible because of selective display. */
20933 set_iterator_to_next (it, true);
20934
20935 /* If we truncate lines, we are done when the last displayed
20936 glyphs reach past the right margin of the window. */
20937 if (it->line_wrap == TRUNCATE
20938 && ((FRAME_WINDOW_P (it->f)
20939 /* Images are preprocessed in produce_image_glyph such
20940 that they are cropped at the right edge of the
20941 window, so an image glyph will always end exactly at
20942 last_visible_x, even if there's no right fringe. */
20943 && ((row->reversed_p
20944 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20945 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20946 || it->what == IT_IMAGE))
20947 ? (it->current_x >= it->last_visible_x)
20948 : (it->current_x > it->last_visible_x)))
20949 {
20950 /* Maybe add truncation glyphs. */
20951 if (!FRAME_WINDOW_P (it->f)
20952 || (row->reversed_p
20953 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20954 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20955 {
20956 int i, n;
20957
20958 if (!row->reversed_p)
20959 {
20960 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20961 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20962 break;
20963 }
20964 else
20965 {
20966 for (i = 0; i < row->used[TEXT_AREA]; i++)
20967 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20968 break;
20969 /* Remove any padding glyphs at the front of ROW, to
20970 make room for the truncation glyphs we will be
20971 adding below. The loop below always inserts at
20972 least one truncation glyph, so also remove the
20973 last glyph added to ROW. */
20974 unproduce_glyphs (it, i + 1);
20975 /* Adjust i for the loop below. */
20976 i = row->used[TEXT_AREA] - (i + 1);
20977 }
20978
20979 /* produce_special_glyphs overwrites the last glyph, so
20980 we don't want that if we want to keep that last
20981 glyph, which means it's an image. */
20982 if (it->current_x > it->last_visible_x)
20983 {
20984 it->current_x = x_before;
20985 if (!FRAME_WINDOW_P (it->f))
20986 {
20987 for (n = row->used[TEXT_AREA]; i < n; ++i)
20988 {
20989 row->used[TEXT_AREA] = i;
20990 produce_special_glyphs (it, IT_TRUNCATION);
20991 }
20992 }
20993 else
20994 {
20995 row->used[TEXT_AREA] = i;
20996 produce_special_glyphs (it, IT_TRUNCATION);
20997 }
20998 it->hpos = hpos_before;
20999 }
21000 }
21001 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21002 {
21003 /* Don't truncate if we can overflow newline into fringe. */
21004 if (!get_next_display_element (it))
21005 {
21006 it->continuation_lines_width = 0;
21007 row->ends_at_zv_p = true;
21008 row->exact_window_width_line_p = true;
21009 break;
21010 }
21011 if (ITERATOR_AT_END_OF_LINE_P (it))
21012 {
21013 row->exact_window_width_line_p = true;
21014 goto at_end_of_line;
21015 }
21016 it->current_x = x_before;
21017 it->hpos = hpos_before;
21018 }
21019
21020 row->truncated_on_right_p = true;
21021 it->continuation_lines_width = 0;
21022 reseat_at_next_visible_line_start (it, false);
21023 /* We insist below that IT's position be at ZV because in
21024 bidi-reordered lines the character at visible line start
21025 might not be the character that follows the newline in
21026 the logical order. */
21027 if (IT_BYTEPOS (*it) > BEG_BYTE)
21028 row->ends_at_zv_p =
21029 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21030 else
21031 row->ends_at_zv_p = false;
21032 break;
21033 }
21034 }
21035
21036 if (wrap_data)
21037 bidi_unshelve_cache (wrap_data, true);
21038
21039 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21040 at the left window margin. */
21041 if (it->first_visible_x
21042 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21043 {
21044 if (!FRAME_WINDOW_P (it->f)
21045 || (((row->reversed_p
21046 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21047 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21048 /* Don't let insert_left_trunc_glyphs overwrite the
21049 first glyph of the row if it is an image. */
21050 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21051 insert_left_trunc_glyphs (it);
21052 row->truncated_on_left_p = true;
21053 }
21054
21055 /* Remember the position at which this line ends.
21056
21057 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21058 cannot be before the call to find_row_edges below, since that is
21059 where these positions are determined. */
21060 row->end = it->current;
21061 if (!it->bidi_p)
21062 {
21063 row->minpos = row->start.pos;
21064 row->maxpos = row->end.pos;
21065 }
21066 else
21067 {
21068 /* ROW->minpos and ROW->maxpos must be the smallest and
21069 `1 + the largest' buffer positions in ROW. But if ROW was
21070 bidi-reordered, these two positions can be anywhere in the
21071 row, so we must determine them now. */
21072 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21073 }
21074
21075 /* If the start of this line is the overlay arrow-position, then
21076 mark this glyph row as the one containing the overlay arrow.
21077 This is clearly a mess with variable size fonts. It would be
21078 better to let it be displayed like cursors under X. */
21079 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21080 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21081 !NILP (overlay_arrow_string)))
21082 {
21083 /* Overlay arrow in window redisplay is a fringe bitmap. */
21084 if (STRINGP (overlay_arrow_string))
21085 {
21086 struct glyph_row *arrow_row
21087 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21088 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21089 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21090 struct glyph *p = row->glyphs[TEXT_AREA];
21091 struct glyph *p2, *end;
21092
21093 /* Copy the arrow glyphs. */
21094 while (glyph < arrow_end)
21095 *p++ = *glyph++;
21096
21097 /* Throw away padding glyphs. */
21098 p2 = p;
21099 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21100 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21101 ++p2;
21102 if (p2 > p)
21103 {
21104 while (p2 < end)
21105 *p++ = *p2++;
21106 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21107 }
21108 }
21109 else
21110 {
21111 eassert (INTEGERP (overlay_arrow_string));
21112 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21113 }
21114 overlay_arrow_seen = true;
21115 }
21116
21117 /* Highlight trailing whitespace. */
21118 if (!NILP (Vshow_trailing_whitespace))
21119 highlight_trailing_whitespace (it->f, it->glyph_row);
21120
21121 /* Compute pixel dimensions of this line. */
21122 compute_line_metrics (it);
21123
21124 /* Implementation note: No changes in the glyphs of ROW or in their
21125 faces can be done past this point, because compute_line_metrics
21126 computes ROW's hash value and stores it within the glyph_row
21127 structure. */
21128
21129 /* Record whether this row ends inside an ellipsis. */
21130 row->ends_in_ellipsis_p
21131 = (it->method == GET_FROM_DISPLAY_VECTOR
21132 && it->ellipsis_p);
21133
21134 /* Save fringe bitmaps in this row. */
21135 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
21136 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
21137 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
21138 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
21139
21140 it->left_user_fringe_bitmap = 0;
21141 it->left_user_fringe_face_id = 0;
21142 it->right_user_fringe_bitmap = 0;
21143 it->right_user_fringe_face_id = 0;
21144
21145 /* Maybe set the cursor. */
21146 cvpos = it->w->cursor.vpos;
21147 if ((cvpos < 0
21148 /* In bidi-reordered rows, keep checking for proper cursor
21149 position even if one has been found already, because buffer
21150 positions in such rows change non-linearly with ROW->VPOS,
21151 when a line is continued. One exception: when we are at ZV,
21152 display cursor on the first suitable glyph row, since all
21153 the empty rows after that also have their position set to ZV. */
21154 /* FIXME: Revisit this when glyph ``spilling'' in continuation
21155 lines' rows is implemented for bidi-reordered rows. */
21156 || (it->bidi_p
21157 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
21158 && PT >= MATRIX_ROW_START_CHARPOS (row)
21159 && PT <= MATRIX_ROW_END_CHARPOS (row)
21160 && cursor_row_p (row))
21161 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21162
21163 /* Prepare for the next line. This line starts horizontally at (X
21164 HPOS) = (0 0). Vertical positions are incremented. As a
21165 convenience for the caller, IT->glyph_row is set to the next
21166 row to be used. */
21167 it->current_x = it->hpos = 0;
21168 it->current_y += row->height;
21169 SET_TEXT_POS (it->eol_pos, 0, 0);
21170 ++it->vpos;
21171 ++it->glyph_row;
21172 /* The next row should by default use the same value of the
21173 reversed_p flag as this one. set_iterator_to_next decides when
21174 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21175 the flag accordingly. */
21176 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21177 it->glyph_row->reversed_p = row->reversed_p;
21178 it->start = row->end;
21179 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21180
21181 #undef RECORD_MAX_MIN_POS
21182 }
21183
21184 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21185 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21186 doc: /* Return paragraph direction at point in BUFFER.
21187 Value is either `left-to-right' or `right-to-left'.
21188 If BUFFER is omitted or nil, it defaults to the current buffer.
21189
21190 Paragraph direction determines how the text in the paragraph is displayed.
21191 In left-to-right paragraphs, text begins at the left margin of the window
21192 and the reading direction is generally left to right. In right-to-left
21193 paragraphs, text begins at the right margin and is read from right to left.
21194
21195 See also `bidi-paragraph-direction'. */)
21196 (Lisp_Object buffer)
21197 {
21198 struct buffer *buf = current_buffer;
21199 struct buffer *old = buf;
21200
21201 if (! NILP (buffer))
21202 {
21203 CHECK_BUFFER (buffer);
21204 buf = XBUFFER (buffer);
21205 }
21206
21207 if (NILP (BVAR (buf, bidi_display_reordering))
21208 || NILP (BVAR (buf, enable_multibyte_characters))
21209 /* When we are loading loadup.el, the character property tables
21210 needed for bidi iteration are not yet available. */
21211 || !NILP (Vpurify_flag))
21212 return Qleft_to_right;
21213 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21214 return BVAR (buf, bidi_paragraph_direction);
21215 else
21216 {
21217 /* Determine the direction from buffer text. We could try to
21218 use current_matrix if it is up to date, but this seems fast
21219 enough as it is. */
21220 struct bidi_it itb;
21221 ptrdiff_t pos = BUF_PT (buf);
21222 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21223 int c;
21224 void *itb_data = bidi_shelve_cache ();
21225
21226 set_buffer_temp (buf);
21227 /* bidi_paragraph_init finds the base direction of the paragraph
21228 by searching forward from paragraph start. We need the base
21229 direction of the current or _previous_ paragraph, so we need
21230 to make sure we are within that paragraph. To that end, find
21231 the previous non-empty line. */
21232 if (pos >= ZV && pos > BEGV)
21233 DEC_BOTH (pos, bytepos);
21234 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21235 if (fast_looking_at (trailing_white_space,
21236 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21237 {
21238 while ((c = FETCH_BYTE (bytepos)) == '\n'
21239 || c == ' ' || c == '\t' || c == '\f')
21240 {
21241 if (bytepos <= BEGV_BYTE)
21242 break;
21243 bytepos--;
21244 pos--;
21245 }
21246 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21247 bytepos--;
21248 }
21249 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21250 itb.paragraph_dir = NEUTRAL_DIR;
21251 itb.string.s = NULL;
21252 itb.string.lstring = Qnil;
21253 itb.string.bufpos = 0;
21254 itb.string.from_disp_str = false;
21255 itb.string.unibyte = false;
21256 /* We have no window to use here for ignoring window-specific
21257 overlays. Using NULL for window pointer will cause
21258 compute_display_string_pos to use the current buffer. */
21259 itb.w = NULL;
21260 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21261 bidi_unshelve_cache (itb_data, false);
21262 set_buffer_temp (old);
21263 switch (itb.paragraph_dir)
21264 {
21265 case L2R:
21266 return Qleft_to_right;
21267 break;
21268 case R2L:
21269 return Qright_to_left;
21270 break;
21271 default:
21272 emacs_abort ();
21273 }
21274 }
21275 }
21276
21277 DEFUN ("bidi-find-overridden-directionality",
21278 Fbidi_find_overridden_directionality,
21279 Sbidi_find_overridden_directionality, 2, 3, 0,
21280 doc: /* Return position between FROM and TO where directionality was overridden.
21281
21282 This function returns the first character position in the specified
21283 region of OBJECT where there is a character whose `bidi-class' property
21284 is `L', but which was forced to display as `R' by a directional
21285 override, and likewise with characters whose `bidi-class' is `R'
21286 or `AL' that were forced to display as `L'.
21287
21288 If no such character is found, the function returns nil.
21289
21290 OBJECT is a Lisp string or buffer to search for overridden
21291 directionality, and defaults to the current buffer if nil or omitted.
21292 OBJECT can also be a window, in which case the function will search
21293 the buffer displayed in that window. Passing the window instead of
21294 a buffer is preferable when the buffer is displayed in some window,
21295 because this function will then be able to correctly account for
21296 window-specific overlays, which can affect the results.
21297
21298 Strong directional characters `L', `R', and `AL' can have their
21299 intrinsic directionality overridden by directional override
21300 control characters RLO (u+202e) and LRO (u+202d). See the
21301 function `get-char-code-property' for a way to inquire about
21302 the `bidi-class' property of a character. */)
21303 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21304 {
21305 struct buffer *buf = current_buffer;
21306 struct buffer *old = buf;
21307 struct window *w = NULL;
21308 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21309 struct bidi_it itb;
21310 ptrdiff_t from_pos, to_pos, from_bpos;
21311 void *itb_data;
21312
21313 if (!NILP (object))
21314 {
21315 if (BUFFERP (object))
21316 buf = XBUFFER (object);
21317 else if (WINDOWP (object))
21318 {
21319 w = decode_live_window (object);
21320 buf = XBUFFER (w->contents);
21321 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21322 }
21323 else
21324 CHECK_STRING (object);
21325 }
21326
21327 if (STRINGP (object))
21328 {
21329 /* Characters in unibyte strings are always treated by bidi.c as
21330 strong LTR. */
21331 if (!STRING_MULTIBYTE (object)
21332 /* When we are loading loadup.el, the character property
21333 tables needed for bidi iteration are not yet
21334 available. */
21335 || !NILP (Vpurify_flag))
21336 return Qnil;
21337
21338 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21339 if (from_pos >= SCHARS (object))
21340 return Qnil;
21341
21342 /* Set up the bidi iterator. */
21343 itb_data = bidi_shelve_cache ();
21344 itb.paragraph_dir = NEUTRAL_DIR;
21345 itb.string.lstring = object;
21346 itb.string.s = NULL;
21347 itb.string.schars = SCHARS (object);
21348 itb.string.bufpos = 0;
21349 itb.string.from_disp_str = false;
21350 itb.string.unibyte = false;
21351 itb.w = w;
21352 bidi_init_it (0, 0, frame_window_p, &itb);
21353 }
21354 else
21355 {
21356 /* Nothing this fancy can happen in unibyte buffers, or in a
21357 buffer that disabled reordering, or if FROM is at EOB. */
21358 if (NILP (BVAR (buf, bidi_display_reordering))
21359 || NILP (BVAR (buf, enable_multibyte_characters))
21360 /* When we are loading loadup.el, the character property
21361 tables needed for bidi iteration are not yet
21362 available. */
21363 || !NILP (Vpurify_flag))
21364 return Qnil;
21365
21366 set_buffer_temp (buf);
21367 validate_region (&from, &to);
21368 from_pos = XINT (from);
21369 to_pos = XINT (to);
21370 if (from_pos >= ZV)
21371 return Qnil;
21372
21373 /* Set up the bidi iterator. */
21374 itb_data = bidi_shelve_cache ();
21375 from_bpos = CHAR_TO_BYTE (from_pos);
21376 if (from_pos == BEGV)
21377 {
21378 itb.charpos = BEGV;
21379 itb.bytepos = BEGV_BYTE;
21380 }
21381 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21382 {
21383 itb.charpos = from_pos;
21384 itb.bytepos = from_bpos;
21385 }
21386 else
21387 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21388 -1, &itb.bytepos);
21389 itb.paragraph_dir = NEUTRAL_DIR;
21390 itb.string.s = NULL;
21391 itb.string.lstring = Qnil;
21392 itb.string.bufpos = 0;
21393 itb.string.from_disp_str = false;
21394 itb.string.unibyte = false;
21395 itb.w = w;
21396 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21397 }
21398
21399 ptrdiff_t found;
21400 do {
21401 /* For the purposes of this function, the actual base direction of
21402 the paragraph doesn't matter, so just set it to L2R. */
21403 bidi_paragraph_init (L2R, &itb, false);
21404 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21405 ;
21406 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21407
21408 bidi_unshelve_cache (itb_data, false);
21409 set_buffer_temp (old);
21410
21411 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21412 }
21413
21414 DEFUN ("move-point-visually", Fmove_point_visually,
21415 Smove_point_visually, 1, 1, 0,
21416 doc: /* Move point in the visual order in the specified DIRECTION.
21417 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21418 left.
21419
21420 Value is the new character position of point. */)
21421 (Lisp_Object direction)
21422 {
21423 struct window *w = XWINDOW (selected_window);
21424 struct buffer *b = XBUFFER (w->contents);
21425 struct glyph_row *row;
21426 int dir;
21427 Lisp_Object paragraph_dir;
21428
21429 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21430 (!(ROW)->continued_p \
21431 && NILP ((GLYPH)->object) \
21432 && (GLYPH)->type == CHAR_GLYPH \
21433 && (GLYPH)->u.ch == ' ' \
21434 && (GLYPH)->charpos >= 0 \
21435 && !(GLYPH)->avoid_cursor_p)
21436
21437 CHECK_NUMBER (direction);
21438 dir = XINT (direction);
21439 if (dir > 0)
21440 dir = 1;
21441 else
21442 dir = -1;
21443
21444 /* If current matrix is up-to-date, we can use the information
21445 recorded in the glyphs, at least as long as the goal is on the
21446 screen. */
21447 if (w->window_end_valid
21448 && !windows_or_buffers_changed
21449 && b
21450 && !b->clip_changed
21451 && !b->prevent_redisplay_optimizations_p
21452 && !window_outdated (w)
21453 /* We rely below on the cursor coordinates to be up to date, but
21454 we cannot trust them if some command moved point since the
21455 last complete redisplay. */
21456 && w->last_point == BUF_PT (b)
21457 && w->cursor.vpos >= 0
21458 && w->cursor.vpos < w->current_matrix->nrows
21459 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21460 {
21461 struct glyph *g = row->glyphs[TEXT_AREA];
21462 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21463 struct glyph *gpt = g + w->cursor.hpos;
21464
21465 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21466 {
21467 if (BUFFERP (g->object) && g->charpos != PT)
21468 {
21469 SET_PT (g->charpos);
21470 w->cursor.vpos = -1;
21471 return make_number (PT);
21472 }
21473 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21474 {
21475 ptrdiff_t new_pos;
21476
21477 if (BUFFERP (gpt->object))
21478 {
21479 new_pos = PT;
21480 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21481 new_pos += (row->reversed_p ? -dir : dir);
21482 else
21483 new_pos -= (row->reversed_p ? -dir : dir);
21484 }
21485 else if (BUFFERP (g->object))
21486 new_pos = g->charpos;
21487 else
21488 break;
21489 SET_PT (new_pos);
21490 w->cursor.vpos = -1;
21491 return make_number (PT);
21492 }
21493 else if (ROW_GLYPH_NEWLINE_P (row, g))
21494 {
21495 /* Glyphs inserted at the end of a non-empty line for
21496 positioning the cursor have zero charpos, so we must
21497 deduce the value of point by other means. */
21498 if (g->charpos > 0)
21499 SET_PT (g->charpos);
21500 else if (row->ends_at_zv_p && PT != ZV)
21501 SET_PT (ZV);
21502 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21503 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21504 else
21505 break;
21506 w->cursor.vpos = -1;
21507 return make_number (PT);
21508 }
21509 }
21510 if (g == e || NILP (g->object))
21511 {
21512 if (row->truncated_on_left_p || row->truncated_on_right_p)
21513 goto simulate_display;
21514 if (!row->reversed_p)
21515 row += dir;
21516 else
21517 row -= dir;
21518 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21519 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21520 goto simulate_display;
21521
21522 if (dir > 0)
21523 {
21524 if (row->reversed_p && !row->continued_p)
21525 {
21526 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21527 w->cursor.vpos = -1;
21528 return make_number (PT);
21529 }
21530 g = row->glyphs[TEXT_AREA];
21531 e = g + row->used[TEXT_AREA];
21532 for ( ; g < e; g++)
21533 {
21534 if (BUFFERP (g->object)
21535 /* Empty lines have only one glyph, which stands
21536 for the newline, and whose charpos is the
21537 buffer position of the newline. */
21538 || ROW_GLYPH_NEWLINE_P (row, g)
21539 /* When the buffer ends in a newline, the line at
21540 EOB also has one glyph, but its charpos is -1. */
21541 || (row->ends_at_zv_p
21542 && !row->reversed_p
21543 && NILP (g->object)
21544 && g->type == CHAR_GLYPH
21545 && g->u.ch == ' '))
21546 {
21547 if (g->charpos > 0)
21548 SET_PT (g->charpos);
21549 else if (!row->reversed_p
21550 && row->ends_at_zv_p
21551 && PT != ZV)
21552 SET_PT (ZV);
21553 else
21554 continue;
21555 w->cursor.vpos = -1;
21556 return make_number (PT);
21557 }
21558 }
21559 }
21560 else
21561 {
21562 if (!row->reversed_p && !row->continued_p)
21563 {
21564 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21565 w->cursor.vpos = -1;
21566 return make_number (PT);
21567 }
21568 e = row->glyphs[TEXT_AREA];
21569 g = e + row->used[TEXT_AREA] - 1;
21570 for ( ; g >= e; g--)
21571 {
21572 if (BUFFERP (g->object)
21573 || (ROW_GLYPH_NEWLINE_P (row, g)
21574 && g->charpos > 0)
21575 /* Empty R2L lines on GUI frames have the buffer
21576 position of the newline stored in the stretch
21577 glyph. */
21578 || g->type == STRETCH_GLYPH
21579 || (row->ends_at_zv_p
21580 && row->reversed_p
21581 && NILP (g->object)
21582 && g->type == CHAR_GLYPH
21583 && g->u.ch == ' '))
21584 {
21585 if (g->charpos > 0)
21586 SET_PT (g->charpos);
21587 else if (row->reversed_p
21588 && row->ends_at_zv_p
21589 && PT != ZV)
21590 SET_PT (ZV);
21591 else
21592 continue;
21593 w->cursor.vpos = -1;
21594 return make_number (PT);
21595 }
21596 }
21597 }
21598 }
21599 }
21600
21601 simulate_display:
21602
21603 /* If we wind up here, we failed to move by using the glyphs, so we
21604 need to simulate display instead. */
21605
21606 if (b)
21607 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21608 else
21609 paragraph_dir = Qleft_to_right;
21610 if (EQ (paragraph_dir, Qright_to_left))
21611 dir = -dir;
21612 if (PT <= BEGV && dir < 0)
21613 xsignal0 (Qbeginning_of_buffer);
21614 else if (PT >= ZV && dir > 0)
21615 xsignal0 (Qend_of_buffer);
21616 else
21617 {
21618 struct text_pos pt;
21619 struct it it;
21620 int pt_x, target_x, pixel_width, pt_vpos;
21621 bool at_eol_p;
21622 bool overshoot_expected = false;
21623 bool target_is_eol_p = false;
21624
21625 /* Setup the arena. */
21626 SET_TEXT_POS (pt, PT, PT_BYTE);
21627 start_display (&it, w, pt);
21628 /* When lines are truncated, we could be called with point
21629 outside of the windows edges, in which case move_it_*
21630 functions either prematurely stop at window's edge or jump to
21631 the next screen line, whereas we rely below on our ability to
21632 reach point, in order to start from its X coordinate. So we
21633 need to disregard the window's horizontal extent in that case. */
21634 if (it.line_wrap == TRUNCATE)
21635 it.last_visible_x = INFINITY;
21636
21637 if (it.cmp_it.id < 0
21638 && it.method == GET_FROM_STRING
21639 && it.area == TEXT_AREA
21640 && it.string_from_display_prop_p
21641 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21642 overshoot_expected = true;
21643
21644 /* Find the X coordinate of point. We start from the beginning
21645 of this or previous line to make sure we are before point in
21646 the logical order (since the move_it_* functions can only
21647 move forward). */
21648 reseat:
21649 reseat_at_previous_visible_line_start (&it);
21650 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21651 if (IT_CHARPOS (it) != PT)
21652 {
21653 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21654 -1, -1, -1, MOVE_TO_POS);
21655 /* If we missed point because the character there is
21656 displayed out of a display vector that has more than one
21657 glyph, retry expecting overshoot. */
21658 if (it.method == GET_FROM_DISPLAY_VECTOR
21659 && it.current.dpvec_index > 0
21660 && !overshoot_expected)
21661 {
21662 overshoot_expected = true;
21663 goto reseat;
21664 }
21665 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21666 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21667 }
21668 pt_x = it.current_x;
21669 pt_vpos = it.vpos;
21670 if (dir > 0 || overshoot_expected)
21671 {
21672 struct glyph_row *row = it.glyph_row;
21673
21674 /* When point is at beginning of line, we don't have
21675 information about the glyph there loaded into struct
21676 it. Calling get_next_display_element fixes that. */
21677 if (pt_x == 0)
21678 get_next_display_element (&it);
21679 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21680 it.glyph_row = NULL;
21681 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21682 it.glyph_row = row;
21683 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21684 it, lest it will become out of sync with it's buffer
21685 position. */
21686 it.current_x = pt_x;
21687 }
21688 else
21689 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21690 pixel_width = it.pixel_width;
21691 if (overshoot_expected && at_eol_p)
21692 pixel_width = 0;
21693 else if (pixel_width <= 0)
21694 pixel_width = 1;
21695
21696 /* If there's a display string (or something similar) at point,
21697 we are actually at the glyph to the left of point, so we need
21698 to correct the X coordinate. */
21699 if (overshoot_expected)
21700 {
21701 if (it.bidi_p)
21702 pt_x += pixel_width * it.bidi_it.scan_dir;
21703 else
21704 pt_x += pixel_width;
21705 }
21706
21707 /* Compute target X coordinate, either to the left or to the
21708 right of point. On TTY frames, all characters have the same
21709 pixel width of 1, so we can use that. On GUI frames we don't
21710 have an easy way of getting at the pixel width of the
21711 character to the left of point, so we use a different method
21712 of getting to that place. */
21713 if (dir > 0)
21714 target_x = pt_x + pixel_width;
21715 else
21716 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21717
21718 /* Target X coordinate could be one line above or below the line
21719 of point, in which case we need to adjust the target X
21720 coordinate. Also, if moving to the left, we need to begin at
21721 the left edge of the point's screen line. */
21722 if (dir < 0)
21723 {
21724 if (pt_x > 0)
21725 {
21726 start_display (&it, w, pt);
21727 if (it.line_wrap == TRUNCATE)
21728 it.last_visible_x = INFINITY;
21729 reseat_at_previous_visible_line_start (&it);
21730 it.current_x = it.current_y = it.hpos = 0;
21731 if (pt_vpos != 0)
21732 move_it_by_lines (&it, pt_vpos);
21733 }
21734 else
21735 {
21736 move_it_by_lines (&it, -1);
21737 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21738 target_is_eol_p = true;
21739 /* Under word-wrap, we don't know the x coordinate of
21740 the last character displayed on the previous line,
21741 which immediately precedes the wrap point. To find
21742 out its x coordinate, we try moving to the right
21743 margin of the window, which will stop at the wrap
21744 point, and then reset target_x to point at the
21745 character that precedes the wrap point. This is not
21746 needed on GUI frames, because (see below) there we
21747 move from the left margin one grapheme cluster at a
21748 time, and stop when we hit the wrap point. */
21749 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21750 {
21751 void *it_data = NULL;
21752 struct it it2;
21753
21754 SAVE_IT (it2, it, it_data);
21755 move_it_in_display_line_to (&it, ZV, target_x,
21756 MOVE_TO_POS | MOVE_TO_X);
21757 /* If we arrived at target_x, that _is_ the last
21758 character on the previous line. */
21759 if (it.current_x != target_x)
21760 target_x = it.current_x - 1;
21761 RESTORE_IT (&it, &it2, it_data);
21762 }
21763 }
21764 }
21765 else
21766 {
21767 if (at_eol_p
21768 || (target_x >= it.last_visible_x
21769 && it.line_wrap != TRUNCATE))
21770 {
21771 if (pt_x > 0)
21772 move_it_by_lines (&it, 0);
21773 move_it_by_lines (&it, 1);
21774 target_x = 0;
21775 }
21776 }
21777
21778 /* Move to the target X coordinate. */
21779 #ifdef HAVE_WINDOW_SYSTEM
21780 /* On GUI frames, as we don't know the X coordinate of the
21781 character to the left of point, moving point to the left
21782 requires walking, one grapheme cluster at a time, until we
21783 find ourself at a place immediately to the left of the
21784 character at point. */
21785 if (FRAME_WINDOW_P (it.f) && dir < 0)
21786 {
21787 struct text_pos new_pos;
21788 enum move_it_result rc = MOVE_X_REACHED;
21789
21790 if (it.current_x == 0)
21791 get_next_display_element (&it);
21792 if (it.what == IT_COMPOSITION)
21793 {
21794 new_pos.charpos = it.cmp_it.charpos;
21795 new_pos.bytepos = -1;
21796 }
21797 else
21798 new_pos = it.current.pos;
21799
21800 while (it.current_x + it.pixel_width <= target_x
21801 && (rc == MOVE_X_REACHED
21802 /* Under word-wrap, move_it_in_display_line_to
21803 stops at correct coordinates, but sometimes
21804 returns MOVE_POS_MATCH_OR_ZV. */
21805 || (it.line_wrap == WORD_WRAP
21806 && rc == MOVE_POS_MATCH_OR_ZV)))
21807 {
21808 int new_x = it.current_x + it.pixel_width;
21809
21810 /* For composed characters, we want the position of the
21811 first character in the grapheme cluster (usually, the
21812 composition's base character), whereas it.current
21813 might give us the position of the _last_ one, e.g. if
21814 the composition is rendered in reverse due to bidi
21815 reordering. */
21816 if (it.what == IT_COMPOSITION)
21817 {
21818 new_pos.charpos = it.cmp_it.charpos;
21819 new_pos.bytepos = -1;
21820 }
21821 else
21822 new_pos = it.current.pos;
21823 if (new_x == it.current_x)
21824 new_x++;
21825 rc = move_it_in_display_line_to (&it, ZV, new_x,
21826 MOVE_TO_POS | MOVE_TO_X);
21827 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21828 break;
21829 }
21830 /* The previous position we saw in the loop is the one we
21831 want. */
21832 if (new_pos.bytepos == -1)
21833 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21834 it.current.pos = new_pos;
21835 }
21836 else
21837 #endif
21838 if (it.current_x != target_x)
21839 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21840
21841 /* If we ended up in a display string that covers point, move to
21842 buffer position to the right in the visual order. */
21843 if (dir > 0)
21844 {
21845 while (IT_CHARPOS (it) == PT)
21846 {
21847 set_iterator_to_next (&it, false);
21848 if (!get_next_display_element (&it))
21849 break;
21850 }
21851 }
21852
21853 /* Move point to that position. */
21854 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21855 }
21856
21857 return make_number (PT);
21858
21859 #undef ROW_GLYPH_NEWLINE_P
21860 }
21861
21862 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21863 Sbidi_resolved_levels, 0, 1, 0,
21864 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21865
21866 The resolved levels are produced by the Emacs bidi reordering engine
21867 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21868 read the Unicode Standard Annex 9 (UAX#9) for background information
21869 about these levels.
21870
21871 VPOS is the zero-based number of the current window's screen line
21872 for which to produce the resolved levels. If VPOS is nil or omitted,
21873 it defaults to the screen line of point. If the window displays a
21874 header line, VPOS of zero will report on the header line, and first
21875 line of text in the window will have VPOS of 1.
21876
21877 Value is an array of resolved levels, indexed by glyph number.
21878 Glyphs are numbered from zero starting from the beginning of the
21879 screen line, i.e. the left edge of the window for left-to-right lines
21880 and from the right edge for right-to-left lines. The resolved levels
21881 are produced only for the window's text area; text in display margins
21882 is not included.
21883
21884 If the selected window's display is not up-to-date, or if the specified
21885 screen line does not display text, this function returns nil. It is
21886 highly recommended to bind this function to some simple key, like F8,
21887 in order to avoid these problems.
21888
21889 This function exists mainly for testing the correctness of the
21890 Emacs UBA implementation, in particular with the test suite. */)
21891 (Lisp_Object vpos)
21892 {
21893 struct window *w = XWINDOW (selected_window);
21894 struct buffer *b = XBUFFER (w->contents);
21895 int nrow;
21896 struct glyph_row *row;
21897
21898 if (NILP (vpos))
21899 {
21900 int d1, d2, d3, d4, d5;
21901
21902 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21903 }
21904 else
21905 {
21906 CHECK_NUMBER_COERCE_MARKER (vpos);
21907 nrow = XINT (vpos);
21908 }
21909
21910 /* We require up-to-date glyph matrix for this window. */
21911 if (w->window_end_valid
21912 && !windows_or_buffers_changed
21913 && b
21914 && !b->clip_changed
21915 && !b->prevent_redisplay_optimizations_p
21916 && !window_outdated (w)
21917 && nrow >= 0
21918 && nrow < w->current_matrix->nrows
21919 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21920 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21921 {
21922 struct glyph *g, *e, *g1;
21923 int nglyphs, i;
21924 Lisp_Object levels;
21925
21926 if (!row->reversed_p) /* Left-to-right glyph row. */
21927 {
21928 g = g1 = row->glyphs[TEXT_AREA];
21929 e = g + row->used[TEXT_AREA];
21930
21931 /* Skip over glyphs at the start of the row that was
21932 generated by redisplay for its own needs. */
21933 while (g < e
21934 && NILP (g->object)
21935 && g->charpos < 0)
21936 g++;
21937 g1 = g;
21938
21939 /* Count the "interesting" glyphs in this row. */
21940 for (nglyphs = 0; g < e && !NILP (g->object); g++)
21941 nglyphs++;
21942
21943 /* Create and fill the array. */
21944 levels = make_uninit_vector (nglyphs);
21945 for (i = 0; g1 < g; i++, g1++)
21946 ASET (levels, i, make_number (g1->resolved_level));
21947 }
21948 else /* Right-to-left glyph row. */
21949 {
21950 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21951 e = row->glyphs[TEXT_AREA] - 1;
21952 while (g > e
21953 && NILP (g->object)
21954 && g->charpos < 0)
21955 g--;
21956 g1 = g;
21957 for (nglyphs = 0; g > e && !NILP (g->object); g--)
21958 nglyphs++;
21959 levels = make_uninit_vector (nglyphs);
21960 for (i = 0; g1 > g; i++, g1--)
21961 ASET (levels, i, make_number (g1->resolved_level));
21962 }
21963 return levels;
21964 }
21965 else
21966 return Qnil;
21967 }
21968
21969
21970 \f
21971 /***********************************************************************
21972 Menu Bar
21973 ***********************************************************************/
21974
21975 /* Redisplay the menu bar in the frame for window W.
21976
21977 The menu bar of X frames that don't have X toolkit support is
21978 displayed in a special window W->frame->menu_bar_window.
21979
21980 The menu bar of terminal frames is treated specially as far as
21981 glyph matrices are concerned. Menu bar lines are not part of
21982 windows, so the update is done directly on the frame matrix rows
21983 for the menu bar. */
21984
21985 static void
21986 display_menu_bar (struct window *w)
21987 {
21988 struct frame *f = XFRAME (WINDOW_FRAME (w));
21989 struct it it;
21990 Lisp_Object items;
21991 int i;
21992
21993 /* Don't do all this for graphical frames. */
21994 #ifdef HAVE_NTGUI
21995 if (FRAME_W32_P (f))
21996 return;
21997 #endif
21998 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21999 if (FRAME_X_P (f))
22000 return;
22001 #endif
22002
22003 #ifdef HAVE_NS
22004 if (FRAME_NS_P (f))
22005 return;
22006 #endif /* HAVE_NS */
22007
22008 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22009 eassert (!FRAME_WINDOW_P (f));
22010 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22011 it.first_visible_x = 0;
22012 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22013 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22014 if (FRAME_WINDOW_P (f))
22015 {
22016 /* Menu bar lines are displayed in the desired matrix of the
22017 dummy window menu_bar_window. */
22018 struct window *menu_w;
22019 menu_w = XWINDOW (f->menu_bar_window);
22020 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22021 MENU_FACE_ID);
22022 it.first_visible_x = 0;
22023 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22024 }
22025 else
22026 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22027 {
22028 /* This is a TTY frame, i.e. character hpos/vpos are used as
22029 pixel x/y. */
22030 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22031 MENU_FACE_ID);
22032 it.first_visible_x = 0;
22033 it.last_visible_x = FRAME_COLS (f);
22034 }
22035
22036 /* FIXME: This should be controlled by a user option. See the
22037 comments in redisplay_tool_bar and display_mode_line about
22038 this. */
22039 it.paragraph_embedding = L2R;
22040
22041 /* Clear all rows of the menu bar. */
22042 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22043 {
22044 struct glyph_row *row = it.glyph_row + i;
22045 clear_glyph_row (row);
22046 row->enabled_p = true;
22047 row->full_width_p = true;
22048 row->reversed_p = false;
22049 }
22050
22051 /* Display all items of the menu bar. */
22052 items = FRAME_MENU_BAR_ITEMS (it.f);
22053 for (i = 0; i < ASIZE (items); i += 4)
22054 {
22055 Lisp_Object string;
22056
22057 /* Stop at nil string. */
22058 string = AREF (items, i + 1);
22059 if (NILP (string))
22060 break;
22061
22062 /* Remember where item was displayed. */
22063 ASET (items, i + 3, make_number (it.hpos));
22064
22065 /* Display the item, pad with one space. */
22066 if (it.current_x < it.last_visible_x)
22067 display_string (NULL, string, Qnil, 0, 0, &it,
22068 SCHARS (string) + 1, 0, 0, -1);
22069 }
22070
22071 /* Fill out the line with spaces. */
22072 if (it.current_x < it.last_visible_x)
22073 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22074
22075 /* Compute the total height of the lines. */
22076 compute_line_metrics (&it);
22077 }
22078
22079 /* Deep copy of a glyph row, including the glyphs. */
22080 static void
22081 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22082 {
22083 struct glyph *pointers[1 + LAST_AREA];
22084 int to_used = to->used[TEXT_AREA];
22085
22086 /* Save glyph pointers of TO. */
22087 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22088
22089 /* Do a structure assignment. */
22090 *to = *from;
22091
22092 /* Restore original glyph pointers of TO. */
22093 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22094
22095 /* Copy the glyphs. */
22096 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22097 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22098
22099 /* If we filled only part of the TO row, fill the rest with
22100 space_glyph (which will display as empty space). */
22101 if (to_used > from->used[TEXT_AREA])
22102 fill_up_frame_row_with_spaces (to, to_used);
22103 }
22104
22105 /* Display one menu item on a TTY, by overwriting the glyphs in the
22106 frame F's desired glyph matrix with glyphs produced from the menu
22107 item text. Called from term.c to display TTY drop-down menus one
22108 item at a time.
22109
22110 ITEM_TEXT is the menu item text as a C string.
22111
22112 FACE_ID is the face ID to be used for this menu item. FACE_ID
22113 could specify one of 3 faces: a face for an enabled item, a face
22114 for a disabled item, or a face for a selected item.
22115
22116 X and Y are coordinates of the first glyph in the frame's desired
22117 matrix to be overwritten by the menu item. Since this is a TTY, Y
22118 is the zero-based number of the glyph row and X is the zero-based
22119 glyph number in the row, starting from left, where to start
22120 displaying the item.
22121
22122 SUBMENU means this menu item drops down a submenu, which
22123 should be indicated by displaying a proper visual cue after the
22124 item text. */
22125
22126 void
22127 display_tty_menu_item (const char *item_text, int width, int face_id,
22128 int x, int y, bool submenu)
22129 {
22130 struct it it;
22131 struct frame *f = SELECTED_FRAME ();
22132 struct window *w = XWINDOW (f->selected_window);
22133 struct glyph_row *row;
22134 size_t item_len = strlen (item_text);
22135
22136 eassert (FRAME_TERMCAP_P (f));
22137
22138 /* Don't write beyond the matrix's last row. This can happen for
22139 TTY screens that are not high enough to show the entire menu.
22140 (This is actually a bit of defensive programming, as
22141 tty_menu_display already limits the number of menu items to one
22142 less than the number of screen lines.) */
22143 if (y >= f->desired_matrix->nrows)
22144 return;
22145
22146 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
22147 it.first_visible_x = 0;
22148 it.last_visible_x = FRAME_COLS (f) - 1;
22149 row = it.glyph_row;
22150 /* Start with the row contents from the current matrix. */
22151 deep_copy_glyph_row (row, f->current_matrix->rows + y);
22152 bool saved_width = row->full_width_p;
22153 row->full_width_p = true;
22154 bool saved_reversed = row->reversed_p;
22155 row->reversed_p = false;
22156 row->enabled_p = true;
22157
22158 /* Arrange for the menu item glyphs to start at (X,Y) and have the
22159 desired face. */
22160 eassert (x < f->desired_matrix->matrix_w);
22161 it.current_x = it.hpos = x;
22162 it.current_y = it.vpos = y;
22163 int saved_used = row->used[TEXT_AREA];
22164 bool saved_truncated = row->truncated_on_right_p;
22165 row->used[TEXT_AREA] = x;
22166 it.face_id = face_id;
22167 it.line_wrap = TRUNCATE;
22168
22169 /* FIXME: This should be controlled by a user option. See the
22170 comments in redisplay_tool_bar and display_mode_line about this.
22171 Also, if paragraph_embedding could ever be R2L, changes will be
22172 needed to avoid shifting to the right the row characters in
22173 term.c:append_glyph. */
22174 it.paragraph_embedding = L2R;
22175
22176 /* Pad with a space on the left. */
22177 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22178 width--;
22179 /* Display the menu item, pad with spaces to WIDTH. */
22180 if (submenu)
22181 {
22182 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22183 item_len, 0, FRAME_COLS (f) - 1, -1);
22184 width -= item_len;
22185 /* Indicate with " >" that there's a submenu. */
22186 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22187 FRAME_COLS (f) - 1, -1);
22188 }
22189 else
22190 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22191 width, 0, FRAME_COLS (f) - 1, -1);
22192
22193 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22194 row->truncated_on_right_p = saved_truncated;
22195 row->hash = row_hash (row);
22196 row->full_width_p = saved_width;
22197 row->reversed_p = saved_reversed;
22198 }
22199 \f
22200 /***********************************************************************
22201 Mode Line
22202 ***********************************************************************/
22203
22204 /* Redisplay mode lines in the window tree whose root is WINDOW.
22205 If FORCE, redisplay mode lines unconditionally.
22206 Otherwise, redisplay only mode lines that are garbaged. Value is
22207 the number of windows whose mode lines were redisplayed. */
22208
22209 static int
22210 redisplay_mode_lines (Lisp_Object window, bool force)
22211 {
22212 int nwindows = 0;
22213
22214 while (!NILP (window))
22215 {
22216 struct window *w = XWINDOW (window);
22217
22218 if (WINDOWP (w->contents))
22219 nwindows += redisplay_mode_lines (w->contents, force);
22220 else if (force
22221 || FRAME_GARBAGED_P (XFRAME (w->frame))
22222 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22223 {
22224 struct text_pos lpoint;
22225 struct buffer *old = current_buffer;
22226
22227 /* Set the window's buffer for the mode line display. */
22228 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22229 set_buffer_internal_1 (XBUFFER (w->contents));
22230
22231 /* Point refers normally to the selected window. For any
22232 other window, set up appropriate value. */
22233 if (!EQ (window, selected_window))
22234 {
22235 struct text_pos pt;
22236
22237 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22238 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22239 }
22240
22241 /* Display mode lines. */
22242 clear_glyph_matrix (w->desired_matrix);
22243 if (display_mode_lines (w))
22244 ++nwindows;
22245
22246 /* Restore old settings. */
22247 set_buffer_internal_1 (old);
22248 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22249 }
22250
22251 window = w->next;
22252 }
22253
22254 return nwindows;
22255 }
22256
22257
22258 /* Display the mode and/or header line of window W. Value is the
22259 sum number of mode lines and header lines displayed. */
22260
22261 static int
22262 display_mode_lines (struct window *w)
22263 {
22264 Lisp_Object old_selected_window = selected_window;
22265 Lisp_Object old_selected_frame = selected_frame;
22266 Lisp_Object new_frame = w->frame;
22267 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22268 int n = 0;
22269
22270 selected_frame = new_frame;
22271 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22272 or window's point, then we'd need select_window_1 here as well. */
22273 XSETWINDOW (selected_window, w);
22274 XFRAME (new_frame)->selected_window = selected_window;
22275
22276 /* These will be set while the mode line specs are processed. */
22277 line_number_displayed = false;
22278 w->column_number_displayed = -1;
22279
22280 if (WINDOW_WANTS_MODELINE_P (w))
22281 {
22282 struct window *sel_w = XWINDOW (old_selected_window);
22283
22284 /* Select mode line face based on the real selected window. */
22285 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22286 BVAR (current_buffer, mode_line_format));
22287 ++n;
22288 }
22289
22290 if (WINDOW_WANTS_HEADER_LINE_P (w))
22291 {
22292 display_mode_line (w, HEADER_LINE_FACE_ID,
22293 BVAR (current_buffer, header_line_format));
22294 ++n;
22295 }
22296
22297 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22298 selected_frame = old_selected_frame;
22299 selected_window = old_selected_window;
22300 if (n > 0)
22301 w->must_be_updated_p = true;
22302 return n;
22303 }
22304
22305
22306 /* Display mode or header line of window W. FACE_ID specifies which
22307 line to display; it is either MODE_LINE_FACE_ID or
22308 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22309 display. Value is the pixel height of the mode/header line
22310 displayed. */
22311
22312 static int
22313 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22314 {
22315 struct it it;
22316 struct face *face;
22317 ptrdiff_t count = SPECPDL_INDEX ();
22318
22319 init_iterator (&it, w, -1, -1, NULL, face_id);
22320 /* Don't extend on a previously drawn mode-line.
22321 This may happen if called from pos_visible_p. */
22322 it.glyph_row->enabled_p = false;
22323 prepare_desired_row (w, it.glyph_row, true);
22324
22325 it.glyph_row->mode_line_p = true;
22326
22327 /* FIXME: This should be controlled by a user option. But
22328 supporting such an option is not trivial, since the mode line is
22329 made up of many separate strings. */
22330 it.paragraph_embedding = L2R;
22331
22332 record_unwind_protect (unwind_format_mode_line,
22333 format_mode_line_unwind_data (NULL, NULL,
22334 Qnil, false));
22335
22336 mode_line_target = MODE_LINE_DISPLAY;
22337
22338 /* Temporarily make frame's keyboard the current kboard so that
22339 kboard-local variables in the mode_line_format will get the right
22340 values. */
22341 push_kboard (FRAME_KBOARD (it.f));
22342 record_unwind_save_match_data ();
22343 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22344 pop_kboard ();
22345
22346 unbind_to (count, Qnil);
22347
22348 /* Fill up with spaces. */
22349 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22350
22351 compute_line_metrics (&it);
22352 it.glyph_row->full_width_p = true;
22353 it.glyph_row->continued_p = false;
22354 it.glyph_row->truncated_on_left_p = false;
22355 it.glyph_row->truncated_on_right_p = false;
22356
22357 /* Make a 3D mode-line have a shadow at its right end. */
22358 face = FACE_FROM_ID (it.f, face_id);
22359 extend_face_to_end_of_line (&it);
22360 if (face->box != FACE_NO_BOX)
22361 {
22362 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22363 + it.glyph_row->used[TEXT_AREA] - 1);
22364 last->right_box_line_p = true;
22365 }
22366
22367 return it.glyph_row->height;
22368 }
22369
22370 /* Move element ELT in LIST to the front of LIST.
22371 Return the updated list. */
22372
22373 static Lisp_Object
22374 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22375 {
22376 register Lisp_Object tail, prev;
22377 register Lisp_Object tem;
22378
22379 tail = list;
22380 prev = Qnil;
22381 while (CONSP (tail))
22382 {
22383 tem = XCAR (tail);
22384
22385 if (EQ (elt, tem))
22386 {
22387 /* Splice out the link TAIL. */
22388 if (NILP (prev))
22389 list = XCDR (tail);
22390 else
22391 Fsetcdr (prev, XCDR (tail));
22392
22393 /* Now make it the first. */
22394 Fsetcdr (tail, list);
22395 return tail;
22396 }
22397 else
22398 prev = tail;
22399 tail = XCDR (tail);
22400 QUIT;
22401 }
22402
22403 /* Not found--return unchanged LIST. */
22404 return list;
22405 }
22406
22407 /* Contribute ELT to the mode line for window IT->w. How it
22408 translates into text depends on its data type.
22409
22410 IT describes the display environment in which we display, as usual.
22411
22412 DEPTH is the depth in recursion. It is used to prevent
22413 infinite recursion here.
22414
22415 FIELD_WIDTH is the number of characters the display of ELT should
22416 occupy in the mode line, and PRECISION is the maximum number of
22417 characters to display from ELT's representation. See
22418 display_string for details.
22419
22420 Returns the hpos of the end of the text generated by ELT.
22421
22422 PROPS is a property list to add to any string we encounter.
22423
22424 If RISKY, remove (disregard) any properties in any string
22425 we encounter, and ignore :eval and :propertize.
22426
22427 The global variable `mode_line_target' determines whether the
22428 output is passed to `store_mode_line_noprop',
22429 `store_mode_line_string', or `display_string'. */
22430
22431 static int
22432 display_mode_element (struct it *it, int depth, int field_width, int precision,
22433 Lisp_Object elt, Lisp_Object props, bool risky)
22434 {
22435 int n = 0, field, prec;
22436 bool literal = false;
22437
22438 tail_recurse:
22439 if (depth > 100)
22440 elt = build_string ("*too-deep*");
22441
22442 depth++;
22443
22444 switch (XTYPE (elt))
22445 {
22446 case Lisp_String:
22447 {
22448 /* A string: output it and check for %-constructs within it. */
22449 unsigned char c;
22450 ptrdiff_t offset = 0;
22451
22452 if (SCHARS (elt) > 0
22453 && (!NILP (props) || risky))
22454 {
22455 Lisp_Object oprops, aelt;
22456 oprops = Ftext_properties_at (make_number (0), elt);
22457
22458 /* If the starting string's properties are not what
22459 we want, translate the string. Also, if the string
22460 is risky, do that anyway. */
22461
22462 if (NILP (Fequal (props, oprops)) || risky)
22463 {
22464 /* If the starting string has properties,
22465 merge the specified ones onto the existing ones. */
22466 if (! NILP (oprops) && !risky)
22467 {
22468 Lisp_Object tem;
22469
22470 oprops = Fcopy_sequence (oprops);
22471 tem = props;
22472 while (CONSP (tem))
22473 {
22474 oprops = Fplist_put (oprops, XCAR (tem),
22475 XCAR (XCDR (tem)));
22476 tem = XCDR (XCDR (tem));
22477 }
22478 props = oprops;
22479 }
22480
22481 aelt = Fassoc (elt, mode_line_proptrans_alist);
22482 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22483 {
22484 /* AELT is what we want. Move it to the front
22485 without consing. */
22486 elt = XCAR (aelt);
22487 mode_line_proptrans_alist
22488 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22489 }
22490 else
22491 {
22492 Lisp_Object tem;
22493
22494 /* If AELT has the wrong props, it is useless.
22495 so get rid of it. */
22496 if (! NILP (aelt))
22497 mode_line_proptrans_alist
22498 = Fdelq (aelt, mode_line_proptrans_alist);
22499
22500 elt = Fcopy_sequence (elt);
22501 Fset_text_properties (make_number (0), Flength (elt),
22502 props, elt);
22503 /* Add this item to mode_line_proptrans_alist. */
22504 mode_line_proptrans_alist
22505 = Fcons (Fcons (elt, props),
22506 mode_line_proptrans_alist);
22507 /* Truncate mode_line_proptrans_alist
22508 to at most 50 elements. */
22509 tem = Fnthcdr (make_number (50),
22510 mode_line_proptrans_alist);
22511 if (! NILP (tem))
22512 XSETCDR (tem, Qnil);
22513 }
22514 }
22515 }
22516
22517 offset = 0;
22518
22519 if (literal)
22520 {
22521 prec = precision - n;
22522 switch (mode_line_target)
22523 {
22524 case MODE_LINE_NOPROP:
22525 case MODE_LINE_TITLE:
22526 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22527 break;
22528 case MODE_LINE_STRING:
22529 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22530 break;
22531 case MODE_LINE_DISPLAY:
22532 n += display_string (NULL, elt, Qnil, 0, 0, it,
22533 0, prec, 0, STRING_MULTIBYTE (elt));
22534 break;
22535 }
22536
22537 break;
22538 }
22539
22540 /* Handle the non-literal case. */
22541
22542 while ((precision <= 0 || n < precision)
22543 && SREF (elt, offset) != 0
22544 && (mode_line_target != MODE_LINE_DISPLAY
22545 || it->current_x < it->last_visible_x))
22546 {
22547 ptrdiff_t last_offset = offset;
22548
22549 /* Advance to end of string or next format specifier. */
22550 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22551 ;
22552
22553 if (offset - 1 != last_offset)
22554 {
22555 ptrdiff_t nchars, nbytes;
22556
22557 /* Output to end of string or up to '%'. Field width
22558 is length of string. Don't output more than
22559 PRECISION allows us. */
22560 offset--;
22561
22562 prec = c_string_width (SDATA (elt) + last_offset,
22563 offset - last_offset, precision - n,
22564 &nchars, &nbytes);
22565
22566 switch (mode_line_target)
22567 {
22568 case MODE_LINE_NOPROP:
22569 case MODE_LINE_TITLE:
22570 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22571 break;
22572 case MODE_LINE_STRING:
22573 {
22574 ptrdiff_t bytepos = last_offset;
22575 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22576 ptrdiff_t endpos = (precision <= 0
22577 ? string_byte_to_char (elt, offset)
22578 : charpos + nchars);
22579 Lisp_Object mode_string
22580 = Fsubstring (elt, make_number (charpos),
22581 make_number (endpos));
22582 n += store_mode_line_string (NULL, mode_string, false,
22583 0, 0, Qnil);
22584 }
22585 break;
22586 case MODE_LINE_DISPLAY:
22587 {
22588 ptrdiff_t bytepos = last_offset;
22589 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22590
22591 if (precision <= 0)
22592 nchars = string_byte_to_char (elt, offset) - charpos;
22593 n += display_string (NULL, elt, Qnil, 0, charpos,
22594 it, 0, nchars, 0,
22595 STRING_MULTIBYTE (elt));
22596 }
22597 break;
22598 }
22599 }
22600 else /* c == '%' */
22601 {
22602 ptrdiff_t percent_position = offset;
22603
22604 /* Get the specified minimum width. Zero means
22605 don't pad. */
22606 field = 0;
22607 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22608 field = field * 10 + c - '0';
22609
22610 /* Don't pad beyond the total padding allowed. */
22611 if (field_width - n > 0 && field > field_width - n)
22612 field = field_width - n;
22613
22614 /* Note that either PRECISION <= 0 or N < PRECISION. */
22615 prec = precision - n;
22616
22617 if (c == 'M')
22618 n += display_mode_element (it, depth, field, prec,
22619 Vglobal_mode_string, props,
22620 risky);
22621 else if (c != 0)
22622 {
22623 bool multibyte;
22624 ptrdiff_t bytepos, charpos;
22625 const char *spec;
22626 Lisp_Object string;
22627
22628 bytepos = percent_position;
22629 charpos = (STRING_MULTIBYTE (elt)
22630 ? string_byte_to_char (elt, bytepos)
22631 : bytepos);
22632 spec = decode_mode_spec (it->w, c, field, &string);
22633 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22634
22635 switch (mode_line_target)
22636 {
22637 case MODE_LINE_NOPROP:
22638 case MODE_LINE_TITLE:
22639 n += store_mode_line_noprop (spec, field, prec);
22640 break;
22641 case MODE_LINE_STRING:
22642 {
22643 Lisp_Object tem = build_string (spec);
22644 props = Ftext_properties_at (make_number (charpos), elt);
22645 /* Should only keep face property in props */
22646 n += store_mode_line_string (NULL, tem, false,
22647 field, prec, props);
22648 }
22649 break;
22650 case MODE_LINE_DISPLAY:
22651 {
22652 int nglyphs_before, nwritten;
22653
22654 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22655 nwritten = display_string (spec, string, elt,
22656 charpos, 0, it,
22657 field, prec, 0,
22658 multibyte);
22659
22660 /* Assign to the glyphs written above the
22661 string where the `%x' came from, position
22662 of the `%'. */
22663 if (nwritten > 0)
22664 {
22665 struct glyph *glyph
22666 = (it->glyph_row->glyphs[TEXT_AREA]
22667 + nglyphs_before);
22668 int i;
22669
22670 for (i = 0; i < nwritten; ++i)
22671 {
22672 glyph[i].object = elt;
22673 glyph[i].charpos = charpos;
22674 }
22675
22676 n += nwritten;
22677 }
22678 }
22679 break;
22680 }
22681 }
22682 else /* c == 0 */
22683 break;
22684 }
22685 }
22686 }
22687 break;
22688
22689 case Lisp_Symbol:
22690 /* A symbol: process the value of the symbol recursively
22691 as if it appeared here directly. Avoid error if symbol void.
22692 Special case: if value of symbol is a string, output the string
22693 literally. */
22694 {
22695 register Lisp_Object tem;
22696
22697 /* If the variable is not marked as risky to set
22698 then its contents are risky to use. */
22699 if (NILP (Fget (elt, Qrisky_local_variable)))
22700 risky = true;
22701
22702 tem = Fboundp (elt);
22703 if (!NILP (tem))
22704 {
22705 tem = Fsymbol_value (elt);
22706 /* If value is a string, output that string literally:
22707 don't check for % within it. */
22708 if (STRINGP (tem))
22709 literal = true;
22710
22711 if (!EQ (tem, elt))
22712 {
22713 /* Give up right away for nil or t. */
22714 elt = tem;
22715 goto tail_recurse;
22716 }
22717 }
22718 }
22719 break;
22720
22721 case Lisp_Cons:
22722 {
22723 register Lisp_Object car, tem;
22724
22725 /* A cons cell: five distinct cases.
22726 If first element is :eval or :propertize, do something special.
22727 If first element is a string or a cons, process all the elements
22728 and effectively concatenate them.
22729 If first element is a negative number, truncate displaying cdr to
22730 at most that many characters. If positive, pad (with spaces)
22731 to at least that many characters.
22732 If first element is a symbol, process the cadr or caddr recursively
22733 according to whether the symbol's value is non-nil or nil. */
22734 car = XCAR (elt);
22735 if (EQ (car, QCeval))
22736 {
22737 /* An element of the form (:eval FORM) means evaluate FORM
22738 and use the result as mode line elements. */
22739
22740 if (risky)
22741 break;
22742
22743 if (CONSP (XCDR (elt)))
22744 {
22745 Lisp_Object spec;
22746 spec = safe__eval (true, XCAR (XCDR (elt)));
22747 n += display_mode_element (it, depth, field_width - n,
22748 precision - n, spec, props,
22749 risky);
22750 }
22751 }
22752 else if (EQ (car, QCpropertize))
22753 {
22754 /* An element of the form (:propertize ELT PROPS...)
22755 means display ELT but applying properties PROPS. */
22756
22757 if (risky)
22758 break;
22759
22760 if (CONSP (XCDR (elt)))
22761 n += display_mode_element (it, depth, field_width - n,
22762 precision - n, XCAR (XCDR (elt)),
22763 XCDR (XCDR (elt)), risky);
22764 }
22765 else if (SYMBOLP (car))
22766 {
22767 tem = Fboundp (car);
22768 elt = XCDR (elt);
22769 if (!CONSP (elt))
22770 goto invalid;
22771 /* elt is now the cdr, and we know it is a cons cell.
22772 Use its car if CAR has a non-nil value. */
22773 if (!NILP (tem))
22774 {
22775 tem = Fsymbol_value (car);
22776 if (!NILP (tem))
22777 {
22778 elt = XCAR (elt);
22779 goto tail_recurse;
22780 }
22781 }
22782 /* Symbol's value is nil (or symbol is unbound)
22783 Get the cddr of the original list
22784 and if possible find the caddr and use that. */
22785 elt = XCDR (elt);
22786 if (NILP (elt))
22787 break;
22788 else if (!CONSP (elt))
22789 goto invalid;
22790 elt = XCAR (elt);
22791 goto tail_recurse;
22792 }
22793 else if (INTEGERP (car))
22794 {
22795 register int lim = XINT (car);
22796 elt = XCDR (elt);
22797 if (lim < 0)
22798 {
22799 /* Negative int means reduce maximum width. */
22800 if (precision <= 0)
22801 precision = -lim;
22802 else
22803 precision = min (precision, -lim);
22804 }
22805 else if (lim > 0)
22806 {
22807 /* Padding specified. Don't let it be more than
22808 current maximum. */
22809 if (precision > 0)
22810 lim = min (precision, lim);
22811
22812 /* If that's more padding than already wanted, queue it.
22813 But don't reduce padding already specified even if
22814 that is beyond the current truncation point. */
22815 field_width = max (lim, field_width);
22816 }
22817 goto tail_recurse;
22818 }
22819 else if (STRINGP (car) || CONSP (car))
22820 {
22821 Lisp_Object halftail = elt;
22822 int len = 0;
22823
22824 while (CONSP (elt)
22825 && (precision <= 0 || n < precision))
22826 {
22827 n += display_mode_element (it, depth,
22828 /* Do padding only after the last
22829 element in the list. */
22830 (! CONSP (XCDR (elt))
22831 ? field_width - n
22832 : 0),
22833 precision - n, XCAR (elt),
22834 props, risky);
22835 elt = XCDR (elt);
22836 len++;
22837 if ((len & 1) == 0)
22838 halftail = XCDR (halftail);
22839 /* Check for cycle. */
22840 if (EQ (halftail, elt))
22841 break;
22842 }
22843 }
22844 }
22845 break;
22846
22847 default:
22848 invalid:
22849 elt = build_string ("*invalid*");
22850 goto tail_recurse;
22851 }
22852
22853 /* Pad to FIELD_WIDTH. */
22854 if (field_width > 0 && n < field_width)
22855 {
22856 switch (mode_line_target)
22857 {
22858 case MODE_LINE_NOPROP:
22859 case MODE_LINE_TITLE:
22860 n += store_mode_line_noprop ("", field_width - n, 0);
22861 break;
22862 case MODE_LINE_STRING:
22863 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
22864 Qnil);
22865 break;
22866 case MODE_LINE_DISPLAY:
22867 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22868 0, 0, 0);
22869 break;
22870 }
22871 }
22872
22873 return n;
22874 }
22875
22876 /* Store a mode-line string element in mode_line_string_list.
22877
22878 If STRING is non-null, display that C string. Otherwise, the Lisp
22879 string LISP_STRING is displayed.
22880
22881 FIELD_WIDTH is the minimum number of output glyphs to produce.
22882 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22883 with spaces. FIELD_WIDTH <= 0 means don't pad.
22884
22885 PRECISION is the maximum number of characters to output from
22886 STRING. PRECISION <= 0 means don't truncate the string.
22887
22888 If COPY_STRING, make a copy of LISP_STRING before adding
22889 properties to the string.
22890
22891 PROPS are the properties to add to the string.
22892 The mode_line_string_face face property is always added to the string.
22893 */
22894
22895 static int
22896 store_mode_line_string (const char *string, Lisp_Object lisp_string,
22897 bool copy_string,
22898 int field_width, int precision, Lisp_Object props)
22899 {
22900 ptrdiff_t len;
22901 int n = 0;
22902
22903 if (string != NULL)
22904 {
22905 len = strlen (string);
22906 if (precision > 0 && len > precision)
22907 len = precision;
22908 lisp_string = make_string (string, len);
22909 if (NILP (props))
22910 props = mode_line_string_face_prop;
22911 else if (!NILP (mode_line_string_face))
22912 {
22913 Lisp_Object face = Fplist_get (props, Qface);
22914 props = Fcopy_sequence (props);
22915 if (NILP (face))
22916 face = mode_line_string_face;
22917 else
22918 face = list2 (face, mode_line_string_face);
22919 props = Fplist_put (props, Qface, face);
22920 }
22921 Fadd_text_properties (make_number (0), make_number (len),
22922 props, lisp_string);
22923 }
22924 else
22925 {
22926 len = XFASTINT (Flength (lisp_string));
22927 if (precision > 0 && len > precision)
22928 {
22929 len = precision;
22930 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22931 precision = -1;
22932 }
22933 if (!NILP (mode_line_string_face))
22934 {
22935 Lisp_Object face;
22936 if (NILP (props))
22937 props = Ftext_properties_at (make_number (0), lisp_string);
22938 face = Fplist_get (props, Qface);
22939 if (NILP (face))
22940 face = mode_line_string_face;
22941 else
22942 face = list2 (face, mode_line_string_face);
22943 props = list2 (Qface, face);
22944 if (copy_string)
22945 lisp_string = Fcopy_sequence (lisp_string);
22946 }
22947 if (!NILP (props))
22948 Fadd_text_properties (make_number (0), make_number (len),
22949 props, lisp_string);
22950 }
22951
22952 if (len > 0)
22953 {
22954 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22955 n += len;
22956 }
22957
22958 if (field_width > len)
22959 {
22960 field_width -= len;
22961 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22962 if (!NILP (props))
22963 Fadd_text_properties (make_number (0), make_number (field_width),
22964 props, lisp_string);
22965 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22966 n += field_width;
22967 }
22968
22969 return n;
22970 }
22971
22972
22973 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22974 1, 4, 0,
22975 doc: /* Format a string out of a mode line format specification.
22976 First arg FORMAT specifies the mode line format (see `mode-line-format'
22977 for details) to use.
22978
22979 By default, the format is evaluated for the currently selected window.
22980
22981 Optional second arg FACE specifies the face property to put on all
22982 characters for which no face is specified. The value nil means the
22983 default face. The value t means whatever face the window's mode line
22984 currently uses (either `mode-line' or `mode-line-inactive',
22985 depending on whether the window is the selected window or not).
22986 An integer value means the value string has no text
22987 properties.
22988
22989 Optional third and fourth args WINDOW and BUFFER specify the window
22990 and buffer to use as the context for the formatting (defaults
22991 are the selected window and the WINDOW's buffer). */)
22992 (Lisp_Object format, Lisp_Object face,
22993 Lisp_Object window, Lisp_Object buffer)
22994 {
22995 struct it it;
22996 int len;
22997 struct window *w;
22998 struct buffer *old_buffer = NULL;
22999 int face_id;
23000 bool no_props = INTEGERP (face);
23001 ptrdiff_t count = SPECPDL_INDEX ();
23002 Lisp_Object str;
23003 int string_start = 0;
23004
23005 w = decode_any_window (window);
23006 XSETWINDOW (window, w);
23007
23008 if (NILP (buffer))
23009 buffer = w->contents;
23010 CHECK_BUFFER (buffer);
23011
23012 /* Make formatting the modeline a non-op when noninteractive, otherwise
23013 there will be problems later caused by a partially initialized frame. */
23014 if (NILP (format) || noninteractive)
23015 return empty_unibyte_string;
23016
23017 if (no_props)
23018 face = Qnil;
23019
23020 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23021 : EQ (face, Qt) ? (EQ (window, selected_window)
23022 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23023 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23024 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23025 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23026 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23027 : DEFAULT_FACE_ID;
23028
23029 old_buffer = current_buffer;
23030
23031 /* Save things including mode_line_proptrans_alist,
23032 and set that to nil so that we don't alter the outer value. */
23033 record_unwind_protect (unwind_format_mode_line,
23034 format_mode_line_unwind_data
23035 (XFRAME (WINDOW_FRAME (w)),
23036 old_buffer, selected_window, true));
23037 mode_line_proptrans_alist = Qnil;
23038
23039 Fselect_window (window, Qt);
23040 set_buffer_internal_1 (XBUFFER (buffer));
23041
23042 init_iterator (&it, w, -1, -1, NULL, face_id);
23043
23044 if (no_props)
23045 {
23046 mode_line_target = MODE_LINE_NOPROP;
23047 mode_line_string_face_prop = Qnil;
23048 mode_line_string_list = Qnil;
23049 string_start = MODE_LINE_NOPROP_LEN (0);
23050 }
23051 else
23052 {
23053 mode_line_target = MODE_LINE_STRING;
23054 mode_line_string_list = Qnil;
23055 mode_line_string_face = face;
23056 mode_line_string_face_prop
23057 = NILP (face) ? Qnil : list2 (Qface, face);
23058 }
23059
23060 push_kboard (FRAME_KBOARD (it.f));
23061 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23062 pop_kboard ();
23063
23064 if (no_props)
23065 {
23066 len = MODE_LINE_NOPROP_LEN (string_start);
23067 str = make_string (mode_line_noprop_buf + string_start, len);
23068 }
23069 else
23070 {
23071 mode_line_string_list = Fnreverse (mode_line_string_list);
23072 str = Fmapconcat (Qidentity, mode_line_string_list,
23073 empty_unibyte_string);
23074 }
23075
23076 unbind_to (count, Qnil);
23077 return str;
23078 }
23079
23080 /* Write a null-terminated, right justified decimal representation of
23081 the positive integer D to BUF using a minimal field width WIDTH. */
23082
23083 static void
23084 pint2str (register char *buf, register int width, register ptrdiff_t d)
23085 {
23086 register char *p = buf;
23087
23088 if (d <= 0)
23089 *p++ = '0';
23090 else
23091 {
23092 while (d > 0)
23093 {
23094 *p++ = d % 10 + '0';
23095 d /= 10;
23096 }
23097 }
23098
23099 for (width -= (int) (p - buf); width > 0; --width)
23100 *p++ = ' ';
23101 *p-- = '\0';
23102 while (p > buf)
23103 {
23104 d = *buf;
23105 *buf++ = *p;
23106 *p-- = d;
23107 }
23108 }
23109
23110 /* Write a null-terminated, right justified decimal and "human
23111 readable" representation of the nonnegative integer D to BUF using
23112 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23113
23114 static const char power_letter[] =
23115 {
23116 0, /* no letter */
23117 'k', /* kilo */
23118 'M', /* mega */
23119 'G', /* giga */
23120 'T', /* tera */
23121 'P', /* peta */
23122 'E', /* exa */
23123 'Z', /* zetta */
23124 'Y' /* yotta */
23125 };
23126
23127 static void
23128 pint2hrstr (char *buf, int width, ptrdiff_t d)
23129 {
23130 /* We aim to represent the nonnegative integer D as
23131 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
23132 ptrdiff_t quotient = d;
23133 int remainder = 0;
23134 /* -1 means: do not use TENTHS. */
23135 int tenths = -1;
23136 int exponent = 0;
23137
23138 /* Length of QUOTIENT.TENTHS as a string. */
23139 int length;
23140
23141 char * psuffix;
23142 char * p;
23143
23144 if (quotient >= 1000)
23145 {
23146 /* Scale to the appropriate EXPONENT. */
23147 do
23148 {
23149 remainder = quotient % 1000;
23150 quotient /= 1000;
23151 exponent++;
23152 }
23153 while (quotient >= 1000);
23154
23155 /* Round to nearest and decide whether to use TENTHS or not. */
23156 if (quotient <= 9)
23157 {
23158 tenths = remainder / 100;
23159 if (remainder % 100 >= 50)
23160 {
23161 if (tenths < 9)
23162 tenths++;
23163 else
23164 {
23165 quotient++;
23166 if (quotient == 10)
23167 tenths = -1;
23168 else
23169 tenths = 0;
23170 }
23171 }
23172 }
23173 else
23174 if (remainder >= 500)
23175 {
23176 if (quotient < 999)
23177 quotient++;
23178 else
23179 {
23180 quotient = 1;
23181 exponent++;
23182 tenths = 0;
23183 }
23184 }
23185 }
23186
23187 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23188 if (tenths == -1 && quotient <= 99)
23189 if (quotient <= 9)
23190 length = 1;
23191 else
23192 length = 2;
23193 else
23194 length = 3;
23195 p = psuffix = buf + max (width, length);
23196
23197 /* Print EXPONENT. */
23198 *psuffix++ = power_letter[exponent];
23199 *psuffix = '\0';
23200
23201 /* Print TENTHS. */
23202 if (tenths >= 0)
23203 {
23204 *--p = '0' + tenths;
23205 *--p = '.';
23206 }
23207
23208 /* Print QUOTIENT. */
23209 do
23210 {
23211 int digit = quotient % 10;
23212 *--p = '0' + digit;
23213 }
23214 while ((quotient /= 10) != 0);
23215
23216 /* Print leading spaces. */
23217 while (buf < p)
23218 *--p = ' ';
23219 }
23220
23221 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23222 If EOL_FLAG, set also a mnemonic character for end-of-line
23223 type of CODING_SYSTEM. Return updated pointer into BUF. */
23224
23225 static unsigned char invalid_eol_type[] = "(*invalid*)";
23226
23227 static char *
23228 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23229 {
23230 Lisp_Object val;
23231 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23232 const unsigned char *eol_str;
23233 int eol_str_len;
23234 /* The EOL conversion we are using. */
23235 Lisp_Object eoltype;
23236
23237 val = CODING_SYSTEM_SPEC (coding_system);
23238 eoltype = Qnil;
23239
23240 if (!VECTORP (val)) /* Not yet decided. */
23241 {
23242 *buf++ = multibyte ? '-' : ' ';
23243 if (eol_flag)
23244 eoltype = eol_mnemonic_undecided;
23245 /* Don't mention EOL conversion if it isn't decided. */
23246 }
23247 else
23248 {
23249 Lisp_Object attrs;
23250 Lisp_Object eolvalue;
23251
23252 attrs = AREF (val, 0);
23253 eolvalue = AREF (val, 2);
23254
23255 *buf++ = multibyte
23256 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23257 : ' ';
23258
23259 if (eol_flag)
23260 {
23261 /* The EOL conversion that is normal on this system. */
23262
23263 if (NILP (eolvalue)) /* Not yet decided. */
23264 eoltype = eol_mnemonic_undecided;
23265 else if (VECTORP (eolvalue)) /* Not yet decided. */
23266 eoltype = eol_mnemonic_undecided;
23267 else /* eolvalue is Qunix, Qdos, or Qmac. */
23268 eoltype = (EQ (eolvalue, Qunix)
23269 ? eol_mnemonic_unix
23270 : EQ (eolvalue, Qdos)
23271 ? eol_mnemonic_dos : eol_mnemonic_mac);
23272 }
23273 }
23274
23275 if (eol_flag)
23276 {
23277 /* Mention the EOL conversion if it is not the usual one. */
23278 if (STRINGP (eoltype))
23279 {
23280 eol_str = SDATA (eoltype);
23281 eol_str_len = SBYTES (eoltype);
23282 }
23283 else if (CHARACTERP (eoltype))
23284 {
23285 int c = XFASTINT (eoltype);
23286 return buf + CHAR_STRING (c, (unsigned char *) buf);
23287 }
23288 else
23289 {
23290 eol_str = invalid_eol_type;
23291 eol_str_len = sizeof (invalid_eol_type) - 1;
23292 }
23293 memcpy (buf, eol_str, eol_str_len);
23294 buf += eol_str_len;
23295 }
23296
23297 return buf;
23298 }
23299
23300 /* Return a string for the output of a mode line %-spec for window W,
23301 generated by character C. FIELD_WIDTH > 0 means pad the string
23302 returned with spaces to that value. Return a Lisp string in
23303 *STRING if the resulting string is taken from that Lisp string.
23304
23305 Note we operate on the current buffer for most purposes. */
23306
23307 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23308
23309 static const char *
23310 decode_mode_spec (struct window *w, register int c, int field_width,
23311 Lisp_Object *string)
23312 {
23313 Lisp_Object obj;
23314 struct frame *f = XFRAME (WINDOW_FRAME (w));
23315 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23316 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23317 produce strings from numerical values, so limit preposterously
23318 large values of FIELD_WIDTH to avoid overrunning the buffer's
23319 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23320 bytes plus the terminating null. */
23321 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23322 struct buffer *b = current_buffer;
23323
23324 obj = Qnil;
23325 *string = Qnil;
23326
23327 switch (c)
23328 {
23329 case '*':
23330 if (!NILP (BVAR (b, read_only)))
23331 return "%";
23332 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23333 return "*";
23334 return "-";
23335
23336 case '+':
23337 /* This differs from %* only for a modified read-only buffer. */
23338 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23339 return "*";
23340 if (!NILP (BVAR (b, read_only)))
23341 return "%";
23342 return "-";
23343
23344 case '&':
23345 /* This differs from %* in ignoring read-only-ness. */
23346 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23347 return "*";
23348 return "-";
23349
23350 case '%':
23351 return "%";
23352
23353 case '[':
23354 {
23355 int i;
23356 char *p;
23357
23358 if (command_loop_level > 5)
23359 return "[[[... ";
23360 p = decode_mode_spec_buf;
23361 for (i = 0; i < command_loop_level; i++)
23362 *p++ = '[';
23363 *p = 0;
23364 return decode_mode_spec_buf;
23365 }
23366
23367 case ']':
23368 {
23369 int i;
23370 char *p;
23371
23372 if (command_loop_level > 5)
23373 return " ...]]]";
23374 p = decode_mode_spec_buf;
23375 for (i = 0; i < command_loop_level; i++)
23376 *p++ = ']';
23377 *p = 0;
23378 return decode_mode_spec_buf;
23379 }
23380
23381 case '-':
23382 {
23383 register int i;
23384
23385 /* Let lots_of_dashes be a string of infinite length. */
23386 if (mode_line_target == MODE_LINE_NOPROP
23387 || mode_line_target == MODE_LINE_STRING)
23388 return "--";
23389 if (field_width <= 0
23390 || field_width > sizeof (lots_of_dashes))
23391 {
23392 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23393 decode_mode_spec_buf[i] = '-';
23394 decode_mode_spec_buf[i] = '\0';
23395 return decode_mode_spec_buf;
23396 }
23397 else
23398 return lots_of_dashes;
23399 }
23400
23401 case 'b':
23402 obj = BVAR (b, name);
23403 break;
23404
23405 case 'c':
23406 /* %c and %l are ignored in `frame-title-format'.
23407 (In redisplay_internal, the frame title is drawn _before_ the
23408 windows are updated, so the stuff which depends on actual
23409 window contents (such as %l) may fail to render properly, or
23410 even crash emacs.) */
23411 if (mode_line_target == MODE_LINE_TITLE)
23412 return "";
23413 else
23414 {
23415 ptrdiff_t col = current_column ();
23416 w->column_number_displayed = col;
23417 pint2str (decode_mode_spec_buf, width, col);
23418 return decode_mode_spec_buf;
23419 }
23420
23421 case 'e':
23422 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23423 {
23424 if (NILP (Vmemory_full))
23425 return "";
23426 else
23427 return "!MEM FULL! ";
23428 }
23429 #else
23430 return "";
23431 #endif
23432
23433 case 'F':
23434 /* %F displays the frame name. */
23435 if (!NILP (f->title))
23436 return SSDATA (f->title);
23437 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23438 return SSDATA (f->name);
23439 return "Emacs";
23440
23441 case 'f':
23442 obj = BVAR (b, filename);
23443 break;
23444
23445 case 'i':
23446 {
23447 ptrdiff_t size = ZV - BEGV;
23448 pint2str (decode_mode_spec_buf, width, size);
23449 return decode_mode_spec_buf;
23450 }
23451
23452 case 'I':
23453 {
23454 ptrdiff_t size = ZV - BEGV;
23455 pint2hrstr (decode_mode_spec_buf, width, size);
23456 return decode_mode_spec_buf;
23457 }
23458
23459 case 'l':
23460 {
23461 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23462 ptrdiff_t topline, nlines, height;
23463 ptrdiff_t junk;
23464
23465 /* %c and %l are ignored in `frame-title-format'. */
23466 if (mode_line_target == MODE_LINE_TITLE)
23467 return "";
23468
23469 startpos = marker_position (w->start);
23470 startpos_byte = marker_byte_position (w->start);
23471 height = WINDOW_TOTAL_LINES (w);
23472
23473 /* If we decided that this buffer isn't suitable for line numbers,
23474 don't forget that too fast. */
23475 if (w->base_line_pos == -1)
23476 goto no_value;
23477
23478 /* If the buffer is very big, don't waste time. */
23479 if (INTEGERP (Vline_number_display_limit)
23480 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23481 {
23482 w->base_line_pos = 0;
23483 w->base_line_number = 0;
23484 goto no_value;
23485 }
23486
23487 if (w->base_line_number > 0
23488 && w->base_line_pos > 0
23489 && w->base_line_pos <= startpos)
23490 {
23491 line = w->base_line_number;
23492 linepos = w->base_line_pos;
23493 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23494 }
23495 else
23496 {
23497 line = 1;
23498 linepos = BUF_BEGV (b);
23499 linepos_byte = BUF_BEGV_BYTE (b);
23500 }
23501
23502 /* Count lines from base line to window start position. */
23503 nlines = display_count_lines (linepos_byte,
23504 startpos_byte,
23505 startpos, &junk);
23506
23507 topline = nlines + line;
23508
23509 /* Determine a new base line, if the old one is too close
23510 or too far away, or if we did not have one.
23511 "Too close" means it's plausible a scroll-down would
23512 go back past it. */
23513 if (startpos == BUF_BEGV (b))
23514 {
23515 w->base_line_number = topline;
23516 w->base_line_pos = BUF_BEGV (b);
23517 }
23518 else if (nlines < height + 25 || nlines > height * 3 + 50
23519 || linepos == BUF_BEGV (b))
23520 {
23521 ptrdiff_t limit = BUF_BEGV (b);
23522 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23523 ptrdiff_t position;
23524 ptrdiff_t distance =
23525 (height * 2 + 30) * line_number_display_limit_width;
23526
23527 if (startpos - distance > limit)
23528 {
23529 limit = startpos - distance;
23530 limit_byte = CHAR_TO_BYTE (limit);
23531 }
23532
23533 nlines = display_count_lines (startpos_byte,
23534 limit_byte,
23535 - (height * 2 + 30),
23536 &position);
23537 /* If we couldn't find the lines we wanted within
23538 line_number_display_limit_width chars per line,
23539 give up on line numbers for this window. */
23540 if (position == limit_byte && limit == startpos - distance)
23541 {
23542 w->base_line_pos = -1;
23543 w->base_line_number = 0;
23544 goto no_value;
23545 }
23546
23547 w->base_line_number = topline - nlines;
23548 w->base_line_pos = BYTE_TO_CHAR (position);
23549 }
23550
23551 /* Now count lines from the start pos to point. */
23552 nlines = display_count_lines (startpos_byte,
23553 PT_BYTE, PT, &junk);
23554
23555 /* Record that we did display the line number. */
23556 line_number_displayed = true;
23557
23558 /* Make the string to show. */
23559 pint2str (decode_mode_spec_buf, width, topline + nlines);
23560 return decode_mode_spec_buf;
23561 no_value:
23562 {
23563 char *p = decode_mode_spec_buf;
23564 int pad = width - 2;
23565 while (pad-- > 0)
23566 *p++ = ' ';
23567 *p++ = '?';
23568 *p++ = '?';
23569 *p = '\0';
23570 return decode_mode_spec_buf;
23571 }
23572 }
23573 break;
23574
23575 case 'm':
23576 obj = BVAR (b, mode_name);
23577 break;
23578
23579 case 'n':
23580 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23581 return " Narrow";
23582 break;
23583
23584 case 'p':
23585 {
23586 ptrdiff_t pos = marker_position (w->start);
23587 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23588
23589 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23590 {
23591 if (pos <= BUF_BEGV (b))
23592 return "All";
23593 else
23594 return "Bottom";
23595 }
23596 else if (pos <= BUF_BEGV (b))
23597 return "Top";
23598 else
23599 {
23600 if (total > 1000000)
23601 /* Do it differently for a large value, to avoid overflow. */
23602 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23603 else
23604 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23605 /* We can't normally display a 3-digit number,
23606 so get us a 2-digit number that is close. */
23607 if (total == 100)
23608 total = 99;
23609 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23610 return decode_mode_spec_buf;
23611 }
23612 }
23613
23614 /* Display percentage of size above the bottom of the screen. */
23615 case 'P':
23616 {
23617 ptrdiff_t toppos = marker_position (w->start);
23618 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23619 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23620
23621 if (botpos >= BUF_ZV (b))
23622 {
23623 if (toppos <= BUF_BEGV (b))
23624 return "All";
23625 else
23626 return "Bottom";
23627 }
23628 else
23629 {
23630 if (total > 1000000)
23631 /* Do it differently for a large value, to avoid overflow. */
23632 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23633 else
23634 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23635 /* We can't normally display a 3-digit number,
23636 so get us a 2-digit number that is close. */
23637 if (total == 100)
23638 total = 99;
23639 if (toppos <= BUF_BEGV (b))
23640 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23641 else
23642 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23643 return decode_mode_spec_buf;
23644 }
23645 }
23646
23647 case 's':
23648 /* status of process */
23649 obj = Fget_buffer_process (Fcurrent_buffer ());
23650 if (NILP (obj))
23651 return "no process";
23652 #ifndef MSDOS
23653 obj = Fsymbol_name (Fprocess_status (obj));
23654 #endif
23655 break;
23656
23657 case '@':
23658 {
23659 ptrdiff_t count = inhibit_garbage_collection ();
23660 Lisp_Object curdir = BVAR (current_buffer, directory);
23661 Lisp_Object val = Qnil;
23662
23663 if (STRINGP (curdir))
23664 val = call1 (intern ("file-remote-p"), curdir);
23665
23666 unbind_to (count, Qnil);
23667
23668 if (NILP (val))
23669 return "-";
23670 else
23671 return "@";
23672 }
23673
23674 case 'z':
23675 /* coding-system (not including end-of-line format) */
23676 case 'Z':
23677 /* coding-system (including end-of-line type) */
23678 {
23679 bool eol_flag = (c == 'Z');
23680 char *p = decode_mode_spec_buf;
23681
23682 if (! FRAME_WINDOW_P (f))
23683 {
23684 /* No need to mention EOL here--the terminal never needs
23685 to do EOL conversion. */
23686 p = decode_mode_spec_coding (CODING_ID_NAME
23687 (FRAME_KEYBOARD_CODING (f)->id),
23688 p, false);
23689 p = decode_mode_spec_coding (CODING_ID_NAME
23690 (FRAME_TERMINAL_CODING (f)->id),
23691 p, false);
23692 }
23693 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23694 p, eol_flag);
23695
23696 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23697 #ifdef subprocesses
23698 obj = Fget_buffer_process (Fcurrent_buffer ());
23699 if (PROCESSP (obj))
23700 {
23701 p = decode_mode_spec_coding
23702 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23703 p = decode_mode_spec_coding
23704 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23705 }
23706 #endif /* subprocesses */
23707 #endif /* false */
23708 *p = 0;
23709 return decode_mode_spec_buf;
23710 }
23711 }
23712
23713 if (STRINGP (obj))
23714 {
23715 *string = obj;
23716 return SSDATA (obj);
23717 }
23718 else
23719 return "";
23720 }
23721
23722
23723 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23724 means count lines back from START_BYTE. But don't go beyond
23725 LIMIT_BYTE. Return the number of lines thus found (always
23726 nonnegative).
23727
23728 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23729 either the position COUNT lines after/before START_BYTE, if we
23730 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23731 COUNT lines. */
23732
23733 static ptrdiff_t
23734 display_count_lines (ptrdiff_t start_byte,
23735 ptrdiff_t limit_byte, ptrdiff_t count,
23736 ptrdiff_t *byte_pos_ptr)
23737 {
23738 register unsigned char *cursor;
23739 unsigned char *base;
23740
23741 register ptrdiff_t ceiling;
23742 register unsigned char *ceiling_addr;
23743 ptrdiff_t orig_count = count;
23744
23745 /* If we are not in selective display mode,
23746 check only for newlines. */
23747 bool selective_display
23748 = (!NILP (BVAR (current_buffer, selective_display))
23749 && !INTEGERP (BVAR (current_buffer, selective_display)));
23750
23751 if (count > 0)
23752 {
23753 while (start_byte < limit_byte)
23754 {
23755 ceiling = BUFFER_CEILING_OF (start_byte);
23756 ceiling = min (limit_byte - 1, ceiling);
23757 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23758 base = (cursor = BYTE_POS_ADDR (start_byte));
23759
23760 do
23761 {
23762 if (selective_display)
23763 {
23764 while (*cursor != '\n' && *cursor != 015
23765 && ++cursor != ceiling_addr)
23766 continue;
23767 if (cursor == ceiling_addr)
23768 break;
23769 }
23770 else
23771 {
23772 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23773 if (! cursor)
23774 break;
23775 }
23776
23777 cursor++;
23778
23779 if (--count == 0)
23780 {
23781 start_byte += cursor - base;
23782 *byte_pos_ptr = start_byte;
23783 return orig_count;
23784 }
23785 }
23786 while (cursor < ceiling_addr);
23787
23788 start_byte += ceiling_addr - base;
23789 }
23790 }
23791 else
23792 {
23793 while (start_byte > limit_byte)
23794 {
23795 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23796 ceiling = max (limit_byte, ceiling);
23797 ceiling_addr = BYTE_POS_ADDR (ceiling);
23798 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23799 while (true)
23800 {
23801 if (selective_display)
23802 {
23803 while (--cursor >= ceiling_addr
23804 && *cursor != '\n' && *cursor != 015)
23805 continue;
23806 if (cursor < ceiling_addr)
23807 break;
23808 }
23809 else
23810 {
23811 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23812 if (! cursor)
23813 break;
23814 }
23815
23816 if (++count == 0)
23817 {
23818 start_byte += cursor - base + 1;
23819 *byte_pos_ptr = start_byte;
23820 /* When scanning backwards, we should
23821 not count the newline posterior to which we stop. */
23822 return - orig_count - 1;
23823 }
23824 }
23825 start_byte += ceiling_addr - base;
23826 }
23827 }
23828
23829 *byte_pos_ptr = limit_byte;
23830
23831 if (count < 0)
23832 return - orig_count + count;
23833 return orig_count - count;
23834
23835 }
23836
23837
23838 \f
23839 /***********************************************************************
23840 Displaying strings
23841 ***********************************************************************/
23842
23843 /* Display a NUL-terminated string, starting with index START.
23844
23845 If STRING is non-null, display that C string. Otherwise, the Lisp
23846 string LISP_STRING is displayed. There's a case that STRING is
23847 non-null and LISP_STRING is not nil. It means STRING is a string
23848 data of LISP_STRING. In that case, we display LISP_STRING while
23849 ignoring its text properties.
23850
23851 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23852 FACE_STRING. Display STRING or LISP_STRING with the face at
23853 FACE_STRING_POS in FACE_STRING:
23854
23855 Display the string in the environment given by IT, but use the
23856 standard display table, temporarily.
23857
23858 FIELD_WIDTH is the minimum number of output glyphs to produce.
23859 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23860 with spaces. If STRING has more characters, more than FIELD_WIDTH
23861 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23862
23863 PRECISION is the maximum number of characters to output from
23864 STRING. PRECISION < 0 means don't truncate the string.
23865
23866 This is roughly equivalent to printf format specifiers:
23867
23868 FIELD_WIDTH PRECISION PRINTF
23869 ----------------------------------------
23870 -1 -1 %s
23871 -1 10 %.10s
23872 10 -1 %10s
23873 20 10 %20.10s
23874
23875 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23876 display them, and < 0 means obey the current buffer's value of
23877 enable_multibyte_characters.
23878
23879 Value is the number of columns displayed. */
23880
23881 static int
23882 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23883 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23884 int field_width, int precision, int max_x, int multibyte)
23885 {
23886 int hpos_at_start = it->hpos;
23887 int saved_face_id = it->face_id;
23888 struct glyph_row *row = it->glyph_row;
23889 ptrdiff_t it_charpos;
23890
23891 /* Initialize the iterator IT for iteration over STRING beginning
23892 with index START. */
23893 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23894 precision, field_width, multibyte);
23895 if (string && STRINGP (lisp_string))
23896 /* LISP_STRING is the one returned by decode_mode_spec. We should
23897 ignore its text properties. */
23898 it->stop_charpos = it->end_charpos;
23899
23900 /* If displaying STRING, set up the face of the iterator from
23901 FACE_STRING, if that's given. */
23902 if (STRINGP (face_string))
23903 {
23904 ptrdiff_t endptr;
23905 struct face *face;
23906
23907 it->face_id
23908 = face_at_string_position (it->w, face_string, face_string_pos,
23909 0, &endptr, it->base_face_id, false);
23910 face = FACE_FROM_ID (it->f, it->face_id);
23911 it->face_box_p = face->box != FACE_NO_BOX;
23912 }
23913
23914 /* Set max_x to the maximum allowed X position. Don't let it go
23915 beyond the right edge of the window. */
23916 if (max_x <= 0)
23917 max_x = it->last_visible_x;
23918 else
23919 max_x = min (max_x, it->last_visible_x);
23920
23921 /* Skip over display elements that are not visible. because IT->w is
23922 hscrolled. */
23923 if (it->current_x < it->first_visible_x)
23924 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23925 MOVE_TO_POS | MOVE_TO_X);
23926
23927 row->ascent = it->max_ascent;
23928 row->height = it->max_ascent + it->max_descent;
23929 row->phys_ascent = it->max_phys_ascent;
23930 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23931 row->extra_line_spacing = it->max_extra_line_spacing;
23932
23933 if (STRINGP (it->string))
23934 it_charpos = IT_STRING_CHARPOS (*it);
23935 else
23936 it_charpos = IT_CHARPOS (*it);
23937
23938 /* This condition is for the case that we are called with current_x
23939 past last_visible_x. */
23940 while (it->current_x < max_x)
23941 {
23942 int x_before, x, n_glyphs_before, i, nglyphs;
23943
23944 /* Get the next display element. */
23945 if (!get_next_display_element (it))
23946 break;
23947
23948 /* Produce glyphs. */
23949 x_before = it->current_x;
23950 n_glyphs_before = row->used[TEXT_AREA];
23951 PRODUCE_GLYPHS (it);
23952
23953 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23954 i = 0;
23955 x = x_before;
23956 while (i < nglyphs)
23957 {
23958 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23959
23960 if (it->line_wrap != TRUNCATE
23961 && x + glyph->pixel_width > max_x)
23962 {
23963 /* End of continued line or max_x reached. */
23964 if (CHAR_GLYPH_PADDING_P (*glyph))
23965 {
23966 /* A wide character is unbreakable. */
23967 if (row->reversed_p)
23968 unproduce_glyphs (it, row->used[TEXT_AREA]
23969 - n_glyphs_before);
23970 row->used[TEXT_AREA] = n_glyphs_before;
23971 it->current_x = x_before;
23972 }
23973 else
23974 {
23975 if (row->reversed_p)
23976 unproduce_glyphs (it, row->used[TEXT_AREA]
23977 - (n_glyphs_before + i));
23978 row->used[TEXT_AREA] = n_glyphs_before + i;
23979 it->current_x = x;
23980 }
23981 break;
23982 }
23983 else if (x + glyph->pixel_width >= it->first_visible_x)
23984 {
23985 /* Glyph is at least partially visible. */
23986 ++it->hpos;
23987 if (x < it->first_visible_x)
23988 row->x = x - it->first_visible_x;
23989 }
23990 else
23991 {
23992 /* Glyph is off the left margin of the display area.
23993 Should not happen. */
23994 emacs_abort ();
23995 }
23996
23997 row->ascent = max (row->ascent, it->max_ascent);
23998 row->height = max (row->height, it->max_ascent + it->max_descent);
23999 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24000 row->phys_height = max (row->phys_height,
24001 it->max_phys_ascent + it->max_phys_descent);
24002 row->extra_line_spacing = max (row->extra_line_spacing,
24003 it->max_extra_line_spacing);
24004 x += glyph->pixel_width;
24005 ++i;
24006 }
24007
24008 /* Stop if max_x reached. */
24009 if (i < nglyphs)
24010 break;
24011
24012 /* Stop at line ends. */
24013 if (ITERATOR_AT_END_OF_LINE_P (it))
24014 {
24015 it->continuation_lines_width = 0;
24016 break;
24017 }
24018
24019 set_iterator_to_next (it, true);
24020 if (STRINGP (it->string))
24021 it_charpos = IT_STRING_CHARPOS (*it);
24022 else
24023 it_charpos = IT_CHARPOS (*it);
24024
24025 /* Stop if truncating at the right edge. */
24026 if (it->line_wrap == TRUNCATE
24027 && it->current_x >= it->last_visible_x)
24028 {
24029 /* Add truncation mark, but don't do it if the line is
24030 truncated at a padding space. */
24031 if (it_charpos < it->string_nchars)
24032 {
24033 if (!FRAME_WINDOW_P (it->f))
24034 {
24035 int ii, n;
24036
24037 if (it->current_x > it->last_visible_x)
24038 {
24039 if (!row->reversed_p)
24040 {
24041 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24042 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24043 break;
24044 }
24045 else
24046 {
24047 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24048 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24049 break;
24050 unproduce_glyphs (it, ii + 1);
24051 ii = row->used[TEXT_AREA] - (ii + 1);
24052 }
24053 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24054 {
24055 row->used[TEXT_AREA] = ii;
24056 produce_special_glyphs (it, IT_TRUNCATION);
24057 }
24058 }
24059 produce_special_glyphs (it, IT_TRUNCATION);
24060 }
24061 row->truncated_on_right_p = true;
24062 }
24063 break;
24064 }
24065 }
24066
24067 /* Maybe insert a truncation at the left. */
24068 if (it->first_visible_x
24069 && it_charpos > 0)
24070 {
24071 if (!FRAME_WINDOW_P (it->f)
24072 || (row->reversed_p
24073 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24074 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24075 insert_left_trunc_glyphs (it);
24076 row->truncated_on_left_p = true;
24077 }
24078
24079 it->face_id = saved_face_id;
24080
24081 /* Value is number of columns displayed. */
24082 return it->hpos - hpos_at_start;
24083 }
24084
24085
24086 \f
24087 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24088 appears as an element of LIST or as the car of an element of LIST.
24089 If PROPVAL is a list, compare each element against LIST in that
24090 way, and return 1/2 if any element of PROPVAL is found in LIST.
24091 Otherwise return 0. This function cannot quit.
24092 The return value is 2 if the text is invisible but with an ellipsis
24093 and 1 if it's invisible and without an ellipsis. */
24094
24095 int
24096 invisible_prop (Lisp_Object propval, Lisp_Object list)
24097 {
24098 Lisp_Object tail, proptail;
24099
24100 for (tail = list; CONSP (tail); tail = XCDR (tail))
24101 {
24102 register Lisp_Object tem;
24103 tem = XCAR (tail);
24104 if (EQ (propval, tem))
24105 return 1;
24106 if (CONSP (tem) && EQ (propval, XCAR (tem)))
24107 return NILP (XCDR (tem)) ? 1 : 2;
24108 }
24109
24110 if (CONSP (propval))
24111 {
24112 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
24113 {
24114 Lisp_Object propelt;
24115 propelt = XCAR (proptail);
24116 for (tail = list; CONSP (tail); tail = XCDR (tail))
24117 {
24118 register Lisp_Object tem;
24119 tem = XCAR (tail);
24120 if (EQ (propelt, tem))
24121 return 1;
24122 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
24123 return NILP (XCDR (tem)) ? 1 : 2;
24124 }
24125 }
24126 }
24127
24128 return 0;
24129 }
24130
24131 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
24132 doc: /* Non-nil if the property makes the text invisible.
24133 POS-OR-PROP can be a marker or number, in which case it is taken to be
24134 a position in the current buffer and the value of the `invisible' property
24135 is checked; or it can be some other value, which is then presumed to be the
24136 value of the `invisible' property of the text of interest.
24137 The non-nil value returned can be t for truly invisible text or something
24138 else if the text is replaced by an ellipsis. */)
24139 (Lisp_Object pos_or_prop)
24140 {
24141 Lisp_Object prop
24142 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
24143 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
24144 : pos_or_prop);
24145 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
24146 return (invis == 0 ? Qnil
24147 : invis == 1 ? Qt
24148 : make_number (invis));
24149 }
24150
24151 /* Calculate a width or height in pixels from a specification using
24152 the following elements:
24153
24154 SPEC ::=
24155 NUM - a (fractional) multiple of the default font width/height
24156 (NUM) - specifies exactly NUM pixels
24157 UNIT - a fixed number of pixels, see below.
24158 ELEMENT - size of a display element in pixels, see below.
24159 (NUM . SPEC) - equals NUM * SPEC
24160 (+ SPEC SPEC ...) - add pixel values
24161 (- SPEC SPEC ...) - subtract pixel values
24162 (- SPEC) - negate pixel value
24163
24164 NUM ::=
24165 INT or FLOAT - a number constant
24166 SYMBOL - use symbol's (buffer local) variable binding.
24167
24168 UNIT ::=
24169 in - pixels per inch *)
24170 mm - pixels per 1/1000 meter *)
24171 cm - pixels per 1/100 meter *)
24172 width - width of current font in pixels.
24173 height - height of current font in pixels.
24174
24175 *) using the ratio(s) defined in display-pixels-per-inch.
24176
24177 ELEMENT ::=
24178
24179 left-fringe - left fringe width in pixels
24180 right-fringe - right fringe width in pixels
24181
24182 left-margin - left margin width in pixels
24183 right-margin - right margin width in pixels
24184
24185 scroll-bar - scroll-bar area width in pixels
24186
24187 Examples:
24188
24189 Pixels corresponding to 5 inches:
24190 (5 . in)
24191
24192 Total width of non-text areas on left side of window (if scroll-bar is on left):
24193 '(space :width (+ left-fringe left-margin scroll-bar))
24194
24195 Align to first text column (in header line):
24196 '(space :align-to 0)
24197
24198 Align to middle of text area minus half the width of variable `my-image'
24199 containing a loaded image:
24200 '(space :align-to (0.5 . (- text my-image)))
24201
24202 Width of left margin minus width of 1 character in the default font:
24203 '(space :width (- left-margin 1))
24204
24205 Width of left margin minus width of 2 characters in the current font:
24206 '(space :width (- left-margin (2 . width)))
24207
24208 Center 1 character over left-margin (in header line):
24209 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24210
24211 Different ways to express width of left fringe plus left margin minus one pixel:
24212 '(space :width (- (+ left-fringe left-margin) (1)))
24213 '(space :width (+ left-fringe left-margin (- (1))))
24214 '(space :width (+ left-fringe left-margin (-1)))
24215
24216 */
24217
24218 static bool
24219 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24220 struct font *font, bool width_p, int *align_to)
24221 {
24222 double pixels;
24223
24224 # define OK_PIXELS(val) (*res = (val), true)
24225 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24226
24227 if (NILP (prop))
24228 return OK_PIXELS (0);
24229
24230 eassert (FRAME_LIVE_P (it->f));
24231
24232 if (SYMBOLP (prop))
24233 {
24234 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24235 {
24236 char *unit = SSDATA (SYMBOL_NAME (prop));
24237
24238 if (unit[0] == 'i' && unit[1] == 'n')
24239 pixels = 1.0;
24240 else if (unit[0] == 'm' && unit[1] == 'm')
24241 pixels = 25.4;
24242 else if (unit[0] == 'c' && unit[1] == 'm')
24243 pixels = 2.54;
24244 else
24245 pixels = 0;
24246 if (pixels > 0)
24247 {
24248 double ppi = (width_p ? FRAME_RES_X (it->f)
24249 : FRAME_RES_Y (it->f));
24250
24251 if (ppi > 0)
24252 return OK_PIXELS (ppi / pixels);
24253 return false;
24254 }
24255 }
24256
24257 #ifdef HAVE_WINDOW_SYSTEM
24258 if (EQ (prop, Qheight))
24259 return OK_PIXELS (font
24260 ? normal_char_height (font, -1)
24261 : FRAME_LINE_HEIGHT (it->f));
24262 if (EQ (prop, Qwidth))
24263 return OK_PIXELS (font
24264 ? FONT_WIDTH (font)
24265 : FRAME_COLUMN_WIDTH (it->f));
24266 #else
24267 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24268 return OK_PIXELS (1);
24269 #endif
24270
24271 if (EQ (prop, Qtext))
24272 return OK_PIXELS (width_p
24273 ? window_box_width (it->w, TEXT_AREA)
24274 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24275
24276 if (align_to && *align_to < 0)
24277 {
24278 *res = 0;
24279 if (EQ (prop, Qleft))
24280 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24281 if (EQ (prop, Qright))
24282 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24283 if (EQ (prop, Qcenter))
24284 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24285 + window_box_width (it->w, TEXT_AREA) / 2);
24286 if (EQ (prop, Qleft_fringe))
24287 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24288 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24289 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24290 if (EQ (prop, Qright_fringe))
24291 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24292 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24293 : window_box_right_offset (it->w, TEXT_AREA));
24294 if (EQ (prop, Qleft_margin))
24295 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24296 if (EQ (prop, Qright_margin))
24297 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24298 if (EQ (prop, Qscroll_bar))
24299 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24300 ? 0
24301 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24302 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24303 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24304 : 0)));
24305 }
24306 else
24307 {
24308 if (EQ (prop, Qleft_fringe))
24309 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24310 if (EQ (prop, Qright_fringe))
24311 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24312 if (EQ (prop, Qleft_margin))
24313 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24314 if (EQ (prop, Qright_margin))
24315 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24316 if (EQ (prop, Qscroll_bar))
24317 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24318 }
24319
24320 prop = buffer_local_value (prop, it->w->contents);
24321 if (EQ (prop, Qunbound))
24322 prop = Qnil;
24323 }
24324
24325 if (NUMBERP (prop))
24326 {
24327 int base_unit = (width_p
24328 ? FRAME_COLUMN_WIDTH (it->f)
24329 : FRAME_LINE_HEIGHT (it->f));
24330 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24331 }
24332
24333 if (CONSP (prop))
24334 {
24335 Lisp_Object car = XCAR (prop);
24336 Lisp_Object cdr = XCDR (prop);
24337
24338 if (SYMBOLP (car))
24339 {
24340 #ifdef HAVE_WINDOW_SYSTEM
24341 if (FRAME_WINDOW_P (it->f)
24342 && valid_image_p (prop))
24343 {
24344 ptrdiff_t id = lookup_image (it->f, prop);
24345 struct image *img = IMAGE_FROM_ID (it->f, id);
24346
24347 return OK_PIXELS (width_p ? img->width : img->height);
24348 }
24349 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
24350 {
24351 // TODO: Don't return dummy size.
24352 return OK_PIXELS (100);
24353 }
24354 #endif
24355 if (EQ (car, Qplus) || EQ (car, Qminus))
24356 {
24357 bool first = true;
24358 double px;
24359
24360 pixels = 0;
24361 while (CONSP (cdr))
24362 {
24363 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24364 font, width_p, align_to))
24365 return false;
24366 if (first)
24367 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24368 else
24369 pixels += px;
24370 cdr = XCDR (cdr);
24371 }
24372 if (EQ (car, Qminus))
24373 pixels = -pixels;
24374 return OK_PIXELS (pixels);
24375 }
24376
24377 car = buffer_local_value (car, it->w->contents);
24378 if (EQ (car, Qunbound))
24379 car = Qnil;
24380 }
24381
24382 if (NUMBERP (car))
24383 {
24384 double fact;
24385 pixels = XFLOATINT (car);
24386 if (NILP (cdr))
24387 return OK_PIXELS (pixels);
24388 if (calc_pixel_width_or_height (&fact, it, cdr,
24389 font, width_p, align_to))
24390 return OK_PIXELS (pixels * fact);
24391 return false;
24392 }
24393
24394 return false;
24395 }
24396
24397 return false;
24398 }
24399
24400 void
24401 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24402 {
24403 #ifdef HAVE_WINDOW_SYSTEM
24404 normal_char_ascent_descent (font, -1, ascent, descent);
24405 #else
24406 *ascent = 1;
24407 *descent = 0;
24408 #endif
24409 }
24410
24411 \f
24412 /***********************************************************************
24413 Glyph Display
24414 ***********************************************************************/
24415
24416 #ifdef HAVE_WINDOW_SYSTEM
24417
24418 #ifdef GLYPH_DEBUG
24419
24420 void
24421 dump_glyph_string (struct glyph_string *s)
24422 {
24423 fprintf (stderr, "glyph string\n");
24424 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24425 s->x, s->y, s->width, s->height);
24426 fprintf (stderr, " ybase = %d\n", s->ybase);
24427 fprintf (stderr, " hl = %d\n", s->hl);
24428 fprintf (stderr, " left overhang = %d, right = %d\n",
24429 s->left_overhang, s->right_overhang);
24430 fprintf (stderr, " nchars = %d\n", s->nchars);
24431 fprintf (stderr, " extends to end of line = %d\n",
24432 s->extends_to_end_of_line_p);
24433 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24434 fprintf (stderr, " bg width = %d\n", s->background_width);
24435 }
24436
24437 #endif /* GLYPH_DEBUG */
24438
24439 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24440 of XChar2b structures for S; it can't be allocated in
24441 init_glyph_string because it must be allocated via `alloca'. W
24442 is the window on which S is drawn. ROW and AREA are the glyph row
24443 and area within the row from which S is constructed. START is the
24444 index of the first glyph structure covered by S. HL is a
24445 face-override for drawing S. */
24446
24447 #ifdef HAVE_NTGUI
24448 #define OPTIONAL_HDC(hdc) HDC hdc,
24449 #define DECLARE_HDC(hdc) HDC hdc;
24450 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24451 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24452 #endif
24453
24454 #ifndef OPTIONAL_HDC
24455 #define OPTIONAL_HDC(hdc)
24456 #define DECLARE_HDC(hdc)
24457 #define ALLOCATE_HDC(hdc, f)
24458 #define RELEASE_HDC(hdc, f)
24459 #endif
24460
24461 static void
24462 init_glyph_string (struct glyph_string *s,
24463 OPTIONAL_HDC (hdc)
24464 XChar2b *char2b, struct window *w, struct glyph_row *row,
24465 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24466 {
24467 memset (s, 0, sizeof *s);
24468 s->w = w;
24469 s->f = XFRAME (w->frame);
24470 #ifdef HAVE_NTGUI
24471 s->hdc = hdc;
24472 #endif
24473 s->display = FRAME_X_DISPLAY (s->f);
24474 s->window = FRAME_X_WINDOW (s->f);
24475 s->char2b = char2b;
24476 s->hl = hl;
24477 s->row = row;
24478 s->area = area;
24479 s->first_glyph = row->glyphs[area] + start;
24480 s->height = row->height;
24481 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24482 s->ybase = s->y + row->ascent;
24483 }
24484
24485
24486 /* Append the list of glyph strings with head H and tail T to the list
24487 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24488
24489 static void
24490 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24491 struct glyph_string *h, struct glyph_string *t)
24492 {
24493 if (h)
24494 {
24495 if (*head)
24496 (*tail)->next = h;
24497 else
24498 *head = h;
24499 h->prev = *tail;
24500 *tail = t;
24501 }
24502 }
24503
24504
24505 /* Prepend the list of glyph strings with head H and tail T to the
24506 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24507 result. */
24508
24509 static void
24510 prepend_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 (*head)->prev = t;
24517 else
24518 *tail = t;
24519 t->next = *head;
24520 *head = h;
24521 }
24522 }
24523
24524
24525 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24526 Set *HEAD and *TAIL to the resulting list. */
24527
24528 static void
24529 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24530 struct glyph_string *s)
24531 {
24532 s->next = s->prev = NULL;
24533 append_glyph_string_lists (head, tail, s, s);
24534 }
24535
24536
24537 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24538 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24539 make sure that X resources for the face returned are allocated.
24540 Value is a pointer to a realized face that is ready for display if
24541 DISPLAY_P. */
24542
24543 static struct face *
24544 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24545 XChar2b *char2b, bool display_p)
24546 {
24547 struct face *face = FACE_FROM_ID (f, face_id);
24548 unsigned code = 0;
24549
24550 if (face->font)
24551 {
24552 code = face->font->driver->encode_char (face->font, c);
24553
24554 if (code == FONT_INVALID_CODE)
24555 code = 0;
24556 }
24557 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24558
24559 /* Make sure X resources of the face are allocated. */
24560 #ifdef HAVE_X_WINDOWS
24561 if (display_p)
24562 #endif
24563 {
24564 eassert (face != NULL);
24565 prepare_face_for_display (f, face);
24566 }
24567
24568 return face;
24569 }
24570
24571
24572 /* Get face and two-byte form of character glyph GLYPH on frame F.
24573 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24574 a pointer to a realized face that is ready for display. */
24575
24576 static struct face *
24577 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24578 XChar2b *char2b)
24579 {
24580 struct face *face;
24581 unsigned code = 0;
24582
24583 eassert (glyph->type == CHAR_GLYPH);
24584 face = FACE_FROM_ID (f, glyph->face_id);
24585
24586 /* Make sure X resources of the face are allocated. */
24587 eassert (face != NULL);
24588 prepare_face_for_display (f, face);
24589
24590 if (face->font)
24591 {
24592 if (CHAR_BYTE8_P (glyph->u.ch))
24593 code = CHAR_TO_BYTE8 (glyph->u.ch);
24594 else
24595 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24596
24597 if (code == FONT_INVALID_CODE)
24598 code = 0;
24599 }
24600
24601 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24602 return face;
24603 }
24604
24605
24606 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24607 Return true iff FONT has a glyph for C. */
24608
24609 static bool
24610 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24611 {
24612 unsigned code;
24613
24614 if (CHAR_BYTE8_P (c))
24615 code = CHAR_TO_BYTE8 (c);
24616 else
24617 code = font->driver->encode_char (font, c);
24618
24619 if (code == FONT_INVALID_CODE)
24620 return false;
24621 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24622 return true;
24623 }
24624
24625
24626 /* Fill glyph string S with composition components specified by S->cmp.
24627
24628 BASE_FACE is the base face of the composition.
24629 S->cmp_from is the index of the first component for S.
24630
24631 OVERLAPS non-zero means S should draw the foreground only, and use
24632 its physical height for clipping. See also draw_glyphs.
24633
24634 Value is the index of a component not in S. */
24635
24636 static int
24637 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24638 int overlaps)
24639 {
24640 int i;
24641 /* For all glyphs of this composition, starting at the offset
24642 S->cmp_from, until we reach the end of the definition or encounter a
24643 glyph that requires the different face, add it to S. */
24644 struct face *face;
24645
24646 eassert (s);
24647
24648 s->for_overlaps = overlaps;
24649 s->face = NULL;
24650 s->font = NULL;
24651 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24652 {
24653 int c = COMPOSITION_GLYPH (s->cmp, i);
24654
24655 /* TAB in a composition means display glyphs with padding space
24656 on the left or right. */
24657 if (c != '\t')
24658 {
24659 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24660 -1, Qnil);
24661
24662 face = get_char_face_and_encoding (s->f, c, face_id,
24663 s->char2b + i, true);
24664 if (face)
24665 {
24666 if (! s->face)
24667 {
24668 s->face = face;
24669 s->font = s->face->font;
24670 }
24671 else if (s->face != face)
24672 break;
24673 }
24674 }
24675 ++s->nchars;
24676 }
24677 s->cmp_to = i;
24678
24679 if (s->face == NULL)
24680 {
24681 s->face = base_face->ascii_face;
24682 s->font = s->face->font;
24683 }
24684
24685 /* All glyph strings for the same composition has the same width,
24686 i.e. the width set for the first component of the composition. */
24687 s->width = s->first_glyph->pixel_width;
24688
24689 /* If the specified font could not be loaded, use the frame's
24690 default font, but record the fact that we couldn't load it in
24691 the glyph string so that we can draw rectangles for the
24692 characters of the glyph string. */
24693 if (s->font == NULL)
24694 {
24695 s->font_not_found_p = true;
24696 s->font = FRAME_FONT (s->f);
24697 }
24698
24699 /* Adjust base line for subscript/superscript text. */
24700 s->ybase += s->first_glyph->voffset;
24701
24702 return s->cmp_to;
24703 }
24704
24705 static int
24706 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24707 int start, int end, int overlaps)
24708 {
24709 struct glyph *glyph, *last;
24710 Lisp_Object lgstring;
24711 int i;
24712
24713 s->for_overlaps = overlaps;
24714 glyph = s->row->glyphs[s->area] + start;
24715 last = s->row->glyphs[s->area] + end;
24716 s->cmp_id = glyph->u.cmp.id;
24717 s->cmp_from = glyph->slice.cmp.from;
24718 s->cmp_to = glyph->slice.cmp.to + 1;
24719 s->face = FACE_FROM_ID (s->f, face_id);
24720 lgstring = composition_gstring_from_id (s->cmp_id);
24721 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24722 glyph++;
24723 while (glyph < last
24724 && glyph->u.cmp.automatic
24725 && glyph->u.cmp.id == s->cmp_id
24726 && s->cmp_to == glyph->slice.cmp.from)
24727 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24728
24729 for (i = s->cmp_from; i < s->cmp_to; i++)
24730 {
24731 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24732 unsigned code = LGLYPH_CODE (lglyph);
24733
24734 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24735 }
24736 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24737 return glyph - s->row->glyphs[s->area];
24738 }
24739
24740
24741 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24742 See the comment of fill_glyph_string for arguments.
24743 Value is the index of the first glyph not in S. */
24744
24745
24746 static int
24747 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24748 int start, int end, int overlaps)
24749 {
24750 struct glyph *glyph, *last;
24751 int voffset;
24752
24753 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24754 s->for_overlaps = overlaps;
24755 glyph = s->row->glyphs[s->area] + start;
24756 last = s->row->glyphs[s->area] + end;
24757 voffset = glyph->voffset;
24758 s->face = FACE_FROM_ID (s->f, face_id);
24759 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24760 s->nchars = 1;
24761 s->width = glyph->pixel_width;
24762 glyph++;
24763 while (glyph < last
24764 && glyph->type == GLYPHLESS_GLYPH
24765 && glyph->voffset == voffset
24766 && glyph->face_id == face_id)
24767 {
24768 s->nchars++;
24769 s->width += glyph->pixel_width;
24770 glyph++;
24771 }
24772 s->ybase += voffset;
24773 return glyph - s->row->glyphs[s->area];
24774 }
24775
24776
24777 /* Fill glyph string S from a sequence of character glyphs.
24778
24779 FACE_ID is the face id of the string. START is the index of the
24780 first glyph to consider, END is the index of the last + 1.
24781 OVERLAPS non-zero means S should draw the foreground only, and use
24782 its physical height for clipping. See also draw_glyphs.
24783
24784 Value is the index of the first glyph not in S. */
24785
24786 static int
24787 fill_glyph_string (struct glyph_string *s, int face_id,
24788 int start, int end, int overlaps)
24789 {
24790 struct glyph *glyph, *last;
24791 int voffset;
24792 bool glyph_not_available_p;
24793
24794 eassert (s->f == XFRAME (s->w->frame));
24795 eassert (s->nchars == 0);
24796 eassert (start >= 0 && end > start);
24797
24798 s->for_overlaps = overlaps;
24799 glyph = s->row->glyphs[s->area] + start;
24800 last = s->row->glyphs[s->area] + end;
24801 voffset = glyph->voffset;
24802 s->padding_p = glyph->padding_p;
24803 glyph_not_available_p = glyph->glyph_not_available_p;
24804
24805 while (glyph < last
24806 && glyph->type == CHAR_GLYPH
24807 && glyph->voffset == voffset
24808 /* Same face id implies same font, nowadays. */
24809 && glyph->face_id == face_id
24810 && glyph->glyph_not_available_p == glyph_not_available_p)
24811 {
24812 s->face = get_glyph_face_and_encoding (s->f, glyph,
24813 s->char2b + s->nchars);
24814 ++s->nchars;
24815 eassert (s->nchars <= end - start);
24816 s->width += glyph->pixel_width;
24817 if (glyph++->padding_p != s->padding_p)
24818 break;
24819 }
24820
24821 s->font = s->face->font;
24822
24823 /* If the specified font could not be loaded, use the frame's font,
24824 but record the fact that we couldn't load it in
24825 S->font_not_found_p so that we can draw rectangles for the
24826 characters of the glyph string. */
24827 if (s->font == NULL || glyph_not_available_p)
24828 {
24829 s->font_not_found_p = true;
24830 s->font = FRAME_FONT (s->f);
24831 }
24832
24833 /* Adjust base line for subscript/superscript text. */
24834 s->ybase += voffset;
24835
24836 eassert (s->face && s->face->gc);
24837 return glyph - s->row->glyphs[s->area];
24838 }
24839
24840
24841 /* Fill glyph string S from image glyph S->first_glyph. */
24842
24843 static void
24844 fill_image_glyph_string (struct glyph_string *s)
24845 {
24846 eassert (s->first_glyph->type == IMAGE_GLYPH);
24847 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24848 eassert (s->img);
24849 s->slice = s->first_glyph->slice.img;
24850 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24851 s->font = s->face->font;
24852 s->width = s->first_glyph->pixel_width;
24853
24854 /* Adjust base line for subscript/superscript text. */
24855 s->ybase += s->first_glyph->voffset;
24856 }
24857
24858
24859 #ifdef HAVE_XWIDGETS
24860 static void
24861 fill_xwidget_glyph_string (struct glyph_string *s)
24862 {
24863 eassert (s->first_glyph->type == XWIDGET_GLYPH);
24864 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24865 s->font = s->face->font;
24866 s->width = s->first_glyph->pixel_width;
24867 s->ybase += s->first_glyph->voffset;
24868 s->xwidget = s->first_glyph->u.xwidget;
24869 }
24870 #endif
24871 /* Fill glyph string S from a sequence of stretch glyphs.
24872
24873 START is the index of the first glyph to consider,
24874 END is the index of the last + 1.
24875
24876 Value is the index of the first glyph not in S. */
24877
24878 static int
24879 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24880 {
24881 struct glyph *glyph, *last;
24882 int voffset, face_id;
24883
24884 eassert (s->first_glyph->type == STRETCH_GLYPH);
24885
24886 glyph = s->row->glyphs[s->area] + start;
24887 last = s->row->glyphs[s->area] + end;
24888 face_id = glyph->face_id;
24889 s->face = FACE_FROM_ID (s->f, face_id);
24890 s->font = s->face->font;
24891 s->width = glyph->pixel_width;
24892 s->nchars = 1;
24893 voffset = glyph->voffset;
24894
24895 for (++glyph;
24896 (glyph < last
24897 && glyph->type == STRETCH_GLYPH
24898 && glyph->voffset == voffset
24899 && glyph->face_id == face_id);
24900 ++glyph)
24901 s->width += glyph->pixel_width;
24902
24903 /* Adjust base line for subscript/superscript text. */
24904 s->ybase += voffset;
24905
24906 /* The case that face->gc == 0 is handled when drawing the glyph
24907 string by calling prepare_face_for_display. */
24908 eassert (s->face);
24909 return glyph - s->row->glyphs[s->area];
24910 }
24911
24912 static struct font_metrics *
24913 get_per_char_metric (struct font *font, XChar2b *char2b)
24914 {
24915 static struct font_metrics metrics;
24916 unsigned code;
24917
24918 if (! font)
24919 return NULL;
24920 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24921 if (code == FONT_INVALID_CODE)
24922 return NULL;
24923 font->driver->text_extents (font, &code, 1, &metrics);
24924 return &metrics;
24925 }
24926
24927 /* A subroutine that computes "normal" values of ASCENT and DESCENT
24928 for FONT. Values are taken from font-global ones, except for fonts
24929 that claim preposterously large values, but whose glyphs actually
24930 have reasonable dimensions. C is the character to use for metrics
24931 if the font-global values are too large; if C is negative, the
24932 function selects a default character. */
24933 static void
24934 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
24935 {
24936 *ascent = FONT_BASE (font);
24937 *descent = FONT_DESCENT (font);
24938
24939 if (FONT_TOO_HIGH (font))
24940 {
24941 XChar2b char2b;
24942
24943 /* Get metrics of C, defaulting to a reasonably sized ASCII
24944 character. */
24945 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
24946 {
24947 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
24948
24949 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
24950 {
24951 /* We add 1 pixel to character dimensions as heuristics
24952 that produces nicer display, e.g. when the face has
24953 the box attribute. */
24954 *ascent = pcm->ascent + 1;
24955 *descent = pcm->descent + 1;
24956 }
24957 }
24958 }
24959 }
24960
24961 /* A subroutine that computes a reasonable "normal character height"
24962 for fonts that claim preposterously large vertical dimensions, but
24963 whose glyphs are actually reasonably sized. C is the character
24964 whose metrics to use for those fonts, or -1 for default
24965 character. */
24966 static int
24967 normal_char_height (struct font *font, int c)
24968 {
24969 int ascent, descent;
24970
24971 normal_char_ascent_descent (font, c, &ascent, &descent);
24972
24973 return ascent + descent;
24974 }
24975
24976 /* EXPORT for RIF:
24977 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24978 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24979 assumed to be zero. */
24980
24981 void
24982 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24983 {
24984 *left = *right = 0;
24985
24986 if (glyph->type == CHAR_GLYPH)
24987 {
24988 XChar2b char2b;
24989 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
24990 if (face->font)
24991 {
24992 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
24993 if (pcm)
24994 {
24995 if (pcm->rbearing > pcm->width)
24996 *right = pcm->rbearing - pcm->width;
24997 if (pcm->lbearing < 0)
24998 *left = -pcm->lbearing;
24999 }
25000 }
25001 }
25002 else if (glyph->type == COMPOSITE_GLYPH)
25003 {
25004 if (! glyph->u.cmp.automatic)
25005 {
25006 struct composition *cmp = composition_table[glyph->u.cmp.id];
25007
25008 if (cmp->rbearing > cmp->pixel_width)
25009 *right = cmp->rbearing - cmp->pixel_width;
25010 if (cmp->lbearing < 0)
25011 *left = - cmp->lbearing;
25012 }
25013 else
25014 {
25015 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25016 struct font_metrics metrics;
25017
25018 composition_gstring_width (gstring, glyph->slice.cmp.from,
25019 glyph->slice.cmp.to + 1, &metrics);
25020 if (metrics.rbearing > metrics.width)
25021 *right = metrics.rbearing - metrics.width;
25022 if (metrics.lbearing < 0)
25023 *left = - metrics.lbearing;
25024 }
25025 }
25026 }
25027
25028
25029 /* Return the index of the first glyph preceding glyph string S that
25030 is overwritten by S because of S's left overhang. Value is -1
25031 if no glyphs are overwritten. */
25032
25033 static int
25034 left_overwritten (struct glyph_string *s)
25035 {
25036 int k;
25037
25038 if (s->left_overhang)
25039 {
25040 int x = 0, i;
25041 struct glyph *glyphs = s->row->glyphs[s->area];
25042 int first = s->first_glyph - glyphs;
25043
25044 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25045 x -= glyphs[i].pixel_width;
25046
25047 k = i + 1;
25048 }
25049 else
25050 k = -1;
25051
25052 return k;
25053 }
25054
25055
25056 /* Return the index of the first glyph preceding glyph string S that
25057 is overwriting S because of its right overhang. Value is -1 if no
25058 glyph in front of S overwrites S. */
25059
25060 static int
25061 left_overwriting (struct glyph_string *s)
25062 {
25063 int i, k, x;
25064 struct glyph *glyphs = s->row->glyphs[s->area];
25065 int first = s->first_glyph - glyphs;
25066
25067 k = -1;
25068 x = 0;
25069 for (i = first - 1; i >= 0; --i)
25070 {
25071 int left, right;
25072 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25073 if (x + right > 0)
25074 k = i;
25075 x -= glyphs[i].pixel_width;
25076 }
25077
25078 return k;
25079 }
25080
25081
25082 /* Return the index of the last glyph following glyph string S that is
25083 overwritten by S because of S's right overhang. Value is -1 if
25084 no such glyph is found. */
25085
25086 static int
25087 right_overwritten (struct glyph_string *s)
25088 {
25089 int k = -1;
25090
25091 if (s->right_overhang)
25092 {
25093 int x = 0, i;
25094 struct glyph *glyphs = s->row->glyphs[s->area];
25095 int first = (s->first_glyph - glyphs
25096 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25097 int end = s->row->used[s->area];
25098
25099 for (i = first; i < end && s->right_overhang > x; ++i)
25100 x += glyphs[i].pixel_width;
25101
25102 k = i;
25103 }
25104
25105 return k;
25106 }
25107
25108
25109 /* Return the index of the last glyph following glyph string S that
25110 overwrites S because of its left overhang. Value is negative
25111 if no such glyph is found. */
25112
25113 static int
25114 right_overwriting (struct glyph_string *s)
25115 {
25116 int i, k, x;
25117 int end = s->row->used[s->area];
25118 struct glyph *glyphs = s->row->glyphs[s->area];
25119 int first = (s->first_glyph - glyphs
25120 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25121
25122 k = -1;
25123 x = 0;
25124 for (i = first; i < end; ++i)
25125 {
25126 int left, right;
25127 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25128 if (x - left < 0)
25129 k = i;
25130 x += glyphs[i].pixel_width;
25131 }
25132
25133 return k;
25134 }
25135
25136
25137 /* Set background width of glyph string S. START is the index of the
25138 first glyph following S. LAST_X is the right-most x-position + 1
25139 in the drawing area. */
25140
25141 static void
25142 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
25143 {
25144 /* If the face of this glyph string has to be drawn to the end of
25145 the drawing area, set S->extends_to_end_of_line_p. */
25146
25147 if (start == s->row->used[s->area]
25148 && ((s->row->fill_line_p
25149 && (s->hl == DRAW_NORMAL_TEXT
25150 || s->hl == DRAW_IMAGE_RAISED
25151 || s->hl == DRAW_IMAGE_SUNKEN))
25152 || s->hl == DRAW_MOUSE_FACE))
25153 s->extends_to_end_of_line_p = true;
25154
25155 /* If S extends its face to the end of the line, set its
25156 background_width to the distance to the right edge of the drawing
25157 area. */
25158 if (s->extends_to_end_of_line_p)
25159 s->background_width = last_x - s->x + 1;
25160 else
25161 s->background_width = s->width;
25162 }
25163
25164
25165 /* Compute overhangs and x-positions for glyph string S and its
25166 predecessors, or successors. X is the starting x-position for S.
25167 BACKWARD_P means process predecessors. */
25168
25169 static void
25170 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
25171 {
25172 if (backward_p)
25173 {
25174 while (s)
25175 {
25176 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25177 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25178 x -= s->width;
25179 s->x = x;
25180 s = s->prev;
25181 }
25182 }
25183 else
25184 {
25185 while (s)
25186 {
25187 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25188 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25189 s->x = x;
25190 x += s->width;
25191 s = s->next;
25192 }
25193 }
25194 }
25195
25196
25197
25198 /* The following macros are only called from draw_glyphs below.
25199 They reference the following parameters of that function directly:
25200 `w', `row', `area', and `overlap_p'
25201 as well as the following local variables:
25202 `s', `f', and `hdc' (in W32) */
25203
25204 #ifdef HAVE_NTGUI
25205 /* On W32, silently add local `hdc' variable to argument list of
25206 init_glyph_string. */
25207 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25208 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25209 #else
25210 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25211 init_glyph_string (s, char2b, w, row, area, start, hl)
25212 #endif
25213
25214 /* Add a glyph string for a stretch glyph to the list of strings
25215 between HEAD and TAIL. START is the index of the stretch glyph in
25216 row area AREA of glyph row ROW. END is the index of the last glyph
25217 in that glyph row area. X is the current output position assigned
25218 to the new glyph string constructed. HL overrides that face of the
25219 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25220 is the right-most x-position of the drawing area. */
25221
25222 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25223 and below -- keep them on one line. */
25224 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25225 do \
25226 { \
25227 s = alloca (sizeof *s); \
25228 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25229 START = fill_stretch_glyph_string (s, START, END); \
25230 append_glyph_string (&HEAD, &TAIL, s); \
25231 s->x = (X); \
25232 } \
25233 while (false)
25234
25235
25236 /* Add a glyph string for an image glyph to the list of strings
25237 between HEAD and TAIL. START is the index of the image glyph in
25238 row area AREA of glyph row ROW. END is the index of the last glyph
25239 in that glyph row area. X is the current output position assigned
25240 to the new glyph string constructed. HL overrides that face of the
25241 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25242 is the right-most x-position of the drawing area. */
25243
25244 #define BUILD_IMAGE_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 fill_image_glyph_string (s); \
25250 append_glyph_string (&HEAD, &TAIL, s); \
25251 ++START; \
25252 s->x = (X); \
25253 } \
25254 while (false)
25255
25256 #ifndef HAVE_XWIDGETS
25257 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25258 eassume (false)
25259 #else
25260 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25261 do \
25262 { \
25263 s = alloca (sizeof *s); \
25264 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25265 fill_xwidget_glyph_string (s); \
25266 append_glyph_string (&(HEAD), &(TAIL), s); \
25267 ++(START); \
25268 s->x = (X); \
25269 } \
25270 while (false)
25271 #endif
25272
25273 /* Add a glyph string for a sequence of character glyphs to the list
25274 of strings between HEAD and TAIL. START is the index of the first
25275 glyph in row area AREA of glyph row ROW that is part of the new
25276 glyph string. END is the index of the last glyph in that glyph row
25277 area. X is the current output position assigned to the new glyph
25278 string constructed. HL overrides that face of the glyph; e.g. it
25279 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25280 right-most x-position of the drawing area. */
25281
25282 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25283 do \
25284 { \
25285 int face_id; \
25286 XChar2b *char2b; \
25287 \
25288 face_id = (row)->glyphs[area][START].face_id; \
25289 \
25290 s = alloca (sizeof *s); \
25291 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25292 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25293 append_glyph_string (&HEAD, &TAIL, s); \
25294 s->x = (X); \
25295 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25296 } \
25297 while (false)
25298
25299
25300 /* Add a glyph string for a composite sequence to the list of strings
25301 between HEAD and TAIL. START is the index of the first glyph in
25302 row area AREA of glyph row ROW that is part of the new glyph
25303 string. END is the index of the last glyph in that glyph row area.
25304 X is the current output position assigned to the new glyph string
25305 constructed. HL overrides that face of the glyph; e.g. it is
25306 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25307 x-position of the drawing area. */
25308
25309 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25310 do { \
25311 int face_id = (row)->glyphs[area][START].face_id; \
25312 struct face *base_face = FACE_FROM_ID (f, face_id); \
25313 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25314 struct composition *cmp = composition_table[cmp_id]; \
25315 XChar2b *char2b; \
25316 struct glyph_string *first_s = NULL; \
25317 int n; \
25318 \
25319 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25320 \
25321 /* Make glyph_strings for each glyph sequence that is drawable by \
25322 the same face, and append them to HEAD/TAIL. */ \
25323 for (n = 0; n < cmp->glyph_len;) \
25324 { \
25325 s = alloca (sizeof *s); \
25326 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25327 append_glyph_string (&(HEAD), &(TAIL), s); \
25328 s->cmp = cmp; \
25329 s->cmp_from = n; \
25330 s->x = (X); \
25331 if (n == 0) \
25332 first_s = s; \
25333 n = fill_composite_glyph_string (s, base_face, overlaps); \
25334 } \
25335 \
25336 ++START; \
25337 s = first_s; \
25338 } while (false)
25339
25340
25341 /* Add a glyph string for a glyph-string sequence to the list of strings
25342 between HEAD and TAIL. */
25343
25344 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25345 do { \
25346 int face_id; \
25347 XChar2b *char2b; \
25348 Lisp_Object gstring; \
25349 \
25350 face_id = (row)->glyphs[area][START].face_id; \
25351 gstring = (composition_gstring_from_id \
25352 ((row)->glyphs[area][START].u.cmp.id)); \
25353 s = alloca (sizeof *s); \
25354 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25355 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25356 append_glyph_string (&(HEAD), &(TAIL), s); \
25357 s->x = (X); \
25358 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25359 } while (false)
25360
25361
25362 /* Add a glyph string for a sequence of glyphless character's glyphs
25363 to the list of strings between HEAD and TAIL. The meanings of
25364 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25365
25366 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25367 do \
25368 { \
25369 int face_id; \
25370 \
25371 face_id = (row)->glyphs[area][START].face_id; \
25372 \
25373 s = alloca (sizeof *s); \
25374 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25375 append_glyph_string (&HEAD, &TAIL, s); \
25376 s->x = (X); \
25377 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25378 overlaps); \
25379 } \
25380 while (false)
25381
25382
25383 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25384 of AREA of glyph row ROW on window W between indices START and END.
25385 HL overrides the face for drawing glyph strings, e.g. it is
25386 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25387 x-positions of the drawing area.
25388
25389 This is an ugly monster macro construct because we must use alloca
25390 to allocate glyph strings (because draw_glyphs can be called
25391 asynchronously). */
25392
25393 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25394 do \
25395 { \
25396 HEAD = TAIL = NULL; \
25397 while (START < END) \
25398 { \
25399 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25400 switch (first_glyph->type) \
25401 { \
25402 case CHAR_GLYPH: \
25403 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25404 HL, X, LAST_X); \
25405 break; \
25406 \
25407 case COMPOSITE_GLYPH: \
25408 if (first_glyph->u.cmp.automatic) \
25409 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25410 HL, X, LAST_X); \
25411 else \
25412 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25413 HL, X, LAST_X); \
25414 break; \
25415 \
25416 case STRETCH_GLYPH: \
25417 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25418 HL, X, LAST_X); \
25419 break; \
25420 \
25421 case IMAGE_GLYPH: \
25422 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25423 HL, X, LAST_X); \
25424 break;
25425
25426 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25427 case XWIDGET_GLYPH: \
25428 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
25429 HL, X, LAST_X); \
25430 break;
25431
25432 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
25433 case GLYPHLESS_GLYPH: \
25434 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25435 HL, X, LAST_X); \
25436 break; \
25437 \
25438 default: \
25439 emacs_abort (); \
25440 } \
25441 \
25442 if (s) \
25443 { \
25444 set_glyph_string_background_width (s, START, LAST_X); \
25445 (X) += s->width; \
25446 } \
25447 } \
25448 } while (false)
25449
25450
25451 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25452 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25453 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25454 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
25455
25456
25457 /* Draw glyphs between START and END in AREA of ROW on window W,
25458 starting at x-position X. X is relative to AREA in W. HL is a
25459 face-override with the following meaning:
25460
25461 DRAW_NORMAL_TEXT draw normally
25462 DRAW_CURSOR draw in cursor face
25463 DRAW_MOUSE_FACE draw in mouse face.
25464 DRAW_INVERSE_VIDEO draw in mode line face
25465 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25466 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25467
25468 If OVERLAPS is non-zero, draw only the foreground of characters and
25469 clip to the physical height of ROW. Non-zero value also defines
25470 the overlapping part to be drawn:
25471
25472 OVERLAPS_PRED overlap with preceding rows
25473 OVERLAPS_SUCC overlap with succeeding rows
25474 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25475 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25476
25477 Value is the x-position reached, relative to AREA of W. */
25478
25479 static int
25480 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25481 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25482 enum draw_glyphs_face hl, int overlaps)
25483 {
25484 struct glyph_string *head, *tail;
25485 struct glyph_string *s;
25486 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25487 int i, j, x_reached, last_x, area_left = 0;
25488 struct frame *f = XFRAME (WINDOW_FRAME (w));
25489 DECLARE_HDC (hdc);
25490
25491 ALLOCATE_HDC (hdc, f);
25492
25493 /* Let's rather be paranoid than getting a SEGV. */
25494 end = min (end, row->used[area]);
25495 start = clip_to_bounds (0, start, end);
25496
25497 /* Translate X to frame coordinates. Set last_x to the right
25498 end of the drawing area. */
25499 if (row->full_width_p)
25500 {
25501 /* X is relative to the left edge of W, without scroll bars
25502 or fringes. */
25503 area_left = WINDOW_LEFT_EDGE_X (w);
25504 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25505 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25506 }
25507 else
25508 {
25509 area_left = window_box_left (w, area);
25510 last_x = area_left + window_box_width (w, area);
25511 }
25512 x += area_left;
25513
25514 /* Build a doubly-linked list of glyph_string structures between
25515 head and tail from what we have to draw. Note that the macro
25516 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25517 the reason we use a separate variable `i'. */
25518 i = start;
25519 USE_SAFE_ALLOCA;
25520 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25521 if (tail)
25522 x_reached = tail->x + tail->background_width;
25523 else
25524 x_reached = x;
25525
25526 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25527 the row, redraw some glyphs in front or following the glyph
25528 strings built above. */
25529 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25530 {
25531 struct glyph_string *h, *t;
25532 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25533 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25534 bool check_mouse_face = false;
25535 int dummy_x = 0;
25536
25537 /* If mouse highlighting is on, we may need to draw adjacent
25538 glyphs using mouse-face highlighting. */
25539 if (area == TEXT_AREA && row->mouse_face_p
25540 && hlinfo->mouse_face_beg_row >= 0
25541 && hlinfo->mouse_face_end_row >= 0)
25542 {
25543 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25544
25545 if (row_vpos >= hlinfo->mouse_face_beg_row
25546 && row_vpos <= hlinfo->mouse_face_end_row)
25547 {
25548 check_mouse_face = true;
25549 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25550 ? hlinfo->mouse_face_beg_col : 0;
25551 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25552 ? hlinfo->mouse_face_end_col
25553 : row->used[TEXT_AREA];
25554 }
25555 }
25556
25557 /* Compute overhangs for all glyph strings. */
25558 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25559 for (s = head; s; s = s->next)
25560 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25561
25562 /* Prepend glyph strings for glyphs in front of the first glyph
25563 string that are overwritten because of the first glyph
25564 string's left overhang. The background of all strings
25565 prepended must be drawn because the first glyph string
25566 draws over it. */
25567 i = left_overwritten (head);
25568 if (i >= 0)
25569 {
25570 enum draw_glyphs_face overlap_hl;
25571
25572 /* If this row contains mouse highlighting, attempt to draw
25573 the overlapped glyphs with the correct highlight. This
25574 code fails if the overlap encompasses more than one glyph
25575 and mouse-highlight spans only some of these glyphs.
25576 However, making it work perfectly involves a lot more
25577 code, and I don't know if the pathological case occurs in
25578 practice, so we'll stick to this for now. --- cyd */
25579 if (check_mouse_face
25580 && mouse_beg_col < start && mouse_end_col > i)
25581 overlap_hl = DRAW_MOUSE_FACE;
25582 else
25583 overlap_hl = DRAW_NORMAL_TEXT;
25584
25585 if (hl != overlap_hl)
25586 clip_head = head;
25587 j = i;
25588 BUILD_GLYPH_STRINGS (j, start, h, t,
25589 overlap_hl, dummy_x, last_x);
25590 start = i;
25591 compute_overhangs_and_x (t, head->x, true);
25592 prepend_glyph_string_lists (&head, &tail, h, t);
25593 if (clip_head == NULL)
25594 clip_head = head;
25595 }
25596
25597 /* Prepend glyph strings for glyphs in front of the first glyph
25598 string that overwrite that glyph string because of their
25599 right overhang. For these strings, only the foreground must
25600 be drawn, because it draws over the glyph string at `head'.
25601 The background must not be drawn because this would overwrite
25602 right overhangs of preceding glyphs for which no glyph
25603 strings exist. */
25604 i = left_overwriting (head);
25605 if (i >= 0)
25606 {
25607 enum draw_glyphs_face overlap_hl;
25608
25609 if (check_mouse_face
25610 && mouse_beg_col < start && mouse_end_col > i)
25611 overlap_hl = DRAW_MOUSE_FACE;
25612 else
25613 overlap_hl = DRAW_NORMAL_TEXT;
25614
25615 if (hl == overlap_hl || clip_head == NULL)
25616 clip_head = head;
25617 BUILD_GLYPH_STRINGS (i, start, h, t,
25618 overlap_hl, dummy_x, last_x);
25619 for (s = h; s; s = s->next)
25620 s->background_filled_p = true;
25621 compute_overhangs_and_x (t, head->x, true);
25622 prepend_glyph_string_lists (&head, &tail, h, t);
25623 }
25624
25625 /* Append glyphs strings for glyphs following the last glyph
25626 string tail that are overwritten by tail. The background of
25627 these strings has to be drawn because tail's foreground draws
25628 over it. */
25629 i = right_overwritten (tail);
25630 if (i >= 0)
25631 {
25632 enum draw_glyphs_face overlap_hl;
25633
25634 if (check_mouse_face
25635 && mouse_beg_col < i && mouse_end_col > end)
25636 overlap_hl = DRAW_MOUSE_FACE;
25637 else
25638 overlap_hl = DRAW_NORMAL_TEXT;
25639
25640 if (hl != overlap_hl)
25641 clip_tail = tail;
25642 BUILD_GLYPH_STRINGS (end, i, h, t,
25643 overlap_hl, x, last_x);
25644 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25645 we don't have `end = i;' here. */
25646 compute_overhangs_and_x (h, tail->x + tail->width, false);
25647 append_glyph_string_lists (&head, &tail, h, t);
25648 if (clip_tail == NULL)
25649 clip_tail = tail;
25650 }
25651
25652 /* Append glyph strings for glyphs following the last glyph
25653 string tail that overwrite tail. The foreground of such
25654 glyphs has to be drawn because it writes into the background
25655 of tail. The background must not be drawn because it could
25656 paint over the foreground of following glyphs. */
25657 i = right_overwriting (tail);
25658 if (i >= 0)
25659 {
25660 enum draw_glyphs_face overlap_hl;
25661 if (check_mouse_face
25662 && mouse_beg_col < i && mouse_end_col > end)
25663 overlap_hl = DRAW_MOUSE_FACE;
25664 else
25665 overlap_hl = DRAW_NORMAL_TEXT;
25666
25667 if (hl == overlap_hl || clip_tail == NULL)
25668 clip_tail = tail;
25669 i++; /* We must include the Ith glyph. */
25670 BUILD_GLYPH_STRINGS (end, i, h, t,
25671 overlap_hl, x, last_x);
25672 for (s = h; s; s = s->next)
25673 s->background_filled_p = true;
25674 compute_overhangs_and_x (h, tail->x + tail->width, false);
25675 append_glyph_string_lists (&head, &tail, h, t);
25676 }
25677 if (clip_head || clip_tail)
25678 for (s = head; s; s = s->next)
25679 {
25680 s->clip_head = clip_head;
25681 s->clip_tail = clip_tail;
25682 }
25683 }
25684
25685 /* Draw all strings. */
25686 for (s = head; s; s = s->next)
25687 FRAME_RIF (f)->draw_glyph_string (s);
25688
25689 #ifndef HAVE_NS
25690 /* When focus a sole frame and move horizontally, this clears on_p
25691 causing a failure to erase prev cursor position. */
25692 if (area == TEXT_AREA
25693 && !row->full_width_p
25694 /* When drawing overlapping rows, only the glyph strings'
25695 foreground is drawn, which doesn't erase a cursor
25696 completely. */
25697 && !overlaps)
25698 {
25699 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25700 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25701 : (tail ? tail->x + tail->background_width : x));
25702 x0 -= area_left;
25703 x1 -= area_left;
25704
25705 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25706 row->y, MATRIX_ROW_BOTTOM_Y (row));
25707 }
25708 #endif
25709
25710 /* Value is the x-position up to which drawn, relative to AREA of W.
25711 This doesn't include parts drawn because of overhangs. */
25712 if (row->full_width_p)
25713 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25714 else
25715 x_reached -= area_left;
25716
25717 RELEASE_HDC (hdc, f);
25718
25719 SAFE_FREE ();
25720 return x_reached;
25721 }
25722
25723 /* Expand row matrix if too narrow. Don't expand if area
25724 is not present. */
25725
25726 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25727 { \
25728 if (!it->f->fonts_changed \
25729 && (it->glyph_row->glyphs[area] \
25730 < it->glyph_row->glyphs[area + 1])) \
25731 { \
25732 it->w->ncols_scale_factor++; \
25733 it->f->fonts_changed = true; \
25734 } \
25735 }
25736
25737 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25738 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25739
25740 static void
25741 append_glyph (struct it *it)
25742 {
25743 struct glyph *glyph;
25744 enum glyph_row_area area = it->area;
25745
25746 eassert (it->glyph_row);
25747 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25748
25749 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25750 if (glyph < it->glyph_row->glyphs[area + 1])
25751 {
25752 /* If the glyph row is reversed, we need to prepend the glyph
25753 rather than append it. */
25754 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25755 {
25756 struct glyph *g;
25757
25758 /* Make room for the additional glyph. */
25759 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25760 g[1] = *g;
25761 glyph = it->glyph_row->glyphs[area];
25762 }
25763 glyph->charpos = CHARPOS (it->position);
25764 glyph->object = it->object;
25765 if (it->pixel_width > 0)
25766 {
25767 glyph->pixel_width = it->pixel_width;
25768 glyph->padding_p = false;
25769 }
25770 else
25771 {
25772 /* Assure at least 1-pixel width. Otherwise, cursor can't
25773 be displayed correctly. */
25774 glyph->pixel_width = 1;
25775 glyph->padding_p = true;
25776 }
25777 glyph->ascent = it->ascent;
25778 glyph->descent = it->descent;
25779 glyph->voffset = it->voffset;
25780 glyph->type = CHAR_GLYPH;
25781 glyph->avoid_cursor_p = it->avoid_cursor_p;
25782 glyph->multibyte_p = it->multibyte_p;
25783 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25784 {
25785 /* In R2L rows, the left and the right box edges need to be
25786 drawn in reverse direction. */
25787 glyph->right_box_line_p = it->start_of_box_run_p;
25788 glyph->left_box_line_p = it->end_of_box_run_p;
25789 }
25790 else
25791 {
25792 glyph->left_box_line_p = it->start_of_box_run_p;
25793 glyph->right_box_line_p = it->end_of_box_run_p;
25794 }
25795 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25796 || it->phys_descent > it->descent);
25797 glyph->glyph_not_available_p = it->glyph_not_available_p;
25798 glyph->face_id = it->face_id;
25799 glyph->u.ch = it->char_to_display;
25800 glyph->slice.img = null_glyph_slice;
25801 glyph->font_type = FONT_TYPE_UNKNOWN;
25802 if (it->bidi_p)
25803 {
25804 glyph->resolved_level = it->bidi_it.resolved_level;
25805 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25806 glyph->bidi_type = it->bidi_it.type;
25807 }
25808 else
25809 {
25810 glyph->resolved_level = 0;
25811 glyph->bidi_type = UNKNOWN_BT;
25812 }
25813 ++it->glyph_row->used[area];
25814 }
25815 else
25816 IT_EXPAND_MATRIX_WIDTH (it, area);
25817 }
25818
25819 /* Store one glyph for the composition IT->cmp_it.id in
25820 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25821 non-null. */
25822
25823 static void
25824 append_composite_glyph (struct it *it)
25825 {
25826 struct glyph *glyph;
25827 enum glyph_row_area area = it->area;
25828
25829 eassert (it->glyph_row);
25830
25831 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25832 if (glyph < it->glyph_row->glyphs[area + 1])
25833 {
25834 /* If the glyph row is reversed, we need to prepend the glyph
25835 rather than append it. */
25836 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25837 {
25838 struct glyph *g;
25839
25840 /* Make room for the new glyph. */
25841 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25842 g[1] = *g;
25843 glyph = it->glyph_row->glyphs[it->area];
25844 }
25845 glyph->charpos = it->cmp_it.charpos;
25846 glyph->object = it->object;
25847 glyph->pixel_width = it->pixel_width;
25848 glyph->ascent = it->ascent;
25849 glyph->descent = it->descent;
25850 glyph->voffset = it->voffset;
25851 glyph->type = COMPOSITE_GLYPH;
25852 if (it->cmp_it.ch < 0)
25853 {
25854 glyph->u.cmp.automatic = false;
25855 glyph->u.cmp.id = it->cmp_it.id;
25856 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25857 }
25858 else
25859 {
25860 glyph->u.cmp.automatic = true;
25861 glyph->u.cmp.id = it->cmp_it.id;
25862 glyph->slice.cmp.from = it->cmp_it.from;
25863 glyph->slice.cmp.to = it->cmp_it.to - 1;
25864 }
25865 glyph->avoid_cursor_p = it->avoid_cursor_p;
25866 glyph->multibyte_p = it->multibyte_p;
25867 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25868 {
25869 /* In R2L rows, the left and the right box edges need to be
25870 drawn in reverse direction. */
25871 glyph->right_box_line_p = it->start_of_box_run_p;
25872 glyph->left_box_line_p = it->end_of_box_run_p;
25873 }
25874 else
25875 {
25876 glyph->left_box_line_p = it->start_of_box_run_p;
25877 glyph->right_box_line_p = it->end_of_box_run_p;
25878 }
25879 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25880 || it->phys_descent > it->descent);
25881 glyph->padding_p = false;
25882 glyph->glyph_not_available_p = false;
25883 glyph->face_id = it->face_id;
25884 glyph->font_type = FONT_TYPE_UNKNOWN;
25885 if (it->bidi_p)
25886 {
25887 glyph->resolved_level = it->bidi_it.resolved_level;
25888 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25889 glyph->bidi_type = it->bidi_it.type;
25890 }
25891 ++it->glyph_row->used[area];
25892 }
25893 else
25894 IT_EXPAND_MATRIX_WIDTH (it, area);
25895 }
25896
25897
25898 /* Change IT->ascent and IT->height according to the setting of
25899 IT->voffset. */
25900
25901 static void
25902 take_vertical_position_into_account (struct it *it)
25903 {
25904 if (it->voffset)
25905 {
25906 if (it->voffset < 0)
25907 /* Increase the ascent so that we can display the text higher
25908 in the line. */
25909 it->ascent -= it->voffset;
25910 else
25911 /* Increase the descent so that we can display the text lower
25912 in the line. */
25913 it->descent += it->voffset;
25914 }
25915 }
25916
25917
25918 /* Produce glyphs/get display metrics for the image IT is loaded with.
25919 See the description of struct display_iterator in dispextern.h for
25920 an overview of struct display_iterator. */
25921
25922 static void
25923 produce_image_glyph (struct it *it)
25924 {
25925 struct image *img;
25926 struct face *face;
25927 int glyph_ascent, crop;
25928 struct glyph_slice slice;
25929
25930 eassert (it->what == IT_IMAGE);
25931
25932 face = FACE_FROM_ID (it->f, it->face_id);
25933 eassert (face);
25934 /* Make sure X resources of the face is loaded. */
25935 prepare_face_for_display (it->f, face);
25936
25937 if (it->image_id < 0)
25938 {
25939 /* Fringe bitmap. */
25940 it->ascent = it->phys_ascent = 0;
25941 it->descent = it->phys_descent = 0;
25942 it->pixel_width = 0;
25943 it->nglyphs = 0;
25944 return;
25945 }
25946
25947 img = IMAGE_FROM_ID (it->f, it->image_id);
25948 eassert (img);
25949 /* Make sure X resources of the image is loaded. */
25950 prepare_image_for_display (it->f, img);
25951
25952 slice.x = slice.y = 0;
25953 slice.width = img->width;
25954 slice.height = img->height;
25955
25956 if (INTEGERP (it->slice.x))
25957 slice.x = XINT (it->slice.x);
25958 else if (FLOATP (it->slice.x))
25959 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25960
25961 if (INTEGERP (it->slice.y))
25962 slice.y = XINT (it->slice.y);
25963 else if (FLOATP (it->slice.y))
25964 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25965
25966 if (INTEGERP (it->slice.width))
25967 slice.width = XINT (it->slice.width);
25968 else if (FLOATP (it->slice.width))
25969 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25970
25971 if (INTEGERP (it->slice.height))
25972 slice.height = XINT (it->slice.height);
25973 else if (FLOATP (it->slice.height))
25974 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25975
25976 if (slice.x >= img->width)
25977 slice.x = img->width;
25978 if (slice.y >= img->height)
25979 slice.y = img->height;
25980 if (slice.x + slice.width >= img->width)
25981 slice.width = img->width - slice.x;
25982 if (slice.y + slice.height > img->height)
25983 slice.height = img->height - slice.y;
25984
25985 if (slice.width == 0 || slice.height == 0)
25986 return;
25987
25988 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25989
25990 it->descent = slice.height - glyph_ascent;
25991 if (slice.y == 0)
25992 it->descent += img->vmargin;
25993 if (slice.y + slice.height == img->height)
25994 it->descent += img->vmargin;
25995 it->phys_descent = it->descent;
25996
25997 it->pixel_width = slice.width;
25998 if (slice.x == 0)
25999 it->pixel_width += img->hmargin;
26000 if (slice.x + slice.width == img->width)
26001 it->pixel_width += img->hmargin;
26002
26003 /* It's quite possible for images to have an ascent greater than
26004 their height, so don't get confused in that case. */
26005 if (it->descent < 0)
26006 it->descent = 0;
26007
26008 it->nglyphs = 1;
26009
26010 if (face->box != FACE_NO_BOX)
26011 {
26012 if (face->box_line_width > 0)
26013 {
26014 if (slice.y == 0)
26015 it->ascent += face->box_line_width;
26016 if (slice.y + slice.height == img->height)
26017 it->descent += face->box_line_width;
26018 }
26019
26020 if (it->start_of_box_run_p && slice.x == 0)
26021 it->pixel_width += eabs (face->box_line_width);
26022 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
26023 it->pixel_width += eabs (face->box_line_width);
26024 }
26025
26026 take_vertical_position_into_account (it);
26027
26028 /* Automatically crop wide image glyphs at right edge so we can
26029 draw the cursor on same display row. */
26030 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
26031 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26032 {
26033 it->pixel_width -= crop;
26034 slice.width -= crop;
26035 }
26036
26037 if (it->glyph_row)
26038 {
26039 struct glyph *glyph;
26040 enum glyph_row_area area = it->area;
26041
26042 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26043 if (it->glyph_row->reversed_p)
26044 {
26045 struct glyph *g;
26046
26047 /* Make room for the new glyph. */
26048 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26049 g[1] = *g;
26050 glyph = it->glyph_row->glyphs[it->area];
26051 }
26052 if (glyph < it->glyph_row->glyphs[area + 1])
26053 {
26054 glyph->charpos = CHARPOS (it->position);
26055 glyph->object = it->object;
26056 glyph->pixel_width = it->pixel_width;
26057 glyph->ascent = glyph_ascent;
26058 glyph->descent = it->descent;
26059 glyph->voffset = it->voffset;
26060 glyph->type = IMAGE_GLYPH;
26061 glyph->avoid_cursor_p = it->avoid_cursor_p;
26062 glyph->multibyte_p = it->multibyte_p;
26063 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26064 {
26065 /* In R2L rows, the left and the right box edges need to be
26066 drawn in reverse direction. */
26067 glyph->right_box_line_p = it->start_of_box_run_p;
26068 glyph->left_box_line_p = it->end_of_box_run_p;
26069 }
26070 else
26071 {
26072 glyph->left_box_line_p = it->start_of_box_run_p;
26073 glyph->right_box_line_p = it->end_of_box_run_p;
26074 }
26075 glyph->overlaps_vertically_p = false;
26076 glyph->padding_p = false;
26077 glyph->glyph_not_available_p = false;
26078 glyph->face_id = it->face_id;
26079 glyph->u.img_id = img->id;
26080 glyph->slice.img = slice;
26081 glyph->font_type = FONT_TYPE_UNKNOWN;
26082 if (it->bidi_p)
26083 {
26084 glyph->resolved_level = it->bidi_it.resolved_level;
26085 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26086 glyph->bidi_type = it->bidi_it.type;
26087 }
26088 ++it->glyph_row->used[area];
26089 }
26090 else
26091 IT_EXPAND_MATRIX_WIDTH (it, area);
26092 }
26093 }
26094
26095 static void
26096 produce_xwidget_glyph (struct it *it)
26097 {
26098 #ifdef HAVE_XWIDGETS
26099 struct xwidget *xw;
26100 int glyph_ascent, crop;
26101 eassert (it->what == IT_XWIDGET);
26102
26103 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26104 eassert (face);
26105 /* Make sure X resources of the face is loaded. */
26106 prepare_face_for_display (it->f, face);
26107
26108 xw = it->xwidget;
26109 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
26110 it->descent = xw->height/2;
26111 it->phys_descent = it->descent;
26112 it->pixel_width = xw->width;
26113 /* It's quite possible for images to have an ascent greater than
26114 their height, so don't get confused in that case. */
26115 if (it->descent < 0)
26116 it->descent = 0;
26117
26118 it->nglyphs = 1;
26119
26120 if (face->box != FACE_NO_BOX)
26121 {
26122 if (face->box_line_width > 0)
26123 {
26124 it->ascent += face->box_line_width;
26125 it->descent += face->box_line_width;
26126 }
26127
26128 if (it->start_of_box_run_p)
26129 it->pixel_width += eabs (face->box_line_width);
26130 it->pixel_width += eabs (face->box_line_width);
26131 }
26132
26133 take_vertical_position_into_account (it);
26134
26135 /* Automatically crop wide image glyphs at right edge so we can
26136 draw the cursor on same display row. */
26137 crop = it->pixel_width - (it->last_visible_x - it->current_x);
26138 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26139 it->pixel_width -= crop;
26140
26141 if (it->glyph_row)
26142 {
26143 enum glyph_row_area area = it->area;
26144 struct glyph *glyph
26145 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26146
26147 if (it->glyph_row->reversed_p)
26148 {
26149 struct glyph *g;
26150
26151 /* Make room for the new glyph. */
26152 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26153 g[1] = *g;
26154 glyph = it->glyph_row->glyphs[it->area];
26155 }
26156 if (glyph < it->glyph_row->glyphs[area + 1])
26157 {
26158 glyph->charpos = CHARPOS (it->position);
26159 glyph->object = it->object;
26160 glyph->pixel_width = it->pixel_width;
26161 glyph->ascent = glyph_ascent;
26162 glyph->descent = it->descent;
26163 glyph->voffset = it->voffset;
26164 glyph->type = XWIDGET_GLYPH;
26165 glyph->avoid_cursor_p = it->avoid_cursor_p;
26166 glyph->multibyte_p = it->multibyte_p;
26167 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26168 {
26169 /* In R2L rows, the left and the right box edges need to be
26170 drawn in reverse direction. */
26171 glyph->right_box_line_p = it->start_of_box_run_p;
26172 glyph->left_box_line_p = it->end_of_box_run_p;
26173 }
26174 else
26175 {
26176 glyph->left_box_line_p = it->start_of_box_run_p;
26177 glyph->right_box_line_p = it->end_of_box_run_p;
26178 }
26179 glyph->overlaps_vertically_p = 0;
26180 glyph->padding_p = 0;
26181 glyph->glyph_not_available_p = 0;
26182 glyph->face_id = it->face_id;
26183 glyph->u.xwidget = it->xwidget;
26184 glyph->font_type = FONT_TYPE_UNKNOWN;
26185 if (it->bidi_p)
26186 {
26187 glyph->resolved_level = it->bidi_it.resolved_level;
26188 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26189 glyph->bidi_type = it->bidi_it.type;
26190 }
26191 ++it->glyph_row->used[area];
26192 }
26193 else
26194 IT_EXPAND_MATRIX_WIDTH (it, area);
26195 }
26196 #endif
26197 }
26198
26199 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
26200 of the glyph, WIDTH and HEIGHT are the width and height of the
26201 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
26202
26203 static void
26204 append_stretch_glyph (struct it *it, Lisp_Object object,
26205 int width, int height, int ascent)
26206 {
26207 struct glyph *glyph;
26208 enum glyph_row_area area = it->area;
26209
26210 eassert (ascent >= 0 && ascent <= height);
26211
26212 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26213 if (glyph < it->glyph_row->glyphs[area + 1])
26214 {
26215 /* If the glyph row is reversed, we need to prepend the glyph
26216 rather than append it. */
26217 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26218 {
26219 struct glyph *g;
26220
26221 /* Make room for the additional glyph. */
26222 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26223 g[1] = *g;
26224 glyph = it->glyph_row->glyphs[area];
26225
26226 /* Decrease the width of the first glyph of the row that
26227 begins before first_visible_x (e.g., due to hscroll).
26228 This is so the overall width of the row becomes smaller
26229 by the scroll amount, and the stretch glyph appended by
26230 extend_face_to_end_of_line will be wider, to shift the
26231 row glyphs to the right. (In L2R rows, the corresponding
26232 left-shift effect is accomplished by setting row->x to a
26233 negative value, which won't work with R2L rows.)
26234
26235 This must leave us with a positive value of WIDTH, since
26236 otherwise the call to move_it_in_display_line_to at the
26237 beginning of display_line would have got past the entire
26238 first glyph, and then it->current_x would have been
26239 greater or equal to it->first_visible_x. */
26240 if (it->current_x < it->first_visible_x)
26241 width -= it->first_visible_x - it->current_x;
26242 eassert (width > 0);
26243 }
26244 glyph->charpos = CHARPOS (it->position);
26245 glyph->object = object;
26246 glyph->pixel_width = width;
26247 glyph->ascent = ascent;
26248 glyph->descent = height - ascent;
26249 glyph->voffset = it->voffset;
26250 glyph->type = STRETCH_GLYPH;
26251 glyph->avoid_cursor_p = it->avoid_cursor_p;
26252 glyph->multibyte_p = it->multibyte_p;
26253 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26254 {
26255 /* In R2L rows, the left and the right box edges need to be
26256 drawn in reverse direction. */
26257 glyph->right_box_line_p = it->start_of_box_run_p;
26258 glyph->left_box_line_p = it->end_of_box_run_p;
26259 }
26260 else
26261 {
26262 glyph->left_box_line_p = it->start_of_box_run_p;
26263 glyph->right_box_line_p = it->end_of_box_run_p;
26264 }
26265 glyph->overlaps_vertically_p = false;
26266 glyph->padding_p = false;
26267 glyph->glyph_not_available_p = false;
26268 glyph->face_id = it->face_id;
26269 glyph->u.stretch.ascent = ascent;
26270 glyph->u.stretch.height = height;
26271 glyph->slice.img = null_glyph_slice;
26272 glyph->font_type = FONT_TYPE_UNKNOWN;
26273 if (it->bidi_p)
26274 {
26275 glyph->resolved_level = it->bidi_it.resolved_level;
26276 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26277 glyph->bidi_type = it->bidi_it.type;
26278 }
26279 else
26280 {
26281 glyph->resolved_level = 0;
26282 glyph->bidi_type = UNKNOWN_BT;
26283 }
26284 ++it->glyph_row->used[area];
26285 }
26286 else
26287 IT_EXPAND_MATRIX_WIDTH (it, area);
26288 }
26289
26290 #endif /* HAVE_WINDOW_SYSTEM */
26291
26292 /* Produce a stretch glyph for iterator IT. IT->object is the value
26293 of the glyph property displayed. The value must be a list
26294 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
26295 being recognized:
26296
26297 1. `:width WIDTH' specifies that the space should be WIDTH *
26298 canonical char width wide. WIDTH may be an integer or floating
26299 point number.
26300
26301 2. `:relative-width FACTOR' specifies that the width of the stretch
26302 should be computed from the width of the first character having the
26303 `glyph' property, and should be FACTOR times that width.
26304
26305 3. `:align-to HPOS' specifies that the space should be wide enough
26306 to reach HPOS, a value in canonical character units.
26307
26308 Exactly one of the above pairs must be present.
26309
26310 4. `:height HEIGHT' specifies that the height of the stretch produced
26311 should be HEIGHT, measured in canonical character units.
26312
26313 5. `:relative-height FACTOR' specifies that the height of the
26314 stretch should be FACTOR times the height of the characters having
26315 the glyph property.
26316
26317 Either none or exactly one of 4 or 5 must be present.
26318
26319 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26320 of the stretch should be used for the ascent of the stretch.
26321 ASCENT must be in the range 0 <= ASCENT <= 100. */
26322
26323 void
26324 produce_stretch_glyph (struct it *it)
26325 {
26326 /* (space :width WIDTH :height HEIGHT ...) */
26327 Lisp_Object prop, plist;
26328 int width = 0, height = 0, align_to = -1;
26329 bool zero_width_ok_p = false;
26330 double tem;
26331 struct font *font = NULL;
26332
26333 #ifdef HAVE_WINDOW_SYSTEM
26334 int ascent = 0;
26335 bool zero_height_ok_p = false;
26336
26337 if (FRAME_WINDOW_P (it->f))
26338 {
26339 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26340 font = face->font ? face->font : FRAME_FONT (it->f);
26341 prepare_face_for_display (it->f, face);
26342 }
26343 #endif
26344
26345 /* List should start with `space'. */
26346 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26347 plist = XCDR (it->object);
26348
26349 /* Compute the width of the stretch. */
26350 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26351 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26352 {
26353 /* Absolute width `:width WIDTH' specified and valid. */
26354 zero_width_ok_p = true;
26355 width = (int)tem;
26356 }
26357 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26358 {
26359 /* Relative width `:relative-width FACTOR' specified and valid.
26360 Compute the width of the characters having the `glyph'
26361 property. */
26362 struct it it2;
26363 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26364
26365 it2 = *it;
26366 if (it->multibyte_p)
26367 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26368 else
26369 {
26370 it2.c = it2.char_to_display = *p, it2.len = 1;
26371 if (! ASCII_CHAR_P (it2.c))
26372 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26373 }
26374
26375 it2.glyph_row = NULL;
26376 it2.what = IT_CHARACTER;
26377 PRODUCE_GLYPHS (&it2);
26378 width = NUMVAL (prop) * it2.pixel_width;
26379 }
26380 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26381 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26382 &align_to))
26383 {
26384 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26385 align_to = (align_to < 0
26386 ? 0
26387 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26388 else if (align_to < 0)
26389 align_to = window_box_left_offset (it->w, TEXT_AREA);
26390 width = max (0, (int)tem + align_to - it->current_x);
26391 zero_width_ok_p = true;
26392 }
26393 else
26394 /* Nothing specified -> width defaults to canonical char width. */
26395 width = FRAME_COLUMN_WIDTH (it->f);
26396
26397 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26398 width = 1;
26399
26400 #ifdef HAVE_WINDOW_SYSTEM
26401 /* Compute height. */
26402 if (FRAME_WINDOW_P (it->f))
26403 {
26404 int default_height = normal_char_height (font, ' ');
26405
26406 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26407 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26408 {
26409 height = (int)tem;
26410 zero_height_ok_p = true;
26411 }
26412 else if (prop = Fplist_get (plist, QCrelative_height),
26413 NUMVAL (prop) > 0)
26414 height = default_height * NUMVAL (prop);
26415 else
26416 height = default_height;
26417
26418 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26419 height = 1;
26420
26421 /* Compute percentage of height used for ascent. If
26422 `:ascent ASCENT' is present and valid, use that. Otherwise,
26423 derive the ascent from the font in use. */
26424 if (prop = Fplist_get (plist, QCascent),
26425 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26426 ascent = height * NUMVAL (prop) / 100.0;
26427 else if (!NILP (prop)
26428 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26429 ascent = min (max (0, (int)tem), height);
26430 else
26431 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26432 }
26433 else
26434 #endif /* HAVE_WINDOW_SYSTEM */
26435 height = 1;
26436
26437 if (width > 0 && it->line_wrap != TRUNCATE
26438 && it->current_x + width > it->last_visible_x)
26439 {
26440 width = it->last_visible_x - it->current_x;
26441 #ifdef HAVE_WINDOW_SYSTEM
26442 /* Subtract one more pixel from the stretch width, but only on
26443 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26444 width -= FRAME_WINDOW_P (it->f);
26445 #endif
26446 }
26447
26448 if (width > 0 && height > 0 && it->glyph_row)
26449 {
26450 Lisp_Object o_object = it->object;
26451 Lisp_Object object = it->stack[it->sp - 1].string;
26452 int n = width;
26453
26454 if (!STRINGP (object))
26455 object = it->w->contents;
26456 #ifdef HAVE_WINDOW_SYSTEM
26457 if (FRAME_WINDOW_P (it->f))
26458 append_stretch_glyph (it, object, width, height, ascent);
26459 else
26460 #endif
26461 {
26462 it->object = object;
26463 it->char_to_display = ' ';
26464 it->pixel_width = it->len = 1;
26465 while (n--)
26466 tty_append_glyph (it);
26467 it->object = o_object;
26468 }
26469 }
26470
26471 it->pixel_width = width;
26472 #ifdef HAVE_WINDOW_SYSTEM
26473 if (FRAME_WINDOW_P (it->f))
26474 {
26475 it->ascent = it->phys_ascent = ascent;
26476 it->descent = it->phys_descent = height - it->ascent;
26477 it->nglyphs = width > 0 && height > 0;
26478 take_vertical_position_into_account (it);
26479 }
26480 else
26481 #endif
26482 it->nglyphs = width;
26483 }
26484
26485 /* Get information about special display element WHAT in an
26486 environment described by IT. WHAT is one of IT_TRUNCATION or
26487 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26488 non-null glyph_row member. This function ensures that fields like
26489 face_id, c, len of IT are left untouched. */
26490
26491 static void
26492 produce_special_glyphs (struct it *it, enum display_element_type what)
26493 {
26494 struct it temp_it;
26495 Lisp_Object gc;
26496 GLYPH glyph;
26497
26498 temp_it = *it;
26499 temp_it.object = Qnil;
26500 memset (&temp_it.current, 0, sizeof temp_it.current);
26501
26502 if (what == IT_CONTINUATION)
26503 {
26504 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26505 if (it->bidi_it.paragraph_dir == R2L)
26506 SET_GLYPH_FROM_CHAR (glyph, '/');
26507 else
26508 SET_GLYPH_FROM_CHAR (glyph, '\\');
26509 if (it->dp
26510 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26511 {
26512 /* FIXME: Should we mirror GC for R2L lines? */
26513 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26514 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26515 }
26516 }
26517 else if (what == IT_TRUNCATION)
26518 {
26519 /* Truncation glyph. */
26520 SET_GLYPH_FROM_CHAR (glyph, '$');
26521 if (it->dp
26522 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26523 {
26524 /* FIXME: Should we mirror GC for R2L lines? */
26525 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26526 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26527 }
26528 }
26529 else
26530 emacs_abort ();
26531
26532 #ifdef HAVE_WINDOW_SYSTEM
26533 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26534 is turned off, we precede the truncation/continuation glyphs by a
26535 stretch glyph whose width is computed such that these special
26536 glyphs are aligned at the window margin, even when very different
26537 fonts are used in different glyph rows. */
26538 if (FRAME_WINDOW_P (temp_it.f)
26539 /* init_iterator calls this with it->glyph_row == NULL, and it
26540 wants only the pixel width of the truncation/continuation
26541 glyphs. */
26542 && temp_it.glyph_row
26543 /* insert_left_trunc_glyphs calls us at the beginning of the
26544 row, and it has its own calculation of the stretch glyph
26545 width. */
26546 && temp_it.glyph_row->used[TEXT_AREA] > 0
26547 && (temp_it.glyph_row->reversed_p
26548 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26549 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26550 {
26551 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26552
26553 if (stretch_width > 0)
26554 {
26555 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26556 struct font *font =
26557 face->font ? face->font : FRAME_FONT (temp_it.f);
26558 int stretch_ascent =
26559 (((temp_it.ascent + temp_it.descent)
26560 * FONT_BASE (font)) / FONT_HEIGHT (font));
26561
26562 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26563 temp_it.ascent + temp_it.descent,
26564 stretch_ascent);
26565 }
26566 }
26567 #endif
26568
26569 temp_it.dp = NULL;
26570 temp_it.what = IT_CHARACTER;
26571 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26572 temp_it.face_id = GLYPH_FACE (glyph);
26573 temp_it.len = CHAR_BYTES (temp_it.c);
26574
26575 PRODUCE_GLYPHS (&temp_it);
26576 it->pixel_width = temp_it.pixel_width;
26577 it->nglyphs = temp_it.nglyphs;
26578 }
26579
26580 #ifdef HAVE_WINDOW_SYSTEM
26581
26582 /* Calculate line-height and line-spacing properties.
26583 An integer value specifies explicit pixel value.
26584 A float value specifies relative value to current face height.
26585 A cons (float . face-name) specifies relative value to
26586 height of specified face font.
26587
26588 Returns height in pixels, or nil. */
26589
26590 static Lisp_Object
26591 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26592 int boff, bool override)
26593 {
26594 Lisp_Object face_name = Qnil;
26595 int ascent, descent, height;
26596
26597 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26598 return val;
26599
26600 if (CONSP (val))
26601 {
26602 face_name = XCAR (val);
26603 val = XCDR (val);
26604 if (!NUMBERP (val))
26605 val = make_number (1);
26606 if (NILP (face_name))
26607 {
26608 height = it->ascent + it->descent;
26609 goto scale;
26610 }
26611 }
26612
26613 if (NILP (face_name))
26614 {
26615 font = FRAME_FONT (it->f);
26616 boff = FRAME_BASELINE_OFFSET (it->f);
26617 }
26618 else if (EQ (face_name, Qt))
26619 {
26620 override = false;
26621 }
26622 else
26623 {
26624 int face_id;
26625 struct face *face;
26626
26627 face_id = lookup_named_face (it->f, face_name, false);
26628 if (face_id < 0)
26629 return make_number (-1);
26630
26631 face = FACE_FROM_ID (it->f, face_id);
26632 font = face->font;
26633 if (font == NULL)
26634 return make_number (-1);
26635 boff = font->baseline_offset;
26636 if (font->vertical_centering)
26637 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26638 }
26639
26640 normal_char_ascent_descent (font, -1, &ascent, &descent);
26641
26642 if (override)
26643 {
26644 it->override_ascent = ascent;
26645 it->override_descent = descent;
26646 it->override_boff = boff;
26647 }
26648
26649 height = ascent + descent;
26650
26651 scale:
26652 if (FLOATP (val))
26653 height = (int)(XFLOAT_DATA (val) * height);
26654 else if (INTEGERP (val))
26655 height *= XINT (val);
26656
26657 return make_number (height);
26658 }
26659
26660
26661 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26662 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26663 and only if this is for a character for which no font was found.
26664
26665 If the display method (it->glyphless_method) is
26666 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26667 length of the acronym or the hexadecimal string, UPPER_XOFF and
26668 UPPER_YOFF are pixel offsets for the upper part of the string,
26669 LOWER_XOFF and LOWER_YOFF are for the lower part.
26670
26671 For the other display methods, LEN through LOWER_YOFF are zero. */
26672
26673 static void
26674 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26675 short upper_xoff, short upper_yoff,
26676 short lower_xoff, short lower_yoff)
26677 {
26678 struct glyph *glyph;
26679 enum glyph_row_area area = it->area;
26680
26681 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26682 if (glyph < it->glyph_row->glyphs[area + 1])
26683 {
26684 /* If the glyph row is reversed, we need to prepend the glyph
26685 rather than append it. */
26686 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26687 {
26688 struct glyph *g;
26689
26690 /* Make room for the additional glyph. */
26691 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26692 g[1] = *g;
26693 glyph = it->glyph_row->glyphs[area];
26694 }
26695 glyph->charpos = CHARPOS (it->position);
26696 glyph->object = it->object;
26697 glyph->pixel_width = it->pixel_width;
26698 glyph->ascent = it->ascent;
26699 glyph->descent = it->descent;
26700 glyph->voffset = it->voffset;
26701 glyph->type = GLYPHLESS_GLYPH;
26702 glyph->u.glyphless.method = it->glyphless_method;
26703 glyph->u.glyphless.for_no_font = for_no_font;
26704 glyph->u.glyphless.len = len;
26705 glyph->u.glyphless.ch = it->c;
26706 glyph->slice.glyphless.upper_xoff = upper_xoff;
26707 glyph->slice.glyphless.upper_yoff = upper_yoff;
26708 glyph->slice.glyphless.lower_xoff = lower_xoff;
26709 glyph->slice.glyphless.lower_yoff = lower_yoff;
26710 glyph->avoid_cursor_p = it->avoid_cursor_p;
26711 glyph->multibyte_p = it->multibyte_p;
26712 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26713 {
26714 /* In R2L rows, the left and the right box edges need to be
26715 drawn in reverse direction. */
26716 glyph->right_box_line_p = it->start_of_box_run_p;
26717 glyph->left_box_line_p = it->end_of_box_run_p;
26718 }
26719 else
26720 {
26721 glyph->left_box_line_p = it->start_of_box_run_p;
26722 glyph->right_box_line_p = it->end_of_box_run_p;
26723 }
26724 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26725 || it->phys_descent > it->descent);
26726 glyph->padding_p = false;
26727 glyph->glyph_not_available_p = false;
26728 glyph->face_id = face_id;
26729 glyph->font_type = FONT_TYPE_UNKNOWN;
26730 if (it->bidi_p)
26731 {
26732 glyph->resolved_level = it->bidi_it.resolved_level;
26733 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26734 glyph->bidi_type = it->bidi_it.type;
26735 }
26736 ++it->glyph_row->used[area];
26737 }
26738 else
26739 IT_EXPAND_MATRIX_WIDTH (it, area);
26740 }
26741
26742
26743 /* Produce a glyph for a glyphless character for iterator IT.
26744 IT->glyphless_method specifies which method to use for displaying
26745 the character. See the description of enum
26746 glyphless_display_method in dispextern.h for the detail.
26747
26748 FOR_NO_FONT is true if and only if this is for a character for
26749 which no font was found. ACRONYM, if non-nil, is an acronym string
26750 for the character. */
26751
26752 static void
26753 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26754 {
26755 int face_id;
26756 struct face *face;
26757 struct font *font;
26758 int base_width, base_height, width, height;
26759 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26760 int len;
26761
26762 /* Get the metrics of the base font. We always refer to the current
26763 ASCII face. */
26764 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26765 font = face->font ? face->font : FRAME_FONT (it->f);
26766 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
26767 it->ascent += font->baseline_offset;
26768 it->descent -= font->baseline_offset;
26769 base_height = it->ascent + it->descent;
26770 base_width = font->average_width;
26771
26772 face_id = merge_glyphless_glyph_face (it);
26773
26774 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26775 {
26776 it->pixel_width = THIN_SPACE_WIDTH;
26777 len = 0;
26778 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26779 }
26780 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26781 {
26782 width = CHAR_WIDTH (it->c);
26783 if (width == 0)
26784 width = 1;
26785 else if (width > 4)
26786 width = 4;
26787 it->pixel_width = base_width * width;
26788 len = 0;
26789 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26790 }
26791 else
26792 {
26793 char buf[7];
26794 const char *str;
26795 unsigned int code[6];
26796 int upper_len;
26797 int ascent, descent;
26798 struct font_metrics metrics_upper, metrics_lower;
26799
26800 face = FACE_FROM_ID (it->f, face_id);
26801 font = face->font ? face->font : FRAME_FONT (it->f);
26802 prepare_face_for_display (it->f, face);
26803
26804 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26805 {
26806 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26807 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26808 if (CONSP (acronym))
26809 acronym = XCAR (acronym);
26810 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26811 }
26812 else
26813 {
26814 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26815 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
26816 str = buf;
26817 }
26818 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26819 code[len] = font->driver->encode_char (font, str[len]);
26820 upper_len = (len + 1) / 2;
26821 font->driver->text_extents (font, code, upper_len,
26822 &metrics_upper);
26823 font->driver->text_extents (font, code + upper_len, len - upper_len,
26824 &metrics_lower);
26825
26826
26827
26828 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26829 width = max (metrics_upper.width, metrics_lower.width) + 4;
26830 upper_xoff = upper_yoff = 2; /* the typical case */
26831 if (base_width >= width)
26832 {
26833 /* Align the upper to the left, the lower to the right. */
26834 it->pixel_width = base_width;
26835 lower_xoff = base_width - 2 - metrics_lower.width;
26836 }
26837 else
26838 {
26839 /* Center the shorter one. */
26840 it->pixel_width = width;
26841 if (metrics_upper.width >= metrics_lower.width)
26842 lower_xoff = (width - metrics_lower.width) / 2;
26843 else
26844 {
26845 /* FIXME: This code doesn't look right. It formerly was
26846 missing the "lower_xoff = 0;", which couldn't have
26847 been right since it left lower_xoff uninitialized. */
26848 lower_xoff = 0;
26849 upper_xoff = (width - metrics_upper.width) / 2;
26850 }
26851 }
26852
26853 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26854 top, bottom, and between upper and lower strings. */
26855 height = (metrics_upper.ascent + metrics_upper.descent
26856 + metrics_lower.ascent + metrics_lower.descent) + 5;
26857 /* Center vertically.
26858 H:base_height, D:base_descent
26859 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26860
26861 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26862 descent = D - H/2 + h/2;
26863 lower_yoff = descent - 2 - ld;
26864 upper_yoff = lower_yoff - la - 1 - ud; */
26865 ascent = - (it->descent - (base_height + height + 1) / 2);
26866 descent = it->descent - (base_height - height) / 2;
26867 lower_yoff = descent - 2 - metrics_lower.descent;
26868 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26869 - metrics_upper.descent);
26870 /* Don't make the height shorter than the base height. */
26871 if (height > base_height)
26872 {
26873 it->ascent = ascent;
26874 it->descent = descent;
26875 }
26876 }
26877
26878 it->phys_ascent = it->ascent;
26879 it->phys_descent = it->descent;
26880 if (it->glyph_row)
26881 append_glyphless_glyph (it, face_id, for_no_font, len,
26882 upper_xoff, upper_yoff,
26883 lower_xoff, lower_yoff);
26884 it->nglyphs = 1;
26885 take_vertical_position_into_account (it);
26886 }
26887
26888
26889 /* RIF:
26890 Produce glyphs/get display metrics for the display element IT is
26891 loaded with. See the description of struct it in dispextern.h
26892 for an overview of struct it. */
26893
26894 void
26895 x_produce_glyphs (struct it *it)
26896 {
26897 int extra_line_spacing = it->extra_line_spacing;
26898
26899 it->glyph_not_available_p = false;
26900
26901 if (it->what == IT_CHARACTER)
26902 {
26903 XChar2b char2b;
26904 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26905 struct font *font = face->font;
26906 struct font_metrics *pcm = NULL;
26907 int boff; /* Baseline offset. */
26908
26909 if (font == NULL)
26910 {
26911 /* When no suitable font is found, display this character by
26912 the method specified in the first extra slot of
26913 Vglyphless_char_display. */
26914 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26915
26916 eassert (it->what == IT_GLYPHLESS);
26917 produce_glyphless_glyph (it, true,
26918 STRINGP (acronym) ? acronym : Qnil);
26919 goto done;
26920 }
26921
26922 boff = font->baseline_offset;
26923 if (font->vertical_centering)
26924 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26925
26926 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26927 {
26928 it->nglyphs = 1;
26929
26930 if (it->override_ascent >= 0)
26931 {
26932 it->ascent = it->override_ascent;
26933 it->descent = it->override_descent;
26934 boff = it->override_boff;
26935 }
26936 else
26937 {
26938 it->ascent = FONT_BASE (font) + boff;
26939 it->descent = FONT_DESCENT (font) - boff;
26940 }
26941
26942 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26943 {
26944 pcm = get_per_char_metric (font, &char2b);
26945 if (pcm->width == 0
26946 && pcm->rbearing == 0 && pcm->lbearing == 0)
26947 pcm = NULL;
26948 }
26949
26950 if (pcm)
26951 {
26952 it->phys_ascent = pcm->ascent + boff;
26953 it->phys_descent = pcm->descent - boff;
26954 it->pixel_width = pcm->width;
26955 /* Don't use font-global values for ascent and descent
26956 if they result in an exceedingly large line height. */
26957 if (it->override_ascent < 0)
26958 {
26959 if (FONT_TOO_HIGH (font))
26960 {
26961 it->ascent = it->phys_ascent;
26962 it->descent = it->phys_descent;
26963 /* These limitations are enforced by an
26964 assertion near the end of this function. */
26965 if (it->ascent < 0)
26966 it->ascent = 0;
26967 if (it->descent < 0)
26968 it->descent = 0;
26969 }
26970 }
26971 }
26972 else
26973 {
26974 it->glyph_not_available_p = true;
26975 it->phys_ascent = it->ascent;
26976 it->phys_descent = it->descent;
26977 it->pixel_width = font->space_width;
26978 }
26979
26980 if (it->constrain_row_ascent_descent_p)
26981 {
26982 if (it->descent > it->max_descent)
26983 {
26984 it->ascent += it->descent - it->max_descent;
26985 it->descent = it->max_descent;
26986 }
26987 if (it->ascent > it->max_ascent)
26988 {
26989 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26990 it->ascent = it->max_ascent;
26991 }
26992 it->phys_ascent = min (it->phys_ascent, it->ascent);
26993 it->phys_descent = min (it->phys_descent, it->descent);
26994 extra_line_spacing = 0;
26995 }
26996
26997 /* If this is a space inside a region of text with
26998 `space-width' property, change its width. */
26999 bool stretched_p
27000 = it->char_to_display == ' ' && !NILP (it->space_width);
27001 if (stretched_p)
27002 it->pixel_width *= XFLOATINT (it->space_width);
27003
27004 /* If face has a box, add the box thickness to the character
27005 height. If character has a box line to the left and/or
27006 right, add the box line width to the character's width. */
27007 if (face->box != FACE_NO_BOX)
27008 {
27009 int thick = face->box_line_width;
27010
27011 if (thick > 0)
27012 {
27013 it->ascent += thick;
27014 it->descent += thick;
27015 }
27016 else
27017 thick = -thick;
27018
27019 if (it->start_of_box_run_p)
27020 it->pixel_width += thick;
27021 if (it->end_of_box_run_p)
27022 it->pixel_width += thick;
27023 }
27024
27025 /* If face has an overline, add the height of the overline
27026 (1 pixel) and a 1 pixel margin to the character height. */
27027 if (face->overline_p)
27028 it->ascent += overline_margin;
27029
27030 if (it->constrain_row_ascent_descent_p)
27031 {
27032 if (it->ascent > it->max_ascent)
27033 it->ascent = it->max_ascent;
27034 if (it->descent > it->max_descent)
27035 it->descent = it->max_descent;
27036 }
27037
27038 take_vertical_position_into_account (it);
27039
27040 /* If we have to actually produce glyphs, do it. */
27041 if (it->glyph_row)
27042 {
27043 if (stretched_p)
27044 {
27045 /* Translate a space with a `space-width' property
27046 into a stretch glyph. */
27047 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
27048 / FONT_HEIGHT (font));
27049 append_stretch_glyph (it, it->object, it->pixel_width,
27050 it->ascent + it->descent, ascent);
27051 }
27052 else
27053 append_glyph (it);
27054
27055 /* If characters with lbearing or rbearing are displayed
27056 in this line, record that fact in a flag of the
27057 glyph row. This is used to optimize X output code. */
27058 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
27059 it->glyph_row->contains_overlapping_glyphs_p = true;
27060 }
27061 if (! stretched_p && it->pixel_width == 0)
27062 /* We assure that all visible glyphs have at least 1-pixel
27063 width. */
27064 it->pixel_width = 1;
27065 }
27066 else if (it->char_to_display == '\n')
27067 {
27068 /* A newline has no width, but we need the height of the
27069 line. But if previous part of the line sets a height,
27070 don't increase that height. */
27071
27072 Lisp_Object height;
27073 Lisp_Object total_height = Qnil;
27074
27075 it->override_ascent = -1;
27076 it->pixel_width = 0;
27077 it->nglyphs = 0;
27078
27079 height = get_it_property (it, Qline_height);
27080 /* Split (line-height total-height) list. */
27081 if (CONSP (height)
27082 && CONSP (XCDR (height))
27083 && NILP (XCDR (XCDR (height))))
27084 {
27085 total_height = XCAR (XCDR (height));
27086 height = XCAR (height);
27087 }
27088 height = calc_line_height_property (it, height, font, boff, true);
27089
27090 if (it->override_ascent >= 0)
27091 {
27092 it->ascent = it->override_ascent;
27093 it->descent = it->override_descent;
27094 boff = it->override_boff;
27095 }
27096 else
27097 {
27098 if (FONT_TOO_HIGH (font))
27099 {
27100 it->ascent = font->pixel_size + boff - 1;
27101 it->descent = -boff + 1;
27102 if (it->descent < 0)
27103 it->descent = 0;
27104 }
27105 else
27106 {
27107 it->ascent = FONT_BASE (font) + boff;
27108 it->descent = FONT_DESCENT (font) - boff;
27109 }
27110 }
27111
27112 if (EQ (height, Qt))
27113 {
27114 if (it->descent > it->max_descent)
27115 {
27116 it->ascent += it->descent - it->max_descent;
27117 it->descent = it->max_descent;
27118 }
27119 if (it->ascent > it->max_ascent)
27120 {
27121 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27122 it->ascent = it->max_ascent;
27123 }
27124 it->phys_ascent = min (it->phys_ascent, it->ascent);
27125 it->phys_descent = min (it->phys_descent, it->descent);
27126 it->constrain_row_ascent_descent_p = true;
27127 extra_line_spacing = 0;
27128 }
27129 else
27130 {
27131 Lisp_Object spacing;
27132
27133 it->phys_ascent = it->ascent;
27134 it->phys_descent = it->descent;
27135
27136 if ((it->max_ascent > 0 || it->max_descent > 0)
27137 && face->box != FACE_NO_BOX
27138 && face->box_line_width > 0)
27139 {
27140 it->ascent += face->box_line_width;
27141 it->descent += face->box_line_width;
27142 }
27143 if (!NILP (height)
27144 && XINT (height) > it->ascent + it->descent)
27145 it->ascent = XINT (height) - it->descent;
27146
27147 if (!NILP (total_height))
27148 spacing = calc_line_height_property (it, total_height, font,
27149 boff, false);
27150 else
27151 {
27152 spacing = get_it_property (it, Qline_spacing);
27153 spacing = calc_line_height_property (it, spacing, font,
27154 boff, false);
27155 }
27156 if (INTEGERP (spacing))
27157 {
27158 extra_line_spacing = XINT (spacing);
27159 if (!NILP (total_height))
27160 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
27161 }
27162 }
27163 }
27164 else /* i.e. (it->char_to_display == '\t') */
27165 {
27166 if (font->space_width > 0)
27167 {
27168 int tab_width = it->tab_width * font->space_width;
27169 int x = it->current_x + it->continuation_lines_width;
27170 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
27171
27172 /* If the distance from the current position to the next tab
27173 stop is less than a space character width, use the
27174 tab stop after that. */
27175 if (next_tab_x - x < font->space_width)
27176 next_tab_x += tab_width;
27177
27178 it->pixel_width = next_tab_x - x;
27179 it->nglyphs = 1;
27180 if (FONT_TOO_HIGH (font))
27181 {
27182 if (get_char_glyph_code (' ', font, &char2b))
27183 {
27184 pcm = get_per_char_metric (font, &char2b);
27185 if (pcm->width == 0
27186 && pcm->rbearing == 0 && pcm->lbearing == 0)
27187 pcm = NULL;
27188 }
27189
27190 if (pcm)
27191 {
27192 it->ascent = pcm->ascent + boff;
27193 it->descent = pcm->descent - boff;
27194 }
27195 else
27196 {
27197 it->ascent = font->pixel_size + boff - 1;
27198 it->descent = -boff + 1;
27199 }
27200 if (it->ascent < 0)
27201 it->ascent = 0;
27202 if (it->descent < 0)
27203 it->descent = 0;
27204 }
27205 else
27206 {
27207 it->ascent = FONT_BASE (font) + boff;
27208 it->descent = FONT_DESCENT (font) - boff;
27209 }
27210 it->phys_ascent = it->ascent;
27211 it->phys_descent = it->descent;
27212
27213 if (it->glyph_row)
27214 {
27215 append_stretch_glyph (it, it->object, it->pixel_width,
27216 it->ascent + it->descent, it->ascent);
27217 }
27218 }
27219 else
27220 {
27221 it->pixel_width = 0;
27222 it->nglyphs = 1;
27223 }
27224 }
27225
27226 if (FONT_TOO_HIGH (font))
27227 {
27228 int font_ascent, font_descent;
27229
27230 /* For very large fonts, where we ignore the declared font
27231 dimensions, and go by per-character metrics instead,
27232 don't let the row ascent and descent values (and the row
27233 height computed from them) be smaller than the "normal"
27234 character metrics. This avoids unpleasant effects
27235 whereby lines on display would change their height
27236 depending on which characters are shown. */
27237 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27238 it->max_ascent = max (it->max_ascent, font_ascent);
27239 it->max_descent = max (it->max_descent, font_descent);
27240 }
27241 }
27242 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
27243 {
27244 /* A static composition.
27245
27246 Note: A composition is represented as one glyph in the
27247 glyph matrix. There are no padding glyphs.
27248
27249 Important note: pixel_width, ascent, and descent are the
27250 values of what is drawn by draw_glyphs (i.e. the values of
27251 the overall glyphs composed). */
27252 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27253 int boff; /* baseline offset */
27254 struct composition *cmp = composition_table[it->cmp_it.id];
27255 int glyph_len = cmp->glyph_len;
27256 struct font *font = face->font;
27257
27258 it->nglyphs = 1;
27259
27260 /* If we have not yet calculated pixel size data of glyphs of
27261 the composition for the current face font, calculate them
27262 now. Theoretically, we have to check all fonts for the
27263 glyphs, but that requires much time and memory space. So,
27264 here we check only the font of the first glyph. This may
27265 lead to incorrect display, but it's very rare, and C-l
27266 (recenter-top-bottom) can correct the display anyway. */
27267 if (! cmp->font || cmp->font != font)
27268 {
27269 /* Ascent and descent of the font of the first character
27270 of this composition (adjusted by baseline offset).
27271 Ascent and descent of overall glyphs should not be less
27272 than these, respectively. */
27273 int font_ascent, font_descent, font_height;
27274 /* Bounding box of the overall glyphs. */
27275 int leftmost, rightmost, lowest, highest;
27276 int lbearing, rbearing;
27277 int i, width, ascent, descent;
27278 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
27279 XChar2b char2b;
27280 struct font_metrics *pcm;
27281 ptrdiff_t pos;
27282
27283 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
27284 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
27285 break;
27286 bool right_padded = glyph_len < cmp->glyph_len;
27287 for (i = 0; i < glyph_len; i++)
27288 {
27289 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
27290 break;
27291 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27292 }
27293 bool left_padded = i > 0;
27294
27295 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
27296 : IT_CHARPOS (*it));
27297 /* If no suitable font is found, use the default font. */
27298 bool font_not_found_p = font == NULL;
27299 if (font_not_found_p)
27300 {
27301 face = face->ascii_face;
27302 font = face->font;
27303 }
27304 boff = font->baseline_offset;
27305 if (font->vertical_centering)
27306 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27307 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27308 font_ascent += boff;
27309 font_descent -= boff;
27310 font_height = font_ascent + font_descent;
27311
27312 cmp->font = font;
27313
27314 pcm = NULL;
27315 if (! font_not_found_p)
27316 {
27317 get_char_face_and_encoding (it->f, c, it->face_id,
27318 &char2b, false);
27319 pcm = get_per_char_metric (font, &char2b);
27320 }
27321
27322 /* Initialize the bounding box. */
27323 if (pcm)
27324 {
27325 width = cmp->glyph_len > 0 ? pcm->width : 0;
27326 ascent = pcm->ascent;
27327 descent = pcm->descent;
27328 lbearing = pcm->lbearing;
27329 rbearing = pcm->rbearing;
27330 }
27331 else
27332 {
27333 width = cmp->glyph_len > 0 ? font->space_width : 0;
27334 ascent = FONT_BASE (font);
27335 descent = FONT_DESCENT (font);
27336 lbearing = 0;
27337 rbearing = width;
27338 }
27339
27340 rightmost = width;
27341 leftmost = 0;
27342 lowest = - descent + boff;
27343 highest = ascent + boff;
27344
27345 if (! font_not_found_p
27346 && font->default_ascent
27347 && CHAR_TABLE_P (Vuse_default_ascent)
27348 && !NILP (Faref (Vuse_default_ascent,
27349 make_number (it->char_to_display))))
27350 highest = font->default_ascent + boff;
27351
27352 /* Draw the first glyph at the normal position. It may be
27353 shifted to right later if some other glyphs are drawn
27354 at the left. */
27355 cmp->offsets[i * 2] = 0;
27356 cmp->offsets[i * 2 + 1] = boff;
27357 cmp->lbearing = lbearing;
27358 cmp->rbearing = rbearing;
27359
27360 /* Set cmp->offsets for the remaining glyphs. */
27361 for (i++; i < glyph_len; i++)
27362 {
27363 int left, right, btm, top;
27364 int ch = COMPOSITION_GLYPH (cmp, i);
27365 int face_id;
27366 struct face *this_face;
27367
27368 if (ch == '\t')
27369 ch = ' ';
27370 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27371 this_face = FACE_FROM_ID (it->f, face_id);
27372 font = this_face->font;
27373
27374 if (font == NULL)
27375 pcm = NULL;
27376 else
27377 {
27378 get_char_face_and_encoding (it->f, ch, face_id,
27379 &char2b, false);
27380 pcm = get_per_char_metric (font, &char2b);
27381 }
27382 if (! pcm)
27383 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27384 else
27385 {
27386 width = pcm->width;
27387 ascent = pcm->ascent;
27388 descent = pcm->descent;
27389 lbearing = pcm->lbearing;
27390 rbearing = pcm->rbearing;
27391 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27392 {
27393 /* Relative composition with or without
27394 alternate chars. */
27395 left = (leftmost + rightmost - width) / 2;
27396 btm = - descent + boff;
27397 if (font->relative_compose
27398 && (! CHAR_TABLE_P (Vignore_relative_composition)
27399 || NILP (Faref (Vignore_relative_composition,
27400 make_number (ch)))))
27401 {
27402
27403 if (- descent >= font->relative_compose)
27404 /* One extra pixel between two glyphs. */
27405 btm = highest + 1;
27406 else if (ascent <= 0)
27407 /* One extra pixel between two glyphs. */
27408 btm = lowest - 1 - ascent - descent;
27409 }
27410 }
27411 else
27412 {
27413 /* A composition rule is specified by an integer
27414 value that encodes global and new reference
27415 points (GREF and NREF). GREF and NREF are
27416 specified by numbers as below:
27417
27418 0---1---2 -- ascent
27419 | |
27420 | |
27421 | |
27422 9--10--11 -- center
27423 | |
27424 ---3---4---5--- baseline
27425 | |
27426 6---7---8 -- descent
27427 */
27428 int rule = COMPOSITION_RULE (cmp, i);
27429 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27430
27431 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27432 grefx = gref % 3, nrefx = nref % 3;
27433 grefy = gref / 3, nrefy = nref / 3;
27434 if (xoff)
27435 xoff = font_height * (xoff - 128) / 256;
27436 if (yoff)
27437 yoff = font_height * (yoff - 128) / 256;
27438
27439 left = (leftmost
27440 + grefx * (rightmost - leftmost) / 2
27441 - nrefx * width / 2
27442 + xoff);
27443
27444 btm = ((grefy == 0 ? highest
27445 : grefy == 1 ? 0
27446 : grefy == 2 ? lowest
27447 : (highest + lowest) / 2)
27448 - (nrefy == 0 ? ascent + descent
27449 : nrefy == 1 ? descent - boff
27450 : nrefy == 2 ? 0
27451 : (ascent + descent) / 2)
27452 + yoff);
27453 }
27454
27455 cmp->offsets[i * 2] = left;
27456 cmp->offsets[i * 2 + 1] = btm + descent;
27457
27458 /* Update the bounding box of the overall glyphs. */
27459 if (width > 0)
27460 {
27461 right = left + width;
27462 if (left < leftmost)
27463 leftmost = left;
27464 if (right > rightmost)
27465 rightmost = right;
27466 }
27467 top = btm + descent + ascent;
27468 if (top > highest)
27469 highest = top;
27470 if (btm < lowest)
27471 lowest = btm;
27472
27473 if (cmp->lbearing > left + lbearing)
27474 cmp->lbearing = left + lbearing;
27475 if (cmp->rbearing < left + rbearing)
27476 cmp->rbearing = left + rbearing;
27477 }
27478 }
27479
27480 /* If there are glyphs whose x-offsets are negative,
27481 shift all glyphs to the right and make all x-offsets
27482 non-negative. */
27483 if (leftmost < 0)
27484 {
27485 for (i = 0; i < cmp->glyph_len; i++)
27486 cmp->offsets[i * 2] -= leftmost;
27487 rightmost -= leftmost;
27488 cmp->lbearing -= leftmost;
27489 cmp->rbearing -= leftmost;
27490 }
27491
27492 if (left_padded && cmp->lbearing < 0)
27493 {
27494 for (i = 0; i < cmp->glyph_len; i++)
27495 cmp->offsets[i * 2] -= cmp->lbearing;
27496 rightmost -= cmp->lbearing;
27497 cmp->rbearing -= cmp->lbearing;
27498 cmp->lbearing = 0;
27499 }
27500 if (right_padded && rightmost < cmp->rbearing)
27501 {
27502 rightmost = cmp->rbearing;
27503 }
27504
27505 cmp->pixel_width = rightmost;
27506 cmp->ascent = highest;
27507 cmp->descent = - lowest;
27508 if (cmp->ascent < font_ascent)
27509 cmp->ascent = font_ascent;
27510 if (cmp->descent < font_descent)
27511 cmp->descent = font_descent;
27512 }
27513
27514 if (it->glyph_row
27515 && (cmp->lbearing < 0
27516 || cmp->rbearing > cmp->pixel_width))
27517 it->glyph_row->contains_overlapping_glyphs_p = true;
27518
27519 it->pixel_width = cmp->pixel_width;
27520 it->ascent = it->phys_ascent = cmp->ascent;
27521 it->descent = it->phys_descent = cmp->descent;
27522 if (face->box != FACE_NO_BOX)
27523 {
27524 int thick = face->box_line_width;
27525
27526 if (thick > 0)
27527 {
27528 it->ascent += thick;
27529 it->descent += thick;
27530 }
27531 else
27532 thick = - thick;
27533
27534 if (it->start_of_box_run_p)
27535 it->pixel_width += thick;
27536 if (it->end_of_box_run_p)
27537 it->pixel_width += thick;
27538 }
27539
27540 /* If face has an overline, add the height of the overline
27541 (1 pixel) and a 1 pixel margin to the character height. */
27542 if (face->overline_p)
27543 it->ascent += overline_margin;
27544
27545 take_vertical_position_into_account (it);
27546 if (it->ascent < 0)
27547 it->ascent = 0;
27548 if (it->descent < 0)
27549 it->descent = 0;
27550
27551 if (it->glyph_row && cmp->glyph_len > 0)
27552 append_composite_glyph (it);
27553 }
27554 else if (it->what == IT_COMPOSITION)
27555 {
27556 /* A dynamic (automatic) composition. */
27557 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27558 Lisp_Object gstring;
27559 struct font_metrics metrics;
27560
27561 it->nglyphs = 1;
27562
27563 gstring = composition_gstring_from_id (it->cmp_it.id);
27564 it->pixel_width
27565 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27566 &metrics);
27567 if (it->glyph_row
27568 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27569 it->glyph_row->contains_overlapping_glyphs_p = true;
27570 it->ascent = it->phys_ascent = metrics.ascent;
27571 it->descent = it->phys_descent = metrics.descent;
27572 if (face->box != FACE_NO_BOX)
27573 {
27574 int thick = face->box_line_width;
27575
27576 if (thick > 0)
27577 {
27578 it->ascent += thick;
27579 it->descent += thick;
27580 }
27581 else
27582 thick = - thick;
27583
27584 if (it->start_of_box_run_p)
27585 it->pixel_width += thick;
27586 if (it->end_of_box_run_p)
27587 it->pixel_width += thick;
27588 }
27589 /* If face has an overline, add the height of the overline
27590 (1 pixel) and a 1 pixel margin to the character height. */
27591 if (face->overline_p)
27592 it->ascent += overline_margin;
27593 take_vertical_position_into_account (it);
27594 if (it->ascent < 0)
27595 it->ascent = 0;
27596 if (it->descent < 0)
27597 it->descent = 0;
27598
27599 if (it->glyph_row)
27600 append_composite_glyph (it);
27601 }
27602 else if (it->what == IT_GLYPHLESS)
27603 produce_glyphless_glyph (it, false, Qnil);
27604 else if (it->what == IT_IMAGE)
27605 produce_image_glyph (it);
27606 else if (it->what == IT_STRETCH)
27607 produce_stretch_glyph (it);
27608 else if (it->what == IT_XWIDGET)
27609 produce_xwidget_glyph (it);
27610
27611 done:
27612 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27613 because this isn't true for images with `:ascent 100'. */
27614 eassert (it->ascent >= 0 && it->descent >= 0);
27615 if (it->area == TEXT_AREA)
27616 it->current_x += it->pixel_width;
27617
27618 if (extra_line_spacing > 0)
27619 {
27620 it->descent += extra_line_spacing;
27621 if (extra_line_spacing > it->max_extra_line_spacing)
27622 it->max_extra_line_spacing = extra_line_spacing;
27623 }
27624
27625 it->max_ascent = max (it->max_ascent, it->ascent);
27626 it->max_descent = max (it->max_descent, it->descent);
27627 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27628 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27629 }
27630
27631 /* EXPORT for RIF:
27632 Output LEN glyphs starting at START at the nominal cursor position.
27633 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27634 being updated, and UPDATED_AREA is the area of that row being updated. */
27635
27636 void
27637 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27638 struct glyph *start, enum glyph_row_area updated_area, int len)
27639 {
27640 int x, hpos, chpos = w->phys_cursor.hpos;
27641
27642 eassert (updated_row);
27643 /* When the window is hscrolled, cursor hpos can legitimately be out
27644 of bounds, but we draw the cursor at the corresponding window
27645 margin in that case. */
27646 if (!updated_row->reversed_p && chpos < 0)
27647 chpos = 0;
27648 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27649 chpos = updated_row->used[TEXT_AREA] - 1;
27650
27651 block_input ();
27652
27653 /* Write glyphs. */
27654
27655 hpos = start - updated_row->glyphs[updated_area];
27656 x = draw_glyphs (w, w->output_cursor.x,
27657 updated_row, updated_area,
27658 hpos, hpos + len,
27659 DRAW_NORMAL_TEXT, 0);
27660
27661 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27662 if (updated_area == TEXT_AREA
27663 && w->phys_cursor_on_p
27664 && w->phys_cursor.vpos == w->output_cursor.vpos
27665 && chpos >= hpos
27666 && chpos < hpos + len)
27667 w->phys_cursor_on_p = false;
27668
27669 unblock_input ();
27670
27671 /* Advance the output cursor. */
27672 w->output_cursor.hpos += len;
27673 w->output_cursor.x = x;
27674 }
27675
27676
27677 /* EXPORT for RIF:
27678 Insert LEN glyphs from START at the nominal cursor position. */
27679
27680 void
27681 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27682 struct glyph *start, enum glyph_row_area updated_area, int len)
27683 {
27684 struct frame *f;
27685 int line_height, shift_by_width, shifted_region_width;
27686 struct glyph_row *row;
27687 struct glyph *glyph;
27688 int frame_x, frame_y;
27689 ptrdiff_t hpos;
27690
27691 eassert (updated_row);
27692 block_input ();
27693 f = XFRAME (WINDOW_FRAME (w));
27694
27695 /* Get the height of the line we are in. */
27696 row = updated_row;
27697 line_height = row->height;
27698
27699 /* Get the width of the glyphs to insert. */
27700 shift_by_width = 0;
27701 for (glyph = start; glyph < start + len; ++glyph)
27702 shift_by_width += glyph->pixel_width;
27703
27704 /* Get the width of the region to shift right. */
27705 shifted_region_width = (window_box_width (w, updated_area)
27706 - w->output_cursor.x
27707 - shift_by_width);
27708
27709 /* Shift right. */
27710 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27711 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27712
27713 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27714 line_height, shift_by_width);
27715
27716 /* Write the glyphs. */
27717 hpos = start - row->glyphs[updated_area];
27718 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27719 hpos, hpos + len,
27720 DRAW_NORMAL_TEXT, 0);
27721
27722 /* Advance the output cursor. */
27723 w->output_cursor.hpos += len;
27724 w->output_cursor.x += shift_by_width;
27725 unblock_input ();
27726 }
27727
27728
27729 /* EXPORT for RIF:
27730 Erase the current text line from the nominal cursor position
27731 (inclusive) to pixel column TO_X (exclusive). The idea is that
27732 everything from TO_X onward is already erased.
27733
27734 TO_X is a pixel position relative to UPDATED_AREA of currently
27735 updated window W. TO_X == -1 means clear to the end of this area. */
27736
27737 void
27738 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27739 enum glyph_row_area updated_area, int to_x)
27740 {
27741 struct frame *f;
27742 int max_x, min_y, max_y;
27743 int from_x, from_y, to_y;
27744
27745 eassert (updated_row);
27746 f = XFRAME (w->frame);
27747
27748 if (updated_row->full_width_p)
27749 max_x = (WINDOW_PIXEL_WIDTH (w)
27750 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27751 else
27752 max_x = window_box_width (w, updated_area);
27753 max_y = window_text_bottom_y (w);
27754
27755 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27756 of window. For TO_X > 0, truncate to end of drawing area. */
27757 if (to_x == 0)
27758 return;
27759 else if (to_x < 0)
27760 to_x = max_x;
27761 else
27762 to_x = min (to_x, max_x);
27763
27764 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27765
27766 /* Notice if the cursor will be cleared by this operation. */
27767 if (!updated_row->full_width_p)
27768 notice_overwritten_cursor (w, updated_area,
27769 w->output_cursor.x, -1,
27770 updated_row->y,
27771 MATRIX_ROW_BOTTOM_Y (updated_row));
27772
27773 from_x = w->output_cursor.x;
27774
27775 /* Translate to frame coordinates. */
27776 if (updated_row->full_width_p)
27777 {
27778 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27779 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27780 }
27781 else
27782 {
27783 int area_left = window_box_left (w, updated_area);
27784 from_x += area_left;
27785 to_x += area_left;
27786 }
27787
27788 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27789 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27790 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27791
27792 /* Prevent inadvertently clearing to end of the X window. */
27793 if (to_x > from_x && to_y > from_y)
27794 {
27795 block_input ();
27796 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27797 to_x - from_x, to_y - from_y);
27798 unblock_input ();
27799 }
27800 }
27801
27802 #endif /* HAVE_WINDOW_SYSTEM */
27803
27804
27805 \f
27806 /***********************************************************************
27807 Cursor types
27808 ***********************************************************************/
27809
27810 /* Value is the internal representation of the specified cursor type
27811 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27812 of the bar cursor. */
27813
27814 static enum text_cursor_kinds
27815 get_specified_cursor_type (Lisp_Object arg, int *width)
27816 {
27817 enum text_cursor_kinds type;
27818
27819 if (NILP (arg))
27820 return NO_CURSOR;
27821
27822 if (EQ (arg, Qbox))
27823 return FILLED_BOX_CURSOR;
27824
27825 if (EQ (arg, Qhollow))
27826 return HOLLOW_BOX_CURSOR;
27827
27828 if (EQ (arg, Qbar))
27829 {
27830 *width = 2;
27831 return BAR_CURSOR;
27832 }
27833
27834 if (CONSP (arg)
27835 && EQ (XCAR (arg), Qbar)
27836 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27837 {
27838 *width = XINT (XCDR (arg));
27839 return BAR_CURSOR;
27840 }
27841
27842 if (EQ (arg, Qhbar))
27843 {
27844 *width = 2;
27845 return HBAR_CURSOR;
27846 }
27847
27848 if (CONSP (arg)
27849 && EQ (XCAR (arg), Qhbar)
27850 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27851 {
27852 *width = XINT (XCDR (arg));
27853 return HBAR_CURSOR;
27854 }
27855
27856 /* Treat anything unknown as "hollow box cursor".
27857 It was bad to signal an error; people have trouble fixing
27858 .Xdefaults with Emacs, when it has something bad in it. */
27859 type = HOLLOW_BOX_CURSOR;
27860
27861 return type;
27862 }
27863
27864 /* Set the default cursor types for specified frame. */
27865 void
27866 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27867 {
27868 int width = 1;
27869 Lisp_Object tem;
27870
27871 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27872 FRAME_CURSOR_WIDTH (f) = width;
27873
27874 /* By default, set up the blink-off state depending on the on-state. */
27875
27876 tem = Fassoc (arg, Vblink_cursor_alist);
27877 if (!NILP (tem))
27878 {
27879 FRAME_BLINK_OFF_CURSOR (f)
27880 = get_specified_cursor_type (XCDR (tem), &width);
27881 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27882 }
27883 else
27884 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27885
27886 /* Make sure the cursor gets redrawn. */
27887 f->cursor_type_changed = true;
27888 }
27889
27890
27891 #ifdef HAVE_WINDOW_SYSTEM
27892
27893 /* Return the cursor we want to be displayed in window W. Return
27894 width of bar/hbar cursor through WIDTH arg. Return with
27895 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
27896 (i.e. if the `system caret' should track this cursor).
27897
27898 In a mini-buffer window, we want the cursor only to appear if we
27899 are reading input from this window. For the selected window, we
27900 want the cursor type given by the frame parameter or buffer local
27901 setting of cursor-type. If explicitly marked off, draw no cursor.
27902 In all other cases, we want a hollow box cursor. */
27903
27904 static enum text_cursor_kinds
27905 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27906 bool *active_cursor)
27907 {
27908 struct frame *f = XFRAME (w->frame);
27909 struct buffer *b = XBUFFER (w->contents);
27910 int cursor_type = DEFAULT_CURSOR;
27911 Lisp_Object alt_cursor;
27912 bool non_selected = false;
27913
27914 *active_cursor = true;
27915
27916 /* Echo area */
27917 if (cursor_in_echo_area
27918 && FRAME_HAS_MINIBUF_P (f)
27919 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27920 {
27921 if (w == XWINDOW (echo_area_window))
27922 {
27923 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27924 {
27925 *width = FRAME_CURSOR_WIDTH (f);
27926 return FRAME_DESIRED_CURSOR (f);
27927 }
27928 else
27929 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27930 }
27931
27932 *active_cursor = false;
27933 non_selected = true;
27934 }
27935
27936 /* Detect a nonselected window or nonselected frame. */
27937 else if (w != XWINDOW (f->selected_window)
27938 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27939 {
27940 *active_cursor = false;
27941
27942 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27943 return NO_CURSOR;
27944
27945 non_selected = true;
27946 }
27947
27948 /* Never display a cursor in a window in which cursor-type is nil. */
27949 if (NILP (BVAR (b, cursor_type)))
27950 return NO_CURSOR;
27951
27952 /* Get the normal cursor type for this window. */
27953 if (EQ (BVAR (b, cursor_type), Qt))
27954 {
27955 cursor_type = FRAME_DESIRED_CURSOR (f);
27956 *width = FRAME_CURSOR_WIDTH (f);
27957 }
27958 else
27959 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27960
27961 /* Use cursor-in-non-selected-windows instead
27962 for non-selected window or frame. */
27963 if (non_selected)
27964 {
27965 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27966 if (!EQ (Qt, alt_cursor))
27967 return get_specified_cursor_type (alt_cursor, width);
27968 /* t means modify the normal cursor type. */
27969 if (cursor_type == FILLED_BOX_CURSOR)
27970 cursor_type = HOLLOW_BOX_CURSOR;
27971 else if (cursor_type == BAR_CURSOR && *width > 1)
27972 --*width;
27973 return cursor_type;
27974 }
27975
27976 /* Use normal cursor if not blinked off. */
27977 if (!w->cursor_off_p)
27978 {
27979 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
27980 return NO_CURSOR;
27981 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27982 {
27983 if (cursor_type == FILLED_BOX_CURSOR)
27984 {
27985 /* Using a block cursor on large images can be very annoying.
27986 So use a hollow cursor for "large" images.
27987 If image is not transparent (no mask), also use hollow cursor. */
27988 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27989 if (img != NULL && IMAGEP (img->spec))
27990 {
27991 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27992 where N = size of default frame font size.
27993 This should cover most of the "tiny" icons people may use. */
27994 if (!img->mask
27995 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27996 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27997 cursor_type = HOLLOW_BOX_CURSOR;
27998 }
27999 }
28000 else if (cursor_type != NO_CURSOR)
28001 {
28002 /* Display current only supports BOX and HOLLOW cursors for images.
28003 So for now, unconditionally use a HOLLOW cursor when cursor is
28004 not a solid box cursor. */
28005 cursor_type = HOLLOW_BOX_CURSOR;
28006 }
28007 }
28008 return cursor_type;
28009 }
28010
28011 /* Cursor is blinked off, so determine how to "toggle" it. */
28012
28013 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
28014 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
28015 return get_specified_cursor_type (XCDR (alt_cursor), width);
28016
28017 /* Then see if frame has specified a specific blink off cursor type. */
28018 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
28019 {
28020 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
28021 return FRAME_BLINK_OFF_CURSOR (f);
28022 }
28023
28024 #if false
28025 /* Some people liked having a permanently visible blinking cursor,
28026 while others had very strong opinions against it. So it was
28027 decided to remove it. KFS 2003-09-03 */
28028
28029 /* Finally perform built-in cursor blinking:
28030 filled box <-> hollow box
28031 wide [h]bar <-> narrow [h]bar
28032 narrow [h]bar <-> no cursor
28033 other type <-> no cursor */
28034
28035 if (cursor_type == FILLED_BOX_CURSOR)
28036 return HOLLOW_BOX_CURSOR;
28037
28038 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
28039 {
28040 *width = 1;
28041 return cursor_type;
28042 }
28043 #endif
28044
28045 return NO_CURSOR;
28046 }
28047
28048
28049 /* Notice when the text cursor of window W has been completely
28050 overwritten by a drawing operation that outputs glyphs in AREA
28051 starting at X0 and ending at X1 in the line starting at Y0 and
28052 ending at Y1. X coordinates are area-relative. X1 < 0 means all
28053 the rest of the line after X0 has been written. Y coordinates
28054 are window-relative. */
28055
28056 static void
28057 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
28058 int x0, int x1, int y0, int y1)
28059 {
28060 int cx0, cx1, cy0, cy1;
28061 struct glyph_row *row;
28062
28063 if (!w->phys_cursor_on_p)
28064 return;
28065 if (area != TEXT_AREA)
28066 return;
28067
28068 if (w->phys_cursor.vpos < 0
28069 || w->phys_cursor.vpos >= w->current_matrix->nrows
28070 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
28071 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
28072 return;
28073
28074 if (row->cursor_in_fringe_p)
28075 {
28076 row->cursor_in_fringe_p = false;
28077 draw_fringe_bitmap (w, row, row->reversed_p);
28078 w->phys_cursor_on_p = false;
28079 return;
28080 }
28081
28082 cx0 = w->phys_cursor.x;
28083 cx1 = cx0 + w->phys_cursor_width;
28084 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
28085 return;
28086
28087 /* The cursor image will be completely removed from the
28088 screen if the output area intersects the cursor area in
28089 y-direction. When we draw in [y0 y1[, and some part of
28090 the cursor is at y < y0, that part must have been drawn
28091 before. When scrolling, the cursor is erased before
28092 actually scrolling, so we don't come here. When not
28093 scrolling, the rows above the old cursor row must have
28094 changed, and in this case these rows must have written
28095 over the cursor image.
28096
28097 Likewise if part of the cursor is below y1, with the
28098 exception of the cursor being in the first blank row at
28099 the buffer and window end because update_text_area
28100 doesn't draw that row. (Except when it does, but
28101 that's handled in update_text_area.) */
28102
28103 cy0 = w->phys_cursor.y;
28104 cy1 = cy0 + w->phys_cursor_height;
28105 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
28106 return;
28107
28108 w->phys_cursor_on_p = false;
28109 }
28110
28111 #endif /* HAVE_WINDOW_SYSTEM */
28112
28113 \f
28114 /************************************************************************
28115 Mouse Face
28116 ************************************************************************/
28117
28118 #ifdef HAVE_WINDOW_SYSTEM
28119
28120 /* EXPORT for RIF:
28121 Fix the display of area AREA of overlapping row ROW in window W
28122 with respect to the overlapping part OVERLAPS. */
28123
28124 void
28125 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
28126 enum glyph_row_area area, int overlaps)
28127 {
28128 int i, x;
28129
28130 block_input ();
28131
28132 x = 0;
28133 for (i = 0; i < row->used[area];)
28134 {
28135 if (row->glyphs[area][i].overlaps_vertically_p)
28136 {
28137 int start = i, start_x = x;
28138
28139 do
28140 {
28141 x += row->glyphs[area][i].pixel_width;
28142 ++i;
28143 }
28144 while (i < row->used[area]
28145 && row->glyphs[area][i].overlaps_vertically_p);
28146
28147 draw_glyphs (w, start_x, row, area,
28148 start, i,
28149 DRAW_NORMAL_TEXT, overlaps);
28150 }
28151 else
28152 {
28153 x += row->glyphs[area][i].pixel_width;
28154 ++i;
28155 }
28156 }
28157
28158 unblock_input ();
28159 }
28160
28161
28162 /* EXPORT:
28163 Draw the cursor glyph of window W in glyph row ROW. See the
28164 comment of draw_glyphs for the meaning of HL. */
28165
28166 void
28167 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
28168 enum draw_glyphs_face hl)
28169 {
28170 /* If cursor hpos is out of bounds, don't draw garbage. This can
28171 happen in mini-buffer windows when switching between echo area
28172 glyphs and mini-buffer. */
28173 if ((row->reversed_p
28174 ? (w->phys_cursor.hpos >= 0)
28175 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
28176 {
28177 bool on_p = w->phys_cursor_on_p;
28178 int x1;
28179 int hpos = w->phys_cursor.hpos;
28180
28181 /* When the window is hscrolled, cursor hpos can legitimately be
28182 out of bounds, but we draw the cursor at the corresponding
28183 window margin in that case. */
28184 if (!row->reversed_p && hpos < 0)
28185 hpos = 0;
28186 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28187 hpos = row->used[TEXT_AREA] - 1;
28188
28189 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
28190 hl, 0);
28191 w->phys_cursor_on_p = on_p;
28192
28193 if (hl == DRAW_CURSOR)
28194 w->phys_cursor_width = x1 - w->phys_cursor.x;
28195 /* When we erase the cursor, and ROW is overlapped by other
28196 rows, make sure that these overlapping parts of other rows
28197 are redrawn. */
28198 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
28199 {
28200 w->phys_cursor_width = x1 - w->phys_cursor.x;
28201
28202 if (row > w->current_matrix->rows
28203 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
28204 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
28205 OVERLAPS_ERASED_CURSOR);
28206
28207 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
28208 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
28209 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
28210 OVERLAPS_ERASED_CURSOR);
28211 }
28212 }
28213 }
28214
28215
28216 /* Erase the image of a cursor of window W from the screen. */
28217
28218 void
28219 erase_phys_cursor (struct window *w)
28220 {
28221 struct frame *f = XFRAME (w->frame);
28222 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28223 int hpos = w->phys_cursor.hpos;
28224 int vpos = w->phys_cursor.vpos;
28225 bool mouse_face_here_p = false;
28226 struct glyph_matrix *active_glyphs = w->current_matrix;
28227 struct glyph_row *cursor_row;
28228 struct glyph *cursor_glyph;
28229 enum draw_glyphs_face hl;
28230
28231 /* No cursor displayed or row invalidated => nothing to do on the
28232 screen. */
28233 if (w->phys_cursor_type == NO_CURSOR)
28234 goto mark_cursor_off;
28235
28236 /* VPOS >= active_glyphs->nrows means that window has been resized.
28237 Don't bother to erase the cursor. */
28238 if (vpos >= active_glyphs->nrows)
28239 goto mark_cursor_off;
28240
28241 /* If row containing cursor is marked invalid, there is nothing we
28242 can do. */
28243 cursor_row = MATRIX_ROW (active_glyphs, vpos);
28244 if (!cursor_row->enabled_p)
28245 goto mark_cursor_off;
28246
28247 /* If line spacing is > 0, old cursor may only be partially visible in
28248 window after split-window. So adjust visible height. */
28249 cursor_row->visible_height = min (cursor_row->visible_height,
28250 window_text_bottom_y (w) - cursor_row->y);
28251
28252 /* If row is completely invisible, don't attempt to delete a cursor which
28253 isn't there. This can happen if cursor is at top of a window, and
28254 we switch to a buffer with a header line in that window. */
28255 if (cursor_row->visible_height <= 0)
28256 goto mark_cursor_off;
28257
28258 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
28259 if (cursor_row->cursor_in_fringe_p)
28260 {
28261 cursor_row->cursor_in_fringe_p = false;
28262 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
28263 goto mark_cursor_off;
28264 }
28265
28266 /* This can happen when the new row is shorter than the old one.
28267 In this case, either draw_glyphs or clear_end_of_line
28268 should have cleared the cursor. Note that we wouldn't be
28269 able to erase the cursor in this case because we don't have a
28270 cursor glyph at hand. */
28271 if ((cursor_row->reversed_p
28272 ? (w->phys_cursor.hpos < 0)
28273 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
28274 goto mark_cursor_off;
28275
28276 /* When the window is hscrolled, cursor hpos can legitimately be out
28277 of bounds, but we draw the cursor at the corresponding window
28278 margin in that case. */
28279 if (!cursor_row->reversed_p && hpos < 0)
28280 hpos = 0;
28281 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
28282 hpos = cursor_row->used[TEXT_AREA] - 1;
28283
28284 /* If the cursor is in the mouse face area, redisplay that when
28285 we clear the cursor. */
28286 if (! NILP (hlinfo->mouse_face_window)
28287 && coords_in_mouse_face_p (w, hpos, vpos)
28288 /* Don't redraw the cursor's spot in mouse face if it is at the
28289 end of a line (on a newline). The cursor appears there, but
28290 mouse highlighting does not. */
28291 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
28292 mouse_face_here_p = true;
28293
28294 /* Maybe clear the display under the cursor. */
28295 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
28296 {
28297 int x, y;
28298 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
28299 int width;
28300
28301 cursor_glyph = get_phys_cursor_glyph (w);
28302 if (cursor_glyph == NULL)
28303 goto mark_cursor_off;
28304
28305 width = cursor_glyph->pixel_width;
28306 x = w->phys_cursor.x;
28307 if (x < 0)
28308 {
28309 width += x;
28310 x = 0;
28311 }
28312 width = min (width, window_box_width (w, TEXT_AREA) - x);
28313 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
28314 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28315
28316 if (width > 0)
28317 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28318 }
28319
28320 /* Erase the cursor by redrawing the character underneath it. */
28321 if (mouse_face_here_p)
28322 hl = DRAW_MOUSE_FACE;
28323 else
28324 hl = DRAW_NORMAL_TEXT;
28325 draw_phys_cursor_glyph (w, cursor_row, hl);
28326
28327 mark_cursor_off:
28328 w->phys_cursor_on_p = false;
28329 w->phys_cursor_type = NO_CURSOR;
28330 }
28331
28332
28333 /* Display or clear cursor of window W. If !ON, clear the cursor.
28334 If ON, display the cursor; where to put the cursor is specified by
28335 HPOS, VPOS, X and Y. */
28336
28337 void
28338 display_and_set_cursor (struct window *w, bool on,
28339 int hpos, int vpos, int x, int y)
28340 {
28341 struct frame *f = XFRAME (w->frame);
28342 int new_cursor_type;
28343 int new_cursor_width;
28344 bool active_cursor;
28345 struct glyph_row *glyph_row;
28346 struct glyph *glyph;
28347
28348 /* This is pointless on invisible frames, and dangerous on garbaged
28349 windows and frames; in the latter case, the frame or window may
28350 be in the midst of changing its size, and x and y may be off the
28351 window. */
28352 if (! FRAME_VISIBLE_P (f)
28353 || FRAME_GARBAGED_P (f)
28354 || vpos >= w->current_matrix->nrows
28355 || hpos >= w->current_matrix->matrix_w)
28356 return;
28357
28358 /* If cursor is off and we want it off, return quickly. */
28359 if (!on && !w->phys_cursor_on_p)
28360 return;
28361
28362 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28363 /* If cursor row is not enabled, we don't really know where to
28364 display the cursor. */
28365 if (!glyph_row->enabled_p)
28366 {
28367 w->phys_cursor_on_p = false;
28368 return;
28369 }
28370
28371 glyph = NULL;
28372 if (!glyph_row->exact_window_width_line_p
28373 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
28374 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28375
28376 eassert (input_blocked_p ());
28377
28378 /* Set new_cursor_type to the cursor we want to be displayed. */
28379 new_cursor_type = get_window_cursor_type (w, glyph,
28380 &new_cursor_width, &active_cursor);
28381
28382 /* If cursor is currently being shown and we don't want it to be or
28383 it is in the wrong place, or the cursor type is not what we want,
28384 erase it. */
28385 if (w->phys_cursor_on_p
28386 && (!on
28387 || w->phys_cursor.x != x
28388 || w->phys_cursor.y != y
28389 /* HPOS can be negative in R2L rows whose
28390 exact_window_width_line_p flag is set (i.e. their newline
28391 would "overflow into the fringe"). */
28392 || hpos < 0
28393 || new_cursor_type != w->phys_cursor_type
28394 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28395 && new_cursor_width != w->phys_cursor_width)))
28396 erase_phys_cursor (w);
28397
28398 /* Don't check phys_cursor_on_p here because that flag is only set
28399 to false in some cases where we know that the cursor has been
28400 completely erased, to avoid the extra work of erasing the cursor
28401 twice. In other words, phys_cursor_on_p can be true and the cursor
28402 still not be visible, or it has only been partly erased. */
28403 if (on)
28404 {
28405 w->phys_cursor_ascent = glyph_row->ascent;
28406 w->phys_cursor_height = glyph_row->height;
28407
28408 /* Set phys_cursor_.* before x_draw_.* is called because some
28409 of them may need the information. */
28410 w->phys_cursor.x = x;
28411 w->phys_cursor.y = glyph_row->y;
28412 w->phys_cursor.hpos = hpos;
28413 w->phys_cursor.vpos = vpos;
28414 }
28415
28416 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28417 new_cursor_type, new_cursor_width,
28418 on, active_cursor);
28419 }
28420
28421
28422 /* Switch the display of W's cursor on or off, according to the value
28423 of ON. */
28424
28425 static void
28426 update_window_cursor (struct window *w, bool on)
28427 {
28428 /* Don't update cursor in windows whose frame is in the process
28429 of being deleted. */
28430 if (w->current_matrix)
28431 {
28432 int hpos = w->phys_cursor.hpos;
28433 int vpos = w->phys_cursor.vpos;
28434 struct glyph_row *row;
28435
28436 if (vpos >= w->current_matrix->nrows
28437 || hpos >= w->current_matrix->matrix_w)
28438 return;
28439
28440 row = MATRIX_ROW (w->current_matrix, vpos);
28441
28442 /* When the window is hscrolled, cursor hpos can legitimately be
28443 out of bounds, but we draw the cursor at the corresponding
28444 window margin in that case. */
28445 if (!row->reversed_p && hpos < 0)
28446 hpos = 0;
28447 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28448 hpos = row->used[TEXT_AREA] - 1;
28449
28450 block_input ();
28451 display_and_set_cursor (w, on, hpos, vpos,
28452 w->phys_cursor.x, w->phys_cursor.y);
28453 unblock_input ();
28454 }
28455 }
28456
28457
28458 /* Call update_window_cursor with parameter ON_P on all leaf windows
28459 in the window tree rooted at W. */
28460
28461 static void
28462 update_cursor_in_window_tree (struct window *w, bool on_p)
28463 {
28464 while (w)
28465 {
28466 if (WINDOWP (w->contents))
28467 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28468 else
28469 update_window_cursor (w, on_p);
28470
28471 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28472 }
28473 }
28474
28475
28476 /* EXPORT:
28477 Display the cursor on window W, or clear it, according to ON_P.
28478 Don't change the cursor's position. */
28479
28480 void
28481 x_update_cursor (struct frame *f, bool on_p)
28482 {
28483 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28484 }
28485
28486
28487 /* EXPORT:
28488 Clear the cursor of window W to background color, and mark the
28489 cursor as not shown. This is used when the text where the cursor
28490 is about to be rewritten. */
28491
28492 void
28493 x_clear_cursor (struct window *w)
28494 {
28495 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28496 update_window_cursor (w, false);
28497 }
28498
28499 #endif /* HAVE_WINDOW_SYSTEM */
28500
28501 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28502 and MSDOS. */
28503 static void
28504 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28505 int start_hpos, int end_hpos,
28506 enum draw_glyphs_face draw)
28507 {
28508 #ifdef HAVE_WINDOW_SYSTEM
28509 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28510 {
28511 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28512 return;
28513 }
28514 #endif
28515 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28516 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28517 #endif
28518 }
28519
28520 /* Display the active region described by mouse_face_* according to DRAW. */
28521
28522 static void
28523 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28524 {
28525 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28526 struct frame *f = XFRAME (WINDOW_FRAME (w));
28527
28528 if (/* If window is in the process of being destroyed, don't bother
28529 to do anything. */
28530 w->current_matrix != NULL
28531 /* Don't update mouse highlight if hidden. */
28532 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28533 /* Recognize when we are called to operate on rows that don't exist
28534 anymore. This can happen when a window is split. */
28535 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28536 {
28537 bool phys_cursor_on_p = w->phys_cursor_on_p;
28538 struct glyph_row *row, *first, *last;
28539
28540 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28541 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28542
28543 for (row = first; row <= last && row->enabled_p; ++row)
28544 {
28545 int start_hpos, end_hpos, start_x;
28546
28547 /* For all but the first row, the highlight starts at column 0. */
28548 if (row == first)
28549 {
28550 /* R2L rows have BEG and END in reversed order, but the
28551 screen drawing geometry is always left to right. So
28552 we need to mirror the beginning and end of the
28553 highlighted area in R2L rows. */
28554 if (!row->reversed_p)
28555 {
28556 start_hpos = hlinfo->mouse_face_beg_col;
28557 start_x = hlinfo->mouse_face_beg_x;
28558 }
28559 else if (row == last)
28560 {
28561 start_hpos = hlinfo->mouse_face_end_col;
28562 start_x = hlinfo->mouse_face_end_x;
28563 }
28564 else
28565 {
28566 start_hpos = 0;
28567 start_x = 0;
28568 }
28569 }
28570 else if (row->reversed_p && row == last)
28571 {
28572 start_hpos = hlinfo->mouse_face_end_col;
28573 start_x = hlinfo->mouse_face_end_x;
28574 }
28575 else
28576 {
28577 start_hpos = 0;
28578 start_x = 0;
28579 }
28580
28581 if (row == last)
28582 {
28583 if (!row->reversed_p)
28584 end_hpos = hlinfo->mouse_face_end_col;
28585 else if (row == first)
28586 end_hpos = hlinfo->mouse_face_beg_col;
28587 else
28588 {
28589 end_hpos = row->used[TEXT_AREA];
28590 if (draw == DRAW_NORMAL_TEXT)
28591 row->fill_line_p = true; /* Clear to end of line. */
28592 }
28593 }
28594 else if (row->reversed_p && row == first)
28595 end_hpos = hlinfo->mouse_face_beg_col;
28596 else
28597 {
28598 end_hpos = row->used[TEXT_AREA];
28599 if (draw == DRAW_NORMAL_TEXT)
28600 row->fill_line_p = true; /* Clear to end of line. */
28601 }
28602
28603 if (end_hpos > start_hpos)
28604 {
28605 draw_row_with_mouse_face (w, start_x, row,
28606 start_hpos, end_hpos, draw);
28607
28608 row->mouse_face_p
28609 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28610 }
28611 }
28612
28613 #ifdef HAVE_WINDOW_SYSTEM
28614 /* When we've written over the cursor, arrange for it to
28615 be displayed again. */
28616 if (FRAME_WINDOW_P (f)
28617 && phys_cursor_on_p && !w->phys_cursor_on_p)
28618 {
28619 int hpos = w->phys_cursor.hpos;
28620
28621 /* When the window is hscrolled, cursor hpos can legitimately be
28622 out of bounds, but we draw the cursor at the corresponding
28623 window margin in that case. */
28624 if (!row->reversed_p && hpos < 0)
28625 hpos = 0;
28626 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28627 hpos = row->used[TEXT_AREA] - 1;
28628
28629 block_input ();
28630 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28631 w->phys_cursor.x, w->phys_cursor.y);
28632 unblock_input ();
28633 }
28634 #endif /* HAVE_WINDOW_SYSTEM */
28635 }
28636
28637 #ifdef HAVE_WINDOW_SYSTEM
28638 /* Change the mouse cursor. */
28639 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28640 {
28641 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28642 if (draw == DRAW_NORMAL_TEXT
28643 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28644 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28645 else
28646 #endif
28647 if (draw == DRAW_MOUSE_FACE)
28648 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28649 else
28650 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28651 }
28652 #endif /* HAVE_WINDOW_SYSTEM */
28653 }
28654
28655 /* EXPORT:
28656 Clear out the mouse-highlighted active region.
28657 Redraw it un-highlighted first. Value is true if mouse
28658 face was actually drawn unhighlighted. */
28659
28660 bool
28661 clear_mouse_face (Mouse_HLInfo *hlinfo)
28662 {
28663 bool cleared
28664 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28665 if (cleared)
28666 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28667 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28668 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28669 hlinfo->mouse_face_window = Qnil;
28670 hlinfo->mouse_face_overlay = Qnil;
28671 return cleared;
28672 }
28673
28674 /* Return true if the coordinates HPOS and VPOS on windows W are
28675 within the mouse face on that window. */
28676 static bool
28677 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28678 {
28679 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28680
28681 /* Quickly resolve the easy cases. */
28682 if (!(WINDOWP (hlinfo->mouse_face_window)
28683 && XWINDOW (hlinfo->mouse_face_window) == w))
28684 return false;
28685 if (vpos < hlinfo->mouse_face_beg_row
28686 || vpos > hlinfo->mouse_face_end_row)
28687 return false;
28688 if (vpos > hlinfo->mouse_face_beg_row
28689 && vpos < hlinfo->mouse_face_end_row)
28690 return true;
28691
28692 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28693 {
28694 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28695 {
28696 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28697 return true;
28698 }
28699 else if ((vpos == hlinfo->mouse_face_beg_row
28700 && hpos >= hlinfo->mouse_face_beg_col)
28701 || (vpos == hlinfo->mouse_face_end_row
28702 && hpos < hlinfo->mouse_face_end_col))
28703 return true;
28704 }
28705 else
28706 {
28707 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28708 {
28709 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28710 return true;
28711 }
28712 else if ((vpos == hlinfo->mouse_face_beg_row
28713 && hpos <= hlinfo->mouse_face_beg_col)
28714 || (vpos == hlinfo->mouse_face_end_row
28715 && hpos > hlinfo->mouse_face_end_col))
28716 return true;
28717 }
28718 return false;
28719 }
28720
28721
28722 /* EXPORT:
28723 True if physical cursor of window W is within mouse face. */
28724
28725 bool
28726 cursor_in_mouse_face_p (struct window *w)
28727 {
28728 int hpos = w->phys_cursor.hpos;
28729 int vpos = w->phys_cursor.vpos;
28730 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28731
28732 /* When the window is hscrolled, cursor hpos can legitimately be out
28733 of bounds, but we draw the cursor at the corresponding window
28734 margin in that case. */
28735 if (!row->reversed_p && hpos < 0)
28736 hpos = 0;
28737 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28738 hpos = row->used[TEXT_AREA] - 1;
28739
28740 return coords_in_mouse_face_p (w, hpos, vpos);
28741 }
28742
28743
28744 \f
28745 /* Find the glyph rows START_ROW and END_ROW of window W that display
28746 characters between buffer positions START_CHARPOS and END_CHARPOS
28747 (excluding END_CHARPOS). DISP_STRING is a display string that
28748 covers these buffer positions. This is similar to
28749 row_containing_pos, but is more accurate when bidi reordering makes
28750 buffer positions change non-linearly with glyph rows. */
28751 static void
28752 rows_from_pos_range (struct window *w,
28753 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28754 Lisp_Object disp_string,
28755 struct glyph_row **start, struct glyph_row **end)
28756 {
28757 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28758 int last_y = window_text_bottom_y (w);
28759 struct glyph_row *row;
28760
28761 *start = NULL;
28762 *end = NULL;
28763
28764 while (!first->enabled_p
28765 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28766 first++;
28767
28768 /* Find the START row. */
28769 for (row = first;
28770 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28771 row++)
28772 {
28773 /* A row can potentially be the START row if the range of the
28774 characters it displays intersects the range
28775 [START_CHARPOS..END_CHARPOS). */
28776 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28777 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28778 /* See the commentary in row_containing_pos, for the
28779 explanation of the complicated way to check whether
28780 some position is beyond the end of the characters
28781 displayed by a row. */
28782 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28783 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28784 && !row->ends_at_zv_p
28785 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28786 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28787 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28788 && !row->ends_at_zv_p
28789 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28790 {
28791 /* Found a candidate row. Now make sure at least one of the
28792 glyphs it displays has a charpos from the range
28793 [START_CHARPOS..END_CHARPOS).
28794
28795 This is not obvious because bidi reordering could make
28796 buffer positions of a row be 1,2,3,102,101,100, and if we
28797 want to highlight characters in [50..60), we don't want
28798 this row, even though [50..60) does intersect [1..103),
28799 the range of character positions given by the row's start
28800 and end positions. */
28801 struct glyph *g = row->glyphs[TEXT_AREA];
28802 struct glyph *e = g + row->used[TEXT_AREA];
28803
28804 while (g < e)
28805 {
28806 if (((BUFFERP (g->object) || NILP (g->object))
28807 && start_charpos <= g->charpos && g->charpos < end_charpos)
28808 /* A glyph that comes from DISP_STRING is by
28809 definition to be highlighted. */
28810 || EQ (g->object, disp_string))
28811 *start = row;
28812 g++;
28813 }
28814 if (*start)
28815 break;
28816 }
28817 }
28818
28819 /* Find the END row. */
28820 if (!*start
28821 /* If the last row is partially visible, start looking for END
28822 from that row, instead of starting from FIRST. */
28823 && !(row->enabled_p
28824 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28825 row = first;
28826 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28827 {
28828 struct glyph_row *next = row + 1;
28829 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28830
28831 if (!next->enabled_p
28832 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28833 /* The first row >= START whose range of displayed characters
28834 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28835 is the row END + 1. */
28836 || (start_charpos < next_start
28837 && end_charpos < next_start)
28838 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28839 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28840 && !next->ends_at_zv_p
28841 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28842 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28843 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28844 && !next->ends_at_zv_p
28845 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28846 {
28847 *end = row;
28848 break;
28849 }
28850 else
28851 {
28852 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28853 but none of the characters it displays are in the range, it is
28854 also END + 1. */
28855 struct glyph *g = next->glyphs[TEXT_AREA];
28856 struct glyph *s = g;
28857 struct glyph *e = g + next->used[TEXT_AREA];
28858
28859 while (g < e)
28860 {
28861 if (((BUFFERP (g->object) || NILP (g->object))
28862 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28863 /* If the buffer position of the first glyph in
28864 the row is equal to END_CHARPOS, it means
28865 the last character to be highlighted is the
28866 newline of ROW, and we must consider NEXT as
28867 END, not END+1. */
28868 || (((!next->reversed_p && g == s)
28869 || (next->reversed_p && g == e - 1))
28870 && (g->charpos == end_charpos
28871 /* Special case for when NEXT is an
28872 empty line at ZV. */
28873 || (g->charpos == -1
28874 && !row->ends_at_zv_p
28875 && next_start == end_charpos)))))
28876 /* A glyph that comes from DISP_STRING is by
28877 definition to be highlighted. */
28878 || EQ (g->object, disp_string))
28879 break;
28880 g++;
28881 }
28882 if (g == e)
28883 {
28884 *end = row;
28885 break;
28886 }
28887 /* The first row that ends at ZV must be the last to be
28888 highlighted. */
28889 else if (next->ends_at_zv_p)
28890 {
28891 *end = next;
28892 break;
28893 }
28894 }
28895 }
28896 }
28897
28898 /* This function sets the mouse_face_* elements of HLINFO, assuming
28899 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28900 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28901 for the overlay or run of text properties specifying the mouse
28902 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28903 before-string and after-string that must also be highlighted.
28904 DISP_STRING, if non-nil, is a display string that may cover some
28905 or all of the highlighted text. */
28906
28907 static void
28908 mouse_face_from_buffer_pos (Lisp_Object window,
28909 Mouse_HLInfo *hlinfo,
28910 ptrdiff_t mouse_charpos,
28911 ptrdiff_t start_charpos,
28912 ptrdiff_t end_charpos,
28913 Lisp_Object before_string,
28914 Lisp_Object after_string,
28915 Lisp_Object disp_string)
28916 {
28917 struct window *w = XWINDOW (window);
28918 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28919 struct glyph_row *r1, *r2;
28920 struct glyph *glyph, *end;
28921 ptrdiff_t ignore, pos;
28922 int x;
28923
28924 eassert (NILP (disp_string) || STRINGP (disp_string));
28925 eassert (NILP (before_string) || STRINGP (before_string));
28926 eassert (NILP (after_string) || STRINGP (after_string));
28927
28928 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28929 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28930 if (r1 == NULL)
28931 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28932 /* If the before-string or display-string contains newlines,
28933 rows_from_pos_range skips to its last row. Move back. */
28934 if (!NILP (before_string) || !NILP (disp_string))
28935 {
28936 struct glyph_row *prev;
28937 while ((prev = r1 - 1, prev >= first)
28938 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28939 && prev->used[TEXT_AREA] > 0)
28940 {
28941 struct glyph *beg = prev->glyphs[TEXT_AREA];
28942 glyph = beg + prev->used[TEXT_AREA];
28943 while (--glyph >= beg && NILP (glyph->object));
28944 if (glyph < beg
28945 || !(EQ (glyph->object, before_string)
28946 || EQ (glyph->object, disp_string)))
28947 break;
28948 r1 = prev;
28949 }
28950 }
28951 if (r2 == NULL)
28952 {
28953 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28954 hlinfo->mouse_face_past_end = true;
28955 }
28956 else if (!NILP (after_string))
28957 {
28958 /* If the after-string has newlines, advance to its last row. */
28959 struct glyph_row *next;
28960 struct glyph_row *last
28961 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28962
28963 for (next = r2 + 1;
28964 next <= last
28965 && next->used[TEXT_AREA] > 0
28966 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28967 ++next)
28968 r2 = next;
28969 }
28970 /* The rest of the display engine assumes that mouse_face_beg_row is
28971 either above mouse_face_end_row or identical to it. But with
28972 bidi-reordered continued lines, the row for START_CHARPOS could
28973 be below the row for END_CHARPOS. If so, swap the rows and store
28974 them in correct order. */
28975 if (r1->y > r2->y)
28976 {
28977 struct glyph_row *tem = r2;
28978
28979 r2 = r1;
28980 r1 = tem;
28981 }
28982
28983 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28984 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28985
28986 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28987 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28988 could be anywhere in the row and in any order. The strategy
28989 below is to find the leftmost and the rightmost glyph that
28990 belongs to either of these 3 strings, or whose position is
28991 between START_CHARPOS and END_CHARPOS, and highlight all the
28992 glyphs between those two. This may cover more than just the text
28993 between START_CHARPOS and END_CHARPOS if the range of characters
28994 strides the bidi level boundary, e.g. if the beginning is in R2L
28995 text while the end is in L2R text or vice versa. */
28996 if (!r1->reversed_p)
28997 {
28998 /* This row is in a left to right paragraph. Scan it left to
28999 right. */
29000 glyph = r1->glyphs[TEXT_AREA];
29001 end = glyph + r1->used[TEXT_AREA];
29002 x = r1->x;
29003
29004 /* Skip truncation glyphs at the start of the glyph row. */
29005 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29006 for (; glyph < end
29007 && NILP (glyph->object)
29008 && glyph->charpos < 0;
29009 ++glyph)
29010 x += glyph->pixel_width;
29011
29012 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29013 or DISP_STRING, and the first glyph from buffer whose
29014 position is between START_CHARPOS and END_CHARPOS. */
29015 for (; glyph < end
29016 && !NILP (glyph->object)
29017 && !EQ (glyph->object, disp_string)
29018 && !(BUFFERP (glyph->object)
29019 && (glyph->charpos >= start_charpos
29020 && glyph->charpos < end_charpos));
29021 ++glyph)
29022 {
29023 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29024 are present at buffer positions between START_CHARPOS and
29025 END_CHARPOS, or if they come from an overlay. */
29026 if (EQ (glyph->object, before_string))
29027 {
29028 pos = string_buffer_position (before_string,
29029 start_charpos);
29030 /* If pos == 0, it means before_string came from an
29031 overlay, not from a buffer position. */
29032 if (!pos || (pos >= start_charpos && pos < end_charpos))
29033 break;
29034 }
29035 else if (EQ (glyph->object, after_string))
29036 {
29037 pos = string_buffer_position (after_string, end_charpos);
29038 if (!pos || (pos >= start_charpos && pos < end_charpos))
29039 break;
29040 }
29041 x += glyph->pixel_width;
29042 }
29043 hlinfo->mouse_face_beg_x = x;
29044 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29045 }
29046 else
29047 {
29048 /* This row is in a right to left paragraph. Scan it right to
29049 left. */
29050 struct glyph *g;
29051
29052 end = r1->glyphs[TEXT_AREA] - 1;
29053 glyph = end + r1->used[TEXT_AREA];
29054
29055 /* Skip truncation glyphs at the start of the glyph row. */
29056 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29057 for (; glyph > end
29058 && NILP (glyph->object)
29059 && glyph->charpos < 0;
29060 --glyph)
29061 ;
29062
29063 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29064 or DISP_STRING, and the first glyph from buffer whose
29065 position is between START_CHARPOS and END_CHARPOS. */
29066 for (; glyph > end
29067 && !NILP (glyph->object)
29068 && !EQ (glyph->object, disp_string)
29069 && !(BUFFERP (glyph->object)
29070 && (glyph->charpos >= start_charpos
29071 && glyph->charpos < end_charpos));
29072 --glyph)
29073 {
29074 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29075 are present at buffer positions between START_CHARPOS and
29076 END_CHARPOS, or if they come from an overlay. */
29077 if (EQ (glyph->object, before_string))
29078 {
29079 pos = string_buffer_position (before_string, start_charpos);
29080 /* If pos == 0, it means before_string came from an
29081 overlay, not from a buffer position. */
29082 if (!pos || (pos >= start_charpos && pos < end_charpos))
29083 break;
29084 }
29085 else if (EQ (glyph->object, after_string))
29086 {
29087 pos = string_buffer_position (after_string, end_charpos);
29088 if (!pos || (pos >= start_charpos && pos < end_charpos))
29089 break;
29090 }
29091 }
29092
29093 glyph++; /* first glyph to the right of the highlighted area */
29094 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
29095 x += g->pixel_width;
29096 hlinfo->mouse_face_beg_x = x;
29097 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29098 }
29099
29100 /* If the highlight ends in a different row, compute GLYPH and END
29101 for the end row. Otherwise, reuse the values computed above for
29102 the row where the highlight begins. */
29103 if (r2 != r1)
29104 {
29105 if (!r2->reversed_p)
29106 {
29107 glyph = r2->glyphs[TEXT_AREA];
29108 end = glyph + r2->used[TEXT_AREA];
29109 x = r2->x;
29110 }
29111 else
29112 {
29113 end = r2->glyphs[TEXT_AREA] - 1;
29114 glyph = end + r2->used[TEXT_AREA];
29115 }
29116 }
29117
29118 if (!r2->reversed_p)
29119 {
29120 /* Skip truncation and continuation glyphs near the end of the
29121 row, and also blanks and stretch glyphs inserted by
29122 extend_face_to_end_of_line. */
29123 while (end > glyph
29124 && NILP ((end - 1)->object))
29125 --end;
29126 /* Scan the rest of the glyph row from the end, looking for the
29127 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29128 DISP_STRING, or whose position is between START_CHARPOS
29129 and END_CHARPOS */
29130 for (--end;
29131 end > glyph
29132 && !NILP (end->object)
29133 && !EQ (end->object, disp_string)
29134 && !(BUFFERP (end->object)
29135 && (end->charpos >= start_charpos
29136 && end->charpos < end_charpos));
29137 --end)
29138 {
29139 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29140 are present at buffer positions between START_CHARPOS and
29141 END_CHARPOS, or if they come from an overlay. */
29142 if (EQ (end->object, before_string))
29143 {
29144 pos = string_buffer_position (before_string, start_charpos);
29145 if (!pos || (pos >= start_charpos && pos < end_charpos))
29146 break;
29147 }
29148 else if (EQ (end->object, after_string))
29149 {
29150 pos = string_buffer_position (after_string, end_charpos);
29151 if (!pos || (pos >= start_charpos && pos < end_charpos))
29152 break;
29153 }
29154 }
29155 /* Find the X coordinate of the last glyph to be highlighted. */
29156 for (; glyph <= end; ++glyph)
29157 x += glyph->pixel_width;
29158
29159 hlinfo->mouse_face_end_x = x;
29160 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
29161 }
29162 else
29163 {
29164 /* Skip truncation and continuation glyphs near the end of the
29165 row, and also blanks and stretch glyphs inserted by
29166 extend_face_to_end_of_line. */
29167 x = r2->x;
29168 end++;
29169 while (end < glyph
29170 && NILP (end->object))
29171 {
29172 x += end->pixel_width;
29173 ++end;
29174 }
29175 /* Scan the rest of the glyph row from the end, looking for the
29176 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29177 DISP_STRING, or whose position is between START_CHARPOS
29178 and END_CHARPOS */
29179 for ( ;
29180 end < glyph
29181 && !NILP (end->object)
29182 && !EQ (end->object, disp_string)
29183 && !(BUFFERP (end->object)
29184 && (end->charpos >= start_charpos
29185 && end->charpos < end_charpos));
29186 ++end)
29187 {
29188 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29189 are present at buffer positions between START_CHARPOS and
29190 END_CHARPOS, or if they come from an overlay. */
29191 if (EQ (end->object, before_string))
29192 {
29193 pos = string_buffer_position (before_string, start_charpos);
29194 if (!pos || (pos >= start_charpos && pos < end_charpos))
29195 break;
29196 }
29197 else if (EQ (end->object, after_string))
29198 {
29199 pos = string_buffer_position (after_string, end_charpos);
29200 if (!pos || (pos >= start_charpos && pos < end_charpos))
29201 break;
29202 }
29203 x += end->pixel_width;
29204 }
29205 /* If we exited the above loop because we arrived at the last
29206 glyph of the row, and its buffer position is still not in
29207 range, it means the last character in range is the preceding
29208 newline. Bump the end column and x values to get past the
29209 last glyph. */
29210 if (end == glyph
29211 && BUFFERP (end->object)
29212 && (end->charpos < start_charpos
29213 || end->charpos >= end_charpos))
29214 {
29215 x += end->pixel_width;
29216 ++end;
29217 }
29218 hlinfo->mouse_face_end_x = x;
29219 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
29220 }
29221
29222 hlinfo->mouse_face_window = window;
29223 hlinfo->mouse_face_face_id
29224 = face_at_buffer_position (w, mouse_charpos, &ignore,
29225 mouse_charpos + 1,
29226 !hlinfo->mouse_face_hidden, -1);
29227 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29228 }
29229
29230 /* The following function is not used anymore (replaced with
29231 mouse_face_from_string_pos), but I leave it here for the time
29232 being, in case someone would. */
29233
29234 #if false /* not used */
29235
29236 /* Find the position of the glyph for position POS in OBJECT in
29237 window W's current matrix, and return in *X, *Y the pixel
29238 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
29239
29240 RIGHT_P means return the position of the right edge of the glyph.
29241 !RIGHT_P means return the left edge position.
29242
29243 If no glyph for POS exists in the matrix, return the position of
29244 the glyph with the next smaller position that is in the matrix, if
29245 RIGHT_P is false. If RIGHT_P, and no glyph for POS
29246 exists in the matrix, return the position of the glyph with the
29247 next larger position in OBJECT.
29248
29249 Value is true if a glyph was found. */
29250
29251 static bool
29252 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
29253 int *hpos, int *vpos, int *x, int *y, bool right_p)
29254 {
29255 int yb = window_text_bottom_y (w);
29256 struct glyph_row *r;
29257 struct glyph *best_glyph = NULL;
29258 struct glyph_row *best_row = NULL;
29259 int best_x = 0;
29260
29261 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29262 r->enabled_p && r->y < yb;
29263 ++r)
29264 {
29265 struct glyph *g = r->glyphs[TEXT_AREA];
29266 struct glyph *e = g + r->used[TEXT_AREA];
29267 int gx;
29268
29269 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29270 if (EQ (g->object, object))
29271 {
29272 if (g->charpos == pos)
29273 {
29274 best_glyph = g;
29275 best_x = gx;
29276 best_row = r;
29277 goto found;
29278 }
29279 else if (best_glyph == NULL
29280 || ((eabs (g->charpos - pos)
29281 < eabs (best_glyph->charpos - pos))
29282 && (right_p
29283 ? g->charpos < pos
29284 : g->charpos > pos)))
29285 {
29286 best_glyph = g;
29287 best_x = gx;
29288 best_row = r;
29289 }
29290 }
29291 }
29292
29293 found:
29294
29295 if (best_glyph)
29296 {
29297 *x = best_x;
29298 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
29299
29300 if (right_p)
29301 {
29302 *x += best_glyph->pixel_width;
29303 ++*hpos;
29304 }
29305
29306 *y = best_row->y;
29307 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
29308 }
29309
29310 return best_glyph != NULL;
29311 }
29312 #endif /* not used */
29313
29314 /* Find the positions of the first and the last glyphs in window W's
29315 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29316 (assumed to be a string), and return in HLINFO's mouse_face_*
29317 members the pixel and column/row coordinates of those glyphs. */
29318
29319 static void
29320 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29321 Lisp_Object object,
29322 ptrdiff_t startpos, ptrdiff_t endpos)
29323 {
29324 int yb = window_text_bottom_y (w);
29325 struct glyph_row *r;
29326 struct glyph *g, *e;
29327 int gx;
29328 bool found = false;
29329
29330 /* Find the glyph row with at least one position in the range
29331 [STARTPOS..ENDPOS), and the first glyph in that row whose
29332 position belongs to that range. */
29333 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29334 r->enabled_p && r->y < yb;
29335 ++r)
29336 {
29337 if (!r->reversed_p)
29338 {
29339 g = r->glyphs[TEXT_AREA];
29340 e = g + r->used[TEXT_AREA];
29341 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29342 if (EQ (g->object, object)
29343 && startpos <= g->charpos && g->charpos < endpos)
29344 {
29345 hlinfo->mouse_face_beg_row
29346 = MATRIX_ROW_VPOS (r, w->current_matrix);
29347 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29348 hlinfo->mouse_face_beg_x = gx;
29349 found = true;
29350 break;
29351 }
29352 }
29353 else
29354 {
29355 struct glyph *g1;
29356
29357 e = r->glyphs[TEXT_AREA];
29358 g = e + r->used[TEXT_AREA];
29359 for ( ; g > e; --g)
29360 if (EQ ((g-1)->object, object)
29361 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29362 {
29363 hlinfo->mouse_face_beg_row
29364 = MATRIX_ROW_VPOS (r, w->current_matrix);
29365 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29366 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29367 gx += g1->pixel_width;
29368 hlinfo->mouse_face_beg_x = gx;
29369 found = true;
29370 break;
29371 }
29372 }
29373 if (found)
29374 break;
29375 }
29376
29377 if (!found)
29378 return;
29379
29380 /* Starting with the next row, look for the first row which does NOT
29381 include any glyphs whose positions are in the range. */
29382 for (++r; r->enabled_p && r->y < yb; ++r)
29383 {
29384 g = r->glyphs[TEXT_AREA];
29385 e = g + r->used[TEXT_AREA];
29386 found = false;
29387 for ( ; g < e; ++g)
29388 if (EQ (g->object, object)
29389 && startpos <= g->charpos && g->charpos < endpos)
29390 {
29391 found = true;
29392 break;
29393 }
29394 if (!found)
29395 break;
29396 }
29397
29398 /* The highlighted region ends on the previous row. */
29399 r--;
29400
29401 /* Set the end row. */
29402 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29403
29404 /* Compute and set the end column and the end column's horizontal
29405 pixel coordinate. */
29406 if (!r->reversed_p)
29407 {
29408 g = r->glyphs[TEXT_AREA];
29409 e = g + r->used[TEXT_AREA];
29410 for ( ; e > g; --e)
29411 if (EQ ((e-1)->object, object)
29412 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29413 break;
29414 hlinfo->mouse_face_end_col = e - g;
29415
29416 for (gx = r->x; g < e; ++g)
29417 gx += g->pixel_width;
29418 hlinfo->mouse_face_end_x = gx;
29419 }
29420 else
29421 {
29422 e = r->glyphs[TEXT_AREA];
29423 g = e + r->used[TEXT_AREA];
29424 for (gx = r->x ; e < g; ++e)
29425 {
29426 if (EQ (e->object, object)
29427 && startpos <= e->charpos && e->charpos < endpos)
29428 break;
29429 gx += e->pixel_width;
29430 }
29431 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29432 hlinfo->mouse_face_end_x = gx;
29433 }
29434 }
29435
29436 #ifdef HAVE_WINDOW_SYSTEM
29437
29438 /* See if position X, Y is within a hot-spot of an image. */
29439
29440 static bool
29441 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29442 {
29443 if (!CONSP (hot_spot))
29444 return false;
29445
29446 if (EQ (XCAR (hot_spot), Qrect))
29447 {
29448 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29449 Lisp_Object rect = XCDR (hot_spot);
29450 Lisp_Object tem;
29451 if (!CONSP (rect))
29452 return false;
29453 if (!CONSP (XCAR (rect)))
29454 return false;
29455 if (!CONSP (XCDR (rect)))
29456 return false;
29457 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29458 return false;
29459 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29460 return false;
29461 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29462 return false;
29463 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29464 return false;
29465 return true;
29466 }
29467 else if (EQ (XCAR (hot_spot), Qcircle))
29468 {
29469 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29470 Lisp_Object circ = XCDR (hot_spot);
29471 Lisp_Object lr, lx0, ly0;
29472 if (CONSP (circ)
29473 && CONSP (XCAR (circ))
29474 && (lr = XCDR (circ), NUMBERP (lr))
29475 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29476 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29477 {
29478 double r = XFLOATINT (lr);
29479 double dx = XINT (lx0) - x;
29480 double dy = XINT (ly0) - y;
29481 return (dx * dx + dy * dy <= r * r);
29482 }
29483 }
29484 else if (EQ (XCAR (hot_spot), Qpoly))
29485 {
29486 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29487 if (VECTORP (XCDR (hot_spot)))
29488 {
29489 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29490 Lisp_Object *poly = v->contents;
29491 ptrdiff_t n = v->header.size;
29492 ptrdiff_t i;
29493 bool inside = false;
29494 Lisp_Object lx, ly;
29495 int x0, y0;
29496
29497 /* Need an even number of coordinates, and at least 3 edges. */
29498 if (n < 6 || n & 1)
29499 return false;
29500
29501 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29502 If count is odd, we are inside polygon. Pixels on edges
29503 may or may not be included depending on actual geometry of the
29504 polygon. */
29505 if ((lx = poly[n-2], !INTEGERP (lx))
29506 || (ly = poly[n-1], !INTEGERP (lx)))
29507 return false;
29508 x0 = XINT (lx), y0 = XINT (ly);
29509 for (i = 0; i < n; i += 2)
29510 {
29511 int x1 = x0, y1 = y0;
29512 if ((lx = poly[i], !INTEGERP (lx))
29513 || (ly = poly[i+1], !INTEGERP (ly)))
29514 return false;
29515 x0 = XINT (lx), y0 = XINT (ly);
29516
29517 /* Does this segment cross the X line? */
29518 if (x0 >= x)
29519 {
29520 if (x1 >= x)
29521 continue;
29522 }
29523 else if (x1 < x)
29524 continue;
29525 if (y > y0 && y > y1)
29526 continue;
29527 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29528 inside = !inside;
29529 }
29530 return inside;
29531 }
29532 }
29533 return false;
29534 }
29535
29536 Lisp_Object
29537 find_hot_spot (Lisp_Object map, int x, int y)
29538 {
29539 while (CONSP (map))
29540 {
29541 if (CONSP (XCAR (map))
29542 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29543 return XCAR (map);
29544 map = XCDR (map);
29545 }
29546
29547 return Qnil;
29548 }
29549
29550 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29551 3, 3, 0,
29552 doc: /* Lookup in image map MAP coordinates X and Y.
29553 An image map is an alist where each element has the format (AREA ID PLIST).
29554 An AREA is specified as either a rectangle, a circle, or a polygon:
29555 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29556 pixel coordinates of the upper left and bottom right corners.
29557 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29558 and the radius of the circle; r may be a float or integer.
29559 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29560 vector describes one corner in the polygon.
29561 Returns the alist element for the first matching AREA in MAP. */)
29562 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29563 {
29564 if (NILP (map))
29565 return Qnil;
29566
29567 CHECK_NUMBER (x);
29568 CHECK_NUMBER (y);
29569
29570 return find_hot_spot (map,
29571 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29572 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29573 }
29574
29575
29576 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29577 static void
29578 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29579 {
29580 /* Do not change cursor shape while dragging mouse. */
29581 if (EQ (do_mouse_tracking, Qdragging))
29582 return;
29583
29584 if (!NILP (pointer))
29585 {
29586 if (EQ (pointer, Qarrow))
29587 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29588 else if (EQ (pointer, Qhand))
29589 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29590 else if (EQ (pointer, Qtext))
29591 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29592 else if (EQ (pointer, intern ("hdrag")))
29593 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29594 else if (EQ (pointer, intern ("nhdrag")))
29595 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29596 #ifdef HAVE_X_WINDOWS
29597 else if (EQ (pointer, intern ("vdrag")))
29598 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29599 #endif
29600 else if (EQ (pointer, intern ("hourglass")))
29601 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29602 else if (EQ (pointer, Qmodeline))
29603 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29604 else
29605 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29606 }
29607
29608 if (cursor != No_Cursor)
29609 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29610 }
29611
29612 #endif /* HAVE_WINDOW_SYSTEM */
29613
29614 /* Take proper action when mouse has moved to the mode or header line
29615 or marginal area AREA of window W, x-position X and y-position Y.
29616 X is relative to the start of the text display area of W, so the
29617 width of bitmap areas and scroll bars must be subtracted to get a
29618 position relative to the start of the mode line. */
29619
29620 static void
29621 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29622 enum window_part area)
29623 {
29624 struct window *w = XWINDOW (window);
29625 struct frame *f = XFRAME (w->frame);
29626 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29627 #ifdef HAVE_WINDOW_SYSTEM
29628 Display_Info *dpyinfo;
29629 #endif
29630 Cursor cursor = No_Cursor;
29631 Lisp_Object pointer = Qnil;
29632 int dx, dy, width, height;
29633 ptrdiff_t charpos;
29634 Lisp_Object string, object = Qnil;
29635 Lisp_Object pos IF_LINT (= Qnil), help;
29636
29637 Lisp_Object mouse_face;
29638 int original_x_pixel = x;
29639 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29640 struct glyph_row *row IF_LINT (= 0);
29641
29642 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29643 {
29644 int x0;
29645 struct glyph *end;
29646
29647 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29648 returns them in row/column units! */
29649 string = mode_line_string (w, area, &x, &y, &charpos,
29650 &object, &dx, &dy, &width, &height);
29651
29652 row = (area == ON_MODE_LINE
29653 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29654 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29655
29656 /* Find the glyph under the mouse pointer. */
29657 if (row->mode_line_p && row->enabled_p)
29658 {
29659 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29660 end = glyph + row->used[TEXT_AREA];
29661
29662 for (x0 = original_x_pixel;
29663 glyph < end && x0 >= glyph->pixel_width;
29664 ++glyph)
29665 x0 -= glyph->pixel_width;
29666
29667 if (glyph >= end)
29668 glyph = NULL;
29669 }
29670 }
29671 else
29672 {
29673 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29674 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29675 returns them in row/column units! */
29676 string = marginal_area_string (w, area, &x, &y, &charpos,
29677 &object, &dx, &dy, &width, &height);
29678 }
29679
29680 help = Qnil;
29681
29682 #ifdef HAVE_WINDOW_SYSTEM
29683 if (IMAGEP (object))
29684 {
29685 Lisp_Object image_map, hotspot;
29686 if ((image_map = Fplist_get (XCDR (object), QCmap),
29687 !NILP (image_map))
29688 && (hotspot = find_hot_spot (image_map, dx, dy),
29689 CONSP (hotspot))
29690 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29691 {
29692 Lisp_Object plist;
29693
29694 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29695 If so, we could look for mouse-enter, mouse-leave
29696 properties in PLIST (and do something...). */
29697 hotspot = XCDR (hotspot);
29698 if (CONSP (hotspot)
29699 && (plist = XCAR (hotspot), CONSP (plist)))
29700 {
29701 pointer = Fplist_get (plist, Qpointer);
29702 if (NILP (pointer))
29703 pointer = Qhand;
29704 help = Fplist_get (plist, Qhelp_echo);
29705 if (!NILP (help))
29706 {
29707 help_echo_string = help;
29708 XSETWINDOW (help_echo_window, w);
29709 help_echo_object = w->contents;
29710 help_echo_pos = charpos;
29711 }
29712 }
29713 }
29714 if (NILP (pointer))
29715 pointer = Fplist_get (XCDR (object), QCpointer);
29716 }
29717 #endif /* HAVE_WINDOW_SYSTEM */
29718
29719 if (STRINGP (string))
29720 pos = make_number (charpos);
29721
29722 /* Set the help text and mouse pointer. If the mouse is on a part
29723 of the mode line without any text (e.g. past the right edge of
29724 the mode line text), use the default help text and pointer. */
29725 if (STRINGP (string) || area == ON_MODE_LINE)
29726 {
29727 /* Arrange to display the help by setting the global variables
29728 help_echo_string, help_echo_object, and help_echo_pos. */
29729 if (NILP (help))
29730 {
29731 if (STRINGP (string))
29732 help = Fget_text_property (pos, Qhelp_echo, string);
29733
29734 if (!NILP (help))
29735 {
29736 help_echo_string = help;
29737 XSETWINDOW (help_echo_window, w);
29738 help_echo_object = string;
29739 help_echo_pos = charpos;
29740 }
29741 else if (area == ON_MODE_LINE)
29742 {
29743 Lisp_Object default_help
29744 = buffer_local_value (Qmode_line_default_help_echo,
29745 w->contents);
29746
29747 if (STRINGP (default_help))
29748 {
29749 help_echo_string = default_help;
29750 XSETWINDOW (help_echo_window, w);
29751 help_echo_object = Qnil;
29752 help_echo_pos = -1;
29753 }
29754 }
29755 }
29756
29757 #ifdef HAVE_WINDOW_SYSTEM
29758 /* Change the mouse pointer according to what is under it. */
29759 if (FRAME_WINDOW_P (f))
29760 {
29761 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29762 || minibuf_level
29763 || NILP (Vresize_mini_windows));
29764
29765 dpyinfo = FRAME_DISPLAY_INFO (f);
29766 if (STRINGP (string))
29767 {
29768 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29769
29770 if (NILP (pointer))
29771 pointer = Fget_text_property (pos, Qpointer, string);
29772
29773 /* Change the mouse pointer according to what is under X/Y. */
29774 if (NILP (pointer)
29775 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29776 {
29777 Lisp_Object map;
29778 map = Fget_text_property (pos, Qlocal_map, string);
29779 if (!KEYMAPP (map))
29780 map = Fget_text_property (pos, Qkeymap, string);
29781 if (!KEYMAPP (map) && draggable)
29782 cursor = dpyinfo->vertical_scroll_bar_cursor;
29783 }
29784 }
29785 else if (draggable)
29786 /* Default mode-line pointer. */
29787 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29788 }
29789 #endif
29790 }
29791
29792 /* Change the mouse face according to what is under X/Y. */
29793 bool mouse_face_shown = false;
29794 if (STRINGP (string))
29795 {
29796 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29797 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29798 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29799 && glyph)
29800 {
29801 Lisp_Object b, e;
29802
29803 struct glyph * tmp_glyph;
29804
29805 int gpos;
29806 int gseq_length;
29807 int total_pixel_width;
29808 ptrdiff_t begpos, endpos, ignore;
29809
29810 int vpos, hpos;
29811
29812 b = Fprevious_single_property_change (make_number (charpos + 1),
29813 Qmouse_face, string, Qnil);
29814 if (NILP (b))
29815 begpos = 0;
29816 else
29817 begpos = XINT (b);
29818
29819 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29820 if (NILP (e))
29821 endpos = SCHARS (string);
29822 else
29823 endpos = XINT (e);
29824
29825 /* Calculate the glyph position GPOS of GLYPH in the
29826 displayed string, relative to the beginning of the
29827 highlighted part of the string.
29828
29829 Note: GPOS is different from CHARPOS. CHARPOS is the
29830 position of GLYPH in the internal string object. A mode
29831 line string format has structures which are converted to
29832 a flattened string by the Emacs Lisp interpreter. The
29833 internal string is an element of those structures. The
29834 displayed string is the flattened string. */
29835 tmp_glyph = row_start_glyph;
29836 while (tmp_glyph < glyph
29837 && (!(EQ (tmp_glyph->object, glyph->object)
29838 && begpos <= tmp_glyph->charpos
29839 && tmp_glyph->charpos < endpos)))
29840 tmp_glyph++;
29841 gpos = glyph - tmp_glyph;
29842
29843 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29844 the highlighted part of the displayed string to which
29845 GLYPH belongs. Note: GSEQ_LENGTH is different from
29846 SCHARS (STRING), because the latter returns the length of
29847 the internal string. */
29848 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29849 tmp_glyph > glyph
29850 && (!(EQ (tmp_glyph->object, glyph->object)
29851 && begpos <= tmp_glyph->charpos
29852 && tmp_glyph->charpos < endpos));
29853 tmp_glyph--)
29854 ;
29855 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29856
29857 /* Calculate the total pixel width of all the glyphs between
29858 the beginning of the highlighted area and GLYPH. */
29859 total_pixel_width = 0;
29860 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29861 total_pixel_width += tmp_glyph->pixel_width;
29862
29863 /* Pre calculation of re-rendering position. Note: X is in
29864 column units here, after the call to mode_line_string or
29865 marginal_area_string. */
29866 hpos = x - gpos;
29867 vpos = (area == ON_MODE_LINE
29868 ? (w->current_matrix)->nrows - 1
29869 : 0);
29870
29871 /* If GLYPH's position is included in the region that is
29872 already drawn in mouse face, we have nothing to do. */
29873 if ( EQ (window, hlinfo->mouse_face_window)
29874 && (!row->reversed_p
29875 ? (hlinfo->mouse_face_beg_col <= hpos
29876 && hpos < hlinfo->mouse_face_end_col)
29877 /* In R2L rows we swap BEG and END, see below. */
29878 : (hlinfo->mouse_face_end_col <= hpos
29879 && hpos < hlinfo->mouse_face_beg_col))
29880 && hlinfo->mouse_face_beg_row == vpos )
29881 return;
29882
29883 if (clear_mouse_face (hlinfo))
29884 cursor = No_Cursor;
29885
29886 if (!row->reversed_p)
29887 {
29888 hlinfo->mouse_face_beg_col = hpos;
29889 hlinfo->mouse_face_beg_x = original_x_pixel
29890 - (total_pixel_width + dx);
29891 hlinfo->mouse_face_end_col = hpos + gseq_length;
29892 hlinfo->mouse_face_end_x = 0;
29893 }
29894 else
29895 {
29896 /* In R2L rows, show_mouse_face expects BEG and END
29897 coordinates to be swapped. */
29898 hlinfo->mouse_face_end_col = hpos;
29899 hlinfo->mouse_face_end_x = original_x_pixel
29900 - (total_pixel_width + dx);
29901 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29902 hlinfo->mouse_face_beg_x = 0;
29903 }
29904
29905 hlinfo->mouse_face_beg_row = vpos;
29906 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29907 hlinfo->mouse_face_past_end = false;
29908 hlinfo->mouse_face_window = window;
29909
29910 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29911 charpos,
29912 0, &ignore,
29913 glyph->face_id,
29914 true);
29915 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29916 mouse_face_shown = true;
29917
29918 if (NILP (pointer))
29919 pointer = Qhand;
29920 }
29921 }
29922
29923 /* If mouse-face doesn't need to be shown, clear any existing
29924 mouse-face. */
29925 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
29926 clear_mouse_face (hlinfo);
29927
29928 #ifdef HAVE_WINDOW_SYSTEM
29929 if (FRAME_WINDOW_P (f))
29930 define_frame_cursor1 (f, cursor, pointer);
29931 #endif
29932 }
29933
29934
29935 /* EXPORT:
29936 Take proper action when the mouse has moved to position X, Y on
29937 frame F with regards to highlighting portions of display that have
29938 mouse-face properties. Also de-highlight portions of display where
29939 the mouse was before, set the mouse pointer shape as appropriate
29940 for the mouse coordinates, and activate help echo (tooltips).
29941 X and Y can be negative or out of range. */
29942
29943 void
29944 note_mouse_highlight (struct frame *f, int x, int y)
29945 {
29946 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29947 enum window_part part = ON_NOTHING;
29948 Lisp_Object window;
29949 struct window *w;
29950 Cursor cursor = No_Cursor;
29951 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29952 struct buffer *b;
29953
29954 /* When a menu is active, don't highlight because this looks odd. */
29955 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29956 if (popup_activated ())
29957 return;
29958 #endif
29959
29960 if (!f->glyphs_initialized_p
29961 || f->pointer_invisible)
29962 return;
29963
29964 hlinfo->mouse_face_mouse_x = x;
29965 hlinfo->mouse_face_mouse_y = y;
29966 hlinfo->mouse_face_mouse_frame = f;
29967
29968 if (hlinfo->mouse_face_defer)
29969 return;
29970
29971 /* Which window is that in? */
29972 window = window_from_coordinates (f, x, y, &part, true);
29973
29974 /* If displaying active text in another window, clear that. */
29975 if (! EQ (window, hlinfo->mouse_face_window)
29976 /* Also clear if we move out of text area in same window. */
29977 || (!NILP (hlinfo->mouse_face_window)
29978 && !NILP (window)
29979 && part != ON_TEXT
29980 && part != ON_MODE_LINE
29981 && part != ON_HEADER_LINE))
29982 clear_mouse_face (hlinfo);
29983
29984 /* Not on a window -> return. */
29985 if (!WINDOWP (window))
29986 return;
29987
29988 /* Reset help_echo_string. It will get recomputed below. */
29989 help_echo_string = Qnil;
29990
29991 /* Convert to window-relative pixel coordinates. */
29992 w = XWINDOW (window);
29993 frame_to_window_pixel_xy (w, &x, &y);
29994
29995 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29996 /* Handle tool-bar window differently since it doesn't display a
29997 buffer. */
29998 if (EQ (window, f->tool_bar_window))
29999 {
30000 note_tool_bar_highlight (f, x, y);
30001 return;
30002 }
30003 #endif
30004
30005 /* Mouse is on the mode, header line or margin? */
30006 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
30007 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30008 {
30009 note_mode_line_or_margin_highlight (window, x, y, part);
30010
30011 #ifdef HAVE_WINDOW_SYSTEM
30012 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30013 {
30014 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30015 /* Show non-text cursor (Bug#16647). */
30016 goto set_cursor;
30017 }
30018 else
30019 #endif
30020 return;
30021 }
30022
30023 #ifdef HAVE_WINDOW_SYSTEM
30024 if (part == ON_VERTICAL_BORDER)
30025 {
30026 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30027 help_echo_string = build_string ("drag-mouse-1: resize");
30028 }
30029 else if (part == ON_RIGHT_DIVIDER)
30030 {
30031 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30032 help_echo_string = build_string ("drag-mouse-1: resize");
30033 }
30034 else if (part == ON_BOTTOM_DIVIDER)
30035 if (! WINDOW_BOTTOMMOST_P (w)
30036 || minibuf_level
30037 || NILP (Vresize_mini_windows))
30038 {
30039 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30040 help_echo_string = build_string ("drag-mouse-1: resize");
30041 }
30042 else
30043 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30044 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
30045 || part == ON_VERTICAL_SCROLL_BAR
30046 || part == ON_HORIZONTAL_SCROLL_BAR)
30047 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30048 else
30049 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30050 #endif
30051
30052 /* Are we in a window whose display is up to date?
30053 And verify the buffer's text has not changed. */
30054 b = XBUFFER (w->contents);
30055 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
30056 {
30057 int hpos, vpos, dx, dy, area = LAST_AREA;
30058 ptrdiff_t pos;
30059 struct glyph *glyph;
30060 Lisp_Object object;
30061 Lisp_Object mouse_face = Qnil, position;
30062 Lisp_Object *overlay_vec = NULL;
30063 ptrdiff_t i, noverlays;
30064 struct buffer *obuf;
30065 ptrdiff_t obegv, ozv;
30066 bool same_region;
30067
30068 /* Find the glyph under X/Y. */
30069 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
30070
30071 #ifdef HAVE_WINDOW_SYSTEM
30072 /* Look for :pointer property on image. */
30073 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
30074 {
30075 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
30076 if (img != NULL && IMAGEP (img->spec))
30077 {
30078 Lisp_Object image_map, hotspot;
30079 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
30080 !NILP (image_map))
30081 && (hotspot = find_hot_spot (image_map,
30082 glyph->slice.img.x + dx,
30083 glyph->slice.img.y + dy),
30084 CONSP (hotspot))
30085 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30086 {
30087 Lisp_Object plist;
30088
30089 /* Could check XCAR (hotspot) to see if we enter/leave
30090 this hot-spot.
30091 If so, we could look for mouse-enter, mouse-leave
30092 properties in PLIST (and do something...). */
30093 hotspot = XCDR (hotspot);
30094 if (CONSP (hotspot)
30095 && (plist = XCAR (hotspot), CONSP (plist)))
30096 {
30097 pointer = Fplist_get (plist, Qpointer);
30098 if (NILP (pointer))
30099 pointer = Qhand;
30100 help_echo_string = Fplist_get (plist, Qhelp_echo);
30101 if (!NILP (help_echo_string))
30102 {
30103 help_echo_window = window;
30104 help_echo_object = glyph->object;
30105 help_echo_pos = glyph->charpos;
30106 }
30107 }
30108 }
30109 if (NILP (pointer))
30110 pointer = Fplist_get (XCDR (img->spec), QCpointer);
30111 }
30112 }
30113 #endif /* HAVE_WINDOW_SYSTEM */
30114
30115 /* Clear mouse face if X/Y not over text. */
30116 if (glyph == NULL
30117 || area != TEXT_AREA
30118 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
30119 /* Glyph's OBJECT is nil for glyphs inserted by the
30120 display engine for its internal purposes, like truncation
30121 and continuation glyphs and blanks beyond the end of
30122 line's text on text terminals. If we are over such a
30123 glyph, we are not over any text. */
30124 || NILP (glyph->object)
30125 /* R2L rows have a stretch glyph at their front, which
30126 stands for no text, whereas L2R rows have no glyphs at
30127 all beyond the end of text. Treat such stretch glyphs
30128 like we do with NULL glyphs in L2R rows. */
30129 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
30130 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
30131 && glyph->type == STRETCH_GLYPH
30132 && glyph->avoid_cursor_p))
30133 {
30134 if (clear_mouse_face (hlinfo))
30135 cursor = No_Cursor;
30136 #ifdef HAVE_WINDOW_SYSTEM
30137 if (FRAME_WINDOW_P (f) && NILP (pointer))
30138 {
30139 if (area != TEXT_AREA)
30140 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30141 else
30142 pointer = Vvoid_text_area_pointer;
30143 }
30144 #endif
30145 goto set_cursor;
30146 }
30147
30148 pos = glyph->charpos;
30149 object = glyph->object;
30150 if (!STRINGP (object) && !BUFFERP (object))
30151 goto set_cursor;
30152
30153 /* If we get an out-of-range value, return now; avoid an error. */
30154 if (BUFFERP (object) && pos > BUF_Z (b))
30155 goto set_cursor;
30156
30157 /* Make the window's buffer temporarily current for
30158 overlays_at and compute_char_face. */
30159 obuf = current_buffer;
30160 current_buffer = b;
30161 obegv = BEGV;
30162 ozv = ZV;
30163 BEGV = BEG;
30164 ZV = Z;
30165
30166 /* Is this char mouse-active or does it have help-echo? */
30167 position = make_number (pos);
30168
30169 USE_SAFE_ALLOCA;
30170
30171 if (BUFFERP (object))
30172 {
30173 /* Put all the overlays we want in a vector in overlay_vec. */
30174 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
30175 /* Sort overlays into increasing priority order. */
30176 noverlays = sort_overlays (overlay_vec, noverlays, w);
30177 }
30178 else
30179 noverlays = 0;
30180
30181 if (NILP (Vmouse_highlight))
30182 {
30183 clear_mouse_face (hlinfo);
30184 goto check_help_echo;
30185 }
30186
30187 same_region = coords_in_mouse_face_p (w, hpos, vpos);
30188
30189 if (same_region)
30190 cursor = No_Cursor;
30191
30192 /* Check mouse-face highlighting. */
30193 if (! same_region
30194 /* If there exists an overlay with mouse-face overlapping
30195 the one we are currently highlighting, we have to
30196 check if we enter the overlapping overlay, and then
30197 highlight only that. */
30198 || (OVERLAYP (hlinfo->mouse_face_overlay)
30199 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
30200 {
30201 /* Find the highest priority overlay with a mouse-face. */
30202 Lisp_Object overlay = Qnil;
30203 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
30204 {
30205 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
30206 if (!NILP (mouse_face))
30207 overlay = overlay_vec[i];
30208 }
30209
30210 /* If we're highlighting the same overlay as before, there's
30211 no need to do that again. */
30212 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
30213 goto check_help_echo;
30214 hlinfo->mouse_face_overlay = overlay;
30215
30216 /* Clear the display of the old active region, if any. */
30217 if (clear_mouse_face (hlinfo))
30218 cursor = No_Cursor;
30219
30220 /* If no overlay applies, get a text property. */
30221 if (NILP (overlay))
30222 mouse_face = Fget_text_property (position, Qmouse_face, object);
30223
30224 /* Next, compute the bounds of the mouse highlighting and
30225 display it. */
30226 if (!NILP (mouse_face) && STRINGP (object))
30227 {
30228 /* The mouse-highlighting comes from a display string
30229 with a mouse-face. */
30230 Lisp_Object s, e;
30231 ptrdiff_t ignore;
30232
30233 s = Fprevious_single_property_change
30234 (make_number (pos + 1), Qmouse_face, object, Qnil);
30235 e = Fnext_single_property_change
30236 (position, Qmouse_face, object, Qnil);
30237 if (NILP (s))
30238 s = make_number (0);
30239 if (NILP (e))
30240 e = make_number (SCHARS (object));
30241 mouse_face_from_string_pos (w, hlinfo, object,
30242 XINT (s), XINT (e));
30243 hlinfo->mouse_face_past_end = false;
30244 hlinfo->mouse_face_window = window;
30245 hlinfo->mouse_face_face_id
30246 = face_at_string_position (w, object, pos, 0, &ignore,
30247 glyph->face_id, true);
30248 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30249 cursor = No_Cursor;
30250 }
30251 else
30252 {
30253 /* The mouse-highlighting, if any, comes from an overlay
30254 or text property in the buffer. */
30255 Lisp_Object buffer IF_LINT (= Qnil);
30256 Lisp_Object disp_string IF_LINT (= Qnil);
30257
30258 if (STRINGP (object))
30259 {
30260 /* If we are on a display string with no mouse-face,
30261 check if the text under it has one. */
30262 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
30263 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30264 pos = string_buffer_position (object, start);
30265 if (pos > 0)
30266 {
30267 mouse_face = get_char_property_and_overlay
30268 (make_number (pos), Qmouse_face, w->contents, &overlay);
30269 buffer = w->contents;
30270 disp_string = object;
30271 }
30272 }
30273 else
30274 {
30275 buffer = object;
30276 disp_string = Qnil;
30277 }
30278
30279 if (!NILP (mouse_face))
30280 {
30281 Lisp_Object before, after;
30282 Lisp_Object before_string, after_string;
30283 /* To correctly find the limits of mouse highlight
30284 in a bidi-reordered buffer, we must not use the
30285 optimization of limiting the search in
30286 previous-single-property-change and
30287 next-single-property-change, because
30288 rows_from_pos_range needs the real start and end
30289 positions to DTRT in this case. That's because
30290 the first row visible in a window does not
30291 necessarily display the character whose position
30292 is the smallest. */
30293 Lisp_Object lim1
30294 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30295 ? Fmarker_position (w->start)
30296 : Qnil;
30297 Lisp_Object lim2
30298 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30299 ? make_number (BUF_Z (XBUFFER (buffer))
30300 - w->window_end_pos)
30301 : Qnil;
30302
30303 if (NILP (overlay))
30304 {
30305 /* Handle the text property case. */
30306 before = Fprevious_single_property_change
30307 (make_number (pos + 1), Qmouse_face, buffer, lim1);
30308 after = Fnext_single_property_change
30309 (make_number (pos), Qmouse_face, buffer, lim2);
30310 before_string = after_string = Qnil;
30311 }
30312 else
30313 {
30314 /* Handle the overlay case. */
30315 before = Foverlay_start (overlay);
30316 after = Foverlay_end (overlay);
30317 before_string = Foverlay_get (overlay, Qbefore_string);
30318 after_string = Foverlay_get (overlay, Qafter_string);
30319
30320 if (!STRINGP (before_string)) before_string = Qnil;
30321 if (!STRINGP (after_string)) after_string = Qnil;
30322 }
30323
30324 mouse_face_from_buffer_pos (window, hlinfo, pos,
30325 NILP (before)
30326 ? 1
30327 : XFASTINT (before),
30328 NILP (after)
30329 ? BUF_Z (XBUFFER (buffer))
30330 : XFASTINT (after),
30331 before_string, after_string,
30332 disp_string);
30333 cursor = No_Cursor;
30334 }
30335 }
30336 }
30337
30338 check_help_echo:
30339
30340 /* Look for a `help-echo' property. */
30341 if (NILP (help_echo_string)) {
30342 Lisp_Object help, overlay;
30343
30344 /* Check overlays first. */
30345 help = overlay = Qnil;
30346 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30347 {
30348 overlay = overlay_vec[i];
30349 help = Foverlay_get (overlay, Qhelp_echo);
30350 }
30351
30352 if (!NILP (help))
30353 {
30354 help_echo_string = help;
30355 help_echo_window = window;
30356 help_echo_object = overlay;
30357 help_echo_pos = pos;
30358 }
30359 else
30360 {
30361 Lisp_Object obj = glyph->object;
30362 ptrdiff_t charpos = glyph->charpos;
30363
30364 /* Try text properties. */
30365 if (STRINGP (obj)
30366 && charpos >= 0
30367 && charpos < SCHARS (obj))
30368 {
30369 help = Fget_text_property (make_number (charpos),
30370 Qhelp_echo, obj);
30371 if (NILP (help))
30372 {
30373 /* If the string itself doesn't specify a help-echo,
30374 see if the buffer text ``under'' it does. */
30375 struct glyph_row *r
30376 = MATRIX_ROW (w->current_matrix, vpos);
30377 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30378 ptrdiff_t p = string_buffer_position (obj, start);
30379 if (p > 0)
30380 {
30381 help = Fget_char_property (make_number (p),
30382 Qhelp_echo, w->contents);
30383 if (!NILP (help))
30384 {
30385 charpos = p;
30386 obj = w->contents;
30387 }
30388 }
30389 }
30390 }
30391 else if (BUFFERP (obj)
30392 && charpos >= BEGV
30393 && charpos < ZV)
30394 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30395 obj);
30396
30397 if (!NILP (help))
30398 {
30399 help_echo_string = help;
30400 help_echo_window = window;
30401 help_echo_object = obj;
30402 help_echo_pos = charpos;
30403 }
30404 }
30405 }
30406
30407 #ifdef HAVE_WINDOW_SYSTEM
30408 /* Look for a `pointer' property. */
30409 if (FRAME_WINDOW_P (f) && NILP (pointer))
30410 {
30411 /* Check overlays first. */
30412 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30413 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30414
30415 if (NILP (pointer))
30416 {
30417 Lisp_Object obj = glyph->object;
30418 ptrdiff_t charpos = glyph->charpos;
30419
30420 /* Try text properties. */
30421 if (STRINGP (obj)
30422 && charpos >= 0
30423 && charpos < SCHARS (obj))
30424 {
30425 pointer = Fget_text_property (make_number (charpos),
30426 Qpointer, obj);
30427 if (NILP (pointer))
30428 {
30429 /* If the string itself doesn't specify a pointer,
30430 see if the buffer text ``under'' it does. */
30431 struct glyph_row *r
30432 = MATRIX_ROW (w->current_matrix, vpos);
30433 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30434 ptrdiff_t p = string_buffer_position (obj, start);
30435 if (p > 0)
30436 pointer = Fget_char_property (make_number (p),
30437 Qpointer, w->contents);
30438 }
30439 }
30440 else if (BUFFERP (obj)
30441 && charpos >= BEGV
30442 && charpos < ZV)
30443 pointer = Fget_text_property (make_number (charpos),
30444 Qpointer, obj);
30445 }
30446 }
30447 #endif /* HAVE_WINDOW_SYSTEM */
30448
30449 BEGV = obegv;
30450 ZV = ozv;
30451 current_buffer = obuf;
30452 SAFE_FREE ();
30453 }
30454
30455 set_cursor:
30456
30457 #ifdef HAVE_WINDOW_SYSTEM
30458 if (FRAME_WINDOW_P (f))
30459 define_frame_cursor1 (f, cursor, pointer);
30460 #else
30461 /* This is here to prevent a compiler error, about "label at end of
30462 compound statement". */
30463 return;
30464 #endif
30465 }
30466
30467
30468 /* EXPORT for RIF:
30469 Clear any mouse-face on window W. This function is part of the
30470 redisplay interface, and is called from try_window_id and similar
30471 functions to ensure the mouse-highlight is off. */
30472
30473 void
30474 x_clear_window_mouse_face (struct window *w)
30475 {
30476 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30477 Lisp_Object window;
30478
30479 block_input ();
30480 XSETWINDOW (window, w);
30481 if (EQ (window, hlinfo->mouse_face_window))
30482 clear_mouse_face (hlinfo);
30483 unblock_input ();
30484 }
30485
30486
30487 /* EXPORT:
30488 Just discard the mouse face information for frame F, if any.
30489 This is used when the size of F is changed. */
30490
30491 void
30492 cancel_mouse_face (struct frame *f)
30493 {
30494 Lisp_Object window;
30495 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30496
30497 window = hlinfo->mouse_face_window;
30498 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30499 reset_mouse_highlight (hlinfo);
30500 }
30501
30502
30503 \f
30504 /***********************************************************************
30505 Exposure Events
30506 ***********************************************************************/
30507
30508 #ifdef HAVE_WINDOW_SYSTEM
30509
30510 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30511 which intersects rectangle R. R is in window-relative coordinates. */
30512
30513 static void
30514 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30515 enum glyph_row_area area)
30516 {
30517 struct glyph *first = row->glyphs[area];
30518 struct glyph *end = row->glyphs[area] + row->used[area];
30519 struct glyph *last;
30520 int first_x, start_x, x;
30521
30522 if (area == TEXT_AREA && row->fill_line_p)
30523 /* If row extends face to end of line write the whole line. */
30524 draw_glyphs (w, 0, row, area,
30525 0, row->used[area],
30526 DRAW_NORMAL_TEXT, 0);
30527 else
30528 {
30529 /* Set START_X to the window-relative start position for drawing glyphs of
30530 AREA. The first glyph of the text area can be partially visible.
30531 The first glyphs of other areas cannot. */
30532 start_x = window_box_left_offset (w, area);
30533 x = start_x;
30534 if (area == TEXT_AREA)
30535 x += row->x;
30536
30537 /* Find the first glyph that must be redrawn. */
30538 while (first < end
30539 && x + first->pixel_width < r->x)
30540 {
30541 x += first->pixel_width;
30542 ++first;
30543 }
30544
30545 /* Find the last one. */
30546 last = first;
30547 first_x = x;
30548 /* Use a signed int intermediate value to avoid catastrophic
30549 failures due to comparison between signed and unsigned, when
30550 x is negative (can happen for wide images that are hscrolled). */
30551 int r_end = r->x + r->width;
30552 while (last < end && x < r_end)
30553 {
30554 x += last->pixel_width;
30555 ++last;
30556 }
30557
30558 /* Repaint. */
30559 if (last > first)
30560 draw_glyphs (w, first_x - start_x, row, area,
30561 first - row->glyphs[area], last - row->glyphs[area],
30562 DRAW_NORMAL_TEXT, 0);
30563 }
30564 }
30565
30566
30567 /* Redraw the parts of the glyph row ROW on window W intersecting
30568 rectangle R. R is in window-relative coordinates. Value is
30569 true if mouse-face was overwritten. */
30570
30571 static bool
30572 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30573 {
30574 eassert (row->enabled_p);
30575
30576 if (row->mode_line_p || w->pseudo_window_p)
30577 draw_glyphs (w, 0, row, TEXT_AREA,
30578 0, row->used[TEXT_AREA],
30579 DRAW_NORMAL_TEXT, 0);
30580 else
30581 {
30582 if (row->used[LEFT_MARGIN_AREA])
30583 expose_area (w, row, r, LEFT_MARGIN_AREA);
30584 if (row->used[TEXT_AREA])
30585 expose_area (w, row, r, TEXT_AREA);
30586 if (row->used[RIGHT_MARGIN_AREA])
30587 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30588 draw_row_fringe_bitmaps (w, row);
30589 }
30590
30591 return row->mouse_face_p;
30592 }
30593
30594
30595 /* Redraw those parts of glyphs rows during expose event handling that
30596 overlap other rows. Redrawing of an exposed line writes over parts
30597 of lines overlapping that exposed line; this function fixes that.
30598
30599 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30600 row in W's current matrix that is exposed and overlaps other rows.
30601 LAST_OVERLAPPING_ROW is the last such row. */
30602
30603 static void
30604 expose_overlaps (struct window *w,
30605 struct glyph_row *first_overlapping_row,
30606 struct glyph_row *last_overlapping_row,
30607 XRectangle *r)
30608 {
30609 struct glyph_row *row;
30610
30611 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30612 if (row->overlapping_p)
30613 {
30614 eassert (row->enabled_p && !row->mode_line_p);
30615
30616 row->clip = r;
30617 if (row->used[LEFT_MARGIN_AREA])
30618 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30619
30620 if (row->used[TEXT_AREA])
30621 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30622
30623 if (row->used[RIGHT_MARGIN_AREA])
30624 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30625 row->clip = NULL;
30626 }
30627 }
30628
30629
30630 /* Return true if W's cursor intersects rectangle R. */
30631
30632 static bool
30633 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30634 {
30635 XRectangle cr, result;
30636 struct glyph *cursor_glyph;
30637 struct glyph_row *row;
30638
30639 if (w->phys_cursor.vpos >= 0
30640 && w->phys_cursor.vpos < w->current_matrix->nrows
30641 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30642 row->enabled_p)
30643 && row->cursor_in_fringe_p)
30644 {
30645 /* Cursor is in the fringe. */
30646 cr.x = window_box_right_offset (w,
30647 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30648 ? RIGHT_MARGIN_AREA
30649 : TEXT_AREA));
30650 cr.y = row->y;
30651 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30652 cr.height = row->height;
30653 return x_intersect_rectangles (&cr, r, &result);
30654 }
30655
30656 cursor_glyph = get_phys_cursor_glyph (w);
30657 if (cursor_glyph)
30658 {
30659 /* r is relative to W's box, but w->phys_cursor.x is relative
30660 to left edge of W's TEXT area. Adjust it. */
30661 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30662 cr.y = w->phys_cursor.y;
30663 cr.width = cursor_glyph->pixel_width;
30664 cr.height = w->phys_cursor_height;
30665 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30666 I assume the effect is the same -- and this is portable. */
30667 return x_intersect_rectangles (&cr, r, &result);
30668 }
30669 /* If we don't understand the format, pretend we're not in the hot-spot. */
30670 return false;
30671 }
30672
30673
30674 /* EXPORT:
30675 Draw a vertical window border to the right of window W if W doesn't
30676 have vertical scroll bars. */
30677
30678 void
30679 x_draw_vertical_border (struct window *w)
30680 {
30681 struct frame *f = XFRAME (WINDOW_FRAME (w));
30682
30683 /* We could do better, if we knew what type of scroll-bar the adjacent
30684 windows (on either side) have... But we don't :-(
30685 However, I think this works ok. ++KFS 2003-04-25 */
30686
30687 /* Redraw borders between horizontally adjacent windows. Don't
30688 do it for frames with vertical scroll bars because either the
30689 right scroll bar of a window, or the left scroll bar of its
30690 neighbor will suffice as a border. */
30691 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30692 return;
30693
30694 /* Note: It is necessary to redraw both the left and the right
30695 borders, for when only this single window W is being
30696 redisplayed. */
30697 if (!WINDOW_RIGHTMOST_P (w)
30698 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30699 {
30700 int x0, x1, y0, y1;
30701
30702 window_box_edges (w, &x0, &y0, &x1, &y1);
30703 y1 -= 1;
30704
30705 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30706 x1 -= 1;
30707
30708 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30709 }
30710
30711 if (!WINDOW_LEFTMOST_P (w)
30712 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30713 {
30714 int x0, x1, y0, y1;
30715
30716 window_box_edges (w, &x0, &y0, &x1, &y1);
30717 y1 -= 1;
30718
30719 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30720 x0 -= 1;
30721
30722 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30723 }
30724 }
30725
30726
30727 /* Draw window dividers for window W. */
30728
30729 void
30730 x_draw_right_divider (struct window *w)
30731 {
30732 struct frame *f = WINDOW_XFRAME (w);
30733
30734 if (w->mini || w->pseudo_window_p)
30735 return;
30736 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30737 {
30738 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30739 int x1 = WINDOW_RIGHT_EDGE_X (w);
30740 int y0 = WINDOW_TOP_EDGE_Y (w);
30741 /* The bottom divider prevails. */
30742 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30743
30744 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30745 }
30746 }
30747
30748 static void
30749 x_draw_bottom_divider (struct window *w)
30750 {
30751 struct frame *f = XFRAME (WINDOW_FRAME (w));
30752
30753 if (w->mini || w->pseudo_window_p)
30754 return;
30755 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30756 {
30757 int x0 = WINDOW_LEFT_EDGE_X (w);
30758 int x1 = WINDOW_RIGHT_EDGE_X (w);
30759 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30760 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30761
30762 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30763 }
30764 }
30765
30766 /* Redraw the part of window W intersection rectangle FR. Pixel
30767 coordinates in FR are frame-relative. Call this function with
30768 input blocked. Value is true if the exposure overwrites
30769 mouse-face. */
30770
30771 static bool
30772 expose_window (struct window *w, XRectangle *fr)
30773 {
30774 struct frame *f = XFRAME (w->frame);
30775 XRectangle wr, r;
30776 bool mouse_face_overwritten_p = false;
30777
30778 /* If window is not yet fully initialized, do nothing. This can
30779 happen when toolkit scroll bars are used and a window is split.
30780 Reconfiguring the scroll bar will generate an expose for a newly
30781 created window. */
30782 if (w->current_matrix == NULL)
30783 return false;
30784
30785 /* When we're currently updating the window, display and current
30786 matrix usually don't agree. Arrange for a thorough display
30787 later. */
30788 if (w->must_be_updated_p)
30789 {
30790 SET_FRAME_GARBAGED (f);
30791 return false;
30792 }
30793
30794 /* Frame-relative pixel rectangle of W. */
30795 wr.x = WINDOW_LEFT_EDGE_X (w);
30796 wr.y = WINDOW_TOP_EDGE_Y (w);
30797 wr.width = WINDOW_PIXEL_WIDTH (w);
30798 wr.height = WINDOW_PIXEL_HEIGHT (w);
30799
30800 if (x_intersect_rectangles (fr, &wr, &r))
30801 {
30802 int yb = window_text_bottom_y (w);
30803 struct glyph_row *row;
30804 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30805
30806 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30807 r.x, r.y, r.width, r.height));
30808
30809 /* Convert to window coordinates. */
30810 r.x -= WINDOW_LEFT_EDGE_X (w);
30811 r.y -= WINDOW_TOP_EDGE_Y (w);
30812
30813 /* Turn off the cursor. */
30814 bool cursor_cleared_p = (!w->pseudo_window_p
30815 && phys_cursor_in_rect_p (w, &r));
30816 if (cursor_cleared_p)
30817 x_clear_cursor (w);
30818
30819 /* If the row containing the cursor extends face to end of line,
30820 then expose_area might overwrite the cursor outside the
30821 rectangle and thus notice_overwritten_cursor might clear
30822 w->phys_cursor_on_p. We remember the original value and
30823 check later if it is changed. */
30824 bool phys_cursor_on_p = w->phys_cursor_on_p;
30825
30826 /* Use a signed int intermediate value to avoid catastrophic
30827 failures due to comparison between signed and unsigned, when
30828 y0 or y1 is negative (can happen for tall images). */
30829 int r_bottom = r.y + r.height;
30830
30831 /* Update lines intersecting rectangle R. */
30832 first_overlapping_row = last_overlapping_row = NULL;
30833 for (row = w->current_matrix->rows;
30834 row->enabled_p;
30835 ++row)
30836 {
30837 int y0 = row->y;
30838 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30839
30840 if ((y0 >= r.y && y0 < r_bottom)
30841 || (y1 > r.y && y1 < r_bottom)
30842 || (r.y >= y0 && r.y < y1)
30843 || (r_bottom > y0 && r_bottom < y1))
30844 {
30845 /* A header line may be overlapping, but there is no need
30846 to fix overlapping areas for them. KFS 2005-02-12 */
30847 if (row->overlapping_p && !row->mode_line_p)
30848 {
30849 if (first_overlapping_row == NULL)
30850 first_overlapping_row = row;
30851 last_overlapping_row = row;
30852 }
30853
30854 row->clip = fr;
30855 if (expose_line (w, row, &r))
30856 mouse_face_overwritten_p = true;
30857 row->clip = NULL;
30858 }
30859 else if (row->overlapping_p)
30860 {
30861 /* We must redraw a row overlapping the exposed area. */
30862 if (y0 < r.y
30863 ? y0 + row->phys_height > r.y
30864 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30865 {
30866 if (first_overlapping_row == NULL)
30867 first_overlapping_row = row;
30868 last_overlapping_row = row;
30869 }
30870 }
30871
30872 if (y1 >= yb)
30873 break;
30874 }
30875
30876 /* Display the mode line if there is one. */
30877 if (WINDOW_WANTS_MODELINE_P (w)
30878 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30879 row->enabled_p)
30880 && row->y < r_bottom)
30881 {
30882 if (expose_line (w, row, &r))
30883 mouse_face_overwritten_p = true;
30884 }
30885
30886 if (!w->pseudo_window_p)
30887 {
30888 /* Fix the display of overlapping rows. */
30889 if (first_overlapping_row)
30890 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30891 fr);
30892
30893 /* Draw border between windows. */
30894 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30895 x_draw_right_divider (w);
30896 else
30897 x_draw_vertical_border (w);
30898
30899 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30900 x_draw_bottom_divider (w);
30901
30902 /* Turn the cursor on again. */
30903 if (cursor_cleared_p
30904 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30905 update_window_cursor (w, true);
30906 }
30907 }
30908
30909 return mouse_face_overwritten_p;
30910 }
30911
30912
30913
30914 /* Redraw (parts) of all windows in the window tree rooted at W that
30915 intersect R. R contains frame pixel coordinates. Value is
30916 true if the exposure overwrites mouse-face. */
30917
30918 static bool
30919 expose_window_tree (struct window *w, XRectangle *r)
30920 {
30921 struct frame *f = XFRAME (w->frame);
30922 bool mouse_face_overwritten_p = false;
30923
30924 while (w && !FRAME_GARBAGED_P (f))
30925 {
30926 mouse_face_overwritten_p
30927 |= (WINDOWP (w->contents)
30928 ? expose_window_tree (XWINDOW (w->contents), r)
30929 : expose_window (w, r));
30930
30931 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30932 }
30933
30934 return mouse_face_overwritten_p;
30935 }
30936
30937
30938 /* EXPORT:
30939 Redisplay an exposed area of frame F. X and Y are the upper-left
30940 corner of the exposed rectangle. W and H are width and height of
30941 the exposed area. All are pixel values. W or H zero means redraw
30942 the entire frame. */
30943
30944 void
30945 expose_frame (struct frame *f, int x, int y, int w, int h)
30946 {
30947 XRectangle r;
30948 bool mouse_face_overwritten_p = false;
30949
30950 TRACE ((stderr, "expose_frame "));
30951
30952 /* No need to redraw if frame will be redrawn soon. */
30953 if (FRAME_GARBAGED_P (f))
30954 {
30955 TRACE ((stderr, " garbaged\n"));
30956 return;
30957 }
30958
30959 /* If basic faces haven't been realized yet, there is no point in
30960 trying to redraw anything. This can happen when we get an expose
30961 event while Emacs is starting, e.g. by moving another window. */
30962 if (FRAME_FACE_CACHE (f) == NULL
30963 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30964 {
30965 TRACE ((stderr, " no faces\n"));
30966 return;
30967 }
30968
30969 if (w == 0 || h == 0)
30970 {
30971 r.x = r.y = 0;
30972 r.width = FRAME_TEXT_WIDTH (f);
30973 r.height = FRAME_TEXT_HEIGHT (f);
30974 }
30975 else
30976 {
30977 r.x = x;
30978 r.y = y;
30979 r.width = w;
30980 r.height = h;
30981 }
30982
30983 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30984 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30985
30986 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30987 if (WINDOWP (f->tool_bar_window))
30988 mouse_face_overwritten_p
30989 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30990 #endif
30991
30992 #ifdef HAVE_X_WINDOWS
30993 #ifndef MSDOS
30994 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30995 if (WINDOWP (f->menu_bar_window))
30996 mouse_face_overwritten_p
30997 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30998 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30999 #endif
31000 #endif
31001
31002 /* Some window managers support a focus-follows-mouse style with
31003 delayed raising of frames. Imagine a partially obscured frame,
31004 and moving the mouse into partially obscured mouse-face on that
31005 frame. The visible part of the mouse-face will be highlighted,
31006 then the WM raises the obscured frame. With at least one WM, KDE
31007 2.1, Emacs is not getting any event for the raising of the frame
31008 (even tried with SubstructureRedirectMask), only Expose events.
31009 These expose events will draw text normally, i.e. not
31010 highlighted. Which means we must redo the highlight here.
31011 Subsume it under ``we love X''. --gerd 2001-08-15 */
31012 /* Included in Windows version because Windows most likely does not
31013 do the right thing if any third party tool offers
31014 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
31015 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
31016 {
31017 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31018 if (f == hlinfo->mouse_face_mouse_frame)
31019 {
31020 int mouse_x = hlinfo->mouse_face_mouse_x;
31021 int mouse_y = hlinfo->mouse_face_mouse_y;
31022 clear_mouse_face (hlinfo);
31023 note_mouse_highlight (f, mouse_x, mouse_y);
31024 }
31025 }
31026 }
31027
31028
31029 /* EXPORT:
31030 Determine the intersection of two rectangles R1 and R2. Return
31031 the intersection in *RESULT. Value is true if RESULT is not
31032 empty. */
31033
31034 bool
31035 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
31036 {
31037 XRectangle *left, *right;
31038 XRectangle *upper, *lower;
31039 bool intersection_p = false;
31040
31041 /* Rearrange so that R1 is the left-most rectangle. */
31042 if (r1->x < r2->x)
31043 left = r1, right = r2;
31044 else
31045 left = r2, right = r1;
31046
31047 /* X0 of the intersection is right.x0, if this is inside R1,
31048 otherwise there is no intersection. */
31049 if (right->x <= left->x + left->width)
31050 {
31051 result->x = right->x;
31052
31053 /* The right end of the intersection is the minimum of
31054 the right ends of left and right. */
31055 result->width = (min (left->x + left->width, right->x + right->width)
31056 - result->x);
31057
31058 /* Same game for Y. */
31059 if (r1->y < r2->y)
31060 upper = r1, lower = r2;
31061 else
31062 upper = r2, lower = r1;
31063
31064 /* The upper end of the intersection is lower.y0, if this is inside
31065 of upper. Otherwise, there is no intersection. */
31066 if (lower->y <= upper->y + upper->height)
31067 {
31068 result->y = lower->y;
31069
31070 /* The lower end of the intersection is the minimum of the lower
31071 ends of upper and lower. */
31072 result->height = (min (lower->y + lower->height,
31073 upper->y + upper->height)
31074 - result->y);
31075 intersection_p = true;
31076 }
31077 }
31078
31079 return intersection_p;
31080 }
31081
31082 #endif /* HAVE_WINDOW_SYSTEM */
31083
31084 \f
31085 /***********************************************************************
31086 Initialization
31087 ***********************************************************************/
31088
31089 void
31090 syms_of_xdisp (void)
31091 {
31092 Vwith_echo_area_save_vector = Qnil;
31093 staticpro (&Vwith_echo_area_save_vector);
31094
31095 Vmessage_stack = Qnil;
31096 staticpro (&Vmessage_stack);
31097
31098 /* Non-nil means don't actually do any redisplay. */
31099 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
31100
31101 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
31102
31103 DEFVAR_BOOL("inhibit-message", inhibit_message,
31104 doc: /* Non-nil means calls to `message' are not displayed.
31105 They are still logged to the *Messages* buffer. */);
31106 inhibit_message = 0;
31107
31108 message_dolog_marker1 = Fmake_marker ();
31109 staticpro (&message_dolog_marker1);
31110 message_dolog_marker2 = Fmake_marker ();
31111 staticpro (&message_dolog_marker2);
31112 message_dolog_marker3 = Fmake_marker ();
31113 staticpro (&message_dolog_marker3);
31114
31115 #ifdef GLYPH_DEBUG
31116 defsubr (&Sdump_frame_glyph_matrix);
31117 defsubr (&Sdump_glyph_matrix);
31118 defsubr (&Sdump_glyph_row);
31119 defsubr (&Sdump_tool_bar_row);
31120 defsubr (&Strace_redisplay);
31121 defsubr (&Strace_to_stderr);
31122 #endif
31123 #ifdef HAVE_WINDOW_SYSTEM
31124 defsubr (&Stool_bar_height);
31125 defsubr (&Slookup_image_map);
31126 #endif
31127 defsubr (&Sline_pixel_height);
31128 defsubr (&Sformat_mode_line);
31129 defsubr (&Sinvisible_p);
31130 defsubr (&Scurrent_bidi_paragraph_direction);
31131 defsubr (&Swindow_text_pixel_size);
31132 defsubr (&Smove_point_visually);
31133 defsubr (&Sbidi_find_overridden_directionality);
31134
31135 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
31136 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
31137 DEFSYM (Qoverriding_local_map, "overriding-local-map");
31138 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
31139 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
31140 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
31141 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
31142 DEFSYM (Qeval, "eval");
31143 DEFSYM (QCdata, ":data");
31144
31145 /* Names of text properties relevant for redisplay. */
31146 DEFSYM (Qdisplay, "display");
31147 DEFSYM (Qspace_width, "space-width");
31148 DEFSYM (Qraise, "raise");
31149 DEFSYM (Qslice, "slice");
31150 DEFSYM (Qspace, "space");
31151 DEFSYM (Qmargin, "margin");
31152 DEFSYM (Qpointer, "pointer");
31153 DEFSYM (Qleft_margin, "left-margin");
31154 DEFSYM (Qright_margin, "right-margin");
31155 DEFSYM (Qcenter, "center");
31156 DEFSYM (Qline_height, "line-height");
31157 DEFSYM (QCalign_to, ":align-to");
31158 DEFSYM (QCrelative_width, ":relative-width");
31159 DEFSYM (QCrelative_height, ":relative-height");
31160 DEFSYM (QCeval, ":eval");
31161 DEFSYM (QCpropertize, ":propertize");
31162 DEFSYM (QCfile, ":file");
31163 DEFSYM (Qfontified, "fontified");
31164 DEFSYM (Qfontification_functions, "fontification-functions");
31165
31166 /* Name of the face used to highlight trailing whitespace. */
31167 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
31168
31169 /* Name and number of the face used to highlight escape glyphs. */
31170 DEFSYM (Qescape_glyph, "escape-glyph");
31171
31172 /* Name and number of the face used to highlight non-breaking spaces. */
31173 DEFSYM (Qnobreak_space, "nobreak-space");
31174
31175 /* The symbol 'image' which is the car of the lists used to represent
31176 images in Lisp. Also a tool bar style. */
31177 DEFSYM (Qimage, "image");
31178
31179 /* Tool bar styles. */
31180 DEFSYM (Qtext, "text");
31181 DEFSYM (Qboth, "both");
31182 DEFSYM (Qboth_horiz, "both-horiz");
31183 DEFSYM (Qtext_image_horiz, "text-image-horiz");
31184
31185 /* The image map types. */
31186 DEFSYM (QCmap, ":map");
31187 DEFSYM (QCpointer, ":pointer");
31188 DEFSYM (Qrect, "rect");
31189 DEFSYM (Qcircle, "circle");
31190 DEFSYM (Qpoly, "poly");
31191
31192 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
31193
31194 DEFSYM (Qgrow_only, "grow-only");
31195 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
31196 DEFSYM (Qposition, "position");
31197 DEFSYM (Qbuffer_position, "buffer-position");
31198 DEFSYM (Qobject, "object");
31199
31200 /* Cursor shapes. */
31201 DEFSYM (Qbar, "bar");
31202 DEFSYM (Qhbar, "hbar");
31203 DEFSYM (Qbox, "box");
31204 DEFSYM (Qhollow, "hollow");
31205
31206 /* Pointer shapes. */
31207 DEFSYM (Qhand, "hand");
31208 DEFSYM (Qarrow, "arrow");
31209 /* also Qtext */
31210
31211 DEFSYM (Qdragging, "dragging");
31212
31213 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
31214
31215 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
31216 staticpro (&list_of_error);
31217
31218 /* Values of those variables at last redisplay are stored as
31219 properties on 'overlay-arrow-position' symbol. However, if
31220 Voverlay_arrow_position is a marker, last-arrow-position is its
31221 numerical position. */
31222 DEFSYM (Qlast_arrow_position, "last-arrow-position");
31223 DEFSYM (Qlast_arrow_string, "last-arrow-string");
31224
31225 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
31226 properties on a symbol in overlay-arrow-variable-list. */
31227 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
31228 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
31229
31230 echo_buffer[0] = echo_buffer[1] = Qnil;
31231 staticpro (&echo_buffer[0]);
31232 staticpro (&echo_buffer[1]);
31233
31234 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
31235 staticpro (&echo_area_buffer[0]);
31236 staticpro (&echo_area_buffer[1]);
31237
31238 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
31239 staticpro (&Vmessages_buffer_name);
31240
31241 mode_line_proptrans_alist = Qnil;
31242 staticpro (&mode_line_proptrans_alist);
31243 mode_line_string_list = Qnil;
31244 staticpro (&mode_line_string_list);
31245 mode_line_string_face = Qnil;
31246 staticpro (&mode_line_string_face);
31247 mode_line_string_face_prop = Qnil;
31248 staticpro (&mode_line_string_face_prop);
31249 Vmode_line_unwind_vector = Qnil;
31250 staticpro (&Vmode_line_unwind_vector);
31251
31252 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
31253
31254 help_echo_string = Qnil;
31255 staticpro (&help_echo_string);
31256 help_echo_object = Qnil;
31257 staticpro (&help_echo_object);
31258 help_echo_window = Qnil;
31259 staticpro (&help_echo_window);
31260 previous_help_echo_string = Qnil;
31261 staticpro (&previous_help_echo_string);
31262 help_echo_pos = -1;
31263
31264 DEFSYM (Qright_to_left, "right-to-left");
31265 DEFSYM (Qleft_to_right, "left-to-right");
31266 defsubr (&Sbidi_resolved_levels);
31267
31268 #ifdef HAVE_WINDOW_SYSTEM
31269 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
31270 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
31271 For example, if a block cursor is over a tab, it will be drawn as
31272 wide as that tab on the display. */);
31273 x_stretch_cursor_p = 0;
31274 #endif
31275
31276 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
31277 doc: /* Non-nil means highlight trailing whitespace.
31278 The face used for trailing whitespace is `trailing-whitespace'. */);
31279 Vshow_trailing_whitespace = Qnil;
31280
31281 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
31282 doc: /* Control highlighting of non-ASCII space and hyphen chars.
31283 If the value is t, Emacs highlights non-ASCII chars which have the
31284 same appearance as an ASCII space or hyphen, using the `nobreak-space'
31285 or `escape-glyph' face respectively.
31286
31287 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
31288 U+2011 (non-breaking hyphen) are affected.
31289
31290 Any other non-nil value means to display these characters as a escape
31291 glyph followed by an ordinary space or hyphen.
31292
31293 A value of nil means no special handling of these characters. */);
31294 Vnobreak_char_display = Qt;
31295
31296 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
31297 doc: /* The pointer shape to show in void text areas.
31298 A value of nil means to show the text pointer. Other options are
31299 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
31300 `hourglass'. */);
31301 Vvoid_text_area_pointer = Qarrow;
31302
31303 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
31304 doc: /* Non-nil means don't actually do any redisplay.
31305 This is used for internal purposes. */);
31306 Vinhibit_redisplay = Qnil;
31307
31308 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
31309 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
31310 Vglobal_mode_string = Qnil;
31311
31312 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
31313 doc: /* Marker for where to display an arrow on top of the buffer text.
31314 This must be the beginning of a line in order to work.
31315 See also `overlay-arrow-string'. */);
31316 Voverlay_arrow_position = Qnil;
31317
31318 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
31319 doc: /* String to display as an arrow in non-window frames.
31320 See also `overlay-arrow-position'. */);
31321 Voverlay_arrow_string = build_pure_c_string ("=>");
31322
31323 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31324 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31325 The symbols on this list are examined during redisplay to determine
31326 where to display overlay arrows. */);
31327 Voverlay_arrow_variable_list
31328 = list1 (intern_c_string ("overlay-arrow-position"));
31329
31330 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31331 doc: /* The number of lines to try scrolling a window by when point moves out.
31332 If that fails to bring point back on frame, point is centered instead.
31333 If this is zero, point is always centered after it moves off frame.
31334 If you want scrolling to always be a line at a time, you should set
31335 `scroll-conservatively' to a large value rather than set this to 1. */);
31336
31337 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31338 doc: /* Scroll up to this many lines, to bring point back on screen.
31339 If point moves off-screen, redisplay will scroll by up to
31340 `scroll-conservatively' lines in order to bring point just barely
31341 onto the screen again. If that cannot be done, then redisplay
31342 recenters point as usual.
31343
31344 If the value is greater than 100, redisplay will never recenter point,
31345 but will always scroll just enough text to bring point into view, even
31346 if you move far away.
31347
31348 A value of zero means always recenter point if it moves off screen. */);
31349 scroll_conservatively = 0;
31350
31351 DEFVAR_INT ("scroll-margin", scroll_margin,
31352 doc: /* Number of lines of margin at the top and bottom of a window.
31353 Recenter the window whenever point gets within this many lines
31354 of the top or bottom of the window. */);
31355 scroll_margin = 0;
31356
31357 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31358 doc: /* Pixels per inch value for non-window system displays.
31359 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31360 Vdisplay_pixels_per_inch = make_float (72.0);
31361
31362 #ifdef GLYPH_DEBUG
31363 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31364 #endif
31365
31366 DEFVAR_LISP ("truncate-partial-width-windows",
31367 Vtruncate_partial_width_windows,
31368 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31369 For an integer value, truncate lines in each window narrower than the
31370 full frame width, provided the window width is less than that integer;
31371 otherwise, respect the value of `truncate-lines'.
31372
31373 For any other non-nil value, truncate lines in all windows that do
31374 not span the full frame width.
31375
31376 A value of nil means to respect the value of `truncate-lines'.
31377
31378 If `word-wrap' is enabled, you might want to reduce this. */);
31379 Vtruncate_partial_width_windows = make_number (50);
31380
31381 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31382 doc: /* Maximum buffer size for which line number should be displayed.
31383 If the buffer is bigger than this, the line number does not appear
31384 in the mode line. A value of nil means no limit. */);
31385 Vline_number_display_limit = Qnil;
31386
31387 DEFVAR_INT ("line-number-display-limit-width",
31388 line_number_display_limit_width,
31389 doc: /* Maximum line width (in characters) for line number display.
31390 If the average length of the lines near point is bigger than this, then the
31391 line number may be omitted from the mode line. */);
31392 line_number_display_limit_width = 200;
31393
31394 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31395 doc: /* Non-nil means highlight region even in nonselected windows. */);
31396 highlight_nonselected_windows = false;
31397
31398 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31399 doc: /* Non-nil if more than one frame is visible on this display.
31400 Minibuffer-only frames don't count, but iconified frames do.
31401 This variable is not guaranteed to be accurate except while processing
31402 `frame-title-format' and `icon-title-format'. */);
31403
31404 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31405 doc: /* Template for displaying the title bar of visible frames.
31406 (Assuming the window manager supports this feature.)
31407
31408 This variable has the same structure as `mode-line-format', except that
31409 the %c and %l constructs are ignored. It is used only on frames for
31410 which no explicit name has been set (see `modify-frame-parameters'). */);
31411
31412 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31413 doc: /* Template for displaying the title bar of an iconified frame.
31414 (Assuming the window manager supports this feature.)
31415 This variable has the same structure as `mode-line-format' (which see),
31416 and is used only on frames for which no explicit name has been set
31417 (see `modify-frame-parameters'). */);
31418 Vicon_title_format
31419 = Vframe_title_format
31420 = listn (CONSTYPE_PURE, 3,
31421 intern_c_string ("multiple-frames"),
31422 build_pure_c_string ("%b"),
31423 listn (CONSTYPE_PURE, 4,
31424 empty_unibyte_string,
31425 intern_c_string ("invocation-name"),
31426 build_pure_c_string ("@"),
31427 intern_c_string ("system-name")));
31428
31429 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31430 doc: /* Maximum number of lines to keep in the message log buffer.
31431 If nil, disable message logging. If t, log messages but don't truncate
31432 the buffer when it becomes large. */);
31433 Vmessage_log_max = make_number (1000);
31434
31435 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
31436 doc: /* Functions called during redisplay, if window sizes have changed.
31437 The value should be a list of functions that take one argument.
31438 During the first part of redisplay, for each frame, if any of its windows
31439 have changed size since the last redisplay, or have been split or deleted,
31440 all the functions in the list are called, with the frame as argument.
31441 If redisplay decides to resize the minibuffer window, it calls these
31442 functions on behalf of that as well. */);
31443 Vwindow_size_change_functions = Qnil;
31444
31445 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31446 doc: /* List of functions to call before redisplaying a window with scrolling.
31447 Each function is called with two arguments, the window and its new
31448 display-start position.
31449 These functions are called whenever the `window-start' marker is modified,
31450 either to point into another buffer (e.g. via `set-window-buffer') or another
31451 place in the same buffer.
31452 Note that the value of `window-end' is not valid when these functions are
31453 called.
31454
31455 Warning: Do not use this feature to alter the way the window
31456 is scrolled. It is not designed for that, and such use probably won't
31457 work. */);
31458 Vwindow_scroll_functions = Qnil;
31459
31460 DEFVAR_LISP ("window-text-change-functions",
31461 Vwindow_text_change_functions,
31462 doc: /* Functions to call in redisplay when text in the window might change. */);
31463 Vwindow_text_change_functions = Qnil;
31464
31465 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31466 doc: /* Functions called when redisplay of a window reaches the end trigger.
31467 Each function is called with two arguments, the window and the end trigger value.
31468 See `set-window-redisplay-end-trigger'. */);
31469 Vredisplay_end_trigger_functions = Qnil;
31470
31471 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31472 doc: /* Non-nil means autoselect window with mouse pointer.
31473 If nil, do not autoselect windows.
31474 A positive number means delay autoselection by that many seconds: a
31475 window is autoselected only after the mouse has remained in that
31476 window for the duration of the delay.
31477 A negative number has a similar effect, but causes windows to be
31478 autoselected only after the mouse has stopped moving. (Because of
31479 the way Emacs compares mouse events, you will occasionally wait twice
31480 that time before the window gets selected.)
31481 Any other value means to autoselect window instantaneously when the
31482 mouse pointer enters it.
31483
31484 Autoselection selects the minibuffer only if it is active, and never
31485 unselects the minibuffer if it is active.
31486
31487 When customizing this variable make sure that the actual value of
31488 `focus-follows-mouse' matches the behavior of your window manager. */);
31489 Vmouse_autoselect_window = Qnil;
31490
31491 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31492 doc: /* Non-nil means automatically resize tool-bars.
31493 This dynamically changes the tool-bar's height to the minimum height
31494 that is needed to make all tool-bar items visible.
31495 If value is `grow-only', the tool-bar's height is only increased
31496 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31497 Vauto_resize_tool_bars = Qt;
31498
31499 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31500 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31501 auto_raise_tool_bar_buttons_p = true;
31502
31503 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31504 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31505 make_cursor_line_fully_visible_p = true;
31506
31507 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31508 doc: /* Border below tool-bar in pixels.
31509 If an integer, use it as the height of the border.
31510 If it is one of `internal-border-width' or `border-width', use the
31511 value of the corresponding frame parameter.
31512 Otherwise, no border is added below the tool-bar. */);
31513 Vtool_bar_border = Qinternal_border_width;
31514
31515 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31516 doc: /* Margin around tool-bar buttons in pixels.
31517 If an integer, use that for both horizontal and vertical margins.
31518 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31519 HORZ specifying the horizontal margin, and VERT specifying the
31520 vertical margin. */);
31521 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31522
31523 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31524 doc: /* Relief thickness of tool-bar buttons. */);
31525 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31526
31527 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31528 doc: /* Tool bar style to use.
31529 It can be one of
31530 image - show images only
31531 text - show text only
31532 both - show both, text below image
31533 both-horiz - show text to the right of the image
31534 text-image-horiz - show text to the left of the image
31535 any other - use system default or image if no system default.
31536
31537 This variable only affects the GTK+ toolkit version of Emacs. */);
31538 Vtool_bar_style = Qnil;
31539
31540 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31541 doc: /* Maximum number of characters a label can have to be shown.
31542 The tool bar style must also show labels for this to have any effect, see
31543 `tool-bar-style'. */);
31544 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31545
31546 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31547 doc: /* List of functions to call to fontify regions of text.
31548 Each function is called with one argument POS. Functions must
31549 fontify a region starting at POS in the current buffer, and give
31550 fontified regions the property `fontified'. */);
31551 Vfontification_functions = Qnil;
31552 Fmake_variable_buffer_local (Qfontification_functions);
31553
31554 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31555 unibyte_display_via_language_environment,
31556 doc: /* Non-nil means display unibyte text according to language environment.
31557 Specifically, this means that raw bytes in the range 160-255 decimal
31558 are displayed by converting them to the equivalent multibyte characters
31559 according to the current language environment. As a result, they are
31560 displayed according to the current fontset.
31561
31562 Note that this variable affects only how these bytes are displayed,
31563 but does not change the fact they are interpreted as raw bytes. */);
31564 unibyte_display_via_language_environment = false;
31565
31566 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31567 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31568 If a float, it specifies a fraction of the mini-window frame's height.
31569 If an integer, it specifies a number of lines. */);
31570 Vmax_mini_window_height = make_float (0.25);
31571
31572 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31573 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31574 A value of nil means don't automatically resize mini-windows.
31575 A value of t means resize them to fit the text displayed in them.
31576 A value of `grow-only', the default, means let mini-windows grow only;
31577 they return to their normal size when the minibuffer is closed, or the
31578 echo area becomes empty. */);
31579 Vresize_mini_windows = Qgrow_only;
31580
31581 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31582 doc: /* Alist specifying how to blink the cursor off.
31583 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31584 `cursor-type' frame-parameter or variable equals ON-STATE,
31585 comparing using `equal', Emacs uses OFF-STATE to specify
31586 how to blink it off. ON-STATE and OFF-STATE are values for
31587 the `cursor-type' frame parameter.
31588
31589 If a frame's ON-STATE has no entry in this list,
31590 the frame's other specifications determine how to blink the cursor off. */);
31591 Vblink_cursor_alist = Qnil;
31592
31593 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31594 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31595 If non-nil, windows are automatically scrolled horizontally to make
31596 point visible. */);
31597 automatic_hscrolling_p = true;
31598 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31599
31600 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31601 doc: /* How many columns away from the window edge point is allowed to get
31602 before automatic hscrolling will horizontally scroll the window. */);
31603 hscroll_margin = 5;
31604
31605 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31606 doc: /* How many columns to scroll the window when point gets too close to the edge.
31607 When point is less than `hscroll-margin' columns from the window
31608 edge, automatic hscrolling will scroll the window by the amount of columns
31609 determined by this variable. If its value is a positive integer, scroll that
31610 many columns. If it's a positive floating-point number, it specifies the
31611 fraction of the window's width to scroll. If it's nil or zero, point will be
31612 centered horizontally after the scroll. Any other value, including negative
31613 numbers, are treated as if the value were zero.
31614
31615 Automatic hscrolling always moves point outside the scroll margin, so if
31616 point was more than scroll step columns inside the margin, the window will
31617 scroll more than the value given by the scroll step.
31618
31619 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31620 and `scroll-right' overrides this variable's effect. */);
31621 Vhscroll_step = make_number (0);
31622
31623 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31624 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31625 Bind this around calls to `message' to let it take effect. */);
31626 message_truncate_lines = false;
31627
31628 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31629 doc: /* Normal hook run to update the menu bar definitions.
31630 Redisplay runs this hook before it redisplays the menu bar.
31631 This is used to update menus such as Buffers, whose contents depend on
31632 various data. */);
31633 Vmenu_bar_update_hook = Qnil;
31634
31635 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31636 doc: /* Frame for which we are updating a menu.
31637 The enable predicate for a menu binding should check this variable. */);
31638 Vmenu_updating_frame = Qnil;
31639
31640 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31641 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31642 inhibit_menubar_update = false;
31643
31644 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31645 doc: /* Prefix prepended to all continuation lines at display time.
31646 The value may be a string, an image, or a stretch-glyph; it is
31647 interpreted in the same way as the value of a `display' text property.
31648
31649 This variable is overridden by any `wrap-prefix' text or overlay
31650 property.
31651
31652 To add a prefix to non-continuation lines, use `line-prefix'. */);
31653 Vwrap_prefix = Qnil;
31654 DEFSYM (Qwrap_prefix, "wrap-prefix");
31655 Fmake_variable_buffer_local (Qwrap_prefix);
31656
31657 DEFVAR_LISP ("line-prefix", Vline_prefix,
31658 doc: /* Prefix prepended to all non-continuation lines at display time.
31659 The value may be a string, an image, or a stretch-glyph; it is
31660 interpreted in the same way as the value of a `display' text property.
31661
31662 This variable is overridden by any `line-prefix' text or overlay
31663 property.
31664
31665 To add a prefix to continuation lines, use `wrap-prefix'. */);
31666 Vline_prefix = Qnil;
31667 DEFSYM (Qline_prefix, "line-prefix");
31668 Fmake_variable_buffer_local (Qline_prefix);
31669
31670 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31671 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31672 inhibit_eval_during_redisplay = false;
31673
31674 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31675 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31676 inhibit_free_realized_faces = false;
31677
31678 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31679 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31680 Intended for use during debugging and for testing bidi display;
31681 see biditest.el in the test suite. */);
31682 inhibit_bidi_mirroring = false;
31683
31684 #ifdef GLYPH_DEBUG
31685 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31686 doc: /* Inhibit try_window_id display optimization. */);
31687 inhibit_try_window_id = false;
31688
31689 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31690 doc: /* Inhibit try_window_reusing display optimization. */);
31691 inhibit_try_window_reusing = false;
31692
31693 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31694 doc: /* Inhibit try_cursor_movement display optimization. */);
31695 inhibit_try_cursor_movement = false;
31696 #endif /* GLYPH_DEBUG */
31697
31698 DEFVAR_INT ("overline-margin", overline_margin,
31699 doc: /* Space between overline and text, in pixels.
31700 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31701 margin to the character height. */);
31702 overline_margin = 2;
31703
31704 DEFVAR_INT ("underline-minimum-offset",
31705 underline_minimum_offset,
31706 doc: /* Minimum distance between baseline and underline.
31707 This can improve legibility of underlined text at small font sizes,
31708 particularly when using variable `x-use-underline-position-properties'
31709 with fonts that specify an UNDERLINE_POSITION relatively close to the
31710 baseline. The default value is 1. */);
31711 underline_minimum_offset = 1;
31712
31713 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31714 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31715 This feature only works when on a window system that can change
31716 cursor shapes. */);
31717 display_hourglass_p = true;
31718
31719 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31720 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31721 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31722
31723 #ifdef HAVE_WINDOW_SYSTEM
31724 hourglass_atimer = NULL;
31725 hourglass_shown_p = false;
31726 #endif /* HAVE_WINDOW_SYSTEM */
31727
31728 /* Name of the face used to display glyphless characters. */
31729 DEFSYM (Qglyphless_char, "glyphless-char");
31730
31731 /* Method symbols for Vglyphless_char_display. */
31732 DEFSYM (Qhex_code, "hex-code");
31733 DEFSYM (Qempty_box, "empty-box");
31734 DEFSYM (Qthin_space, "thin-space");
31735 DEFSYM (Qzero_width, "zero-width");
31736
31737 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31738 doc: /* Function run just before redisplay.
31739 It is called with one argument, which is the set of windows that are to
31740 be redisplayed. This set can be nil (meaning, only the selected window),
31741 or t (meaning all windows). */);
31742 Vpre_redisplay_function = intern ("ignore");
31743
31744 /* Symbol for the purpose of Vglyphless_char_display. */
31745 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31746 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31747
31748 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31749 doc: /* Char-table defining glyphless characters.
31750 Each element, if non-nil, should be one of the following:
31751 an ASCII acronym string: display this string in a box
31752 `hex-code': display the hexadecimal code of a character in a box
31753 `empty-box': display as an empty box
31754 `thin-space': display as 1-pixel width space
31755 `zero-width': don't display
31756 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31757 display method for graphical terminals and text terminals respectively.
31758 GRAPHICAL and TEXT should each have one of the values listed above.
31759
31760 The char-table has one extra slot to control the display of a character for
31761 which no font is found. This slot only takes effect on graphical terminals.
31762 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31763 `thin-space'. The default is `empty-box'.
31764
31765 If a character has a non-nil entry in an active display table, the
31766 display table takes effect; in this case, Emacs does not consult
31767 `glyphless-char-display' at all. */);
31768 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31769 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31770 Qempty_box);
31771
31772 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31773 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31774 Vdebug_on_message = Qnil;
31775
31776 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31777 doc: /* */);
31778 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
31779
31780 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31781 doc: /* */);
31782 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
31783
31784 DEFVAR_LISP ("redisplay--variables", Vredisplay__variables,
31785 doc: /* A hash-table of variables changing which triggers a thorough redisplay. */);
31786 Vredisplay__variables = Qnil;
31787 }
31788
31789
31790 /* Initialize this module when Emacs starts. */
31791
31792 void
31793 init_xdisp (void)
31794 {
31795 CHARPOS (this_line_start_pos) = 0;
31796
31797 if (!noninteractive)
31798 {
31799 struct window *m = XWINDOW (minibuf_window);
31800 Lisp_Object frame = m->frame;
31801 struct frame *f = XFRAME (frame);
31802 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31803 struct window *r = XWINDOW (root);
31804 int i;
31805
31806 echo_area_window = minibuf_window;
31807
31808 r->top_line = FRAME_TOP_MARGIN (f);
31809 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31810 r->total_cols = FRAME_COLS (f);
31811 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31812 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31813 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31814
31815 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31816 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31817 m->total_cols = FRAME_COLS (f);
31818 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31819 m->total_lines = 1;
31820 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31821
31822 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31823 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31824 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31825
31826 /* The default ellipsis glyphs `...'. */
31827 for (i = 0; i < 3; ++i)
31828 default_invis_vector[i] = make_number ('.');
31829 }
31830
31831 {
31832 /* Allocate the buffer for frame titles.
31833 Also used for `format-mode-line'. */
31834 int size = 100;
31835 mode_line_noprop_buf = xmalloc (size);
31836 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31837 mode_line_noprop_ptr = mode_line_noprop_buf;
31838 mode_line_target = MODE_LINE_DISPLAY;
31839 }
31840
31841 help_echo_showing_p = false;
31842 }
31843
31844 #ifdef HAVE_WINDOW_SYSTEM
31845
31846 /* Platform-independent portion of hourglass implementation. */
31847
31848 /* Timer function of hourglass_atimer. */
31849
31850 static void
31851 show_hourglass (struct atimer *timer)
31852 {
31853 /* The timer implementation will cancel this timer automatically
31854 after this function has run. Set hourglass_atimer to null
31855 so that we know the timer doesn't have to be canceled. */
31856 hourglass_atimer = NULL;
31857
31858 if (!hourglass_shown_p)
31859 {
31860 Lisp_Object tail, frame;
31861
31862 block_input ();
31863
31864 FOR_EACH_FRAME (tail, frame)
31865 {
31866 struct frame *f = XFRAME (frame);
31867
31868 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31869 && FRAME_RIF (f)->show_hourglass)
31870 FRAME_RIF (f)->show_hourglass (f);
31871 }
31872
31873 hourglass_shown_p = true;
31874 unblock_input ();
31875 }
31876 }
31877
31878 /* Cancel a currently active hourglass timer, and start a new one. */
31879
31880 void
31881 start_hourglass (void)
31882 {
31883 struct timespec delay;
31884
31885 cancel_hourglass ();
31886
31887 if (INTEGERP (Vhourglass_delay)
31888 && XINT (Vhourglass_delay) > 0)
31889 delay = make_timespec (min (XINT (Vhourglass_delay),
31890 TYPE_MAXIMUM (time_t)),
31891 0);
31892 else if (FLOATP (Vhourglass_delay)
31893 && XFLOAT_DATA (Vhourglass_delay) > 0)
31894 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31895 else
31896 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31897
31898 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31899 show_hourglass, NULL);
31900 }
31901
31902 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31903 shown. */
31904
31905 void
31906 cancel_hourglass (void)
31907 {
31908 if (hourglass_atimer)
31909 {
31910 cancel_atimer (hourglass_atimer);
31911 hourglass_atimer = NULL;
31912 }
31913
31914 if (hourglass_shown_p)
31915 {
31916 Lisp_Object tail, frame;
31917
31918 block_input ();
31919
31920 FOR_EACH_FRAME (tail, frame)
31921 {
31922 struct frame *f = XFRAME (frame);
31923
31924 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31925 && FRAME_RIF (f)->hide_hourglass)
31926 FRAME_RIF (f)->hide_hourglass (f);
31927 #ifdef HAVE_NTGUI
31928 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31929 else if (!FRAME_W32_P (f))
31930 w32_arrow_cursor ();
31931 #endif
31932 }
31933
31934 hourglass_shown_p = false;
31935 unblock_input ();
31936 }
31937 }
31938
31939 #endif /* HAVE_WINDOW_SYSTEM */