; Improve commentary of Info-default-directory-list
[emacs.git] / src / xdisp.c
blob422912e57a6d89b2faca77a9fa330b3a680aca21
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2017 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or (at
11 your option) any later version.
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.
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/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
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.
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.
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.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
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.
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.
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.
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:
85 . try_cursor_movement
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.
91 . try_window_reusing_current_matrix
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).
97 . try_window_id
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.)
105 . try_window
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.)
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.
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.
131 Desired matrices.
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.
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.
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.
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.
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).
176 Frame matrices.
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.
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.
197 Bidirectional display.
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.
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...
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.
240 Bidirectional display and character compositions
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.
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.
267 Moving an iterator in bidirectional text
268 without producing glyphs
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. */
289 #include <config.h>
290 #include <stdio.h>
291 #include <stdlib.h>
292 #include <limits.h>
293 #include <math.h>
295 #include "lisp.h"
296 #include "atimer.h"
297 #include "composite.h"
298 #include "keyboard.h"
299 #include "systime.h"
300 #include "frame.h"
301 #include "window.h"
302 #include "termchar.h"
303 #include "dispextern.h"
304 #include "character.h"
305 #include "buffer.h"
306 #include "charset.h"
307 #include "indent.h"
308 #include "commands.h"
309 #include "keymap.h"
310 #include "disptab.h"
311 #include "termhooks.h"
312 #include "termopts.h"
313 #include "intervals.h"
314 #include "coding.h"
315 #include "region-cache.h"
316 #include "font.h"
317 #include "fontset.h"
318 #include "blockinput.h"
319 #include "xwidget.h"
320 #ifdef HAVE_WINDOW_SYSTEM
321 #include TERM_HEADER
322 #endif /* HAVE_WINDOW_SYSTEM */
324 #ifndef FRAME_X_OUTPUT
325 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
326 #endif
328 #define DISP_INFINITY 10000000
330 /* Holds the list (error). */
331 static Lisp_Object list_of_error;
333 #ifdef HAVE_WINDOW_SYSTEM
335 /* Test if overflow newline into fringe. Called with iterator IT
336 at or past right window margin, and with IT->current_x set. */
338 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
339 (!NILP (Voverflow_newline_into_fringe) \
340 && FRAME_WINDOW_P ((IT)->f) \
341 && ((IT)->bidi_it.paragraph_dir == R2L \
342 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
343 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
344 && (IT)->current_x == (IT)->last_visible_x)
346 #else /* !HAVE_WINDOW_SYSTEM */
347 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
348 #endif /* HAVE_WINDOW_SYSTEM */
350 /* Test if the display element loaded in IT, or the underlying buffer
351 or string character, is a space or a TAB character. This is used
352 to determine where word wrapping can occur. */
354 #define IT_DISPLAYING_WHITESPACE(it) \
355 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
356 || ((STRINGP (it->string) \
357 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
358 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
359 || (it->s \
360 && (it->s[IT_BYTEPOS (*it)] == ' ' \
361 || it->s[IT_BYTEPOS (*it)] == '\t')) \
362 || (IT_BYTEPOS (*it) < ZV_BYTE \
363 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
364 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
366 /* True means print newline to stdout before next mini-buffer message. */
368 bool noninteractive_need_newline;
370 /* True means print newline to message log before next message. */
372 static bool message_log_need_newline;
374 /* Three markers that message_dolog uses.
375 It could allocate them itself, but that causes trouble
376 in handling memory-full errors. */
377 static Lisp_Object message_dolog_marker1;
378 static Lisp_Object message_dolog_marker2;
379 static Lisp_Object message_dolog_marker3;
381 /* The buffer position of the first character appearing entirely or
382 partially on the line of the selected window which contains the
383 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
384 redisplay optimization in redisplay_internal. */
386 static struct text_pos this_line_start_pos;
388 /* Number of characters past the end of the line above, including the
389 terminating newline. */
391 static struct text_pos this_line_end_pos;
393 /* The vertical positions and the height of this line. */
395 static int this_line_vpos;
396 static int this_line_y;
397 static int this_line_pixel_height;
399 /* X position at which this display line starts. Usually zero;
400 negative if first character is partially visible. */
402 static int this_line_start_x;
404 /* The smallest character position seen by move_it_* functions as they
405 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
406 hscrolled lines, see display_line. */
408 static struct text_pos this_line_min_pos;
410 /* Buffer that this_line_.* variables are referring to. */
412 static struct buffer *this_line_buffer;
414 /* True if an overlay arrow has been displayed in this window. */
416 static bool overlay_arrow_seen;
418 /* Vector containing glyphs for an ellipsis `...'. */
420 static Lisp_Object default_invis_vector[3];
422 /* This is the window where the echo area message was displayed. It
423 is always a mini-buffer window, but it may not be the same window
424 currently active as a mini-buffer. */
426 Lisp_Object echo_area_window;
428 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
429 pushes the current message and the value of
430 message_enable_multibyte on the stack, the function restore_message
431 pops the stack and displays MESSAGE again. */
433 static Lisp_Object Vmessage_stack;
435 /* True means multibyte characters were enabled when the echo area
436 message was specified. */
438 static bool message_enable_multibyte;
440 /* At each redisplay cycle, we should refresh everything there is to refresh.
441 To do that efficiently, we use many optimizations that try to make sure we
442 don't waste too much time updating things that haven't changed.
443 The coarsest such optimization is that, in the most common cases, we only
444 look at the selected-window.
446 To know whether other windows should be considered for redisplay, we use the
447 variable windows_or_buffers_changed: as long as it is 0, it means that we
448 have not noticed anything that should require updating anything else than
449 the selected-window. If it is set to REDISPLAY_SOME, it means that since
450 last redisplay, some changes have been made which could impact other
451 windows. To know which ones need redisplay, every buffer, window, and frame
452 has a `redisplay' bit, which (if true) means that this object needs to be
453 redisplayed. If windows_or_buffers_changed is 0, we know there's no point
454 looking for those `redisplay' bits (actually, there might be some such bits
455 set, but then only on objects which aren't displayed anyway).
457 OTOH if it's non-zero we wil have to loop through all windows and then check
458 the `redisplay' bit of the corresponding window, frame, and buffer, in order
459 to decide whether that window needs attention or not. Note that we can't
460 just look at the frame's redisplay bit to decide that the whole frame can be
461 skipped, since even if the frame's redisplay bit is unset, some of its
462 windows's redisplay bits may be set.
464 Mostly for historical reasons, windows_or_buffers_changed can also take
465 other non-zero values. In that case, the precise value doesn't matter (it
466 encodes the cause of the setting but is only used for debugging purposes),
467 and what it means is that we shouldn't pay attention to any `redisplay' bits
468 and we should simply try and redisplay every window out there. */
470 int windows_or_buffers_changed;
472 /* Nonzero if we should redraw the mode lines on the next redisplay.
473 Similarly to `windows_or_buffers_changed', If it has value REDISPLAY_SOME,
474 then only redisplay the mode lines in those buffers/windows/frames where the
475 `redisplay' bit has been set.
476 For any other value, redisplay all mode lines (the number used is then only
477 used to track down the cause for this full-redisplay).
479 Since the frame title uses the same %-constructs as the mode line
480 (except %c, %C, and %l), if this variable is non-zero, we also consider
481 redisplaying the title of each frame, see x_consider_frame_title.
483 The `redisplay' bits are the same as those used for
484 windows_or_buffers_changed, and setting windows_or_buffers_changed also
485 causes recomputation of the mode lines of all those windows. IOW this
486 variable only has an effect if windows_or_buffers_changed is zero, in which
487 case we should only need to redisplay the mode-line of those objects with
488 a `redisplay' bit set but not the window's text content (tho we may still
489 need to refresh the text content of the selected-window). */
491 int update_mode_lines;
493 /* True after display_mode_line if %l was used and it displayed a
494 line number. */
496 static bool line_number_displayed;
498 /* The name of the *Messages* buffer, a string. */
500 static Lisp_Object Vmessages_buffer_name;
502 /* Current, index 0, and last displayed echo area message. Either
503 buffers from echo_buffers, or nil to indicate no message. */
505 Lisp_Object echo_area_buffer[2];
507 /* The buffers referenced from echo_area_buffer. */
509 static Lisp_Object echo_buffer[2];
511 /* A vector saved used in with_area_buffer to reduce consing. */
513 static Lisp_Object Vwith_echo_area_save_vector;
515 /* True means display_echo_area should display the last echo area
516 message again. Set by redisplay_preserve_echo_area. */
518 static bool display_last_displayed_message_p;
520 /* True if echo area is being used by print; false if being used by
521 message. */
523 static bool message_buf_print;
525 /* Set to true in clear_message to make redisplay_internal aware
526 of an emptied echo area. */
528 static bool message_cleared_p;
530 /* A scratch glyph row with contents used for generating truncation
531 glyphs. Also used in direct_output_for_insert. */
533 #define MAX_SCRATCH_GLYPHS 100
534 static struct glyph_row scratch_glyph_row;
535 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
537 /* Ascent and height of the last line processed by move_it_to. */
539 static int last_height;
541 /* True if there's a help-echo in the echo area. */
543 bool help_echo_showing_p;
545 /* The maximum distance to look ahead for text properties. Values
546 that are too small let us call compute_char_face and similar
547 functions too often which is expensive. Values that are too large
548 let us call compute_char_face and alike too often because we
549 might not be interested in text properties that far away. */
551 #define TEXT_PROP_DISTANCE_LIMIT 100
553 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
554 iterator state and later restore it. This is needed because the
555 bidi iterator on bidi.c keeps a stacked cache of its states, which
556 is really a singleton. When we use scratch iterator objects to
557 move around the buffer, we can cause the bidi cache to be pushed or
558 popped, and therefore we need to restore the cache state when we
559 return to the original iterator. */
560 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
561 do { \
562 if (CACHE) \
563 bidi_unshelve_cache (CACHE, true); \
564 ITCOPY = ITORIG; \
565 CACHE = bidi_shelve_cache (); \
566 } while (false)
568 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
569 do { \
570 if (pITORIG != pITCOPY) \
571 *(pITORIG) = *(pITCOPY); \
572 bidi_unshelve_cache (CACHE, false); \
573 CACHE = NULL; \
574 } while (false)
576 /* Functions to mark elements as needing redisplay. */
577 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
579 void
580 redisplay_other_windows (void)
582 if (!windows_or_buffers_changed)
583 windows_or_buffers_changed = REDISPLAY_SOME;
586 void
587 wset_redisplay (struct window *w)
589 /* Beware: selected_window can be nil during early stages. */
590 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
591 redisplay_other_windows ();
592 w->redisplay = true;
595 void
596 fset_redisplay (struct frame *f)
598 redisplay_other_windows ();
599 f->redisplay = true;
602 void
603 bset_redisplay (struct buffer *b)
605 int count = buffer_window_count (b);
606 if (count > 0)
608 /* ... it's visible in other window than selected, */
609 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
610 redisplay_other_windows ();
611 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
612 so that if we later set windows_or_buffers_changed, this buffer will
613 not be omitted. */
614 b->text->redisplay = true;
618 void
619 bset_update_mode_line (struct buffer *b)
621 if (!update_mode_lines)
622 update_mode_lines = REDISPLAY_SOME;
623 b->text->redisplay = true;
626 DEFUN ("set-buffer-redisplay", Fset_buffer_redisplay,
627 Sset_buffer_redisplay, 4, 4, 0,
628 doc: /* Mark the current buffer for redisplay.
629 This function may be passed to `add-variable-watcher'. */)
630 (Lisp_Object symbol, Lisp_Object newval, Lisp_Object op, Lisp_Object where)
632 bset_update_mode_line (current_buffer);
633 current_buffer->prevent_redisplay_optimizations_p = true;
634 return Qnil;
637 #ifdef GLYPH_DEBUG
639 /* True means print traces of redisplay if compiled with
640 GLYPH_DEBUG defined. */
642 bool trace_redisplay_p;
644 #endif /* GLYPH_DEBUG */
646 #ifdef DEBUG_TRACE_MOVE
647 /* True means trace with TRACE_MOVE to stderr. */
648 static bool trace_move;
650 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
651 #else
652 #define TRACE_MOVE(x) (void) 0
653 #endif
655 /* Buffer being redisplayed -- for redisplay_window_error. */
657 static struct buffer *displayed_buffer;
659 /* Value returned from text property handlers (see below). */
661 enum prop_handled
663 HANDLED_NORMALLY,
664 HANDLED_RECOMPUTE_PROPS,
665 HANDLED_OVERLAY_STRING_CONSUMED,
666 HANDLED_RETURN
669 /* A description of text properties that redisplay is interested
670 in. */
672 struct props
674 /* The symbol index of the name of the property. */
675 short name;
677 /* A unique index for the property. */
678 enum prop_idx idx;
680 /* A handler function called to set up iterator IT from the property
681 at IT's current position. Value is used to steer handle_stop. */
682 enum prop_handled (*handler) (struct it *it);
685 static enum prop_handled handle_face_prop (struct it *);
686 static enum prop_handled handle_invisible_prop (struct it *);
687 static enum prop_handled handle_display_prop (struct it *);
688 static enum prop_handled handle_composition_prop (struct it *);
689 static enum prop_handled handle_overlay_change (struct it *);
690 static enum prop_handled handle_fontified_prop (struct it *);
692 /* Properties handled by iterators. */
694 static struct props it_props[] =
696 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
697 /* Handle `face' before `display' because some sub-properties of
698 `display' need to know the face. */
699 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
700 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
701 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
702 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
703 {0, 0, NULL}
706 /* Value is the position described by X. If X is a marker, value is
707 the marker_position of X. Otherwise, value is X. */
709 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
711 /* Enumeration returned by some move_it_.* functions internally. */
713 enum move_it_result
715 /* Not used. Undefined value. */
716 MOVE_UNDEFINED,
718 /* Move ended at the requested buffer position or ZV. */
719 MOVE_POS_MATCH_OR_ZV,
721 /* Move ended at the requested X pixel position. */
722 MOVE_X_REACHED,
724 /* Move within a line ended at the end of a line that must be
725 continued. */
726 MOVE_LINE_CONTINUED,
728 /* Move within a line ended at the end of a line that would
729 be displayed truncated. */
730 MOVE_LINE_TRUNCATED,
732 /* Move within a line ended at a line end. */
733 MOVE_NEWLINE_OR_CR
736 /* This counter is used to clear the face cache every once in a while
737 in redisplay_internal. It is incremented for each redisplay.
738 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
739 cleared. */
741 #define CLEAR_FACE_CACHE_COUNT 500
742 static int clear_face_cache_count;
744 /* Similarly for the image cache. */
746 #ifdef HAVE_WINDOW_SYSTEM
747 #define CLEAR_IMAGE_CACHE_COUNT 101
748 static int clear_image_cache_count;
750 /* Null glyph slice */
751 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
752 #endif
754 /* True while redisplay_internal is in progress. */
756 bool redisplaying_p;
758 /* If a string, XTread_socket generates an event to display that string.
759 (The display is done in read_char.) */
761 Lisp_Object help_echo_string;
762 Lisp_Object help_echo_window;
763 Lisp_Object help_echo_object;
764 ptrdiff_t help_echo_pos;
766 /* Temporary variable for XTread_socket. */
768 Lisp_Object previous_help_echo_string;
770 /* Platform-independent portion of hourglass implementation. */
772 #ifdef HAVE_WINDOW_SYSTEM
774 /* True means an hourglass cursor is currently shown. */
775 static bool hourglass_shown_p;
777 /* If non-null, an asynchronous timer that, when it expires, displays
778 an hourglass cursor on all frames. */
779 static struct atimer *hourglass_atimer;
781 #endif /* HAVE_WINDOW_SYSTEM */
783 /* Default number of seconds to wait before displaying an hourglass
784 cursor. */
785 #define DEFAULT_HOURGLASS_DELAY 1
787 #ifdef HAVE_WINDOW_SYSTEM
789 /* Default pixel width of `thin-space' display method. */
790 #define THIN_SPACE_WIDTH 1
792 #endif /* HAVE_WINDOW_SYSTEM */
794 /* Function prototypes. */
796 static void setup_for_ellipsis (struct it *, int);
797 static void set_iterator_to_next (struct it *, bool);
798 static void mark_window_display_accurate_1 (struct window *, bool);
799 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
800 static bool cursor_row_p (struct glyph_row *);
801 static int redisplay_mode_lines (Lisp_Object, bool);
803 static void handle_line_prefix (struct it *);
805 static void handle_stop_backwards (struct it *, ptrdiff_t);
806 static void unwind_with_echo_area_buffer (Lisp_Object);
807 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
808 static bool current_message_1 (ptrdiff_t, Lisp_Object);
809 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
810 static void set_message (Lisp_Object);
811 static bool set_message_1 (ptrdiff_t, Lisp_Object);
812 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
813 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
814 static void unwind_redisplay (void);
815 static void extend_face_to_end_of_line (struct it *);
816 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
817 static void push_it (struct it *, struct text_pos *);
818 static void iterate_out_of_display_property (struct it *);
819 static void pop_it (struct it *);
820 static void redisplay_internal (void);
821 static void echo_area_display (bool);
822 static void block_buffer_flips (void);
823 static void unblock_buffer_flips (void);
824 static void redisplay_windows (Lisp_Object);
825 static void redisplay_window (Lisp_Object, bool);
826 static Lisp_Object redisplay_window_error (Lisp_Object);
827 static Lisp_Object redisplay_window_0 (Lisp_Object);
828 static Lisp_Object redisplay_window_1 (Lisp_Object);
829 static bool set_cursor_from_row (struct window *, struct glyph_row *,
830 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
831 int, int);
832 static bool cursor_row_fully_visible_p (struct window *, bool, bool);
833 static bool update_menu_bar (struct frame *, bool, bool);
834 static bool try_window_reusing_current_matrix (struct window *);
835 static int try_window_id (struct window *);
836 static void maybe_produce_line_number (struct it *);
837 static bool should_produce_line_number (struct it *);
838 static bool display_line (struct it *, int);
839 static int display_mode_lines (struct window *);
840 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
841 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
842 Lisp_Object, bool);
843 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
844 Lisp_Object);
845 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
846 static void display_menu_bar (struct window *);
847 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
848 ptrdiff_t *);
849 static void pint2str (register char *, register int, register ptrdiff_t);
851 static int display_string (const char *, Lisp_Object, Lisp_Object,
852 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
853 static void compute_line_metrics (struct it *);
854 static void run_redisplay_end_trigger_hook (struct it *);
855 static bool get_overlay_strings (struct it *, ptrdiff_t);
856 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
857 static void next_overlay_string (struct it *);
858 static void reseat (struct it *, struct text_pos, bool);
859 static void reseat_1 (struct it *, struct text_pos, bool);
860 static bool next_element_from_display_vector (struct it *);
861 static bool next_element_from_string (struct it *);
862 static bool next_element_from_c_string (struct it *);
863 static bool next_element_from_buffer (struct it *);
864 static bool next_element_from_composition (struct it *);
865 static bool next_element_from_image (struct it *);
866 static bool next_element_from_stretch (struct it *);
867 static bool next_element_from_xwidget (struct it *);
868 static void load_overlay_strings (struct it *, ptrdiff_t);
869 static bool get_next_display_element (struct it *);
870 static enum move_it_result
871 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
872 enum move_operation_enum);
873 static void get_visually_first_element (struct it *);
874 static void compute_stop_pos (struct it *);
875 static int face_before_or_after_it_pos (struct it *, bool);
876 static ptrdiff_t next_overlay_change (ptrdiff_t);
877 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
878 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
879 static int handle_single_display_spec (struct it *, Lisp_Object,
880 Lisp_Object, Lisp_Object,
881 struct text_pos *, ptrdiff_t, int, bool);
882 static int underlying_face_id (struct it *);
884 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
885 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
887 #ifdef HAVE_WINDOW_SYSTEM
889 static void update_tool_bar (struct frame *, bool);
890 static void x_draw_bottom_divider (struct window *w);
891 static void notice_overwritten_cursor (struct window *,
892 enum glyph_row_area,
893 int, int, int, int);
894 static int normal_char_height (struct font *, int);
895 static void normal_char_ascent_descent (struct font *, int, int *, int *);
897 static void append_stretch_glyph (struct it *, Lisp_Object,
898 int, int, int);
900 static Lisp_Object get_it_property (struct it *, Lisp_Object);
901 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
902 struct font *, int, bool);
904 #endif /* HAVE_WINDOW_SYSTEM */
906 static void produce_special_glyphs (struct it *, enum display_element_type);
907 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
908 static bool coords_in_mouse_face_p (struct window *, int, int);
912 /***********************************************************************
913 Window display dimensions
914 ***********************************************************************/
916 /* Return the bottom boundary y-position for text lines in window W.
917 This is the first y position at which a line cannot start.
918 It is relative to the top of the window.
920 This is the height of W minus the height of a mode line, if any. */
923 window_text_bottom_y (struct window *w)
925 int height = WINDOW_PIXEL_HEIGHT (w);
927 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
929 if (window_wants_mode_line (w))
930 height -= CURRENT_MODE_LINE_HEIGHT (w);
932 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
934 return height;
937 /* Return the pixel width of display area AREA of window W.
938 ANY_AREA means return the total width of W, not including
939 fringes to the left and right of the window. */
942 window_box_width (struct window *w, enum glyph_row_area area)
944 int width = w->pixel_width;
946 if (!w->pseudo_window_p)
948 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
949 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
951 if (area == TEXT_AREA)
952 width -= (WINDOW_MARGINS_WIDTH (w)
953 + WINDOW_FRINGES_WIDTH (w));
954 else if (area == LEFT_MARGIN_AREA)
955 width = WINDOW_LEFT_MARGIN_WIDTH (w);
956 else if (area == RIGHT_MARGIN_AREA)
957 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
960 /* With wide margins, fringes, etc. we might end up with a negative
961 width, correct that here. */
962 return max (0, width);
966 /* Return the pixel height of the display area of window W, not
967 including mode lines of W, if any. */
970 window_box_height (struct window *w)
972 struct frame *f = XFRAME (w->frame);
973 int height = WINDOW_PIXEL_HEIGHT (w);
975 eassert (height >= 0);
977 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
978 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
980 /* Note: the code below that determines the mode-line/header-line
981 height is essentially the same as that contained in the macro
982 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
983 the appropriate glyph row has its `mode_line_p' flag set,
984 and if it doesn't, uses estimate_mode_line_height instead. */
986 if (window_wants_mode_line (w))
988 struct glyph_row *ml_row
989 = (w->current_matrix && w->current_matrix->rows
990 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
991 : 0);
992 if (ml_row && ml_row->mode_line_p)
993 height -= ml_row->height;
994 else
995 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
998 if (window_wants_header_line (w))
1000 struct glyph_row *hl_row
1001 = (w->current_matrix && w->current_matrix->rows
1002 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1003 : 0);
1004 if (hl_row && hl_row->mode_line_p)
1005 height -= hl_row->height;
1006 else
1007 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1010 /* With a very small font and a mode-line that's taller than
1011 default, we might end up with a negative height. */
1012 return max (0, height);
1015 /* Return the window-relative coordinate of the left edge of display
1016 area AREA of window W. ANY_AREA means return the left edge of the
1017 whole window, to the right of the left fringe of W. */
1020 window_box_left_offset (struct window *w, enum glyph_row_area area)
1022 int x;
1024 if (w->pseudo_window_p)
1025 return 0;
1027 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1029 if (area == TEXT_AREA)
1030 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1031 + window_box_width (w, LEFT_MARGIN_AREA));
1032 else if (area == RIGHT_MARGIN_AREA)
1033 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1034 + window_box_width (w, LEFT_MARGIN_AREA)
1035 + window_box_width (w, TEXT_AREA)
1036 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1038 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1039 else if (area == LEFT_MARGIN_AREA
1040 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1041 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1043 /* Don't return more than the window's pixel width. */
1044 return min (x, w->pixel_width);
1048 /* Return the window-relative coordinate of the right edge of display
1049 area AREA of window W. ANY_AREA means return the right edge of the
1050 whole window, to the left of the right fringe of W. */
1052 static int
1053 window_box_right_offset (struct window *w, enum glyph_row_area area)
1055 /* Don't return more than the window's pixel width. */
1056 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1057 w->pixel_width);
1060 /* Return the frame-relative coordinate of the left edge of display
1061 area AREA of window W. ANY_AREA means return the left edge of the
1062 whole window, to the right of the left fringe of W. */
1065 window_box_left (struct window *w, enum glyph_row_area area)
1067 struct frame *f = XFRAME (w->frame);
1068 int x;
1070 if (w->pseudo_window_p)
1071 return FRAME_INTERNAL_BORDER_WIDTH (f);
1073 x = (WINDOW_LEFT_EDGE_X (w)
1074 + window_box_left_offset (w, area));
1076 return x;
1080 /* Return the frame-relative coordinate of the right edge of display
1081 area AREA of window W. ANY_AREA means return the right edge of the
1082 whole window, to the left of the right fringe of W. */
1085 window_box_right (struct window *w, enum glyph_row_area area)
1087 return window_box_left (w, area) + window_box_width (w, area);
1090 /* Get the bounding box of the display area AREA of window W, without
1091 mode lines, in frame-relative coordinates. ANY_AREA means the
1092 whole window, not including the left and right fringes of
1093 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1094 coordinates of the upper-left corner of the box. Return in
1095 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1097 void
1098 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1099 int *box_y, int *box_width, int *box_height)
1101 if (box_width)
1102 *box_width = window_box_width (w, area);
1103 if (box_height)
1104 *box_height = window_box_height (w);
1105 if (box_x)
1106 *box_x = window_box_left (w, area);
1107 if (box_y)
1109 *box_y = WINDOW_TOP_EDGE_Y (w);
1110 if (window_wants_header_line (w))
1111 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1115 #ifdef HAVE_WINDOW_SYSTEM
1117 /* Get the bounding box of the display area AREA of window W, without
1118 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1119 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1120 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1121 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1122 box. */
1124 static void
1125 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1126 int *bottom_right_x, int *bottom_right_y)
1128 window_box (w, ANY_AREA, top_left_x, top_left_y,
1129 bottom_right_x, bottom_right_y);
1130 *bottom_right_x += *top_left_x;
1131 *bottom_right_y += *top_left_y;
1134 #endif /* HAVE_WINDOW_SYSTEM */
1136 /***********************************************************************
1137 Utilities
1138 ***********************************************************************/
1140 /* Return the bottom y-position of the line the iterator IT is in.
1141 This can modify IT's settings. */
1144 line_bottom_y (struct it *it)
1146 int line_height = it->max_ascent + it->max_descent;
1147 int line_top_y = it->current_y;
1149 if (line_height == 0)
1151 if (last_height)
1152 line_height = last_height;
1153 else if (IT_CHARPOS (*it) < ZV)
1155 move_it_by_lines (it, 1);
1156 line_height = (it->max_ascent || it->max_descent
1157 ? it->max_ascent + it->max_descent
1158 : last_height);
1160 else
1162 struct glyph_row *row = it->glyph_row;
1164 /* Use the default character height. */
1165 it->glyph_row = NULL;
1166 it->what = IT_CHARACTER;
1167 it->c = ' ';
1168 it->len = 1;
1169 PRODUCE_GLYPHS (it);
1170 line_height = it->ascent + it->descent;
1171 it->glyph_row = row;
1175 return line_top_y + line_height;
1178 DEFUN ("line-pixel-height", Fline_pixel_height,
1179 Sline_pixel_height, 0, 0, 0,
1180 doc: /* Return height in pixels of text line in the selected window.
1182 Value is the height in pixels of the line at point. */)
1183 (void)
1185 struct it it;
1186 struct text_pos pt;
1187 struct window *w = XWINDOW (selected_window);
1188 struct buffer *old_buffer = NULL;
1189 Lisp_Object result;
1191 if (XBUFFER (w->contents) != current_buffer)
1193 old_buffer = current_buffer;
1194 set_buffer_internal_1 (XBUFFER (w->contents));
1196 SET_TEXT_POS (pt, PT, PT_BYTE);
1197 start_display (&it, w, pt);
1198 it.vpos = it.current_y = 0;
1199 last_height = 0;
1200 result = make_number (line_bottom_y (&it));
1201 if (old_buffer)
1202 set_buffer_internal_1 (old_buffer);
1204 return result;
1207 /* Return the default pixel height of text lines in window W. The
1208 value is the canonical height of the W frame's default font, plus
1209 any extra space required by the line-spacing variable or frame
1210 parameter.
1212 Implementation note: this ignores any line-spacing text properties
1213 put on the newline characters. This is because those properties
1214 only affect the _screen_ line ending in the newline (i.e., in a
1215 continued line, only the last screen line will be affected), which
1216 means only a small number of lines in a buffer can ever use this
1217 feature. Since this function is used to compute the default pixel
1218 equivalent of text lines in a window, we can safely ignore those
1219 few lines. For the same reasons, we ignore the line-height
1220 properties. */
1222 default_line_pixel_height (struct window *w)
1224 struct frame *f = WINDOW_XFRAME (w);
1225 int height = FRAME_LINE_HEIGHT (f);
1227 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1229 struct buffer *b = XBUFFER (w->contents);
1230 Lisp_Object val = BVAR (b, extra_line_spacing);
1232 if (NILP (val))
1233 val = BVAR (&buffer_defaults, extra_line_spacing);
1234 if (!NILP (val))
1236 if (RANGED_INTEGERP (0, val, INT_MAX))
1237 height += XFASTINT (val);
1238 else if (FLOATP (val))
1240 int addon = XFLOAT_DATA (val) * height + 0.5;
1242 if (addon >= 0)
1243 height += addon;
1246 else
1247 height += f->extra_line_spacing;
1250 return height;
1253 /* Subroutine of pos_visible_p below. Extracts a display string, if
1254 any, from the display spec given as its argument. */
1255 static Lisp_Object
1256 string_from_display_spec (Lisp_Object spec)
1258 if (VECTORP (spec))
1260 for (ptrdiff_t i = 0; i < ASIZE (spec); i++)
1261 if (STRINGP (AREF (spec, i)))
1262 return AREF (spec, i);
1264 else
1266 for (; CONSP (spec); spec = XCDR (spec))
1267 if (STRINGP (XCAR (spec)))
1268 return XCAR (spec);
1270 return spec;
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)
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);
1284 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1285 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1287 return window_hscroll;
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). */
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)
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;
1307 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1308 return visible_p;
1310 if (XBUFFER (w->contents) != current_buffer)
1312 old_buffer = current_buffer;
1313 set_buffer_internal_1 (XBUFFER (w->contents));
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. Also, someone might narrow-to-region and immediately
1320 call a scroll function. */
1321 if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
1322 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1324 /* If the top of the window is after CHARPOS, the latter is surely
1325 not visible. */
1326 if (charpos >= 0 && CHARPOS (top) > charpos)
1327 return visible_p;
1329 /* Some Lisp hook could call us in the middle of redisplaying this
1330 very window. If, by some bad luck, we are retrying redisplay
1331 because we found that the mode-line height and/or header-line
1332 height needs to be updated, the assignment of mode_line_height
1333 and header_line_height below could disrupt that, due to the
1334 selected/nonselected window dance during mode-line display, and
1335 we could infloop. Avoid that. */
1336 int prev_mode_line_height = w->mode_line_height;
1337 int prev_header_line_height = w->header_line_height;
1338 /* Compute exact mode line heights. */
1339 if (window_wants_mode_line (w))
1341 Lisp_Object window_mode_line_format
1342 = window_parameter (w, Qmode_line_format);
1344 w->mode_line_height
1345 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1346 NILP (window_mode_line_format)
1347 ? BVAR (current_buffer, mode_line_format)
1348 : window_mode_line_format);
1351 if (window_wants_header_line (w))
1353 Lisp_Object window_header_line_format
1354 = window_parameter (w, Qheader_line_format);
1356 w->header_line_height
1357 = display_mode_line (w, HEADER_LINE_FACE_ID,
1358 NILP (window_header_line_format)
1359 ? BVAR (current_buffer, header_line_format)
1360 : window_header_line_format);
1363 start_display (&it, w, top);
1364 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1365 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1367 if (charpos >= 0
1368 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1369 && IT_CHARPOS (it) >= charpos)
1370 /* When scanning backwards under bidi iteration, move_it_to
1371 stops at or _before_ CHARPOS, because it stops at or to
1372 the _right_ of the character at CHARPOS. */
1373 || (it.bidi_p && it.bidi_it.scan_dir == -1
1374 && IT_CHARPOS (it) <= charpos)))
1376 /* We have reached CHARPOS, or passed it. How the call to
1377 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1378 or covered by a display property, move_it_to stops at the end
1379 of the invisible text, to the right of CHARPOS. (ii) If
1380 CHARPOS is in a display vector, move_it_to stops on its last
1381 glyph. */
1382 int top_x = it.current_x;
1383 int top_y = it.current_y;
1384 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1385 int bottom_y;
1386 struct it save_it;
1387 void *save_it_data = NULL;
1389 /* Calling line_bottom_y may change it.method, it.position, etc. */
1390 SAVE_IT (save_it, it, save_it_data);
1391 last_height = 0;
1392 bottom_y = line_bottom_y (&it);
1393 if (top_y < window_top_y)
1394 visible_p = bottom_y > window_top_y;
1395 else if (top_y < it.last_visible_y)
1396 visible_p = true;
1397 if (bottom_y >= it.last_visible_y
1398 && it.bidi_p && it.bidi_it.scan_dir == -1
1399 && IT_CHARPOS (it) < charpos)
1401 /* When the last line of the window is scanned backwards
1402 under bidi iteration, we could be duped into thinking
1403 that we have passed CHARPOS, when in fact move_it_to
1404 simply stopped short of CHARPOS because it reached
1405 last_visible_y. To see if that's what happened, we call
1406 move_it_to again with a slightly larger vertical limit,
1407 and see if it actually moved vertically; if it did, we
1408 didn't really reach CHARPOS, which is beyond window end. */
1409 /* Why 10? because we don't know how many canonical lines
1410 will the height of the next line(s) be. So we guess. */
1411 int ten_more_lines = 10 * default_line_pixel_height (w);
1413 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1414 MOVE_TO_POS | MOVE_TO_Y);
1415 if (it.current_y > top_y)
1416 visible_p = false;
1419 RESTORE_IT (&it, &save_it, save_it_data);
1420 if (visible_p)
1422 if (it.method == GET_FROM_DISPLAY_VECTOR)
1424 /* We stopped on the last glyph of a display vector.
1425 Try and recompute. Hack alert! */
1426 if (charpos < 2 || top.charpos >= charpos)
1427 top_x = it.glyph_row->x;
1428 else
1430 struct it it2, it2_prev;
1431 /* The idea is to get to the previous buffer
1432 position, consume the character there, and use
1433 the pixel coordinates we get after that. But if
1434 the previous buffer position is also displayed
1435 from a display vector, we need to consume all of
1436 the glyphs from that display vector. */
1437 start_display (&it2, w, top);
1438 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1439 /* If we didn't get to CHARPOS - 1, there's some
1440 replacing display property at that position, and
1441 we stopped after it. That is exactly the place
1442 whose coordinates we want. */
1443 if (IT_CHARPOS (it2) != charpos - 1)
1444 it2_prev = it2;
1445 else
1447 /* Iterate until we get out of the display
1448 vector that displays the character at
1449 CHARPOS - 1. */
1450 do {
1451 get_next_display_element (&it2);
1452 PRODUCE_GLYPHS (&it2);
1453 it2_prev = it2;
1454 set_iterator_to_next (&it2, true);
1455 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1456 && IT_CHARPOS (it2) < charpos);
1458 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1459 || it2_prev.current_x > it2_prev.last_visible_x)
1460 top_x = it.glyph_row->x;
1461 else
1463 top_x = it2_prev.current_x;
1464 top_y = it2_prev.current_y;
1468 else if (IT_CHARPOS (it) != charpos)
1470 Lisp_Object cpos = make_number (charpos);
1471 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1472 Lisp_Object string = string_from_display_spec (spec);
1473 struct text_pos tpos;
1474 bool newline_in_string
1475 = (STRINGP (string)
1476 && memchr (SDATA (string), '\n', SBYTES (string)));
1478 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1479 bool replacing_spec_p
1480 = (!NILP (spec)
1481 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1482 charpos, FRAME_WINDOW_P (it.f)));
1483 /* The tricky code below is needed because there's a
1484 discrepancy between move_it_to and how we set cursor
1485 when PT is at the beginning of a portion of text
1486 covered by a display property or an overlay with a
1487 display property, or the display line ends in a
1488 newline from a display string. move_it_to will stop
1489 _after_ such display strings, whereas
1490 set_cursor_from_row conspires with cursor_row_p to
1491 place the cursor on the first glyph produced from the
1492 display string. */
1494 /* We have overshoot PT because it is covered by a
1495 display property that replaces the text it covers.
1496 If the string includes embedded newlines, we are also
1497 in the wrong display line. Backtrack to the correct
1498 line, where the display property begins. */
1499 if (replacing_spec_p)
1501 Lisp_Object startpos, endpos;
1502 EMACS_INT start, end;
1503 struct it it3;
1505 /* Find the first and the last buffer positions
1506 covered by the display string. */
1507 endpos =
1508 Fnext_single_char_property_change (cpos, Qdisplay,
1509 Qnil, Qnil);
1510 startpos =
1511 Fprevious_single_char_property_change (endpos, Qdisplay,
1512 Qnil, Qnil);
1513 start = XFASTINT (startpos);
1514 end = XFASTINT (endpos);
1515 /* Move to the last buffer position before the
1516 display property. */
1517 start_display (&it3, w, top);
1518 if (start > CHARPOS (top))
1519 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1520 /* Move forward one more line if the position before
1521 the display string is a newline or if it is the
1522 rightmost character on a line that is
1523 continued or word-wrapped. */
1524 if (it3.method == GET_FROM_BUFFER
1525 && (it3.c == '\n'
1526 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1527 move_it_by_lines (&it3, 1);
1528 else if (move_it_in_display_line_to (&it3, -1,
1529 it3.current_x
1530 + it3.pixel_width,
1531 MOVE_TO_X)
1532 == MOVE_LINE_CONTINUED)
1534 move_it_by_lines (&it3, 1);
1535 /* When we are under word-wrap, the #$@%!
1536 move_it_by_lines moves 2 lines, so we need to
1537 fix that up. */
1538 if (it3.line_wrap == WORD_WRAP)
1539 move_it_by_lines (&it3, -1);
1542 /* Record the vertical coordinate of the display
1543 line where we wound up. */
1544 top_y = it3.current_y;
1545 if (it3.bidi_p)
1547 /* When characters are reordered for display,
1548 the character displayed to the left of the
1549 display string could be _after_ the display
1550 property in the logical order. Use the
1551 smallest vertical position of these two. */
1552 start_display (&it3, w, top);
1553 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1554 if (it3.current_y < top_y)
1555 top_y = it3.current_y;
1557 /* Move from the top of the window to the beginning
1558 of the display line where the display string
1559 begins. */
1560 start_display (&it3, w, top);
1561 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1562 /* If it3_moved stays false after the 'while' loop
1563 below, that means we already were at a newline
1564 before the loop (e.g., the display string begins
1565 with a newline), so we don't need to (and cannot)
1566 inspect the glyphs of it3.glyph_row, because
1567 PRODUCE_GLYPHS will not produce anything for a
1568 newline, and thus it3.glyph_row stays at its
1569 stale content it got at top of the window. */
1570 bool it3_moved = false;
1571 /* Finally, advance the iterator until we hit the
1572 first display element whose character position is
1573 CHARPOS, or until the first newline from the
1574 display string, which signals the end of the
1575 display line. */
1576 while (get_next_display_element (&it3))
1578 PRODUCE_GLYPHS (&it3);
1579 if (IT_CHARPOS (it3) == charpos
1580 || ITERATOR_AT_END_OF_LINE_P (&it3))
1581 break;
1582 it3_moved = true;
1583 set_iterator_to_next (&it3, false);
1585 top_x = it3.current_x - it3.pixel_width;
1586 /* Normally, we would exit the above loop because we
1587 found the display element whose character
1588 position is CHARPOS. For the contingency that we
1589 didn't, and stopped at the first newline from the
1590 display string, move back over the glyphs
1591 produced from the string, until we find the
1592 rightmost glyph not from the string. */
1593 if (it3_moved
1594 && newline_in_string
1595 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1597 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1598 + it3.glyph_row->used[TEXT_AREA];
1600 while (EQ ((g - 1)->object, string))
1602 --g;
1603 top_x -= g->pixel_width;
1605 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1606 + it3.glyph_row->used[TEXT_AREA]);
1611 *x = top_x;
1612 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1613 *rtop = max (0, window_top_y - top_y);
1614 *rbot = max (0, bottom_y - it.last_visible_y);
1615 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1616 - max (top_y, window_top_y)));
1617 *vpos = it.vpos;
1618 if (it.bidi_it.paragraph_dir == R2L)
1619 r2l = true;
1622 else
1624 /* Either we were asked to provide info about WINDOW_END, or
1625 CHARPOS is in the partially visible glyph row at end of
1626 window. */
1627 struct it it2;
1628 void *it2data = NULL;
1630 SAVE_IT (it2, it, it2data);
1631 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1632 move_it_by_lines (&it, 1);
1633 if (charpos < IT_CHARPOS (it)
1634 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1636 visible_p = true;
1637 RESTORE_IT (&it2, &it2, it2data);
1638 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1639 *x = it2.current_x;
1640 *y = it2.current_y + it2.max_ascent - it2.ascent;
1641 *rtop = max (0, -it2.current_y);
1642 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1643 - it.last_visible_y));
1644 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1645 it.last_visible_y)
1646 - max (it2.current_y,
1647 WINDOW_HEADER_LINE_HEIGHT (w))));
1648 *vpos = it2.vpos;
1649 if (it2.bidi_it.paragraph_dir == R2L)
1650 r2l = true;
1652 else
1653 bidi_unshelve_cache (it2data, true);
1655 bidi_unshelve_cache (itdata, false);
1657 if (old_buffer)
1658 set_buffer_internal_1 (old_buffer);
1660 if (visible_p)
1662 if (w->hscroll > 0)
1663 *x -=
1664 window_hscroll_limited (w, WINDOW_XFRAME (w))
1665 * WINDOW_FRAME_COLUMN_WIDTH (w);
1666 /* For lines in an R2L paragraph, we need to mirror the X pixel
1667 coordinate wrt the text area. For the reasons, see the
1668 commentary in buffer_posn_from_coords and the explanation of
1669 the geometry used by the move_it_* functions at the end of
1670 the large commentary near the beginning of this file. */
1671 if (r2l)
1672 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1675 #if false
1676 /* Debugging code. */
1677 if (visible_p)
1678 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1679 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1680 else
1681 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1682 #endif
1684 /* Restore potentially overwritten values. */
1685 w->mode_line_height = prev_mode_line_height;
1686 w->header_line_height = prev_header_line_height;
1688 return visible_p;
1692 /* Return the next character from STR. Return in *LEN the length of
1693 the character. This is like STRING_CHAR_AND_LENGTH but never
1694 returns an invalid character. If we find one, we return a `?', but
1695 with the length of the invalid character. */
1697 static int
1698 string_char_and_length (const unsigned char *str, int *len)
1700 int c;
1702 c = STRING_CHAR_AND_LENGTH (str, *len);
1703 if (!CHAR_VALID_P (c))
1704 /* We may not change the length here because other places in Emacs
1705 don't use this function, i.e. they silently accept invalid
1706 characters. */
1707 c = '?';
1709 return c;
1714 /* Given a position POS containing a valid character and byte position
1715 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1717 static struct text_pos
1718 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1720 eassert (STRINGP (string) && nchars >= 0);
1722 if (STRING_MULTIBYTE (string))
1724 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1725 int len;
1727 while (nchars--)
1729 string_char_and_length (p, &len);
1730 p += len;
1731 CHARPOS (pos) += 1;
1732 BYTEPOS (pos) += len;
1735 else
1736 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1738 return pos;
1742 /* Value is the text position, i.e. character and byte position,
1743 for character position CHARPOS in STRING. */
1745 static struct text_pos
1746 string_pos (ptrdiff_t charpos, Lisp_Object string)
1748 struct text_pos pos;
1749 eassert (STRINGP (string));
1750 eassert (charpos >= 0);
1751 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1752 return pos;
1756 /* Value is a text position, i.e. character and byte position, for
1757 character position CHARPOS in C string S. MULTIBYTE_P
1758 means recognize multibyte characters. */
1760 static struct text_pos
1761 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1763 struct text_pos pos;
1765 eassert (s != NULL);
1766 eassert (charpos >= 0);
1768 if (multibyte_p)
1770 int len;
1772 SET_TEXT_POS (pos, 0, 0);
1773 while (charpos--)
1775 string_char_and_length ((const unsigned char *) s, &len);
1776 s += len;
1777 CHARPOS (pos) += 1;
1778 BYTEPOS (pos) += len;
1781 else
1782 SET_TEXT_POS (pos, charpos, charpos);
1784 return pos;
1788 /* Value is the number of characters in C string S. MULTIBYTE_P
1789 means recognize multibyte characters. */
1791 static ptrdiff_t
1792 number_of_chars (const char *s, bool multibyte_p)
1794 ptrdiff_t nchars;
1796 if (multibyte_p)
1798 ptrdiff_t rest = strlen (s);
1799 int len;
1800 const unsigned char *p = (const unsigned char *) s;
1802 for (nchars = 0; rest > 0; ++nchars)
1804 string_char_and_length (p, &len);
1805 rest -= len, p += len;
1808 else
1809 nchars = strlen (s);
1811 return nchars;
1815 /* Compute byte position NEWPOS->bytepos corresponding to
1816 NEWPOS->charpos. POS is a known position in string STRING.
1817 NEWPOS->charpos must be >= POS.charpos. */
1819 static void
1820 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1822 eassert (STRINGP (string));
1823 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1825 if (STRING_MULTIBYTE (string))
1826 *newpos = string_pos_nchars_ahead (pos, string,
1827 CHARPOS (*newpos) - CHARPOS (pos));
1828 else
1829 BYTEPOS (*newpos) = CHARPOS (*newpos);
1832 /* EXPORT:
1833 Return an estimation of the pixel height of mode or header lines on
1834 frame F. FACE_ID specifies what line's height to estimate. */
1837 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1839 #ifdef HAVE_WINDOW_SYSTEM
1840 if (FRAME_WINDOW_P (f))
1842 int height = FONT_HEIGHT (FRAME_FONT (f));
1844 /* This function is called so early when Emacs starts that the face
1845 cache and mode line face are not yet initialized. */
1846 if (FRAME_FACE_CACHE (f))
1848 struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1849 if (face)
1851 if (face->font)
1852 height = normal_char_height (face->font, -1);
1853 if (face->box_line_width > 0)
1854 height += 2 * face->box_line_width;
1858 return height;
1860 #endif
1862 return 1;
1865 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1866 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1867 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1868 not force the value into range. */
1870 void
1871 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1872 NativeRectangle *bounds, bool noclip)
1875 #ifdef HAVE_WINDOW_SYSTEM
1876 if (FRAME_WINDOW_P (f))
1878 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1879 even for negative values. */
1880 if (pix_x < 0)
1881 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1882 if (pix_y < 0)
1883 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1885 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1886 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1888 if (bounds)
1889 STORE_NATIVE_RECT (*bounds,
1890 FRAME_COL_TO_PIXEL_X (f, pix_x),
1891 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1892 FRAME_COLUMN_WIDTH (f) - 1,
1893 FRAME_LINE_HEIGHT (f) - 1);
1895 /* PXW: Should we clip pixels before converting to columns/lines? */
1896 if (!noclip)
1898 if (pix_x < 0)
1899 pix_x = 0;
1900 else if (pix_x > FRAME_TOTAL_COLS (f))
1901 pix_x = FRAME_TOTAL_COLS (f);
1903 if (pix_y < 0)
1904 pix_y = 0;
1905 else if (pix_y > FRAME_TOTAL_LINES (f))
1906 pix_y = FRAME_TOTAL_LINES (f);
1909 #endif
1911 *x = pix_x;
1912 *y = pix_y;
1916 /* Find the glyph under window-relative coordinates X/Y in window W.
1917 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1918 strings. Return in *HPOS and *VPOS the row and column number of
1919 the glyph found. Return in *AREA the glyph area containing X.
1920 Value is a pointer to the glyph found or null if X/Y is not on
1921 text, or we can't tell because W's current matrix is not up to
1922 date. */
1924 static struct glyph *
1925 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1926 int *dx, int *dy, int *area)
1928 struct glyph *glyph, *end;
1929 struct glyph_row *row = NULL;
1930 int x0, i;
1932 /* Find row containing Y. Give up if some row is not enabled. */
1933 for (i = 0; i < w->current_matrix->nrows; ++i)
1935 row = MATRIX_ROW (w->current_matrix, i);
1936 if (!row->enabled_p)
1937 return NULL;
1938 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1939 break;
1942 *vpos = i;
1943 *hpos = 0;
1945 /* Give up if Y is not in the window. */
1946 if (i == w->current_matrix->nrows)
1947 return NULL;
1949 /* Get the glyph area containing X. */
1950 if (w->pseudo_window_p)
1952 *area = TEXT_AREA;
1953 x0 = 0;
1955 else
1957 if (x < window_box_left_offset (w, TEXT_AREA))
1959 *area = LEFT_MARGIN_AREA;
1960 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1962 else if (x < window_box_right_offset (w, TEXT_AREA))
1964 *area = TEXT_AREA;
1965 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1967 else
1969 *area = RIGHT_MARGIN_AREA;
1970 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1974 /* Find glyph containing X. */
1975 glyph = row->glyphs[*area];
1976 end = glyph + row->used[*area];
1977 x -= x0;
1978 while (glyph < end && x >= glyph->pixel_width)
1980 x -= glyph->pixel_width;
1981 ++glyph;
1984 if (glyph == end)
1985 return NULL;
1987 if (dx)
1989 *dx = x;
1990 *dy = y - (row->y + row->ascent - glyph->ascent);
1993 *hpos = glyph - row->glyphs[*area];
1994 return glyph;
1997 /* Convert frame-relative x/y to coordinates relative to window W.
1998 Takes pseudo-windows into account. */
2000 static void
2001 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2003 if (w->pseudo_window_p)
2005 /* A pseudo-window is always full-width, and starts at the
2006 left edge of the frame, plus a frame border. */
2007 struct frame *f = XFRAME (w->frame);
2008 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2009 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2011 else
2013 *x -= WINDOW_LEFT_EDGE_X (w);
2014 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2018 #ifdef HAVE_WINDOW_SYSTEM
2020 /* EXPORT:
2021 Return in RECTS[] at most N clipping rectangles for glyph string S.
2022 Return the number of stored rectangles. */
2025 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2027 XRectangle r;
2029 if (n <= 0)
2030 return 0;
2032 if (s->row->full_width_p)
2034 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2035 r.x = WINDOW_LEFT_EDGE_X (s->w);
2036 if (s->row->mode_line_p)
2037 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2038 else
2039 r.width = WINDOW_PIXEL_WIDTH (s->w);
2041 /* Unless displaying a mode or menu bar line, which are always
2042 fully visible, clip to the visible part of the row. */
2043 if (s->w->pseudo_window_p)
2044 r.height = s->row->visible_height;
2045 else
2046 r.height = s->height;
2048 else
2050 /* This is a text line that may be partially visible. */
2051 r.x = window_box_left (s->w, s->area);
2052 r.width = window_box_width (s->w, s->area);
2053 r.height = s->row->visible_height;
2056 if (s->clip_head)
2057 if (r.x < s->clip_head->x)
2059 if (r.width >= s->clip_head->x - r.x)
2060 r.width -= s->clip_head->x - r.x;
2061 else
2062 r.width = 0;
2063 r.x = s->clip_head->x;
2065 if (s->clip_tail)
2066 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2068 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2069 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2070 else
2071 r.width = 0;
2074 /* If S draws overlapping rows, it's sufficient to use the top and
2075 bottom of the window for clipping because this glyph string
2076 intentionally draws over other lines. */
2077 if (s->for_overlaps)
2079 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2080 r.height = window_text_bottom_y (s->w) - r.y;
2082 /* Alas, the above simple strategy does not work for the
2083 environments with anti-aliased text: if the same text is
2084 drawn onto the same place multiple times, it gets thicker.
2085 If the overlap we are processing is for the erased cursor, we
2086 take the intersection with the rectangle of the cursor. */
2087 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2089 XRectangle rc, r_save = r;
2091 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2092 rc.y = s->w->phys_cursor.y;
2093 rc.width = s->w->phys_cursor_width;
2094 rc.height = s->w->phys_cursor_height;
2096 x_intersect_rectangles (&r_save, &rc, &r);
2099 else
2101 /* Don't use S->y for clipping because it doesn't take partially
2102 visible lines into account. For example, it can be negative for
2103 partially visible lines at the top of a window. */
2104 if (!s->row->full_width_p
2105 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2106 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2107 else
2108 r.y = max (0, s->row->y);
2111 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2113 /* If drawing the cursor, don't let glyph draw outside its
2114 advertised boundaries. Cleartype does this under some circumstances. */
2115 if (s->hl == DRAW_CURSOR)
2117 struct glyph *glyph = s->first_glyph;
2118 int height, max_y;
2120 if (s->x > r.x)
2122 if (r.width >= s->x - r.x)
2123 r.width -= s->x - r.x;
2124 else /* R2L hscrolled row with cursor outside text area */
2125 r.width = 0;
2126 r.x = s->x;
2128 r.width = min (r.width, glyph->pixel_width);
2130 /* If r.y is below window bottom, ensure that we still see a cursor. */
2131 height = min (glyph->ascent + glyph->descent,
2132 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2133 max_y = window_text_bottom_y (s->w) - height;
2134 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2135 if (s->ybase - glyph->ascent > max_y)
2137 r.y = max_y;
2138 r.height = height;
2140 else
2142 /* Don't draw cursor glyph taller than our actual glyph. */
2143 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2144 if (height < r.height)
2146 max_y = r.y + r.height;
2147 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2148 r.height = min (max_y - r.y, height);
2153 if (s->row->clip)
2155 XRectangle r_save = r;
2157 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2158 r.width = 0;
2161 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2162 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2164 #ifdef CONVERT_FROM_XRECT
2165 CONVERT_FROM_XRECT (r, *rects);
2166 #else
2167 *rects = r;
2168 #endif
2169 return 1;
2171 else
2173 /* If we are processing overlapping and allowed to return
2174 multiple clipping rectangles, we exclude the row of the glyph
2175 string from the clipping rectangle. This is to avoid drawing
2176 the same text on the environment with anti-aliasing. */
2177 #ifdef CONVERT_FROM_XRECT
2178 XRectangle rs[2];
2179 #else
2180 XRectangle *rs = rects;
2181 #endif
2182 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2184 if (s->for_overlaps & OVERLAPS_PRED)
2186 rs[i] = r;
2187 if (r.y + r.height > row_y)
2189 if (r.y < row_y)
2190 rs[i].height = row_y - r.y;
2191 else
2192 rs[i].height = 0;
2194 i++;
2196 if (s->for_overlaps & OVERLAPS_SUCC)
2198 rs[i] = r;
2199 if (r.y < row_y + s->row->visible_height)
2201 if (r.y + r.height > row_y + s->row->visible_height)
2203 rs[i].y = row_y + s->row->visible_height;
2204 rs[i].height = r.y + r.height - rs[i].y;
2206 else
2207 rs[i].height = 0;
2209 i++;
2212 n = i;
2213 #ifdef CONVERT_FROM_XRECT
2214 for (i = 0; i < n; i++)
2215 CONVERT_FROM_XRECT (rs[i], rects[i]);
2216 #endif
2217 return n;
2221 /* EXPORT:
2222 Return in *NR the clipping rectangle for glyph string S. */
2224 void
2225 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2227 get_glyph_string_clip_rects (s, nr, 1);
2231 /* EXPORT:
2232 Return the position and height of the phys cursor in window W.
2233 Set w->phys_cursor_width to width of phys cursor.
2236 void
2237 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2238 struct glyph *glyph, int *xp, int *yp, int *heightp)
2240 struct frame *f = XFRAME (WINDOW_FRAME (w));
2241 int x, y, wd, h, h0, y0, ascent;
2243 /* Compute the width of the rectangle to draw. If on a stretch
2244 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2245 rectangle as wide as the glyph, but use a canonical character
2246 width instead. */
2247 wd = glyph->pixel_width;
2249 x = w->phys_cursor.x;
2250 if (x < 0)
2252 wd += x;
2253 x = 0;
2256 if (glyph->type == STRETCH_GLYPH
2257 && !x_stretch_cursor_p)
2258 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2259 w->phys_cursor_width = wd;
2261 /* Don't let the hollow cursor glyph descend below the glyph row's
2262 ascent value, lest the hollow cursor looks funny. */
2263 y = w->phys_cursor.y;
2264 ascent = row->ascent;
2265 if (row->ascent < glyph->ascent)
2267 y -= glyph->ascent - row->ascent;
2268 ascent = glyph->ascent;
2271 /* If y is below window bottom, ensure that we still see a cursor. */
2272 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2274 h = max (h0, ascent + glyph->descent);
2275 h0 = min (h0, ascent + glyph->descent);
2277 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2278 if (y < y0)
2280 h = max (h - (y0 - y) + 1, h0);
2281 y = y0 - 1;
2283 else
2285 y0 = window_text_bottom_y (w) - h0;
2286 if (y > y0)
2288 h += y - y0;
2289 y = y0;
2293 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2294 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2295 *heightp = h;
2299 * Remember which glyph the mouse is over.
2302 void
2303 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2305 Lisp_Object window;
2306 struct window *w;
2307 struct glyph_row *r, *gr, *end_row;
2308 enum window_part part;
2309 enum glyph_row_area area;
2310 int x, y, width, height;
2312 /* Try to determine frame pixel position and size of the glyph under
2313 frame pixel coordinates X/Y on frame F. */
2315 if (window_resize_pixelwise)
2317 width = height = 1;
2318 goto virtual_glyph;
2320 else if (!f->glyphs_initialized_p
2321 || (window = window_from_coordinates (f, gx, gy, &part, false),
2322 NILP (window)))
2324 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2325 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2326 goto virtual_glyph;
2329 w = XWINDOW (window);
2330 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2331 height = WINDOW_FRAME_LINE_HEIGHT (w);
2333 x = window_relative_x_coord (w, part, gx);
2334 y = gy - WINDOW_TOP_EDGE_Y (w);
2336 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2337 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2339 if (w->pseudo_window_p)
2341 area = TEXT_AREA;
2342 part = ON_MODE_LINE; /* Don't adjust margin. */
2343 goto text_glyph;
2346 switch (part)
2348 case ON_LEFT_MARGIN:
2349 area = LEFT_MARGIN_AREA;
2350 goto text_glyph;
2352 case ON_RIGHT_MARGIN:
2353 area = RIGHT_MARGIN_AREA;
2354 goto text_glyph;
2356 case ON_HEADER_LINE:
2357 case ON_MODE_LINE:
2358 gr = (part == ON_HEADER_LINE
2359 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2360 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2361 gy = gr->y;
2362 area = TEXT_AREA;
2363 goto text_glyph_row_found;
2365 case ON_TEXT:
2366 area = TEXT_AREA;
2368 text_glyph:
2369 gr = 0; gy = 0;
2370 for (; r <= end_row && r->enabled_p; ++r)
2371 if (r->y + r->height > y)
2373 gr = r; gy = r->y;
2374 break;
2377 text_glyph_row_found:
2378 if (gr && gy <= y)
2380 struct glyph *g = gr->glyphs[area];
2381 struct glyph *end = g + gr->used[area];
2383 height = gr->height;
2384 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2385 if (gx + g->pixel_width > x)
2386 break;
2388 if (g < end)
2390 if (g->type == IMAGE_GLYPH)
2392 /* Don't remember when mouse is over image, as
2393 image may have hot-spots. */
2394 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2395 return;
2397 width = g->pixel_width;
2399 else
2401 /* Use nominal char spacing at end of line. */
2402 x -= gx;
2403 gx += (x / width) * width;
2406 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2408 gx += window_box_left_offset (w, area);
2409 /* Don't expand over the modeline to make sure the vertical
2410 drag cursor is shown early enough. */
2411 height = min (height,
2412 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2415 else
2417 /* Use nominal line height at end of window. */
2418 gx = (x / width) * width;
2419 y -= gy;
2420 gy += (y / height) * height;
2421 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2422 /* See comment above. */
2423 height = min (height,
2424 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2426 break;
2428 case ON_LEFT_FRINGE:
2429 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2430 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2431 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2432 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2433 goto row_glyph;
2435 case ON_RIGHT_FRINGE:
2436 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2437 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2438 : window_box_right_offset (w, TEXT_AREA));
2439 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2440 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2441 && !WINDOW_RIGHTMOST_P (w))
2442 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2443 /* Make sure the vertical border can get her own glyph to the
2444 right of the one we build here. */
2445 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2446 else
2447 width = WINDOW_PIXEL_WIDTH (w) - gx;
2448 else
2449 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2451 goto row_glyph;
2453 case ON_VERTICAL_BORDER:
2454 gx = WINDOW_PIXEL_WIDTH (w) - width;
2455 goto row_glyph;
2457 case ON_VERTICAL_SCROLL_BAR:
2458 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2460 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2461 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2462 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2463 : 0)));
2464 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2466 row_glyph:
2467 gr = 0, gy = 0;
2468 for (; r <= end_row && r->enabled_p; ++r)
2469 if (r->y + r->height > y)
2471 gr = r; gy = r->y;
2472 break;
2475 if (gr && gy <= y)
2476 height = gr->height;
2477 else
2479 /* Use nominal line height at end of window. */
2480 y -= gy;
2481 gy += (y / height) * height;
2483 break;
2485 case ON_RIGHT_DIVIDER:
2486 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2487 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2488 gy = 0;
2489 /* The bottom divider prevails. */
2490 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2491 goto add_edge;
2493 case ON_BOTTOM_DIVIDER:
2494 gx = 0;
2495 width = WINDOW_PIXEL_WIDTH (w);
2496 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2497 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2498 goto add_edge;
2500 default:
2502 virtual_glyph:
2503 /* If there is no glyph under the mouse, then we divide the screen
2504 into a grid of the smallest glyph in the frame, and use that
2505 as our "glyph". */
2507 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2508 round down even for negative values. */
2509 if (gx < 0)
2510 gx -= width - 1;
2511 if (gy < 0)
2512 gy -= height - 1;
2514 gx = (gx / width) * width;
2515 gy = (gy / height) * height;
2517 goto store_rect;
2520 add_edge:
2521 gx += WINDOW_LEFT_EDGE_X (w);
2522 gy += WINDOW_TOP_EDGE_Y (w);
2524 store_rect:
2525 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2527 /* Visible feedback for debugging. */
2528 #if false && defined HAVE_X_WINDOWS
2529 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
2530 f->output_data.x->normal_gc,
2531 gx, gy, width, height);
2532 #endif
2536 #endif /* HAVE_WINDOW_SYSTEM */
2538 static void
2539 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2541 eassert (w);
2542 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2543 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2544 w->window_end_vpos
2545 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2548 static bool
2549 hscrolling_current_line_p (struct window *w)
2551 return (!w->suspend_auto_hscroll
2552 && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
2553 Qcurrent_line));
2556 /***********************************************************************
2557 Lisp form evaluation
2558 ***********************************************************************/
2560 /* Error handler for safe_eval and safe_call. */
2562 static Lisp_Object
2563 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2565 add_to_log ("Error during redisplay: %S signaled %S",
2566 Flist (nargs, args), arg);
2567 return Qnil;
2570 /* Call function FUNC with the rest of NARGS - 1 arguments
2571 following. Return the result, or nil if something went
2572 wrong. Prevent redisplay during the evaluation. */
2574 static Lisp_Object
2575 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2577 Lisp_Object val;
2579 if (inhibit_eval_during_redisplay)
2580 val = Qnil;
2581 else
2583 ptrdiff_t i;
2584 ptrdiff_t count = SPECPDL_INDEX ();
2585 Lisp_Object *args;
2586 USE_SAFE_ALLOCA;
2587 SAFE_ALLOCA_LISP (args, nargs);
2589 args[0] = func;
2590 for (i = 1; i < nargs; i++)
2591 args[i] = va_arg (ap, Lisp_Object);
2593 specbind (Qinhibit_redisplay, Qt);
2594 if (inhibit_quit)
2595 specbind (Qinhibit_quit, Qt);
2596 /* Use Qt to ensure debugger does not run,
2597 so there is no possibility of wanting to redisplay. */
2598 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2599 safe_eval_handler);
2600 SAFE_FREE ();
2601 val = unbind_to (count, val);
2604 return val;
2607 Lisp_Object
2608 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2610 Lisp_Object retval;
2611 va_list ap;
2613 va_start (ap, func);
2614 retval = safe__call (false, nargs, func, ap);
2615 va_end (ap);
2616 return retval;
2619 /* Call function FN with one argument ARG.
2620 Return the result, or nil if something went wrong. */
2622 Lisp_Object
2623 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2625 return safe_call (2, fn, arg);
2628 static Lisp_Object
2629 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2631 Lisp_Object retval;
2632 va_list ap;
2634 va_start (ap, fn);
2635 retval = safe__call (inhibit_quit, 2, fn, ap);
2636 va_end (ap);
2637 return retval;
2640 Lisp_Object
2641 safe_eval (Lisp_Object sexpr)
2643 return safe__call1 (false, Qeval, sexpr);
2646 static Lisp_Object
2647 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2649 return safe__call1 (inhibit_quit, Qeval, sexpr);
2652 /* Call function FN with two arguments ARG1 and ARG2.
2653 Return the result, or nil if something went wrong. */
2655 Lisp_Object
2656 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2658 return safe_call (3, fn, arg1, arg2);
2663 /***********************************************************************
2664 Debugging
2665 ***********************************************************************/
2667 /* Define CHECK_IT to perform sanity checks on iterators.
2668 This is for debugging. It is too slow to do unconditionally. */
2670 static void
2671 CHECK_IT (struct it *it)
2673 #if false
2674 if (it->method == GET_FROM_STRING)
2676 eassert (STRINGP (it->string));
2677 eassert (IT_STRING_CHARPOS (*it) >= 0);
2679 else
2681 eassert (IT_STRING_CHARPOS (*it) < 0);
2682 if (it->method == GET_FROM_BUFFER)
2684 /* Check that character and byte positions agree. */
2685 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2689 if (it->dpvec)
2690 eassert (it->current.dpvec_index >= 0);
2691 else
2692 eassert (it->current.dpvec_index < 0);
2693 #endif
2697 /* Check that the window end of window W is what we expect it
2698 to be---the last row in the current matrix displaying text. */
2700 static void
2701 CHECK_WINDOW_END (struct window *w)
2703 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2704 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2706 struct glyph_row *row;
2707 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2708 !row->enabled_p
2709 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2710 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2712 #endif
2715 /***********************************************************************
2716 Iterator initialization
2717 ***********************************************************************/
2719 /* Initialize IT for displaying current_buffer in window W, starting
2720 at character position CHARPOS. CHARPOS < 0 means that no buffer
2721 position is specified which is useful when the iterator is assigned
2722 a position later. BYTEPOS is the byte position corresponding to
2723 CHARPOS.
2725 If ROW is not null, calls to produce_glyphs with IT as parameter
2726 will produce glyphs in that row.
2728 BASE_FACE_ID is the id of a base face to use. It must be one of
2729 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2730 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2731 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2733 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2734 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2735 will be initialized to use the corresponding mode line glyph row of
2736 the desired matrix of W. */
2738 void
2739 init_iterator (struct it *it, struct window *w,
2740 ptrdiff_t charpos, ptrdiff_t bytepos,
2741 struct glyph_row *row, enum face_id base_face_id)
2743 enum face_id remapped_base_face_id = base_face_id;
2745 /* Some precondition checks. */
2746 eassert (w != NULL && it != NULL);
2747 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2748 && charpos <= ZV));
2750 /* If face attributes have been changed since the last redisplay,
2751 free realized faces now because they depend on face definitions
2752 that might have changed. Don't free faces while there might be
2753 desired matrices pending which reference these faces. */
2754 if (!inhibit_free_realized_faces)
2756 if (face_change)
2758 face_change = false;
2759 XFRAME (w->frame)->face_change = 0;
2760 free_all_realized_faces (Qnil);
2762 else if (XFRAME (w->frame)->face_change)
2764 XFRAME (w->frame)->face_change = 0;
2765 free_all_realized_faces (w->frame);
2769 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2770 if (! NILP (Vface_remapping_alist))
2771 remapped_base_face_id
2772 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2774 /* Use one of the mode line rows of W's desired matrix if
2775 appropriate. */
2776 if (row == NULL)
2778 if (base_face_id == MODE_LINE_FACE_ID
2779 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2780 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2781 else if (base_face_id == HEADER_LINE_FACE_ID)
2782 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2785 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2786 Other parts of redisplay rely on that. */
2787 memclear (it, sizeof *it);
2788 it->current.overlay_string_index = -1;
2789 it->current.dpvec_index = -1;
2790 it->base_face_id = remapped_base_face_id;
2791 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2792 it->paragraph_embedding = L2R;
2793 it->bidi_it.w = w;
2795 /* The window in which we iterate over current_buffer: */
2796 XSETWINDOW (it->window, w);
2797 it->w = w;
2798 it->f = XFRAME (w->frame);
2800 it->cmp_it.id = -1;
2802 /* Extra space between lines (on window systems only). */
2803 if (base_face_id == DEFAULT_FACE_ID
2804 && FRAME_WINDOW_P (it->f))
2806 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2807 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2808 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2809 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2810 * FRAME_LINE_HEIGHT (it->f));
2811 else if (it->f->extra_line_spacing > 0)
2812 it->extra_line_spacing = it->f->extra_line_spacing;
2815 /* If realized faces have been removed, e.g. because of face
2816 attribute changes of named faces, recompute them. When running
2817 in batch mode, the face cache of the initial frame is null. If
2818 we happen to get called, make a dummy face cache. */
2819 if (FRAME_FACE_CACHE (it->f) == NULL)
2820 init_frame_faces (it->f);
2821 if (FRAME_FACE_CACHE (it->f)->used == 0)
2822 recompute_basic_faces (it->f);
2824 it->override_ascent = -1;
2826 /* Are control characters displayed as `^C'? */
2827 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2829 /* -1 means everything between a CR and the following line end
2830 is invisible. >0 means lines indented more than this value are
2831 invisible. */
2832 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2833 ? (clip_to_bounds
2834 (-1, XINT (BVAR (current_buffer, selective_display)),
2835 PTRDIFF_MAX))
2836 : (!NILP (BVAR (current_buffer, selective_display))
2837 ? -1 : 0));
2838 it->selective_display_ellipsis_p
2839 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2841 /* Display table to use. */
2842 it->dp = window_display_table (w);
2844 /* Are multibyte characters enabled in current_buffer? */
2845 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2847 /* Get the position at which the redisplay_end_trigger hook should
2848 be run, if it is to be run at all. */
2849 if (MARKERP (w->redisplay_end_trigger)
2850 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2851 it->redisplay_end_trigger_charpos
2852 = marker_position (w->redisplay_end_trigger);
2853 else if (INTEGERP (w->redisplay_end_trigger))
2854 it->redisplay_end_trigger_charpos
2855 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2856 PTRDIFF_MAX);
2858 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2860 /* Are lines in the display truncated? */
2861 if (TRUNCATE != 0)
2862 it->line_wrap = TRUNCATE;
2863 if (base_face_id == DEFAULT_FACE_ID
2864 && !it->w->hscroll
2865 && (WINDOW_FULL_WIDTH_P (it->w)
2866 || NILP (Vtruncate_partial_width_windows)
2867 || (INTEGERP (Vtruncate_partial_width_windows)
2868 /* PXW: Shall we do something about this? */
2869 && (XINT (Vtruncate_partial_width_windows)
2870 <= WINDOW_TOTAL_COLS (it->w))))
2871 && NILP (BVAR (current_buffer, truncate_lines)))
2872 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2873 ? WINDOW_WRAP : WORD_WRAP;
2875 /* Get dimensions of truncation and continuation glyphs. These are
2876 displayed as fringe bitmaps under X, but we need them for such
2877 frames when the fringes are turned off. The no_special_glyphs slot
2878 of the iterator's frame, when set, suppresses their display - by
2879 default for tooltip frames and when set via the 'no-special-glyphs'
2880 frame parameter. */
2881 #ifdef HAVE_WINDOW_SYSTEM
2882 if (!(FRAME_WINDOW_P (it->f) && it->f->no_special_glyphs))
2883 #endif
2885 if (it->line_wrap == TRUNCATE)
2887 /* We will need the truncation glyph. */
2888 eassert (it->glyph_row == NULL);
2889 produce_special_glyphs (it, IT_TRUNCATION);
2890 it->truncation_pixel_width = it->pixel_width;
2892 else
2894 /* We will need the continuation glyph. */
2895 eassert (it->glyph_row == NULL);
2896 produce_special_glyphs (it, IT_CONTINUATION);
2897 it->continuation_pixel_width = it->pixel_width;
2901 /* Reset these values to zero because the produce_special_glyphs
2902 above has changed them. */
2903 it->pixel_width = it->ascent = it->descent = 0;
2904 it->phys_ascent = it->phys_descent = 0;
2906 /* Set this after getting the dimensions of truncation and
2907 continuation glyphs, so that we don't produce glyphs when calling
2908 produce_special_glyphs, above. */
2909 it->glyph_row = row;
2910 it->area = TEXT_AREA;
2912 /* Get the dimensions of the display area. The display area
2913 consists of the visible window area plus a horizontally scrolled
2914 part to the left of the window. All x-values are relative to the
2915 start of this total display area. */
2916 if (base_face_id != DEFAULT_FACE_ID)
2918 /* Mode lines, menu bar in terminal frames. */
2919 it->first_visible_x = 0;
2920 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2922 else
2924 /* When hscrolling only the current line, don't apply the
2925 hscroll here, it will be applied by display_line when it gets
2926 to laying out the line showing point. However, if the
2927 window's min_hscroll is positive, the user specified a lower
2928 bound for automatic hscrolling, so they expect the
2929 non-current lines to obey that hscroll amount. */
2930 if (hscrolling_current_line_p (w))
2932 if (w->min_hscroll > 0)
2933 it->first_visible_x = w->min_hscroll * FRAME_COLUMN_WIDTH (it->f);
2934 else
2935 it->first_visible_x = 0;
2937 else
2938 it->first_visible_x =
2939 window_hscroll_limited (w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2940 it->last_visible_x = (it->first_visible_x
2941 + window_box_width (w, TEXT_AREA));
2943 /* If we truncate lines, leave room for the truncation glyph(s) at
2944 the right margin. Otherwise, leave room for the continuation
2945 glyph(s). Done only if the window has no right fringe. */
2946 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2948 if (it->line_wrap == TRUNCATE)
2949 it->last_visible_x -= it->truncation_pixel_width;
2950 else
2951 it->last_visible_x -= it->continuation_pixel_width;
2954 it->header_line_p = window_wants_header_line (w);
2955 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2958 /* Leave room for a border glyph. */
2959 if (!FRAME_WINDOW_P (it->f)
2960 && !WINDOW_RIGHTMOST_P (it->w))
2961 it->last_visible_x -= 1;
2963 it->last_visible_y = window_text_bottom_y (w);
2965 /* For mode lines and alike, arrange for the first glyph having a
2966 left box line if the face specifies a box. */
2967 if (base_face_id != DEFAULT_FACE_ID)
2969 struct face *face;
2971 it->face_id = remapped_base_face_id;
2973 /* If we have a boxed mode line, make the first character appear
2974 with a left box line. */
2975 face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
2976 if (face && face->box != FACE_NO_BOX)
2977 it->start_of_box_run_p = true;
2980 /* If a buffer position was specified, set the iterator there,
2981 getting overlays and face properties from that position. */
2982 if (charpos >= BUF_BEG (current_buffer))
2984 it->stop_charpos = charpos;
2985 it->end_charpos = ZV;
2986 eassert (charpos == BYTE_TO_CHAR (bytepos));
2987 IT_CHARPOS (*it) = charpos;
2988 IT_BYTEPOS (*it) = bytepos;
2990 /* We will rely on `reseat' to set this up properly, via
2991 handle_face_prop. */
2992 it->face_id = it->base_face_id;
2994 it->start = it->current;
2995 /* Do we need to reorder bidirectional text? Not if this is a
2996 unibyte buffer: by definition, none of the single-byte
2997 characters are strong R2L, so no reordering is needed. And
2998 bidi.c doesn't support unibyte buffers anyway. Also, don't
2999 reorder while we are loading loadup.el, since the tables of
3000 character properties needed for reordering are not yet
3001 available. */
3002 it->bidi_p =
3003 !redisplay__inhibit_bidi
3004 && !NILP (BVAR (current_buffer, bidi_display_reordering))
3005 && it->multibyte_p;
3007 /* If we are to reorder bidirectional text, init the bidi
3008 iterator. */
3009 if (it->bidi_p)
3011 /* Since we don't know at this point whether there will be
3012 any R2L lines in the window, we reserve space for
3013 truncation/continuation glyphs even if only the left
3014 fringe is absent. */
3015 if (base_face_id == DEFAULT_FACE_ID
3016 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
3017 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
3019 if (it->line_wrap == TRUNCATE)
3020 it->last_visible_x -= it->truncation_pixel_width;
3021 else
3022 it->last_visible_x -= it->continuation_pixel_width;
3024 /* Note the paragraph direction that this buffer wants to
3025 use. */
3026 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3027 Qleft_to_right))
3028 it->paragraph_embedding = L2R;
3029 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3030 Qright_to_left))
3031 it->paragraph_embedding = R2L;
3032 else
3033 it->paragraph_embedding = NEUTRAL_DIR;
3034 bidi_unshelve_cache (NULL, false);
3035 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
3036 &it->bidi_it);
3039 /* Compute faces etc. */
3040 reseat (it, it->current.pos, true);
3043 CHECK_IT (it);
3047 /* Initialize IT for the display of window W with window start POS. */
3049 void
3050 start_display (struct it *it, struct window *w, struct text_pos pos)
3052 struct glyph_row *row;
3053 bool first_vpos = window_wants_header_line (w);
3055 row = w->desired_matrix->rows + first_vpos;
3056 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3057 it->first_vpos = first_vpos;
3059 /* Don't reseat to previous visible line start if current start
3060 position is in a string or image. */
3061 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3063 int first_y = it->current_y;
3065 /* If window start is not at a line start, skip forward to POS to
3066 get the correct continuation lines width. */
3067 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3068 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3069 if (!start_at_line_beg_p)
3071 int new_x;
3073 reseat_at_previous_visible_line_start (it);
3074 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3076 new_x = it->current_x + it->pixel_width;
3078 /* If lines are continued, this line may end in the middle
3079 of a multi-glyph character (e.g. a control character
3080 displayed as \003, or in the middle of an overlay
3081 string). In this case move_it_to above will not have
3082 taken us to the start of the continuation line but to the
3083 end of the continued line. */
3084 if (it->current_x > 0
3085 && it->line_wrap != TRUNCATE /* Lines are continued. */
3086 && (/* And glyph doesn't fit on the line. */
3087 new_x > it->last_visible_x
3088 /* Or it fits exactly and we're on a window
3089 system frame. */
3090 || (new_x == it->last_visible_x
3091 && FRAME_WINDOW_P (it->f)
3092 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3093 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3094 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3096 if ((it->current.dpvec_index >= 0
3097 || it->current.overlay_string_index >= 0)
3098 /* If we are on a newline from a display vector or
3099 overlay string, then we are already at the end of
3100 a screen line; no need to go to the next line in
3101 that case, as this line is not really continued.
3102 (If we do go to the next line, C-e will not DTRT.) */
3103 && it->c != '\n')
3105 set_iterator_to_next (it, true);
3106 move_it_in_display_line_to (it, -1, -1, 0);
3109 it->continuation_lines_width += it->current_x;
3111 /* If the character at POS is displayed via a display
3112 vector, move_it_to above stops at the final glyph of
3113 IT->dpvec. To make the caller redisplay that character
3114 again (a.k.a. start at POS), we need to reset the
3115 dpvec_index to the beginning of IT->dpvec. */
3116 else if (it->current.dpvec_index >= 0)
3117 it->current.dpvec_index = 0;
3119 /* We're starting a new display line, not affected by the
3120 height of the continued line, so clear the appropriate
3121 fields in the iterator structure. */
3122 it->max_ascent = it->max_descent = 0;
3123 it->max_phys_ascent = it->max_phys_descent = 0;
3125 it->current_y = first_y;
3126 it->vpos = 0;
3127 it->current_x = it->hpos = 0;
3133 /* Return true if POS is a position in ellipses displayed for invisible
3134 text. W is the window we display, for text property lookup. */
3136 static bool
3137 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3139 Lisp_Object prop, window;
3140 bool ellipses_p = false;
3141 ptrdiff_t charpos = CHARPOS (pos->pos);
3143 /* If POS specifies a position in a display vector, this might
3144 be for an ellipsis displayed for invisible text. We won't
3145 get the iterator set up for delivering that ellipsis unless
3146 we make sure that it gets aware of the invisible text. */
3147 if (pos->dpvec_index >= 0
3148 && pos->overlay_string_index < 0
3149 && CHARPOS (pos->string_pos) < 0
3150 && charpos > BEGV
3151 && (XSETWINDOW (window, w),
3152 prop = Fget_char_property (make_number (charpos),
3153 Qinvisible, window),
3154 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3156 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3157 window);
3158 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3161 return ellipses_p;
3165 /* Initialize IT for stepping through current_buffer in window W,
3166 starting at position POS that includes overlay string and display
3167 vector/ control character translation position information. Value
3168 is false if there are overlay strings with newlines at POS. */
3170 static bool
3171 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3173 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3174 int i;
3175 bool overlay_strings_with_newlines = false;
3177 /* If POS specifies a position in a display vector, this might
3178 be for an ellipsis displayed for invisible text. We won't
3179 get the iterator set up for delivering that ellipsis unless
3180 we make sure that it gets aware of the invisible text. */
3181 if (in_ellipses_for_invisible_text_p (pos, w))
3183 --charpos;
3184 bytepos = 0;
3187 /* Keep in mind: the call to reseat in init_iterator skips invisible
3188 text, so we might end up at a position different from POS. This
3189 is only a problem when POS is a row start after a newline and an
3190 overlay starts there with an after-string, and the overlay has an
3191 invisible property. Since we don't skip invisible text in
3192 display_line and elsewhere immediately after consuming the
3193 newline before the row start, such a POS will not be in a string,
3194 but the call to init_iterator below will move us to the
3195 after-string. */
3196 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3198 /* This only scans the current chunk -- it should scan all chunks.
3199 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3200 to 16 in 22.1 to make this a lesser problem. */
3201 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3203 const char *s = SSDATA (it->overlay_strings[i]);
3204 const char *e = s + SBYTES (it->overlay_strings[i]);
3206 while (s < e && *s != '\n')
3207 ++s;
3209 if (s < e)
3211 overlay_strings_with_newlines = true;
3212 break;
3216 /* If position is within an overlay string, set up IT to the right
3217 overlay string. */
3218 if (pos->overlay_string_index >= 0)
3220 int relative_index;
3222 /* If the first overlay string happens to have a `display'
3223 property for an image, the iterator will be set up for that
3224 image, and we have to undo that setup first before we can
3225 correct the overlay string index. */
3226 if (it->method == GET_FROM_IMAGE)
3227 pop_it (it);
3229 /* We already have the first chunk of overlay strings in
3230 IT->overlay_strings. Load more until the one for
3231 pos->overlay_string_index is in IT->overlay_strings. */
3232 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3234 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3235 it->current.overlay_string_index = 0;
3236 while (n--)
3238 load_overlay_strings (it, 0);
3239 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3243 it->current.overlay_string_index = pos->overlay_string_index;
3244 relative_index = (it->current.overlay_string_index
3245 % OVERLAY_STRING_CHUNK_SIZE);
3246 it->string = it->overlay_strings[relative_index];
3247 eassert (STRINGP (it->string));
3248 it->current.string_pos = pos->string_pos;
3249 it->method = GET_FROM_STRING;
3250 it->end_charpos = SCHARS (it->string);
3251 /* Set up the bidi iterator for this overlay string. */
3252 if (it->bidi_p)
3254 it->bidi_it.string.lstring = it->string;
3255 it->bidi_it.string.s = NULL;
3256 it->bidi_it.string.schars = SCHARS (it->string);
3257 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3258 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3259 it->bidi_it.string.unibyte = !it->multibyte_p;
3260 it->bidi_it.w = it->w;
3261 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3262 FRAME_WINDOW_P (it->f), &it->bidi_it);
3264 /* Synchronize the state of the bidi iterator with
3265 pos->string_pos. For any string position other than
3266 zero, this will be done automagically when we resume
3267 iteration over the string and get_visually_first_element
3268 is called. But if string_pos is zero, and the string is
3269 to be reordered for display, we need to resync manually,
3270 since it could be that the iteration state recorded in
3271 pos ended at string_pos of 0 moving backwards in string. */
3272 if (CHARPOS (pos->string_pos) == 0)
3274 get_visually_first_element (it);
3275 if (IT_STRING_CHARPOS (*it) != 0)
3276 do {
3277 /* Paranoia. */
3278 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3279 bidi_move_to_visually_next (&it->bidi_it);
3280 } while (it->bidi_it.charpos != 0);
3282 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3283 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3287 if (CHARPOS (pos->string_pos) >= 0)
3289 /* Recorded position is not in an overlay string, but in another
3290 string. This can only be a string from a `display' property.
3291 IT should already be filled with that string. */
3292 it->current.string_pos = pos->string_pos;
3293 eassert (STRINGP (it->string));
3294 if (it->bidi_p)
3295 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3296 FRAME_WINDOW_P (it->f), &it->bidi_it);
3299 /* Restore position in display vector translations, control
3300 character translations or ellipses. */
3301 if (pos->dpvec_index >= 0)
3303 if (it->dpvec == NULL)
3304 get_next_display_element (it);
3305 eassert (it->dpvec && it->current.dpvec_index == 0);
3306 it->current.dpvec_index = pos->dpvec_index;
3309 CHECK_IT (it);
3310 return !overlay_strings_with_newlines;
3314 /* Initialize IT for stepping through current_buffer in window W
3315 starting at ROW->start. */
3317 static void
3318 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3320 init_from_display_pos (it, w, &row->start);
3321 it->start = row->start;
3322 it->continuation_lines_width = row->continuation_lines_width;
3323 CHECK_IT (it);
3327 /* Initialize IT for stepping through current_buffer in window W
3328 starting in the line following ROW, i.e. starting at ROW->end.
3329 Value is false if there are overlay strings with newlines at ROW's
3330 end position. */
3332 static bool
3333 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3335 bool success = false;
3337 if (init_from_display_pos (it, w, &row->end))
3339 if (row->continued_p)
3340 it->continuation_lines_width
3341 = row->continuation_lines_width + row->pixel_width;
3342 CHECK_IT (it);
3343 success = true;
3346 return success;
3352 /***********************************************************************
3353 Text properties
3354 ***********************************************************************/
3356 /* Called when IT reaches IT->stop_charpos. Handle text property and
3357 overlay changes. Set IT->stop_charpos to the next position where
3358 to stop. */
3360 static void
3361 handle_stop (struct it *it)
3363 enum prop_handled handled;
3364 bool handle_overlay_change_p;
3365 struct props *p;
3367 it->dpvec = NULL;
3368 it->current.dpvec_index = -1;
3369 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3370 it->ellipsis_p = false;
3372 /* Use face of preceding text for ellipsis (if invisible) */
3373 if (it->selective_display_ellipsis_p)
3374 it->saved_face_id = it->face_id;
3376 /* Here's the description of the semantics of, and the logic behind,
3377 the various HANDLED_* statuses:
3379 HANDLED_NORMALLY means the handler did its job, and the loop
3380 should proceed to calling the next handler in order.
3382 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3383 change in the properties and overlays at current position, so the
3384 loop should be restarted, to re-invoke the handlers that were
3385 already called. This happens when fontification-functions were
3386 called by handle_fontified_prop, and actually fontified
3387 something. Another case where HANDLED_RECOMPUTE_PROPS is
3388 returned is when we discover overlay strings that need to be
3389 displayed right away. The loop below will continue for as long
3390 as the status is HANDLED_RECOMPUTE_PROPS.
3392 HANDLED_RETURN means return immediately to the caller, to
3393 continue iteration without calling any further handlers. This is
3394 used when we need to act on some property right away, for example
3395 when we need to display the ellipsis or a replacing display
3396 property, such as display string or image.
3398 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3399 consumed, and the handler switched to the next overlay string.
3400 This signals the loop below to refrain from looking for more
3401 overlays before all the overlay strings of the current overlay
3402 are processed.
3404 Some of the handlers called by the loop push the iterator state
3405 onto the stack (see 'push_it'), and arrange for the iteration to
3406 continue with another object, such as an image, a display string,
3407 or an overlay string. In most such cases, it->stop_charpos is
3408 set to the first character of the string, so that when the
3409 iteration resumes, this function will immediately be called
3410 again, to examine the properties at the beginning of the string.
3412 When a display or overlay string is exhausted, the iterator state
3413 is popped (see 'pop_it'), and iteration continues with the
3414 previous object. Again, in many such cases this function is
3415 called again to find the next position where properties might
3416 change. */
3420 handled = HANDLED_NORMALLY;
3422 /* Call text property handlers. */
3423 for (p = it_props; p->handler; ++p)
3425 handled = p->handler (it);
3427 if (handled == HANDLED_RECOMPUTE_PROPS)
3428 break;
3429 else if (handled == HANDLED_RETURN)
3431 /* We still want to show before and after strings from
3432 overlays even if the actual buffer text is replaced. */
3433 if (!handle_overlay_change_p
3434 || it->sp > 1
3435 /* Don't call get_overlay_strings_1 if we already
3436 have overlay strings loaded, because doing so
3437 will load them again and push the iterator state
3438 onto the stack one more time, which is not
3439 expected by the rest of the code that processes
3440 overlay strings. */
3441 || (it->current.overlay_string_index < 0
3442 && !get_overlay_strings_1 (it, 0, false)))
3444 if (it->ellipsis_p)
3445 setup_for_ellipsis (it, 0);
3446 /* When handling a display spec, we might load an
3447 empty string. In that case, discard it here. We
3448 used to discard it in handle_single_display_spec,
3449 but that causes get_overlay_strings_1, above, to
3450 ignore overlay strings that we must check. */
3451 if (STRINGP (it->string) && !SCHARS (it->string))
3452 pop_it (it);
3453 return;
3455 else if (STRINGP (it->string) && !SCHARS (it->string))
3456 pop_it (it);
3457 else
3459 it->string_from_display_prop_p = false;
3460 it->from_disp_prop_p = false;
3461 handle_overlay_change_p = false;
3463 handled = HANDLED_RECOMPUTE_PROPS;
3464 break;
3466 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3467 handle_overlay_change_p = false;
3470 if (handled != HANDLED_RECOMPUTE_PROPS)
3472 /* Don't check for overlay strings below when set to deliver
3473 characters from a display vector. */
3474 if (it->method == GET_FROM_DISPLAY_VECTOR)
3475 handle_overlay_change_p = false;
3477 /* Handle overlay changes.
3478 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3479 if it finds overlays. */
3480 if (handle_overlay_change_p)
3481 handled = handle_overlay_change (it);
3484 if (it->ellipsis_p)
3486 setup_for_ellipsis (it, 0);
3487 break;
3490 while (handled == HANDLED_RECOMPUTE_PROPS);
3492 /* Determine where to stop next. */
3493 if (handled == HANDLED_NORMALLY)
3494 compute_stop_pos (it);
3498 /* Compute IT->stop_charpos from text property and overlay change
3499 information for IT's current position. */
3501 static void
3502 compute_stop_pos (struct it *it)
3504 register INTERVAL iv, next_iv;
3505 Lisp_Object object, limit, position;
3506 ptrdiff_t charpos, bytepos;
3508 if (STRINGP (it->string))
3510 /* Strings are usually short, so don't limit the search for
3511 properties. */
3512 it->stop_charpos = it->end_charpos;
3513 object = it->string;
3514 limit = Qnil;
3515 charpos = IT_STRING_CHARPOS (*it);
3516 bytepos = IT_STRING_BYTEPOS (*it);
3518 else
3520 ptrdiff_t pos;
3522 /* If end_charpos is out of range for some reason, such as a
3523 misbehaving display function, rationalize it (Bug#5984). */
3524 if (it->end_charpos > ZV)
3525 it->end_charpos = ZV;
3526 it->stop_charpos = it->end_charpos;
3528 /* If next overlay change is in front of the current stop pos
3529 (which is IT->end_charpos), stop there. Note: value of
3530 next_overlay_change is point-max if no overlay change
3531 follows. */
3532 charpos = IT_CHARPOS (*it);
3533 bytepos = IT_BYTEPOS (*it);
3534 pos = next_overlay_change (charpos);
3535 if (pos < it->stop_charpos)
3536 it->stop_charpos = pos;
3538 /* Set up variables for computing the stop position from text
3539 property changes. */
3540 XSETBUFFER (object, current_buffer);
3541 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3544 /* Get the interval containing IT's position. Value is a null
3545 interval if there isn't such an interval. */
3546 position = make_number (charpos);
3547 iv = validate_interval_range (object, &position, &position, false);
3548 if (iv)
3550 Lisp_Object values_here[LAST_PROP_IDX];
3551 struct props *p;
3553 /* Get properties here. */
3554 for (p = it_props; p->handler; ++p)
3555 values_here[p->idx] = textget (iv->plist,
3556 builtin_lisp_symbol (p->name));
3558 /* Look for an interval following iv that has different
3559 properties. */
3560 for (next_iv = next_interval (iv);
3561 (next_iv
3562 && (NILP (limit)
3563 || XFASTINT (limit) > next_iv->position));
3564 next_iv = next_interval (next_iv))
3566 for (p = it_props; p->handler; ++p)
3568 Lisp_Object new_value = textget (next_iv->plist,
3569 builtin_lisp_symbol (p->name));
3570 if (!EQ (values_here[p->idx], new_value))
3571 break;
3574 if (p->handler)
3575 break;
3578 if (next_iv)
3580 if (INTEGERP (limit)
3581 && next_iv->position >= XFASTINT (limit))
3582 /* No text property change up to limit. */
3583 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3584 else
3585 /* Text properties change in next_iv. */
3586 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3590 if (it->cmp_it.id < 0)
3592 ptrdiff_t stoppos = it->end_charpos;
3594 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3595 stoppos = -1;
3596 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3597 stoppos, it->string);
3600 eassert (STRINGP (it->string)
3601 || (it->stop_charpos >= BEGV
3602 && it->stop_charpos >= IT_CHARPOS (*it)));
3606 /* Return the position of the next overlay change after POS in
3607 current_buffer. Value is point-max if no overlay change
3608 follows. This is like `next-overlay-change' but doesn't use
3609 xmalloc. */
3611 static ptrdiff_t
3612 next_overlay_change (ptrdiff_t pos)
3614 ptrdiff_t i, noverlays;
3615 ptrdiff_t endpos;
3616 Lisp_Object *overlays;
3617 USE_SAFE_ALLOCA;
3619 /* Get all overlays at the given position. */
3620 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3622 /* If any of these overlays ends before endpos,
3623 use its ending point instead. */
3624 for (i = 0; i < noverlays; ++i)
3626 Lisp_Object oend;
3627 ptrdiff_t oendpos;
3629 oend = OVERLAY_END (overlays[i]);
3630 oendpos = OVERLAY_POSITION (oend);
3631 endpos = min (endpos, oendpos);
3634 SAFE_FREE ();
3635 return endpos;
3638 /* How many characters forward to search for a display property or
3639 display string. Searching too far forward makes the bidi display
3640 sluggish, especially in small windows. */
3641 #define MAX_DISP_SCAN 250
3643 /* Return the character position of a display string at or after
3644 position specified by POSITION. If no display string exists at or
3645 after POSITION, return ZV. A display string is either an overlay
3646 with `display' property whose value is a string, or a `display'
3647 text property whose value is a string. STRING is data about the
3648 string to iterate; if STRING->lstring is nil, we are iterating a
3649 buffer. FRAME_WINDOW_P is true when we are displaying a window
3650 on a GUI frame. DISP_PROP is set to zero if we searched
3651 MAX_DISP_SCAN characters forward without finding any display
3652 strings, non-zero otherwise. It is set to 2 if the display string
3653 uses any kind of `(space ...)' spec that will produce a stretch of
3654 white space in the text area. */
3655 ptrdiff_t
3656 compute_display_string_pos (struct text_pos *position,
3657 struct bidi_string_data *string,
3658 struct window *w,
3659 bool frame_window_p, int *disp_prop)
3661 /* OBJECT = nil means current buffer. */
3662 Lisp_Object object, object1;
3663 Lisp_Object pos, spec, limpos;
3664 bool string_p = string && (STRINGP (string->lstring) || string->s);
3665 ptrdiff_t eob = string_p ? string->schars : ZV;
3666 ptrdiff_t begb = string_p ? 0 : BEGV;
3667 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3668 ptrdiff_t lim =
3669 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3670 struct text_pos tpos;
3671 int rv = 0;
3673 if (string && STRINGP (string->lstring))
3674 object1 = object = string->lstring;
3675 else if (w && !string_p)
3677 XSETWINDOW (object, w);
3678 object1 = Qnil;
3680 else
3681 object1 = object = Qnil;
3683 *disp_prop = 1;
3685 if (charpos >= eob
3686 /* We don't support display properties whose values are strings
3687 that have display string properties. */
3688 || string->from_disp_str
3689 /* C strings cannot have display properties. */
3690 || (string->s && !STRINGP (object)))
3692 *disp_prop = 0;
3693 return eob;
3696 /* If the character at CHARPOS is where the display string begins,
3697 return CHARPOS. */
3698 pos = make_number (charpos);
3699 if (STRINGP (object))
3700 bufpos = string->bufpos;
3701 else
3702 bufpos = charpos;
3703 tpos = *position;
3704 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3705 && (charpos <= begb
3706 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3707 object),
3708 spec))
3709 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3710 frame_window_p)))
3712 if (rv == 2)
3713 *disp_prop = 2;
3714 return charpos;
3717 /* Look forward for the first character with a `display' property
3718 that will replace the underlying text when displayed. */
3719 limpos = make_number (lim);
3720 do {
3721 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3722 CHARPOS (tpos) = XFASTINT (pos);
3723 if (CHARPOS (tpos) >= lim)
3725 *disp_prop = 0;
3726 break;
3728 if (STRINGP (object))
3729 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3730 else
3731 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3732 spec = Fget_char_property (pos, Qdisplay, object);
3733 if (!STRINGP (object))
3734 bufpos = CHARPOS (tpos);
3735 } while (NILP (spec)
3736 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3737 bufpos, frame_window_p)));
3738 if (rv == 2)
3739 *disp_prop = 2;
3741 return CHARPOS (tpos);
3744 /* Return the character position of the end of the display string that
3745 started at CHARPOS. If there's no display string at CHARPOS,
3746 return -1. A display string is either an overlay with `display'
3747 property whose value is a string or a `display' text property whose
3748 value is a string. */
3749 ptrdiff_t
3750 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3752 /* OBJECT = nil means current buffer. */
3753 Lisp_Object object =
3754 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3755 Lisp_Object pos = make_number (charpos);
3756 ptrdiff_t eob =
3757 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3759 if (charpos >= eob || (string->s && !STRINGP (object)))
3760 return eob;
3762 /* It could happen that the display property or overlay was removed
3763 since we found it in compute_display_string_pos above. One way
3764 this can happen is if JIT font-lock was called (through
3765 handle_fontified_prop), and jit-lock-functions remove text
3766 properties or overlays from the portion of buffer that includes
3767 CHARPOS. Muse mode is known to do that, for example. In this
3768 case, we return -1 to the caller, to signal that no display
3769 string is actually present at CHARPOS. See bidi_fetch_char for
3770 how this is handled.
3772 An alternative would be to never look for display properties past
3773 it->stop_charpos. But neither compute_display_string_pos nor
3774 bidi_fetch_char that calls it know or care where the next
3775 stop_charpos is. */
3776 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3777 return -1;
3779 /* Look forward for the first character where the `display' property
3780 changes. */
3781 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3783 return XFASTINT (pos);
3788 /***********************************************************************
3789 Fontification
3790 ***********************************************************************/
3792 /* Handle changes in the `fontified' property of the current buffer by
3793 calling hook functions from Qfontification_functions to fontify
3794 regions of text. */
3796 static enum prop_handled
3797 handle_fontified_prop (struct it *it)
3799 Lisp_Object prop, pos;
3800 enum prop_handled handled = HANDLED_NORMALLY;
3802 if (!NILP (Vmemory_full))
3803 return handled;
3805 /* Get the value of the `fontified' property at IT's current buffer
3806 position. (The `fontified' property doesn't have a special
3807 meaning in strings.) If the value is nil, call functions from
3808 Qfontification_functions. */
3809 if (!STRINGP (it->string)
3810 && it->s == NULL
3811 && !NILP (Vfontification_functions)
3812 && !NILP (Vrun_hooks)
3813 && (pos = make_number (IT_CHARPOS (*it)),
3814 prop = Fget_char_property (pos, Qfontified, Qnil),
3815 /* Ignore the special cased nil value always present at EOB since
3816 no amount of fontifying will be able to change it. */
3817 NILP (prop) && IT_CHARPOS (*it) < Z))
3819 ptrdiff_t count = SPECPDL_INDEX ();
3820 Lisp_Object val;
3821 struct buffer *obuf = current_buffer;
3822 ptrdiff_t begv = BEGV, zv = ZV;
3823 bool old_clip_changed = current_buffer->clip_changed;
3825 val = Vfontification_functions;
3826 specbind (Qfontification_functions, Qnil);
3828 eassert (it->end_charpos == ZV);
3830 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3831 safe_call1 (val, pos);
3832 else
3834 Lisp_Object fns, fn;
3836 fns = Qnil;
3838 for (; CONSP (val); val = XCDR (val))
3840 fn = XCAR (val);
3842 if (EQ (fn, Qt))
3844 /* A value of t indicates this hook has a local
3845 binding; it means to run the global binding too.
3846 In a global value, t should not occur. If it
3847 does, we must ignore it to avoid an endless
3848 loop. */
3849 for (fns = Fdefault_value (Qfontification_functions);
3850 CONSP (fns);
3851 fns = XCDR (fns))
3853 fn = XCAR (fns);
3854 if (!EQ (fn, Qt))
3855 safe_call1 (fn, pos);
3858 else
3859 safe_call1 (fn, pos);
3863 unbind_to (count, Qnil);
3865 /* Fontification functions routinely call `save-restriction'.
3866 Normally, this tags clip_changed, which can confuse redisplay
3867 (see discussion in Bug#6671). Since we don't perform any
3868 special handling of fontification changes in the case where
3869 `save-restriction' isn't called, there's no point doing so in
3870 this case either. So, if the buffer's restrictions are
3871 actually left unchanged, reset clip_changed. */
3872 if (obuf == current_buffer)
3874 if (begv == BEGV && zv == ZV)
3875 current_buffer->clip_changed = old_clip_changed;
3877 /* There isn't much we can reasonably do to protect against
3878 misbehaving fontification, but here's a fig leaf. */
3879 else if (BUFFER_LIVE_P (obuf))
3880 set_buffer_internal_1 (obuf);
3882 /* The fontification code may have added/removed text.
3883 It could do even a lot worse, but let's at least protect against
3884 the most obvious case where only the text past `pos' gets changed',
3885 as is/was done in grep.el where some escapes sequences are turned
3886 into face properties (bug#7876). */
3887 it->end_charpos = ZV;
3889 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3890 something. This avoids an endless loop if they failed to
3891 fontify the text for which reason ever. */
3892 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3893 handled = HANDLED_RECOMPUTE_PROPS;
3896 return handled;
3901 /***********************************************************************
3902 Faces
3903 ***********************************************************************/
3905 /* Set up iterator IT from face properties at its current position.
3906 Called from handle_stop. */
3908 static enum prop_handled
3909 handle_face_prop (struct it *it)
3911 int new_face_id;
3912 ptrdiff_t next_stop;
3914 if (!STRINGP (it->string))
3916 new_face_id
3917 = face_at_buffer_position (it->w,
3918 IT_CHARPOS (*it),
3919 &next_stop,
3920 (IT_CHARPOS (*it)
3921 + TEXT_PROP_DISTANCE_LIMIT),
3922 false, it->base_face_id);
3924 /* Is this a start of a run of characters with box face?
3925 Caveat: this can be called for a freshly initialized
3926 iterator; face_id is -1 in this case. We know that the new
3927 face will not change until limit, i.e. if the new face has a
3928 box, all characters up to limit will have one. But, as
3929 usual, we don't know whether limit is really the end. */
3930 if (new_face_id != it->face_id)
3932 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3933 /* If it->face_id is -1, old_face below will be NULL, see
3934 the definition of FACE_FROM_ID_OR_NULL. This will happen
3935 if this is the initial call that gets the face. */
3936 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3938 /* If the value of face_id of the iterator is -1, we have to
3939 look in front of IT's position and see whether there is a
3940 face there that's different from new_face_id. */
3941 if (!old_face && IT_CHARPOS (*it) > BEG)
3943 int prev_face_id = face_before_it_pos (it);
3945 old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
3948 /* If the new face has a box, but the old face does not,
3949 this is the start of a run of characters with box face,
3950 i.e. this character has a shadow on the left side. */
3951 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3952 && (old_face == NULL || !old_face->box));
3953 it->face_box_p = new_face->box != FACE_NO_BOX;
3956 else
3958 int base_face_id;
3959 ptrdiff_t bufpos;
3960 int i;
3961 Lisp_Object from_overlay
3962 = (it->current.overlay_string_index >= 0
3963 ? it->string_overlays[it->current.overlay_string_index
3964 % OVERLAY_STRING_CHUNK_SIZE]
3965 : Qnil);
3967 /* See if we got to this string directly or indirectly from
3968 an overlay property. That includes the before-string or
3969 after-string of an overlay, strings in display properties
3970 provided by an overlay, their text properties, etc.
3972 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3973 if (! NILP (from_overlay))
3974 for (i = it->sp - 1; i >= 0; i--)
3976 if (it->stack[i].current.overlay_string_index >= 0)
3977 from_overlay
3978 = it->string_overlays[it->stack[i].current.overlay_string_index
3979 % OVERLAY_STRING_CHUNK_SIZE];
3980 else if (! NILP (it->stack[i].from_overlay))
3981 from_overlay = it->stack[i].from_overlay;
3983 if (!NILP (from_overlay))
3984 break;
3987 if (! NILP (from_overlay))
3989 bufpos = IT_CHARPOS (*it);
3990 /* For a string from an overlay, the base face depends
3991 only on text properties and ignores overlays. */
3992 base_face_id
3993 = face_for_overlay_string (it->w,
3994 IT_CHARPOS (*it),
3995 &next_stop,
3996 (IT_CHARPOS (*it)
3997 + TEXT_PROP_DISTANCE_LIMIT),
3998 false,
3999 from_overlay);
4001 else
4003 bufpos = 0;
4005 /* For strings from a `display' property, use the face at
4006 IT's current buffer position as the base face to merge
4007 with, so that overlay strings appear in the same face as
4008 surrounding text, unless they specify their own faces.
4009 For strings from wrap-prefix and line-prefix properties,
4010 use the default face, possibly remapped via
4011 Vface_remapping_alist. */
4012 /* Note that the fact that we use the face at _buffer_
4013 position means that a 'display' property on an overlay
4014 string will not inherit the face of that overlay string,
4015 but will instead revert to the face of buffer text
4016 covered by the overlay. This is visible, e.g., when the
4017 overlay specifies a box face, but neither the buffer nor
4018 the display string do. This sounds like a design bug,
4019 but Emacs always did that since v21.1, so changing that
4020 might be a big deal. */
4021 base_face_id = it->string_from_prefix_prop_p
4022 ? (!NILP (Vface_remapping_alist)
4023 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
4024 : DEFAULT_FACE_ID)
4025 : underlying_face_id (it);
4028 new_face_id = face_at_string_position (it->w,
4029 it->string,
4030 IT_STRING_CHARPOS (*it),
4031 bufpos,
4032 &next_stop,
4033 base_face_id, false);
4035 /* Is this a start of a run of characters with box? Caveat:
4036 this can be called for a freshly allocated iterator; face_id
4037 is -1 is this case. We know that the new face will not
4038 change until the next check pos, i.e. if the new face has a
4039 box, all characters up to that position will have a
4040 box. But, as usual, we don't know whether that position
4041 is really the end. */
4042 if (new_face_id != it->face_id)
4044 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4045 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
4047 /* If new face has a box but old face hasn't, this is the
4048 start of a run of characters with box, i.e. it has a
4049 shadow on the left side. */
4050 it->start_of_box_run_p
4051 = new_face->box && (old_face == NULL || !old_face->box);
4052 it->face_box_p = new_face->box != FACE_NO_BOX;
4056 it->face_id = new_face_id;
4057 return HANDLED_NORMALLY;
4061 /* Return the ID of the face ``underlying'' IT's current position,
4062 which is in a string. If the iterator is associated with a
4063 buffer, return the face at IT's current buffer position.
4064 Otherwise, use the iterator's base_face_id. */
4066 static int
4067 underlying_face_id (struct it *it)
4069 int face_id = it->base_face_id, i;
4071 eassert (STRINGP (it->string));
4073 for (i = it->sp - 1; i >= 0; --i)
4074 if (NILP (it->stack[i].string))
4075 face_id = it->stack[i].face_id;
4077 return face_id;
4081 /* Compute the face one character before or after the current position
4082 of IT, in the visual order. BEFORE_P means get the face
4083 in front (to the left in L2R paragraphs, to the right in R2L
4084 paragraphs) of IT's screen position. Value is the ID of the face. */
4086 static int
4087 face_before_or_after_it_pos (struct it *it, bool before_p)
4089 int face_id, limit;
4090 ptrdiff_t next_check_charpos;
4091 struct it it_copy;
4092 void *it_copy_data = NULL;
4094 eassert (it->s == NULL);
4096 if (STRINGP (it->string))
4098 ptrdiff_t bufpos, charpos;
4099 int base_face_id;
4101 /* No face change past the end of the string (for the case
4102 we are padding with spaces). No face change before the
4103 string start. */
4104 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4105 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4106 return it->face_id;
4108 if (!it->bidi_p)
4110 /* Set charpos to the position before or after IT's current
4111 position, in the logical order, which in the non-bidi
4112 case is the same as the visual order. */
4113 if (before_p)
4114 charpos = IT_STRING_CHARPOS (*it) - 1;
4115 else if (it->what == IT_COMPOSITION)
4116 /* For composition, we must check the character after the
4117 composition. */
4118 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4119 else
4120 charpos = IT_STRING_CHARPOS (*it) + 1;
4122 else
4124 if (before_p)
4126 /* With bidi iteration, the character before the current
4127 in the visual order cannot be found by simple
4128 iteration, because "reverse" reordering is not
4129 supported. Instead, we need to start from the string
4130 beginning and go all the way to the current string
4131 position, remembering the previous position. */
4132 /* Ignore face changes before the first visible
4133 character on this display line. */
4134 if (it->current_x <= it->first_visible_x)
4135 return it->face_id;
4136 SAVE_IT (it_copy, *it, it_copy_data);
4137 IT_STRING_CHARPOS (it_copy) = 0;
4138 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4142 charpos = IT_STRING_CHARPOS (it_copy);
4143 if (charpos >= SCHARS (it->string))
4144 break;
4145 bidi_move_to_visually_next (&it_copy.bidi_it);
4147 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4149 RESTORE_IT (it, it, it_copy_data);
4151 else
4153 /* Set charpos to the string position of the character
4154 that comes after IT's current position in the visual
4155 order. */
4156 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4158 it_copy = *it;
4159 while (n--)
4160 bidi_move_to_visually_next (&it_copy.bidi_it);
4162 charpos = it_copy.bidi_it.charpos;
4165 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4167 if (it->current.overlay_string_index >= 0)
4168 bufpos = IT_CHARPOS (*it);
4169 else
4170 bufpos = 0;
4172 base_face_id = underlying_face_id (it);
4174 /* Get the face for ASCII, or unibyte. */
4175 face_id = face_at_string_position (it->w,
4176 it->string,
4177 charpos,
4178 bufpos,
4179 &next_check_charpos,
4180 base_face_id, false);
4182 /* Correct the face for charsets different from ASCII. Do it
4183 for the multibyte case only. The face returned above is
4184 suitable for unibyte text if IT->string is unibyte. */
4185 if (STRING_MULTIBYTE (it->string))
4187 struct text_pos pos1 = string_pos (charpos, it->string);
4188 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4189 int c, len;
4190 struct face *face = FACE_FROM_ID (it->f, face_id);
4192 c = string_char_and_length (p, &len);
4193 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4196 else
4198 struct text_pos pos;
4200 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4201 || (IT_CHARPOS (*it) <= BEGV && before_p))
4202 return it->face_id;
4204 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4205 pos = it->current.pos;
4207 if (!it->bidi_p)
4209 if (before_p)
4210 DEC_TEXT_POS (pos, it->multibyte_p);
4211 else
4213 if (it->what == IT_COMPOSITION)
4215 /* For composition, we must check the position after
4216 the composition. */
4217 pos.charpos += it->cmp_it.nchars;
4218 pos.bytepos += it->len;
4220 else
4221 INC_TEXT_POS (pos, it->multibyte_p);
4224 else
4226 if (before_p)
4228 int current_x;
4230 /* With bidi iteration, the character before the current
4231 in the visual order cannot be found by simple
4232 iteration, because "reverse" reordering is not
4233 supported. Instead, we need to use the move_it_*
4234 family of functions, and move to the previous
4235 character starting from the beginning of the visual
4236 line. */
4237 /* Ignore face changes before the first visible
4238 character on this display line. */
4239 if (it->current_x <= it->first_visible_x)
4240 return it->face_id;
4241 SAVE_IT (it_copy, *it, it_copy_data);
4242 /* Implementation note: Since move_it_in_display_line
4243 works in the iterator geometry, and thinks the first
4244 character is always the leftmost, even in R2L lines,
4245 we don't need to distinguish between the R2L and L2R
4246 cases here. */
4247 current_x = it_copy.current_x;
4248 move_it_vertically_backward (&it_copy, 0);
4249 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4250 pos = it_copy.current.pos;
4251 RESTORE_IT (it, it, it_copy_data);
4253 else
4255 /* Set charpos to the buffer position of the character
4256 that comes after IT's current position in the visual
4257 order. */
4258 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4260 it_copy = *it;
4261 while (n--)
4262 bidi_move_to_visually_next (&it_copy.bidi_it);
4264 SET_TEXT_POS (pos,
4265 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4268 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4270 /* Determine face for CHARSET_ASCII, or unibyte. */
4271 face_id = face_at_buffer_position (it->w,
4272 CHARPOS (pos),
4273 &next_check_charpos,
4274 limit, false, -1);
4276 /* Correct the face for charsets different from ASCII. Do it
4277 for the multibyte case only. The face returned above is
4278 suitable for unibyte text if current_buffer is unibyte. */
4279 if (it->multibyte_p)
4281 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4282 struct face *face = FACE_FROM_ID (it->f, face_id);
4283 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4287 return face_id;
4292 /***********************************************************************
4293 Invisible text
4294 ***********************************************************************/
4296 /* Set up iterator IT from invisible properties at its current
4297 position. Called from handle_stop. */
4299 static enum prop_handled
4300 handle_invisible_prop (struct it *it)
4302 enum prop_handled handled = HANDLED_NORMALLY;
4303 int invis;
4304 Lisp_Object prop;
4306 if (STRINGP (it->string))
4308 Lisp_Object end_charpos, limit;
4310 /* Get the value of the invisible text property at the
4311 current position. Value will be nil if there is no such
4312 property. */
4313 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4314 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4315 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4317 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4319 /* Record whether we have to display an ellipsis for the
4320 invisible text. */
4321 bool display_ellipsis_p = (invis == 2);
4322 ptrdiff_t len, endpos;
4324 handled = HANDLED_RECOMPUTE_PROPS;
4326 /* Get the position at which the next visible text can be
4327 found in IT->string, if any. */
4328 endpos = len = SCHARS (it->string);
4329 XSETINT (limit, len);
4332 end_charpos
4333 = Fnext_single_property_change (end_charpos, Qinvisible,
4334 it->string, limit);
4335 /* Since LIMIT is always an integer, so should be the
4336 value returned by Fnext_single_property_change. */
4337 eassert (INTEGERP (end_charpos));
4338 if (INTEGERP (end_charpos))
4340 endpos = XFASTINT (end_charpos);
4341 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4342 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4343 if (invis == 2)
4344 display_ellipsis_p = true;
4346 else /* Should never happen; but if it does, exit the loop. */
4347 endpos = len;
4349 while (invis != 0 && endpos < len);
4351 if (display_ellipsis_p)
4352 it->ellipsis_p = true;
4354 if (endpos < len)
4356 /* Text at END_CHARPOS is visible. Move IT there. */
4357 struct text_pos old;
4358 ptrdiff_t oldpos;
4360 old = it->current.string_pos;
4361 oldpos = CHARPOS (old);
4362 if (it->bidi_p)
4364 if (it->bidi_it.first_elt
4365 && it->bidi_it.charpos < SCHARS (it->string))
4366 bidi_paragraph_init (it->paragraph_embedding,
4367 &it->bidi_it, true);
4368 /* Bidi-iterate out of the invisible text. */
4371 bidi_move_to_visually_next (&it->bidi_it);
4373 while (oldpos <= it->bidi_it.charpos
4374 && it->bidi_it.charpos < endpos
4375 && it->bidi_it.charpos < it->bidi_it.string.schars);
4377 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4378 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4379 if (IT_CHARPOS (*it) >= endpos)
4380 it->prev_stop = endpos;
4382 else
4384 IT_STRING_CHARPOS (*it) = endpos;
4385 compute_string_pos (&it->current.string_pos, old, it->string);
4388 else
4390 /* The rest of the string is invisible. If this is an
4391 overlay string, proceed with the next overlay string
4392 or whatever comes and return a character from there. */
4393 if (it->current.overlay_string_index >= 0
4394 && !display_ellipsis_p)
4396 next_overlay_string (it);
4397 /* Don't check for overlay strings when we just
4398 finished processing them. */
4399 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4401 else
4403 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4404 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4409 else
4411 ptrdiff_t newpos, next_stop, start_charpos, tem;
4412 Lisp_Object pos, overlay;
4414 /* First of all, is there invisible text at this position? */
4415 tem = start_charpos = IT_CHARPOS (*it);
4416 pos = make_number (tem);
4417 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4418 &overlay);
4419 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4421 /* If we are on invisible text, skip over it. */
4422 if (invis != 0 && start_charpos < it->end_charpos)
4424 /* Record whether we have to display an ellipsis for the
4425 invisible text. */
4426 bool display_ellipsis_p = invis == 2;
4428 handled = HANDLED_RECOMPUTE_PROPS;
4430 /* Loop skipping over invisible text. The loop is left at
4431 ZV or with IT on the first char being visible again. */
4434 /* Try to skip some invisible text. Return value is the
4435 position reached which can be equal to where we start
4436 if there is nothing invisible there. This skips both
4437 over invisible text properties and overlays with
4438 invisible property. */
4439 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4441 /* If we skipped nothing at all we weren't at invisible
4442 text in the first place. If everything to the end of
4443 the buffer was skipped, end the loop. */
4444 if (newpos == tem || newpos >= ZV)
4445 invis = 0;
4446 else
4448 /* We skipped some characters but not necessarily
4449 all there are. Check if we ended up on visible
4450 text. Fget_char_property returns the property of
4451 the char before the given position, i.e. if we
4452 get invis = 0, this means that the char at
4453 newpos is visible. */
4454 pos = make_number (newpos);
4455 prop = Fget_char_property (pos, Qinvisible, it->window);
4456 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4459 /* If we ended up on invisible text, proceed to
4460 skip starting with next_stop. */
4461 if (invis != 0)
4462 tem = next_stop;
4464 /* If there are adjacent invisible texts, don't lose the
4465 second one's ellipsis. */
4466 if (invis == 2)
4467 display_ellipsis_p = true;
4469 while (invis != 0);
4471 /* The position newpos is now either ZV or on visible text. */
4472 if (it->bidi_p)
4474 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4475 bool on_newline
4476 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4477 bool after_newline
4478 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4480 /* If the invisible text ends on a newline or on a
4481 character after a newline, we can avoid the costly,
4482 character by character, bidi iteration to NEWPOS, and
4483 instead simply reseat the iterator there. That's
4484 because all bidi reordering information is tossed at
4485 the newline. This is a big win for modes that hide
4486 complete lines, like Outline, Org, etc. */
4487 if (on_newline || after_newline)
4489 struct text_pos tpos;
4490 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4492 SET_TEXT_POS (tpos, newpos, bpos);
4493 reseat_1 (it, tpos, false);
4494 /* If we reseat on a newline/ZV, we need to prep the
4495 bidi iterator for advancing to the next character
4496 after the newline/EOB, keeping the current paragraph
4497 direction (so that PRODUCE_GLYPHS does TRT wrt
4498 prepending/appending glyphs to a glyph row). */
4499 if (on_newline)
4501 it->bidi_it.first_elt = false;
4502 it->bidi_it.paragraph_dir = pdir;
4503 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4504 it->bidi_it.nchars = 1;
4505 it->bidi_it.ch_len = 1;
4508 else /* Must use the slow method. */
4510 /* With bidi iteration, the region of invisible text
4511 could start and/or end in the middle of a
4512 non-base embedding level. Therefore, we need to
4513 skip invisible text using the bidi iterator,
4514 starting at IT's current position, until we find
4515 ourselves outside of the invisible text.
4516 Skipping invisible text _after_ bidi iteration
4517 avoids affecting the visual order of the
4518 displayed text when invisible properties are
4519 added or removed. */
4520 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4522 /* If we were `reseat'ed to a new paragraph,
4523 determine the paragraph base direction. We
4524 need to do it now because
4525 next_element_from_buffer may not have a
4526 chance to do it, if we are going to skip any
4527 text at the beginning, which resets the
4528 FIRST_ELT flag. */
4529 bidi_paragraph_init (it->paragraph_embedding,
4530 &it->bidi_it, true);
4534 bidi_move_to_visually_next (&it->bidi_it);
4536 while (it->stop_charpos <= it->bidi_it.charpos
4537 && it->bidi_it.charpos < newpos);
4538 IT_CHARPOS (*it) = it->bidi_it.charpos;
4539 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4540 /* If we overstepped NEWPOS, record its position in
4541 the iterator, so that we skip invisible text if
4542 later the bidi iteration lands us in the
4543 invisible region again. */
4544 if (IT_CHARPOS (*it) >= newpos)
4545 it->prev_stop = newpos;
4548 else
4550 IT_CHARPOS (*it) = newpos;
4551 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4554 if (display_ellipsis_p)
4556 /* Make sure that the glyphs of the ellipsis will get
4557 correct `charpos' values. If we would not update
4558 it->position here, the glyphs would belong to the
4559 last visible character _before_ the invisible
4560 text, which confuses `set_cursor_from_row'.
4562 We use the last invisible position instead of the
4563 first because this way the cursor is always drawn on
4564 the first "." of the ellipsis, whenever PT is inside
4565 the invisible text. Otherwise the cursor would be
4566 placed _after_ the ellipsis when the point is after the
4567 first invisible character. */
4568 if (!STRINGP (it->object))
4570 it->position.charpos = newpos - 1;
4571 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4575 /* If there are before-strings at the start of invisible
4576 text, and the text is invisible because of a text
4577 property, arrange to show before-strings because 20.x did
4578 it that way. (If the text is invisible because of an
4579 overlay property instead of a text property, this is
4580 already handled in the overlay code.) */
4581 if (NILP (overlay)
4582 && get_overlay_strings (it, it->stop_charpos))
4584 handled = HANDLED_RECOMPUTE_PROPS;
4585 if (it->sp > 0)
4587 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4588 /* The call to get_overlay_strings above recomputes
4589 it->stop_charpos, but it only considers changes
4590 in properties and overlays beyond iterator's
4591 current position. This causes us to miss changes
4592 that happen exactly where the invisible property
4593 ended. So we play it safe here and force the
4594 iterator to check for potential stop positions
4595 immediately after the invisible text. Note that
4596 if get_overlay_strings returns true, it
4597 normally also pushed the iterator stack, so we
4598 need to update the stop position in the slot
4599 below the current one. */
4600 it->stack[it->sp - 1].stop_charpos
4601 = CHARPOS (it->stack[it->sp - 1].current.pos);
4604 else if (display_ellipsis_p)
4606 it->ellipsis_p = true;
4607 /* Let the ellipsis display before
4608 considering any properties of the following char.
4609 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4610 handled = HANDLED_RETURN;
4615 return handled;
4619 /* Make iterator IT return `...' next.
4620 Replaces LEN characters from buffer. */
4622 static void
4623 setup_for_ellipsis (struct it *it, int len)
4625 /* Use the display table definition for `...'. Invalid glyphs
4626 will be handled by the method returning elements from dpvec. */
4627 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4629 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4630 it->dpvec = v->contents;
4631 it->dpend = v->contents + v->header.size;
4633 else
4635 /* Default `...'. */
4636 it->dpvec = default_invis_vector;
4637 it->dpend = default_invis_vector + 3;
4640 it->dpvec_char_len = len;
4641 it->current.dpvec_index = 0;
4642 it->dpvec_face_id = -1;
4644 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4645 face as the preceding text. IT->saved_face_id was set in
4646 handle_stop to the face of the preceding character, and will be
4647 different from IT->face_id only if the invisible text skipped in
4648 handle_invisible_prop has some non-default face on its first
4649 character. We thus ignore the face of the invisible text when we
4650 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4651 if (it->saved_face_id >= 0)
4652 it->face_id = it->saved_face_id;
4654 /* If the ellipsis represents buffer text, it means we advanced in
4655 the buffer, so we should no longer ignore overlay strings. */
4656 if (it->method == GET_FROM_BUFFER)
4657 it->ignore_overlay_strings_at_pos_p = false;
4659 it->method = GET_FROM_DISPLAY_VECTOR;
4660 it->ellipsis_p = true;
4665 /***********************************************************************
4666 'display' property
4667 ***********************************************************************/
4669 /* Set up iterator IT from `display' property at its current position.
4670 Called from handle_stop.
4671 We return HANDLED_RETURN if some part of the display property
4672 overrides the display of the buffer text itself.
4673 Otherwise we return HANDLED_NORMALLY. */
4675 static enum prop_handled
4676 handle_display_prop (struct it *it)
4678 Lisp_Object propval, object, overlay;
4679 struct text_pos *position;
4680 ptrdiff_t bufpos;
4681 /* Nonzero if some property replaces the display of the text itself. */
4682 int display_replaced = 0;
4684 if (STRINGP (it->string))
4686 object = it->string;
4687 position = &it->current.string_pos;
4688 bufpos = CHARPOS (it->current.pos);
4690 else
4692 XSETWINDOW (object, it->w);
4693 position = &it->current.pos;
4694 bufpos = CHARPOS (*position);
4697 /* Reset those iterator values set from display property values. */
4698 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4699 it->space_width = Qnil;
4700 it->font_height = Qnil;
4701 it->voffset = 0;
4703 /* We don't support recursive `display' properties, i.e. string
4704 values that have a string `display' property, that have a string
4705 `display' property etc. */
4706 if (!it->string_from_display_prop_p)
4707 it->area = TEXT_AREA;
4709 propval = get_char_property_and_overlay (make_number (position->charpos),
4710 Qdisplay, object, &overlay);
4711 if (NILP (propval))
4712 return HANDLED_NORMALLY;
4713 /* Now OVERLAY is the overlay that gave us this property, or nil
4714 if it was a text property. */
4716 if (!STRINGP (it->string))
4717 object = it->w->contents;
4719 display_replaced = handle_display_spec (it, propval, object, overlay,
4720 position, bufpos,
4721 FRAME_WINDOW_P (it->f));
4722 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4725 /* Subroutine of handle_display_prop. Returns non-zero if the display
4726 specification in SPEC is a replacing specification, i.e. it would
4727 replace the text covered by `display' property with something else,
4728 such as an image or a display string. If SPEC includes any kind or
4729 `(space ...) specification, the value is 2; this is used by
4730 compute_display_string_pos, which see.
4732 See handle_single_display_spec for documentation of arguments.
4733 FRAME_WINDOW_P is true if the window being redisplayed is on a
4734 GUI frame; this argument is used only if IT is NULL, see below.
4736 IT can be NULL, if this is called by the bidi reordering code
4737 through compute_display_string_pos, which see. In that case, this
4738 function only examines SPEC, but does not otherwise "handle" it, in
4739 the sense that it doesn't set up members of IT from the display
4740 spec. */
4741 static int
4742 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4743 Lisp_Object overlay, struct text_pos *position,
4744 ptrdiff_t bufpos, bool frame_window_p)
4746 int replacing = 0;
4748 if (CONSP (spec)
4749 /* Simple specifications. */
4750 && !EQ (XCAR (spec), Qimage)
4751 #ifdef HAVE_XWIDGETS
4752 && !EQ (XCAR (spec), Qxwidget)
4753 #endif
4754 && !EQ (XCAR (spec), Qspace)
4755 && !EQ (XCAR (spec), Qwhen)
4756 && !EQ (XCAR (spec), Qslice)
4757 && !EQ (XCAR (spec), Qspace_width)
4758 && !EQ (XCAR (spec), Qheight)
4759 && !EQ (XCAR (spec), Qraise)
4760 /* Marginal area specifications. */
4761 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4762 && !EQ (XCAR (spec), Qleft_fringe)
4763 && !EQ (XCAR (spec), Qright_fringe)
4764 && !NILP (XCAR (spec)))
4766 for (; CONSP (spec); spec = XCDR (spec))
4768 int rv = handle_single_display_spec (it, XCAR (spec), object,
4769 overlay, position, bufpos,
4770 replacing, frame_window_p);
4771 if (rv != 0)
4773 replacing = rv;
4774 /* If some text in a string is replaced, `position' no
4775 longer points to the position of `object'. */
4776 if (!it || STRINGP (object))
4777 break;
4781 else if (VECTORP (spec))
4783 ptrdiff_t i;
4784 for (i = 0; i < ASIZE (spec); ++i)
4786 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4787 overlay, position, bufpos,
4788 replacing, frame_window_p);
4789 if (rv != 0)
4791 replacing = rv;
4792 /* If some text in a string is replaced, `position' no
4793 longer points to the position of `object'. */
4794 if (!it || STRINGP (object))
4795 break;
4799 else
4800 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4801 bufpos, 0, frame_window_p);
4802 return replacing;
4805 /* Value is the position of the end of the `display' property starting
4806 at START_POS in OBJECT. */
4808 static struct text_pos
4809 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4811 Lisp_Object end;
4812 struct text_pos end_pos;
4814 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4815 Qdisplay, object, Qnil);
4816 CHARPOS (end_pos) = XFASTINT (end);
4817 if (STRINGP (object))
4818 compute_string_pos (&end_pos, start_pos, it->string);
4819 else
4820 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4822 return end_pos;
4826 /* Set up IT from a single `display' property specification SPEC. OBJECT
4827 is the object in which the `display' property was found. *POSITION
4828 is the position in OBJECT at which the `display' property was found.
4829 BUFPOS is the buffer position of OBJECT (different from POSITION if
4830 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4831 previously saw a display specification which already replaced text
4832 display with something else, for example an image; we ignore such
4833 properties after the first one has been processed.
4835 OVERLAY is the overlay this `display' property came from,
4836 or nil if it was a text property.
4838 If SPEC is a `space' or `image' specification, and in some other
4839 cases too, set *POSITION to the position where the `display'
4840 property ends.
4842 If IT is NULL, only examine the property specification in SPEC, but
4843 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4844 is intended to be displayed in a window on a GUI frame.
4846 Value is non-zero if something was found which replaces the display
4847 of buffer or string text. */
4849 static int
4850 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4851 Lisp_Object overlay, struct text_pos *position,
4852 ptrdiff_t bufpos, int display_replaced,
4853 bool frame_window_p)
4855 Lisp_Object form;
4856 Lisp_Object location, value;
4857 struct text_pos start_pos = *position;
4859 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4860 If the result is non-nil, use VALUE instead of SPEC. */
4861 form = Qt;
4862 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4864 spec = XCDR (spec);
4865 if (!CONSP (spec))
4866 return 0;
4867 form = XCAR (spec);
4868 spec = XCDR (spec);
4871 if (!NILP (form) && !EQ (form, Qt))
4873 ptrdiff_t count = SPECPDL_INDEX ();
4875 /* Bind `object' to the object having the `display' property, a
4876 buffer or string. Bind `position' to the position in the
4877 object where the property was found, and `buffer-position'
4878 to the current position in the buffer. */
4880 if (NILP (object))
4881 XSETBUFFER (object, current_buffer);
4882 specbind (Qobject, object);
4883 specbind (Qposition, make_number (CHARPOS (*position)));
4884 specbind (Qbuffer_position, make_number (bufpos));
4885 form = safe_eval (form);
4886 unbind_to (count, Qnil);
4889 if (NILP (form))
4890 return 0;
4892 /* Handle `(height HEIGHT)' specifications. */
4893 if (CONSP (spec)
4894 && EQ (XCAR (spec), Qheight)
4895 && CONSP (XCDR (spec)))
4897 if (it)
4899 if (!FRAME_WINDOW_P (it->f))
4900 return 0;
4902 it->font_height = XCAR (XCDR (spec));
4903 if (!NILP (it->font_height))
4905 int new_height = -1;
4907 if (CONSP (it->font_height)
4908 && (EQ (XCAR (it->font_height), Qplus)
4909 || EQ (XCAR (it->font_height), Qminus))
4910 && CONSP (XCDR (it->font_height))
4911 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4913 /* `(+ N)' or `(- N)' where N is an integer. */
4914 int steps = XINT (XCAR (XCDR (it->font_height)));
4915 if (EQ (XCAR (it->font_height), Qplus))
4916 steps = - steps;
4917 it->face_id = smaller_face (it->f, it->face_id, steps);
4919 else if (FUNCTIONP (it->font_height))
4921 /* Call function with current height as argument.
4922 Value is the new height. */
4923 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4924 Lisp_Object height;
4925 height = safe_call1 (it->font_height,
4926 face->lface[LFACE_HEIGHT_INDEX]);
4927 if (NUMBERP (height))
4928 new_height = XFLOATINT (height);
4930 else if (NUMBERP (it->font_height))
4932 /* Value is a multiple of the canonical char height. */
4933 struct face *f;
4935 f = FACE_FROM_ID (it->f,
4936 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4937 new_height = (XFLOATINT (it->font_height)
4938 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4940 else
4942 /* Evaluate IT->font_height with `height' bound to the
4943 current specified height to get the new height. */
4944 ptrdiff_t count = SPECPDL_INDEX ();
4945 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4947 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4948 value = safe_eval (it->font_height);
4949 unbind_to (count, Qnil);
4951 if (NUMBERP (value))
4952 new_height = XFLOATINT (value);
4955 if (new_height > 0)
4956 it->face_id = face_with_height (it->f, it->face_id, new_height);
4960 return 0;
4963 /* Handle `(space-width WIDTH)'. */
4964 if (CONSP (spec)
4965 && EQ (XCAR (spec), Qspace_width)
4966 && CONSP (XCDR (spec)))
4968 if (it)
4970 if (!FRAME_WINDOW_P (it->f))
4971 return 0;
4973 value = XCAR (XCDR (spec));
4974 if (NUMBERP (value) && XFLOATINT (value) > 0)
4975 it->space_width = value;
4978 return 0;
4981 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4982 if (CONSP (spec)
4983 && EQ (XCAR (spec), Qslice))
4985 Lisp_Object tem;
4987 if (it)
4989 if (!FRAME_WINDOW_P (it->f))
4990 return 0;
4992 if (tem = XCDR (spec), CONSP (tem))
4994 it->slice.x = XCAR (tem);
4995 if (tem = XCDR (tem), CONSP (tem))
4997 it->slice.y = XCAR (tem);
4998 if (tem = XCDR (tem), CONSP (tem))
5000 it->slice.width = XCAR (tem);
5001 if (tem = XCDR (tem), CONSP (tem))
5002 it->slice.height = XCAR (tem);
5008 return 0;
5011 /* Handle `(raise FACTOR)'. */
5012 if (CONSP (spec)
5013 && EQ (XCAR (spec), Qraise)
5014 && CONSP (XCDR (spec)))
5016 if (it)
5018 if (!FRAME_WINDOW_P (it->f))
5019 return 0;
5021 #ifdef HAVE_WINDOW_SYSTEM
5022 value = XCAR (XCDR (spec));
5023 if (NUMBERP (value))
5025 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5026 it->voffset = - (XFLOATINT (value)
5027 * (normal_char_height (face->font, -1)));
5029 #endif /* HAVE_WINDOW_SYSTEM */
5032 return 0;
5035 /* Don't handle the other kinds of display specifications
5036 inside a string that we got from a `display' property. */
5037 if (it && it->string_from_display_prop_p)
5038 return 0;
5040 /* Characters having this form of property are not displayed, so
5041 we have to find the end of the property. */
5042 if (it)
5044 start_pos = *position;
5045 *position = display_prop_end (it, object, start_pos);
5046 /* If the display property comes from an overlay, don't consider
5047 any potential stop_charpos values before the end of that
5048 overlay. Since display_prop_end will happily find another
5049 'display' property coming from some other overlay or text
5050 property on buffer positions before this overlay's end, we
5051 need to ignore them, or else we risk displaying this
5052 overlay's display string/image twice. */
5053 if (!NILP (overlay))
5055 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5057 /* Some borderline-sane Lisp might call us with the current
5058 buffer narrowed so that overlay-end is outside the
5059 POINT_MIN..POINT_MAX region, which will then cause
5060 various assertion violations and crashes down the road,
5061 starting with pop_it when it will attempt to use POSITION
5062 set below. Prevent that. */
5063 ovendpos = clip_to_bounds (BEGV, ovendpos, ZV);
5065 if (ovendpos > CHARPOS (*position))
5066 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5069 value = Qnil;
5071 /* Stop the scan at that end position--we assume that all
5072 text properties change there. */
5073 if (it)
5074 it->stop_charpos = position->charpos;
5076 /* Handle `(left-fringe BITMAP [FACE])'
5077 and `(right-fringe BITMAP [FACE])'. */
5078 if (CONSP (spec)
5079 && (EQ (XCAR (spec), Qleft_fringe)
5080 || EQ (XCAR (spec), Qright_fringe))
5081 && CONSP (XCDR (spec)))
5083 if (it)
5085 if (!FRAME_WINDOW_P (it->f))
5086 /* If we return here, POSITION has been advanced
5087 across the text with this property. */
5089 /* Synchronize the bidi iterator with POSITION. This is
5090 needed because we are not going to push the iterator
5091 on behalf of this display property, so there will be
5092 no pop_it call to do this synchronization for us. */
5093 if (it->bidi_p)
5095 it->position = *position;
5096 iterate_out_of_display_property (it);
5097 *position = it->position;
5099 return 1;
5102 else if (!frame_window_p)
5103 return 1;
5105 #ifdef HAVE_WINDOW_SYSTEM
5106 value = XCAR (XCDR (spec));
5107 int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
5108 if (! fringe_bitmap)
5109 /* If we return here, POSITION has been advanced
5110 across the text with this property. */
5112 if (it && it->bidi_p)
5114 it->position = *position;
5115 iterate_out_of_display_property (it);
5116 *position = it->position;
5118 return 1;
5121 if (it)
5123 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5125 if (CONSP (XCDR (XCDR (spec))))
5127 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5128 int face_id2 = lookup_derived_face (it->f, face_name,
5129 FRINGE_FACE_ID, false);
5130 if (face_id2 >= 0)
5131 face_id = face_id2;
5134 /* Save current settings of IT so that we can restore them
5135 when we are finished with the glyph property value. */
5136 push_it (it, position);
5138 it->area = TEXT_AREA;
5139 it->what = IT_IMAGE;
5140 it->image_id = -1; /* no image */
5141 it->position = start_pos;
5142 it->object = NILP (object) ? it->w->contents : object;
5143 it->method = GET_FROM_IMAGE;
5144 it->from_overlay = Qnil;
5145 it->face_id = face_id;
5146 it->from_disp_prop_p = true;
5148 /* Say that we haven't consumed the characters with
5149 `display' property yet. The call to pop_it in
5150 set_iterator_to_next will clean this up. */
5151 *position = start_pos;
5153 if (EQ (XCAR (spec), Qleft_fringe))
5155 it->left_user_fringe_bitmap = fringe_bitmap;
5156 it->left_user_fringe_face_id = face_id;
5158 else
5160 it->right_user_fringe_bitmap = fringe_bitmap;
5161 it->right_user_fringe_face_id = face_id;
5164 #endif /* HAVE_WINDOW_SYSTEM */
5165 return 1;
5168 /* Prepare to handle `((margin left-margin) ...)',
5169 `((margin right-margin) ...)' and `((margin nil) ...)'
5170 prefixes for display specifications. */
5171 location = Qunbound;
5172 if (CONSP (spec) && CONSP (XCAR (spec)))
5174 Lisp_Object tem;
5176 value = XCDR (spec);
5177 if (CONSP (value))
5178 value = XCAR (value);
5180 tem = XCAR (spec);
5181 if (EQ (XCAR (tem), Qmargin)
5182 && (tem = XCDR (tem),
5183 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5184 (NILP (tem)
5185 || EQ (tem, Qleft_margin)
5186 || EQ (tem, Qright_margin))))
5187 location = tem;
5190 if (EQ (location, Qunbound))
5192 location = Qnil;
5193 value = spec;
5196 /* After this point, VALUE is the property after any
5197 margin prefix has been stripped. It must be a string,
5198 an image specification, or `(space ...)'.
5200 LOCATION specifies where to display: `left-margin',
5201 `right-margin' or nil. */
5203 bool valid_p = (STRINGP (value)
5204 #ifdef HAVE_WINDOW_SYSTEM
5205 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5206 && valid_image_p (value))
5207 #endif /* not HAVE_WINDOW_SYSTEM */
5208 || (CONSP (value) && EQ (XCAR (value), Qspace))
5209 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5210 && valid_xwidget_spec_p (value)));
5212 if (valid_p && display_replaced == 0)
5214 int retval = 1;
5216 if (!it)
5218 /* Callers need to know whether the display spec is any kind
5219 of `(space ...)' spec that is about to affect text-area
5220 display. */
5221 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5222 retval = 2;
5223 return retval;
5226 /* Save current settings of IT so that we can restore them
5227 when we are finished with the glyph property value. */
5228 push_it (it, position);
5229 it->from_overlay = overlay;
5230 it->from_disp_prop_p = true;
5232 if (NILP (location))
5233 it->area = TEXT_AREA;
5234 else if (EQ (location, Qleft_margin))
5235 it->area = LEFT_MARGIN_AREA;
5236 else
5237 it->area = RIGHT_MARGIN_AREA;
5239 if (STRINGP (value))
5241 it->string = value;
5242 it->multibyte_p = STRING_MULTIBYTE (it->string);
5243 it->current.overlay_string_index = -1;
5244 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5245 it->end_charpos = it->string_nchars = SCHARS (it->string);
5246 it->method = GET_FROM_STRING;
5247 it->stop_charpos = 0;
5248 it->prev_stop = 0;
5249 it->base_level_stop = 0;
5250 it->string_from_display_prop_p = true;
5251 /* Say that we haven't consumed the characters with
5252 `display' property yet. The call to pop_it in
5253 set_iterator_to_next will clean this up. */
5254 if (BUFFERP (object))
5255 *position = start_pos;
5257 /* Force paragraph direction to be that of the parent
5258 object. If the parent object's paragraph direction is
5259 not yet determined, default to L2R. */
5260 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5261 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5262 else
5263 it->paragraph_embedding = L2R;
5265 /* Set up the bidi iterator for this display string. */
5266 if (it->bidi_p)
5268 it->bidi_it.string.lstring = it->string;
5269 it->bidi_it.string.s = NULL;
5270 it->bidi_it.string.schars = it->end_charpos;
5271 it->bidi_it.string.bufpos = bufpos;
5272 it->bidi_it.string.from_disp_str = true;
5273 it->bidi_it.string.unibyte = !it->multibyte_p;
5274 it->bidi_it.w = it->w;
5275 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5278 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5280 it->method = GET_FROM_STRETCH;
5281 it->object = value;
5282 *position = it->position = start_pos;
5283 retval = 1 + (it->area == TEXT_AREA);
5285 else if (valid_xwidget_spec_p (value))
5287 it->what = IT_XWIDGET;
5288 it->method = GET_FROM_XWIDGET;
5289 it->position = start_pos;
5290 it->object = NILP (object) ? it->w->contents : object;
5291 *position = start_pos;
5292 it->xwidget = lookup_xwidget (value);
5294 #ifdef HAVE_WINDOW_SYSTEM
5295 else
5297 it->what = IT_IMAGE;
5298 it->image_id = lookup_image (it->f, value);
5299 it->position = start_pos;
5300 it->object = NILP (object) ? it->w->contents : object;
5301 it->method = GET_FROM_IMAGE;
5303 /* Say that we haven't consumed the characters with
5304 `display' property yet. The call to pop_it in
5305 set_iterator_to_next will clean this up. */
5306 *position = start_pos;
5308 #endif /* HAVE_WINDOW_SYSTEM */
5310 return retval;
5313 /* Invalid property or property not supported. Restore
5314 POSITION to what it was before. */
5315 *position = start_pos;
5316 return 0;
5319 /* Check if PROP is a display property value whose text should be
5320 treated as intangible. OVERLAY is the overlay from which PROP
5321 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5322 specify the buffer position covered by PROP. */
5324 bool
5325 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5326 ptrdiff_t charpos, ptrdiff_t bytepos)
5328 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5329 struct text_pos position;
5331 SET_TEXT_POS (position, charpos, bytepos);
5332 return (handle_display_spec (NULL, prop, Qnil, overlay,
5333 &position, charpos, frame_window_p)
5334 != 0);
5338 /* Return true if PROP is a display sub-property value containing STRING.
5340 Implementation note: this and the following function are really
5341 special cases of handle_display_spec and
5342 handle_single_display_spec, and should ideally use the same code.
5343 Until they do, these two pairs must be consistent and must be
5344 modified in sync. */
5346 static bool
5347 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5349 if (EQ (string, prop))
5350 return true;
5352 /* Skip over `when FORM'. */
5353 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5355 prop = XCDR (prop);
5356 if (!CONSP (prop))
5357 return false;
5358 /* Actually, the condition following `when' should be eval'ed,
5359 like handle_single_display_spec does, and we should return
5360 false if it evaluates to nil. However, this function is
5361 called only when the buffer was already displayed and some
5362 glyph in the glyph matrix was found to come from a display
5363 string. Therefore, the condition was already evaluated, and
5364 the result was non-nil, otherwise the display string wouldn't
5365 have been displayed and we would have never been called for
5366 this property. Thus, we can skip the evaluation and assume
5367 its result is non-nil. */
5368 prop = XCDR (prop);
5371 if (CONSP (prop))
5372 /* Skip over `margin LOCATION'. */
5373 if (EQ (XCAR (prop), Qmargin))
5375 prop = XCDR (prop);
5376 if (!CONSP (prop))
5377 return false;
5379 prop = XCDR (prop);
5380 if (!CONSP (prop))
5381 return false;
5384 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5388 /* Return true if STRING appears in the `display' property PROP. */
5390 static bool
5391 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5393 if (CONSP (prop)
5394 && !EQ (XCAR (prop), Qwhen)
5395 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5397 /* A list of sub-properties. */
5398 while (CONSP (prop))
5400 if (single_display_spec_string_p (XCAR (prop), string))
5401 return true;
5402 prop = XCDR (prop);
5405 else if (VECTORP (prop))
5407 /* A vector of sub-properties. */
5408 ptrdiff_t i;
5409 for (i = 0; i < ASIZE (prop); ++i)
5410 if (single_display_spec_string_p (AREF (prop, i), string))
5411 return true;
5413 else
5414 return single_display_spec_string_p (prop, string);
5416 return false;
5419 /* Look for STRING in overlays and text properties in the current
5420 buffer, between character positions FROM and TO (excluding TO).
5421 BACK_P means look back (in this case, TO is supposed to be
5422 less than FROM).
5423 Value is the first character position where STRING was found, or
5424 zero if it wasn't found before hitting TO.
5426 This function may only use code that doesn't eval because it is
5427 called asynchronously from note_mouse_highlight. */
5429 static ptrdiff_t
5430 string_buffer_position_lim (Lisp_Object string,
5431 ptrdiff_t from, ptrdiff_t to, bool back_p)
5433 Lisp_Object limit, prop, pos;
5434 bool found = false;
5436 pos = make_number (max (from, BEGV));
5438 if (!back_p) /* looking forward */
5440 limit = make_number (min (to, ZV));
5441 while (!found && !EQ (pos, limit))
5443 prop = Fget_char_property (pos, Qdisplay, Qnil);
5444 if (!NILP (prop) && display_prop_string_p (prop, string))
5445 found = true;
5446 else
5447 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5448 limit);
5451 else /* looking back */
5453 limit = make_number (max (to, BEGV));
5454 while (!found && !EQ (pos, limit))
5456 prop = Fget_char_property (pos, Qdisplay, Qnil);
5457 if (!NILP (prop) && display_prop_string_p (prop, string))
5458 found = true;
5459 else
5460 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5461 limit);
5465 return found ? XINT (pos) : 0;
5468 /* Determine which buffer position in current buffer STRING comes from.
5469 AROUND_CHARPOS is an approximate position where it could come from.
5470 Value is the buffer position or 0 if it couldn't be determined.
5472 This function is necessary because we don't record buffer positions
5473 in glyphs generated from strings (to keep struct glyph small).
5474 This function may only use code that doesn't eval because it is
5475 called asynchronously from note_mouse_highlight. */
5477 static ptrdiff_t
5478 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5480 const int MAX_DISTANCE = 1000;
5481 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5482 around_charpos + MAX_DISTANCE,
5483 false);
5485 if (!found)
5486 found = string_buffer_position_lim (string, around_charpos,
5487 around_charpos - MAX_DISTANCE, true);
5488 return found;
5493 /***********************************************************************
5494 `composition' property
5495 ***********************************************************************/
5497 /* Set up iterator IT from `composition' property at its current
5498 position. Called from handle_stop. */
5500 static enum prop_handled
5501 handle_composition_prop (struct it *it)
5503 Lisp_Object prop, string;
5504 ptrdiff_t pos, pos_byte, start, end;
5506 if (STRINGP (it->string))
5508 unsigned char *s;
5510 pos = IT_STRING_CHARPOS (*it);
5511 pos_byte = IT_STRING_BYTEPOS (*it);
5512 string = it->string;
5513 s = SDATA (string) + pos_byte;
5514 it->c = STRING_CHAR (s);
5516 else
5518 pos = IT_CHARPOS (*it);
5519 pos_byte = IT_BYTEPOS (*it);
5520 string = Qnil;
5521 it->c = FETCH_CHAR (pos_byte);
5524 /* If there's a valid composition and point is not inside of the
5525 composition (in the case that the composition is from the current
5526 buffer), draw a glyph composed from the composition components. */
5527 if (find_composition (pos, -1, &start, &end, &prop, string)
5528 && composition_valid_p (start, end, prop)
5529 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5531 if (start < pos)
5532 /* As we can't handle this situation (perhaps font-lock added
5533 a new composition), we just return here hoping that next
5534 redisplay will detect this composition much earlier. */
5535 return HANDLED_NORMALLY;
5536 if (start != pos)
5538 if (STRINGP (it->string))
5539 pos_byte = string_char_to_byte (it->string, start);
5540 else
5541 pos_byte = CHAR_TO_BYTE (start);
5543 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5544 prop, string);
5546 if (it->cmp_it.id >= 0)
5548 it->cmp_it.ch = -1;
5549 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5550 it->cmp_it.nglyphs = -1;
5554 return HANDLED_NORMALLY;
5559 /***********************************************************************
5560 Overlay strings
5561 ***********************************************************************/
5563 /* The following structure is used to record overlay strings for
5564 later sorting in load_overlay_strings. */
5566 struct overlay_entry
5568 Lisp_Object overlay;
5569 Lisp_Object string;
5570 EMACS_INT priority;
5571 bool after_string_p;
5575 /* Set up iterator IT from overlay strings at its current position.
5576 Called from handle_stop. */
5578 static enum prop_handled
5579 handle_overlay_change (struct it *it)
5581 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5582 return HANDLED_RECOMPUTE_PROPS;
5583 else
5584 return HANDLED_NORMALLY;
5588 /* Set up the next overlay string for delivery by IT, if there is an
5589 overlay string to deliver. Called by set_iterator_to_next when the
5590 end of the current overlay string is reached. If there are more
5591 overlay strings to display, IT->string and
5592 IT->current.overlay_string_index are set appropriately here.
5593 Otherwise IT->string is set to nil. */
5595 static void
5596 next_overlay_string (struct it *it)
5598 ++it->current.overlay_string_index;
5599 if (it->current.overlay_string_index == it->n_overlay_strings)
5601 /* No more overlay strings. Restore IT's settings to what
5602 they were before overlay strings were processed, and
5603 continue to deliver from current_buffer. */
5605 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5606 pop_it (it);
5607 eassert (it->sp > 0
5608 || (NILP (it->string)
5609 && it->method == GET_FROM_BUFFER
5610 && it->stop_charpos >= BEGV
5611 && it->stop_charpos <= it->end_charpos));
5612 it->current.overlay_string_index = -1;
5613 it->n_overlay_strings = 0;
5614 /* If there's an empty display string on the stack, pop the
5615 stack, to resync the bidi iterator with IT's position. Such
5616 empty strings are pushed onto the stack in
5617 get_overlay_strings_1. */
5618 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5619 pop_it (it);
5621 /* Since we've exhausted overlay strings at this buffer
5622 position, set the flag to ignore overlays until we move to
5623 another position. (The flag will be reset in
5624 next_element_from_buffer.) But don't do that if the overlay
5625 strings were loaded at position other than the current one,
5626 which could happen if we called pop_it above, or if the
5627 overlay strings were loaded by handle_invisible_prop at the
5628 beginning of invisible text. */
5629 if (it->overlay_strings_charpos == IT_CHARPOS (*it))
5630 it->ignore_overlay_strings_at_pos_p = true;
5632 /* If we're at the end of the buffer, record that we have
5633 processed the overlay strings there already, so that
5634 next_element_from_buffer doesn't try it again. */
5635 if (NILP (it->string)
5636 && IT_CHARPOS (*it) >= it->end_charpos
5637 && it->overlay_strings_charpos >= it->end_charpos)
5638 it->overlay_strings_at_end_processed_p = true;
5639 /* Note: we reset overlay_strings_charpos only here, to make
5640 sure the just-processed overlays were indeed at EOB.
5641 Otherwise, overlays on text with invisible text property,
5642 which are processed with IT's position past the invisible
5643 text, might fool us into thinking the overlays at EOB were
5644 already processed (linum-mode can cause this, for
5645 example). */
5646 it->overlay_strings_charpos = -1;
5648 else
5650 /* There are more overlay strings to process. If
5651 IT->current.overlay_string_index has advanced to a position
5652 where we must load IT->overlay_strings with more strings, do
5653 it. We must load at the IT->overlay_strings_charpos where
5654 IT->n_overlay_strings was originally computed; when invisible
5655 text is present, this might not be IT_CHARPOS (Bug#7016). */
5656 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5658 if (it->current.overlay_string_index && i == 0)
5659 load_overlay_strings (it, it->overlay_strings_charpos);
5661 /* Initialize IT to deliver display elements from the overlay
5662 string. */
5663 it->string = it->overlay_strings[i];
5664 it->multibyte_p = STRING_MULTIBYTE (it->string);
5665 SET_TEXT_POS (it->current.string_pos, 0, 0);
5666 it->method = GET_FROM_STRING;
5667 it->stop_charpos = 0;
5668 it->end_charpos = SCHARS (it->string);
5669 if (it->cmp_it.stop_pos >= 0)
5670 it->cmp_it.stop_pos = 0;
5671 it->prev_stop = 0;
5672 it->base_level_stop = 0;
5674 /* Set up the bidi iterator for this overlay string. */
5675 if (it->bidi_p)
5677 it->bidi_it.string.lstring = it->string;
5678 it->bidi_it.string.s = NULL;
5679 it->bidi_it.string.schars = SCHARS (it->string);
5680 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5681 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5682 it->bidi_it.string.unibyte = !it->multibyte_p;
5683 it->bidi_it.w = it->w;
5684 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5688 CHECK_IT (it);
5692 /* Compare two overlay_entry structures E1 and E2. Used as a
5693 comparison function for qsort in load_overlay_strings. Overlay
5694 strings for the same position are sorted so that
5696 1. All after-strings come in front of before-strings, except
5697 when they come from the same overlay.
5699 2. Within after-strings, strings are sorted so that overlay strings
5700 from overlays with higher priorities come first.
5702 2. Within before-strings, strings are sorted so that overlay
5703 strings from overlays with higher priorities come last.
5705 Value is analogous to strcmp. */
5708 static int
5709 compare_overlay_entries (const void *e1, const void *e2)
5711 struct overlay_entry const *entry1 = e1;
5712 struct overlay_entry const *entry2 = e2;
5713 int result;
5715 if (entry1->after_string_p != entry2->after_string_p)
5717 /* Let after-strings appear in front of before-strings if
5718 they come from different overlays. */
5719 if (EQ (entry1->overlay, entry2->overlay))
5720 result = entry1->after_string_p ? 1 : -1;
5721 else
5722 result = entry1->after_string_p ? -1 : 1;
5724 else if (entry1->priority != entry2->priority)
5726 if (entry1->after_string_p)
5727 /* After-strings sorted in order of decreasing priority. */
5728 result = entry2->priority < entry1->priority ? -1 : 1;
5729 else
5730 /* Before-strings sorted in order of increasing priority. */
5731 result = entry1->priority < entry2->priority ? -1 : 1;
5733 else
5734 result = 0;
5736 return result;
5740 /* Load the vector IT->overlay_strings with overlay strings from IT's
5741 current buffer position, or from CHARPOS if that is > 0. Set
5742 IT->n_overlays to the total number of overlay strings found.
5744 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5745 a time. On entry into load_overlay_strings,
5746 IT->current.overlay_string_index gives the number of overlay
5747 strings that have already been loaded by previous calls to this
5748 function.
5750 IT->add_overlay_start contains an additional overlay start
5751 position to consider for taking overlay strings from, if non-zero.
5752 This position comes into play when the overlay has an `invisible'
5753 property, and both before and after-strings. When we've skipped to
5754 the end of the overlay, because of its `invisible' property, we
5755 nevertheless want its before-string to appear.
5756 IT->add_overlay_start will contain the overlay start position
5757 in this case.
5759 Overlay strings are sorted so that after-string strings come in
5760 front of before-string strings. Within before and after-strings,
5761 strings are sorted by overlay priority. See also function
5762 compare_overlay_entries. */
5764 static void
5765 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5767 Lisp_Object overlay, window, str, invisible;
5768 struct Lisp_Overlay *ov;
5769 ptrdiff_t start, end;
5770 ptrdiff_t n = 0, i, j;
5771 int invis;
5772 struct overlay_entry entriesbuf[20];
5773 ptrdiff_t size = ARRAYELTS (entriesbuf);
5774 struct overlay_entry *entries = entriesbuf;
5775 USE_SAFE_ALLOCA;
5777 if (charpos <= 0)
5778 charpos = IT_CHARPOS (*it);
5780 /* Append the overlay string STRING of overlay OVERLAY to vector
5781 `entries' which has size `size' and currently contains `n'
5782 elements. AFTER_P means STRING is an after-string of
5783 OVERLAY. */
5784 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5785 do \
5787 Lisp_Object priority; \
5789 if (n == size) \
5791 struct overlay_entry *old = entries; \
5792 SAFE_NALLOCA (entries, 2, size); \
5793 memcpy (entries, old, size * sizeof *entries); \
5794 size *= 2; \
5797 entries[n].string = (STRING); \
5798 entries[n].overlay = (OVERLAY); \
5799 priority = Foverlay_get ((OVERLAY), Qpriority); \
5800 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5801 entries[n].after_string_p = (AFTER_P); \
5802 ++n; \
5804 while (false)
5806 /* Process overlay before the overlay center. */
5807 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5809 XSETMISC (overlay, ov);
5810 eassert (OVERLAYP (overlay));
5811 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5812 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5814 if (end < charpos)
5815 break;
5817 /* Skip this overlay if it doesn't start or end at IT's current
5818 position. */
5819 if (end != charpos && start != charpos)
5820 continue;
5822 /* Skip this overlay if it doesn't apply to IT->w. */
5823 window = Foverlay_get (overlay, Qwindow);
5824 if (WINDOWP (window) && XWINDOW (window) != it->w)
5825 continue;
5827 /* If the text ``under'' the overlay is invisible, both before-
5828 and after-strings from this overlay are visible; start and
5829 end position are indistinguishable. */
5830 invisible = Foverlay_get (overlay, Qinvisible);
5831 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5833 /* If overlay has a non-empty before-string, record it. */
5834 if ((start == charpos || (end == charpos && invis != 0))
5835 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5836 && SCHARS (str))
5837 RECORD_OVERLAY_STRING (overlay, str, false);
5839 /* If overlay has a non-empty after-string, record it. */
5840 if ((end == charpos || (start == charpos && invis != 0))
5841 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5842 && SCHARS (str))
5843 RECORD_OVERLAY_STRING (overlay, str, true);
5846 /* Process overlays after the overlay center. */
5847 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5849 XSETMISC (overlay, ov);
5850 eassert (OVERLAYP (overlay));
5851 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5852 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5854 if (start > charpos)
5855 break;
5857 /* Skip this overlay if it doesn't start or end at IT's current
5858 position. */
5859 if (end != charpos && start != charpos)
5860 continue;
5862 /* Skip this overlay if it doesn't apply to IT->w. */
5863 window = Foverlay_get (overlay, Qwindow);
5864 if (WINDOWP (window) && XWINDOW (window) != it->w)
5865 continue;
5867 /* If the text ``under'' the overlay is invisible, it has a zero
5868 dimension, and both before- and after-strings apply. */
5869 invisible = Foverlay_get (overlay, Qinvisible);
5870 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5872 /* If overlay has a non-empty before-string, record it. */
5873 if ((start == charpos || (end == charpos && invis != 0))
5874 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5875 && SCHARS (str))
5876 RECORD_OVERLAY_STRING (overlay, str, false);
5878 /* If overlay has a non-empty after-string, record it. */
5879 if ((end == charpos || (start == charpos && invis != 0))
5880 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5881 && SCHARS (str))
5882 RECORD_OVERLAY_STRING (overlay, str, true);
5885 #undef RECORD_OVERLAY_STRING
5887 /* Sort entries. */
5888 if (n > 1)
5889 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5891 /* Record number of overlay strings, and where we computed it. */
5892 it->n_overlay_strings = n;
5893 it->overlay_strings_charpos = charpos;
5895 /* IT->current.overlay_string_index is the number of overlay strings
5896 that have already been consumed by IT. Copy some of the
5897 remaining overlay strings to IT->overlay_strings. */
5898 i = 0;
5899 j = it->current.overlay_string_index;
5900 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5902 it->overlay_strings[i] = entries[j].string;
5903 it->string_overlays[i++] = entries[j++].overlay;
5906 CHECK_IT (it);
5907 SAFE_FREE ();
5911 /* Get the first chunk of overlay strings at IT's current buffer
5912 position, or at CHARPOS if that is > 0. Value is true if at
5913 least one overlay string was found. */
5915 static bool
5916 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5918 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5919 process. This fills IT->overlay_strings with strings, and sets
5920 IT->n_overlay_strings to the total number of strings to process.
5921 IT->pos.overlay_string_index has to be set temporarily to zero
5922 because load_overlay_strings needs this; it must be set to -1
5923 when no overlay strings are found because a zero value would
5924 indicate a position in the first overlay string. */
5925 it->current.overlay_string_index = 0;
5926 load_overlay_strings (it, charpos);
5928 /* If we found overlay strings, set up IT to deliver display
5929 elements from the first one. Otherwise set up IT to deliver
5930 from current_buffer. */
5931 if (it->n_overlay_strings)
5933 /* Make sure we know settings in current_buffer, so that we can
5934 restore meaningful values when we're done with the overlay
5935 strings. */
5936 if (compute_stop_p)
5937 compute_stop_pos (it);
5938 eassert (it->face_id >= 0);
5940 /* Save IT's settings. They are restored after all overlay
5941 strings have been processed. */
5942 eassert (!compute_stop_p || it->sp == 0);
5944 /* When called from handle_stop, there might be an empty display
5945 string loaded. In that case, don't bother saving it. But
5946 don't use this optimization with the bidi iterator, since we
5947 need the corresponding pop_it call to resync the bidi
5948 iterator's position with IT's position, after we are done
5949 with the overlay strings. (The corresponding call to pop_it
5950 in case of an empty display string is in
5951 next_overlay_string.) */
5952 if (!(!it->bidi_p
5953 && STRINGP (it->string) && !SCHARS (it->string)))
5954 push_it (it, NULL);
5956 /* Set up IT to deliver display elements from the first overlay
5957 string. */
5958 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5959 it->string = it->overlay_strings[0];
5960 it->from_overlay = Qnil;
5961 it->stop_charpos = 0;
5962 eassert (STRINGP (it->string));
5963 it->end_charpos = SCHARS (it->string);
5964 it->prev_stop = 0;
5965 it->base_level_stop = 0;
5966 it->multibyte_p = STRING_MULTIBYTE (it->string);
5967 it->method = GET_FROM_STRING;
5968 it->from_disp_prop_p = 0;
5970 /* Force paragraph direction to be that of the parent
5971 buffer. */
5972 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5973 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5974 else
5975 it->paragraph_embedding = L2R;
5977 /* Set up the bidi iterator for this overlay string. */
5978 if (it->bidi_p)
5980 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5982 it->bidi_it.string.lstring = it->string;
5983 it->bidi_it.string.s = NULL;
5984 it->bidi_it.string.schars = SCHARS (it->string);
5985 it->bidi_it.string.bufpos = pos;
5986 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5987 it->bidi_it.string.unibyte = !it->multibyte_p;
5988 it->bidi_it.w = it->w;
5989 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5991 return true;
5994 it->current.overlay_string_index = -1;
5995 return false;
5998 static bool
5999 get_overlay_strings (struct it *it, ptrdiff_t charpos)
6001 it->string = Qnil;
6002 it->method = GET_FROM_BUFFER;
6004 get_overlay_strings_1 (it, charpos, true);
6006 CHECK_IT (it);
6008 /* Value is true if we found at least one overlay string. */
6009 return STRINGP (it->string);
6014 /***********************************************************************
6015 Saving and restoring state
6016 ***********************************************************************/
6018 /* Save current settings of IT on IT->stack. Called, for example,
6019 before setting up IT for an overlay string, to be able to restore
6020 IT's settings to what they were after the overlay string has been
6021 processed. If POSITION is non-NULL, it is the position to save on
6022 the stack instead of IT->position. */
6024 static void
6025 push_it (struct it *it, struct text_pos *position)
6027 struct iterator_stack_entry *p;
6029 eassert (it->sp < IT_STACK_SIZE);
6030 p = it->stack + it->sp;
6032 p->stop_charpos = it->stop_charpos;
6033 p->prev_stop = it->prev_stop;
6034 p->base_level_stop = it->base_level_stop;
6035 p->cmp_it = it->cmp_it;
6036 eassert (it->face_id >= 0);
6037 p->face_id = it->face_id;
6038 p->string = it->string;
6039 p->method = it->method;
6040 p->from_overlay = it->from_overlay;
6041 switch (p->method)
6043 case GET_FROM_IMAGE:
6044 p->u.image.object = it->object;
6045 p->u.image.image_id = it->image_id;
6046 p->u.image.slice = it->slice;
6047 break;
6048 case GET_FROM_STRETCH:
6049 p->u.stretch.object = it->object;
6050 break;
6051 case GET_FROM_XWIDGET:
6052 p->u.xwidget.object = it->object;
6053 break;
6054 case GET_FROM_BUFFER:
6055 case GET_FROM_DISPLAY_VECTOR:
6056 case GET_FROM_STRING:
6057 case GET_FROM_C_STRING:
6058 break;
6059 default:
6060 emacs_abort ();
6062 p->position = position ? *position : it->position;
6063 p->current = it->current;
6064 p->end_charpos = it->end_charpos;
6065 p->string_nchars = it->string_nchars;
6066 p->area = it->area;
6067 p->multibyte_p = it->multibyte_p;
6068 p->avoid_cursor_p = it->avoid_cursor_p;
6069 p->space_width = it->space_width;
6070 p->font_height = it->font_height;
6071 p->voffset = it->voffset;
6072 p->string_from_display_prop_p = it->string_from_display_prop_p;
6073 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6074 p->display_ellipsis_p = false;
6075 p->line_wrap = it->line_wrap;
6076 p->bidi_p = it->bidi_p;
6077 p->paragraph_embedding = it->paragraph_embedding;
6078 p->from_disp_prop_p = it->from_disp_prop_p;
6079 ++it->sp;
6081 /* Save the state of the bidi iterator as well. */
6082 if (it->bidi_p)
6083 bidi_push_it (&it->bidi_it);
6086 static void
6087 iterate_out_of_display_property (struct it *it)
6089 bool buffer_p = !STRINGP (it->string);
6090 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6091 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6093 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6095 /* Maybe initialize paragraph direction. If we are at the beginning
6096 of a new paragraph, next_element_from_buffer may not have a
6097 chance to do that. */
6098 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6099 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6100 /* prev_stop can be zero, so check against BEGV as well. */
6101 while (it->bidi_it.charpos >= bob
6102 && it->prev_stop <= it->bidi_it.charpos
6103 && it->bidi_it.charpos < CHARPOS (it->position)
6104 && it->bidi_it.charpos < eob)
6105 bidi_move_to_visually_next (&it->bidi_it);
6106 /* Record the stop_pos we just crossed, for when we cross it
6107 back, maybe. */
6108 if (it->bidi_it.charpos > CHARPOS (it->position))
6109 it->prev_stop = CHARPOS (it->position);
6110 /* If we ended up not where pop_it put us, resync IT's
6111 positional members with the bidi iterator. */
6112 if (it->bidi_it.charpos != CHARPOS (it->position))
6113 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6114 if (buffer_p)
6115 it->current.pos = it->position;
6116 else
6117 it->current.string_pos = it->position;
6120 /* Restore IT's settings from IT->stack. Called, for example, when no
6121 more overlay strings must be processed, and we return to delivering
6122 display elements from a buffer, or when the end of a string from a
6123 `display' property is reached and we return to delivering display
6124 elements from an overlay string, or from a buffer. */
6126 static void
6127 pop_it (struct it *it)
6129 struct iterator_stack_entry *p;
6130 bool from_display_prop = it->from_disp_prop_p;
6131 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6133 eassert (it->sp > 0);
6134 --it->sp;
6135 p = it->stack + it->sp;
6136 it->stop_charpos = p->stop_charpos;
6137 it->prev_stop = p->prev_stop;
6138 it->base_level_stop = p->base_level_stop;
6139 it->cmp_it = p->cmp_it;
6140 it->face_id = p->face_id;
6141 it->current = p->current;
6142 it->position = p->position;
6143 it->string = p->string;
6144 it->from_overlay = p->from_overlay;
6145 if (NILP (it->string))
6146 SET_TEXT_POS (it->current.string_pos, -1, -1);
6147 it->method = p->method;
6148 switch (it->method)
6150 case GET_FROM_IMAGE:
6151 it->image_id = p->u.image.image_id;
6152 it->object = p->u.image.object;
6153 it->slice = p->u.image.slice;
6154 break;
6155 case GET_FROM_XWIDGET:
6156 it->object = p->u.xwidget.object;
6157 break;
6158 case GET_FROM_STRETCH:
6159 it->object = p->u.stretch.object;
6160 break;
6161 case GET_FROM_BUFFER:
6162 it->object = it->w->contents;
6163 break;
6164 case GET_FROM_STRING:
6166 struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
6168 /* Restore the face_box_p flag, since it could have been
6169 overwritten by the face of the object that we just finished
6170 displaying. */
6171 if (face)
6172 it->face_box_p = face->box != FACE_NO_BOX;
6173 it->object = it->string;
6175 break;
6176 case GET_FROM_DISPLAY_VECTOR:
6177 if (it->s)
6178 it->method = GET_FROM_C_STRING;
6179 else if (STRINGP (it->string))
6180 it->method = GET_FROM_STRING;
6181 else
6183 it->method = GET_FROM_BUFFER;
6184 it->object = it->w->contents;
6186 break;
6187 case GET_FROM_C_STRING:
6188 break;
6189 default:
6190 emacs_abort ();
6192 it->end_charpos = p->end_charpos;
6193 it->string_nchars = p->string_nchars;
6194 it->area = p->area;
6195 it->multibyte_p = p->multibyte_p;
6196 it->avoid_cursor_p = p->avoid_cursor_p;
6197 it->space_width = p->space_width;
6198 it->font_height = p->font_height;
6199 it->voffset = p->voffset;
6200 it->string_from_display_prop_p = p->string_from_display_prop_p;
6201 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6202 it->line_wrap = p->line_wrap;
6203 it->bidi_p = p->bidi_p;
6204 it->paragraph_embedding = p->paragraph_embedding;
6205 it->from_disp_prop_p = p->from_disp_prop_p;
6206 if (it->bidi_p)
6208 bidi_pop_it (&it->bidi_it);
6209 /* Bidi-iterate until we get out of the portion of text, if any,
6210 covered by a `display' text property or by an overlay with
6211 `display' property. (We cannot just jump there, because the
6212 internal coherency of the bidi iterator state can not be
6213 preserved across such jumps.) We also must determine the
6214 paragraph base direction if the overlay we just processed is
6215 at the beginning of a new paragraph. */
6216 if (from_display_prop
6217 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6218 iterate_out_of_display_property (it);
6220 eassert ((BUFFERP (it->object)
6221 && IT_CHARPOS (*it) == it->bidi_it.charpos
6222 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6223 || (STRINGP (it->object)
6224 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6225 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6226 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6228 /* If we move the iterator over text covered by a display property
6229 to a new buffer position, any info about previously seen overlays
6230 is no longer valid. */
6231 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6232 it->ignore_overlay_strings_at_pos_p = false;
6237 /***********************************************************************
6238 Moving over lines
6239 ***********************************************************************/
6241 /* Set IT's current position to the previous line start. */
6243 static void
6244 back_to_previous_line_start (struct it *it)
6246 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6248 DEC_BOTH (cp, bp);
6249 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6253 /* Move IT to the next line start.
6255 Value is true if a newline was found. Set *SKIPPED_P to true if
6256 we skipped over part of the text (as opposed to moving the iterator
6257 continuously over the text). Otherwise, don't change the value
6258 of *SKIPPED_P.
6260 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6261 iterator on the newline, if it was found.
6263 Newlines may come from buffer text, overlay strings, or strings
6264 displayed via the `display' property. That's the reason we can't
6265 simply use find_newline_no_quit.
6267 Note that this function may not skip over invisible text that is so
6268 because of text properties and immediately follows a newline. If
6269 it would, function reseat_at_next_visible_line_start, when called
6270 from set_iterator_to_next, would effectively make invisible
6271 characters following a newline part of the wrong glyph row, which
6272 leads to wrong cursor motion. */
6274 static bool
6275 forward_to_next_line_start (struct it *it, bool *skipped_p,
6276 struct bidi_it *bidi_it_prev)
6278 ptrdiff_t old_selective;
6279 bool newline_found_p = false;
6280 int n;
6281 const int MAX_NEWLINE_DISTANCE = 500;
6283 /* If already on a newline, just consume it to avoid unintended
6284 skipping over invisible text below. */
6285 if (it->what == IT_CHARACTER
6286 && it->c == '\n'
6287 && CHARPOS (it->position) == IT_CHARPOS (*it))
6289 if (it->bidi_p && bidi_it_prev)
6290 *bidi_it_prev = it->bidi_it;
6291 set_iterator_to_next (it, false);
6292 it->c = 0;
6293 return true;
6296 /* Don't handle selective display in the following. It's (a)
6297 unnecessary because it's done by the caller, and (b) leads to an
6298 infinite recursion because next_element_from_ellipsis indirectly
6299 calls this function. */
6300 old_selective = it->selective;
6301 it->selective = 0;
6303 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6304 from buffer text. */
6305 for (n = 0;
6306 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6307 n += !STRINGP (it->string))
6309 if (!get_next_display_element (it))
6310 return false;
6311 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6312 if (newline_found_p && it->bidi_p && bidi_it_prev)
6313 *bidi_it_prev = it->bidi_it;
6314 set_iterator_to_next (it, false);
6317 /* If we didn't find a newline near enough, see if we can use a
6318 short-cut. */
6319 if (!newline_found_p)
6321 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6322 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6323 1, &bytepos);
6324 Lisp_Object pos;
6326 eassert (!STRINGP (it->string));
6328 /* If there isn't any `display' property in sight, and no
6329 overlays, we can just use the position of the newline in
6330 buffer text. */
6331 if (it->stop_charpos >= limit
6332 || ((pos = Fnext_single_property_change (make_number (start),
6333 Qdisplay, Qnil,
6334 make_number (limit)),
6335 NILP (pos))
6336 && next_overlay_change (start) == ZV))
6338 if (!it->bidi_p)
6340 IT_CHARPOS (*it) = limit;
6341 IT_BYTEPOS (*it) = bytepos;
6343 else
6345 struct bidi_it bprev;
6347 /* Help bidi.c avoid expensive searches for display
6348 properties and overlays, by telling it that there are
6349 none up to `limit'. */
6350 if (it->bidi_it.disp_pos < limit)
6352 it->bidi_it.disp_pos = limit;
6353 it->bidi_it.disp_prop = 0;
6355 do {
6356 bprev = it->bidi_it;
6357 bidi_move_to_visually_next (&it->bidi_it);
6358 } while (it->bidi_it.charpos != limit);
6359 IT_CHARPOS (*it) = limit;
6360 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6361 if (bidi_it_prev)
6362 *bidi_it_prev = bprev;
6364 *skipped_p = newline_found_p = true;
6366 else
6368 while (!newline_found_p)
6370 if (!get_next_display_element (it))
6371 break;
6372 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6373 if (newline_found_p && it->bidi_p && bidi_it_prev)
6374 *bidi_it_prev = it->bidi_it;
6375 set_iterator_to_next (it, false);
6380 it->selective = old_selective;
6381 return newline_found_p;
6385 /* Set IT's current position to the previous visible line start. Skip
6386 invisible text that is so either due to text properties or due to
6387 selective display. Caution: this does not change IT->current_x and
6388 IT->hpos. */
6390 static void
6391 back_to_previous_visible_line_start (struct it *it)
6393 while (IT_CHARPOS (*it) > BEGV)
6395 back_to_previous_line_start (it);
6397 if (IT_CHARPOS (*it) <= BEGV)
6398 break;
6400 /* If selective > 0, then lines indented more than its value are
6401 invisible. */
6402 if (it->selective > 0
6403 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6404 it->selective))
6405 continue;
6407 /* Check the newline before point for invisibility. */
6409 Lisp_Object prop;
6410 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6411 Qinvisible, it->window);
6412 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6413 continue;
6416 if (IT_CHARPOS (*it) <= BEGV)
6417 break;
6420 struct it it2;
6421 void *it2data = NULL;
6422 ptrdiff_t pos;
6423 ptrdiff_t beg, end;
6424 Lisp_Object val, overlay;
6426 SAVE_IT (it2, *it, it2data);
6428 /* If newline is part of a composition, continue from start of composition */
6429 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6430 && beg < IT_CHARPOS (*it))
6431 goto replaced;
6433 /* If newline is replaced by a display property, find start of overlay
6434 or interval and continue search from that point. */
6435 pos = --IT_CHARPOS (it2);
6436 --IT_BYTEPOS (it2);
6437 it2.sp = 0;
6438 bidi_unshelve_cache (NULL, false);
6439 it2.string_from_display_prop_p = false;
6440 it2.from_disp_prop_p = false;
6441 if (handle_display_prop (&it2) == HANDLED_RETURN
6442 && !NILP (val = get_char_property_and_overlay
6443 (make_number (pos), Qdisplay, Qnil, &overlay))
6444 && (OVERLAYP (overlay)
6445 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6446 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6448 RESTORE_IT (it, it, it2data);
6449 goto replaced;
6452 /* Newline is not replaced by anything -- so we are done. */
6453 RESTORE_IT (it, it, it2data);
6454 break;
6456 replaced:
6457 if (beg < BEGV)
6458 beg = BEGV;
6459 IT_CHARPOS (*it) = beg;
6460 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6464 it->continuation_lines_width = 0;
6466 eassert (IT_CHARPOS (*it) >= BEGV);
6467 eassert (IT_CHARPOS (*it) == BEGV
6468 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6469 CHECK_IT (it);
6473 /* Reseat iterator IT at the previous visible line start. Skip
6474 invisible text that is so either due to text properties or due to
6475 selective display. At the end, update IT's overlay information,
6476 face information etc. */
6478 void
6479 reseat_at_previous_visible_line_start (struct it *it)
6481 back_to_previous_visible_line_start (it);
6482 reseat (it, it->current.pos, true);
6483 CHECK_IT (it);
6487 /* Reseat iterator IT on the next visible line start in the current
6488 buffer. ON_NEWLINE_P means position IT on the newline
6489 preceding the line start. Skip over invisible text that is so
6490 because of selective display. Compute faces, overlays etc at the
6491 new position. Note that this function does not skip over text that
6492 is invisible because of text properties. */
6494 static void
6495 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6497 bool skipped_p = false;
6498 struct bidi_it bidi_it_prev;
6499 bool newline_found_p
6500 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6502 /* Skip over lines that are invisible because they are indented
6503 more than the value of IT->selective. */
6504 if (it->selective > 0)
6505 while (IT_CHARPOS (*it) < ZV
6506 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6507 it->selective))
6509 eassert (IT_BYTEPOS (*it) == BEGV
6510 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6511 newline_found_p =
6512 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6515 /* Position on the newline if that's what's requested. */
6516 if (on_newline_p && newline_found_p)
6518 if (STRINGP (it->string))
6520 if (IT_STRING_CHARPOS (*it) > 0)
6522 if (!it->bidi_p)
6524 --IT_STRING_CHARPOS (*it);
6525 --IT_STRING_BYTEPOS (*it);
6527 else
6529 /* We need to restore the bidi iterator to the state
6530 it had on the newline, and resync the IT's
6531 position with that. */
6532 it->bidi_it = bidi_it_prev;
6533 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6534 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6538 else if (IT_CHARPOS (*it) > BEGV)
6540 if (!it->bidi_p)
6542 --IT_CHARPOS (*it);
6543 --IT_BYTEPOS (*it);
6545 else
6547 /* We need to restore the bidi iterator to the state it
6548 had on the newline and resync IT with that. */
6549 it->bidi_it = bidi_it_prev;
6550 IT_CHARPOS (*it) = it->bidi_it.charpos;
6551 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6553 reseat (it, it->current.pos, false);
6556 else if (skipped_p)
6557 reseat (it, it->current.pos, false);
6559 CHECK_IT (it);
6564 /***********************************************************************
6565 Changing an iterator's position
6566 ***********************************************************************/
6568 /* Change IT's current position to POS in current_buffer.
6569 If FORCE_P, always check for text properties at the new position.
6570 Otherwise, text properties are only looked up if POS >=
6571 IT->check_charpos of a property. */
6573 static void
6574 reseat (struct it *it, struct text_pos pos, bool force_p)
6576 ptrdiff_t original_pos = IT_CHARPOS (*it);
6578 reseat_1 (it, pos, false);
6580 /* Determine where to check text properties. Avoid doing it
6581 where possible because text property lookup is very expensive. */
6582 if (force_p
6583 || CHARPOS (pos) > it->stop_charpos
6584 || CHARPOS (pos) < original_pos)
6586 if (it->bidi_p)
6588 /* For bidi iteration, we need to prime prev_stop and
6589 base_level_stop with our best estimations. */
6590 /* Implementation note: Of course, POS is not necessarily a
6591 stop position, so assigning prev_pos to it is a lie; we
6592 should have called compute_stop_backwards. However, if
6593 the current buffer does not include any R2L characters,
6594 that call would be a waste of cycles, because the
6595 iterator will never move back, and thus never cross this
6596 "fake" stop position. So we delay that backward search
6597 until the time we really need it, in next_element_from_buffer. */
6598 if (CHARPOS (pos) != it->prev_stop)
6599 it->prev_stop = CHARPOS (pos);
6600 if (CHARPOS (pos) < it->base_level_stop)
6601 it->base_level_stop = 0; /* meaning it's unknown */
6602 handle_stop (it);
6604 else
6606 handle_stop (it);
6607 it->prev_stop = it->base_level_stop = 0;
6612 CHECK_IT (it);
6616 /* Change IT's buffer position to POS. SET_STOP_P means set
6617 IT->stop_pos to POS, also. */
6619 static void
6620 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6622 /* Don't call this function when scanning a C string. */
6623 eassert (it->s == NULL);
6625 /* POS must be a reasonable value. */
6626 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6628 it->current.pos = it->position = pos;
6629 it->end_charpos = ZV;
6630 it->dpvec = NULL;
6631 it->current.dpvec_index = -1;
6632 it->current.overlay_string_index = -1;
6633 IT_STRING_CHARPOS (*it) = -1;
6634 IT_STRING_BYTEPOS (*it) = -1;
6635 it->string = Qnil;
6636 it->method = GET_FROM_BUFFER;
6637 it->object = it->w->contents;
6638 it->area = TEXT_AREA;
6639 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6640 it->sp = 0;
6641 it->string_from_display_prop_p = false;
6642 it->string_from_prefix_prop_p = false;
6644 it->from_disp_prop_p = false;
6645 it->face_before_selective_p = false;
6646 if (it->bidi_p)
6648 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6649 &it->bidi_it);
6650 bidi_unshelve_cache (NULL, false);
6651 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6652 it->bidi_it.string.s = NULL;
6653 it->bidi_it.string.lstring = Qnil;
6654 it->bidi_it.string.bufpos = 0;
6655 it->bidi_it.string.from_disp_str = false;
6656 it->bidi_it.string.unibyte = false;
6657 it->bidi_it.w = it->w;
6660 if (set_stop_p)
6662 it->stop_charpos = CHARPOS (pos);
6663 it->base_level_stop = CHARPOS (pos);
6665 /* This make the information stored in it->cmp_it invalidate. */
6666 it->cmp_it.id = -1;
6670 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6671 If S is non-null, it is a C string to iterate over. Otherwise,
6672 STRING gives a Lisp string to iterate over.
6674 If PRECISION > 0, don't return more then PRECISION number of
6675 characters from the string.
6677 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6678 characters have been returned. FIELD_WIDTH < 0 means an infinite
6679 field width.
6681 MULTIBYTE = 0 means disable processing of multibyte characters,
6682 MULTIBYTE > 0 means enable it,
6683 MULTIBYTE < 0 means use IT->multibyte_p.
6685 IT must be initialized via a prior call to init_iterator before
6686 calling this function. */
6688 static void
6689 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6690 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6691 int multibyte)
6693 /* No text property checks performed by default, but see below. */
6694 it->stop_charpos = -1;
6696 /* Set iterator position and end position. */
6697 memset (&it->current, 0, sizeof it->current);
6698 it->current.overlay_string_index = -1;
6699 it->current.dpvec_index = -1;
6700 eassert (charpos >= 0);
6702 /* If STRING is specified, use its multibyteness, otherwise use the
6703 setting of MULTIBYTE, if specified. */
6704 if (multibyte >= 0)
6705 it->multibyte_p = multibyte > 0;
6707 /* Bidirectional reordering of strings is controlled by the default
6708 value of bidi-display-reordering. Don't try to reorder while
6709 loading loadup.el, as the necessary character property tables are
6710 not yet available. */
6711 it->bidi_p =
6712 !redisplay__inhibit_bidi
6713 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6715 if (s == NULL)
6717 eassert (STRINGP (string));
6718 it->string = string;
6719 it->s = NULL;
6720 it->end_charpos = it->string_nchars = SCHARS (string);
6721 it->method = GET_FROM_STRING;
6722 it->current.string_pos = string_pos (charpos, string);
6724 if (it->bidi_p)
6726 it->bidi_it.string.lstring = string;
6727 it->bidi_it.string.s = NULL;
6728 it->bidi_it.string.schars = it->end_charpos;
6729 it->bidi_it.string.bufpos = 0;
6730 it->bidi_it.string.from_disp_str = false;
6731 it->bidi_it.string.unibyte = !it->multibyte_p;
6732 it->bidi_it.w = it->w;
6733 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6734 FRAME_WINDOW_P (it->f), &it->bidi_it);
6737 else
6739 it->s = (const unsigned char *) s;
6740 it->string = Qnil;
6742 /* Note that we use IT->current.pos, not it->current.string_pos,
6743 for displaying C strings. */
6744 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6745 if (it->multibyte_p)
6747 it->current.pos = c_string_pos (charpos, s, true);
6748 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6750 else
6752 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6753 it->end_charpos = it->string_nchars = strlen (s);
6756 if (it->bidi_p)
6758 it->bidi_it.string.lstring = Qnil;
6759 it->bidi_it.string.s = (const unsigned char *) s;
6760 it->bidi_it.string.schars = it->end_charpos;
6761 it->bidi_it.string.bufpos = 0;
6762 it->bidi_it.string.from_disp_str = false;
6763 it->bidi_it.string.unibyte = !it->multibyte_p;
6764 it->bidi_it.w = it->w;
6765 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6766 &it->bidi_it);
6768 it->method = GET_FROM_C_STRING;
6771 /* PRECISION > 0 means don't return more than PRECISION characters
6772 from the string. */
6773 if (precision > 0 && it->end_charpos - charpos > precision)
6775 it->end_charpos = it->string_nchars = charpos + precision;
6776 if (it->bidi_p)
6777 it->bidi_it.string.schars = it->end_charpos;
6780 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6781 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6782 FIELD_WIDTH < 0 means infinite field width. This is useful for
6783 padding with `-' at the end of a mode line. */
6784 if (field_width < 0)
6785 field_width = DISP_INFINITY;
6786 /* Implementation note: We deliberately don't enlarge
6787 it->bidi_it.string.schars here to fit it->end_charpos, because
6788 the bidi iterator cannot produce characters out of thin air. */
6789 if (field_width > it->end_charpos - charpos)
6790 it->end_charpos = charpos + field_width;
6792 /* Use the standard display table for displaying strings. */
6793 if (DISP_TABLE_P (Vstandard_display_table))
6794 it->dp = XCHAR_TABLE (Vstandard_display_table);
6796 it->stop_charpos = charpos;
6797 it->prev_stop = charpos;
6798 it->base_level_stop = 0;
6799 if (it->bidi_p)
6801 it->bidi_it.first_elt = true;
6802 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6803 it->bidi_it.disp_pos = -1;
6805 if (s == NULL && it->multibyte_p)
6807 ptrdiff_t endpos = SCHARS (it->string);
6808 if (endpos > it->end_charpos)
6809 endpos = it->end_charpos;
6810 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6811 it->string);
6813 CHECK_IT (it);
6818 /***********************************************************************
6819 Iteration
6820 ***********************************************************************/
6822 /* Map enum it_method value to corresponding next_element_from_* function. */
6824 typedef bool (*next_element_function) (struct it *);
6826 static next_element_function const get_next_element[NUM_IT_METHODS] =
6828 next_element_from_buffer,
6829 next_element_from_display_vector,
6830 next_element_from_string,
6831 next_element_from_c_string,
6832 next_element_from_image,
6833 next_element_from_stretch,
6834 next_element_from_xwidget,
6837 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6840 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6841 (possibly with the following characters). */
6843 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6844 ((IT)->cmp_it.id >= 0 \
6845 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6846 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6847 END_CHARPOS, (IT)->w, \
6848 FACE_FROM_ID_OR_NULL ((IT)->f, \
6849 (IT)->face_id), \
6850 (IT)->string)))
6853 /* Lookup the char-table Vglyphless_char_display for character C (-1
6854 if we want information for no-font case), and return the display
6855 method symbol. By side-effect, update it->what and
6856 it->glyphless_method. This function is called from
6857 get_next_display_element for each character element, and from
6858 x_produce_glyphs when no suitable font was found. */
6860 Lisp_Object
6861 lookup_glyphless_char_display (int c, struct it *it)
6863 Lisp_Object glyphless_method = Qnil;
6865 if (CHAR_TABLE_P (Vglyphless_char_display)
6866 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6868 if (c >= 0)
6870 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6871 if (CONSP (glyphless_method))
6872 glyphless_method = FRAME_WINDOW_P (it->f)
6873 ? XCAR (glyphless_method)
6874 : XCDR (glyphless_method);
6876 else
6877 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6880 retry:
6881 if (NILP (glyphless_method))
6883 if (c >= 0)
6884 /* The default is to display the character by a proper font. */
6885 return Qnil;
6886 /* The default for the no-font case is to display an empty box. */
6887 glyphless_method = Qempty_box;
6889 if (EQ (glyphless_method, Qzero_width))
6891 if (c >= 0)
6892 return glyphless_method;
6893 /* This method can't be used for the no-font case. */
6894 glyphless_method = Qempty_box;
6896 if (EQ (glyphless_method, Qthin_space))
6897 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6898 else if (EQ (glyphless_method, Qempty_box))
6899 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6900 else if (EQ (glyphless_method, Qhex_code))
6901 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6902 else if (STRINGP (glyphless_method))
6903 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6904 else
6906 /* Invalid value. We use the default method. */
6907 glyphless_method = Qnil;
6908 goto retry;
6910 it->what = IT_GLYPHLESS;
6911 return glyphless_method;
6914 /* Merge escape glyph face and cache the result. */
6916 static struct frame *last_escape_glyph_frame = NULL;
6917 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6918 static int last_escape_glyph_merged_face_id = 0;
6920 static int
6921 merge_escape_glyph_face (struct it *it)
6923 int face_id;
6925 if (it->f == last_escape_glyph_frame
6926 && it->face_id == last_escape_glyph_face_id)
6927 face_id = last_escape_glyph_merged_face_id;
6928 else
6930 /* Merge the `escape-glyph' face into the current face. */
6931 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6932 last_escape_glyph_frame = it->f;
6933 last_escape_glyph_face_id = it->face_id;
6934 last_escape_glyph_merged_face_id = face_id;
6936 return face_id;
6939 /* Likewise for glyphless glyph face. */
6941 static struct frame *last_glyphless_glyph_frame = NULL;
6942 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6943 static int last_glyphless_glyph_merged_face_id = 0;
6946 merge_glyphless_glyph_face (struct it *it)
6948 int face_id;
6950 if (it->f == last_glyphless_glyph_frame
6951 && it->face_id == last_glyphless_glyph_face_id)
6952 face_id = last_glyphless_glyph_merged_face_id;
6953 else
6955 /* Merge the `glyphless-char' face into the current face. */
6956 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6957 last_glyphless_glyph_frame = it->f;
6958 last_glyphless_glyph_face_id = it->face_id;
6959 last_glyphless_glyph_merged_face_id = face_id;
6961 return face_id;
6964 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6965 be called before redisplaying windows, and when the frame's face
6966 cache is freed. */
6967 void
6968 forget_escape_and_glyphless_faces (void)
6970 last_escape_glyph_frame = NULL;
6971 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6972 last_glyphless_glyph_frame = NULL;
6973 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6976 /* Load IT's display element fields with information about the next
6977 display element from the current position of IT. Value is false if
6978 end of buffer (or C string) is reached. */
6980 static bool
6981 get_next_display_element (struct it *it)
6983 /* True means that we found a display element. False means that
6984 we hit the end of what we iterate over. Performance note: the
6985 function pointer `method' used here turns out to be faster than
6986 using a sequence of if-statements. */
6987 bool success_p;
6989 get_next:
6990 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6992 if (it->what == IT_CHARACTER)
6994 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6995 and only if (a) the resolved directionality of that character
6996 is R..." */
6997 /* FIXME: Do we need an exception for characters from display
6998 tables? */
6999 if (it->bidi_p && it->bidi_it.type == STRONG_R
7000 && !inhibit_bidi_mirroring)
7001 it->c = bidi_mirror_char (it->c);
7002 /* Map via display table or translate control characters.
7003 IT->c, IT->len etc. have been set to the next character by
7004 the function call above. If we have a display table, and it
7005 contains an entry for IT->c, translate it. Don't do this if
7006 IT->c itself comes from a display table, otherwise we could
7007 end up in an infinite recursion. (An alternative could be to
7008 count the recursion depth of this function and signal an
7009 error when a certain maximum depth is reached.) Is it worth
7010 it? */
7011 if (success_p && it->dpvec == NULL)
7013 Lisp_Object dv;
7014 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
7015 bool nonascii_space_p = false;
7016 bool nonascii_hyphen_p = false;
7017 int c = it->c; /* This is the character to display. */
7019 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
7021 eassert (SINGLE_BYTE_CHAR_P (c));
7022 if (unibyte_display_via_language_environment)
7024 c = DECODE_CHAR (unibyte, c);
7025 if (c < 0)
7026 c = BYTE8_TO_CHAR (it->c);
7028 else
7029 c = BYTE8_TO_CHAR (it->c);
7032 if (it->dp
7033 && (dv = DISP_CHAR_VECTOR (it->dp, c),
7034 VECTORP (dv)))
7036 struct Lisp_Vector *v = XVECTOR (dv);
7038 /* Return the first character from the display table
7039 entry, if not empty. If empty, don't display the
7040 current character. */
7041 if (v->header.size)
7043 it->dpvec_char_len = it->len;
7044 it->dpvec = v->contents;
7045 it->dpend = v->contents + v->header.size;
7046 it->current.dpvec_index = 0;
7047 it->dpvec_face_id = -1;
7048 it->saved_face_id = it->face_id;
7049 it->method = GET_FROM_DISPLAY_VECTOR;
7050 it->ellipsis_p = false;
7052 else
7054 set_iterator_to_next (it, false);
7056 goto get_next;
7059 if (! NILP (lookup_glyphless_char_display (c, it)))
7061 if (it->what == IT_GLYPHLESS)
7062 goto done;
7063 /* Don't display this character. */
7064 set_iterator_to_next (it, false);
7065 goto get_next;
7068 /* If `nobreak-char-display' is non-nil, we display
7069 non-ASCII spaces and hyphens specially. */
7070 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7072 if (c == NO_BREAK_SPACE)
7073 nonascii_space_p = true;
7074 else if (c == SOFT_HYPHEN || c == HYPHEN
7075 || c == NON_BREAKING_HYPHEN)
7076 nonascii_hyphen_p = true;
7079 /* Translate control characters into `\003' or `^C' form.
7080 Control characters coming from a display table entry are
7081 currently not translated because we use IT->dpvec to hold
7082 the translation. This could easily be changed but I
7083 don't believe that it is worth doing.
7085 The characters handled by `nobreak-char-display' must be
7086 translated too.
7088 Non-printable characters and raw-byte characters are also
7089 translated to octal or hexadecimal form. */
7090 if (((c < ' ' || c == 127) /* ASCII control chars. */
7091 ? (it->area != TEXT_AREA
7092 /* In mode line, treat \n, \t like other crl chars. */
7093 || (c != '\t'
7094 && it->glyph_row
7095 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7096 || (c != '\n' && c != '\t'))
7097 : (nonascii_space_p
7098 || nonascii_hyphen_p
7099 || CHAR_BYTE8_P (c)
7100 || ! CHAR_PRINTABLE_P (c))))
7102 /* C is a control character, non-ASCII space/hyphen,
7103 raw-byte, or a non-printable character which must be
7104 displayed either as '\003' or as `^C' where the '\\'
7105 and '^' can be defined in the display table. Fill
7106 IT->ctl_chars with glyphs for what we have to
7107 display. Then, set IT->dpvec to these glyphs. */
7108 Lisp_Object gc;
7109 int ctl_len;
7110 int face_id;
7111 int lface_id = 0;
7112 int escape_glyph;
7114 /* Handle control characters with ^. */
7116 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7118 int g;
7120 g = '^'; /* default glyph for Control */
7121 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7122 if (it->dp
7123 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7125 g = GLYPH_CODE_CHAR (gc);
7126 lface_id = GLYPH_CODE_FACE (gc);
7129 face_id = (lface_id
7130 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7131 : merge_escape_glyph_face (it));
7133 XSETINT (it->ctl_chars[0], g);
7134 XSETINT (it->ctl_chars[1], c ^ 0100);
7135 ctl_len = 2;
7136 goto display_control;
7139 /* Handle non-ascii space in the mode where it only gets
7140 highlighting. */
7142 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7144 /* Merge `nobreak-space' into the current face. */
7145 face_id = merge_faces (it->f, Qnobreak_space, 0,
7146 it->face_id);
7147 XSETINT (it->ctl_chars[0], ' ');
7148 ctl_len = 1;
7149 goto display_control;
7152 /* Handle non-ascii hyphens in the mode where it only
7153 gets highlighting. */
7155 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7157 /* Merge `nobreak-space' into the current face. */
7158 face_id = merge_faces (it->f, Qnobreak_hyphen, 0,
7159 it->face_id);
7160 XSETINT (it->ctl_chars[0], '-');
7161 ctl_len = 1;
7162 goto display_control;
7165 /* Handle sequences that start with the "escape glyph". */
7167 /* the default escape glyph is \. */
7168 escape_glyph = '\\';
7170 if (it->dp
7171 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7173 escape_glyph = GLYPH_CODE_CHAR (gc);
7174 lface_id = GLYPH_CODE_FACE (gc);
7177 face_id = (lface_id
7178 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7179 : merge_escape_glyph_face (it));
7181 /* Draw non-ASCII space/hyphen with escape glyph: */
7183 if (nonascii_space_p || nonascii_hyphen_p)
7185 XSETINT (it->ctl_chars[0], escape_glyph);
7186 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7187 ctl_len = 2;
7188 goto display_control;
7192 char str[10];
7193 int len, i;
7195 if (CHAR_BYTE8_P (c))
7196 /* Display \200 or \x80 instead of \17777600. */
7197 c = CHAR_TO_BYTE8 (c);
7198 const char *format_string = display_raw_bytes_as_hex
7199 ? "x%02x"
7200 : "%03o";
7201 len = sprintf (str, format_string, c + 0u);
7203 XSETINT (it->ctl_chars[0], escape_glyph);
7204 for (i = 0; i < len; i++)
7205 XSETINT (it->ctl_chars[i + 1], str[i]);
7206 ctl_len = len + 1;
7209 display_control:
7210 /* Set up IT->dpvec and return first character from it. */
7211 it->dpvec_char_len = it->len;
7212 it->dpvec = it->ctl_chars;
7213 it->dpend = it->dpvec + ctl_len;
7214 it->current.dpvec_index = 0;
7215 it->dpvec_face_id = face_id;
7216 it->saved_face_id = it->face_id;
7217 it->method = GET_FROM_DISPLAY_VECTOR;
7218 it->ellipsis_p = false;
7219 goto get_next;
7221 it->char_to_display = c;
7223 else if (success_p)
7225 it->char_to_display = it->c;
7229 #ifdef HAVE_WINDOW_SYSTEM
7230 /* Adjust face id for a multibyte character. There are no multibyte
7231 character in unibyte text. */
7232 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7233 && it->multibyte_p
7234 && success_p
7235 && FRAME_WINDOW_P (it->f))
7237 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7239 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7241 /* Automatic composition with glyph-string. */
7242 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7244 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7246 else
7248 ptrdiff_t pos = (it->s ? -1
7249 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7250 : IT_CHARPOS (*it));
7251 int c;
7253 if (it->what == IT_CHARACTER)
7254 c = it->char_to_display;
7255 else
7257 struct composition *cmp = composition_table[it->cmp_it.id];
7258 int i;
7260 c = ' ';
7261 for (i = 0; i < cmp->glyph_len; i++)
7262 /* TAB in a composition means display glyphs with
7263 padding space on the left or right. */
7264 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7265 break;
7267 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7270 #endif /* HAVE_WINDOW_SYSTEM */
7272 done:
7273 /* Is this character the last one of a run of characters with
7274 box? If yes, set IT->end_of_box_run_p to true. */
7275 if (it->face_box_p
7276 && it->s == NULL)
7278 if (it->method == GET_FROM_STRING && it->sp)
7280 int face_id = underlying_face_id (it);
7281 struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
7283 if (face)
7285 if (face->box == FACE_NO_BOX)
7287 /* If the box comes from face properties in a
7288 display string, check faces in that string. */
7289 int string_face_id = face_after_it_pos (it);
7290 it->end_of_box_run_p
7291 = (FACE_FROM_ID (it->f, string_face_id)->box
7292 == FACE_NO_BOX);
7294 /* Otherwise, the box comes from the underlying face.
7295 If this is the last string character displayed, check
7296 the next buffer location. */
7297 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7298 /* n_overlay_strings is unreliable unless
7299 overlay_string_index is non-negative. */
7300 && ((it->current.overlay_string_index >= 0
7301 && (it->current.overlay_string_index
7302 == it->n_overlay_strings - 1))
7303 /* A string from display property. */
7304 || it->from_disp_prop_p))
7306 ptrdiff_t ignore;
7307 int next_face_id;
7308 bool text_from_string = false;
7309 /* Normally, the next buffer location is stored in
7310 IT->current.pos... */
7311 struct text_pos pos = it->current.pos;
7313 /* ...but for a string from a display property, the
7314 next buffer position is stored in the 'position'
7315 member of the iteration stack slot below the
7316 current one, see handle_single_display_spec. By
7317 contrast, it->current.pos was not yet updated to
7318 point to that buffer position; that will happen
7319 in pop_it, after we finish displaying the current
7320 string. Note that we already checked above that
7321 it->sp is positive, so subtracting one from it is
7322 safe. */
7323 if (it->from_disp_prop_p)
7325 int stackp = it->sp - 1;
7327 /* Find the stack level with data from buffer. */
7328 while (stackp >= 0
7329 && STRINGP ((it->stack + stackp)->string))
7330 stackp--;
7331 if (stackp < 0)
7333 /* If no stack slot was found for iterating
7334 a buffer, we are displaying text from a
7335 string, most probably the mode line or
7336 the header line, and that string has a
7337 display string on some of its
7338 characters. */
7339 text_from_string = true;
7340 pos = it->stack[it->sp - 1].position;
7342 else
7343 pos = (it->stack + stackp)->position;
7345 else
7346 INC_TEXT_POS (pos, it->multibyte_p);
7348 if (text_from_string)
7350 Lisp_Object base_string = it->stack[it->sp - 1].string;
7352 if (CHARPOS (pos) >= SCHARS (base_string) - 1)
7353 it->end_of_box_run_p = true;
7354 else
7356 next_face_id
7357 = face_at_string_position (it->w, base_string,
7358 CHARPOS (pos), 0,
7359 &ignore, face_id, false);
7360 it->end_of_box_run_p
7361 = (FACE_FROM_ID (it->f, next_face_id)->box
7362 == FACE_NO_BOX);
7365 else if (CHARPOS (pos) >= ZV)
7366 it->end_of_box_run_p = true;
7367 else
7369 next_face_id =
7370 face_at_buffer_position (it->w, CHARPOS (pos), &ignore,
7371 CHARPOS (pos)
7372 + TEXT_PROP_DISTANCE_LIMIT,
7373 false, -1);
7374 it->end_of_box_run_p
7375 = (FACE_FROM_ID (it->f, next_face_id)->box
7376 == FACE_NO_BOX);
7381 /* next_element_from_display_vector sets this flag according to
7382 faces of the display vector glyphs, see there. */
7383 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7385 int face_id = face_after_it_pos (it);
7386 it->end_of_box_run_p
7387 = (face_id != it->face_id
7388 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7391 /* If we reached the end of the object we've been iterating (e.g., a
7392 display string or an overlay string), and there's something on
7393 IT->stack, proceed with what's on the stack. It doesn't make
7394 sense to return false if there's unprocessed stuff on the stack,
7395 because otherwise that stuff will never be displayed. */
7396 if (!success_p && it->sp > 0)
7398 set_iterator_to_next (it, false);
7399 success_p = get_next_display_element (it);
7402 /* Value is false if end of buffer or string reached. */
7403 return success_p;
7407 /* Move IT to the next display element.
7409 RESEAT_P means if called on a newline in buffer text,
7410 skip to the next visible line start.
7412 Functions get_next_display_element and set_iterator_to_next are
7413 separate because I find this arrangement easier to handle than a
7414 get_next_display_element function that also increments IT's
7415 position. The way it is we can first look at an iterator's current
7416 display element, decide whether it fits on a line, and if it does,
7417 increment the iterator position. The other way around we probably
7418 would either need a flag indicating whether the iterator has to be
7419 incremented the next time, or we would have to implement a
7420 decrement position function which would not be easy to write. */
7422 void
7423 set_iterator_to_next (struct it *it, bool reseat_p)
7425 /* Reset flags indicating start and end of a sequence of characters
7426 with box. Reset them at the start of this function because
7427 moving the iterator to a new position might set them. */
7428 it->start_of_box_run_p = it->end_of_box_run_p = false;
7430 switch (it->method)
7432 case GET_FROM_BUFFER:
7433 /* The current display element of IT is a character from
7434 current_buffer. Advance in the buffer, and maybe skip over
7435 invisible lines that are so because of selective display. */
7436 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7437 reseat_at_next_visible_line_start (it, false);
7438 else if (it->cmp_it.id >= 0)
7440 /* We are currently getting glyphs from a composition. */
7441 if (! it->bidi_p)
7443 IT_CHARPOS (*it) += it->cmp_it.nchars;
7444 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7446 else
7448 int i;
7450 /* Update IT's char/byte positions to point to the first
7451 character of the next grapheme cluster, or to the
7452 character visually after the current composition. */
7453 for (i = 0; i < it->cmp_it.nchars; i++)
7454 bidi_move_to_visually_next (&it->bidi_it);
7455 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7456 IT_CHARPOS (*it) = it->bidi_it.charpos;
7459 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7460 && it->cmp_it.to < it->cmp_it.nglyphs)
7462 /* Composition created while scanning forward. Proceed
7463 to the next grapheme cluster. */
7464 it->cmp_it.from = it->cmp_it.to;
7466 else if ((it->bidi_p && it->cmp_it.reversed_p)
7467 && it->cmp_it.from > 0)
7469 /* Composition created while scanning backward. Proceed
7470 to the previous grapheme cluster. */
7471 it->cmp_it.to = it->cmp_it.from;
7473 else
7475 /* No more grapheme clusters in this composition.
7476 Find the next stop position. */
7477 ptrdiff_t stop = it->end_charpos;
7479 if (it->bidi_it.scan_dir < 0)
7480 /* Now we are scanning backward and don't know
7481 where to stop. */
7482 stop = -1;
7483 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7484 IT_BYTEPOS (*it), stop, Qnil);
7487 else
7489 eassert (it->len != 0);
7491 if (!it->bidi_p)
7493 IT_BYTEPOS (*it) += it->len;
7494 IT_CHARPOS (*it) += 1;
7496 else
7498 int prev_scan_dir = it->bidi_it.scan_dir;
7499 /* If this is a new paragraph, determine its base
7500 direction (a.k.a. its base embedding level). */
7501 if (it->bidi_it.new_paragraph)
7502 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7503 false);
7504 bidi_move_to_visually_next (&it->bidi_it);
7505 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7506 IT_CHARPOS (*it) = it->bidi_it.charpos;
7507 if (prev_scan_dir != it->bidi_it.scan_dir)
7509 /* As the scan direction was changed, we must
7510 re-compute the stop position for composition. */
7511 ptrdiff_t stop = it->end_charpos;
7512 if (it->bidi_it.scan_dir < 0)
7513 stop = -1;
7514 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7515 IT_BYTEPOS (*it), stop, Qnil);
7518 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7520 break;
7522 case GET_FROM_C_STRING:
7523 /* Current display element of IT is from a C string. */
7524 if (!it->bidi_p
7525 /* If the string position is beyond string's end, it means
7526 next_element_from_c_string is padding the string with
7527 blanks, in which case we bypass the bidi iterator,
7528 because it cannot deal with such virtual characters. */
7529 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7531 IT_BYTEPOS (*it) += it->len;
7532 IT_CHARPOS (*it) += 1;
7534 else
7536 bidi_move_to_visually_next (&it->bidi_it);
7537 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7538 IT_CHARPOS (*it) = it->bidi_it.charpos;
7540 break;
7542 case GET_FROM_DISPLAY_VECTOR:
7543 /* Current display element of IT is from a display table entry.
7544 Advance in the display table definition. Reset it to null if
7545 end reached, and continue with characters from buffers/
7546 strings. */
7547 ++it->current.dpvec_index;
7549 /* Restore face of the iterator to what they were before the
7550 display vector entry (these entries may contain faces). */
7551 it->face_id = it->saved_face_id;
7553 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7555 bool recheck_faces = it->ellipsis_p;
7557 if (it->s)
7558 it->method = GET_FROM_C_STRING;
7559 else if (STRINGP (it->string))
7560 it->method = GET_FROM_STRING;
7561 else
7563 it->method = GET_FROM_BUFFER;
7564 it->object = it->w->contents;
7567 it->dpvec = NULL;
7568 it->current.dpvec_index = -1;
7570 /* Skip over characters which were displayed via IT->dpvec. */
7571 if (it->dpvec_char_len < 0)
7572 reseat_at_next_visible_line_start (it, true);
7573 else if (it->dpvec_char_len > 0)
7575 it->len = it->dpvec_char_len;
7576 set_iterator_to_next (it, reseat_p);
7579 /* Maybe recheck faces after display vector. */
7580 if (recheck_faces)
7582 if (it->method == GET_FROM_STRING)
7583 it->stop_charpos = IT_STRING_CHARPOS (*it);
7584 else
7585 it->stop_charpos = IT_CHARPOS (*it);
7588 break;
7590 case GET_FROM_STRING:
7591 /* Current display element is a character from a Lisp string. */
7592 eassert (it->s == NULL && STRINGP (it->string));
7593 /* Don't advance past string end. These conditions are true
7594 when set_iterator_to_next is called at the end of
7595 get_next_display_element, in which case the Lisp string is
7596 already exhausted, and all we want is pop the iterator
7597 stack. */
7598 if (it->current.overlay_string_index >= 0)
7600 /* This is an overlay string, so there's no padding with
7601 spaces, and the number of characters in the string is
7602 where the string ends. */
7603 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7604 goto consider_string_end;
7606 else
7608 /* Not an overlay string. There could be padding, so test
7609 against it->end_charpos. */
7610 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7611 goto consider_string_end;
7613 if (it->cmp_it.id >= 0)
7615 /* We are delivering display elements from a composition.
7616 Update the string position past the grapheme cluster
7617 we've just processed. */
7618 if (! it->bidi_p)
7620 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7621 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7623 else
7625 int i;
7627 for (i = 0; i < it->cmp_it.nchars; i++)
7628 bidi_move_to_visually_next (&it->bidi_it);
7629 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7630 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7633 /* Did we exhaust all the grapheme clusters of this
7634 composition? */
7635 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7636 && (it->cmp_it.to < it->cmp_it.nglyphs))
7638 /* Not all the grapheme clusters were processed yet;
7639 advance to the next cluster. */
7640 it->cmp_it.from = it->cmp_it.to;
7642 else if ((it->bidi_p && it->cmp_it.reversed_p)
7643 && it->cmp_it.from > 0)
7645 /* Likewise: advance to the next cluster, but going in
7646 the reverse direction. */
7647 it->cmp_it.to = it->cmp_it.from;
7649 else
7651 /* This composition was fully processed; find the next
7652 candidate place for checking for composed
7653 characters. */
7654 /* Always limit string searches to the string length;
7655 any padding spaces are not part of the string, and
7656 there cannot be any compositions in that padding. */
7657 ptrdiff_t stop = SCHARS (it->string);
7659 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7660 stop = -1;
7661 else if (it->end_charpos < stop)
7663 /* Cf. PRECISION in reseat_to_string: we might be
7664 limited in how many of the string characters we
7665 need to deliver. */
7666 stop = it->end_charpos;
7668 composition_compute_stop_pos (&it->cmp_it,
7669 IT_STRING_CHARPOS (*it),
7670 IT_STRING_BYTEPOS (*it), stop,
7671 it->string);
7674 else
7676 if (!it->bidi_p
7677 /* If the string position is beyond string's end, it
7678 means next_element_from_string is padding the string
7679 with blanks, in which case we bypass the bidi
7680 iterator, because it cannot deal with such virtual
7681 characters. */
7682 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7684 IT_STRING_BYTEPOS (*it) += it->len;
7685 IT_STRING_CHARPOS (*it) += 1;
7687 else
7689 int prev_scan_dir = it->bidi_it.scan_dir;
7691 bidi_move_to_visually_next (&it->bidi_it);
7692 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7693 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7694 /* If the scan direction changes, we may need to update
7695 the place where to check for composed characters. */
7696 if (prev_scan_dir != it->bidi_it.scan_dir)
7698 ptrdiff_t stop = SCHARS (it->string);
7700 if (it->bidi_it.scan_dir < 0)
7701 stop = -1;
7702 else if (it->end_charpos < stop)
7703 stop = it->end_charpos;
7705 composition_compute_stop_pos (&it->cmp_it,
7706 IT_STRING_CHARPOS (*it),
7707 IT_STRING_BYTEPOS (*it), stop,
7708 it->string);
7713 consider_string_end:
7715 if (it->current.overlay_string_index >= 0)
7717 /* IT->string is an overlay string. Advance to the
7718 next, if there is one. */
7719 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7721 it->ellipsis_p = false;
7722 next_overlay_string (it);
7723 if (it->ellipsis_p)
7724 setup_for_ellipsis (it, 0);
7727 else
7729 /* IT->string is not an overlay string. If we reached
7730 its end, and there is something on IT->stack, proceed
7731 with what is on the stack. This can be either another
7732 string, this time an overlay string, or a buffer. */
7733 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7734 && it->sp > 0)
7736 pop_it (it);
7737 if (it->method == GET_FROM_STRING)
7738 goto consider_string_end;
7741 break;
7743 case GET_FROM_IMAGE:
7744 case GET_FROM_STRETCH:
7745 case GET_FROM_XWIDGET:
7747 /* The position etc with which we have to proceed are on
7748 the stack. The position may be at the end of a string,
7749 if the `display' property takes up the whole string. */
7750 eassert (it->sp > 0);
7751 pop_it (it);
7752 if (it->method == GET_FROM_STRING)
7753 goto consider_string_end;
7754 break;
7756 default:
7757 /* There are no other methods defined, so this should be a bug. */
7758 emacs_abort ();
7761 eassert (it->method != GET_FROM_STRING
7762 || (STRINGP (it->string)
7763 && IT_STRING_CHARPOS (*it) >= 0));
7766 /* Load IT's display element fields with information about the next
7767 display element which comes from a display table entry or from the
7768 result of translating a control character to one of the forms `^C'
7769 or `\003'.
7771 IT->dpvec holds the glyphs to return as characters.
7772 IT->saved_face_id holds the face id before the display vector--it
7773 is restored into IT->face_id in set_iterator_to_next. */
7775 static bool
7776 next_element_from_display_vector (struct it *it)
7778 Lisp_Object gc;
7779 int prev_face_id = it->face_id;
7780 int next_face_id;
7782 /* Precondition. */
7783 eassert (it->dpvec && it->current.dpvec_index >= 0);
7785 it->face_id = it->saved_face_id;
7787 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7788 That seemed totally bogus - so I changed it... */
7789 if (it->dpend - it->dpvec > 0 /* empty dpvec[] is invalid */
7790 && (gc = it->dpvec[it->current.dpvec_index], GLYPH_CODE_P (gc)))
7792 struct face *this_face, *prev_face, *next_face;
7794 it->c = GLYPH_CODE_CHAR (gc);
7795 it->len = CHAR_BYTES (it->c);
7797 /* The entry may contain a face id to use. Such a face id is
7798 the id of a Lisp face, not a realized face. A face id of
7799 zero means no face is specified. */
7800 if (it->dpvec_face_id >= 0)
7801 it->face_id = it->dpvec_face_id;
7802 else
7804 int lface_id = GLYPH_CODE_FACE (gc);
7805 if (lface_id > 0)
7806 it->face_id = merge_faces (it->f, Qt, lface_id,
7807 it->saved_face_id);
7810 /* Glyphs in the display vector could have the box face, so we
7811 need to set the related flags in the iterator, as
7812 appropriate. */
7813 this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
7814 prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
7816 /* Is this character the first character of a box-face run? */
7817 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7818 && (!prev_face
7819 || prev_face->box == FACE_NO_BOX));
7821 /* For the last character of the box-face run, we need to look
7822 either at the next glyph from the display vector, or at the
7823 face we saw before the display vector. */
7824 next_face_id = it->saved_face_id;
7825 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7827 if (it->dpvec_face_id >= 0)
7828 next_face_id = it->dpvec_face_id;
7829 else
7831 int lface_id =
7832 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7834 if (lface_id > 0)
7835 next_face_id = merge_faces (it->f, Qt, lface_id,
7836 it->saved_face_id);
7839 next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
7840 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7841 && (!next_face
7842 || next_face->box == FACE_NO_BOX));
7843 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7845 else
7846 /* Display table entry is invalid. Return a space. */
7847 it->c = ' ', it->len = 1;
7849 /* Don't change position and object of the iterator here. They are
7850 still the values of the character that had this display table
7851 entry or was translated, and that's what we want. */
7852 it->what = IT_CHARACTER;
7853 return true;
7856 /* Get the first element of string/buffer in the visual order, after
7857 being reseated to a new position in a string or a buffer. */
7858 static void
7859 get_visually_first_element (struct it *it)
7861 bool string_p = STRINGP (it->string) || it->s;
7862 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7863 ptrdiff_t bob = (string_p ? 0 : BEGV);
7865 if (STRINGP (it->string))
7867 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7868 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7870 else
7872 it->bidi_it.charpos = IT_CHARPOS (*it);
7873 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7876 if (it->bidi_it.charpos == eob)
7878 /* Nothing to do, but reset the FIRST_ELT flag, like
7879 bidi_paragraph_init does, because we are not going to
7880 call it. */
7881 it->bidi_it.first_elt = false;
7883 else if (it->bidi_it.charpos == bob
7884 || (!string_p
7885 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7886 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7888 /* If we are at the beginning of a line/string, we can produce
7889 the next element right away. */
7890 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7891 bidi_move_to_visually_next (&it->bidi_it);
7893 else
7895 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7897 /* We need to prime the bidi iterator starting at the line's or
7898 string's beginning, before we will be able to produce the
7899 next element. */
7900 if (string_p)
7901 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7902 else
7903 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7904 IT_BYTEPOS (*it), -1,
7905 &it->bidi_it.bytepos);
7906 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7909 /* Now return to buffer/string position where we were asked
7910 to get the next display element, and produce that. */
7911 bidi_move_to_visually_next (&it->bidi_it);
7913 while (it->bidi_it.bytepos != orig_bytepos
7914 && it->bidi_it.charpos < eob);
7917 /* Adjust IT's position information to where we ended up. */
7918 if (STRINGP (it->string))
7920 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7921 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7923 else
7925 IT_CHARPOS (*it) = it->bidi_it.charpos;
7926 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7929 if (STRINGP (it->string) || !it->s)
7931 ptrdiff_t stop, charpos, bytepos;
7933 if (STRINGP (it->string))
7935 eassert (!it->s);
7936 stop = SCHARS (it->string);
7937 if (stop > it->end_charpos)
7938 stop = it->end_charpos;
7939 charpos = IT_STRING_CHARPOS (*it);
7940 bytepos = IT_STRING_BYTEPOS (*it);
7942 else
7944 stop = it->end_charpos;
7945 charpos = IT_CHARPOS (*it);
7946 bytepos = IT_BYTEPOS (*it);
7948 if (it->bidi_it.scan_dir < 0)
7949 stop = -1;
7950 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7951 it->string);
7955 /* Load IT with the next display element from Lisp string IT->string.
7956 IT->current.string_pos is the current position within the string.
7957 If IT->current.overlay_string_index >= 0, the Lisp string is an
7958 overlay string. */
7960 static bool
7961 next_element_from_string (struct it *it)
7963 struct text_pos position;
7965 eassert (STRINGP (it->string));
7966 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7967 eassert (IT_STRING_CHARPOS (*it) >= 0);
7968 position = it->current.string_pos;
7970 /* With bidi reordering, the character to display might not be the
7971 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7972 that we were reseat()ed to a new string, whose paragraph
7973 direction is not known. */
7974 if (it->bidi_p && it->bidi_it.first_elt)
7976 get_visually_first_element (it);
7977 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7980 /* Time to check for invisible text? */
7981 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7983 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7985 if (!(!it->bidi_p
7986 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7987 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7989 /* With bidi non-linear iteration, we could find
7990 ourselves far beyond the last computed stop_charpos,
7991 with several other stop positions in between that we
7992 missed. Scan them all now, in buffer's logical
7993 order, until we find and handle the last stop_charpos
7994 that precedes our current position. */
7995 handle_stop_backwards (it, it->stop_charpos);
7996 return GET_NEXT_DISPLAY_ELEMENT (it);
7998 else
8000 if (it->bidi_p)
8002 /* Take note of the stop position we just moved
8003 across, for when we will move back across it. */
8004 it->prev_stop = it->stop_charpos;
8005 /* If we are at base paragraph embedding level, take
8006 note of the last stop position seen at this
8007 level. */
8008 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8009 it->base_level_stop = it->stop_charpos;
8011 handle_stop (it);
8013 /* Since a handler may have changed IT->method, we must
8014 recurse here. */
8015 return GET_NEXT_DISPLAY_ELEMENT (it);
8018 else if (it->bidi_p
8019 /* If we are before prev_stop, we may have overstepped
8020 on our way backwards a stop_pos, and if so, we need
8021 to handle that stop_pos. */
8022 && IT_STRING_CHARPOS (*it) < it->prev_stop
8023 /* We can sometimes back up for reasons that have nothing
8024 to do with bidi reordering. E.g., compositions. The
8025 code below is only needed when we are above the base
8026 embedding level, so test for that explicitly. */
8027 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8029 /* If we lost track of base_level_stop, we have no better
8030 place for handle_stop_backwards to start from than string
8031 beginning. This happens, e.g., when we were reseated to
8032 the previous screenful of text by vertical-motion. */
8033 if (it->base_level_stop <= 0
8034 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
8035 it->base_level_stop = 0;
8036 handle_stop_backwards (it, it->base_level_stop);
8037 return GET_NEXT_DISPLAY_ELEMENT (it);
8041 if (it->current.overlay_string_index >= 0)
8043 /* Get the next character from an overlay string. In overlay
8044 strings, there is no field width or padding with spaces to
8045 do. */
8046 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
8048 it->what = IT_EOB;
8049 return false;
8051 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8052 IT_STRING_BYTEPOS (*it),
8053 it->bidi_it.scan_dir < 0
8054 ? -1
8055 : SCHARS (it->string))
8056 && next_element_from_composition (it))
8058 return true;
8060 else if (STRING_MULTIBYTE (it->string))
8062 const unsigned char *s = (SDATA (it->string)
8063 + IT_STRING_BYTEPOS (*it));
8064 it->c = string_char_and_length (s, &it->len);
8066 else
8068 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8069 it->len = 1;
8072 else
8074 /* Get the next character from a Lisp string that is not an
8075 overlay string. Such strings come from the mode line, for
8076 example. We may have to pad with spaces, or truncate the
8077 string. See also next_element_from_c_string. */
8078 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
8080 it->what = IT_EOB;
8081 return false;
8083 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
8085 /* Pad with spaces. */
8086 it->c = ' ', it->len = 1;
8087 CHARPOS (position) = BYTEPOS (position) = -1;
8089 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8090 IT_STRING_BYTEPOS (*it),
8091 it->bidi_it.scan_dir < 0
8092 ? -1
8093 : it->string_nchars)
8094 && next_element_from_composition (it))
8096 return true;
8098 else if (STRING_MULTIBYTE (it->string))
8100 const unsigned char *s = (SDATA (it->string)
8101 + IT_STRING_BYTEPOS (*it));
8102 it->c = string_char_and_length (s, &it->len);
8104 else
8106 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8107 it->len = 1;
8111 /* Record what we have and where it came from. */
8112 it->what = IT_CHARACTER;
8113 it->object = it->string;
8114 it->position = position;
8115 return true;
8119 /* Load IT with next display element from C string IT->s.
8120 IT->string_nchars is the maximum number of characters to return
8121 from the string. IT->end_charpos may be greater than
8122 IT->string_nchars when this function is called, in which case we
8123 may have to return padding spaces. Value is false if end of string
8124 reached, including padding spaces. */
8126 static bool
8127 next_element_from_c_string (struct it *it)
8129 bool success_p = true;
8131 eassert (it->s);
8132 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8133 it->what = IT_CHARACTER;
8134 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8135 it->object = make_number (0);
8137 /* With bidi reordering, the character to display might not be the
8138 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8139 we were reseated to a new string, whose paragraph direction is
8140 not known. */
8141 if (it->bidi_p && it->bidi_it.first_elt)
8142 get_visually_first_element (it);
8144 /* IT's position can be greater than IT->string_nchars in case a
8145 field width or precision has been specified when the iterator was
8146 initialized. */
8147 if (IT_CHARPOS (*it) >= it->end_charpos)
8149 /* End of the game. */
8150 it->what = IT_EOB;
8151 success_p = false;
8153 else if (IT_CHARPOS (*it) >= it->string_nchars)
8155 /* Pad with spaces. */
8156 it->c = ' ', it->len = 1;
8157 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8159 else if (it->multibyte_p)
8160 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8161 else
8162 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8164 return success_p;
8168 /* Set up IT to return characters from an ellipsis, if appropriate.
8169 The definition of the ellipsis glyphs may come from a display table
8170 entry. This function fills IT with the first glyph from the
8171 ellipsis if an ellipsis is to be displayed. */
8173 static bool
8174 next_element_from_ellipsis (struct it *it)
8176 if (it->selective_display_ellipsis_p)
8177 setup_for_ellipsis (it, it->len);
8178 else
8180 /* The face at the current position may be different from the
8181 face we find after the invisible text. Remember what it
8182 was in IT->saved_face_id, and signal that it's there by
8183 setting face_before_selective_p. */
8184 it->saved_face_id = it->face_id;
8185 it->method = GET_FROM_BUFFER;
8186 it->object = it->w->contents;
8187 reseat_at_next_visible_line_start (it, true);
8188 it->face_before_selective_p = true;
8191 return GET_NEXT_DISPLAY_ELEMENT (it);
8195 /* Deliver an image display element. The iterator IT is already
8196 filled with image information (done in handle_display_prop). Value
8197 is always true. */
8200 static bool
8201 next_element_from_image (struct it *it)
8203 it->what = IT_IMAGE;
8204 return true;
8207 static bool
8208 next_element_from_xwidget (struct it *it)
8210 it->what = IT_XWIDGET;
8211 return true;
8215 /* Fill iterator IT with next display element from a stretch glyph
8216 property. IT->object is the value of the text property. Value is
8217 always true. */
8219 static bool
8220 next_element_from_stretch (struct it *it)
8222 it->what = IT_STRETCH;
8223 return true;
8226 /* Scan backwards from IT's current position until we find a stop
8227 position, or until BEGV. This is called when we find ourself
8228 before both the last known prev_stop and base_level_stop while
8229 reordering bidirectional text. */
8231 static void
8232 compute_stop_pos_backwards (struct it *it)
8234 const int SCAN_BACK_LIMIT = 1000;
8235 struct text_pos pos;
8236 struct display_pos save_current = it->current;
8237 struct text_pos save_position = it->position;
8238 ptrdiff_t charpos = IT_CHARPOS (*it);
8239 ptrdiff_t where_we_are = charpos;
8240 ptrdiff_t save_stop_pos = it->stop_charpos;
8241 ptrdiff_t save_end_pos = it->end_charpos;
8243 eassert (NILP (it->string) && !it->s);
8244 eassert (it->bidi_p);
8245 it->bidi_p = false;
8248 it->end_charpos = min (charpos + 1, ZV);
8249 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8250 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8251 reseat_1 (it, pos, false);
8252 compute_stop_pos (it);
8253 /* We must advance forward, right? */
8254 if (it->stop_charpos <= charpos)
8255 emacs_abort ();
8257 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8259 if (it->stop_charpos <= where_we_are)
8260 it->prev_stop = it->stop_charpos;
8261 else
8262 it->prev_stop = BEGV;
8263 it->bidi_p = true;
8264 it->current = save_current;
8265 it->position = save_position;
8266 it->stop_charpos = save_stop_pos;
8267 it->end_charpos = save_end_pos;
8270 /* Scan forward from CHARPOS in the current buffer/string, until we
8271 find a stop position > current IT's position. Then handle the stop
8272 position before that. This is called when we bump into a stop
8273 position while reordering bidirectional text. CHARPOS should be
8274 the last previously processed stop_pos (or BEGV/0, if none were
8275 processed yet) whose position is less that IT's current
8276 position. */
8278 static void
8279 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8281 bool bufp = !STRINGP (it->string);
8282 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8283 struct display_pos save_current = it->current;
8284 struct text_pos save_position = it->position;
8285 struct text_pos pos1;
8286 ptrdiff_t next_stop;
8288 /* Scan in strict logical order. */
8289 eassert (it->bidi_p);
8290 it->bidi_p = false;
8293 it->prev_stop = charpos;
8294 if (bufp)
8296 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8297 reseat_1 (it, pos1, false);
8299 else
8300 it->current.string_pos = string_pos (charpos, it->string);
8301 compute_stop_pos (it);
8302 /* We must advance forward, right? */
8303 if (it->stop_charpos <= it->prev_stop)
8304 emacs_abort ();
8305 charpos = it->stop_charpos;
8307 while (charpos <= where_we_are);
8309 it->bidi_p = true;
8310 it->current = save_current;
8311 it->position = save_position;
8312 next_stop = it->stop_charpos;
8313 it->stop_charpos = it->prev_stop;
8314 handle_stop (it);
8315 it->stop_charpos = next_stop;
8318 /* Load IT with the next display element from current_buffer. Value
8319 is false if end of buffer reached. IT->stop_charpos is the next
8320 position at which to stop and check for text properties or buffer
8321 end. */
8323 static bool
8324 next_element_from_buffer (struct it *it)
8326 bool success_p = true;
8328 eassert (IT_CHARPOS (*it) >= BEGV);
8329 eassert (NILP (it->string) && !it->s);
8330 eassert (!it->bidi_p
8331 || (EQ (it->bidi_it.string.lstring, Qnil)
8332 && it->bidi_it.string.s == NULL));
8334 /* With bidi reordering, the character to display might not be the
8335 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8336 we were reseat()ed to a new buffer position, which is potentially
8337 a different paragraph. */
8338 if (it->bidi_p && it->bidi_it.first_elt)
8340 get_visually_first_element (it);
8341 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8344 if (IT_CHARPOS (*it) >= it->stop_charpos)
8346 if (IT_CHARPOS (*it) >= it->end_charpos)
8348 bool overlay_strings_follow_p;
8350 /* End of the game, except when overlay strings follow that
8351 haven't been returned yet. */
8352 if (it->overlay_strings_at_end_processed_p)
8353 overlay_strings_follow_p = false;
8354 else
8356 it->overlay_strings_at_end_processed_p = true;
8357 overlay_strings_follow_p = get_overlay_strings (it, 0);
8360 if (overlay_strings_follow_p)
8361 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8362 else
8364 it->what = IT_EOB;
8365 it->position = it->current.pos;
8366 success_p = false;
8369 else if (!(!it->bidi_p
8370 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8371 || IT_CHARPOS (*it) == it->stop_charpos))
8373 /* With bidi non-linear iteration, we could find ourselves
8374 far beyond the last computed stop_charpos, with several
8375 other stop positions in between that we missed. Scan
8376 them all now, in buffer's logical order, until we find
8377 and handle the last stop_charpos that precedes our
8378 current position. */
8379 handle_stop_backwards (it, it->stop_charpos);
8380 it->ignore_overlay_strings_at_pos_p = false;
8381 return GET_NEXT_DISPLAY_ELEMENT (it);
8383 else
8385 if (it->bidi_p)
8387 /* Take note of the stop position we just moved across,
8388 for when we will move back across it. */
8389 it->prev_stop = it->stop_charpos;
8390 /* If we are at base paragraph embedding level, take
8391 note of the last stop position seen at this
8392 level. */
8393 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8394 it->base_level_stop = it->stop_charpos;
8396 handle_stop (it);
8397 it->ignore_overlay_strings_at_pos_p = false;
8398 return GET_NEXT_DISPLAY_ELEMENT (it);
8401 else if (it->bidi_p
8402 /* If we are before prev_stop, we may have overstepped on
8403 our way backwards a stop_pos, and if so, we need to
8404 handle that stop_pos. */
8405 && IT_CHARPOS (*it) < it->prev_stop
8406 /* We can sometimes back up for reasons that have nothing
8407 to do with bidi reordering. E.g., compositions. The
8408 code below is only needed when we are above the base
8409 embedding level, so test for that explicitly. */
8410 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8412 if (it->base_level_stop <= 0
8413 || IT_CHARPOS (*it) < it->base_level_stop)
8415 /* If we lost track of base_level_stop, we need to find
8416 prev_stop by looking backwards. This happens, e.g., when
8417 we were reseated to the previous screenful of text by
8418 vertical-motion. */
8419 it->base_level_stop = BEGV;
8420 compute_stop_pos_backwards (it);
8421 handle_stop_backwards (it, it->prev_stop);
8423 else
8424 handle_stop_backwards (it, it->base_level_stop);
8425 it->ignore_overlay_strings_at_pos_p = false;
8426 return GET_NEXT_DISPLAY_ELEMENT (it);
8428 else
8430 /* No face changes, overlays etc. in sight, so just return a
8431 character from current_buffer. */
8432 unsigned char *p;
8433 ptrdiff_t stop;
8435 /* We moved to the next buffer position, so any info about
8436 previously seen overlays is no longer valid. */
8437 it->ignore_overlay_strings_at_pos_p = false;
8439 /* Maybe run the redisplay end trigger hook. Performance note:
8440 This doesn't seem to cost measurable time. */
8441 if (it->redisplay_end_trigger_charpos
8442 && it->glyph_row
8443 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8444 run_redisplay_end_trigger_hook (it);
8446 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8447 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8448 stop)
8449 && next_element_from_composition (it))
8451 return true;
8454 /* Get the next character, maybe multibyte. */
8455 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8456 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8457 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8458 else
8459 it->c = *p, it->len = 1;
8461 /* Record what we have and where it came from. */
8462 it->what = IT_CHARACTER;
8463 it->object = it->w->contents;
8464 it->position = it->current.pos;
8466 /* Normally we return the character found above, except when we
8467 really want to return an ellipsis for selective display. */
8468 if (it->selective)
8470 if (it->c == '\n')
8472 /* A value of selective > 0 means hide lines indented more
8473 than that number of columns. */
8474 if (it->selective > 0
8475 && IT_CHARPOS (*it) + 1 < ZV
8476 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8477 IT_BYTEPOS (*it) + 1,
8478 it->selective))
8480 success_p = next_element_from_ellipsis (it);
8481 it->dpvec_char_len = -1;
8484 else if (it->c == '\r' && it->selective == -1)
8486 /* A value of selective == -1 means that everything from the
8487 CR to the end of the line is invisible, with maybe an
8488 ellipsis displayed for it. */
8489 success_p = next_element_from_ellipsis (it);
8490 it->dpvec_char_len = -1;
8495 /* Value is false if end of buffer reached. */
8496 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8497 return success_p;
8501 /* Run the redisplay end trigger hook for IT. */
8503 static void
8504 run_redisplay_end_trigger_hook (struct it *it)
8506 /* IT->glyph_row should be non-null, i.e. we should be actually
8507 displaying something, or otherwise we should not run the hook. */
8508 eassert (it->glyph_row);
8510 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8511 it->redisplay_end_trigger_charpos = 0;
8513 /* Since we are *trying* to run these functions, don't try to run
8514 them again, even if they get an error. */
8515 wset_redisplay_end_trigger (it->w, Qnil);
8516 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8517 make_number (charpos));
8519 /* Notice if it changed the face of the character we are on. */
8520 handle_face_prop (it);
8524 /* Deliver a composition display element. Unlike the other
8525 next_element_from_XXX, this function is not registered in the array
8526 get_next_element[]. It is called from next_element_from_buffer and
8527 next_element_from_string when necessary. */
8529 static bool
8530 next_element_from_composition (struct it *it)
8532 it->what = IT_COMPOSITION;
8533 it->len = it->cmp_it.nbytes;
8534 if (STRINGP (it->string))
8536 if (it->c < 0)
8538 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8539 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8540 return false;
8542 it->position = it->current.string_pos;
8543 it->object = it->string;
8544 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8545 IT_STRING_BYTEPOS (*it), it->string);
8547 else
8549 if (it->c < 0)
8551 IT_CHARPOS (*it) += it->cmp_it.nchars;
8552 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8553 if (it->bidi_p)
8555 if (it->bidi_it.new_paragraph)
8556 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8557 false);
8558 /* Resync the bidi iterator with IT's new position.
8559 FIXME: this doesn't support bidirectional text. */
8560 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8561 bidi_move_to_visually_next (&it->bidi_it);
8563 return false;
8565 it->position = it->current.pos;
8566 it->object = it->w->contents;
8567 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8568 IT_BYTEPOS (*it), Qnil);
8570 return true;
8575 /***********************************************************************
8576 Moving an iterator without producing glyphs
8577 ***********************************************************************/
8579 /* Check if iterator is at a position corresponding to a valid buffer
8580 position after some move_it_ call. */
8582 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8583 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8586 /* Move iterator IT to a specified buffer or X position within one
8587 line on the display without producing glyphs.
8589 OP should be a bit mask including some or all of these bits:
8590 MOVE_TO_X: Stop upon reaching x-position TO_X.
8591 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8592 Regardless of OP's value, stop upon reaching the end of the display line.
8594 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8595 This means, in particular, that TO_X includes window's horizontal
8596 scroll amount.
8598 The return value has several possible values that
8599 say what condition caused the scan to stop:
8601 MOVE_POS_MATCH_OR_ZV
8602 - when TO_POS or ZV was reached.
8604 MOVE_X_REACHED
8605 -when TO_X was reached before TO_POS or ZV were reached.
8607 MOVE_LINE_CONTINUED
8608 - when we reached the end of the display area and the line must
8609 be continued.
8611 MOVE_LINE_TRUNCATED
8612 - when we reached the end of the display area and the line is
8613 truncated.
8615 MOVE_NEWLINE_OR_CR
8616 - when we stopped at a line end, i.e. a newline or a CR and selective
8617 display is on. */
8619 static enum move_it_result
8620 move_it_in_display_line_to (struct it *it,
8621 ptrdiff_t to_charpos, int to_x,
8622 enum move_operation_enum op)
8624 enum move_it_result result = MOVE_UNDEFINED;
8625 struct glyph_row *saved_glyph_row;
8626 struct it wrap_it, atpos_it, atx_it, ppos_it;
8627 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8628 void *ppos_data = NULL;
8629 bool may_wrap = false;
8630 enum it_method prev_method = it->method;
8631 ptrdiff_t closest_pos UNINIT;
8632 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8633 bool saw_smaller_pos = prev_pos < to_charpos;
8634 bool line_number_pending = false;
8636 /* Don't produce glyphs in produce_glyphs. */
8637 saved_glyph_row = it->glyph_row;
8638 it->glyph_row = NULL;
8640 /* Use wrap_it to save a copy of IT wherever a word wrap could
8641 occur. Use atpos_it to save a copy of IT at the desired buffer
8642 position, if found, so that we can scan ahead and check if the
8643 word later overshoots the window edge. Use atx_it similarly, for
8644 pixel positions. */
8645 wrap_it.sp = -1;
8646 atpos_it.sp = -1;
8647 atx_it.sp = -1;
8649 /* Use ppos_it under bidi reordering to save a copy of IT for the
8650 initial position. We restore that position in IT when we have
8651 scanned the entire display line without finding a match for
8652 TO_CHARPOS and all the character positions are greater than
8653 TO_CHARPOS. We then restart the scan from the initial position,
8654 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8655 the closest to TO_CHARPOS. */
8656 if (it->bidi_p)
8658 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8660 SAVE_IT (ppos_it, *it, ppos_data);
8661 closest_pos = IT_CHARPOS (*it);
8663 else
8664 closest_pos = ZV;
8667 #define BUFFER_POS_REACHED_P() \
8668 ((op & MOVE_TO_POS) != 0 \
8669 && BUFFERP (it->object) \
8670 && (IT_CHARPOS (*it) == to_charpos \
8671 || ((!it->bidi_p \
8672 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8673 && IT_CHARPOS (*it) > to_charpos) \
8674 || (it->what == IT_COMPOSITION \
8675 && ((IT_CHARPOS (*it) > to_charpos \
8676 && to_charpos >= it->cmp_it.charpos) \
8677 || (IT_CHARPOS (*it) < to_charpos \
8678 && to_charpos <= it->cmp_it.charpos)))) \
8679 && (it->method == GET_FROM_BUFFER \
8680 || (it->method == GET_FROM_DISPLAY_VECTOR \
8681 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8683 if (it->hpos == 0)
8685 /* If line numbers are being displayed, produce a line number. */
8686 if (should_produce_line_number (it))
8688 if (it->current_x == it->first_visible_x)
8689 maybe_produce_line_number (it);
8690 else
8691 line_number_pending = true;
8693 /* If there's a line-/wrap-prefix, handle it. */
8694 if (it->method == GET_FROM_BUFFER)
8695 handle_line_prefix (it);
8698 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8699 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8701 while (true)
8703 int x, i, ascent = 0, descent = 0;
8705 /* Utility macro to reset an iterator with x, ascent, and descent. */
8706 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8707 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8708 (IT)->max_descent = descent)
8710 /* Stop if we move beyond TO_CHARPOS (after an image or a
8711 display string or stretch glyph). */
8712 if ((op & MOVE_TO_POS) != 0
8713 && BUFFERP (it->object)
8714 && it->method == GET_FROM_BUFFER
8715 && (((!it->bidi_p
8716 /* When the iterator is at base embedding level, we
8717 are guaranteed that characters are delivered for
8718 display in strictly increasing order of their
8719 buffer positions. */
8720 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8721 && IT_CHARPOS (*it) > to_charpos)
8722 || (it->bidi_p
8723 && (prev_method == GET_FROM_IMAGE
8724 || prev_method == GET_FROM_STRETCH
8725 || prev_method == GET_FROM_STRING)
8726 /* Passed TO_CHARPOS from left to right. */
8727 && ((prev_pos < to_charpos
8728 && IT_CHARPOS (*it) > to_charpos)
8729 /* Passed TO_CHARPOS from right to left. */
8730 || (prev_pos > to_charpos
8731 && IT_CHARPOS (*it) < to_charpos)))))
8733 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8735 result = MOVE_POS_MATCH_OR_ZV;
8736 break;
8738 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8739 /* If wrap_it is valid, the current position might be in a
8740 word that is wrapped. So, save the iterator in
8741 atpos_it and continue to see if wrapping happens. */
8742 SAVE_IT (atpos_it, *it, atpos_data);
8745 /* Stop when ZV reached.
8746 We used to stop here when TO_CHARPOS reached as well, but that is
8747 too soon if this glyph does not fit on this line. So we handle it
8748 explicitly below. */
8749 if (!get_next_display_element (it))
8751 result = MOVE_POS_MATCH_OR_ZV;
8752 break;
8755 if (it->line_wrap == TRUNCATE)
8757 if (BUFFER_POS_REACHED_P ())
8759 result = MOVE_POS_MATCH_OR_ZV;
8760 break;
8763 else
8765 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8767 if (IT_DISPLAYING_WHITESPACE (it))
8768 may_wrap = true;
8769 else if (may_wrap)
8771 /* We have reached a glyph that follows one or more
8772 whitespace characters. If the position is
8773 already found, we are done. */
8774 if (atpos_it.sp >= 0)
8776 RESTORE_IT (it, &atpos_it, atpos_data);
8777 result = MOVE_POS_MATCH_OR_ZV;
8778 goto done;
8780 if (atx_it.sp >= 0)
8782 RESTORE_IT (it, &atx_it, atx_data);
8783 result = MOVE_X_REACHED;
8784 goto done;
8786 /* Otherwise, we can wrap here. */
8787 SAVE_IT (wrap_it, *it, wrap_data);
8788 may_wrap = false;
8793 /* Remember the line height for the current line, in case
8794 the next element doesn't fit on the line. */
8795 ascent = it->max_ascent;
8796 descent = it->max_descent;
8798 /* The call to produce_glyphs will get the metrics of the
8799 display element IT is loaded with. Record the x-position
8800 before this display element, in case it doesn't fit on the
8801 line. */
8802 x = it->current_x;
8804 PRODUCE_GLYPHS (it);
8806 if (it->area != TEXT_AREA)
8808 prev_method = it->method;
8809 if (it->method == GET_FROM_BUFFER)
8810 prev_pos = IT_CHARPOS (*it);
8811 set_iterator_to_next (it, true);
8812 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8813 SET_TEXT_POS (this_line_min_pos,
8814 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8815 if (it->bidi_p
8816 && (op & MOVE_TO_POS)
8817 && IT_CHARPOS (*it) > to_charpos
8818 && IT_CHARPOS (*it) < closest_pos)
8819 closest_pos = IT_CHARPOS (*it);
8820 continue;
8823 /* The number of glyphs we get back in IT->nglyphs will normally
8824 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8825 character on a terminal frame, or (iii) a line end. For the
8826 second case, IT->nglyphs - 1 padding glyphs will be present.
8827 (On X frames, there is only one glyph produced for a
8828 composite character.)
8830 The behavior implemented below means, for continuation lines,
8831 that as many spaces of a TAB as fit on the current line are
8832 displayed there. For terminal frames, as many glyphs of a
8833 multi-glyph character are displayed in the current line, too.
8834 This is what the old redisplay code did, and we keep it that
8835 way. Under X, the whole shape of a complex character must
8836 fit on the line or it will be completely displayed in the
8837 next line.
8839 Note that both for tabs and padding glyphs, all glyphs have
8840 the same width. */
8841 if (it->nglyphs)
8843 /* More than one glyph or glyph doesn't fit on line. All
8844 glyphs have the same width. */
8845 int single_glyph_width = it->pixel_width / it->nglyphs;
8846 int new_x;
8847 int x_before_this_char = x;
8848 int hpos_before_this_char = it->hpos;
8850 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8852 new_x = x + single_glyph_width;
8854 /* We want to leave anything reaching TO_X to the caller. */
8855 if ((op & MOVE_TO_X) && new_x > to_x)
8857 if (BUFFER_POS_REACHED_P ())
8859 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8860 goto buffer_pos_reached;
8861 if (atpos_it.sp < 0)
8863 SAVE_IT (atpos_it, *it, atpos_data);
8864 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8867 else
8869 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8871 it->current_x = x;
8872 result = MOVE_X_REACHED;
8873 break;
8875 if (atx_it.sp < 0)
8877 SAVE_IT (atx_it, *it, atx_data);
8878 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8883 if (/* Lines are continued. */
8884 it->line_wrap != TRUNCATE
8885 && (/* And glyph doesn't fit on the line. */
8886 new_x > it->last_visible_x
8887 /* Or it fits exactly and we're on a window
8888 system frame. */
8889 || (new_x == it->last_visible_x
8890 && FRAME_WINDOW_P (it->f)
8891 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8892 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8893 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8895 bool moved_forward = false;
8897 if (/* IT->hpos == 0 means the very first glyph
8898 doesn't fit on the line, e.g. a wide image. */
8899 it->hpos == 0
8900 || (new_x == it->last_visible_x
8901 && FRAME_WINDOW_P (it->f)))
8903 ++it->hpos;
8904 it->current_x = new_x;
8906 /* The character's last glyph just barely fits
8907 in this row. */
8908 if (i == it->nglyphs - 1)
8910 /* If this is the destination position,
8911 return a position *before* it in this row,
8912 now that we know it fits in this row. */
8913 if (BUFFER_POS_REACHED_P ())
8915 bool can_wrap = true;
8917 /* If we are at a whitespace character
8918 that barely fits on this screen line,
8919 but the next character is also
8920 whitespace, we cannot wrap here. */
8921 if (it->line_wrap == WORD_WRAP
8922 && wrap_it.sp >= 0
8923 && may_wrap
8924 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8926 struct it tem_it;
8927 void *tem_data = NULL;
8929 SAVE_IT (tem_it, *it, tem_data);
8930 set_iterator_to_next (it, true);
8931 if (get_next_display_element (it)
8932 && IT_DISPLAYING_WHITESPACE (it))
8933 can_wrap = false;
8934 RESTORE_IT (it, &tem_it, tem_data);
8936 if (it->line_wrap != WORD_WRAP
8937 || wrap_it.sp < 0
8938 /* If we've just found whitespace
8939 where we can wrap, effectively
8940 ignore the previous wrap point --
8941 it is no longer relevant, but we
8942 won't have an opportunity to
8943 update it, since we've reached
8944 the edge of this screen line. */
8945 || (may_wrap && can_wrap
8946 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8948 it->hpos = hpos_before_this_char;
8949 it->current_x = x_before_this_char;
8950 result = MOVE_POS_MATCH_OR_ZV;
8951 break;
8953 if (it->line_wrap == WORD_WRAP
8954 && atpos_it.sp < 0)
8956 SAVE_IT (atpos_it, *it, atpos_data);
8957 atpos_it.current_x = x_before_this_char;
8958 atpos_it.hpos = hpos_before_this_char;
8962 prev_method = it->method;
8963 if (it->method == GET_FROM_BUFFER)
8964 prev_pos = IT_CHARPOS (*it);
8965 set_iterator_to_next (it, true);
8966 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8967 SET_TEXT_POS (this_line_min_pos,
8968 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8969 /* On graphical terminals, newlines may
8970 "overflow" into the fringe if
8971 overflow-newline-into-fringe is non-nil.
8972 On text terminals, and on graphical
8973 terminals with no right margin, newlines
8974 may overflow into the last glyph on the
8975 display line.*/
8976 if (!FRAME_WINDOW_P (it->f)
8977 || ((it->bidi_p
8978 && it->bidi_it.paragraph_dir == R2L)
8979 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8980 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8981 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8983 if (!get_next_display_element (it))
8985 result = MOVE_POS_MATCH_OR_ZV;
8986 break;
8988 moved_forward = true;
8989 if (BUFFER_POS_REACHED_P ())
8991 if (ITERATOR_AT_END_OF_LINE_P (it))
8992 result = MOVE_POS_MATCH_OR_ZV;
8993 else
8994 result = MOVE_LINE_CONTINUED;
8995 break;
8997 if (ITERATOR_AT_END_OF_LINE_P (it)
8998 && (it->line_wrap != WORD_WRAP
8999 || wrap_it.sp < 0
9000 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
9002 result = MOVE_NEWLINE_OR_CR;
9003 break;
9008 else
9009 IT_RESET_X_ASCENT_DESCENT (it);
9011 /* If the screen line ends with whitespace, and we
9012 are under word-wrap, don't use wrap_it: it is no
9013 longer relevant, but we won't have an opportunity
9014 to update it, since we are done with this screen
9015 line. */
9016 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
9017 /* If the character after the one which set the
9018 may_wrap flag is also whitespace, we can't
9019 wrap here, since the screen line cannot be
9020 wrapped in the middle of whitespace.
9021 Therefore, wrap_it _is_ relevant in that
9022 case. */
9023 && !(moved_forward && IT_DISPLAYING_WHITESPACE (it)))
9025 /* If we've found TO_X, go back there, as we now
9026 know the last word fits on this screen line. */
9027 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
9028 && atx_it.sp >= 0)
9030 RESTORE_IT (it, &atx_it, atx_data);
9031 atpos_it.sp = -1;
9032 atx_it.sp = -1;
9033 result = MOVE_X_REACHED;
9034 break;
9037 else if (wrap_it.sp >= 0)
9039 RESTORE_IT (it, &wrap_it, wrap_data);
9040 atpos_it.sp = -1;
9041 atx_it.sp = -1;
9044 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
9045 IT_CHARPOS (*it)));
9046 result = MOVE_LINE_CONTINUED;
9047 break;
9050 if (BUFFER_POS_REACHED_P ())
9052 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
9053 goto buffer_pos_reached;
9054 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
9056 SAVE_IT (atpos_it, *it, atpos_data);
9057 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
9061 if (new_x > it->first_visible_x)
9063 /* If we have reached the visible portion of the
9064 screen line, produce the line number if needed. */
9065 if (line_number_pending)
9067 line_number_pending = false;
9068 it->current_x = it->first_visible_x;
9069 maybe_produce_line_number (it);
9070 it->current_x += new_x - it->first_visible_x;
9072 /* Glyph is visible. Increment number of glyphs that
9073 would be displayed. */
9074 ++it->hpos;
9078 if (result != MOVE_UNDEFINED)
9079 break;
9081 else if (BUFFER_POS_REACHED_P ())
9083 buffer_pos_reached:
9084 IT_RESET_X_ASCENT_DESCENT (it);
9085 result = MOVE_POS_MATCH_OR_ZV;
9086 break;
9088 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
9090 /* Stop when TO_X specified and reached. This check is
9091 necessary here because of lines consisting of a line end,
9092 only. The line end will not produce any glyphs and we
9093 would never get MOVE_X_REACHED. */
9094 eassert (it->nglyphs == 0);
9095 result = MOVE_X_REACHED;
9096 break;
9099 /* Is this a line end? If yes, we're done. */
9100 if (ITERATOR_AT_END_OF_LINE_P (it))
9102 /* If we are past TO_CHARPOS, but never saw any character
9103 positions smaller than TO_CHARPOS, return
9104 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
9105 did. */
9106 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
9108 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
9110 if (closest_pos < ZV)
9112 RESTORE_IT (it, &ppos_it, ppos_data);
9113 /* Don't recurse if closest_pos is equal to
9114 to_charpos, since we have just tried that. */
9115 if (closest_pos != to_charpos)
9116 move_it_in_display_line_to (it, closest_pos, -1,
9117 MOVE_TO_POS);
9118 result = MOVE_POS_MATCH_OR_ZV;
9120 else
9121 goto buffer_pos_reached;
9123 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
9124 && IT_CHARPOS (*it) > to_charpos)
9125 goto buffer_pos_reached;
9126 else
9127 result = MOVE_NEWLINE_OR_CR;
9129 else
9130 result = MOVE_NEWLINE_OR_CR;
9131 /* If we've processed the newline, make sure this flag is
9132 reset, as it must only be set when the newline itself is
9133 processed. */
9134 if (result == MOVE_NEWLINE_OR_CR)
9135 it->constrain_row_ascent_descent_p = false;
9136 break;
9139 prev_method = it->method;
9140 if (it->method == GET_FROM_BUFFER)
9141 prev_pos = IT_CHARPOS (*it);
9142 /* The current display element has been consumed. Advance
9143 to the next. */
9144 set_iterator_to_next (it, true);
9145 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9146 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
9147 if (IT_CHARPOS (*it) < to_charpos)
9148 saw_smaller_pos = true;
9149 if (it->bidi_p
9150 && (op & MOVE_TO_POS)
9151 && IT_CHARPOS (*it) >= to_charpos
9152 && IT_CHARPOS (*it) < closest_pos)
9153 closest_pos = IT_CHARPOS (*it);
9155 /* Stop if lines are truncated and IT's current x-position is
9156 past the right edge of the window now. */
9157 if (it->line_wrap == TRUNCATE
9158 && it->current_x >= it->last_visible_x)
9160 if (!FRAME_WINDOW_P (it->f)
9161 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9162 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9163 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9164 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9166 bool at_eob_p = false;
9168 if ((at_eob_p = !get_next_display_element (it))
9169 || BUFFER_POS_REACHED_P ()
9170 /* If we are past TO_CHARPOS, but never saw any
9171 character positions smaller than TO_CHARPOS,
9172 return MOVE_POS_MATCH_OR_ZV, like the
9173 unidirectional display did. */
9174 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9175 && !saw_smaller_pos
9176 && IT_CHARPOS (*it) > to_charpos))
9178 if (it->bidi_p
9179 && !BUFFER_POS_REACHED_P ()
9180 && !at_eob_p && closest_pos < ZV)
9182 RESTORE_IT (it, &ppos_it, ppos_data);
9183 if (closest_pos != to_charpos)
9184 move_it_in_display_line_to (it, closest_pos, -1,
9185 MOVE_TO_POS);
9187 result = MOVE_POS_MATCH_OR_ZV;
9188 break;
9190 if (ITERATOR_AT_END_OF_LINE_P (it))
9192 result = MOVE_NEWLINE_OR_CR;
9193 break;
9196 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9197 && !saw_smaller_pos
9198 && IT_CHARPOS (*it) > to_charpos)
9200 if (closest_pos < ZV)
9202 RESTORE_IT (it, &ppos_it, ppos_data);
9203 if (closest_pos != to_charpos)
9204 move_it_in_display_line_to (it, closest_pos, -1,
9205 MOVE_TO_POS);
9207 result = MOVE_POS_MATCH_OR_ZV;
9208 break;
9210 result = MOVE_LINE_TRUNCATED;
9211 break;
9213 #undef IT_RESET_X_ASCENT_DESCENT
9216 #undef BUFFER_POS_REACHED_P
9218 /* If we scanned beyond TO_POS, restore the saved iterator either to
9219 the wrap point (if found), or to atpos/atx location. We decide which
9220 data to use to restore the saved iterator state by their X coordinates,
9221 since buffer positions might increase non-monotonically with screen
9222 coordinates due to bidi reordering. */
9223 if (result == MOVE_LINE_CONTINUED
9224 && it->line_wrap == WORD_WRAP
9225 && wrap_it.sp >= 0
9226 && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
9227 || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
9228 RESTORE_IT (it, &wrap_it, wrap_data);
9229 else if (atpos_it.sp >= 0)
9230 RESTORE_IT (it, &atpos_it, atpos_data);
9231 else if (atx_it.sp >= 0)
9232 RESTORE_IT (it, &atx_it, atx_data);
9234 done:
9236 if (atpos_data)
9237 bidi_unshelve_cache (atpos_data, true);
9238 if (atx_data)
9239 bidi_unshelve_cache (atx_data, true);
9240 if (wrap_data)
9241 bidi_unshelve_cache (wrap_data, true);
9242 if (ppos_data)
9243 bidi_unshelve_cache (ppos_data, true);
9245 /* Restore the iterator settings altered at the beginning of this
9246 function. */
9247 it->glyph_row = saved_glyph_row;
9248 return result;
9251 /* For external use. */
9252 void
9253 move_it_in_display_line (struct it *it,
9254 ptrdiff_t to_charpos, int to_x,
9255 enum move_operation_enum op)
9257 if (it->line_wrap == WORD_WRAP
9258 && (op & MOVE_TO_X))
9260 struct it save_it;
9261 void *save_data = NULL;
9262 int skip;
9264 SAVE_IT (save_it, *it, save_data);
9265 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9266 /* When word-wrap is on, TO_X may lie past the end
9267 of a wrapped line. Then it->current is the
9268 character on the next line, so backtrack to the
9269 space before the wrap point. */
9270 if (skip == MOVE_LINE_CONTINUED)
9272 int prev_x = max (it->current_x - 1, 0);
9273 RESTORE_IT (it, &save_it, save_data);
9274 move_it_in_display_line_to
9275 (it, -1, prev_x, MOVE_TO_X);
9277 else
9278 bidi_unshelve_cache (save_data, true);
9280 else
9281 move_it_in_display_line_to (it, to_charpos, to_x, op);
9285 /* Move IT forward until it satisfies one or more of the criteria in
9286 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9288 OP is a bit-mask that specifies where to stop, and in particular,
9289 which of those four position arguments makes a difference. See the
9290 description of enum move_operation_enum.
9292 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9293 screen line, this function will set IT to the next position that is
9294 displayed to the right of TO_CHARPOS on the screen.
9296 Return the maximum pixel length of any line scanned but never more
9297 than it.last_visible_x. */
9300 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9302 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9303 int line_height, line_start_x = 0, reached = 0;
9304 int max_current_x = 0;
9305 void *backup_data = NULL;
9307 for (;;)
9309 if (op & MOVE_TO_VPOS)
9311 /* If no TO_CHARPOS and no TO_X specified, stop at the
9312 start of the line TO_VPOS. */
9313 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9315 if (it->vpos == to_vpos)
9317 reached = 1;
9318 break;
9320 else
9321 skip = move_it_in_display_line_to (it, -1, -1, 0);
9323 else
9325 /* TO_VPOS >= 0 means stop at TO_X in the line at
9326 TO_VPOS, or at TO_POS, whichever comes first. */
9327 if (it->vpos == to_vpos)
9329 reached = 2;
9330 break;
9333 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9335 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9337 reached = 3;
9338 break;
9340 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9342 /* We have reached TO_X but not in the line we want. */
9343 skip = move_it_in_display_line_to (it, to_charpos,
9344 -1, MOVE_TO_POS);
9345 if (skip == MOVE_POS_MATCH_OR_ZV)
9347 reached = 4;
9348 break;
9353 else if (op & MOVE_TO_Y)
9355 struct it it_backup;
9357 if (it->line_wrap == WORD_WRAP)
9358 SAVE_IT (it_backup, *it, backup_data);
9360 /* TO_Y specified means stop at TO_X in the line containing
9361 TO_Y---or at TO_CHARPOS if this is reached first. The
9362 problem is that we can't really tell whether the line
9363 contains TO_Y before we have completely scanned it, and
9364 this may skip past TO_X. What we do is to first scan to
9365 TO_X.
9367 If TO_X is not specified, use a TO_X of zero. The reason
9368 is to make the outcome of this function more predictable.
9369 If we didn't use TO_X == 0, we would stop at the end of
9370 the line which is probably not what a caller would expect
9371 to happen. */
9372 skip = move_it_in_display_line_to
9373 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9374 (MOVE_TO_X | (op & MOVE_TO_POS)));
9376 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9377 if (skip == MOVE_POS_MATCH_OR_ZV)
9378 reached = 5;
9379 else if (skip == MOVE_X_REACHED)
9381 /* If TO_X was reached, we want to know whether TO_Y is
9382 in the line. We know this is the case if the already
9383 scanned glyphs make the line tall enough. Otherwise,
9384 we must check by scanning the rest of the line. */
9385 line_height = it->max_ascent + it->max_descent;
9386 if (to_y >= it->current_y
9387 && to_y < it->current_y + line_height)
9389 reached = 6;
9390 break;
9392 SAVE_IT (it_backup, *it, backup_data);
9393 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9394 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9395 op & MOVE_TO_POS);
9396 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9397 line_height = it->max_ascent + it->max_descent;
9398 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9400 if (to_y >= it->current_y
9401 && to_y < it->current_y + line_height)
9403 /* If TO_Y is in this line and TO_X was reached
9404 above, we scanned too far. We have to restore
9405 IT's settings to the ones before skipping. But
9406 keep the more accurate values of max_ascent and
9407 max_descent we've found while skipping the rest
9408 of the line, for the sake of callers, such as
9409 pos_visible_p, that need to know the line
9410 height. */
9411 int max_ascent = it->max_ascent;
9412 int max_descent = it->max_descent;
9414 RESTORE_IT (it, &it_backup, backup_data);
9415 it->max_ascent = max_ascent;
9416 it->max_descent = max_descent;
9417 reached = 6;
9419 else
9421 skip = skip2;
9422 if (skip == MOVE_POS_MATCH_OR_ZV)
9423 reached = 7;
9426 else
9428 /* Check whether TO_Y is in this line. */
9429 line_height = it->max_ascent + it->max_descent;
9430 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9432 if (to_y >= it->current_y
9433 && to_y < it->current_y + line_height)
9435 if (to_y > it->current_y)
9436 max_current_x = max (it->current_x, max_current_x);
9438 /* When word-wrap is on, TO_X may lie past the end
9439 of a wrapped line. Then it->current is the
9440 character on the next line, so backtrack to the
9441 space before the wrap point. */
9442 if (skip == MOVE_LINE_CONTINUED
9443 && it->line_wrap == WORD_WRAP)
9445 int prev_x = max (it->current_x - 1, 0);
9446 RESTORE_IT (it, &it_backup, backup_data);
9447 skip = move_it_in_display_line_to
9448 (it, -1, prev_x, MOVE_TO_X);
9451 reached = 6;
9455 if (reached)
9457 max_current_x = max (it->current_x, max_current_x);
9458 break;
9461 else if (BUFFERP (it->object)
9462 && (it->method == GET_FROM_BUFFER
9463 || it->method == GET_FROM_STRETCH)
9464 && IT_CHARPOS (*it) >= to_charpos
9465 /* Under bidi iteration, a call to set_iterator_to_next
9466 can scan far beyond to_charpos if the initial
9467 portion of the next line needs to be reordered. In
9468 that case, give move_it_in_display_line_to another
9469 chance below. */
9470 && !(it->bidi_p
9471 && it->bidi_it.scan_dir == -1))
9472 skip = MOVE_POS_MATCH_OR_ZV;
9473 else
9474 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9476 switch (skip)
9478 case MOVE_POS_MATCH_OR_ZV:
9479 max_current_x = max (it->current_x, max_current_x);
9480 reached = 8;
9481 goto out;
9483 case MOVE_NEWLINE_OR_CR:
9484 max_current_x = max (it->current_x, max_current_x);
9485 set_iterator_to_next (it, true);
9486 it->continuation_lines_width = 0;
9487 break;
9489 case MOVE_LINE_TRUNCATED:
9490 max_current_x = it->last_visible_x;
9491 it->continuation_lines_width = 0;
9492 reseat_at_next_visible_line_start (it, false);
9493 if ((op & MOVE_TO_POS) != 0
9494 && IT_CHARPOS (*it) > to_charpos)
9496 reached = 9;
9497 goto out;
9499 break;
9501 case MOVE_LINE_CONTINUED:
9502 max_current_x = it->last_visible_x;
9503 /* For continued lines ending in a tab, some of the glyphs
9504 associated with the tab are displayed on the current
9505 line. Since it->current_x does not include these glyphs,
9506 we use it->last_visible_x instead. */
9507 if (it->c == '\t')
9509 it->continuation_lines_width += it->last_visible_x;
9510 /* When moving by vpos, ensure that the iterator really
9511 advances to the next line (bug#847, bug#969). Fixme:
9512 do we need to do this in other circumstances? */
9513 if (it->current_x != it->last_visible_x
9514 && (op & MOVE_TO_VPOS)
9515 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9517 line_start_x = it->current_x + it->pixel_width
9518 - it->last_visible_x;
9519 if (FRAME_WINDOW_P (it->f))
9521 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9522 struct font *face_font = face->font;
9524 /* When display_line produces a continued line
9525 that ends in a TAB, it skips a tab stop that
9526 is closer than the font's space character
9527 width (see x_produce_glyphs where it produces
9528 the stretch glyph which represents a TAB).
9529 We need to reproduce the same logic here. */
9530 eassert (face_font);
9531 if (face_font)
9533 if (line_start_x < face_font->space_width)
9534 line_start_x
9535 += it->tab_width * face_font->space_width;
9538 set_iterator_to_next (it, false);
9541 else
9542 it->continuation_lines_width += it->current_x;
9543 break;
9545 default:
9546 emacs_abort ();
9549 /* Reset/increment for the next run. */
9550 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9551 it->current_x = line_start_x;
9552 line_start_x = 0;
9553 it->hpos = 0;
9554 it->current_y += it->max_ascent + it->max_descent;
9555 ++it->vpos;
9556 last_height = it->max_ascent + it->max_descent;
9557 it->max_ascent = it->max_descent = 0;
9560 out:
9562 /* On text terminals, we may stop at the end of a line in the middle
9563 of a multi-character glyph. If the glyph itself is continued,
9564 i.e. it is actually displayed on the next line, don't treat this
9565 stopping point as valid; move to the next line instead (unless
9566 that brings us offscreen). */
9567 if (!FRAME_WINDOW_P (it->f)
9568 && op & MOVE_TO_POS
9569 && IT_CHARPOS (*it) == to_charpos
9570 && it->what == IT_CHARACTER
9571 && it->nglyphs > 1
9572 && it->line_wrap == WINDOW_WRAP
9573 && it->current_x == it->last_visible_x - 1
9574 && it->c != '\n'
9575 && it->c != '\t'
9576 && it->w->window_end_valid
9577 && it->vpos < it->w->window_end_vpos)
9579 it->continuation_lines_width += it->current_x;
9580 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9581 it->current_y += it->max_ascent + it->max_descent;
9582 ++it->vpos;
9583 last_height = it->max_ascent + it->max_descent;
9586 if (backup_data)
9587 bidi_unshelve_cache (backup_data, true);
9589 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9591 return max_current_x;
9595 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9597 If DY > 0, move IT backward at least that many pixels. DY = 0
9598 means move IT backward to the preceding line start or BEGV. This
9599 function may move over more than DY pixels if IT->current_y - DY
9600 ends up in the middle of a line; in this case IT->current_y will be
9601 set to the top of the line moved to. */
9603 void
9604 move_it_vertically_backward (struct it *it, int dy)
9606 int nlines, h;
9607 struct it it2, it3;
9608 void *it2data = NULL, *it3data = NULL;
9609 ptrdiff_t start_pos;
9610 int nchars_per_row
9611 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9612 ptrdiff_t pos_limit;
9614 move_further_back:
9615 eassert (dy >= 0);
9617 start_pos = IT_CHARPOS (*it);
9619 /* Estimate how many newlines we must move back. */
9620 nlines = max (1, dy / default_line_pixel_height (it->w));
9621 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9622 pos_limit = BEGV;
9623 else
9624 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9626 /* Set the iterator's position that many lines back. But don't go
9627 back more than NLINES full screen lines -- this wins a day with
9628 buffers which have very long lines. */
9629 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9630 back_to_previous_visible_line_start (it);
9632 /* Reseat the iterator here. When moving backward, we don't want
9633 reseat to skip forward over invisible text, set up the iterator
9634 to deliver from overlay strings at the new position etc. So,
9635 use reseat_1 here. */
9636 reseat_1 (it, it->current.pos, true);
9638 /* We are now surely at a line start. */
9639 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9640 reordering is in effect. */
9641 it->continuation_lines_width = 0;
9643 /* Move forward and see what y-distance we moved. First move to the
9644 start of the next line so that we get its height. We need this
9645 height to be able to tell whether we reached the specified
9646 y-distance. */
9647 SAVE_IT (it2, *it, it2data);
9648 it2.max_ascent = it2.max_descent = 0;
9651 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9652 MOVE_TO_POS | MOVE_TO_VPOS);
9654 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9655 /* If we are in a display string which starts at START_POS,
9656 and that display string includes a newline, and we are
9657 right after that newline (i.e. at the beginning of a
9658 display line), exit the loop, because otherwise we will
9659 infloop, since move_it_to will see that it is already at
9660 START_POS and will not move. */
9661 || (it2.method == GET_FROM_STRING
9662 && IT_CHARPOS (it2) == start_pos
9663 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9664 eassert (IT_CHARPOS (*it) >= BEGV);
9665 SAVE_IT (it3, it2, it3data);
9667 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9668 eassert (IT_CHARPOS (*it) >= BEGV);
9669 /* H is the actual vertical distance from the position in *IT
9670 and the starting position. */
9671 h = it2.current_y - it->current_y;
9672 /* NLINES is the distance in number of lines. */
9673 nlines = it2.vpos - it->vpos;
9675 /* Correct IT's y and vpos position
9676 so that they are relative to the starting point. */
9677 it->vpos -= nlines;
9678 it->current_y -= h;
9680 if (dy == 0)
9682 /* DY == 0 means move to the start of the screen line. The
9683 value of nlines is > 0 if continuation lines were involved,
9684 or if the original IT position was at start of a line. */
9685 RESTORE_IT (it, it, it2data);
9686 if (nlines > 0)
9687 move_it_by_lines (it, nlines);
9688 /* The above code moves us to some position NLINES down,
9689 usually to its first glyph (leftmost in an L2R line), but
9690 that's not necessarily the start of the line, under bidi
9691 reordering. We want to get to the character position
9692 that is immediately after the newline of the previous
9693 line. */
9694 if (it->bidi_p
9695 && !it->continuation_lines_width
9696 && !STRINGP (it->string)
9697 && IT_CHARPOS (*it) > BEGV
9698 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9700 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9702 DEC_BOTH (cp, bp);
9703 cp = find_newline_no_quit (cp, bp, -1, NULL);
9704 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9706 bidi_unshelve_cache (it3data, true);
9708 else
9710 /* The y-position we try to reach, relative to *IT.
9711 Note that H has been subtracted in front of the if-statement. */
9712 int target_y = it->current_y + h - dy;
9713 int y0 = it3.current_y;
9714 int y1;
9715 int line_height;
9717 RESTORE_IT (&it3, &it3, it3data);
9718 y1 = line_bottom_y (&it3);
9719 line_height = y1 - y0;
9720 RESTORE_IT (it, it, it2data);
9721 /* If we did not reach target_y, try to move further backward if
9722 we can. If we moved too far backward, try to move forward. */
9723 if (target_y < it->current_y
9724 /* This is heuristic. In a window that's 3 lines high, with
9725 a line height of 13 pixels each, recentering with point
9726 on the bottom line will try to move -39/2 = 19 pixels
9727 backward. Try to avoid moving into the first line. */
9728 && (it->current_y - target_y
9729 > min (window_box_height (it->w), line_height * 2 / 3))
9730 && IT_CHARPOS (*it) > BEGV)
9732 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9733 target_y - it->current_y));
9734 dy = it->current_y - target_y;
9735 goto move_further_back;
9737 else if (target_y >= it->current_y + line_height
9738 && IT_CHARPOS (*it) < ZV)
9740 /* Should move forward by at least one line, maybe more.
9742 Note: Calling move_it_by_lines can be expensive on
9743 terminal frames, where compute_motion is used (via
9744 vmotion) to do the job, when there are very long lines
9745 and truncate-lines is nil. That's the reason for
9746 treating terminal frames specially here. */
9748 if (!FRAME_WINDOW_P (it->f))
9749 move_it_vertically (it, target_y - it->current_y);
9750 else
9754 move_it_by_lines (it, 1);
9756 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9763 /* Move IT by a specified amount of pixel lines DY. DY negative means
9764 move backwards. DY = 0 means move to start of screen line. At the
9765 end, IT will be on the start of a screen line. */
9767 void
9768 move_it_vertically (struct it *it, int dy)
9770 if (dy <= 0)
9771 move_it_vertically_backward (it, -dy);
9772 else
9774 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9775 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9776 MOVE_TO_POS | MOVE_TO_Y);
9777 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9779 /* If buffer ends in ZV without a newline, move to the start of
9780 the line to satisfy the post-condition. */
9781 if (IT_CHARPOS (*it) == ZV
9782 && ZV > BEGV
9783 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9784 move_it_by_lines (it, 0);
9789 /* Move iterator IT past the end of the text line it is in. */
9791 void
9792 move_it_past_eol (struct it *it)
9794 enum move_it_result rc;
9796 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9797 if (rc == MOVE_NEWLINE_OR_CR)
9798 set_iterator_to_next (it, false);
9802 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9803 negative means move up. DVPOS == 0 means move to the start of the
9804 screen line.
9806 Optimization idea: If we would know that IT->f doesn't use
9807 a face with proportional font, we could be faster for
9808 truncate-lines nil. */
9810 void
9811 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9814 /* The commented-out optimization uses vmotion on terminals. This
9815 gives bad results, because elements like it->what, on which
9816 callers such as pos_visible_p rely, aren't updated. */
9817 /* struct position pos;
9818 if (!FRAME_WINDOW_P (it->f))
9820 struct text_pos textpos;
9822 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9823 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9824 reseat (it, textpos, true);
9825 it->vpos += pos.vpos;
9826 it->current_y += pos.vpos;
9828 else */
9830 if (dvpos == 0)
9832 /* DVPOS == 0 means move to the start of the screen line. */
9833 move_it_vertically_backward (it, 0);
9834 /* Let next call to line_bottom_y calculate real line height. */
9835 last_height = 0;
9837 else if (dvpos > 0)
9839 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9840 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9842 /* Only move to the next buffer position if we ended up in a
9843 string from display property, not in an overlay string
9844 (before-string or after-string). That is because the
9845 latter don't conceal the underlying buffer position, so
9846 we can ask to move the iterator to the exact position we
9847 are interested in. Note that, even if we are already at
9848 IT_CHARPOS (*it), the call below is not a no-op, as it
9849 will detect that we are at the end of the string, pop the
9850 iterator, and compute it->current_x and it->hpos
9851 correctly. */
9852 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9853 -1, -1, -1, MOVE_TO_POS);
9856 else
9858 struct it it2;
9859 void *it2data = NULL;
9860 ptrdiff_t start_charpos, i;
9861 int nchars_per_row
9862 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9863 bool hit_pos_limit = false;
9864 ptrdiff_t pos_limit;
9866 /* Start at the beginning of the screen line containing IT's
9867 position. This may actually move vertically backwards,
9868 in case of overlays, so adjust dvpos accordingly. */
9869 dvpos += it->vpos;
9870 move_it_vertically_backward (it, 0);
9871 dvpos -= it->vpos;
9873 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9874 screen lines, and reseat the iterator there. */
9875 start_charpos = IT_CHARPOS (*it);
9876 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9877 pos_limit = BEGV;
9878 else
9879 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9881 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9882 back_to_previous_visible_line_start (it);
9883 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9884 hit_pos_limit = true;
9885 reseat (it, it->current.pos, true);
9887 /* Move further back if we end up in a string or an image. */
9888 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9890 /* First try to move to start of display line. */
9891 dvpos += it->vpos;
9892 move_it_vertically_backward (it, 0);
9893 dvpos -= it->vpos;
9894 if (IT_POS_VALID_AFTER_MOVE_P (it))
9895 break;
9896 /* If start of line is still in string or image,
9897 move further back. */
9898 back_to_previous_visible_line_start (it);
9899 reseat (it, it->current.pos, true);
9900 dvpos--;
9903 it->current_x = it->hpos = 0;
9905 /* Above call may have moved too far if continuation lines
9906 are involved. Scan forward and see if it did. */
9907 SAVE_IT (it2, *it, it2data);
9908 it2.vpos = it2.current_y = 0;
9909 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9910 it->vpos -= it2.vpos;
9911 it->current_y -= it2.current_y;
9912 it->current_x = it->hpos = 0;
9914 /* If we moved too far back, move IT some lines forward. */
9915 if (it2.vpos > -dvpos)
9917 int delta = it2.vpos + dvpos;
9919 RESTORE_IT (&it2, &it2, it2data);
9920 SAVE_IT (it2, *it, it2data);
9921 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9922 /* Move back again if we got too far ahead. */
9923 if (IT_CHARPOS (*it) >= start_charpos)
9924 RESTORE_IT (it, &it2, it2data);
9925 else
9926 bidi_unshelve_cache (it2data, true);
9928 else if (hit_pos_limit && pos_limit > BEGV
9929 && dvpos < 0 && it2.vpos < -dvpos)
9931 /* If we hit the limit, but still didn't make it far enough
9932 back, that means there's a display string with a newline
9933 covering a large chunk of text, and that caused
9934 back_to_previous_visible_line_start try to go too far.
9935 Punish those who commit such atrocities by going back
9936 until we've reached DVPOS, after lifting the limit, which
9937 could make it slow for very long lines. "If it hurts,
9938 don't do that!" */
9939 dvpos += it2.vpos;
9940 RESTORE_IT (it, it, it2data);
9941 for (i = -dvpos; i > 0; --i)
9943 back_to_previous_visible_line_start (it);
9944 it->vpos--;
9946 reseat_1 (it, it->current.pos, true);
9948 else
9949 RESTORE_IT (it, it, it2data);
9954 partial_line_height (struct it *it_origin)
9956 int partial_height;
9957 void *it_data = NULL;
9958 struct it it;
9959 SAVE_IT (it, *it_origin, it_data);
9960 move_it_to (&it, ZV, -1, it.last_visible_y, -1,
9961 MOVE_TO_POS | MOVE_TO_Y);
9962 if (it.what == IT_EOB)
9964 int vis_height = it.last_visible_y - it.current_y;
9965 int height = it.ascent + it.descent;
9966 partial_height = (vis_height < height) ? vis_height : 0;
9968 else
9970 int last_line_y = it.current_y;
9971 move_it_by_lines (&it, 1);
9972 partial_height = (it.current_y > it.last_visible_y)
9973 ? it.last_visible_y - last_line_y : 0;
9975 RESTORE_IT (&it, &it, it_data);
9976 return partial_height;
9979 /* Return true if IT points into the middle of a display vector. */
9981 bool
9982 in_display_vector_p (struct it *it)
9984 return (it->method == GET_FROM_DISPLAY_VECTOR
9985 && it->current.dpvec_index > 0
9986 && it->dpvec + it->current.dpvec_index != it->dpend);
9989 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9990 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9991 WINDOW must be a live window and defaults to the selected one. The
9992 return value is a cons of the maximum pixel-width of any text line and
9993 the maximum pixel-height of all text lines.
9995 The optional argument FROM, if non-nil, specifies the first text
9996 position and defaults to the minimum accessible position of the buffer.
9997 If FROM is t, use the minimum accessible position that starts a
9998 non-empty line. TO, if non-nil, specifies the last text position and
9999 defaults to the maximum accessible position of the buffer. If TO is t,
10000 use the maximum accessible position that ends a non-empty line.
10002 The optional argument X-LIMIT, if non-nil, specifies the maximum text
10003 width that can be returned. X-LIMIT nil or omitted, means to use the
10004 pixel-width of WINDOW's body; use this if you want to know how high
10005 WINDOW should be become in order to fit all of its buffer's text with
10006 the width of WINDOW unaltered. Use the maximum width WINDOW may assume
10007 if you intend to change WINDOW's width. In any case, text whose
10008 x-coordinate is beyond X-LIMIT is ignored. Since calculating the width
10009 of long lines can take some time, it's always a good idea to make this
10010 argument as small as possible; in particular, if the buffer contains
10011 long lines that shall be truncated anyway.
10013 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
10014 height (excluding the height of the mode- or header-line, if any) that
10015 can be returned. Text lines whose y-coordinate is beyond Y-LIMIT are
10016 ignored. Since calculating the text height of a large buffer can take
10017 some time, it makes sense to specify this argument if the size of the
10018 buffer is large or unknown.
10020 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
10021 include the height of the mode- or header-line of WINDOW in the return
10022 value. If it is either the symbol `mode-line' or `header-line', include
10023 only the height of that line, if present, in the return value. If t,
10024 include the height of both, if present, in the return value. */)
10025 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
10026 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
10028 struct window *w = decode_live_window (window);
10029 Lisp_Object buffer = w->contents;
10030 struct buffer *b;
10031 struct it it;
10032 struct buffer *old_b = NULL;
10033 ptrdiff_t start, end, pos;
10034 struct text_pos startp;
10035 void *itdata = NULL;
10036 int c, max_x = 0, max_y = 0, x = 0, y = 0;
10038 CHECK_BUFFER (buffer);
10039 b = XBUFFER (buffer);
10041 if (b != current_buffer)
10043 old_b = current_buffer;
10044 set_buffer_internal (b);
10047 if (NILP (from))
10048 start = BEGV;
10049 else if (EQ (from, Qt))
10051 start = pos = BEGV;
10052 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
10053 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10054 start = pos;
10055 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10056 start = pos;
10058 else
10060 CHECK_NUMBER_COERCE_MARKER (from);
10061 start = min (max (XINT (from), BEGV), ZV);
10064 if (NILP (to))
10065 end = ZV;
10066 else if (EQ (to, Qt))
10068 end = pos = ZV;
10069 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
10070 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10071 end = pos;
10072 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10073 end = pos;
10075 else
10077 CHECK_NUMBER_COERCE_MARKER (to);
10078 end = max (start, min (XINT (to), ZV));
10081 if (!NILP (x_limit) && RANGED_INTEGERP (0, x_limit, INT_MAX))
10082 max_x = XINT (x_limit);
10084 if (NILP (y_limit))
10085 max_y = INT_MAX;
10086 else if (RANGED_INTEGERP (0, y_limit, INT_MAX))
10087 max_y = XINT (y_limit);
10089 itdata = bidi_shelve_cache ();
10090 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
10091 start_display (&it, w, startp);
10093 if (NILP (x_limit))
10094 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
10095 else
10097 it.last_visible_x = max_x;
10098 /* Actually, we never want move_it_to stop at to_x. But to make
10099 sure that move_it_in_display_line_to always moves far enough,
10100 we set it to INT_MAX and specify MOVE_TO_X. */
10101 x = move_it_to (&it, end, INT_MAX, max_y, -1,
10102 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10103 /* Don't return more than X-LIMIT. */
10104 if (x > max_x)
10105 x = max_x;
10108 /* Subtract height of header-line which was counted automatically by
10109 start_display. */
10110 y = it.current_y + it.max_ascent + it.max_descent
10111 - WINDOW_HEADER_LINE_HEIGHT (w);
10112 /* Don't return more than Y-LIMIT. */
10113 if (y > max_y)
10114 y = max_y;
10116 if (EQ (mode_and_header_line, Qheader_line)
10117 || EQ (mode_and_header_line, Qt))
10118 /* Re-add height of header-line as requested. */
10119 y = y + WINDOW_HEADER_LINE_HEIGHT (w);
10121 if (EQ (mode_and_header_line, Qmode_line)
10122 || EQ (mode_and_header_line, Qt))
10123 /* Add height of mode-line as requested. */
10124 y = y + WINDOW_MODE_LINE_HEIGHT (w);
10126 bidi_unshelve_cache (itdata, false);
10128 if (old_b)
10129 set_buffer_internal (old_b);
10131 return Fcons (make_number (x), make_number (y));
10134 /***********************************************************************
10135 Messages
10136 ***********************************************************************/
10138 /* Return the number of arguments the format string FORMAT needs. */
10140 static ptrdiff_t
10141 format_nargs (char const *format)
10143 ptrdiff_t nargs = 0;
10144 for (char const *p = format; (p = strchr (p, '%')); p++)
10145 if (p[1] == '%')
10146 p++;
10147 else
10148 nargs++;
10149 return nargs;
10152 /* Add a message with format string FORMAT and formatted arguments
10153 to *Messages*. */
10155 void
10156 add_to_log (const char *format, ...)
10158 va_list ap;
10159 va_start (ap, format);
10160 vadd_to_log (format, ap);
10161 va_end (ap);
10164 void
10165 vadd_to_log (char const *format, va_list ap)
10167 ptrdiff_t form_nargs = format_nargs (format);
10168 ptrdiff_t nargs = 1 + form_nargs;
10169 Lisp_Object args[10];
10170 eassert (nargs <= ARRAYELTS (args));
10171 AUTO_STRING (args0, format);
10172 args[0] = args0;
10173 for (ptrdiff_t i = 1; i <= nargs; i++)
10174 args[i] = va_arg (ap, Lisp_Object);
10175 Lisp_Object msg = Qnil;
10176 msg = Fformat_message (nargs, args);
10178 ptrdiff_t len = SBYTES (msg) + 1;
10179 USE_SAFE_ALLOCA;
10180 char *buffer = SAFE_ALLOCA (len);
10181 memcpy (buffer, SDATA (msg), len);
10183 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
10184 SAFE_FREE ();
10188 /* Output a newline in the *Messages* buffer if "needs" one. */
10190 void
10191 message_log_maybe_newline (void)
10193 if (message_log_need_newline)
10194 message_dolog ("", 0, true, false);
10198 /* Add a string M of length NBYTES to the message log, optionally
10199 terminated with a newline when NLFLAG is true. MULTIBYTE, if
10200 true, means interpret the contents of M as multibyte. This
10201 function calls low-level routines in order to bypass text property
10202 hooks, etc. which might not be safe to run.
10204 This may GC (insert may run before/after change hooks),
10205 so the buffer M must NOT point to a Lisp string. */
10207 void
10208 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10210 const unsigned char *msg = (const unsigned char *) m;
10212 if (!NILP (Vmemory_full))
10213 return;
10215 if (!NILP (Vmessage_log_max))
10217 struct buffer *oldbuf;
10218 Lisp_Object oldpoint, oldbegv, oldzv;
10219 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10220 ptrdiff_t point_at_end = 0;
10221 ptrdiff_t zv_at_end = 0;
10222 Lisp_Object old_deactivate_mark;
10224 old_deactivate_mark = Vdeactivate_mark;
10225 oldbuf = current_buffer;
10227 /* Ensure the Messages buffer exists, and switch to it.
10228 If we created it, set the major-mode. */
10229 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10230 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10231 if (newbuffer
10232 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10233 call0 (intern ("messages-buffer-mode"));
10235 bset_undo_list (current_buffer, Qt);
10236 bset_cache_long_scans (current_buffer, Qnil);
10238 oldpoint = message_dolog_marker1;
10239 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10240 oldbegv = message_dolog_marker2;
10241 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10242 oldzv = message_dolog_marker3;
10243 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10245 if (PT == Z)
10246 point_at_end = 1;
10247 if (ZV == Z)
10248 zv_at_end = 1;
10250 BEGV = BEG;
10251 BEGV_BYTE = BEG_BYTE;
10252 ZV = Z;
10253 ZV_BYTE = Z_BYTE;
10254 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10256 /* Insert the string--maybe converting multibyte to single byte
10257 or vice versa, so that all the text fits the buffer. */
10258 if (multibyte
10259 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10261 ptrdiff_t i;
10262 int c, char_bytes;
10263 char work[1];
10265 /* Convert a multibyte string to single-byte
10266 for the *Message* buffer. */
10267 for (i = 0; i < nbytes; i += char_bytes)
10269 c = string_char_and_length (msg + i, &char_bytes);
10270 work[0] = CHAR_TO_BYTE8 (c);
10271 insert_1_both (work, 1, 1, true, false, false);
10274 else if (! multibyte
10275 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10277 ptrdiff_t i;
10278 int c, char_bytes;
10279 unsigned char str[MAX_MULTIBYTE_LENGTH];
10280 /* Convert a single-byte string to multibyte
10281 for the *Message* buffer. */
10282 for (i = 0; i < nbytes; i++)
10284 c = msg[i];
10285 MAKE_CHAR_MULTIBYTE (c);
10286 char_bytes = CHAR_STRING (c, str);
10287 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10290 else if (nbytes)
10291 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10292 true, false, false);
10294 if (nlflag)
10296 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10297 printmax_t dups;
10299 insert_1_both ("\n", 1, 1, true, false, false);
10301 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10302 this_bol = PT;
10303 this_bol_byte = PT_BYTE;
10305 /* See if this line duplicates the previous one.
10306 If so, combine duplicates. */
10307 if (this_bol > BEG)
10309 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10310 prev_bol = PT;
10311 prev_bol_byte = PT_BYTE;
10313 dups = message_log_check_duplicate (prev_bol_byte,
10314 this_bol_byte);
10315 if (dups)
10317 del_range_both (prev_bol, prev_bol_byte,
10318 this_bol, this_bol_byte, false);
10319 if (dups > 1)
10321 char dupstr[sizeof " [ times]"
10322 + INT_STRLEN_BOUND (printmax_t)];
10324 /* If you change this format, don't forget to also
10325 change message_log_check_duplicate. */
10326 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10327 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10328 insert_1_both (dupstr, duplen, duplen,
10329 true, false, true);
10334 /* If we have more than the desired maximum number of lines
10335 in the *Messages* buffer now, delete the oldest ones.
10336 This is safe because we don't have undo in this buffer. */
10338 if (NATNUMP (Vmessage_log_max))
10340 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10341 -XFASTINT (Vmessage_log_max) - 1, false);
10342 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10345 BEGV = marker_position (oldbegv);
10346 BEGV_BYTE = marker_byte_position (oldbegv);
10348 if (zv_at_end)
10350 ZV = Z;
10351 ZV_BYTE = Z_BYTE;
10353 else
10355 ZV = marker_position (oldzv);
10356 ZV_BYTE = marker_byte_position (oldzv);
10359 if (point_at_end)
10360 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10361 else
10362 /* We can't do Fgoto_char (oldpoint) because it will run some
10363 Lisp code. */
10364 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10365 marker_byte_position (oldpoint));
10367 unchain_marker (XMARKER (oldpoint));
10368 unchain_marker (XMARKER (oldbegv));
10369 unchain_marker (XMARKER (oldzv));
10371 /* We called insert_1_both above with its 5th argument (PREPARE)
10372 false, which prevents insert_1_both from calling
10373 prepare_to_modify_buffer, which in turns prevents us from
10374 incrementing windows_or_buffers_changed even if *Messages* is
10375 shown in some window. So we must manually set
10376 windows_or_buffers_changed here to make up for that. */
10377 windows_or_buffers_changed = old_windows_or_buffers_changed;
10378 bset_redisplay (current_buffer);
10380 set_buffer_internal (oldbuf);
10382 message_log_need_newline = !nlflag;
10383 Vdeactivate_mark = old_deactivate_mark;
10388 /* We are at the end of the buffer after just having inserted a newline.
10389 (Note: We depend on the fact we won't be crossing the gap.)
10390 Check to see if the most recent message looks a lot like the previous one.
10391 Return 0 if different, 1 if the new one should just replace it, or a
10392 value N > 1 if we should also append " [N times]". */
10394 static intmax_t
10395 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10397 ptrdiff_t i;
10398 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10399 bool seen_dots = false;
10400 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10401 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10403 for (i = 0; i < len; i++)
10405 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10406 seen_dots = true;
10407 if (p1[i] != p2[i])
10408 return seen_dots;
10410 p1 += len;
10411 if (*p1 == '\n')
10412 return 2;
10413 if (*p1++ == ' ' && *p1++ == '[')
10415 char *pend;
10416 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10417 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10418 return n + 1;
10420 return 0;
10424 /* Display an echo area message M with a specified length of NBYTES
10425 bytes. The string may include null characters. If M is not a
10426 string, clear out any existing message, and let the mini-buffer
10427 text show through.
10429 This function cancels echoing. */
10431 void
10432 message3 (Lisp_Object m)
10434 clear_message (true, true);
10435 cancel_echoing ();
10437 /* First flush out any partial line written with print. */
10438 message_log_maybe_newline ();
10439 if (STRINGP (m))
10441 ptrdiff_t nbytes = SBYTES (m);
10442 bool multibyte = STRING_MULTIBYTE (m);
10443 char *buffer;
10444 USE_SAFE_ALLOCA;
10445 SAFE_ALLOCA_STRING (buffer, m);
10446 message_dolog (buffer, nbytes, true, multibyte);
10447 SAFE_FREE ();
10449 if (! inhibit_message)
10450 message3_nolog (m);
10453 /* Log the message M to stderr. Log an empty line if M is not a string. */
10455 static void
10456 message_to_stderr (Lisp_Object m)
10458 if (noninteractive_need_newline)
10460 noninteractive_need_newline = false;
10461 fputc ('\n', stderr);
10463 if (STRINGP (m))
10465 Lisp_Object coding_system = Vlocale_coding_system;
10466 Lisp_Object s;
10468 if (!NILP (Vcoding_system_for_write))
10469 coding_system = Vcoding_system_for_write;
10470 if (!NILP (coding_system))
10471 s = code_convert_string_norecord (m, coding_system, true);
10472 else
10473 s = m;
10475 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10477 if (!cursor_in_echo_area)
10478 fputc ('\n', stderr);
10479 fflush (stderr);
10482 /* The non-logging version of message3.
10483 This does not cancel echoing, because it is used for echoing.
10484 Perhaps we need to make a separate function for echoing
10485 and make this cancel echoing. */
10487 void
10488 message3_nolog (Lisp_Object m)
10490 struct frame *sf = SELECTED_FRAME ();
10492 if (FRAME_INITIAL_P (sf))
10493 message_to_stderr (m);
10494 /* Error messages get reported properly by cmd_error, so this must be just an
10495 informative message; if the frame hasn't really been initialized yet, just
10496 toss it. */
10497 else if (INTERACTIVE && sf->glyphs_initialized_p)
10499 /* Get the frame containing the mini-buffer
10500 that the selected frame is using. */
10501 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10502 Lisp_Object frame = XWINDOW (mini_window)->frame;
10503 struct frame *f = XFRAME (frame);
10505 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10506 Fmake_frame_visible (frame);
10508 if (STRINGP (m) && SCHARS (m) > 0)
10510 set_message (m);
10511 if (minibuffer_auto_raise)
10512 Fraise_frame (frame);
10513 /* Assume we are not echoing.
10514 (If we are, echo_now will override this.) */
10515 echo_message_buffer = Qnil;
10517 else
10518 clear_message (true, true);
10520 do_pending_window_change (false);
10521 echo_area_display (true);
10522 do_pending_window_change (false);
10523 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10524 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10529 /* Display a null-terminated echo area message M. If M is 0, clear
10530 out any existing message, and let the mini-buffer text show through.
10532 The buffer M must continue to exist until after the echo area gets
10533 cleared or some other message gets displayed there. Do not pass
10534 text that is stored in a Lisp string. Do not pass text in a buffer
10535 that was alloca'd. */
10537 void
10538 message1 (const char *m)
10540 message3 (m ? build_unibyte_string (m) : Qnil);
10544 /* The non-logging counterpart of message1. */
10546 void
10547 message1_nolog (const char *m)
10549 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10552 /* Display a message M which contains a single %s
10553 which gets replaced with STRING. */
10555 void
10556 message_with_string (const char *m, Lisp_Object string, bool log)
10558 CHECK_STRING (string);
10560 bool need_message;
10561 if (noninteractive)
10562 need_message = !!m;
10563 else if (!INTERACTIVE)
10564 need_message = false;
10565 else
10567 /* The frame whose minibuffer we're going to display the message on.
10568 It may be larger than the selected frame, so we need
10569 to use its buffer, not the selected frame's buffer. */
10570 Lisp_Object mini_window;
10571 struct frame *f, *sf = SELECTED_FRAME ();
10573 /* Get the frame containing the minibuffer
10574 that the selected frame is using. */
10575 mini_window = FRAME_MINIBUF_WINDOW (sf);
10576 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10578 /* Error messages get reported properly by cmd_error, so this must be
10579 just an informative message; if the frame hasn't really been
10580 initialized yet, just toss it. */
10581 need_message = f->glyphs_initialized_p;
10584 if (need_message)
10586 AUTO_STRING (fmt, m);
10587 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10589 if (noninteractive)
10590 message_to_stderr (msg);
10591 else
10593 if (log)
10594 message3 (msg);
10595 else
10596 message3_nolog (msg);
10598 /* Print should start at the beginning of the message
10599 buffer next time. */
10600 message_buf_print = false;
10606 /* Dump an informative message to the minibuf. If M is 0, clear out
10607 any existing message, and let the mini-buffer text show through.
10609 The message must be safe ASCII (because when Emacs is
10610 non-interactive the message is sent straight to stderr without
10611 encoding first) and the format must not contain ` or ' (because
10612 this function does not account for `text-quoting-style'). If your
10613 message and format do not fit into this category, convert your
10614 arguments to Lisp objects and use Fmessage instead. */
10616 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10617 vmessage (const char *m, va_list ap)
10619 if (noninteractive)
10621 if (m)
10623 if (noninteractive_need_newline)
10624 putc ('\n', stderr);
10625 noninteractive_need_newline = false;
10626 vfprintf (stderr, m, ap);
10627 if (!cursor_in_echo_area)
10628 fprintf (stderr, "\n");
10629 fflush (stderr);
10632 else if (INTERACTIVE)
10634 /* The frame whose mini-buffer we're going to display the message
10635 on. It may be larger than the selected frame, so we need to
10636 use its buffer, not the selected frame's buffer. */
10637 Lisp_Object mini_window;
10638 struct frame *f, *sf = SELECTED_FRAME ();
10640 /* Get the frame containing the mini-buffer
10641 that the selected frame is using. */
10642 mini_window = FRAME_MINIBUF_WINDOW (sf);
10643 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10645 /* Error messages get reported properly by cmd_error, so this must be
10646 just an informative message; if the frame hasn't really been
10647 initialized yet, just toss it. */
10648 if (f->glyphs_initialized_p)
10650 if (m)
10652 ptrdiff_t len;
10653 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10654 USE_SAFE_ALLOCA;
10655 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10657 len = doprnt (message_buf, maxsize, m, 0, ap);
10659 message3 (make_string (message_buf, len));
10660 SAFE_FREE ();
10662 else
10663 message1 (0);
10665 /* Print should start at the beginning of the message
10666 buffer next time. */
10667 message_buf_print = false;
10672 /* See vmessage for restrictions on the text of the message. */
10673 void
10674 message (const char *m, ...)
10676 va_list ap;
10677 va_start (ap, m);
10678 vmessage (m, ap);
10679 va_end (ap);
10683 /* Display the current message in the current mini-buffer. This is
10684 only called from error handlers in process.c, and is not time
10685 critical. */
10687 void
10688 update_echo_area (void)
10690 if (!NILP (echo_area_buffer[0]))
10692 Lisp_Object string;
10693 string = Fcurrent_message ();
10694 message3 (string);
10699 /* Make sure echo area buffers in `echo_buffers' are live.
10700 If they aren't, make new ones. */
10702 static void
10703 ensure_echo_area_buffers (void)
10705 for (int i = 0; i < 2; i++)
10706 if (!BUFFERP (echo_buffer[i])
10707 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10709 Lisp_Object old_buffer = echo_buffer[i];
10710 static char const name_fmt[] = " *Echo Area %d*";
10711 char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
10712 AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
10713 echo_buffer[i] = Fget_buffer_create (lname);
10714 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10715 /* to force word wrap in echo area -
10716 it was decided to postpone this*/
10717 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10719 for (int j = 0; j < 2; j++)
10720 if (EQ (old_buffer, echo_area_buffer[j]))
10721 echo_area_buffer[j] = echo_buffer[i];
10726 /* Call FN with args A1..A2 with either the current or last displayed
10727 echo_area_buffer as current buffer.
10729 WHICH zero means use the current message buffer
10730 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10731 from echo_buffer[] and clear it.
10733 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10734 suitable buffer from echo_buffer[] and clear it.
10736 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10737 that the current message becomes the last displayed one, choose a
10738 suitable buffer for echo_area_buffer[0], and clear it.
10740 Value is what FN returns. */
10742 static bool
10743 with_echo_area_buffer (struct window *w, int which,
10744 bool (*fn) (ptrdiff_t, Lisp_Object),
10745 ptrdiff_t a1, Lisp_Object a2)
10747 Lisp_Object buffer;
10748 bool this_one, the_other, clear_buffer_p, rc;
10749 ptrdiff_t count = SPECPDL_INDEX ();
10751 /* If buffers aren't live, make new ones. */
10752 ensure_echo_area_buffers ();
10754 clear_buffer_p = false;
10756 if (which == 0)
10757 this_one = false, the_other = true;
10758 else if (which > 0)
10759 this_one = true, the_other = false;
10760 else
10762 this_one = false, the_other = true;
10763 clear_buffer_p = true;
10765 /* We need a fresh one in case the current echo buffer equals
10766 the one containing the last displayed echo area message. */
10767 if (!NILP (echo_area_buffer[this_one])
10768 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10769 echo_area_buffer[this_one] = Qnil;
10772 /* Choose a suitable buffer from echo_buffer[] if we don't
10773 have one. */
10774 if (NILP (echo_area_buffer[this_one]))
10776 echo_area_buffer[this_one]
10777 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10778 ? echo_buffer[the_other]
10779 : echo_buffer[this_one]);
10780 clear_buffer_p = true;
10783 buffer = echo_area_buffer[this_one];
10785 /* Don't get confused by reusing the buffer used for echoing
10786 for a different purpose. */
10787 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10788 cancel_echoing ();
10790 record_unwind_protect (unwind_with_echo_area_buffer,
10791 with_echo_area_buffer_unwind_data (w));
10793 /* Make the echo area buffer current. Note that for display
10794 purposes, it is not necessary that the displayed window's buffer
10795 == current_buffer, except for text property lookup. So, let's
10796 only set that buffer temporarily here without doing a full
10797 Fset_window_buffer. We must also change w->pointm, though,
10798 because otherwise an assertions in unshow_buffer fails, and Emacs
10799 aborts. */
10800 set_buffer_internal_1 (XBUFFER (buffer));
10801 if (w)
10803 wset_buffer (w, buffer);
10804 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10805 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10808 bset_undo_list (current_buffer, Qt);
10809 bset_read_only (current_buffer, Qnil);
10810 specbind (Qinhibit_read_only, Qt);
10811 specbind (Qinhibit_modification_hooks, Qt);
10813 if (clear_buffer_p && Z > BEG)
10814 del_range (BEG, Z);
10816 eassert (BEGV >= BEG);
10817 eassert (ZV <= Z && ZV >= BEGV);
10819 rc = fn (a1, a2);
10821 eassert (BEGV >= BEG);
10822 eassert (ZV <= Z && ZV >= BEGV);
10824 unbind_to (count, Qnil);
10825 return rc;
10829 /* Save state that should be preserved around the call to the function
10830 FN called in with_echo_area_buffer. */
10832 static Lisp_Object
10833 with_echo_area_buffer_unwind_data (struct window *w)
10835 int i = 0;
10836 Lisp_Object vector, tmp;
10838 /* Reduce consing by keeping one vector in
10839 Vwith_echo_area_save_vector. */
10840 vector = Vwith_echo_area_save_vector;
10841 Vwith_echo_area_save_vector = Qnil;
10843 if (NILP (vector))
10844 vector = Fmake_vector (make_number (11), Qnil);
10846 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10847 ASET (vector, i, Vdeactivate_mark); ++i;
10848 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10850 if (w)
10852 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10853 ASET (vector, i, w->contents); ++i;
10854 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10855 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10856 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10857 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10858 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10859 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10861 else
10863 int end = i + 8;
10864 for (; i < end; ++i)
10865 ASET (vector, i, Qnil);
10868 eassert (i == ASIZE (vector));
10869 return vector;
10873 /* Restore global state from VECTOR which was created by
10874 with_echo_area_buffer_unwind_data. */
10876 static void
10877 unwind_with_echo_area_buffer (Lisp_Object vector)
10879 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10880 Vdeactivate_mark = AREF (vector, 1);
10881 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10883 if (WINDOWP (AREF (vector, 3)))
10885 struct window *w;
10886 Lisp_Object buffer;
10888 w = XWINDOW (AREF (vector, 3));
10889 buffer = AREF (vector, 4);
10891 wset_buffer (w, buffer);
10892 set_marker_both (w->pointm, buffer,
10893 XFASTINT (AREF (vector, 5)),
10894 XFASTINT (AREF (vector, 6)));
10895 set_marker_both (w->old_pointm, buffer,
10896 XFASTINT (AREF (vector, 7)),
10897 XFASTINT (AREF (vector, 8)));
10898 set_marker_both (w->start, buffer,
10899 XFASTINT (AREF (vector, 9)),
10900 XFASTINT (AREF (vector, 10)));
10903 Vwith_echo_area_save_vector = vector;
10907 /* Set up the echo area for use by print functions. MULTIBYTE_P
10908 means we will print multibyte. */
10910 void
10911 setup_echo_area_for_printing (bool multibyte_p)
10913 /* If we can't find an echo area any more, exit. */
10914 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10915 Fkill_emacs (Qnil);
10917 ensure_echo_area_buffers ();
10919 if (!message_buf_print)
10921 /* A message has been output since the last time we printed.
10922 Choose a fresh echo area buffer. */
10923 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10924 echo_area_buffer[0] = echo_buffer[1];
10925 else
10926 echo_area_buffer[0] = echo_buffer[0];
10928 /* Switch to that buffer and clear it. */
10929 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10930 bset_truncate_lines (current_buffer, Qnil);
10932 if (Z > BEG)
10934 ptrdiff_t count = SPECPDL_INDEX ();
10935 specbind (Qinhibit_read_only, Qt);
10936 /* Note that undo recording is always disabled. */
10937 del_range (BEG, Z);
10938 unbind_to (count, Qnil);
10940 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10942 /* Set up the buffer for the multibyteness we need. */
10943 if (multibyte_p
10944 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10945 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10947 /* Raise the frame containing the echo area. */
10948 if (minibuffer_auto_raise)
10950 struct frame *sf = SELECTED_FRAME ();
10951 Lisp_Object mini_window;
10952 mini_window = FRAME_MINIBUF_WINDOW (sf);
10953 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10956 message_log_maybe_newline ();
10957 message_buf_print = true;
10959 else
10961 if (NILP (echo_area_buffer[0]))
10963 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10964 echo_area_buffer[0] = echo_buffer[1];
10965 else
10966 echo_area_buffer[0] = echo_buffer[0];
10969 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10971 /* Someone switched buffers between print requests. */
10972 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10973 bset_truncate_lines (current_buffer, Qnil);
10979 /* Display an echo area message in window W. Value is true if W's
10980 height is changed. If display_last_displayed_message_p,
10981 display the message that was last displayed, otherwise
10982 display the current message. */
10984 static bool
10985 display_echo_area (struct window *w)
10987 bool no_message_p, window_height_changed_p;
10989 /* Temporarily disable garbage collections while displaying the echo
10990 area. This is done because a GC can print a message itself.
10991 That message would modify the echo area buffer's contents while a
10992 redisplay of the buffer is going on, and seriously confuse
10993 redisplay. */
10994 ptrdiff_t count = inhibit_garbage_collection ();
10996 /* If there is no message, we must call display_echo_area_1
10997 nevertheless because it resizes the window. But we will have to
10998 reset the echo_area_buffer in question to nil at the end because
10999 with_echo_area_buffer will sets it to an empty buffer. */
11000 bool i = display_last_displayed_message_p;
11001 /* According to the C99, C11 and C++11 standards, the integral value
11002 of a "bool" is always 0 or 1, so this array access is safe here,
11003 if oddly typed. */
11004 no_message_p = NILP (echo_area_buffer[i]);
11006 window_height_changed_p
11007 = with_echo_area_buffer (w, display_last_displayed_message_p,
11008 display_echo_area_1,
11009 (intptr_t) w, Qnil);
11011 if (no_message_p)
11012 echo_area_buffer[i] = Qnil;
11014 unbind_to (count, Qnil);
11015 return window_height_changed_p;
11019 /* Helper for display_echo_area. Display the current buffer which
11020 contains the current echo area message in window W, a mini-window,
11021 a pointer to which is passed in A1. A2..A4 are currently not used.
11022 Change the height of W so that all of the message is displayed.
11023 Value is true if height of W was changed. */
11025 static bool
11026 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
11028 intptr_t i1 = a1;
11029 struct window *w = (struct window *) i1;
11030 Lisp_Object window;
11031 struct text_pos start;
11033 /* We are about to enter redisplay without going through
11034 redisplay_internal, so we need to forget these faces by hand
11035 here. */
11036 forget_escape_and_glyphless_faces ();
11038 /* Do this before displaying, so that we have a large enough glyph
11039 matrix for the display. If we can't get enough space for the
11040 whole text, display the last N lines. That works by setting w->start. */
11041 bool window_height_changed_p = resize_mini_window (w, false);
11043 /* Use the starting position chosen by resize_mini_window. */
11044 SET_TEXT_POS_FROM_MARKER (start, w->start);
11046 /* Display. */
11047 clear_glyph_matrix (w->desired_matrix);
11048 XSETWINDOW (window, w);
11049 try_window (window, start, 0);
11051 return window_height_changed_p;
11055 /* Resize the echo area window to exactly the size needed for the
11056 currently displayed message, if there is one. If a mini-buffer
11057 is active, don't shrink it. */
11059 void
11060 resize_echo_area_exactly (void)
11062 if (BUFFERP (echo_area_buffer[0])
11063 && WINDOWP (echo_area_window))
11065 struct window *w = XWINDOW (echo_area_window);
11066 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
11067 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
11068 (intptr_t) w, resize_exactly);
11069 if (resized_p)
11071 windows_or_buffers_changed = 42;
11072 update_mode_lines = 30;
11073 redisplay_internal ();
11079 /* Callback function for with_echo_area_buffer, when used from
11080 resize_echo_area_exactly. A1 contains a pointer to the window to
11081 resize, EXACTLY non-nil means resize the mini-window exactly to the
11082 size of the text displayed. A3 and A4 are not used. Value is what
11083 resize_mini_window returns. */
11085 static bool
11086 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
11088 intptr_t i1 = a1;
11089 return resize_mini_window ((struct window *) i1, !NILP (exactly));
11093 /* Resize mini-window W to fit the size of its contents. EXACT_P
11094 means size the window exactly to the size needed. Otherwise, it's
11095 only enlarged until W's buffer is empty.
11097 Set W->start to the right place to begin display. If the whole
11098 contents fit, start at the beginning. Otherwise, start so as
11099 to make the end of the contents appear. This is particularly
11100 important for y-or-n-p, but seems desirable generally.
11102 Value is true if the window height has been changed. */
11104 bool
11105 resize_mini_window (struct window *w, bool exact_p)
11107 struct frame *f = XFRAME (w->frame);
11108 bool window_height_changed_p = false;
11110 eassert (MINI_WINDOW_P (w));
11112 /* By default, start display at the beginning. */
11113 set_marker_both (w->start, w->contents,
11114 BUF_BEGV (XBUFFER (w->contents)),
11115 BUF_BEGV_BYTE (XBUFFER (w->contents)));
11117 /* Don't resize windows while redisplaying a window; it would
11118 confuse redisplay functions when the size of the window they are
11119 displaying changes from under them. Such a resizing can happen,
11120 for instance, when which-func prints a long message while
11121 we are running fontification-functions. We're running these
11122 functions with safe_call which binds inhibit-redisplay to t. */
11123 if (!NILP (Vinhibit_redisplay))
11124 return false;
11126 /* Nil means don't try to resize. */
11127 if (NILP (Vresize_mini_windows)
11128 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
11129 return false;
11131 if (!FRAME_MINIBUF_ONLY_P (f))
11133 struct it it;
11134 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
11135 + WINDOW_PIXEL_HEIGHT (w));
11136 int unit = FRAME_LINE_HEIGHT (f);
11137 int height, max_height;
11138 struct text_pos start;
11139 struct buffer *old_current_buffer = NULL;
11141 if (current_buffer != XBUFFER (w->contents))
11143 old_current_buffer = current_buffer;
11144 set_buffer_internal (XBUFFER (w->contents));
11147 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
11149 /* Compute the max. number of lines specified by the user. */
11150 if (FLOATP (Vmax_mini_window_height))
11151 max_height = XFLOAT_DATA (Vmax_mini_window_height) * total_height;
11152 else if (INTEGERP (Vmax_mini_window_height))
11153 max_height = XINT (Vmax_mini_window_height) * unit;
11154 else
11155 max_height = total_height / 4;
11157 /* Correct that max. height if it's bogus. */
11158 max_height = clip_to_bounds (unit, max_height, total_height);
11160 /* Find out the height of the text in the window. */
11161 if (it.line_wrap == TRUNCATE)
11162 height = unit;
11163 else
11165 last_height = 0;
11166 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
11167 if (it.max_ascent == 0 && it.max_descent == 0)
11168 height = it.current_y + last_height;
11169 else
11170 height = it.current_y + it.max_ascent + it.max_descent;
11171 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
11174 /* Compute a suitable window start. */
11175 if (height > max_height)
11177 height = (max_height / unit) * unit;
11178 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
11179 move_it_vertically_backward (&it, height - unit);
11180 start = it.current.pos;
11182 else
11183 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
11184 SET_MARKER_FROM_TEXT_POS (w->start, start);
11186 if (EQ (Vresize_mini_windows, Qgrow_only))
11188 /* Let it grow only, until we display an empty message, in which
11189 case the window shrinks again. */
11190 if (height > WINDOW_PIXEL_HEIGHT (w))
11192 int old_height = WINDOW_PIXEL_HEIGHT (w);
11194 FRAME_WINDOWS_FROZEN (f) = true;
11195 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11196 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11198 else if (height < WINDOW_PIXEL_HEIGHT (w)
11199 && (exact_p || BEGV == ZV))
11201 int old_height = WINDOW_PIXEL_HEIGHT (w);
11203 FRAME_WINDOWS_FROZEN (f) = false;
11204 shrink_mini_window (w, true);
11205 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11208 else
11210 /* Always resize to exact size needed. */
11211 if (height > WINDOW_PIXEL_HEIGHT (w))
11213 int old_height = WINDOW_PIXEL_HEIGHT (w);
11215 FRAME_WINDOWS_FROZEN (f) = true;
11216 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11217 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11219 else if (height < WINDOW_PIXEL_HEIGHT (w))
11221 int old_height = WINDOW_PIXEL_HEIGHT (w);
11223 FRAME_WINDOWS_FROZEN (f) = false;
11224 shrink_mini_window (w, true);
11226 if (height)
11228 FRAME_WINDOWS_FROZEN (f) = true;
11229 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11232 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11236 if (old_current_buffer)
11237 set_buffer_internal (old_current_buffer);
11240 return window_height_changed_p;
11244 /* Value is the current message, a string, or nil if there is no
11245 current message. */
11247 Lisp_Object
11248 current_message (void)
11250 Lisp_Object msg;
11252 if (!BUFFERP (echo_area_buffer[0]))
11253 msg = Qnil;
11254 else
11256 with_echo_area_buffer (0, 0, current_message_1,
11257 (intptr_t) &msg, Qnil);
11258 if (NILP (msg))
11259 echo_area_buffer[0] = Qnil;
11262 return msg;
11266 static bool
11267 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11269 intptr_t i1 = a1;
11270 Lisp_Object *msg = (Lisp_Object *) i1;
11272 if (Z > BEG)
11273 *msg = make_buffer_string (BEG, Z, true);
11274 else
11275 *msg = Qnil;
11276 return false;
11280 /* Push the current message on Vmessage_stack for later restoration
11281 by restore_message. Value is true if the current message isn't
11282 empty. This is a relatively infrequent operation, so it's not
11283 worth optimizing. */
11285 bool
11286 push_message (void)
11288 Lisp_Object msg = current_message ();
11289 Vmessage_stack = Fcons (msg, Vmessage_stack);
11290 return STRINGP (msg);
11294 /* Restore message display from the top of Vmessage_stack. */
11296 void
11297 restore_message (void)
11299 eassert (CONSP (Vmessage_stack));
11300 message3_nolog (XCAR (Vmessage_stack));
11304 /* Handler for unwind-protect calling pop_message. */
11306 void
11307 pop_message_unwind (void)
11309 /* Pop the top-most entry off Vmessage_stack. */
11310 eassert (CONSP (Vmessage_stack));
11311 Vmessage_stack = XCDR (Vmessage_stack);
11315 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11316 exits. If the stack is not empty, we have a missing pop_message
11317 somewhere. */
11319 void
11320 check_message_stack (void)
11322 if (!NILP (Vmessage_stack))
11323 emacs_abort ();
11327 /* Truncate to NCHARS what will be displayed in the echo area the next
11328 time we display it---but don't redisplay it now. */
11330 void
11331 truncate_echo_area (ptrdiff_t nchars)
11333 if (nchars == 0)
11334 echo_area_buffer[0] = Qnil;
11335 else if (!noninteractive
11336 && INTERACTIVE
11337 && !NILP (echo_area_buffer[0]))
11339 struct frame *sf = SELECTED_FRAME ();
11340 /* Error messages get reported properly by cmd_error, so this must be
11341 just an informative message; if the frame hasn't really been
11342 initialized yet, just toss it. */
11343 if (sf->glyphs_initialized_p)
11344 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11349 /* Helper function for truncate_echo_area. Truncate the current
11350 message to at most NCHARS characters. */
11352 static bool
11353 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11355 if (BEG + nchars < Z)
11356 del_range (BEG + nchars, Z);
11357 if (Z == BEG)
11358 echo_area_buffer[0] = Qnil;
11359 return false;
11362 /* Set the current message to STRING. */
11364 static void
11365 set_message (Lisp_Object string)
11367 eassert (STRINGP (string));
11369 message_enable_multibyte = STRING_MULTIBYTE (string);
11371 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11372 message_buf_print = false;
11373 help_echo_showing_p = false;
11375 if (STRINGP (Vdebug_on_message)
11376 && STRINGP (string)
11377 && fast_string_match (Vdebug_on_message, string) >= 0)
11378 call_debugger (list2 (Qerror, string));
11382 /* Helper function for set_message. First argument is ignored and second
11383 argument has the same meaning as for set_message.
11384 This function is called with the echo area buffer being current. */
11386 static bool
11387 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11389 eassert (STRINGP (string));
11391 /* Change multibyteness of the echo buffer appropriately. */
11392 if (message_enable_multibyte
11393 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11394 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11396 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11397 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11398 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11400 /* Insert new message at BEG. */
11401 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11403 /* This function takes care of single/multibyte conversion.
11404 We just have to ensure that the echo area buffer has the right
11405 setting of enable_multibyte_characters. */
11406 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11408 return false;
11412 /* Clear messages. CURRENT_P means clear the current message.
11413 LAST_DISPLAYED_P means clear the message last displayed. */
11415 void
11416 clear_message (bool current_p, bool last_displayed_p)
11418 if (current_p)
11420 echo_area_buffer[0] = Qnil;
11421 message_cleared_p = true;
11424 if (last_displayed_p)
11425 echo_area_buffer[1] = Qnil;
11427 message_buf_print = false;
11430 /* Clear garbaged frames.
11432 This function is used where the old redisplay called
11433 redraw_garbaged_frames which in turn called redraw_frame which in
11434 turn called clear_frame. The call to clear_frame was a source of
11435 flickering. I believe a clear_frame is not necessary. It should
11436 suffice in the new redisplay to invalidate all current matrices,
11437 and ensure a complete redisplay of all windows. */
11439 static void
11440 clear_garbaged_frames (void)
11442 if (frame_garbaged)
11444 Lisp_Object tail, frame;
11445 struct frame *sf = SELECTED_FRAME ();
11447 FOR_EACH_FRAME (tail, frame)
11449 struct frame *f = XFRAME (frame);
11451 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11453 if (f->resized_p
11454 /* It makes no sense to redraw a non-selected TTY
11455 frame, since that will actually clear the
11456 selected frame, and might leave the selected
11457 frame with corrupted display, if it happens not
11458 to be marked garbaged. */
11459 && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
11460 redraw_frame (f);
11461 else
11462 clear_current_matrices (f);
11464 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11465 x_clear_under_internal_border (f);
11466 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11468 fset_redisplay (f);
11469 f->garbaged = false;
11470 f->resized_p = false;
11474 frame_garbaged = false;
11479 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11480 selected_frame. */
11482 static void
11483 echo_area_display (bool update_frame_p)
11485 Lisp_Object mini_window;
11486 struct window *w;
11487 struct frame *f;
11488 bool window_height_changed_p = false;
11489 struct frame *sf = SELECTED_FRAME ();
11491 mini_window = FRAME_MINIBUF_WINDOW (sf);
11492 w = XWINDOW (mini_window);
11493 f = XFRAME (WINDOW_FRAME (w));
11495 /* Don't display if frame is invisible or not yet initialized. */
11496 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11497 return;
11499 #ifdef HAVE_WINDOW_SYSTEM
11500 /* When Emacs starts, selected_frame may be the initial terminal
11501 frame. If we let this through, a message would be displayed on
11502 the terminal. */
11503 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11504 return;
11505 #endif /* HAVE_WINDOW_SYSTEM */
11507 /* Redraw garbaged frames. */
11508 clear_garbaged_frames ();
11510 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11512 echo_area_window = mini_window;
11513 window_height_changed_p = display_echo_area (w);
11514 w->must_be_updated_p = true;
11516 /* Update the display, unless called from redisplay_internal.
11517 Also don't update the screen during redisplay itself. The
11518 update will happen at the end of redisplay, and an update
11519 here could cause confusion. */
11520 if (update_frame_p && !redisplaying_p)
11522 int n = 0;
11524 /* If the display update has been interrupted by pending
11525 input, update mode lines in the frame. Due to the
11526 pending input, it might have been that redisplay hasn't
11527 been called, so that mode lines above the echo area are
11528 garbaged. This looks odd, so we prevent it here. */
11529 if (!display_completed)
11531 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11533 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11534 x_clear_under_internal_border (f);
11535 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11539 if (window_height_changed_p
11540 /* Don't do this if Emacs is shutting down. Redisplay
11541 needs to run hooks. */
11542 && !NILP (Vrun_hooks))
11544 /* Must update other windows. Likewise as in other
11545 cases, don't let this update be interrupted by
11546 pending input. */
11547 ptrdiff_t count = SPECPDL_INDEX ();
11548 specbind (Qredisplay_dont_pause, Qt);
11549 fset_redisplay (f);
11550 redisplay_internal ();
11551 unbind_to (count, Qnil);
11553 else if (FRAME_WINDOW_P (f) && n == 0)
11555 /* Window configuration is the same as before.
11556 Can do with a display update of the echo area,
11557 unless we displayed some mode lines. */
11558 update_single_window (w);
11559 flush_frame (f);
11561 else
11562 update_frame (f, true, true);
11564 /* If cursor is in the echo area, make sure that the next
11565 redisplay displays the minibuffer, so that the cursor will
11566 be replaced with what the minibuffer wants. */
11567 if (cursor_in_echo_area)
11568 wset_redisplay (XWINDOW (mini_window));
11571 else if (!EQ (mini_window, selected_window))
11572 wset_redisplay (XWINDOW (mini_window));
11574 /* Last displayed message is now the current message. */
11575 echo_area_buffer[1] = echo_area_buffer[0];
11576 /* Inform read_char that we're not echoing. */
11577 echo_message_buffer = Qnil;
11579 /* Prevent redisplay optimization in redisplay_internal by resetting
11580 this_line_start_pos. This is done because the mini-buffer now
11581 displays the message instead of its buffer text. */
11582 if (EQ (mini_window, selected_window))
11583 CHARPOS (this_line_start_pos) = 0;
11585 if (window_height_changed_p)
11587 fset_redisplay (f);
11589 /* If window configuration was changed, frames may have been
11590 marked garbaged. Clear them or we will experience
11591 surprises wrt scrolling.
11592 FIXME: How/why/when? */
11593 clear_garbaged_frames ();
11597 /* True if W's buffer was changed but not saved. */
11599 static bool
11600 window_buffer_changed (struct window *w)
11602 struct buffer *b = XBUFFER (w->contents);
11604 eassert (BUFFER_LIVE_P (b));
11606 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11609 /* True if W has %c or %C in its mode line and mode line should be updated. */
11611 static bool
11612 mode_line_update_needed (struct window *w)
11614 return (w->column_number_displayed != -1
11615 && !(PT == w->last_point && !window_outdated (w))
11616 && (w->column_number_displayed != current_column ()));
11619 /* True if window start of W is frozen and may not be changed during
11620 redisplay. */
11622 static bool
11623 window_frozen_p (struct window *w)
11625 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11627 Lisp_Object window;
11629 XSETWINDOW (window, w);
11630 if (MINI_WINDOW_P (w))
11631 return false;
11632 else if (EQ (window, selected_window))
11633 return false;
11634 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11635 && EQ (window, Vminibuf_scroll_window))
11636 /* This special window can't be frozen too. */
11637 return false;
11638 else
11639 return true;
11641 return false;
11644 /***********************************************************************
11645 Mode Lines and Frame Titles
11646 ***********************************************************************/
11648 /* A buffer for constructing non-propertized mode-line strings and
11649 frame titles in it; allocated from the heap in init_xdisp and
11650 resized as needed in store_mode_line_noprop_char. */
11652 static char *mode_line_noprop_buf;
11654 /* The buffer's end, and a current output position in it. */
11656 static char *mode_line_noprop_buf_end;
11657 static char *mode_line_noprop_ptr;
11659 #define MODE_LINE_NOPROP_LEN(start) \
11660 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11662 static enum {
11663 MODE_LINE_DISPLAY = 0,
11664 MODE_LINE_TITLE,
11665 MODE_LINE_NOPROP,
11666 MODE_LINE_STRING
11667 } mode_line_target;
11669 /* Alist that caches the results of :propertize.
11670 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11671 static Lisp_Object mode_line_proptrans_alist;
11673 /* List of strings making up the mode-line. */
11674 static Lisp_Object mode_line_string_list;
11676 /* Base face property when building propertized mode line string. */
11677 static Lisp_Object mode_line_string_face;
11678 static Lisp_Object mode_line_string_face_prop;
11681 /* Unwind data for mode line strings */
11683 static Lisp_Object Vmode_line_unwind_vector;
11685 static Lisp_Object
11686 format_mode_line_unwind_data (struct frame *target_frame,
11687 struct buffer *obuf,
11688 Lisp_Object owin,
11689 bool save_proptrans)
11691 Lisp_Object vector, tmp;
11693 /* Reduce consing by keeping one vector in
11694 Vwith_echo_area_save_vector. */
11695 vector = Vmode_line_unwind_vector;
11696 Vmode_line_unwind_vector = Qnil;
11698 if (NILP (vector))
11699 vector = Fmake_vector (make_number (10), Qnil);
11701 ASET (vector, 0, make_number (mode_line_target));
11702 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11703 ASET (vector, 2, mode_line_string_list);
11704 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11705 ASET (vector, 4, mode_line_string_face);
11706 ASET (vector, 5, mode_line_string_face_prop);
11708 if (obuf)
11709 XSETBUFFER (tmp, obuf);
11710 else
11711 tmp = Qnil;
11712 ASET (vector, 6, tmp);
11713 ASET (vector, 7, owin);
11714 if (target_frame)
11716 /* Similarly to `with-selected-window', if the operation selects
11717 a window on another frame, we must restore that frame's
11718 selected window, and (for a tty) the top-frame. */
11719 ASET (vector, 8, target_frame->selected_window);
11720 if (FRAME_TERMCAP_P (target_frame))
11721 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11724 return vector;
11727 static void
11728 unwind_format_mode_line (Lisp_Object vector)
11730 Lisp_Object old_window = AREF (vector, 7);
11731 Lisp_Object target_frame_window = AREF (vector, 8);
11732 Lisp_Object old_top_frame = AREF (vector, 9);
11734 mode_line_target = XINT (AREF (vector, 0));
11735 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11736 mode_line_string_list = AREF (vector, 2);
11737 if (! EQ (AREF (vector, 3), Qt))
11738 mode_line_proptrans_alist = AREF (vector, 3);
11739 mode_line_string_face = AREF (vector, 4);
11740 mode_line_string_face_prop = AREF (vector, 5);
11742 /* Select window before buffer, since it may change the buffer. */
11743 if (!NILP (old_window))
11745 /* If the operation that we are unwinding had selected a window
11746 on a different frame, reset its frame-selected-window. For a
11747 text terminal, reset its top-frame if necessary. */
11748 if (!NILP (target_frame_window))
11750 Lisp_Object frame
11751 = WINDOW_FRAME (XWINDOW (target_frame_window));
11753 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11754 Fselect_window (target_frame_window, Qt);
11756 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11757 Fselect_frame (old_top_frame, Qt);
11760 Fselect_window (old_window, Qt);
11763 if (!NILP (AREF (vector, 6)))
11765 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11766 ASET (vector, 6, Qnil);
11769 Vmode_line_unwind_vector = vector;
11773 /* Store a single character C for the frame title in mode_line_noprop_buf.
11774 Re-allocate mode_line_noprop_buf if necessary. */
11776 static void
11777 store_mode_line_noprop_char (char c)
11779 /* If output position has reached the end of the allocated buffer,
11780 increase the buffer's size. */
11781 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11783 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11784 ptrdiff_t size = len;
11785 mode_line_noprop_buf =
11786 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11787 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11788 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11791 *mode_line_noprop_ptr++ = c;
11795 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11796 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11797 characters that yield more columns than PRECISION; PRECISION <= 0
11798 means copy the whole string. Pad with spaces until FIELD_WIDTH
11799 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11800 pad. Called from display_mode_element when it is used to build a
11801 frame title. */
11803 static int
11804 store_mode_line_noprop (const char *string, int field_width, int precision)
11806 const unsigned char *str = (const unsigned char *) string;
11807 int n = 0;
11808 ptrdiff_t dummy, nbytes;
11810 /* Copy at most PRECISION chars from STR. */
11811 nbytes = strlen (string);
11812 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11813 while (nbytes--)
11814 store_mode_line_noprop_char (*str++);
11816 /* Fill up with spaces until FIELD_WIDTH reached. */
11817 while (field_width > 0
11818 && n < field_width)
11820 store_mode_line_noprop_char (' ');
11821 ++n;
11824 return n;
11827 /***********************************************************************
11828 Frame Titles
11829 ***********************************************************************/
11831 #ifdef HAVE_WINDOW_SYSTEM
11833 /* Set the title of FRAME, if it has changed. The title format is
11834 Vicon_title_format if FRAME is iconified, otherwise it is
11835 frame_title_format. */
11837 static void
11838 x_consider_frame_title (Lisp_Object frame)
11840 struct frame *f = XFRAME (frame);
11842 if ((FRAME_WINDOW_P (f)
11843 || FRAME_MINIBUF_ONLY_P (f)
11844 || f->explicit_name)
11845 && NILP (Fframe_parameter (frame, Qtooltip)))
11847 /* Do we have more than one visible frame on this X display? */
11848 Lisp_Object tail, other_frame, fmt;
11849 ptrdiff_t title_start;
11850 char *title;
11851 ptrdiff_t len;
11852 struct it it;
11853 ptrdiff_t count = SPECPDL_INDEX ();
11855 FOR_EACH_FRAME (tail, other_frame)
11857 struct frame *tf = XFRAME (other_frame);
11859 if (tf != f
11860 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11861 && !FRAME_MINIBUF_ONLY_P (tf)
11862 && !EQ (other_frame, tip_frame)
11863 && !FRAME_PARENT_FRAME (tf)
11864 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11865 break;
11868 /* Set global variable indicating that multiple frames exist. */
11869 multiple_frames = CONSP (tail);
11871 /* Switch to the buffer of selected window of the frame. Set up
11872 mode_line_target so that display_mode_element will output into
11873 mode_line_noprop_buf; then display the title. */
11874 record_unwind_protect (unwind_format_mode_line,
11875 format_mode_line_unwind_data
11876 (f, current_buffer, selected_window, false));
11877 /* select-frame calls resize_mini_window, which could resize the
11878 mini-window and by that undo the effect of this redisplay
11879 cycle wrt minibuffer and echo-area display. Binding
11880 inhibit-redisplay to t makes the call to resize_mini_window a
11881 no-op, thus avoiding the adverse side effects. */
11882 specbind (Qinhibit_redisplay, Qt);
11884 Fselect_window (f->selected_window, Qt);
11885 set_buffer_internal_1
11886 (XBUFFER (XWINDOW (f->selected_window)->contents));
11887 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11889 mode_line_target = MODE_LINE_TITLE;
11890 title_start = MODE_LINE_NOPROP_LEN (0);
11891 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11892 NULL, DEFAULT_FACE_ID);
11893 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11894 len = MODE_LINE_NOPROP_LEN (title_start);
11895 title = mode_line_noprop_buf + title_start;
11896 unbind_to (count, Qnil);
11898 /* Set the title only if it's changed. This avoids consing in
11899 the common case where it hasn't. (If it turns out that we've
11900 already wasted too much time by walking through the list with
11901 display_mode_element, then we might need to optimize at a
11902 higher level than this.) */
11903 if (! STRINGP (f->name)
11904 || SBYTES (f->name) != len
11905 || memcmp (title, SDATA (f->name), len) != 0)
11906 x_implicitly_set_name (f, make_string (title, len), Qnil);
11910 #endif /* not HAVE_WINDOW_SYSTEM */
11913 /***********************************************************************
11914 Menu Bars
11915 ***********************************************************************/
11917 /* True if we will not redisplay all visible windows. */
11918 #define REDISPLAY_SOME_P() \
11919 ((windows_or_buffers_changed == 0 \
11920 || windows_or_buffers_changed == REDISPLAY_SOME) \
11921 && (update_mode_lines == 0 \
11922 || update_mode_lines == REDISPLAY_SOME))
11924 /* Prepare for redisplay by updating menu-bar item lists when
11925 appropriate. This can call eval. */
11927 static void
11928 prepare_menu_bars (void)
11930 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11931 bool some_windows = REDISPLAY_SOME_P ();
11932 Lisp_Object tooltip_frame;
11934 #ifdef HAVE_WINDOW_SYSTEM
11935 tooltip_frame = tip_frame;
11936 #else
11937 tooltip_frame = Qnil;
11938 #endif
11940 if (FUNCTIONP (Vpre_redisplay_function))
11942 Lisp_Object windows = all_windows ? Qt : Qnil;
11943 if (all_windows && some_windows)
11945 Lisp_Object ws = window_list ();
11946 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11948 Lisp_Object this = XCAR (ws);
11949 struct window *w = XWINDOW (this);
11950 if (w->redisplay
11951 || XFRAME (w->frame)->redisplay
11952 || XBUFFER (w->contents)->text->redisplay)
11954 windows = Fcons (this, windows);
11958 safe__call1 (true, Vpre_redisplay_function, windows);
11961 /* Update all frame titles based on their buffer names, etc. We do
11962 this before the menu bars so that the buffer-menu will show the
11963 up-to-date frame titles. */
11964 #ifdef HAVE_WINDOW_SYSTEM
11965 if (all_windows)
11967 Lisp_Object tail, frame;
11969 FOR_EACH_FRAME (tail, frame)
11971 struct frame *f = XFRAME (frame);
11972 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11973 if (some_windows
11974 && !f->redisplay
11975 && !w->redisplay
11976 && !XBUFFER (w->contents)->text->redisplay)
11977 continue;
11979 if (!EQ (frame, tooltip_frame)
11980 && !FRAME_PARENT_FRAME (f)
11981 && (FRAME_ICONIFIED_P (f)
11982 || FRAME_VISIBLE_P (f) == 1
11983 /* Exclude TTY frames that are obscured because they
11984 are not the top frame on their console. This is
11985 because x_consider_frame_title actually switches
11986 to the frame, which for TTY frames means it is
11987 marked as garbaged, and will be completely
11988 redrawn on the next redisplay cycle. This causes
11989 TTY frames to be completely redrawn, when there
11990 are more than one of them, even though nothing
11991 should be changed on display. */
11992 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11993 x_consider_frame_title (frame);
11996 #endif /* HAVE_WINDOW_SYSTEM */
11998 /* Update the menu bar item lists, if appropriate. This has to be
11999 done before any actual redisplay or generation of display lines. */
12001 if (all_windows)
12003 Lisp_Object tail, frame;
12004 ptrdiff_t count = SPECPDL_INDEX ();
12005 /* True means that update_menu_bar has run its hooks
12006 so any further calls to update_menu_bar shouldn't do so again. */
12007 bool menu_bar_hooks_run = false;
12009 record_unwind_save_match_data ();
12011 FOR_EACH_FRAME (tail, frame)
12013 struct frame *f = XFRAME (frame);
12014 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
12016 /* Ignore tooltip frame. */
12017 if (EQ (frame, tooltip_frame))
12018 continue;
12020 if (some_windows
12021 && !f->redisplay
12022 && !w->redisplay
12023 && !XBUFFER (w->contents)->text->redisplay)
12024 continue;
12026 run_window_size_change_functions (frame);
12028 if (FRAME_PARENT_FRAME (f))
12029 continue;
12031 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
12032 #ifdef HAVE_WINDOW_SYSTEM
12033 update_tool_bar (f, false);
12034 #endif
12037 unbind_to (count, Qnil);
12039 else
12041 struct frame *sf = SELECTED_FRAME ();
12042 update_menu_bar (sf, true, false);
12043 #ifdef HAVE_WINDOW_SYSTEM
12044 update_tool_bar (sf, true);
12045 #endif
12050 /* Update the menu bar item list for frame F. This has to be done
12051 before we start to fill in any display lines, because it can call
12052 eval.
12054 If SAVE_MATCH_DATA, we must save and restore it here.
12056 If HOOKS_RUN, a previous call to update_menu_bar
12057 already ran the menu bar hooks for this redisplay, so there
12058 is no need to run them again. The return value is the
12059 updated value of this flag, to pass to the next call. */
12061 static bool
12062 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
12064 Lisp_Object window;
12065 struct window *w;
12067 /* If called recursively during a menu update, do nothing. This can
12068 happen when, for instance, an activate-menubar-hook causes a
12069 redisplay. */
12070 if (inhibit_menubar_update)
12071 return hooks_run;
12073 window = FRAME_SELECTED_WINDOW (f);
12074 w = XWINDOW (window);
12076 if (FRAME_WINDOW_P (f)
12078 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12079 || defined (HAVE_NS) || defined (USE_GTK)
12080 FRAME_EXTERNAL_MENU_BAR (f)
12081 #else
12082 FRAME_MENU_BAR_LINES (f) > 0
12083 #endif
12084 : FRAME_MENU_BAR_LINES (f) > 0)
12086 /* If the user has switched buffers or windows, we need to
12087 recompute to reflect the new bindings. But we'll
12088 recompute when update_mode_lines is set too; that means
12089 that people can use force-mode-line-update to request
12090 that the menu bar be recomputed. The adverse effect on
12091 the rest of the redisplay algorithm is about the same as
12092 windows_or_buffers_changed anyway. */
12093 if (windows_or_buffers_changed
12094 /* This used to test w->update_mode_line, but we believe
12095 there is no need to recompute the menu in that case. */
12096 || update_mode_lines
12097 || window_buffer_changed (w))
12099 struct buffer *prev = current_buffer;
12100 ptrdiff_t count = SPECPDL_INDEX ();
12102 specbind (Qinhibit_menubar_update, Qt);
12104 set_buffer_internal_1 (XBUFFER (w->contents));
12105 if (save_match_data)
12106 record_unwind_save_match_data ();
12107 if (NILP (Voverriding_local_map_menu_flag))
12109 specbind (Qoverriding_terminal_local_map, Qnil);
12110 specbind (Qoverriding_local_map, Qnil);
12113 if (!hooks_run)
12115 /* Run the Lucid hook. */
12116 safe_run_hooks (Qactivate_menubar_hook);
12118 /* If it has changed current-menubar from previous value,
12119 really recompute the menu-bar from the value. */
12120 if (! NILP (Vlucid_menu_bar_dirty_flag))
12121 call0 (Qrecompute_lucid_menubar);
12123 safe_run_hooks (Qmenu_bar_update_hook);
12125 hooks_run = true;
12128 XSETFRAME (Vmenu_updating_frame, f);
12129 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
12131 /* Redisplay the menu bar in case we changed it. */
12132 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12133 || defined (HAVE_NS) || defined (USE_GTK)
12134 if (FRAME_WINDOW_P (f))
12136 #if defined (HAVE_NS)
12137 /* All frames on Mac OS share the same menubar. So only
12138 the selected frame should be allowed to set it. */
12139 if (f == SELECTED_FRAME ())
12140 #endif
12141 set_frame_menubar (f, false, false);
12143 else
12144 /* On a terminal screen, the menu bar is an ordinary screen
12145 line, and this makes it get updated. */
12146 w->update_mode_line = true;
12147 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12148 /* In the non-toolkit version, the menu bar is an ordinary screen
12149 line, and this makes it get updated. */
12150 w->update_mode_line = true;
12151 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12153 unbind_to (count, Qnil);
12154 set_buffer_internal_1 (prev);
12158 return hooks_run;
12161 /***********************************************************************
12162 Tool-bars
12163 ***********************************************************************/
12165 #ifdef HAVE_WINDOW_SYSTEM
12167 /* Select `frame' temporarily without running all the code in
12168 do_switch_frame.
12169 FIXME: Maybe do_switch_frame should be trimmed down similarly
12170 when `norecord' is set. */
12171 static void
12172 fast_set_selected_frame (Lisp_Object frame)
12174 if (!EQ (selected_frame, frame))
12176 selected_frame = frame;
12177 selected_window = XFRAME (frame)->selected_window;
12181 /* Update the tool-bar item list for frame F. This has to be done
12182 before we start to fill in any display lines. Called from
12183 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
12184 and restore it here. */
12186 static void
12187 update_tool_bar (struct frame *f, bool save_match_data)
12189 #if defined (USE_GTK) || defined (HAVE_NS)
12190 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
12191 #else
12192 bool do_update = (WINDOWP (f->tool_bar_window)
12193 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
12194 #endif
12196 if (do_update)
12198 Lisp_Object window;
12199 struct window *w;
12201 window = FRAME_SELECTED_WINDOW (f);
12202 w = XWINDOW (window);
12204 /* If the user has switched buffers or windows, we need to
12205 recompute to reflect the new bindings. But we'll
12206 recompute when update_mode_lines is set too; that means
12207 that people can use force-mode-line-update to request
12208 that the menu bar be recomputed. The adverse effect on
12209 the rest of the redisplay algorithm is about the same as
12210 windows_or_buffers_changed anyway. */
12211 if (windows_or_buffers_changed
12212 || w->update_mode_line
12213 || update_mode_lines
12214 || window_buffer_changed (w))
12216 struct buffer *prev = current_buffer;
12217 ptrdiff_t count = SPECPDL_INDEX ();
12218 Lisp_Object frame, new_tool_bar;
12219 int new_n_tool_bar;
12221 /* Set current_buffer to the buffer of the selected
12222 window of the frame, so that we get the right local
12223 keymaps. */
12224 set_buffer_internal_1 (XBUFFER (w->contents));
12226 /* Save match data, if we must. */
12227 if (save_match_data)
12228 record_unwind_save_match_data ();
12230 /* Make sure that we don't accidentally use bogus keymaps. */
12231 if (NILP (Voverriding_local_map_menu_flag))
12233 specbind (Qoverriding_terminal_local_map, Qnil);
12234 specbind (Qoverriding_local_map, Qnil);
12237 /* We must temporarily set the selected frame to this frame
12238 before calling tool_bar_items, because the calculation of
12239 the tool-bar keymap uses the selected frame (see
12240 `tool-bar-make-keymap' in tool-bar.el). */
12241 eassert (EQ (selected_window,
12242 /* Since we only explicitly preserve selected_frame,
12243 check that selected_window would be redundant. */
12244 XFRAME (selected_frame)->selected_window));
12245 record_unwind_protect (fast_set_selected_frame, selected_frame);
12246 XSETFRAME (frame, f);
12247 fast_set_selected_frame (frame);
12249 /* Build desired tool-bar items from keymaps. */
12250 new_tool_bar
12251 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12252 &new_n_tool_bar);
12254 /* Redisplay the tool-bar if we changed it. */
12255 if (new_n_tool_bar != f->n_tool_bar_items
12256 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12258 /* Redisplay that happens asynchronously due to an expose event
12259 may access f->tool_bar_items. Make sure we update both
12260 variables within BLOCK_INPUT so no such event interrupts. */
12261 block_input ();
12262 fset_tool_bar_items (f, new_tool_bar);
12263 f->n_tool_bar_items = new_n_tool_bar;
12264 w->update_mode_line = true;
12265 unblock_input ();
12268 unbind_to (count, Qnil);
12269 set_buffer_internal_1 (prev);
12274 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12276 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12277 F's desired tool-bar contents. F->tool_bar_items must have
12278 been set up previously by calling prepare_menu_bars. */
12280 static void
12281 build_desired_tool_bar_string (struct frame *f)
12283 int i, size, size_needed;
12284 Lisp_Object image, plist;
12286 image = plist = Qnil;
12288 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12289 Otherwise, make a new string. */
12291 /* The size of the string we might be able to reuse. */
12292 size = (STRINGP (f->desired_tool_bar_string)
12293 ? SCHARS (f->desired_tool_bar_string)
12294 : 0);
12296 /* We need one space in the string for each image. */
12297 size_needed = f->n_tool_bar_items;
12299 /* Reuse f->desired_tool_bar_string, if possible. */
12300 if (size < size_needed || NILP (f->desired_tool_bar_string))
12301 fset_desired_tool_bar_string
12302 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12303 else
12305 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12306 Fremove_text_properties (make_number (0), make_number (size),
12307 props, f->desired_tool_bar_string);
12310 /* Put a `display' property on the string for the images to display,
12311 put a `menu_item' property on tool-bar items with a value that
12312 is the index of the item in F's tool-bar item vector. */
12313 for (i = 0; i < f->n_tool_bar_items; ++i)
12315 #define PROP(IDX) \
12316 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12318 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12319 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12320 int hmargin, vmargin, relief, idx, end;
12322 /* If image is a vector, choose the image according to the
12323 button state. */
12324 image = PROP (TOOL_BAR_ITEM_IMAGES);
12325 if (VECTORP (image))
12327 if (enabled_p)
12328 idx = (selected_p
12329 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12330 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12331 else
12332 idx = (selected_p
12333 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12334 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12336 eassert (ASIZE (image) >= idx);
12337 image = AREF (image, idx);
12339 else
12340 idx = -1;
12342 /* Ignore invalid image specifications. */
12343 if (!valid_image_p (image))
12344 continue;
12346 /* Display the tool-bar button pressed, or depressed. */
12347 plist = Fcopy_sequence (XCDR (image));
12349 /* Compute margin and relief to draw. */
12350 relief = (tool_bar_button_relief >= 0
12351 ? tool_bar_button_relief
12352 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12353 hmargin = vmargin = relief;
12355 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12356 INT_MAX - max (hmargin, vmargin)))
12358 hmargin += XFASTINT (Vtool_bar_button_margin);
12359 vmargin += XFASTINT (Vtool_bar_button_margin);
12361 else if (CONSP (Vtool_bar_button_margin))
12363 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12364 INT_MAX - hmargin))
12365 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12367 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12368 INT_MAX - vmargin))
12369 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12372 if (auto_raise_tool_bar_buttons_p)
12374 /* Add a `:relief' property to the image spec if the item is
12375 selected. */
12376 if (selected_p)
12378 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12379 hmargin -= relief;
12380 vmargin -= relief;
12383 else
12385 /* If image is selected, display it pressed, i.e. with a
12386 negative relief. If it's not selected, display it with a
12387 raised relief. */
12388 plist = Fplist_put (plist, QCrelief,
12389 (selected_p
12390 ? make_number (-relief)
12391 : make_number (relief)));
12392 hmargin -= relief;
12393 vmargin -= relief;
12396 /* Put a margin around the image. */
12397 if (hmargin || vmargin)
12399 if (hmargin == vmargin)
12400 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12401 else
12402 plist = Fplist_put (plist, QCmargin,
12403 Fcons (make_number (hmargin),
12404 make_number (vmargin)));
12407 /* If button is not enabled, and we don't have special images
12408 for the disabled state, make the image appear disabled by
12409 applying an appropriate algorithm to it. */
12410 if (!enabled_p && idx < 0)
12411 plist = Fplist_put (plist, QCconversion, Qdisabled);
12413 /* Put a `display' text property on the string for the image to
12414 display. Put a `menu-item' property on the string that gives
12415 the start of this item's properties in the tool-bar items
12416 vector. */
12417 image = Fcons (Qimage, plist);
12418 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12419 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12421 /* Let the last image hide all remaining spaces in the tool bar
12422 string. The string can be longer than needed when we reuse a
12423 previous string. */
12424 if (i + 1 == f->n_tool_bar_items)
12425 end = SCHARS (f->desired_tool_bar_string);
12426 else
12427 end = i + 1;
12428 Fadd_text_properties (make_number (i), make_number (end),
12429 props, f->desired_tool_bar_string);
12430 #undef PROP
12435 /* Display one line of the tool-bar of frame IT->f.
12437 HEIGHT specifies the desired height of the tool-bar line.
12438 If the actual height of the glyph row is less than HEIGHT, the
12439 row's height is increased to HEIGHT, and the icons are centered
12440 vertically in the new height.
12442 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12443 count a final empty row in case the tool-bar width exactly matches
12444 the window width.
12447 static void
12448 display_tool_bar_line (struct it *it, int height)
12450 struct glyph_row *row = it->glyph_row;
12451 int max_x = it->last_visible_x;
12452 struct glyph *last;
12454 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12455 clear_glyph_row (row);
12456 row->enabled_p = true;
12457 row->y = it->current_y;
12459 /* Note that this isn't made use of if the face hasn't a box,
12460 so there's no need to check the face here. */
12461 it->start_of_box_run_p = true;
12463 while (it->current_x < max_x)
12465 int x, n_glyphs_before, i, nglyphs;
12466 struct it it_before;
12468 /* Get the next display element. */
12469 if (!get_next_display_element (it))
12471 /* Don't count empty row if we are counting needed tool-bar lines. */
12472 if (height < 0 && !it->hpos)
12473 return;
12474 break;
12477 /* Produce glyphs. */
12478 n_glyphs_before = row->used[TEXT_AREA];
12479 it_before = *it;
12481 PRODUCE_GLYPHS (it);
12483 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12484 i = 0;
12485 x = it_before.current_x;
12486 while (i < nglyphs)
12488 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12490 if (x + glyph->pixel_width > max_x)
12492 /* Glyph doesn't fit on line. Backtrack. */
12493 row->used[TEXT_AREA] = n_glyphs_before;
12494 *it = it_before;
12495 /* If this is the only glyph on this line, it will never fit on the
12496 tool-bar, so skip it. But ensure there is at least one glyph,
12497 so we don't accidentally disable the tool-bar. */
12498 if (n_glyphs_before == 0
12499 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12500 break;
12501 goto out;
12504 ++it->hpos;
12505 x += glyph->pixel_width;
12506 ++i;
12509 /* Stop at line end. */
12510 if (ITERATOR_AT_END_OF_LINE_P (it))
12511 break;
12513 set_iterator_to_next (it, true);
12516 out:;
12518 row->displays_text_p = row->used[TEXT_AREA] != 0;
12520 /* Use default face for the border below the tool bar.
12522 FIXME: When auto-resize-tool-bars is grow-only, there is
12523 no additional border below the possibly empty tool-bar lines.
12524 So to make the extra empty lines look "normal", we have to
12525 use the tool-bar face for the border too. */
12526 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12527 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12528 it->face_id = DEFAULT_FACE_ID;
12530 extend_face_to_end_of_line (it);
12531 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12532 last->right_box_line_p = true;
12533 if (last == row->glyphs[TEXT_AREA])
12534 last->left_box_line_p = true;
12536 /* Make line the desired height and center it vertically. */
12537 if ((height -= it->max_ascent + it->max_descent) > 0)
12539 /* Don't add more than one line height. */
12540 height %= FRAME_LINE_HEIGHT (it->f);
12541 it->max_ascent += height / 2;
12542 it->max_descent += (height + 1) / 2;
12545 compute_line_metrics (it);
12547 /* If line is empty, make it occupy the rest of the tool-bar. */
12548 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12550 row->height = row->phys_height = it->last_visible_y - row->y;
12551 row->visible_height = row->height;
12552 row->ascent = row->phys_ascent = 0;
12553 row->extra_line_spacing = 0;
12556 row->full_width_p = true;
12557 row->continued_p = false;
12558 row->truncated_on_left_p = false;
12559 row->truncated_on_right_p = false;
12561 it->current_x = it->hpos = 0;
12562 it->current_y += row->height;
12563 ++it->vpos;
12564 ++it->glyph_row;
12568 /* Value is the number of pixels needed to make all tool-bar items of
12569 frame F visible. The actual number of glyph rows needed is
12570 returned in *N_ROWS if non-NULL. */
12571 static int
12572 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12574 struct window *w = XWINDOW (f->tool_bar_window);
12575 struct it it;
12576 /* tool_bar_height is called from redisplay_tool_bar after building
12577 the desired matrix, so use (unused) mode-line row as temporary row to
12578 avoid destroying the first tool-bar row. */
12579 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12581 /* Initialize an iterator for iteration over
12582 F->desired_tool_bar_string in the tool-bar window of frame F. */
12583 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12584 temp_row->reversed_p = false;
12585 it.first_visible_x = 0;
12586 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12587 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12588 it.paragraph_embedding = L2R;
12590 while (!ITERATOR_AT_END_P (&it))
12592 clear_glyph_row (temp_row);
12593 it.glyph_row = temp_row;
12594 display_tool_bar_line (&it, -1);
12596 clear_glyph_row (temp_row);
12598 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12599 if (n_rows)
12600 *n_rows = it.vpos > 0 ? it.vpos : -1;
12602 if (pixelwise)
12603 return it.current_y;
12604 else
12605 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12608 #endif /* !USE_GTK && !HAVE_NS */
12610 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12611 0, 2, 0,
12612 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12613 If FRAME is nil or omitted, use the selected frame. Optional argument
12614 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12615 (Lisp_Object frame, Lisp_Object pixelwise)
12617 int height = 0;
12619 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12620 struct frame *f = decode_any_frame (frame);
12622 if (WINDOWP (f->tool_bar_window)
12623 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12625 update_tool_bar (f, true);
12626 if (f->n_tool_bar_items)
12628 build_desired_tool_bar_string (f);
12629 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12632 #endif
12634 return make_number (height);
12638 /* Display the tool-bar of frame F. Value is true if tool-bar's
12639 height should be changed. */
12640 static bool
12641 redisplay_tool_bar (struct frame *f)
12643 f->tool_bar_redisplayed = true;
12644 #if defined (USE_GTK) || defined (HAVE_NS)
12646 if (FRAME_EXTERNAL_TOOL_BAR (f))
12647 update_frame_tool_bar (f);
12648 return false;
12650 #else /* !USE_GTK && !HAVE_NS */
12652 struct window *w;
12653 struct it it;
12654 struct glyph_row *row;
12656 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12657 do anything. This means you must start with tool-bar-lines
12658 non-zero to get the auto-sizing effect. Or in other words, you
12659 can turn off tool-bars by specifying tool-bar-lines zero. */
12660 if (!WINDOWP (f->tool_bar_window)
12661 || (w = XWINDOW (f->tool_bar_window),
12662 WINDOW_TOTAL_LINES (w) == 0))
12663 return false;
12665 /* Set up an iterator for the tool-bar window. */
12666 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12667 it.first_visible_x = 0;
12668 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12669 row = it.glyph_row;
12670 row->reversed_p = false;
12672 /* Build a string that represents the contents of the tool-bar. */
12673 build_desired_tool_bar_string (f);
12674 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12675 /* FIXME: This should be controlled by a user option. But it
12676 doesn't make sense to have an R2L tool bar if the menu bar cannot
12677 be drawn also R2L, and making the menu bar R2L is tricky due
12678 toolkit-specific code that implements it. If an R2L tool bar is
12679 ever supported, display_tool_bar_line should also be augmented to
12680 call unproduce_glyphs like display_line and display_string
12681 do. */
12682 it.paragraph_embedding = L2R;
12684 if (f->n_tool_bar_rows == 0)
12686 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12688 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12690 x_change_tool_bar_height (f, new_height);
12691 frame_default_tool_bar_height = new_height;
12692 /* Always do that now. */
12693 clear_glyph_matrix (w->desired_matrix);
12694 f->fonts_changed = true;
12695 return true;
12699 /* Display as many lines as needed to display all tool-bar items. */
12701 if (f->n_tool_bar_rows > 0)
12703 int border, rows, height, extra;
12705 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12706 border = XINT (Vtool_bar_border);
12707 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12708 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12709 else if (EQ (Vtool_bar_border, Qborder_width))
12710 border = f->border_width;
12711 else
12712 border = 0;
12713 if (border < 0)
12714 border = 0;
12716 rows = f->n_tool_bar_rows;
12717 height = max (1, (it.last_visible_y - border) / rows);
12718 extra = it.last_visible_y - border - height * rows;
12720 while (it.current_y < it.last_visible_y)
12722 int h = 0;
12723 if (extra > 0 && rows-- > 0)
12725 h = (extra + rows - 1) / rows;
12726 extra -= h;
12728 display_tool_bar_line (&it, height + h);
12731 else
12733 while (it.current_y < it.last_visible_y)
12734 display_tool_bar_line (&it, 0);
12737 /* It doesn't make much sense to try scrolling in the tool-bar
12738 window, so don't do it. */
12739 w->desired_matrix->no_scrolling_p = true;
12740 w->must_be_updated_p = true;
12742 if (!NILP (Vauto_resize_tool_bars))
12744 bool change_height_p = true;
12746 /* If we couldn't display everything, change the tool-bar's
12747 height if there is room for more. */
12748 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12749 change_height_p = true;
12751 /* We subtract 1 because display_tool_bar_line advances the
12752 glyph_row pointer before returning to its caller. We want to
12753 examine the last glyph row produced by
12754 display_tool_bar_line. */
12755 row = it.glyph_row - 1;
12757 /* If there are blank lines at the end, except for a partially
12758 visible blank line at the end that is smaller than
12759 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12760 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12761 && row->height >= FRAME_LINE_HEIGHT (f))
12762 change_height_p = true;
12764 /* If row displays tool-bar items, but is partially visible,
12765 change the tool-bar's height. */
12766 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12767 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12768 change_height_p = true;
12770 /* Resize windows as needed by changing the `tool-bar-lines'
12771 frame parameter. */
12772 if (change_height_p)
12774 int nrows;
12775 int new_height = tool_bar_height (f, &nrows, true);
12777 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12778 && !f->minimize_tool_bar_window_p)
12779 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12780 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12781 f->minimize_tool_bar_window_p = false;
12783 if (change_height_p)
12785 x_change_tool_bar_height (f, new_height);
12786 frame_default_tool_bar_height = new_height;
12787 clear_glyph_matrix (w->desired_matrix);
12788 f->n_tool_bar_rows = nrows;
12789 f->fonts_changed = true;
12791 return true;
12796 f->minimize_tool_bar_window_p = false;
12797 return false;
12799 #endif /* USE_GTK || HAVE_NS */
12802 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12804 /* Get information about the tool-bar item which is displayed in GLYPH
12805 on frame F. Return in *PROP_IDX the index where tool-bar item
12806 properties start in F->tool_bar_items. Value is false if
12807 GLYPH doesn't display a tool-bar item. */
12809 static bool
12810 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12812 Lisp_Object prop;
12813 int charpos;
12815 /* This function can be called asynchronously, which means we must
12816 exclude any possibility that Fget_text_property signals an
12817 error. */
12818 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12819 charpos = max (0, charpos);
12821 /* Get the text property `menu-item' at pos. The value of that
12822 property is the start index of this item's properties in
12823 F->tool_bar_items. */
12824 prop = Fget_text_property (make_number (charpos),
12825 Qmenu_item, f->current_tool_bar_string);
12826 if (! INTEGERP (prop))
12827 return false;
12828 *prop_idx = XINT (prop);
12829 return true;
12833 /* Get information about the tool-bar item at position X/Y on frame F.
12834 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12835 the current matrix of the tool-bar window of F, or NULL if not
12836 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12837 item in F->tool_bar_items. Value is
12839 -1 if X/Y is not on a tool-bar item
12840 0 if X/Y is on the same item that was highlighted before.
12841 1 otherwise. */
12843 static int
12844 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12845 int *hpos, int *vpos, int *prop_idx)
12847 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12848 struct window *w = XWINDOW (f->tool_bar_window);
12849 int area;
12851 /* Find the glyph under X/Y. */
12852 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12853 if (*glyph == NULL)
12854 return -1;
12856 /* Get the start of this tool-bar item's properties in
12857 f->tool_bar_items. */
12858 if (!tool_bar_item_info (f, *glyph, prop_idx))
12859 return -1;
12861 /* Is mouse on the highlighted item? */
12862 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12863 && *vpos >= hlinfo->mouse_face_beg_row
12864 && *vpos <= hlinfo->mouse_face_end_row
12865 && (*vpos > hlinfo->mouse_face_beg_row
12866 || *hpos >= hlinfo->mouse_face_beg_col)
12867 && (*vpos < hlinfo->mouse_face_end_row
12868 || *hpos < hlinfo->mouse_face_end_col
12869 || hlinfo->mouse_face_past_end))
12870 return 0;
12872 return 1;
12876 /* EXPORT:
12877 Handle mouse button event on the tool-bar of frame F, at
12878 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12879 false for button release. MODIFIERS is event modifiers for button
12880 release. */
12882 void
12883 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12884 int modifiers)
12886 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12887 struct window *w = XWINDOW (f->tool_bar_window);
12888 int hpos, vpos, prop_idx;
12889 struct glyph *glyph;
12890 Lisp_Object enabled_p;
12891 int ts;
12893 /* If not on the highlighted tool-bar item, and mouse-highlight is
12894 non-nil, return. This is so we generate the tool-bar button
12895 click only when the mouse button is released on the same item as
12896 where it was pressed. However, when mouse-highlight is disabled,
12897 generate the click when the button is released regardless of the
12898 highlight, since tool-bar items are not highlighted in that
12899 case. */
12900 frame_to_window_pixel_xy (w, &x, &y);
12901 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12902 if (ts == -1
12903 || (ts != 0 && !NILP (Vmouse_highlight)))
12904 return;
12906 /* When mouse-highlight is off, generate the click for the item
12907 where the button was pressed, disregarding where it was
12908 released. */
12909 if (NILP (Vmouse_highlight) && !down_p)
12910 prop_idx = f->last_tool_bar_item;
12912 /* If item is disabled, do nothing. */
12913 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12914 if (NILP (enabled_p))
12915 return;
12917 if (down_p)
12919 /* Show item in pressed state. */
12920 if (!NILP (Vmouse_highlight))
12921 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12922 f->last_tool_bar_item = prop_idx;
12924 else
12926 Lisp_Object key, frame;
12927 struct input_event event;
12928 EVENT_INIT (event);
12930 /* Show item in released state. */
12931 if (!NILP (Vmouse_highlight))
12932 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12934 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12936 XSETFRAME (frame, f);
12937 event.kind = TOOL_BAR_EVENT;
12938 event.frame_or_window = frame;
12939 event.arg = frame;
12940 kbd_buffer_store_event (&event);
12942 event.kind = TOOL_BAR_EVENT;
12943 event.frame_or_window = frame;
12944 event.arg = key;
12945 event.modifiers = modifiers;
12946 kbd_buffer_store_event (&event);
12947 f->last_tool_bar_item = -1;
12952 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12953 tool-bar window-relative coordinates X/Y. Called from
12954 note_mouse_highlight. */
12956 static void
12957 note_tool_bar_highlight (struct frame *f, int x, int y)
12959 Lisp_Object window = f->tool_bar_window;
12960 struct window *w = XWINDOW (window);
12961 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12962 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12963 int hpos, vpos;
12964 struct glyph *glyph;
12965 struct glyph_row *row;
12966 int i;
12967 Lisp_Object enabled_p;
12968 int prop_idx;
12969 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12970 bool mouse_down_p;
12971 int rc;
12973 /* Function note_mouse_highlight is called with negative X/Y
12974 values when mouse moves outside of the frame. */
12975 if (x <= 0 || y <= 0)
12977 clear_mouse_face (hlinfo);
12978 return;
12981 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12982 if (rc < 0)
12984 /* Not on tool-bar item. */
12985 clear_mouse_face (hlinfo);
12986 return;
12988 else if (rc == 0)
12989 /* On same tool-bar item as before. */
12990 goto set_help_echo;
12992 clear_mouse_face (hlinfo);
12994 /* Mouse is down, but on different tool-bar item? */
12995 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12996 && f == dpyinfo->last_mouse_frame);
12998 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12999 return;
13001 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
13003 /* If tool-bar item is not enabled, don't highlight it. */
13004 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
13005 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
13007 /* Compute the x-position of the glyph. In front and past the
13008 image is a space. We include this in the highlighted area. */
13009 row = MATRIX_ROW (w->current_matrix, vpos);
13010 for (i = x = 0; i < hpos; ++i)
13011 x += row->glyphs[TEXT_AREA][i].pixel_width;
13013 /* Record this as the current active region. */
13014 hlinfo->mouse_face_beg_col = hpos;
13015 hlinfo->mouse_face_beg_row = vpos;
13016 hlinfo->mouse_face_beg_x = x;
13017 hlinfo->mouse_face_past_end = false;
13019 hlinfo->mouse_face_end_col = hpos + 1;
13020 hlinfo->mouse_face_end_row = vpos;
13021 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
13022 hlinfo->mouse_face_window = window;
13023 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
13025 /* Display it as active. */
13026 show_mouse_face (hlinfo, draw);
13029 set_help_echo:
13031 /* Set help_echo_string to a help string to display for this tool-bar item.
13032 XTread_socket does the rest. */
13033 help_echo_object = help_echo_window = Qnil;
13034 help_echo_pos = -1;
13035 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
13036 if (NILP (help_echo_string))
13037 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
13040 #endif /* !USE_GTK && !HAVE_NS */
13042 #endif /* HAVE_WINDOW_SYSTEM */
13046 /************************************************************************
13047 Horizontal scrolling
13048 ************************************************************************/
13050 /* For all leaf windows in the window tree rooted at WINDOW, set their
13051 hscroll value so that PT is (i) visible in the window, and (ii) so
13052 that it is not within a certain margin at the window's left and
13053 right border. Value is true if any window's hscroll has been
13054 changed. */
13056 static bool
13057 hscroll_window_tree (Lisp_Object window)
13059 bool hscrolled_p = false;
13060 bool hscroll_relative_p = FLOATP (Vhscroll_step);
13061 int hscroll_step_abs = 0;
13062 double hscroll_step_rel = 0;
13064 if (hscroll_relative_p)
13066 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
13067 if (hscroll_step_rel < 0)
13069 hscroll_relative_p = false;
13070 hscroll_step_abs = 0;
13073 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
13075 hscroll_step_abs = XINT (Vhscroll_step);
13076 if (hscroll_step_abs < 0)
13077 hscroll_step_abs = 0;
13079 else
13080 hscroll_step_abs = 0;
13082 while (WINDOWP (window))
13084 struct window *w = XWINDOW (window);
13086 if (WINDOWP (w->contents))
13087 hscrolled_p |= hscroll_window_tree (w->contents);
13088 else if (w->cursor.vpos >= 0)
13090 int h_margin;
13091 int text_area_width;
13092 struct glyph_row *cursor_row;
13093 struct glyph_row *bottom_row;
13095 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
13096 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
13097 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
13098 else
13099 cursor_row = bottom_row - 1;
13101 if (!cursor_row->enabled_p)
13103 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13104 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
13105 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13106 else
13107 cursor_row = bottom_row - 1;
13109 bool row_r2l_p = cursor_row->reversed_p;
13110 bool hscl = hscrolling_current_line_p (w);
13111 int x_offset = 0;
13112 /* When line numbers are displayed, we need to account for
13113 the horizontal space they consume. */
13114 if (!NILP (Vdisplay_line_numbers))
13116 struct glyph *g;
13117 if (!row_r2l_p)
13119 for (g = cursor_row->glyphs[TEXT_AREA];
13120 g < cursor_row->glyphs[TEXT_AREA]
13121 + cursor_row->used[TEXT_AREA];
13122 g++)
13124 if (!(NILP (g->object) && g->charpos < 0))
13125 break;
13126 x_offset += g->pixel_width;
13129 else
13131 for (g = cursor_row->glyphs[TEXT_AREA]
13132 + cursor_row->used[TEXT_AREA];
13133 g > cursor_row->glyphs[TEXT_AREA];
13134 g--)
13136 if (!(NILP ((g - 1)->object) && (g - 1)->charpos < 0))
13137 break;
13138 x_offset += (g - 1)->pixel_width;
13142 if (cursor_row->truncated_on_left_p)
13144 /* On TTY frames, don't count the left truncation glyph. */
13145 struct frame *f = XFRAME (WINDOW_FRAME (w));
13146 x_offset -= (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f));
13149 text_area_width = window_box_width (w, TEXT_AREA);
13151 /* Scroll when cursor is inside this scroll margin. */
13152 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
13154 /* If the position of this window's point has explicitly
13155 changed, no more suspend auto hscrolling. */
13156 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
13157 w->suspend_auto_hscroll = false;
13159 /* Remember window point. */
13160 Fset_marker (w->old_pointm,
13161 ((w == XWINDOW (selected_window))
13162 ? make_number (BUF_PT (XBUFFER (w->contents)))
13163 : Fmarker_position (w->pointm)),
13164 w->contents);
13166 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
13167 && !w->suspend_auto_hscroll
13168 /* In some pathological cases, like restoring a window
13169 configuration into a frame that is much smaller than
13170 the one from which the configuration was saved, we
13171 get glyph rows whose start and end have zero buffer
13172 positions, which we cannot handle below. Just skip
13173 such windows. */
13174 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
13175 /* For left-to-right rows, hscroll when cursor is either
13176 (i) inside the right hscroll margin, or (ii) if it is
13177 inside the left margin and the window is already
13178 hscrolled. */
13179 && ((!row_r2l_p
13180 && ((w->hscroll && w->cursor.x <= h_margin + x_offset)
13181 || (cursor_row->enabled_p
13182 && cursor_row->truncated_on_right_p
13183 && (w->cursor.x >= text_area_width - h_margin))))
13184 /* For right-to-left rows, the logic is similar,
13185 except that rules for scrolling to left and right
13186 are reversed. E.g., if cursor.x <= h_margin, we
13187 need to hscroll "to the right" unconditionally,
13188 and that will scroll the screen to the left so as
13189 to reveal the next portion of the row. */
13190 || (row_r2l_p
13191 && ((cursor_row->enabled_p
13192 /* FIXME: It is confusing to set the
13193 truncated_on_right_p flag when R2L rows
13194 are actually truncated on the left. */
13195 && cursor_row->truncated_on_right_p
13196 && w->cursor.x <= h_margin)
13197 || (w->hscroll
13198 && (w->cursor.x >= (text_area_width - h_margin
13199 - x_offset)))))
13200 /* This last condition is needed when moving
13201 vertically from an hscrolled line to a short line
13202 that doesn't need to be hscrolled. If we omit
13203 this condition, the line from which we move will
13204 remain hscrolled. */
13205 || (hscl
13206 && w->hscroll != w->min_hscroll
13207 && !cursor_row->truncated_on_left_p)))
13209 struct it it;
13210 ptrdiff_t hscroll;
13211 struct buffer *saved_current_buffer;
13212 ptrdiff_t pt;
13213 int wanted_x;
13215 /* Find point in a display of infinite width. */
13216 saved_current_buffer = current_buffer;
13217 current_buffer = XBUFFER (w->contents);
13219 if (w == XWINDOW (selected_window))
13220 pt = PT;
13221 else
13222 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
13224 /* Move iterator to pt starting at cursor_row->start in
13225 a line with infinite width. */
13226 init_to_row_start (&it, w, cursor_row);
13227 if (hscl)
13228 it.first_visible_x = window_hscroll_limited (w, it.f)
13229 * FRAME_COLUMN_WIDTH (it.f);
13230 it.last_visible_x = DISP_INFINITY;
13231 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
13232 /* If the line ends in an overlay string with a newline,
13233 we might infloop, because displaying the window will
13234 want to put the cursor after the overlay, i.e. at X
13235 coordinate of zero on the next screen line. So we
13236 use the buffer position prior to the overlay string
13237 instead. */
13238 if (it.method == GET_FROM_STRING && pt > 1)
13240 init_to_row_start (&it, w, cursor_row);
13241 if (hscl)
13242 it.first_visible_x = (window_hscroll_limited (w, it.f)
13243 * FRAME_COLUMN_WIDTH (it.f));
13244 move_it_in_display_line_to (&it, pt - 1, -1, MOVE_TO_POS);
13246 current_buffer = saved_current_buffer;
13248 /* Position cursor in window. */
13249 if (!hscroll_relative_p && hscroll_step_abs == 0)
13250 hscroll = max (0, (it.current_x
13251 - (ITERATOR_AT_END_OF_LINE_P (&it)
13252 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13253 : (text_area_width / 2))))
13254 / FRAME_COLUMN_WIDTH (it.f);
13255 else if ((!row_r2l_p
13256 && w->cursor.x >= text_area_width - h_margin)
13257 || (row_r2l_p && w->cursor.x <= h_margin))
13259 if (hscroll_relative_p)
13260 wanted_x = text_area_width * (1 - hscroll_step_rel)
13261 - h_margin;
13262 else
13263 wanted_x = text_area_width
13264 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13265 - h_margin;
13266 hscroll
13267 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13269 else
13271 if (hscroll_relative_p)
13272 wanted_x = text_area_width * hscroll_step_rel
13273 + h_margin;
13274 else
13275 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13276 + h_margin;
13277 hscroll
13278 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13280 hscroll = max (hscroll, w->min_hscroll);
13282 /* Don't prevent redisplay optimizations if hscroll
13283 hasn't changed, as it will unnecessarily slow down
13284 redisplay. */
13285 if (w->hscroll != hscroll
13286 /* When hscrolling only the current line, we need to
13287 report hscroll even if its value is equal to the
13288 previous one, because the new line might need a
13289 different value. */
13290 || (hscl && w->last_cursor_vpos != w->cursor.vpos))
13292 struct buffer *b = XBUFFER (w->contents);
13293 b->prevent_redisplay_optimizations_p = true;
13294 w->hscroll = hscroll;
13295 hscrolled_p = true;
13300 window = w->next;
13303 /* Value is true if hscroll of any leaf window has been changed. */
13304 return hscrolled_p;
13308 /* Set hscroll so that cursor is visible and not inside horizontal
13309 scroll margins for all windows in the tree rooted at WINDOW. See
13310 also hscroll_window_tree above. Value is true if any window's
13311 hscroll has been changed. If it has, desired matrices on the frame
13312 of WINDOW are cleared. */
13314 static bool
13315 hscroll_windows (Lisp_Object window)
13317 bool hscrolled_p = hscroll_window_tree (window);
13318 if (hscrolled_p)
13319 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13320 return hscrolled_p;
13325 /************************************************************************
13326 Redisplay
13327 ************************************************************************/
13329 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13330 This is sometimes handy to have in a debugger session. */
13332 #ifdef GLYPH_DEBUG
13334 /* First and last unchanged row for try_window_id. */
13336 static int debug_first_unchanged_at_end_vpos;
13337 static int debug_last_unchanged_at_beg_vpos;
13339 /* Delta vpos and y. */
13341 static int debug_dvpos, debug_dy;
13343 /* Delta in characters and bytes for try_window_id. */
13345 static ptrdiff_t debug_delta, debug_delta_bytes;
13347 /* Values of window_end_pos and window_end_vpos at the end of
13348 try_window_id. */
13350 static ptrdiff_t debug_end_vpos;
13352 /* Append a string to W->desired_matrix->method. FMT is a printf
13353 format string. If trace_redisplay_p is true also printf the
13354 resulting string to stderr. */
13356 static void debug_method_add (struct window *, char const *, ...)
13357 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13359 static void
13360 debug_method_add (struct window *w, char const *fmt, ...)
13362 void *ptr = w;
13363 char *method = w->desired_matrix->method;
13364 int len = strlen (method);
13365 int size = sizeof w->desired_matrix->method;
13366 int remaining = size - len - 1;
13367 va_list ap;
13369 if (len && remaining)
13371 method[len] = '|';
13372 --remaining, ++len;
13375 va_start (ap, fmt);
13376 vsnprintf (method + len, remaining + 1, fmt, ap);
13377 va_end (ap);
13379 if (trace_redisplay_p)
13380 fprintf (stderr, "%p (%s): %s\n",
13381 ptr,
13382 ((BUFFERP (w->contents)
13383 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13384 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13385 : "no buffer"),
13386 method + len);
13389 #endif /* GLYPH_DEBUG */
13392 /* Value is true if all changes in window W, which displays
13393 current_buffer, are in the text between START and END. START is a
13394 buffer position, END is given as a distance from Z. Used in
13395 redisplay_internal for display optimization. */
13397 static bool
13398 text_outside_line_unchanged_p (struct window *w,
13399 ptrdiff_t start, ptrdiff_t end)
13401 bool unchanged_p = true;
13403 /* If text or overlays have changed, see where. */
13404 if (window_outdated (w))
13406 /* Gap in the line? */
13407 if (GPT < start || Z - GPT < end)
13408 unchanged_p = false;
13410 /* Changes start in front of the line, or end after it? */
13411 if (unchanged_p
13412 && (BEG_UNCHANGED < start - 1
13413 || END_UNCHANGED < end))
13414 unchanged_p = false;
13416 /* If selective display, can't optimize if changes start at the
13417 beginning of the line. */
13418 if (unchanged_p
13419 && INTEGERP (BVAR (current_buffer, selective_display))
13420 && XINT (BVAR (current_buffer, selective_display)) > 0
13421 && (BEG_UNCHANGED < start || GPT <= start))
13422 unchanged_p = false;
13424 /* If there are overlays at the start or end of the line, these
13425 may have overlay strings with newlines in them. A change at
13426 START, for instance, may actually concern the display of such
13427 overlay strings as well, and they are displayed on different
13428 lines. So, quickly rule out this case. (For the future, it
13429 might be desirable to implement something more telling than
13430 just BEG/END_UNCHANGED.) */
13431 if (unchanged_p)
13433 if (BEG + BEG_UNCHANGED == start
13434 && overlay_touches_p (start))
13435 unchanged_p = false;
13436 if (END_UNCHANGED == end
13437 && overlay_touches_p (Z - end))
13438 unchanged_p = false;
13441 /* Under bidi reordering, adding or deleting a character in the
13442 beginning of a paragraph, before the first strong directional
13443 character, can change the base direction of the paragraph (unless
13444 the buffer specifies a fixed paragraph direction), which will
13445 require redisplaying the whole paragraph. It might be worthwhile
13446 to find the paragraph limits and widen the range of redisplayed
13447 lines to that, but for now just give up this optimization. */
13448 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13449 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13450 unchanged_p = false;
13453 return unchanged_p;
13457 /* Do a frame update, taking possible shortcuts into account. This is
13458 the main external entry point for redisplay.
13460 If the last redisplay displayed an echo area message and that message
13461 is no longer requested, we clear the echo area or bring back the
13462 mini-buffer if that is in use. */
13464 void
13465 redisplay (void)
13467 redisplay_internal ();
13471 static Lisp_Object
13472 overlay_arrow_string_or_property (Lisp_Object var)
13474 Lisp_Object val;
13476 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13477 return val;
13479 return Voverlay_arrow_string;
13482 /* Return true if there are any overlay-arrows in current_buffer. */
13483 static bool
13484 overlay_arrow_in_current_buffer_p (void)
13486 Lisp_Object vlist;
13488 for (vlist = Voverlay_arrow_variable_list;
13489 CONSP (vlist);
13490 vlist = XCDR (vlist))
13492 Lisp_Object var = XCAR (vlist);
13493 Lisp_Object val;
13495 if (!SYMBOLP (var))
13496 continue;
13497 val = find_symbol_value (var);
13498 if (MARKERP (val)
13499 && current_buffer == XMARKER (val)->buffer)
13500 return true;
13502 return false;
13506 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13507 has changed.
13508 If SET_REDISPLAY is true, additionally, set the `redisplay' bit in those
13509 buffers that are affected. */
13511 static bool
13512 overlay_arrows_changed_p (bool set_redisplay)
13514 Lisp_Object vlist;
13515 bool changed = false;
13517 for (vlist = Voverlay_arrow_variable_list;
13518 CONSP (vlist);
13519 vlist = XCDR (vlist))
13521 Lisp_Object var = XCAR (vlist);
13522 Lisp_Object val, pstr;
13524 if (!SYMBOLP (var))
13525 continue;
13526 val = find_symbol_value (var);
13527 if (!MARKERP (val))
13528 continue;
13529 if (! EQ (COERCE_MARKER (val),
13530 /* FIXME: Don't we have a problem, using such a global
13531 * "last-position" if the variable is buffer-local? */
13532 Fget (var, Qlast_arrow_position))
13533 || ! (pstr = overlay_arrow_string_or_property (var),
13534 EQ (pstr, Fget (var, Qlast_arrow_string))))
13536 struct buffer *buf = XMARKER (val)->buffer;
13538 if (set_redisplay)
13540 if (buf)
13541 bset_redisplay (buf);
13542 changed = true;
13544 else
13545 return true;
13548 return changed;
13551 /* Mark overlay arrows to be updated on next redisplay. */
13553 static void
13554 update_overlay_arrows (int up_to_date)
13556 Lisp_Object vlist;
13558 for (vlist = Voverlay_arrow_variable_list;
13559 CONSP (vlist);
13560 vlist = XCDR (vlist))
13562 Lisp_Object var = XCAR (vlist);
13564 if (!SYMBOLP (var))
13565 continue;
13567 if (up_to_date > 0)
13569 Lisp_Object val = find_symbol_value (var);
13570 if (!MARKERP (val))
13571 continue;
13572 Fput (var, Qlast_arrow_position,
13573 COERCE_MARKER (val));
13574 Fput (var, Qlast_arrow_string,
13575 overlay_arrow_string_or_property (var));
13577 else if (up_to_date < 0
13578 || !NILP (Fget (var, Qlast_arrow_position)))
13580 Fput (var, Qlast_arrow_position, Qt);
13581 Fput (var, Qlast_arrow_string, Qt);
13587 /* Return overlay arrow string to display at row.
13588 Return integer (bitmap number) for arrow bitmap in left fringe.
13589 Return nil if no overlay arrow. */
13591 static Lisp_Object
13592 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13594 Lisp_Object vlist;
13596 for (vlist = Voverlay_arrow_variable_list;
13597 CONSP (vlist);
13598 vlist = XCDR (vlist))
13600 Lisp_Object var = XCAR (vlist);
13601 Lisp_Object val;
13603 if (!SYMBOLP (var))
13604 continue;
13606 val = find_symbol_value (var);
13608 if (MARKERP (val)
13609 && current_buffer == XMARKER (val)->buffer
13610 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13612 if (FRAME_WINDOW_P (it->f)
13613 /* FIXME: if ROW->reversed_p is set, this should test
13614 the right fringe, not the left one. */
13615 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13617 #ifdef HAVE_WINDOW_SYSTEM
13618 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13620 int fringe_bitmap = lookup_fringe_bitmap (val);
13621 if (fringe_bitmap != 0)
13622 return make_number (fringe_bitmap);
13624 #endif
13625 return make_number (-1); /* Use default arrow bitmap. */
13627 return overlay_arrow_string_or_property (var);
13631 return Qnil;
13634 /* Return true if point moved out of or into a composition. Otherwise
13635 return false. PREV_BUF and PREV_PT are the last point buffer and
13636 position. BUF and PT are the current point buffer and position. */
13638 static bool
13639 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13640 struct buffer *buf, ptrdiff_t pt)
13642 ptrdiff_t start, end;
13643 Lisp_Object prop;
13644 Lisp_Object buffer;
13646 XSETBUFFER (buffer, buf);
13647 /* Check a composition at the last point if point moved within the
13648 same buffer. */
13649 if (prev_buf == buf)
13651 if (prev_pt == pt)
13652 /* Point didn't move. */
13653 return false;
13655 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13656 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13657 && composition_valid_p (start, end, prop)
13658 && start < prev_pt && end > prev_pt)
13659 /* The last point was within the composition. Return true iff
13660 point moved out of the composition. */
13661 return (pt <= start || pt >= end);
13664 /* Check a composition at the current point. */
13665 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13666 && find_composition (pt, -1, &start, &end, &prop, buffer)
13667 && composition_valid_p (start, end, prop)
13668 && start < pt && end > pt);
13671 /* Reconsider the clip changes of buffer which is displayed in W. */
13673 static void
13674 reconsider_clip_changes (struct window *w)
13676 struct buffer *b = XBUFFER (w->contents);
13678 if (b->clip_changed
13679 && w->window_end_valid
13680 && w->current_matrix->buffer == b
13681 && w->current_matrix->zv == BUF_ZV (b)
13682 && w->current_matrix->begv == BUF_BEGV (b))
13683 b->clip_changed = false;
13685 /* If display wasn't paused, and W is not a tool bar window, see if
13686 point has been moved into or out of a composition. In that case,
13687 set b->clip_changed to force updating the screen. If
13688 b->clip_changed has already been set, skip this check. */
13689 if (!b->clip_changed && w->window_end_valid)
13691 ptrdiff_t pt = (w == XWINDOW (selected_window)
13692 ? PT : marker_position (w->pointm));
13694 if ((w->current_matrix->buffer != b || pt != w->last_point)
13695 && check_point_in_composition (w->current_matrix->buffer,
13696 w->last_point, b, pt))
13697 b->clip_changed = true;
13701 static void
13702 propagate_buffer_redisplay (void)
13703 { /* Resetting b->text->redisplay is problematic!
13704 We can't just reset it in the case that some window that displays
13705 it has not been redisplayed; and such a window can stay
13706 unredisplayed for a long time if it's currently invisible.
13707 But we do want to reset it at the end of redisplay otherwise
13708 its displayed windows will keep being redisplayed over and over
13709 again.
13710 So we copy all b->text->redisplay flags up to their windows here,
13711 such that mark_window_display_accurate can safely reset
13712 b->text->redisplay. */
13713 Lisp_Object ws = window_list ();
13714 for (; CONSP (ws); ws = XCDR (ws))
13716 struct window *thisw = XWINDOW (XCAR (ws));
13717 struct buffer *thisb = XBUFFER (thisw->contents);
13718 if (thisb->text->redisplay)
13719 thisw->redisplay = true;
13723 #define STOP_POLLING \
13724 do { if (! polling_stopped_here) stop_polling (); \
13725 polling_stopped_here = true; } while (false)
13727 #define RESUME_POLLING \
13728 do { if (polling_stopped_here) start_polling (); \
13729 polling_stopped_here = false; } while (false)
13732 /* Perhaps in the future avoid recentering windows if it
13733 is not necessary; currently that causes some problems. */
13735 static void
13736 redisplay_internal (void)
13738 struct window *w = XWINDOW (selected_window);
13739 struct window *sw;
13740 struct frame *fr;
13741 bool pending;
13742 bool must_finish = false, match_p;
13743 struct text_pos tlbufpos, tlendpos;
13744 int number_of_visible_frames;
13745 ptrdiff_t count;
13746 struct frame *sf;
13747 bool polling_stopped_here = false;
13748 Lisp_Object tail, frame;
13750 /* Set a limit to the number of retries we perform due to horizontal
13751 scrolling, this avoids getting stuck in an uninterruptible
13752 infinite loop (Bug #24633). */
13753 enum { MAX_HSCROLL_RETRIES = 16 };
13754 int hscroll_retries = 0;
13756 /* Limit the number of retries for when frame(s) become garbaged as
13757 result of redisplaying them. Some packages set various redisplay
13758 hooks, such as window-scroll-functions, to run Lisp that always
13759 calls APIs which cause the frame's garbaged flag to become set,
13760 so we loop indefinitely. */
13761 enum {MAX_GARBAGED_FRAME_RETRIES = 2 };
13762 int garbaged_frame_retries = 0;
13764 /* True means redisplay has to consider all windows on all
13765 frames. False, only selected_window is considered. */
13766 bool consider_all_windows_p;
13768 /* True means redisplay has to redisplay the miniwindow. */
13769 bool update_miniwindow_p = false;
13771 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13773 /* No redisplay if running in batch mode or frame is not yet fully
13774 initialized, or redisplay is explicitly turned off by setting
13775 Vinhibit_redisplay. */
13776 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13777 || !NILP (Vinhibit_redisplay))
13778 return;
13780 /* Don't examine these until after testing Vinhibit_redisplay.
13781 When Emacs is shutting down, perhaps because its connection to
13782 X has dropped, we should not look at them at all. */
13783 fr = XFRAME (w->frame);
13784 sf = SELECTED_FRAME ();
13786 if (!fr->glyphs_initialized_p)
13787 return;
13789 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13790 if (popup_activated ())
13791 return;
13792 #endif
13794 /* I don't think this happens but let's be paranoid. */
13795 if (redisplaying_p)
13796 return;
13798 /* Record a function that clears redisplaying_p
13799 when we leave this function. */
13800 count = SPECPDL_INDEX ();
13801 record_unwind_protect_void (unwind_redisplay);
13802 redisplaying_p = true;
13803 block_buffer_flips ();
13804 specbind (Qinhibit_free_realized_faces, Qnil);
13806 /* Record this function, so it appears on the profiler's backtraces. */
13807 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13809 FOR_EACH_FRAME (tail, frame)
13810 XFRAME (frame)->already_hscrolled_p = false;
13812 retry:
13813 /* Remember the currently selected window. */
13814 sw = w;
13816 pending = false;
13817 forget_escape_and_glyphless_faces ();
13819 inhibit_free_realized_faces = false;
13821 /* If face_change, init_iterator will free all realized faces, which
13822 includes the faces referenced from current matrices. So, we
13823 can't reuse current matrices in this case. */
13824 if (face_change)
13825 windows_or_buffers_changed = 47;
13827 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13828 && FRAME_TTY (sf)->previous_frame != sf)
13830 /* Since frames on a single ASCII terminal share the same
13831 display area, displaying a different frame means redisplay
13832 the whole thing. */
13833 SET_FRAME_GARBAGED (sf);
13834 #ifndef DOS_NT
13835 set_tty_color_mode (FRAME_TTY (sf), sf);
13836 #endif
13837 FRAME_TTY (sf)->previous_frame = sf;
13840 /* Set the visible flags for all frames. Do this before checking for
13841 resized or garbaged frames; they want to know if their frames are
13842 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13843 number_of_visible_frames = 0;
13845 FOR_EACH_FRAME (tail, frame)
13847 struct frame *f = XFRAME (frame);
13849 if (FRAME_VISIBLE_P (f))
13851 ++number_of_visible_frames;
13852 /* Adjust matrices for visible frames only. */
13853 if (f->fonts_changed)
13855 adjust_frame_glyphs (f);
13856 /* Disable all redisplay optimizations for this frame.
13857 This is because adjust_frame_glyphs resets the
13858 enabled_p flag for all glyph rows of all windows, so
13859 many optimizations will fail anyway, and some might
13860 fail to test that flag and do bogus things as
13861 result. */
13862 SET_FRAME_GARBAGED (f);
13863 f->fonts_changed = false;
13865 /* If cursor type has been changed on the frame
13866 other than selected, consider all frames. */
13867 if (f != sf && f->cursor_type_changed)
13868 fset_redisplay (f);
13870 clear_desired_matrices (f);
13873 /* Notice any pending interrupt request to change frame size. */
13874 do_pending_window_change (true);
13876 /* do_pending_window_change could change the selected_window due to
13877 frame resizing which makes the selected window too small. */
13878 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13879 sw = w;
13881 /* Clear frames marked as garbaged. */
13882 clear_garbaged_frames ();
13884 /* Build menubar and tool-bar items. */
13885 if (NILP (Vmemory_full))
13886 prepare_menu_bars ();
13888 reconsider_clip_changes (w);
13890 /* In most cases selected window displays current buffer. */
13891 match_p = XBUFFER (w->contents) == current_buffer;
13892 if (match_p)
13894 /* Detect case that we need to write or remove a star in the mode line. */
13895 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13896 w->update_mode_line = true;
13898 if (mode_line_update_needed (w))
13899 w->update_mode_line = true;
13901 /* If reconsider_clip_changes above decided that the narrowing
13902 in the current buffer changed, make sure all other windows
13903 showing that buffer will be redisplayed. */
13904 if (current_buffer->clip_changed)
13905 bset_update_mode_line (current_buffer);
13908 /* Normally the message* functions will have already displayed and
13909 updated the echo area, but the frame may have been trashed, or
13910 the update may have been preempted, so display the echo area
13911 again here. Checking message_cleared_p captures the case that
13912 the echo area should be cleared. */
13913 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13914 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13915 || (message_cleared_p
13916 && minibuf_level == 0
13917 /* If the mini-window is currently selected, this means the
13918 echo-area doesn't show through. */
13919 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13921 echo_area_display (false);
13923 /* If echo_area_display resizes the mini-window, the redisplay and
13924 window_sizes_changed flags of the selected frame are set, but
13925 it's too late for the hooks in window-size-change-functions,
13926 which have been examined already in prepare_menu_bars. So in
13927 that case we call the hooks here only for the selected frame. */
13928 if (sf->redisplay)
13930 ptrdiff_t count1 = SPECPDL_INDEX ();
13932 record_unwind_save_match_data ();
13933 run_window_size_change_functions (selected_frame);
13934 unbind_to (count1, Qnil);
13937 if (message_cleared_p)
13938 update_miniwindow_p = true;
13940 must_finish = true;
13942 /* If we don't display the current message, don't clear the
13943 message_cleared_p flag, because, if we did, we wouldn't clear
13944 the echo area in the next redisplay which doesn't preserve
13945 the echo area. */
13946 if (!display_last_displayed_message_p)
13947 message_cleared_p = false;
13949 else if (EQ (selected_window, minibuf_window)
13950 && (current_buffer->clip_changed || window_outdated (w))
13951 && resize_mini_window (w, false))
13953 if (sf->redisplay)
13955 ptrdiff_t count1 = SPECPDL_INDEX ();
13957 record_unwind_save_match_data ();
13958 run_window_size_change_functions (selected_frame);
13959 unbind_to (count1, Qnil);
13962 /* Resized active mini-window to fit the size of what it is
13963 showing if its contents might have changed. */
13964 must_finish = true;
13966 /* If window configuration was changed, frames may have been
13967 marked garbaged. Clear them or we will experience
13968 surprises wrt scrolling. */
13969 clear_garbaged_frames ();
13972 if (windows_or_buffers_changed && !update_mode_lines)
13973 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13974 only the windows's contents needs to be refreshed, or whether the
13975 mode-lines also need a refresh. */
13976 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13977 ? REDISPLAY_SOME : 32);
13979 /* If specs for an arrow have changed, do thorough redisplay
13980 to ensure we remove any arrow that should no longer exist. */
13981 /* Apparently, this is the only case where we update other windows,
13982 without updating other mode-lines. */
13983 overlay_arrows_changed_p (true);
13985 consider_all_windows_p = (update_mode_lines
13986 || windows_or_buffers_changed);
13988 #define AINC(a,i) \
13990 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13991 if (INTEGERP (entry)) \
13992 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13995 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13996 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13998 /* Optimize the case that only the line containing the cursor in the
13999 selected window has changed. Variables starting with this_ are
14000 set in display_line and record information about the line
14001 containing the cursor. */
14002 tlbufpos = this_line_start_pos;
14003 tlendpos = this_line_end_pos;
14004 if (!consider_all_windows_p
14005 && CHARPOS (tlbufpos) > 0
14006 && !w->update_mode_line
14007 && !current_buffer->clip_changed
14008 && !current_buffer->prevent_redisplay_optimizations_p
14009 && FRAME_VISIBLE_P (XFRAME (w->frame))
14010 && !FRAME_OBSCURED_P (XFRAME (w->frame))
14011 && !XFRAME (w->frame)->cursor_type_changed
14012 && !XFRAME (w->frame)->face_change
14013 /* Make sure recorded data applies to current buffer, etc. */
14014 && this_line_buffer == current_buffer
14015 && match_p
14016 && !w->force_start
14017 && !w->optional_new_start
14018 /* Point must be on the line that we have info recorded about. */
14019 && PT >= CHARPOS (tlbufpos)
14020 && PT <= Z - CHARPOS (tlendpos)
14021 /* All text outside that line, including its final newline,
14022 must be unchanged. */
14023 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
14024 CHARPOS (tlendpos)))
14026 if (CHARPOS (tlbufpos) > BEGV
14027 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
14028 && (CHARPOS (tlbufpos) == ZV
14029 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
14030 /* Former continuation line has disappeared by becoming empty. */
14031 goto cancel;
14032 else if (window_outdated (w) || MINI_WINDOW_P (w))
14034 /* We have to handle the case of continuation around a
14035 wide-column character (see the comment in indent.c around
14036 line 1340).
14038 For instance, in the following case:
14040 -------- Insert --------
14041 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
14042 J_I_ ==> J_I_ `^^' are cursors.
14043 ^^ ^^
14044 -------- --------
14046 As we have to redraw the line above, we cannot use this
14047 optimization. */
14049 struct it it;
14050 int line_height_before = this_line_pixel_height;
14052 /* Note that start_display will handle the case that the
14053 line starting at tlbufpos is a continuation line. */
14054 start_display (&it, w, tlbufpos);
14056 /* Implementation note: It this still necessary? */
14057 if (it.current_x != this_line_start_x)
14058 goto cancel;
14060 TRACE ((stderr, "trying display optimization 1\n"));
14061 w->cursor.vpos = -1;
14062 overlay_arrow_seen = false;
14063 it.vpos = this_line_vpos;
14064 it.current_y = this_line_y;
14065 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
14066 display_line (&it, -1);
14068 /* If line contains point, is not continued,
14069 and ends at same distance from eob as before, we win. */
14070 if (w->cursor.vpos >= 0
14071 /* Line is not continued, otherwise this_line_start_pos
14072 would have been set to 0 in display_line. */
14073 && CHARPOS (this_line_start_pos)
14074 /* Line ends as before. */
14075 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
14076 /* Line has same height as before. Otherwise other lines
14077 would have to be shifted up or down. */
14078 && this_line_pixel_height == line_height_before)
14080 /* If this is not the window's last line, we must adjust
14081 the charstarts of the lines below. */
14082 if (it.current_y < it.last_visible_y)
14084 struct glyph_row *row
14085 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
14086 ptrdiff_t delta, delta_bytes;
14088 /* We used to distinguish between two cases here,
14089 conditioned by Z - CHARPOS (tlendpos) == ZV, for
14090 when the line ends in a newline or the end of the
14091 buffer's accessible portion. But both cases did
14092 the same, so they were collapsed. */
14093 delta = (Z
14094 - CHARPOS (tlendpos)
14095 - MATRIX_ROW_START_CHARPOS (row));
14096 delta_bytes = (Z_BYTE
14097 - BYTEPOS (tlendpos)
14098 - MATRIX_ROW_START_BYTEPOS (row));
14100 increment_matrix_positions (w->current_matrix,
14101 this_line_vpos + 1,
14102 w->current_matrix->nrows,
14103 delta, delta_bytes);
14106 /* If this row displays text now but previously didn't,
14107 or vice versa, w->window_end_vpos may have to be
14108 adjusted. */
14109 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
14111 if (w->window_end_vpos < this_line_vpos)
14112 w->window_end_vpos = this_line_vpos;
14114 else if (w->window_end_vpos == this_line_vpos
14115 && this_line_vpos > 0)
14116 w->window_end_vpos = this_line_vpos - 1;
14117 w->window_end_valid = false;
14119 /* Update hint: No need to try to scroll in update_window. */
14120 w->desired_matrix->no_scrolling_p = true;
14122 #ifdef GLYPH_DEBUG
14123 *w->desired_matrix->method = 0;
14124 debug_method_add (w, "optimization 1");
14125 #endif
14126 #ifdef HAVE_WINDOW_SYSTEM
14127 update_window_fringes (w, false);
14128 #endif
14129 goto update;
14131 else
14132 goto cancel;
14134 else if (/* Cursor position hasn't changed. */
14135 PT == w->last_point
14136 /* Make sure the cursor was last displayed
14137 in this window. Otherwise we have to reposition it. */
14139 /* PXW: Must be converted to pixels, probably. */
14140 && 0 <= w->cursor.vpos
14141 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
14143 if (!must_finish)
14145 do_pending_window_change (true);
14146 /* If selected_window changed, redisplay again. */
14147 if (WINDOWP (selected_window)
14148 && (w = XWINDOW (selected_window)) != sw)
14149 goto retry;
14151 /* We used to always goto end_of_redisplay here, but this
14152 isn't enough if we have a blinking cursor. */
14153 if (w->cursor_off_p == w->last_cursor_off_p)
14154 goto end_of_redisplay;
14156 goto update;
14158 /* If highlighting the region, or if the cursor is in the echo area,
14159 then we can't just move the cursor. */
14160 else if (NILP (Vshow_trailing_whitespace)
14161 && !cursor_in_echo_area)
14163 struct it it;
14164 struct glyph_row *row;
14166 /* Skip from tlbufpos to PT and see where it is. Note that
14167 PT may be in invisible text. If so, we will end at the
14168 next visible position. */
14169 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
14170 NULL, DEFAULT_FACE_ID);
14171 it.current_x = this_line_start_x;
14172 it.current_y = this_line_y;
14173 it.vpos = this_line_vpos;
14175 /* The call to move_it_to stops in front of PT, but
14176 moves over before-strings. */
14177 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
14179 if (it.vpos == this_line_vpos
14180 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
14181 row->enabled_p))
14183 eassert (this_line_vpos == it.vpos);
14184 eassert (this_line_y == it.current_y);
14185 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14186 if (cursor_row_fully_visible_p (w, false, true))
14188 #ifdef GLYPH_DEBUG
14189 *w->desired_matrix->method = 0;
14190 debug_method_add (w, "optimization 3");
14191 #endif
14192 goto update;
14194 else
14195 goto cancel;
14197 else
14198 goto cancel;
14201 cancel:
14202 /* Text changed drastically or point moved off of line. */
14203 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
14206 CHARPOS (this_line_start_pos) = 0;
14207 ++clear_face_cache_count;
14208 #ifdef HAVE_WINDOW_SYSTEM
14209 ++clear_image_cache_count;
14210 #endif
14212 /* Build desired matrices, and update the display. If
14213 consider_all_windows_p, do it for all windows on all frames that
14214 require redisplay, as specified by their 'redisplay' flag.
14215 Otherwise do it for selected_window, only. */
14217 if (consider_all_windows_p)
14219 FOR_EACH_FRAME (tail, frame)
14220 XFRAME (frame)->updated_p = false;
14222 propagate_buffer_redisplay ();
14224 FOR_EACH_FRAME (tail, frame)
14226 struct frame *f = XFRAME (frame);
14228 /* We don't have to do anything for unselected terminal
14229 frames. */
14230 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
14231 && !EQ (FRAME_TTY (f)->top_frame, frame))
14232 continue;
14234 retry_frame:
14235 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14237 bool gcscrollbars
14238 /* Only GC scrollbars when we redisplay the whole frame. */
14239 = f->redisplay || !REDISPLAY_SOME_P ();
14240 bool f_redisplay_flag = f->redisplay;
14241 /* Mark all the scroll bars to be removed; we'll redeem
14242 the ones we want when we redisplay their windows. */
14243 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14244 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14246 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14247 redisplay_windows (FRAME_ROOT_WINDOW (f));
14248 /* Remember that the invisible frames need to be redisplayed next
14249 time they're visible. */
14250 else if (!REDISPLAY_SOME_P ())
14251 f->redisplay = true;
14253 /* The X error handler may have deleted that frame. */
14254 if (!FRAME_LIVE_P (f))
14255 continue;
14257 /* Any scroll bars which redisplay_windows should have
14258 nuked should now go away. */
14259 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14260 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14262 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14264 /* If fonts changed on visible frame, display again. */
14265 if (f->fonts_changed)
14267 adjust_frame_glyphs (f);
14268 /* Disable all redisplay optimizations for this
14269 frame. For the reasons, see the comment near
14270 the previous call to adjust_frame_glyphs above. */
14271 SET_FRAME_GARBAGED (f);
14272 f->fonts_changed = false;
14273 goto retry_frame;
14276 /* See if we have to hscroll. */
14277 if (!f->already_hscrolled_p)
14279 f->already_hscrolled_p = true;
14280 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14281 && hscroll_windows (f->root_window))
14283 hscroll_retries++;
14284 goto retry_frame;
14288 /* If the frame's redisplay flag was not set before
14289 we went about redisplaying its windows, but it is
14290 set now, that means we employed some redisplay
14291 optimizations inside redisplay_windows, and
14292 bypassed producing some screen lines. But if
14293 f->redisplay is now set, it might mean the old
14294 faces are no longer valid (e.g., if redisplaying
14295 some window called some Lisp which defined a new
14296 face or redefined an existing face), so trying to
14297 use them in update_frame will segfault.
14298 Therefore, we must redisplay this frame. */
14299 if (!f_redisplay_flag && f->redisplay)
14300 goto retry_frame;
14301 /* In some case (e.g., window resize), we notice
14302 only during window updating that the window
14303 content changed unpredictably (e.g., a GTK
14304 scrollbar moved, or some Lisp hook that winds up
14305 calling adjust_frame_glyphs) and that our
14306 previous estimation of the frame content was
14307 garbage. We have to start over. These cases
14308 should be rare, so going all the way back to the
14309 top of redisplay should be good enough. */
14310 if (FRAME_GARBAGED_P (f)
14311 && garbaged_frame_retries++ < MAX_GARBAGED_FRAME_RETRIES)
14312 goto retry;
14314 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
14315 x_clear_under_internal_border (f);
14316 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
14318 /* Prevent various kinds of signals during display
14319 update. stdio is not robust about handling
14320 signals, which can cause an apparent I/O error. */
14321 if (interrupt_input)
14322 unrequest_sigio ();
14323 STOP_POLLING;
14325 pending |= update_frame (f, false, false);
14326 f->cursor_type_changed = false;
14327 f->updated_p = true;
14332 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14334 if (!pending)
14336 /* Do the mark_window_display_accurate after all windows have
14337 been redisplayed because this call resets flags in buffers
14338 which are needed for proper redisplay. */
14339 FOR_EACH_FRAME (tail, frame)
14341 struct frame *f = XFRAME (frame);
14342 if (f->updated_p)
14344 f->redisplay = false;
14345 f->garbaged = false;
14346 mark_window_display_accurate (f->root_window, true);
14347 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14348 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14353 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14355 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14356 /* Use list_of_error, not Qerror, so that
14357 we catch only errors and don't run the debugger. */
14358 internal_condition_case_1 (redisplay_window_1, selected_window,
14359 list_of_error,
14360 redisplay_window_error);
14361 if (update_miniwindow_p)
14362 internal_condition_case_1 (redisplay_window_1,
14363 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14364 redisplay_window_error);
14366 /* Compare desired and current matrices, perform output. */
14368 update:
14369 /* If fonts changed, display again. Likewise if redisplay_window_1
14370 above caused some change (e.g., a change in faces) that requires
14371 considering the entire frame again. */
14372 if (sf->fonts_changed || sf->redisplay)
14374 if (sf->redisplay)
14376 /* Set this to force a more thorough redisplay.
14377 Otherwise, we might immediately loop back to the
14378 above "else-if" clause (since all the conditions that
14379 led here might still be true), and we will then
14380 infloop, because the selected-frame's redisplay flag
14381 is not (and cannot be) reset. */
14382 windows_or_buffers_changed = 50;
14384 goto retry;
14387 /* Prevent freeing of realized faces, since desired matrices are
14388 pending that reference the faces we computed and cached. */
14389 inhibit_free_realized_faces = true;
14391 /* Prevent various kinds of signals during display update.
14392 stdio is not robust about handling signals,
14393 which can cause an apparent I/O error. */
14394 if (interrupt_input)
14395 unrequest_sigio ();
14396 STOP_POLLING;
14398 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14400 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14401 && hscroll_windows (selected_window))
14403 hscroll_retries++;
14404 goto retry;
14407 XWINDOW (selected_window)->must_be_updated_p = true;
14408 pending = update_frame (sf, false, false);
14409 sf->cursor_type_changed = false;
14412 /* We may have called echo_area_display at the top of this
14413 function. If the echo area is on another frame, that may
14414 have put text on a frame other than the selected one, so the
14415 above call to update_frame would not have caught it. Catch
14416 it here. */
14417 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14418 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14420 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14422 XWINDOW (mini_window)->must_be_updated_p = true;
14423 pending |= update_frame (mini_frame, false, false);
14424 mini_frame->cursor_type_changed = false;
14425 if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
14426 && hscroll_windows (mini_window))
14428 hscroll_retries++;
14429 goto retry;
14434 /* If display was paused because of pending input, make sure we do a
14435 thorough update the next time. */
14436 if (pending)
14438 /* Prevent the optimization at the beginning of
14439 redisplay_internal that tries a single-line update of the
14440 line containing the cursor in the selected window. */
14441 CHARPOS (this_line_start_pos) = 0;
14443 /* Let the overlay arrow be updated the next time. */
14444 update_overlay_arrows (0);
14446 /* If we pause after scrolling, some rows in the current
14447 matrices of some windows are not valid. */
14448 if (!WINDOW_FULL_WIDTH_P (w)
14449 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14450 update_mode_lines = 36;
14452 else
14454 if (!consider_all_windows_p)
14456 /* This has already been done above if
14457 consider_all_windows_p is set. */
14458 if (XBUFFER (w->contents)->text->redisplay
14459 && buffer_window_count (XBUFFER (w->contents)) > 1)
14460 /* This can happen if b->text->redisplay was set during
14461 jit-lock. */
14462 propagate_buffer_redisplay ();
14463 mark_window_display_accurate_1 (w, true);
14465 /* Say overlay arrows are up to date. */
14466 update_overlay_arrows (1);
14468 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14469 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14472 update_mode_lines = 0;
14473 windows_or_buffers_changed = 0;
14476 /* Start SIGIO interrupts coming again. Having them off during the
14477 code above makes it less likely one will discard output, but not
14478 impossible, since there might be stuff in the system buffer here.
14479 But it is much hairier to try to do anything about that. */
14480 if (interrupt_input)
14481 request_sigio ();
14482 RESUME_POLLING;
14484 /* If a frame has become visible which was not before, redisplay
14485 again, so that we display it. Expose events for such a frame
14486 (which it gets when becoming visible) don't call the parts of
14487 redisplay constructing glyphs, so simply exposing a frame won't
14488 display anything in this case. So, we have to display these
14489 frames here explicitly. */
14490 if (!pending)
14492 int new_count = 0;
14494 FOR_EACH_FRAME (tail, frame)
14496 if (XFRAME (frame)->visible)
14497 new_count++;
14500 if (new_count != number_of_visible_frames)
14501 windows_or_buffers_changed = 52;
14504 /* Change frame size now if a change is pending. */
14505 do_pending_window_change (true);
14507 /* If we just did a pending size change, or have additional
14508 visible frames, or selected_window changed, redisplay again. */
14509 if ((windows_or_buffers_changed && !pending)
14510 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14511 goto retry;
14513 /* Clear the face and image caches.
14515 We used to do this only if consider_all_windows_p. But the cache
14516 needs to be cleared if a timer creates images in the current
14517 buffer (e.g. the test case in Bug#6230). */
14519 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14521 clear_face_cache (false);
14522 clear_face_cache_count = 0;
14525 #ifdef HAVE_WINDOW_SYSTEM
14526 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14528 clear_image_caches (Qnil);
14529 clear_image_cache_count = 0;
14531 #endif /* HAVE_WINDOW_SYSTEM */
14533 end_of_redisplay:
14534 #ifdef HAVE_NS
14535 ns_set_doc_edited ();
14536 #endif
14537 if (interrupt_input && interrupts_deferred)
14538 request_sigio ();
14540 unbind_to (count, Qnil);
14541 RESUME_POLLING;
14544 static void
14545 unwind_redisplay_preserve_echo_area (void)
14547 unblock_buffer_flips ();
14550 /* Redisplay, but leave alone any recent echo area message unless
14551 another message has been requested in its place.
14553 This is useful in situations where you need to redisplay but no
14554 user action has occurred, making it inappropriate for the message
14555 area to be cleared. See tracking_off and
14556 wait_reading_process_output for examples of these situations.
14558 FROM_WHERE is an integer saying from where this function was
14559 called. This is useful for debugging. */
14561 void
14562 redisplay_preserve_echo_area (int from_where)
14564 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14566 block_input ();
14567 ptrdiff_t count = SPECPDL_INDEX ();
14568 record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
14569 block_buffer_flips ();
14570 unblock_input ();
14572 if (!NILP (echo_area_buffer[1]))
14574 /* We have a previously displayed message, but no current
14575 message. Redisplay the previous message. */
14576 display_last_displayed_message_p = true;
14577 redisplay_internal ();
14578 display_last_displayed_message_p = false;
14580 else
14581 redisplay_internal ();
14583 flush_frame (SELECTED_FRAME ());
14584 unbind_to (count, Qnil);
14588 /* Function registered with record_unwind_protect in redisplay_internal. */
14590 static void
14591 unwind_redisplay (void)
14593 redisplaying_p = false;
14594 unblock_buffer_flips ();
14598 /* Mark the display of leaf window W as accurate or inaccurate.
14599 If ACCURATE_P, mark display of W as accurate.
14600 If !ACCURATE_P, arrange for W to be redisplayed the next
14601 time redisplay_internal is called. */
14603 static void
14604 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14606 struct buffer *b = XBUFFER (w->contents);
14608 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14609 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14610 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14612 if (accurate_p)
14614 b->clip_changed = false;
14615 b->prevent_redisplay_optimizations_p = false;
14616 eassert (buffer_window_count (b) > 0);
14617 /* Resetting b->text->redisplay is problematic!
14618 In order to make it safer to do it here, redisplay_internal must
14619 have copied all b->text->redisplay to their respective windows. */
14620 b->text->redisplay = false;
14622 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14623 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14624 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14625 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14627 w->current_matrix->buffer = b;
14628 w->current_matrix->begv = BUF_BEGV (b);
14629 w->current_matrix->zv = BUF_ZV (b);
14631 w->last_cursor_vpos = w->cursor.vpos;
14632 w->last_cursor_off_p = w->cursor_off_p;
14634 if (w == XWINDOW (selected_window))
14635 w->last_point = BUF_PT (b);
14636 else
14637 w->last_point = marker_position (w->pointm);
14639 w->window_end_valid = true;
14640 w->update_mode_line = false;
14643 w->redisplay = !accurate_p;
14647 /* Mark the display of windows in the window tree rooted at WINDOW as
14648 accurate or inaccurate. If ACCURATE_P, mark display of
14649 windows as accurate. If !ACCURATE_P, arrange for windows to
14650 be redisplayed the next time redisplay_internal is called. */
14652 void
14653 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14655 struct window *w;
14657 for (; !NILP (window); window = w->next)
14659 w = XWINDOW (window);
14660 if (WINDOWP (w->contents))
14661 mark_window_display_accurate (w->contents, accurate_p);
14662 else
14663 mark_window_display_accurate_1 (w, accurate_p);
14666 if (accurate_p)
14667 update_overlay_arrows (1);
14668 else
14669 /* Force a thorough redisplay the next time by setting
14670 last_arrow_position and last_arrow_string to t, which is
14671 unequal to any useful value of Voverlay_arrow_... */
14672 update_overlay_arrows (-1);
14676 /* Return value in display table DP (Lisp_Char_Table *) for character
14677 C. Since a display table doesn't have any parent, we don't have to
14678 follow parent. Do not call this function directly but use the
14679 macro DISP_CHAR_VECTOR. */
14681 Lisp_Object
14682 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14684 Lisp_Object val;
14686 if (ASCII_CHAR_P (c))
14688 val = dp->ascii;
14689 if (SUB_CHAR_TABLE_P (val))
14690 val = XSUB_CHAR_TABLE (val)->contents[c];
14692 else
14694 Lisp_Object table;
14696 XSETCHAR_TABLE (table, dp);
14697 val = char_table_ref (table, c);
14699 if (NILP (val))
14700 val = dp->defalt;
14701 return val;
14704 static int buffer_flip_blocked_depth;
14706 static void
14707 block_buffer_flips (void)
14709 eassert (buffer_flip_blocked_depth >= 0);
14710 buffer_flip_blocked_depth++;
14713 static void
14714 unblock_buffer_flips (void)
14716 eassert (buffer_flip_blocked_depth > 0);
14717 if (--buffer_flip_blocked_depth == 0)
14719 Lisp_Object tail, frame;
14720 block_input ();
14721 FOR_EACH_FRAME (tail, frame)
14723 struct frame *f = XFRAME (frame);
14724 if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
14725 (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
14727 unblock_input ();
14731 bool
14732 buffer_flipping_blocked_p (void)
14734 return buffer_flip_blocked_depth > 0;
14738 /***********************************************************************
14739 Window Redisplay
14740 ***********************************************************************/
14742 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14744 static void
14745 redisplay_windows (Lisp_Object window)
14747 while (!NILP (window))
14749 struct window *w = XWINDOW (window);
14751 if (WINDOWP (w->contents))
14752 redisplay_windows (w->contents);
14753 else if (BUFFERP (w->contents))
14755 displayed_buffer = XBUFFER (w->contents);
14756 /* Use list_of_error, not Qerror, so that
14757 we catch only errors and don't run the debugger. */
14758 internal_condition_case_1 (redisplay_window_0, window,
14759 list_of_error,
14760 redisplay_window_error);
14763 window = w->next;
14767 static Lisp_Object
14768 redisplay_window_error (Lisp_Object ignore)
14770 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14771 return Qnil;
14774 static Lisp_Object
14775 redisplay_window_0 (Lisp_Object window)
14777 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14778 redisplay_window (window, false);
14779 return Qnil;
14782 static Lisp_Object
14783 redisplay_window_1 (Lisp_Object window)
14785 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14786 redisplay_window (window, true);
14787 return Qnil;
14791 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14792 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14793 which positions recorded in ROW differ from current buffer
14794 positions.
14796 Return true iff cursor is on this row. */
14798 static bool
14799 set_cursor_from_row (struct window *w, struct glyph_row *row,
14800 struct glyph_matrix *matrix,
14801 ptrdiff_t delta, ptrdiff_t delta_bytes,
14802 int dy, int dvpos)
14804 struct glyph *glyph = row->glyphs[TEXT_AREA];
14805 struct glyph *end = glyph + row->used[TEXT_AREA];
14806 struct glyph *cursor = NULL;
14807 /* The last known character position in row. */
14808 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14809 int x = row->x;
14810 ptrdiff_t pt_old = PT - delta;
14811 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14812 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14813 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14814 /* A glyph beyond the edge of TEXT_AREA which we should never
14815 touch. */
14816 struct glyph *glyphs_end = end;
14817 /* True means we've found a match for cursor position, but that
14818 glyph has the avoid_cursor_p flag set. */
14819 bool match_with_avoid_cursor = false;
14820 /* True means we've seen at least one glyph that came from a
14821 display string. */
14822 bool string_seen = false;
14823 /* Largest and smallest buffer positions seen so far during scan of
14824 glyph row. */
14825 ptrdiff_t bpos_max = pos_before;
14826 ptrdiff_t bpos_min = pos_after;
14827 /* Last buffer position covered by an overlay string with an integer
14828 `cursor' property. */
14829 ptrdiff_t bpos_covered = 0;
14830 /* True means the display string on which to display the cursor
14831 comes from a text property, not from an overlay. */
14832 bool string_from_text_prop = false;
14834 /* Don't even try doing anything if called for a mode-line or
14835 header-line row, since the rest of the code isn't prepared to
14836 deal with such calamities. */
14837 eassert (!row->mode_line_p);
14838 if (row->mode_line_p)
14839 return false;
14841 /* Skip over glyphs not having an object at the start and the end of
14842 the row. These are special glyphs like truncation marks on
14843 terminal frames. */
14844 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14846 if (!row->reversed_p)
14848 while (glyph < end
14849 && NILP (glyph->object)
14850 && glyph->charpos < 0)
14852 x += glyph->pixel_width;
14853 ++glyph;
14855 while (end > glyph
14856 && NILP ((end - 1)->object)
14857 /* CHARPOS is zero for blanks and stretch glyphs
14858 inserted by extend_face_to_end_of_line. */
14859 && (end - 1)->charpos <= 0)
14860 --end;
14861 glyph_before = glyph - 1;
14862 glyph_after = end;
14864 else
14866 struct glyph *g;
14868 /* If the glyph row is reversed, we need to process it from back
14869 to front, so swap the edge pointers. */
14870 glyphs_end = end = glyph - 1;
14871 glyph += row->used[TEXT_AREA] - 1;
14873 while (glyph > end + 1
14874 && NILP (glyph->object)
14875 && glyph->charpos < 0)
14876 --glyph;
14877 if (NILP (glyph->object) && glyph->charpos < 0)
14878 --glyph;
14879 /* By default, in reversed rows we put the cursor on the
14880 rightmost (first in the reading order) glyph. */
14881 for (x = 0, g = end + 1; g < glyph; g++)
14882 x += g->pixel_width;
14883 while (end < glyph
14884 && NILP ((end + 1)->object)
14885 && (end + 1)->charpos <= 0)
14886 ++end;
14887 glyph_before = glyph + 1;
14888 glyph_after = end;
14891 else if (row->reversed_p)
14893 /* In R2L rows that don't display text, put the cursor on the
14894 rightmost glyph. Case in point: an empty last line that is
14895 part of an R2L paragraph. */
14896 cursor = end - 1;
14897 /* Avoid placing the cursor on the last glyph of the row, where
14898 on terminal frames we hold the vertical border between
14899 adjacent windows. */
14900 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14901 && !WINDOW_RIGHTMOST_P (w)
14902 && cursor == row->glyphs[LAST_AREA] - 1)
14903 cursor--;
14904 x = -1; /* will be computed below, at label compute_x */
14907 /* Step 1: Try to find the glyph whose character position
14908 corresponds to point. If that's not possible, find 2 glyphs
14909 whose character positions are the closest to point, one before
14910 point, the other after it. */
14911 if (!row->reversed_p)
14912 while (/* not marched to end of glyph row */
14913 glyph < end
14914 /* glyph was not inserted by redisplay for internal purposes */
14915 && !NILP (glyph->object))
14917 if (BUFFERP (glyph->object))
14919 ptrdiff_t dpos = glyph->charpos - pt_old;
14921 if (glyph->charpos > bpos_max)
14922 bpos_max = glyph->charpos;
14923 if (glyph->charpos < bpos_min)
14924 bpos_min = glyph->charpos;
14925 if (!glyph->avoid_cursor_p)
14927 /* If we hit point, we've found the glyph on which to
14928 display the cursor. */
14929 if (dpos == 0)
14931 match_with_avoid_cursor = false;
14932 break;
14934 /* See if we've found a better approximation to
14935 POS_BEFORE or to POS_AFTER. */
14936 if (0 > dpos && dpos > pos_before - pt_old)
14938 pos_before = glyph->charpos;
14939 glyph_before = glyph;
14941 else if (0 < dpos && dpos < pos_after - pt_old)
14943 pos_after = glyph->charpos;
14944 glyph_after = glyph;
14947 else if (dpos == 0)
14948 match_with_avoid_cursor = true;
14950 else if (STRINGP (glyph->object))
14952 Lisp_Object chprop;
14953 ptrdiff_t glyph_pos = glyph->charpos;
14955 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14956 glyph->object);
14957 if (!NILP (chprop))
14959 /* If the string came from a `display' text property,
14960 look up the buffer position of that property and
14961 use that position to update bpos_max, as if we
14962 actually saw such a position in one of the row's
14963 glyphs. This helps with supporting integer values
14964 of `cursor' property on the display string in
14965 situations where most or all of the row's buffer
14966 text is completely covered by display properties,
14967 so that no glyph with valid buffer positions is
14968 ever seen in the row. */
14969 ptrdiff_t prop_pos =
14970 string_buffer_position_lim (glyph->object, pos_before,
14971 pos_after, false);
14973 if (prop_pos >= pos_before)
14974 bpos_max = prop_pos;
14976 if (INTEGERP (chprop))
14978 bpos_covered = bpos_max + XINT (chprop);
14979 /* If the `cursor' property covers buffer positions up
14980 to and including point, we should display cursor on
14981 this glyph. Note that, if a `cursor' property on one
14982 of the string's characters has an integer value, we
14983 will break out of the loop below _before_ we get to
14984 the position match above. IOW, integer values of
14985 the `cursor' property override the "exact match for
14986 point" strategy of positioning the cursor. */
14987 /* Implementation note: bpos_max == pt_old when, e.g.,
14988 we are in an empty line, where bpos_max is set to
14989 MATRIX_ROW_START_CHARPOS, see above. */
14990 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14992 cursor = glyph;
14993 break;
14997 string_seen = true;
14999 x += glyph->pixel_width;
15000 ++glyph;
15002 else if (glyph > end) /* row is reversed */
15003 while (!NILP (glyph->object))
15005 if (BUFFERP (glyph->object))
15007 ptrdiff_t dpos = glyph->charpos - pt_old;
15009 if (glyph->charpos > bpos_max)
15010 bpos_max = glyph->charpos;
15011 if (glyph->charpos < bpos_min)
15012 bpos_min = glyph->charpos;
15013 if (!glyph->avoid_cursor_p)
15015 if (dpos == 0)
15017 match_with_avoid_cursor = false;
15018 break;
15020 if (0 > dpos && dpos > pos_before - pt_old)
15022 pos_before = glyph->charpos;
15023 glyph_before = glyph;
15025 else if (0 < dpos && dpos < pos_after - pt_old)
15027 pos_after = glyph->charpos;
15028 glyph_after = glyph;
15031 else if (dpos == 0)
15032 match_with_avoid_cursor = true;
15034 else if (STRINGP (glyph->object))
15036 Lisp_Object chprop;
15037 ptrdiff_t glyph_pos = glyph->charpos;
15039 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
15040 glyph->object);
15041 if (!NILP (chprop))
15043 ptrdiff_t prop_pos =
15044 string_buffer_position_lim (glyph->object, pos_before,
15045 pos_after, false);
15047 if (prop_pos >= pos_before)
15048 bpos_max = prop_pos;
15050 if (INTEGERP (chprop))
15052 bpos_covered = bpos_max + XINT (chprop);
15053 /* If the `cursor' property covers buffer positions up
15054 to and including point, we should display cursor on
15055 this glyph. */
15056 if (bpos_max <= pt_old && bpos_covered >= pt_old)
15058 cursor = glyph;
15059 break;
15062 string_seen = true;
15064 --glyph;
15065 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
15067 x--; /* can't use any pixel_width */
15068 break;
15070 x -= glyph->pixel_width;
15073 /* Step 2: If we didn't find an exact match for point, we need to
15074 look for a proper place to put the cursor among glyphs between
15075 GLYPH_BEFORE and GLYPH_AFTER. */
15076 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15077 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
15078 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
15080 /* An empty line has a single glyph whose OBJECT is nil and
15081 whose CHARPOS is the position of a newline on that line.
15082 Note that on a TTY, there are more glyphs after that, which
15083 were produced by extend_face_to_end_of_line, but their
15084 CHARPOS is zero or negative. */
15085 bool empty_line_p =
15086 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15087 && NILP (glyph->object) && glyph->charpos > 0
15088 /* On a TTY, continued and truncated rows also have a glyph at
15089 their end whose OBJECT is nil and whose CHARPOS is
15090 positive (the continuation and truncation glyphs), but such
15091 rows are obviously not "empty". */
15092 && !(row->continued_p || row->truncated_on_right_p));
15094 if (row->ends_in_ellipsis_p && pos_after == last_pos)
15096 ptrdiff_t ellipsis_pos;
15098 /* Scan back over the ellipsis glyphs. */
15099 if (!row->reversed_p)
15101 ellipsis_pos = (glyph - 1)->charpos;
15102 while (glyph > row->glyphs[TEXT_AREA]
15103 && (glyph - 1)->charpos == ellipsis_pos)
15104 glyph--, x -= glyph->pixel_width;
15105 /* That loop always goes one position too far, including
15106 the glyph before the ellipsis. So scan forward over
15107 that one. */
15108 x += glyph->pixel_width;
15109 glyph++;
15111 else /* row is reversed */
15113 ellipsis_pos = (glyph + 1)->charpos;
15114 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15115 && (glyph + 1)->charpos == ellipsis_pos)
15116 glyph++, x += glyph->pixel_width;
15117 x -= glyph->pixel_width;
15118 glyph--;
15121 else if (match_with_avoid_cursor)
15123 cursor = glyph_after;
15124 x = -1;
15126 else if (string_seen)
15128 int incr = row->reversed_p ? -1 : +1;
15130 /* Need to find the glyph that came out of a string which is
15131 present at point. That glyph is somewhere between
15132 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
15133 positioned between POS_BEFORE and POS_AFTER in the
15134 buffer. */
15135 struct glyph *start, *stop;
15136 ptrdiff_t pos = pos_before;
15138 x = -1;
15140 /* If the row ends in a newline from a display string,
15141 reordering could have moved the glyphs belonging to the
15142 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
15143 in this case we extend the search to the last glyph in
15144 the row that was not inserted by redisplay. */
15145 if (row->ends_in_newline_from_string_p)
15147 glyph_after = end;
15148 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
15151 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
15152 correspond to POS_BEFORE and POS_AFTER, respectively. We
15153 need START and STOP in the order that corresponds to the
15154 row's direction as given by its reversed_p flag. If the
15155 directionality of characters between POS_BEFORE and
15156 POS_AFTER is the opposite of the row's base direction,
15157 these characters will have been reordered for display,
15158 and we need to reverse START and STOP. */
15159 if (!row->reversed_p)
15161 start = min (glyph_before, glyph_after);
15162 stop = max (glyph_before, glyph_after);
15164 else
15166 start = max (glyph_before, glyph_after);
15167 stop = min (glyph_before, glyph_after);
15169 for (glyph = start + incr;
15170 row->reversed_p ? glyph > stop : glyph < stop; )
15173 /* Any glyphs that come from the buffer are here because
15174 of bidi reordering. Skip them, and only pay
15175 attention to glyphs that came from some string. */
15176 if (STRINGP (glyph->object))
15178 Lisp_Object str;
15179 ptrdiff_t tem;
15180 /* If the display property covers the newline, we
15181 need to search for it one position farther. */
15182 ptrdiff_t lim = pos_after
15183 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
15185 string_from_text_prop = false;
15186 str = glyph->object;
15187 tem = string_buffer_position_lim (str, pos, lim, false);
15188 if (tem == 0 /* from overlay */
15189 || pos <= tem)
15191 /* If the string from which this glyph came is
15192 found in the buffer at point, or at position
15193 that is closer to point than pos_after, then
15194 we've found the glyph we've been looking for.
15195 If it comes from an overlay (tem == 0), and
15196 it has the `cursor' property on one of its
15197 glyphs, record that glyph as a candidate for
15198 displaying the cursor. (As in the
15199 unidirectional version, we will display the
15200 cursor on the last candidate we find.) */
15201 if (tem == 0
15202 || tem == pt_old
15203 || (tem - pt_old > 0 && tem < pos_after))
15205 /* The glyphs from this string could have
15206 been reordered. Find the one with the
15207 smallest string position. Or there could
15208 be a character in the string with the
15209 `cursor' property, which means display
15210 cursor on that character's glyph. */
15211 ptrdiff_t strpos = glyph->charpos;
15213 if (tem)
15215 cursor = glyph;
15216 string_from_text_prop = true;
15218 for ( ;
15219 (row->reversed_p ? glyph > stop : glyph < stop)
15220 && EQ (glyph->object, str);
15221 glyph += incr)
15223 Lisp_Object cprop;
15224 ptrdiff_t gpos = glyph->charpos;
15226 cprop = Fget_char_property (make_number (gpos),
15227 Qcursor,
15228 glyph->object);
15229 if (!NILP (cprop))
15231 cursor = glyph;
15232 break;
15234 if (tem && glyph->charpos < strpos)
15236 strpos = glyph->charpos;
15237 cursor = glyph;
15241 if (tem == pt_old
15242 || (tem - pt_old > 0 && tem < pos_after))
15243 goto compute_x;
15245 if (tem)
15246 pos = tem + 1; /* don't find previous instances */
15248 /* This string is not what we want; skip all of the
15249 glyphs that came from it. */
15250 while ((row->reversed_p ? glyph > stop : glyph < stop)
15251 && EQ (glyph->object, str))
15252 glyph += incr;
15254 else
15255 glyph += incr;
15258 /* If we reached the end of the line, and END was from a string,
15259 the cursor is not on this line. */
15260 if (cursor == NULL
15261 && (row->reversed_p ? glyph <= end : glyph >= end)
15262 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
15263 && STRINGP (end->object)
15264 && row->continued_p)
15265 return false;
15267 /* A truncated row may not include PT among its character positions.
15268 Setting the cursor inside the scroll margin will trigger
15269 recalculation of hscroll in hscroll_window_tree. But if a
15270 display string covers point, defer to the string-handling
15271 code below to figure this out. */
15272 else if (row->truncated_on_left_p && pt_old < bpos_min)
15274 cursor = glyph_before;
15275 x = -1;
15277 else if ((row->truncated_on_right_p && pt_old > bpos_max)
15278 /* Zero-width characters produce no glyphs. */
15279 || (!empty_line_p
15280 && (row->reversed_p
15281 ? glyph_after > glyphs_end
15282 : glyph_after < glyphs_end)))
15284 cursor = glyph_after;
15285 x = -1;
15289 compute_x:
15290 if (cursor != NULL)
15291 glyph = cursor;
15292 else if (glyph == glyphs_end
15293 && pos_before == pos_after
15294 && STRINGP ((row->reversed_p
15295 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15296 : row->glyphs[TEXT_AREA])->object))
15298 /* If all the glyphs of this row came from strings, put the
15299 cursor on the first glyph of the row. This avoids having the
15300 cursor outside of the text area in this very rare and hard
15301 use case. */
15302 glyph =
15303 row->reversed_p
15304 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15305 : row->glyphs[TEXT_AREA];
15307 if (x < 0)
15309 struct glyph *g;
15311 /* Need to compute x that corresponds to GLYPH. */
15312 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15314 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15315 emacs_abort ();
15316 x += g->pixel_width;
15320 /* ROW could be part of a continued line, which, under bidi
15321 reordering, might have other rows whose start and end charpos
15322 occlude point. Only set w->cursor if we found a better
15323 approximation to the cursor position than we have from previously
15324 examined candidate rows belonging to the same continued line. */
15325 if (/* We already have a candidate row. */
15326 w->cursor.vpos >= 0
15327 /* That candidate is not the row we are processing. */
15328 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15329 /* Make sure cursor.vpos specifies a row whose start and end
15330 charpos occlude point, and it is valid candidate for being a
15331 cursor-row. This is because some callers of this function
15332 leave cursor.vpos at the row where the cursor was displayed
15333 during the last redisplay cycle. */
15334 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15335 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15336 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15338 struct glyph *g1
15339 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15341 /* Don't consider glyphs that are outside TEXT_AREA. */
15342 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15343 return false;
15344 /* Keep the candidate whose buffer position is the closest to
15345 point or has the `cursor' property. */
15346 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15347 w->cursor.hpos >= 0
15348 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15349 && ((BUFFERP (g1->object)
15350 && (g1->charpos == pt_old /* An exact match always wins. */
15351 || (BUFFERP (glyph->object)
15352 && eabs (g1->charpos - pt_old)
15353 < eabs (glyph->charpos - pt_old))))
15354 /* Previous candidate is a glyph from a string that has
15355 a non-nil `cursor' property. */
15356 || (STRINGP (g1->object)
15357 && (!NILP (Fget_char_property (make_number (g1->charpos),
15358 Qcursor, g1->object))
15359 /* Previous candidate is from the same display
15360 string as this one, and the display string
15361 came from a text property. */
15362 || (EQ (g1->object, glyph->object)
15363 && string_from_text_prop)
15364 /* this candidate is from newline and its
15365 position is not an exact match */
15366 || (NILP (glyph->object)
15367 && glyph->charpos != pt_old)))))
15368 return false;
15369 /* If this candidate gives an exact match, use that. */
15370 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15371 /* If this candidate is a glyph created for the
15372 terminating newline of a line, and point is on that
15373 newline, it wins because it's an exact match. */
15374 || (!row->continued_p
15375 && NILP (glyph->object)
15376 && glyph->charpos == 0
15377 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15378 /* Otherwise, keep the candidate that comes from a row
15379 spanning less buffer positions. This may win when one or
15380 both candidate positions are on glyphs that came from
15381 display strings, for which we cannot compare buffer
15382 positions. */
15383 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15384 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15385 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15386 return false;
15388 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15389 w->cursor.x = x;
15390 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15391 w->cursor.y = row->y + dy;
15393 if (w == XWINDOW (selected_window))
15395 if (!row->continued_p
15396 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15397 && row->x == 0)
15399 this_line_buffer = XBUFFER (w->contents);
15401 CHARPOS (this_line_start_pos)
15402 = MATRIX_ROW_START_CHARPOS (row) + delta;
15403 BYTEPOS (this_line_start_pos)
15404 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15406 CHARPOS (this_line_end_pos)
15407 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15408 BYTEPOS (this_line_end_pos)
15409 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15411 this_line_y = w->cursor.y;
15412 this_line_pixel_height = row->height;
15413 this_line_vpos = w->cursor.vpos;
15414 this_line_start_x = row->x;
15416 else
15417 CHARPOS (this_line_start_pos) = 0;
15420 return true;
15424 /* Run window scroll functions, if any, for WINDOW with new window
15425 start STARTP. Sets the window start of WINDOW to that position.
15427 We assume that the window's buffer is really current. */
15429 static struct text_pos
15430 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15432 struct window *w = XWINDOW (window);
15433 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15435 eassert (current_buffer == XBUFFER (w->contents));
15437 if (!NILP (Vwindow_scroll_functions))
15439 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15440 make_number (CHARPOS (startp)));
15441 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15442 /* In case the hook functions switch buffers. */
15443 set_buffer_internal (XBUFFER (w->contents));
15446 return startp;
15450 /* Make sure the line containing the cursor is fully visible.
15451 A value of true means there is nothing to be done.
15452 (Either the line is fully visible, or it cannot be made so,
15453 or we cannot tell.)
15455 If FORCE_P, return false even if partial visible cursor row
15456 is higher than window.
15458 If CURRENT_MATRIX_P, use the information from the
15459 window's current glyph matrix; otherwise use the desired glyph
15460 matrix.
15462 A value of false means the caller should do scrolling
15463 as if point had gone off the screen. */
15465 static bool
15466 cursor_row_fully_visible_p (struct window *w, bool force_p,
15467 bool current_matrix_p)
15469 struct glyph_matrix *matrix;
15470 struct glyph_row *row;
15471 int window_height;
15473 if (!make_cursor_line_fully_visible_p)
15474 return true;
15476 /* It's not always possible to find the cursor, e.g, when a window
15477 is full of overlay strings. Don't do anything in that case. */
15478 if (w->cursor.vpos < 0)
15479 return true;
15481 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15482 row = MATRIX_ROW (matrix, w->cursor.vpos);
15484 /* If the cursor row is not partially visible, there's nothing to do. */
15485 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15486 return true;
15488 /* If the row the cursor is in is taller than the window's height,
15489 it's not clear what to do, so do nothing. */
15490 window_height = window_box_height (w);
15491 if (row->height >= window_height)
15493 if (!force_p || MINI_WINDOW_P (w)
15494 || w->vscroll || w->cursor.vpos == 0)
15495 return true;
15497 return false;
15501 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15502 means only WINDOW is redisplayed in redisplay_internal.
15503 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15504 in redisplay_window to bring a partially visible line into view in
15505 the case that only the cursor has moved.
15507 LAST_LINE_MISFIT should be true if we're scrolling because the
15508 last screen line's vertical height extends past the end of the screen.
15510 Value is
15512 1 if scrolling succeeded
15514 0 if scrolling didn't find point.
15516 -1 if new fonts have been loaded so that we must interrupt
15517 redisplay, adjust glyph matrices, and try again. */
15519 enum
15521 SCROLLING_SUCCESS,
15522 SCROLLING_FAILED,
15523 SCROLLING_NEED_LARGER_MATRICES
15526 /* If scroll-conservatively is more than this, never recenter.
15528 If you change this, don't forget to update the doc string of
15529 `scroll-conservatively' and the Emacs manual. */
15530 #define SCROLL_LIMIT 100
15532 static int
15533 try_scrolling (Lisp_Object window, bool just_this_one_p,
15534 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15535 bool temp_scroll_step, bool last_line_misfit)
15537 struct window *w = XWINDOW (window);
15538 struct text_pos pos, startp;
15539 struct it it;
15540 int this_scroll_margin, scroll_max, rc, height;
15541 int dy = 0, amount_to_scroll = 0;
15542 bool scroll_down_p = false;
15543 int extra_scroll_margin_lines = last_line_misfit;
15544 Lisp_Object aggressive;
15545 /* We will never try scrolling more than this number of lines. */
15546 int scroll_limit = SCROLL_LIMIT;
15547 int frame_line_height = default_line_pixel_height (w);
15549 #ifdef GLYPH_DEBUG
15550 debug_method_add (w, "try_scrolling");
15551 #endif
15553 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15555 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
15557 /* Force arg_scroll_conservatively to have a reasonable value, to
15558 avoid scrolling too far away with slow move_it_* functions. Note
15559 that the user can supply scroll-conservatively equal to
15560 `most-positive-fixnum', which can be larger than INT_MAX. */
15561 if (arg_scroll_conservatively > scroll_limit)
15563 arg_scroll_conservatively = scroll_limit + 1;
15564 scroll_max = scroll_limit * frame_line_height;
15566 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15567 /* Compute how much we should try to scroll maximally to bring
15568 point into view. */
15569 scroll_max = (max (scroll_step,
15570 max (arg_scroll_conservatively, temp_scroll_step))
15571 * frame_line_height);
15572 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15573 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15574 /* We're trying to scroll because of aggressive scrolling but no
15575 scroll_step is set. Choose an arbitrary one. */
15576 scroll_max = 10 * frame_line_height;
15577 else
15578 scroll_max = 0;
15580 too_near_end:
15582 /* Decide whether to scroll down. */
15583 if (PT > CHARPOS (startp))
15585 int scroll_margin_y;
15587 /* Compute the pixel ypos of the scroll margin, then move IT to
15588 either that ypos or PT, whichever comes first. */
15589 start_display (&it, w, startp);
15590 scroll_margin_y = it.last_visible_y - partial_line_height (&it)
15591 - this_scroll_margin
15592 - frame_line_height * extra_scroll_margin_lines;
15593 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15594 (MOVE_TO_POS | MOVE_TO_Y));
15596 if (PT > CHARPOS (it.current.pos))
15598 int y0 = line_bottom_y (&it);
15599 /* Compute how many pixels below window bottom to stop searching
15600 for PT. This avoids costly search for PT that is far away if
15601 the user limited scrolling by a small number of lines, but
15602 always finds PT if scroll_conservatively is set to a large
15603 number, such as most-positive-fixnum. */
15604 int slack = max (scroll_max, 10 * frame_line_height);
15605 int y_to_move = it.last_visible_y + slack;
15607 /* Compute the distance from the scroll margin to PT or to
15608 the scroll limit, whichever comes first. This should
15609 include the height of the cursor line, to make that line
15610 fully visible. */
15611 move_it_to (&it, PT, -1, y_to_move,
15612 -1, MOVE_TO_POS | MOVE_TO_Y);
15613 dy = line_bottom_y (&it) - y0;
15615 if (dy > scroll_max)
15616 return SCROLLING_FAILED;
15618 if (dy > 0)
15619 scroll_down_p = true;
15621 else if (PT == IT_CHARPOS (it)
15622 && IT_CHARPOS (it) < ZV
15623 && it.method == GET_FROM_STRING
15624 && arg_scroll_conservatively > scroll_limit
15625 && it.current_x == 0)
15627 enum move_it_result skip;
15628 int y1 = it.current_y;
15629 int vpos;
15631 /* A before-string that includes newlines and is displayed
15632 on the last visible screen line could fail us under
15633 scroll-conservatively > 100, because we will be unable to
15634 position the cursor on that last visible line. Try to
15635 recover by finding the first screen line that has some
15636 glyphs coming from the buffer text. */
15637 do {
15638 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15639 if (skip != MOVE_NEWLINE_OR_CR
15640 || IT_CHARPOS (it) != PT
15641 || it.method == GET_FROM_BUFFER)
15642 break;
15643 vpos = it.vpos;
15644 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15645 } while (it.vpos > vpos);
15647 dy = it.current_y - y1;
15649 if (dy > scroll_max)
15650 return SCROLLING_FAILED;
15652 if (dy > 0)
15653 scroll_down_p = true;
15657 if (scroll_down_p)
15659 /* Point is in or below the bottom scroll margin, so move the
15660 window start down. If scrolling conservatively, move it just
15661 enough down to make point visible. If scroll_step is set,
15662 move it down by scroll_step. */
15663 if (arg_scroll_conservatively)
15664 amount_to_scroll
15665 = min (max (dy, frame_line_height),
15666 frame_line_height * arg_scroll_conservatively);
15667 else if (scroll_step || temp_scroll_step)
15668 amount_to_scroll = scroll_max;
15669 else
15671 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15672 height = WINDOW_BOX_TEXT_HEIGHT (w);
15673 if (NUMBERP (aggressive))
15675 double float_amount = XFLOATINT (aggressive) * height;
15676 int aggressive_scroll = float_amount;
15677 if (aggressive_scroll == 0 && float_amount > 0)
15678 aggressive_scroll = 1;
15679 /* Don't let point enter the scroll margin near top of
15680 the window. This could happen if the value of
15681 scroll_up_aggressively is too large and there are
15682 non-zero margins, because scroll_up_aggressively
15683 means put point that fraction of window height
15684 _from_the_bottom_margin_. */
15685 if (aggressive_scroll + 2 * this_scroll_margin > height)
15686 aggressive_scroll = height - 2 * this_scroll_margin;
15687 amount_to_scroll = dy + aggressive_scroll;
15691 if (amount_to_scroll <= 0)
15692 return SCROLLING_FAILED;
15694 start_display (&it, w, startp);
15695 if (arg_scroll_conservatively <= scroll_limit)
15696 move_it_vertically (&it, amount_to_scroll);
15697 else
15699 /* Extra precision for users who set scroll-conservatively
15700 to a large number: make sure the amount we scroll
15701 the window start is never less than amount_to_scroll,
15702 which was computed as distance from window bottom to
15703 point. This matters when lines at window top and lines
15704 below window bottom have different height. */
15705 struct it it1;
15706 void *it1data = NULL;
15707 /* We use a temporary it1 because line_bottom_y can modify
15708 its argument, if it moves one line down; see there. */
15709 int start_y;
15711 SAVE_IT (it1, it, it1data);
15712 start_y = line_bottom_y (&it1);
15713 do {
15714 RESTORE_IT (&it, &it, it1data);
15715 move_it_by_lines (&it, 1);
15716 SAVE_IT (it1, it, it1data);
15717 } while (IT_CHARPOS (it) < ZV
15718 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15719 bidi_unshelve_cache (it1data, true);
15722 /* If STARTP is unchanged, move it down another screen line. */
15723 if (IT_CHARPOS (it) == CHARPOS (startp))
15724 move_it_by_lines (&it, 1);
15725 startp = it.current.pos;
15727 else
15729 struct text_pos scroll_margin_pos = startp;
15730 int y_offset = 0;
15732 /* See if point is inside the scroll margin at the top of the
15733 window. */
15734 if (this_scroll_margin)
15736 int y_start;
15738 start_display (&it, w, startp);
15739 y_start = it.current_y;
15740 move_it_vertically (&it, this_scroll_margin);
15741 scroll_margin_pos = it.current.pos;
15742 /* If we didn't move enough before hitting ZV, request
15743 additional amount of scroll, to move point out of the
15744 scroll margin. */
15745 if (IT_CHARPOS (it) == ZV
15746 && it.current_y - y_start < this_scroll_margin)
15747 y_offset = this_scroll_margin - (it.current_y - y_start);
15750 if (PT < CHARPOS (scroll_margin_pos))
15752 /* Point is in the scroll margin at the top of the window or
15753 above what is displayed in the window. */
15754 int y0, y_to_move;
15756 /* Compute the vertical distance from PT to the scroll
15757 margin position. Move as far as scroll_max allows, or
15758 one screenful, or 10 screen lines, whichever is largest.
15759 Give up if distance is greater than scroll_max or if we
15760 didn't reach the scroll margin position. */
15761 SET_TEXT_POS (pos, PT, PT_BYTE);
15762 start_display (&it, w, pos);
15763 y0 = it.current_y;
15764 y_to_move = max (it.last_visible_y,
15765 max (scroll_max, 10 * frame_line_height));
15766 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15767 y_to_move, -1,
15768 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15769 dy = it.current_y - y0;
15770 if (dy > scroll_max
15771 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15772 return SCROLLING_FAILED;
15774 /* Additional scroll for when ZV was too close to point. */
15775 dy += y_offset;
15777 /* Compute new window start. */
15778 start_display (&it, w, startp);
15780 if (arg_scroll_conservatively)
15781 amount_to_scroll = max (dy, frame_line_height
15782 * max (scroll_step, temp_scroll_step));
15783 else if (scroll_step || temp_scroll_step)
15784 amount_to_scroll = scroll_max;
15785 else
15787 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15788 height = WINDOW_BOX_TEXT_HEIGHT (w);
15789 if (NUMBERP (aggressive))
15791 double float_amount = XFLOATINT (aggressive) * height;
15792 int aggressive_scroll = float_amount;
15793 if (aggressive_scroll == 0 && float_amount > 0)
15794 aggressive_scroll = 1;
15795 /* Don't let point enter the scroll margin near
15796 bottom of the window, if the value of
15797 scroll_down_aggressively happens to be too
15798 large. */
15799 if (aggressive_scroll + 2 * this_scroll_margin > height)
15800 aggressive_scroll = height - 2 * this_scroll_margin;
15801 amount_to_scroll = dy + aggressive_scroll;
15805 if (amount_to_scroll <= 0)
15806 return SCROLLING_FAILED;
15808 move_it_vertically_backward (&it, amount_to_scroll);
15809 startp = it.current.pos;
15813 /* Run window scroll functions. */
15814 startp = run_window_scroll_functions (window, startp);
15816 /* Display the window. Give up if new fonts are loaded, or if point
15817 doesn't appear. */
15818 if (!try_window (window, startp, 0))
15819 rc = SCROLLING_NEED_LARGER_MATRICES;
15820 else if (w->cursor.vpos < 0)
15822 clear_glyph_matrix (w->desired_matrix);
15823 rc = SCROLLING_FAILED;
15825 else
15827 /* Maybe forget recorded base line for line number display. */
15828 if (!just_this_one_p
15829 || current_buffer->clip_changed
15830 || BEG_UNCHANGED < CHARPOS (startp))
15831 w->base_line_number = 0;
15833 /* If cursor ends up on a partially visible line,
15834 treat that as being off the bottom of the screen. */
15835 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15836 false)
15837 /* It's possible that the cursor is on the first line of the
15838 buffer, which is partially obscured due to a vscroll
15839 (Bug#7537). In that case, avoid looping forever. */
15840 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15842 clear_glyph_matrix (w->desired_matrix);
15843 ++extra_scroll_margin_lines;
15844 goto too_near_end;
15846 rc = SCROLLING_SUCCESS;
15849 return rc;
15853 /* Compute a suitable window start for window W if display of W starts
15854 on a continuation line. Value is true if a new window start
15855 was computed.
15857 The new window start will be computed, based on W's width, starting
15858 from the start of the continued line. It is the start of the
15859 screen line with the minimum distance from the old start W->start,
15860 which is still before point (otherwise point will definitely not
15861 be visible in the window). */
15863 static bool
15864 compute_window_start_on_continuation_line (struct window *w)
15866 struct text_pos pos, start_pos, pos_before_pt;
15867 bool window_start_changed_p = false;
15869 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15871 /* If window start is on a continuation line... Window start may be
15872 < BEGV in case there's invisible text at the start of the
15873 buffer (M-x rmail, for example). */
15874 if (CHARPOS (start_pos) > BEGV
15875 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15877 struct it it;
15878 struct glyph_row *row;
15880 /* Handle the case that the window start is out of range. */
15881 if (CHARPOS (start_pos) < BEGV)
15882 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15883 else if (CHARPOS (start_pos) > ZV)
15884 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15886 /* Find the start of the continued line. This should be fast
15887 because find_newline is fast (newline cache). */
15888 row = w->desired_matrix->rows + window_wants_header_line (w);
15889 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15890 row, DEFAULT_FACE_ID);
15891 reseat_at_previous_visible_line_start (&it);
15893 /* If the line start is "too far" away from the window start,
15894 say it takes too much time to compute a new window start.
15895 Also, give up if the line start is after point, as in that
15896 case point will not be visible with any window start we
15897 compute. */
15898 if (IT_CHARPOS (it) <= PT
15899 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15900 /* PXW: Do we need upper bounds here? */
15901 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
15903 int min_distance, distance;
15905 /* Move forward by display lines to find the new window
15906 start. If window width was enlarged, the new start can
15907 be expected to be > the old start. If window width was
15908 decreased, the new window start will be < the old start.
15909 So, we're looking for the display line start with the
15910 minimum distance from the old window start. */
15911 pos_before_pt = pos = it.current.pos;
15912 min_distance = DISP_INFINITY;
15913 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15914 distance < min_distance)
15916 min_distance = distance;
15917 if (CHARPOS (pos) <= PT)
15918 pos_before_pt = pos;
15919 pos = it.current.pos;
15920 if (it.line_wrap == WORD_WRAP)
15922 /* Under WORD_WRAP, move_it_by_lines is likely to
15923 overshoot and stop not at the first, but the
15924 second character from the left margin. So in
15925 that case, we need a more tight control on the X
15926 coordinate of the iterator than move_it_by_lines
15927 promises in its contract. The method is to first
15928 go to the last (rightmost) visible character of a
15929 line, then move to the leftmost character on the
15930 next line in a separate call. */
15931 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15932 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15933 move_it_to (&it, ZV, 0,
15934 it.current_y + it.max_ascent + it.max_descent, -1,
15935 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15937 else
15938 move_it_by_lines (&it, 1);
15941 /* It makes very little sense to make the new window start
15942 after point, as point won't be visible. If that's what
15943 the loop above finds, fall back on the candidate before
15944 or at point that is closest to the old window start. */
15945 if (CHARPOS (pos) > PT)
15946 pos = pos_before_pt;
15948 /* Set the window start there. */
15949 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15950 window_start_changed_p = true;
15954 return window_start_changed_p;
15958 /* Try cursor movement in case text has not changed in window WINDOW,
15959 with window start STARTP. Value is
15961 CURSOR_MOVEMENT_SUCCESS if successful
15963 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15965 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15966 display. *SCROLL_STEP is set to true, under certain circumstances, if
15967 we want to scroll as if scroll-step were set to 1. See the code.
15969 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15970 which case we have to abort this redisplay, and adjust matrices
15971 first. */
15973 enum
15975 CURSOR_MOVEMENT_SUCCESS,
15976 CURSOR_MOVEMENT_CANNOT_BE_USED,
15977 CURSOR_MOVEMENT_MUST_SCROLL,
15978 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15981 static int
15982 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15983 bool *scroll_step)
15985 struct window *w = XWINDOW (window);
15986 struct frame *f = XFRAME (w->frame);
15987 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15989 #ifdef GLYPH_DEBUG
15990 if (inhibit_try_cursor_movement)
15991 return rc;
15992 #endif
15994 /* Previously, there was a check for Lisp integer in the
15995 if-statement below. Now, this field is converted to
15996 ptrdiff_t, thus zero means invalid position in a buffer. */
15997 eassert (w->last_point > 0);
15998 /* Likewise there was a check whether window_end_vpos is nil or larger
15999 than the window. Now window_end_vpos is int and so never nil, but
16000 let's leave eassert to check whether it fits in the window. */
16001 eassert (!w->window_end_valid
16002 || w->window_end_vpos < w->current_matrix->nrows);
16004 /* Handle case where text has not changed, only point, and it has
16005 not moved off the frame. */
16006 if (/* Point may be in this window. */
16007 PT >= CHARPOS (startp)
16008 /* Selective display hasn't changed. */
16009 && !current_buffer->clip_changed
16010 /* Function force-mode-line-update is used to force a thorough
16011 redisplay. It sets either windows_or_buffers_changed or
16012 update_mode_lines. So don't take a shortcut here for these
16013 cases. */
16014 && !update_mode_lines
16015 && !windows_or_buffers_changed
16016 && !f->cursor_type_changed
16017 && NILP (Vshow_trailing_whitespace)
16018 /* When display-line-numbers is in relative mode, moving point
16019 requires to redraw the entire window. */
16020 && !EQ (Vdisplay_line_numbers, Qrelative)
16021 && !EQ (Vdisplay_line_numbers, Qvisual)
16022 /* When the current line number should be displayed in a
16023 distinct face, moving point cannot be handled in optimized
16024 way as below. */
16025 && !(!NILP (Vdisplay_line_numbers)
16026 && NILP (Finternal_lisp_face_equal_p (Qline_number,
16027 Qline_number_current_line,
16028 w->frame)))
16029 /* This code is not used for mini-buffer for the sake of the case
16030 of redisplaying to replace an echo area message; since in
16031 that case the mini-buffer contents per se are usually
16032 unchanged. This code is of no real use in the mini-buffer
16033 since the handling of this_line_start_pos, etc., in redisplay
16034 handles the same cases. */
16035 && !EQ (window, minibuf_window)
16036 && (FRAME_WINDOW_P (f)
16037 || !overlay_arrow_in_current_buffer_p ()))
16039 int this_scroll_margin, top_scroll_margin;
16040 struct glyph_row *row = NULL;
16042 #ifdef GLYPH_DEBUG
16043 debug_method_add (w, "cursor movement");
16044 #endif
16046 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
16048 top_scroll_margin = this_scroll_margin;
16049 if (window_wants_header_line (w))
16050 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
16052 /* Start with the row the cursor was displayed during the last
16053 not paused redisplay. Give up if that row is not valid. */
16054 if (w->last_cursor_vpos < 0
16055 || w->last_cursor_vpos >= w->current_matrix->nrows)
16056 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16057 else
16059 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
16060 if (row->mode_line_p)
16061 ++row;
16062 if (!row->enabled_p)
16063 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16066 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
16068 bool scroll_p = false, must_scroll = false;
16069 int last_y = window_text_bottom_y (w) - this_scroll_margin;
16071 if (PT > w->last_point)
16073 /* Point has moved forward. */
16074 while (MATRIX_ROW_END_CHARPOS (row) < PT
16075 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
16077 eassert (row->enabled_p);
16078 ++row;
16081 /* If the end position of a row equals the start
16082 position of the next row, and PT is at that position,
16083 we would rather display cursor in the next line. */
16084 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16085 && MATRIX_ROW_END_CHARPOS (row) == PT
16086 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
16087 && MATRIX_ROW_START_CHARPOS (row+1) == PT
16088 && !cursor_row_p (row))
16089 ++row;
16091 /* If within the scroll margin, scroll. Note that
16092 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
16093 the next line would be drawn, and that
16094 this_scroll_margin can be zero. */
16095 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
16096 || PT > MATRIX_ROW_END_CHARPOS (row)
16097 /* Line is completely visible last line in window
16098 and PT is to be set in the next line. */
16099 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
16100 && PT == MATRIX_ROW_END_CHARPOS (row)
16101 && !row->ends_at_zv_p
16102 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16103 scroll_p = true;
16105 else if (PT < w->last_point)
16107 /* Cursor has to be moved backward. Note that PT >=
16108 CHARPOS (startp) because of the outer if-statement. */
16109 while (!row->mode_line_p
16110 && (MATRIX_ROW_START_CHARPOS (row) > PT
16111 || (MATRIX_ROW_START_CHARPOS (row) == PT
16112 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
16113 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
16114 row > w->current_matrix->rows
16115 && (row-1)->ends_in_newline_from_string_p))))
16116 && (row->y > top_scroll_margin
16117 || CHARPOS (startp) == BEGV))
16119 eassert (row->enabled_p);
16120 --row;
16123 /* Consider the following case: Window starts at BEGV,
16124 there is invisible, intangible text at BEGV, so that
16125 display starts at some point START > BEGV. It can
16126 happen that we are called with PT somewhere between
16127 BEGV and START. Try to handle that case. */
16128 if (row < w->current_matrix->rows
16129 || row->mode_line_p)
16131 row = w->current_matrix->rows;
16132 if (row->mode_line_p)
16133 ++row;
16136 /* Due to newlines in overlay strings, we may have to
16137 skip forward over overlay strings. */
16138 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16139 && MATRIX_ROW_END_CHARPOS (row) == PT
16140 && !cursor_row_p (row))
16141 ++row;
16143 /* If within the scroll margin, scroll. */
16144 if (row->y < top_scroll_margin
16145 && CHARPOS (startp) != BEGV)
16146 scroll_p = true;
16148 else
16150 /* Cursor did not move. So don't scroll even if cursor line
16151 is partially visible, as it was so before. */
16152 rc = CURSOR_MOVEMENT_SUCCESS;
16155 if (PT < MATRIX_ROW_START_CHARPOS (row)
16156 || PT > MATRIX_ROW_END_CHARPOS (row))
16158 /* if PT is not in the glyph row, give up. */
16159 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16160 must_scroll = true;
16162 else if (rc != CURSOR_MOVEMENT_SUCCESS
16163 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16165 struct glyph_row *row1;
16167 /* If rows are bidi-reordered and point moved, back up
16168 until we find a row that does not belong to a
16169 continuation line. This is because we must consider
16170 all rows of a continued line as candidates for the
16171 new cursor positioning, since row start and end
16172 positions change non-linearly with vertical position
16173 in such rows. */
16174 /* FIXME: Revisit this when glyph ``spilling'' in
16175 continuation lines' rows is implemented for
16176 bidi-reordered rows. */
16177 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16178 MATRIX_ROW_CONTINUATION_LINE_P (row);
16179 --row)
16181 /* If we hit the beginning of the displayed portion
16182 without finding the first row of a continued
16183 line, give up. */
16184 if (row <= row1)
16186 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16187 break;
16189 eassert (row->enabled_p);
16192 if (must_scroll)
16194 else if (rc != CURSOR_MOVEMENT_SUCCESS
16195 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
16196 /* Make sure this isn't a header line by any chance, since
16197 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
16198 && !row->mode_line_p
16199 && make_cursor_line_fully_visible_p)
16201 if (PT == MATRIX_ROW_END_CHARPOS (row)
16202 && !row->ends_at_zv_p
16203 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16204 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16205 else if (row->height > window_box_height (w))
16207 /* If we end up in a partially visible line, let's
16208 make it fully visible, except when it's taller
16209 than the window, in which case we can't do much
16210 about it. */
16211 *scroll_step = true;
16212 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16214 else
16216 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16217 if (!cursor_row_fully_visible_p (w, false, true))
16218 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16219 else
16220 rc = CURSOR_MOVEMENT_SUCCESS;
16223 else if (scroll_p)
16224 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16225 else if (rc != CURSOR_MOVEMENT_SUCCESS
16226 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16228 /* With bidi-reordered rows, there could be more than
16229 one candidate row whose start and end positions
16230 occlude point. We need to let set_cursor_from_row
16231 find the best candidate. */
16232 /* FIXME: Revisit this when glyph ``spilling'' in
16233 continuation lines' rows is implemented for
16234 bidi-reordered rows. */
16235 bool rv = false;
16239 bool at_zv_p = false, exact_match_p = false;
16241 if (MATRIX_ROW_START_CHARPOS (row) <= PT
16242 && PT <= MATRIX_ROW_END_CHARPOS (row)
16243 && cursor_row_p (row))
16244 rv |= set_cursor_from_row (w, row, w->current_matrix,
16245 0, 0, 0, 0);
16246 /* As soon as we've found the exact match for point,
16247 or the first suitable row whose ends_at_zv_p flag
16248 is set, we are done. */
16249 if (rv)
16251 at_zv_p = MATRIX_ROW (w->current_matrix,
16252 w->cursor.vpos)->ends_at_zv_p;
16253 if (!at_zv_p
16254 && w->cursor.hpos >= 0
16255 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
16256 w->cursor.vpos))
16258 struct glyph_row *candidate =
16259 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16260 struct glyph *g =
16261 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
16262 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
16264 exact_match_p =
16265 (BUFFERP (g->object) && g->charpos == PT)
16266 || (NILP (g->object)
16267 && (g->charpos == PT
16268 || (g->charpos == 0 && endpos - 1 == PT)));
16270 if (at_zv_p || exact_match_p)
16272 rc = CURSOR_MOVEMENT_SUCCESS;
16273 break;
16276 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
16277 break;
16278 ++row;
16280 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
16281 || row->continued_p)
16282 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
16283 || (MATRIX_ROW_START_CHARPOS (row) == PT
16284 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
16285 /* If we didn't find any candidate rows, or exited the
16286 loop before all the candidates were examined, signal
16287 to the caller that this method failed. */
16288 if (rc != CURSOR_MOVEMENT_SUCCESS
16289 && !(rv
16290 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
16291 && !row->continued_p))
16292 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16293 else if (rv)
16294 rc = CURSOR_MOVEMENT_SUCCESS;
16296 else
16300 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16302 rc = CURSOR_MOVEMENT_SUCCESS;
16303 break;
16305 ++row;
16307 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16308 && MATRIX_ROW_START_CHARPOS (row) == PT
16309 && cursor_row_p (row));
16314 return rc;
16318 void
16319 set_vertical_scroll_bar (struct window *w)
16321 ptrdiff_t start, end, whole;
16323 /* Calculate the start and end positions for the current window.
16324 At some point, it would be nice to choose between scrollbars
16325 which reflect the whole buffer size, with special markers
16326 indicating narrowing, and scrollbars which reflect only the
16327 visible region.
16329 Note that mini-buffers sometimes aren't displaying any text. */
16330 if (!MINI_WINDOW_P (w)
16331 || (w == XWINDOW (minibuf_window)
16332 && NILP (echo_area_buffer[0])))
16334 struct buffer *buf = XBUFFER (w->contents);
16335 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16336 start = marker_position (w->start) - BUF_BEGV (buf);
16337 /* I don't think this is guaranteed to be right. For the
16338 moment, we'll pretend it is. */
16339 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16341 if (end < start)
16342 end = start;
16343 if (whole < (end - start))
16344 whole = end - start;
16346 else
16347 start = end = whole = 0;
16349 /* Indicate what this scroll bar ought to be displaying now. */
16350 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16351 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16352 (w, end - start, whole, start);
16356 void
16357 set_horizontal_scroll_bar (struct window *w)
16359 int start, end, whole, portion;
16361 if (!MINI_WINDOW_P (w)
16362 || (w == XWINDOW (minibuf_window)
16363 && NILP (echo_area_buffer[0])))
16365 struct buffer *b = XBUFFER (w->contents);
16366 struct buffer *old_buffer = NULL;
16367 struct it it;
16368 struct text_pos startp;
16370 if (b != current_buffer)
16372 old_buffer = current_buffer;
16373 set_buffer_internal (b);
16376 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16377 start_display (&it, w, startp);
16378 it.last_visible_x = INT_MAX;
16379 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16380 MOVE_TO_X | MOVE_TO_Y);
16381 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16382 window_box_height (w), -1,
16383 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16385 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16386 end = start + window_box_width (w, TEXT_AREA);
16387 portion = end - start;
16388 /* After enlarging a horizontally scrolled window such that it
16389 gets at least as wide as the text it contains, make sure that
16390 the thumb doesn't fill the entire scroll bar so we can still
16391 drag it back to see the entire text. */
16392 whole = max (whole, end);
16394 if (it.bidi_p)
16396 Lisp_Object pdir;
16398 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16399 if (EQ (pdir, Qright_to_left))
16401 start = whole - end;
16402 end = start + portion;
16406 if (old_buffer)
16407 set_buffer_internal (old_buffer);
16409 else
16410 start = end = whole = portion = 0;
16412 w->hscroll_whole = whole;
16414 /* Indicate what this scroll bar ought to be displaying now. */
16415 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16416 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16417 (w, portion, whole, start);
16421 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16422 selected_window is redisplayed.
16424 We can return without actually redisplaying the window if fonts has been
16425 changed on window's frame. In that case, redisplay_internal will retry.
16427 As one of the important parts of redisplaying a window, we need to
16428 decide whether the previous window-start position (stored in the
16429 window's w->start marker position) is still valid, and if it isn't,
16430 recompute it. Some details about that:
16432 . The previous window-start could be in a continuation line, in
16433 which case we need to recompute it when the window width
16434 changes. See compute_window_start_on_continuation_line and its
16435 call below.
16437 . The text that changed since last redisplay could include the
16438 previous window-start position. In that case, we try to salvage
16439 what we can from the current glyph matrix by calling
16440 try_scrolling, which see.
16442 . Some Emacs command could force us to use a specific window-start
16443 position by setting the window's force_start flag, or gently
16444 propose doing that by setting the window's optional_new_start
16445 flag. In these cases, we try using the specified start point if
16446 that succeeds (i.e. the window desired matrix is successfully
16447 recomputed, and point location is within the window). In case
16448 of optional_new_start, we first check if the specified start
16449 position is feasible, i.e. if it will allow point to be
16450 displayed in the window. If using the specified start point
16451 fails, e.g., if new fonts are needed to be loaded, we abort the
16452 redisplay cycle and leave it up to the next cycle to figure out
16453 things.
16455 . Note that the window's force_start flag is sometimes set by
16456 redisplay itself, when it decides that the previous window start
16457 point is fine and should be kept. Search for "goto force_start"
16458 below to see the details. Like the values of window-start
16459 specified outside of redisplay, these internally-deduced values
16460 are tested for feasibility, and ignored if found to be
16461 unfeasible.
16463 . Note that the function try_window, used to completely redisplay
16464 a window, accepts the window's start point as its argument.
16465 This is used several times in the redisplay code to control
16466 where the window start will be, according to user options such
16467 as scroll-conservatively, and also to ensure the screen line
16468 showing point will be fully (as opposed to partially) visible on
16469 display. */
16471 static void
16472 redisplay_window (Lisp_Object window, bool just_this_one_p)
16474 struct window *w = XWINDOW (window);
16475 struct frame *f = XFRAME (w->frame);
16476 struct buffer *buffer = XBUFFER (w->contents);
16477 struct buffer *old = current_buffer;
16478 struct text_pos lpoint, opoint, startp;
16479 bool update_mode_line;
16480 int tem;
16481 struct it it;
16482 /* Record it now because it's overwritten. */
16483 bool current_matrix_up_to_date_p = false;
16484 bool used_current_matrix_p = false;
16485 /* This is less strict than current_matrix_up_to_date_p.
16486 It indicates that the buffer contents and narrowing are unchanged. */
16487 bool buffer_unchanged_p = false;
16488 bool temp_scroll_step = false;
16489 ptrdiff_t count = SPECPDL_INDEX ();
16490 int rc;
16491 int centering_position = -1;
16492 bool last_line_misfit = false;
16493 ptrdiff_t beg_unchanged, end_unchanged;
16494 int frame_line_height, margin;
16495 bool use_desired_matrix;
16496 void *itdata = NULL;
16498 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16499 opoint = lpoint;
16501 #ifdef GLYPH_DEBUG
16502 *w->desired_matrix->method = 0;
16503 #endif
16505 if (!just_this_one_p
16506 && REDISPLAY_SOME_P ()
16507 && !w->redisplay
16508 && !w->update_mode_line
16509 && !f->face_change
16510 && !f->redisplay
16511 && !buffer->text->redisplay
16512 && BUF_PT (buffer) == w->last_point)
16513 return;
16515 /* Make sure that both W's markers are valid. */
16516 eassert (XMARKER (w->start)->buffer == buffer);
16517 eassert (XMARKER (w->pointm)->buffer == buffer);
16519 reconsider_clip_changes (w);
16520 frame_line_height = default_line_pixel_height (w);
16521 margin = window_scroll_margin (w, MARGIN_IN_LINES);
16524 /* Has the mode line to be updated? */
16525 update_mode_line = (w->update_mode_line
16526 || update_mode_lines
16527 || buffer->clip_changed
16528 || buffer->prevent_redisplay_optimizations_p);
16530 if (!just_this_one_p)
16531 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16532 cleverly elsewhere. */
16533 w->must_be_updated_p = true;
16535 if (MINI_WINDOW_P (w))
16537 if (w == XWINDOW (echo_area_window)
16538 && !NILP (echo_area_buffer[0]))
16540 if (update_mode_line)
16541 /* We may have to update a tty frame's menu bar or a
16542 tool-bar. Example `M-x C-h C-h C-g'. */
16543 goto finish_menu_bars;
16544 else
16545 /* We've already displayed the echo area glyphs in this window. */
16546 goto finish_scroll_bars;
16548 else if ((w != XWINDOW (minibuf_window)
16549 || minibuf_level == 0)
16550 /* When buffer is nonempty, redisplay window normally. */
16551 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16552 /* Quail displays non-mini buffers in minibuffer window.
16553 In that case, redisplay the window normally. */
16554 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16556 /* W is a mini-buffer window, but it's not active, so clear
16557 it. */
16558 int yb = window_text_bottom_y (w);
16559 struct glyph_row *row;
16560 int y;
16562 for (y = 0, row = w->desired_matrix->rows;
16563 y < yb;
16564 y += row->height, ++row)
16565 blank_row (w, row, y);
16566 goto finish_scroll_bars;
16569 clear_glyph_matrix (w->desired_matrix);
16572 /* Otherwise set up data on this window; select its buffer and point
16573 value. */
16574 /* Really select the buffer, for the sake of buffer-local
16575 variables. */
16576 set_buffer_internal_1 (XBUFFER (w->contents));
16578 current_matrix_up_to_date_p
16579 = (w->window_end_valid
16580 && !current_buffer->clip_changed
16581 && !current_buffer->prevent_redisplay_optimizations_p
16582 && !window_outdated (w)
16583 && !hscrolling_current_line_p (w));
16585 beg_unchanged = BEG_UNCHANGED;
16586 end_unchanged = END_UNCHANGED;
16588 SET_TEXT_POS (opoint, PT, PT_BYTE);
16590 specbind (Qinhibit_point_motion_hooks, Qt);
16592 buffer_unchanged_p
16593 = (w->window_end_valid
16594 && !current_buffer->clip_changed
16595 && !window_outdated (w));
16597 /* When windows_or_buffers_changed is non-zero, we can't rely
16598 on the window end being valid, so set it to zero there. */
16599 if (windows_or_buffers_changed)
16601 /* If window starts on a continuation line, maybe adjust the
16602 window start in case the window's width changed. */
16603 if (XMARKER (w->start)->buffer == current_buffer)
16604 compute_window_start_on_continuation_line (w);
16606 w->window_end_valid = false;
16607 /* If so, we also can't rely on current matrix
16608 and should not fool try_cursor_movement below. */
16609 current_matrix_up_to_date_p = false;
16612 /* Some sanity checks. */
16613 CHECK_WINDOW_END (w);
16614 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16615 emacs_abort ();
16616 if (BYTEPOS (opoint) < CHARPOS (opoint))
16617 emacs_abort ();
16619 if (mode_line_update_needed (w))
16620 update_mode_line = true;
16622 /* Point refers normally to the selected window. For any other
16623 window, set up appropriate value. */
16624 if (!EQ (window, selected_window))
16626 ptrdiff_t new_pt = marker_position (w->pointm);
16627 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16629 if (new_pt < BEGV)
16631 new_pt = BEGV;
16632 new_pt_byte = BEGV_BYTE;
16633 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16635 else if (new_pt > (ZV - 1))
16637 new_pt = ZV;
16638 new_pt_byte = ZV_BYTE;
16639 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16642 /* We don't use SET_PT so that the point-motion hooks don't run. */
16643 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16646 /* If any of the character widths specified in the display table
16647 have changed, invalidate the width run cache. It's true that
16648 this may be a bit late to catch such changes, but the rest of
16649 redisplay goes (non-fatally) haywire when the display table is
16650 changed, so why should we worry about doing any better? */
16651 if (current_buffer->width_run_cache
16652 || (current_buffer->base_buffer
16653 && current_buffer->base_buffer->width_run_cache))
16655 struct Lisp_Char_Table *disptab = buffer_display_table ();
16657 if (! disptab_matches_widthtab
16658 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16660 struct buffer *buf = current_buffer;
16662 if (buf->base_buffer)
16663 buf = buf->base_buffer;
16664 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16665 recompute_width_table (current_buffer, disptab);
16669 /* If window-start is screwed up, choose a new one. */
16670 if (XMARKER (w->start)->buffer != current_buffer)
16671 goto recenter;
16673 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16675 /* If someone specified a new starting point but did not insist,
16676 check whether it can be used. */
16677 if ((w->optional_new_start || window_frozen_p (w))
16678 && CHARPOS (startp) >= BEGV
16679 && CHARPOS (startp) <= ZV)
16681 ptrdiff_t it_charpos;
16683 w->optional_new_start = false;
16684 start_display (&it, w, startp);
16685 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16686 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16687 /* Record IT's position now, since line_bottom_y might change
16688 that. */
16689 it_charpos = IT_CHARPOS (it);
16690 /* Make sure we set the force_start flag only if the cursor row
16691 will be fully visible. Otherwise, the code under force_start
16692 label below will try to move point back into view, which is
16693 not what the code which sets optional_new_start wants. */
16694 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16695 && !w->force_start)
16697 if (it_charpos == PT)
16698 w->force_start = true;
16699 /* IT may overshoot PT if text at PT is invisible. */
16700 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16701 w->force_start = true;
16702 #ifdef GLYPH_DEBUG
16703 if (w->force_start)
16705 if (window_frozen_p (w))
16706 debug_method_add (w, "set force_start from frozen window start");
16707 else
16708 debug_method_add (w, "set force_start from optional_new_start");
16710 #endif
16714 force_start:
16716 /* Handle case where place to start displaying has been specified,
16717 unless the specified location is outside the accessible range. */
16718 if (w->force_start)
16720 /* We set this later on if we have to adjust point. */
16721 int new_vpos = -1;
16723 w->force_start = false;
16724 w->vscroll = 0;
16725 w->window_end_valid = false;
16727 /* Forget any recorded base line for line number display. */
16728 if (!buffer_unchanged_p)
16729 w->base_line_number = 0;
16731 /* Redisplay the mode line. Select the buffer properly for that.
16732 Also, run the hook window-scroll-functions
16733 because we have scrolled. */
16734 /* Note, we do this after clearing force_start because
16735 if there's an error, it is better to forget about force_start
16736 than to get into an infinite loop calling the hook functions
16737 and having them get more errors. */
16738 if (!update_mode_line
16739 || ! NILP (Vwindow_scroll_functions))
16741 update_mode_line = true;
16742 w->update_mode_line = true;
16743 startp = run_window_scroll_functions (window, startp);
16746 if (CHARPOS (startp) < BEGV)
16747 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16748 else if (CHARPOS (startp) > ZV)
16749 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16751 /* Redisplay, then check if cursor has been set during the
16752 redisplay. Give up if new fonts were loaded. */
16753 /* We used to issue a CHECK_MARGINS argument to try_window here,
16754 but this causes scrolling to fail when point begins inside
16755 the scroll margin (bug#148) -- cyd */
16756 if (!try_window (window, startp, 0))
16758 w->force_start = true;
16759 clear_glyph_matrix (w->desired_matrix);
16760 goto need_larger_matrices;
16763 if (w->cursor.vpos < 0)
16765 /* If point does not appear, try to move point so it does
16766 appear. The desired matrix has been built above, so we
16767 can use it here. First see if point is in invisible
16768 text, and if so, move it to the first visible buffer
16769 position past that. */
16770 struct glyph_row *r = NULL;
16771 Lisp_Object invprop =
16772 get_char_property_and_overlay (make_number (PT), Qinvisible,
16773 Qnil, NULL);
16775 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16777 ptrdiff_t alt_pt;
16778 Lisp_Object invprop_end =
16779 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16780 Qnil, Qnil);
16782 if (NATNUMP (invprop_end))
16783 alt_pt = XFASTINT (invprop_end);
16784 else
16785 alt_pt = ZV;
16786 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16787 NULL, 0);
16789 if (r)
16790 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16791 else /* Give up and just move to the middle of the window. */
16792 new_vpos = window_box_height (w) / 2;
16795 if (!cursor_row_fully_visible_p (w, false, false))
16797 /* Point does appear, but on a line partly visible at end of window.
16798 Move it back to a fully-visible line. */
16799 new_vpos = window_box_height (w);
16800 /* But if window_box_height suggests a Y coordinate that is
16801 not less than we already have, that line will clearly not
16802 be fully visible, so give up and scroll the display.
16803 This can happen when the default face uses a font whose
16804 dimensions are different from the frame's default
16805 font. */
16806 if (new_vpos >= w->cursor.y)
16808 w->cursor.vpos = -1;
16809 clear_glyph_matrix (w->desired_matrix);
16810 goto try_to_scroll;
16813 else if (w->cursor.vpos >= 0)
16815 /* Some people insist on not letting point enter the scroll
16816 margin, even though this part handles windows that didn't
16817 scroll at all. */
16818 int pixel_margin = margin * frame_line_height;
16819 bool header_line = window_wants_header_line (w);
16821 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16822 below, which finds the row to move point to, advances by
16823 the Y coordinate of the _next_ row, see the definition of
16824 MATRIX_ROW_BOTTOM_Y. */
16825 if (w->cursor.vpos < margin + header_line)
16827 w->cursor.vpos = -1;
16828 clear_glyph_matrix (w->desired_matrix);
16829 goto try_to_scroll;
16831 else
16833 int window_height = window_box_height (w);
16835 if (header_line)
16836 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16837 if (w->cursor.y >= window_height - pixel_margin)
16839 w->cursor.vpos = -1;
16840 clear_glyph_matrix (w->desired_matrix);
16841 goto try_to_scroll;
16846 /* If we need to move point for either of the above reasons,
16847 now actually do it. */
16848 if (new_vpos >= 0)
16850 struct glyph_row *row;
16852 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16853 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16854 ++row;
16856 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16857 MATRIX_ROW_START_BYTEPOS (row));
16859 if (w != XWINDOW (selected_window))
16860 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16861 else if (current_buffer == old)
16862 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16864 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16866 /* Re-run pre-redisplay-function so it can update the region
16867 according to the new position of point. */
16868 /* Other than the cursor, w's redisplay is done so we can set its
16869 redisplay to false. Also the buffer's redisplay can be set to
16870 false, since propagate_buffer_redisplay should have already
16871 propagated its info to `w' anyway. */
16872 w->redisplay = false;
16873 XBUFFER (w->contents)->text->redisplay = false;
16874 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16876 if (w->redisplay || XBUFFER (w->contents)->text->redisplay
16877 || ((EQ (Vdisplay_line_numbers, Qrelative)
16878 || EQ (Vdisplay_line_numbers, Qvisual))
16879 && row != MATRIX_FIRST_TEXT_ROW (w->desired_matrix)))
16881 /* Either pre-redisplay-function made changes (e.g. move
16882 the region), or we moved point in a window that is
16883 under display-line-numbers = relative mode. We need
16884 another round of redisplay. */
16885 clear_glyph_matrix (w->desired_matrix);
16886 if (!try_window (window, startp, 0))
16887 goto need_larger_matrices;
16890 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16892 clear_glyph_matrix (w->desired_matrix);
16893 goto try_to_scroll;
16896 #ifdef GLYPH_DEBUG
16897 debug_method_add (w, "forced window start");
16898 #endif
16899 goto done;
16902 /* Handle case where text has not changed, only point, and it has
16903 not moved off the frame, and we are not retrying after hscroll.
16904 (current_matrix_up_to_date_p is true when retrying.) */
16905 if (current_matrix_up_to_date_p
16906 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16907 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16909 switch (rc)
16911 case CURSOR_MOVEMENT_SUCCESS:
16912 used_current_matrix_p = true;
16913 goto done;
16915 case CURSOR_MOVEMENT_MUST_SCROLL:
16916 goto try_to_scroll;
16918 default:
16919 emacs_abort ();
16922 /* If current starting point was originally the beginning of a line
16923 but no longer is, find a new starting point. */
16924 else if (w->start_at_line_beg
16925 && !(CHARPOS (startp) <= BEGV
16926 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16928 #ifdef GLYPH_DEBUG
16929 debug_method_add (w, "recenter 1");
16930 #endif
16931 goto recenter;
16934 /* Try scrolling with try_window_id. Value is > 0 if update has
16935 been done, it is -1 if we know that the same window start will
16936 not work. It is 0 if unsuccessful for some other reason. */
16937 else if ((tem = try_window_id (w)) != 0)
16939 #ifdef GLYPH_DEBUG
16940 debug_method_add (w, "try_window_id %d", tem);
16941 #endif
16943 if (f->fonts_changed)
16944 goto need_larger_matrices;
16945 if (tem > 0)
16946 goto done;
16948 /* Otherwise try_window_id has returned -1 which means that we
16949 don't want the alternative below this comment to execute. */
16951 else if (CHARPOS (startp) >= BEGV
16952 && CHARPOS (startp) <= ZV
16953 && PT >= CHARPOS (startp)
16954 && (CHARPOS (startp) < ZV
16955 /* Avoid starting at end of buffer. */
16956 || CHARPOS (startp) == BEGV
16957 || !window_outdated (w)))
16959 int d1, d2, d5, d6;
16960 int rtop, rbot;
16962 /* If first window line is a continuation line, and window start
16963 is inside the modified region, but the first change is before
16964 current window start, we must select a new window start.
16966 However, if this is the result of a down-mouse event (e.g. by
16967 extending the mouse-drag-overlay), we don't want to select a
16968 new window start, since that would change the position under
16969 the mouse, resulting in an unwanted mouse-movement rather
16970 than a simple mouse-click. */
16971 if (!w->start_at_line_beg
16972 && NILP (do_mouse_tracking)
16973 && CHARPOS (startp) > BEGV
16974 && CHARPOS (startp) > BEG + beg_unchanged
16975 && CHARPOS (startp) <= Z - end_unchanged
16976 /* Even if w->start_at_line_beg is nil, a new window may
16977 start at a line_beg, since that's how set_buffer_window
16978 sets it. So, we need to check the return value of
16979 compute_window_start_on_continuation_line. (See also
16980 bug#197). */
16981 && XMARKER (w->start)->buffer == current_buffer
16982 && compute_window_start_on_continuation_line (w)
16983 /* It doesn't make sense to force the window start like we
16984 do at label force_start if it is already known that point
16985 will not be fully visible in the resulting window, because
16986 doing so will move point from its correct position
16987 instead of scrolling the window to bring point into view.
16988 See bug#9324. */
16989 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16990 /* A very tall row could need more than the window height,
16991 in which case we accept that it is partially visible. */
16992 && (rtop != 0) == (rbot != 0))
16994 w->force_start = true;
16995 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16996 #ifdef GLYPH_DEBUG
16997 debug_method_add (w, "recomputed window start in continuation line");
16998 #endif
16999 goto force_start;
17002 #ifdef GLYPH_DEBUG
17003 debug_method_add (w, "same window start");
17004 #endif
17006 /* Try to redisplay starting at same place as before.
17007 If point has not moved off frame, accept the results. */
17008 if (!current_matrix_up_to_date_p
17009 /* Don't use try_window_reusing_current_matrix in this case
17010 because a window scroll function can have changed the
17011 buffer. */
17012 || !NILP (Vwindow_scroll_functions)
17013 || MINI_WINDOW_P (w)
17014 || !(used_current_matrix_p
17015 = try_window_reusing_current_matrix (w)))
17017 IF_DEBUG (debug_method_add (w, "1"));
17018 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
17019 /* -1 means we need to scroll.
17020 0 means we need new matrices, but fonts_changed
17021 is set in that case, so we will detect it below. */
17022 goto try_to_scroll;
17025 if (f->fonts_changed)
17026 goto need_larger_matrices;
17028 if (w->cursor.vpos >= 0)
17030 if (!just_this_one_p
17031 || current_buffer->clip_changed
17032 || BEG_UNCHANGED < CHARPOS (startp))
17033 /* Forget any recorded base line for line number display. */
17034 w->base_line_number = 0;
17036 if (!cursor_row_fully_visible_p (w, true, false))
17038 clear_glyph_matrix (w->desired_matrix);
17039 last_line_misfit = true;
17041 /* Drop through and scroll. */
17042 else
17043 goto done;
17045 else
17046 clear_glyph_matrix (w->desired_matrix);
17049 try_to_scroll:
17051 /* Redisplay the mode line. Select the buffer properly for that. */
17052 if (!update_mode_line)
17054 update_mode_line = true;
17055 w->update_mode_line = true;
17058 /* Try to scroll by specified few lines. */
17059 if ((scroll_conservatively
17060 || emacs_scroll_step
17061 || temp_scroll_step
17062 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
17063 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
17064 && CHARPOS (startp) >= BEGV
17065 && CHARPOS (startp) <= ZV)
17067 /* The function returns -1 if new fonts were loaded, 1 if
17068 successful, 0 if not successful. */
17069 int ss = try_scrolling (window, just_this_one_p,
17070 scroll_conservatively,
17071 emacs_scroll_step,
17072 temp_scroll_step, last_line_misfit);
17073 switch (ss)
17075 case SCROLLING_SUCCESS:
17076 goto done;
17078 case SCROLLING_NEED_LARGER_MATRICES:
17079 goto need_larger_matrices;
17081 case SCROLLING_FAILED:
17082 break;
17084 default:
17085 emacs_abort ();
17089 /* Finally, just choose a place to start which positions point
17090 according to user preferences. */
17092 recenter:
17094 #ifdef GLYPH_DEBUG
17095 debug_method_add (w, "recenter");
17096 #endif
17098 /* Forget any previously recorded base line for line number display. */
17099 if (!buffer_unchanged_p)
17100 w->base_line_number = 0;
17102 /* Determine the window start relative to point. */
17103 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17104 it.current_y = it.last_visible_y;
17105 if (centering_position < 0)
17107 ptrdiff_t margin_pos = CHARPOS (startp);
17108 Lisp_Object aggressive;
17109 bool scrolling_up;
17111 /* If there is a scroll margin at the top of the window, find
17112 its character position. */
17113 if (margin
17114 /* Cannot call start_display if startp is not in the
17115 accessible region of the buffer. This can happen when we
17116 have just switched to a different buffer and/or changed
17117 its restriction. In that case, startp is initialized to
17118 the character position 1 (BEGV) because we did not yet
17119 have chance to display the buffer even once. */
17120 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
17122 struct it it1;
17123 void *it1data = NULL;
17125 SAVE_IT (it1, it, it1data);
17126 start_display (&it1, w, startp);
17127 move_it_vertically (&it1, margin * frame_line_height);
17128 margin_pos = IT_CHARPOS (it1);
17129 RESTORE_IT (&it, &it, it1data);
17131 scrolling_up = PT > margin_pos;
17132 aggressive =
17133 scrolling_up
17134 ? BVAR (current_buffer, scroll_up_aggressively)
17135 : BVAR (current_buffer, scroll_down_aggressively);
17137 if (!MINI_WINDOW_P (w)
17138 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
17140 int pt_offset = 0;
17142 /* Setting scroll-conservatively overrides
17143 scroll-*-aggressively. */
17144 if (!scroll_conservatively && NUMBERP (aggressive))
17146 double float_amount = XFLOATINT (aggressive);
17148 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
17149 if (pt_offset == 0 && float_amount > 0)
17150 pt_offset = 1;
17151 if (pt_offset && margin > 0)
17152 margin -= 1;
17154 /* Compute how much to move the window start backward from
17155 point so that point will be displayed where the user
17156 wants it. */
17157 if (scrolling_up)
17159 centering_position = it.last_visible_y;
17160 if (pt_offset)
17161 centering_position -= pt_offset;
17162 centering_position -=
17163 (frame_line_height * (1 + margin + last_line_misfit)
17164 + WINDOW_HEADER_LINE_HEIGHT (w));
17165 /* Don't let point enter the scroll margin near top of
17166 the window. */
17167 if (centering_position < margin * frame_line_height)
17168 centering_position = margin * frame_line_height;
17170 else
17171 centering_position = margin * frame_line_height + pt_offset;
17173 else
17174 /* Set the window start half the height of the window backward
17175 from point. */
17176 centering_position = window_box_height (w) / 2;
17178 move_it_vertically_backward (&it, centering_position);
17180 eassert (IT_CHARPOS (it) >= BEGV);
17182 /* The function move_it_vertically_backward may move over more
17183 than the specified y-distance. If it->w is small, e.g. a
17184 mini-buffer window, we may end up in front of the window's
17185 display area. Start displaying at the start of the line
17186 containing PT in this case. */
17187 if (it.current_y <= 0)
17189 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17190 move_it_vertically_backward (&it, 0);
17191 it.current_y = 0;
17194 it.current_x = it.hpos = 0;
17196 /* Set the window start position here explicitly, to avoid an
17197 infinite loop in case the functions in window-scroll-functions
17198 get errors. */
17199 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
17201 /* Run scroll hooks. */
17202 startp = run_window_scroll_functions (window, it.current.pos);
17204 /* We invoke try_window and try_window_reusing_current_matrix below,
17205 and they manipulate the bidi cache. Save and restore the cache
17206 state of our iterator, so we could continue using it after that. */
17207 itdata = bidi_shelve_cache ();
17209 /* Redisplay the window. */
17210 use_desired_matrix = false;
17211 if (!current_matrix_up_to_date_p
17212 || windows_or_buffers_changed
17213 || f->cursor_type_changed
17214 /* Don't use try_window_reusing_current_matrix in this case
17215 because it can have changed the buffer. */
17216 || !NILP (Vwindow_scroll_functions)
17217 || !just_this_one_p
17218 || MINI_WINDOW_P (w)
17219 || !(used_current_matrix_p
17220 = try_window_reusing_current_matrix (w)))
17221 use_desired_matrix = (try_window (window, startp, 0) == 1);
17223 bidi_unshelve_cache (itdata, false);
17225 /* If new fonts have been loaded (due to fontsets), give up. We
17226 have to start a new redisplay since we need to re-adjust glyph
17227 matrices. */
17228 if (f->fonts_changed)
17229 goto need_larger_matrices;
17231 /* If cursor did not appear assume that the middle of the window is
17232 in the first line of the window. Do it again with the next line.
17233 (Imagine a window of height 100, displaying two lines of height
17234 60. Moving back 50 from it->last_visible_y will end in the first
17235 line.) */
17236 if (w->cursor.vpos < 0)
17238 if (w->window_end_valid && PT >= Z - w->window_end_pos)
17240 clear_glyph_matrix (w->desired_matrix);
17241 move_it_by_lines (&it, 1);
17242 try_window (window, it.current.pos, 0);
17244 else if (PT < IT_CHARPOS (it))
17246 clear_glyph_matrix (w->desired_matrix);
17247 move_it_by_lines (&it, -1);
17248 try_window (window, it.current.pos, 0);
17250 else if (scroll_conservatively > SCROLL_LIMIT
17251 && (it.method == GET_FROM_STRING
17252 || overlay_touches_p (IT_CHARPOS (it)))
17253 && IT_CHARPOS (it) < ZV)
17255 /* If the window starts with a before-string that spans more
17256 than one screen line, using that position to display the
17257 window might fail to bring point into the view, because
17258 start_display will always start by displaying the string,
17259 whereas the code above determines where to set w->start
17260 by the buffer position of the place where it takes screen
17261 coordinates. Try to recover by finding the next screen
17262 line that displays buffer text. */
17263 ptrdiff_t pos0 = IT_CHARPOS (it);
17265 clear_glyph_matrix (w->desired_matrix);
17266 do {
17267 move_it_by_lines (&it, 1);
17268 } while (IT_CHARPOS (it) == pos0);
17269 try_window (window, it.current.pos, 0);
17271 else
17273 /* Not much we can do about it. */
17277 /* Consider the following case: Window starts at BEGV, there is
17278 invisible, intangible text at BEGV, so that display starts at
17279 some point START > BEGV. It can happen that we are called with
17280 PT somewhere between BEGV and START. Try to handle that case,
17281 and similar ones. */
17282 if (w->cursor.vpos < 0)
17284 /* Prefer the desired matrix to the current matrix, if possible,
17285 in the fallback calculations below. This is because using
17286 the current matrix might completely goof, e.g. if its first
17287 row is after point. */
17288 struct glyph_matrix *matrix =
17289 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17290 /* First, try locating the proper glyph row for PT. */
17291 struct glyph_row *row =
17292 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17294 /* Sometimes point is at the beginning of invisible text that is
17295 before the 1st character displayed in the row. In that case,
17296 row_containing_pos fails to find the row, because no glyphs
17297 with appropriate buffer positions are present in the row.
17298 Therefore, we next try to find the row which shows the 1st
17299 position after the invisible text. */
17300 if (!row)
17302 Lisp_Object val =
17303 get_char_property_and_overlay (make_number (PT), Qinvisible,
17304 Qnil, NULL);
17306 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17308 ptrdiff_t alt_pos;
17309 Lisp_Object invis_end =
17310 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17311 Qnil, Qnil);
17313 if (NATNUMP (invis_end))
17314 alt_pos = XFASTINT (invis_end);
17315 else
17316 alt_pos = ZV;
17317 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17320 /* Finally, fall back on the first row of the window after the
17321 header line (if any). This is slightly better than not
17322 displaying the cursor at all. */
17323 if (!row)
17325 row = matrix->rows;
17326 if (row->mode_line_p)
17327 ++row;
17329 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17332 if (!cursor_row_fully_visible_p (w, false, false))
17334 /* If vscroll is enabled, disable it and try again. */
17335 if (w->vscroll)
17337 w->vscroll = 0;
17338 clear_glyph_matrix (w->desired_matrix);
17339 goto recenter;
17342 /* Users who set scroll-conservatively to a large number want
17343 point just above/below the scroll margin. If we ended up
17344 with point's row partially visible, move the window start to
17345 make that row fully visible and out of the margin. */
17346 if (scroll_conservatively > SCROLL_LIMIT)
17348 int window_total_lines
17349 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17350 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17352 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17353 clear_glyph_matrix (w->desired_matrix);
17354 if (1 == try_window (window, it.current.pos,
17355 TRY_WINDOW_CHECK_MARGINS))
17356 goto done;
17359 /* If centering point failed to make the whole line visible,
17360 put point at the top instead. That has to make the whole line
17361 visible, if it can be done. */
17362 if (centering_position == 0)
17363 goto done;
17365 clear_glyph_matrix (w->desired_matrix);
17366 centering_position = 0;
17367 goto recenter;
17370 done:
17372 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17373 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17374 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17376 /* Display the mode line, if we must. */
17377 if ((update_mode_line
17378 /* If window not full width, must redo its mode line
17379 if (a) the window to its side is being redone and
17380 (b) we do a frame-based redisplay. This is a consequence
17381 of how inverted lines are drawn in frame-based redisplay. */
17382 || (!just_this_one_p
17383 && !FRAME_WINDOW_P (f)
17384 && !WINDOW_FULL_WIDTH_P (w))
17385 /* Line number to display. */
17386 || w->base_line_pos > 0
17387 /* Column number is displayed and different from the one displayed. */
17388 || (w->column_number_displayed != -1
17389 && (w->column_number_displayed != current_column ())))
17390 /* This means that the window has a mode line. */
17391 && (window_wants_mode_line (w)
17392 || window_wants_header_line (w)))
17395 display_mode_lines (w);
17397 /* If mode line height has changed, arrange for a thorough
17398 immediate redisplay using the correct mode line height. */
17399 if (window_wants_mode_line (w)
17400 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17402 f->fonts_changed = true;
17403 w->mode_line_height = -1;
17404 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17405 = DESIRED_MODE_LINE_HEIGHT (w);
17408 /* If header line height has changed, arrange for a thorough
17409 immediate redisplay using the correct header line height. */
17410 if (window_wants_header_line (w)
17411 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17413 f->fonts_changed = true;
17414 w->header_line_height = -1;
17415 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17416 = DESIRED_HEADER_LINE_HEIGHT (w);
17419 if (f->fonts_changed)
17420 goto need_larger_matrices;
17423 if (!line_number_displayed && w->base_line_pos != -1)
17425 w->base_line_pos = 0;
17426 w->base_line_number = 0;
17429 finish_menu_bars:
17431 /* When we reach a frame's selected window, redo the frame's menu
17432 bar and the frame's title. */
17433 if (update_mode_line
17434 && EQ (FRAME_SELECTED_WINDOW (f), window))
17436 bool redisplay_menu_p;
17438 if (FRAME_WINDOW_P (f))
17440 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17441 || defined (HAVE_NS) || defined (USE_GTK)
17442 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17443 #else
17444 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17445 #endif
17447 else
17448 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17450 if (redisplay_menu_p)
17451 display_menu_bar (w);
17453 #ifdef HAVE_WINDOW_SYSTEM
17454 if (FRAME_WINDOW_P (f))
17456 #if defined (USE_GTK) || defined (HAVE_NS)
17457 if (FRAME_EXTERNAL_TOOL_BAR (f))
17458 redisplay_tool_bar (f);
17459 #else
17460 if (WINDOWP (f->tool_bar_window)
17461 && (FRAME_TOOL_BAR_LINES (f) > 0
17462 || !NILP (Vauto_resize_tool_bars))
17463 && redisplay_tool_bar (f))
17464 ignore_mouse_drag_p = true;
17465 #endif
17467 x_consider_frame_title (w->frame);
17468 #endif
17471 #ifdef HAVE_WINDOW_SYSTEM
17472 if (FRAME_WINDOW_P (f)
17473 && update_window_fringes (w, (just_this_one_p
17474 || (!used_current_matrix_p && !overlay_arrow_seen)
17475 || w->pseudo_window_p)))
17477 update_begin (f);
17478 block_input ();
17479 if (draw_window_fringes (w, true))
17481 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17482 x_draw_right_divider (w);
17483 else
17484 x_draw_vertical_border (w);
17486 unblock_input ();
17487 update_end (f);
17490 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17491 x_draw_bottom_divider (w);
17492 #endif /* HAVE_WINDOW_SYSTEM */
17494 /* We go to this label, with fonts_changed set, if it is
17495 necessary to try again using larger glyph matrices.
17496 We have to redeem the scroll bar even in this case,
17497 because the loop in redisplay_internal expects that. */
17498 need_larger_matrices:
17500 finish_scroll_bars:
17502 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17504 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17505 /* Set the thumb's position and size. */
17506 set_vertical_scroll_bar (w);
17508 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17509 /* Set the thumb's position and size. */
17510 set_horizontal_scroll_bar (w);
17512 /* Note that we actually used the scroll bar attached to this
17513 window, so it shouldn't be deleted at the end of redisplay. */
17514 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17515 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17518 /* Restore current_buffer and value of point in it. The window
17519 update may have changed the buffer, so first make sure `opoint'
17520 is still valid (Bug#6177). */
17521 if (CHARPOS (opoint) < BEGV)
17522 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17523 else if (CHARPOS (opoint) > ZV)
17524 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17525 else
17526 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17528 set_buffer_internal_1 (old);
17529 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17530 shorter. This can be caused by log truncation in *Messages*. */
17531 if (CHARPOS (lpoint) <= ZV)
17532 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17534 unbind_to (count, Qnil);
17538 /* Build the complete desired matrix of WINDOW with a window start
17539 buffer position POS.
17541 Value is 1 if successful. It is zero if fonts were loaded during
17542 redisplay which makes re-adjusting glyph matrices necessary, and -1
17543 if point would appear in the scroll margins.
17544 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17545 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17546 set in FLAGS.) */
17549 try_window (Lisp_Object window, struct text_pos pos, int flags)
17551 struct window *w = XWINDOW (window);
17552 struct it it;
17553 struct glyph_row *last_text_row = NULL;
17554 struct frame *f = XFRAME (w->frame);
17555 int cursor_vpos = w->cursor.vpos;
17557 /* Make POS the new window start. */
17558 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17560 /* Mark cursor position as unknown. No overlay arrow seen. */
17561 w->cursor.vpos = -1;
17562 overlay_arrow_seen = false;
17564 /* Initialize iterator and info to start at POS. */
17565 start_display (&it, w, pos);
17566 it.glyph_row->reversed_p = false;
17568 /* Display all lines of W. */
17569 while (it.current_y < it.last_visible_y)
17571 if (display_line (&it, cursor_vpos))
17572 last_text_row = it.glyph_row - 1;
17573 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17574 return 0;
17577 /* Save the character position of 'it' before we call
17578 'start_display' again. */
17579 ptrdiff_t it_charpos = IT_CHARPOS (it);
17581 /* Don't let the cursor end in the scroll margins. */
17582 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17583 && !MINI_WINDOW_P (w))
17585 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
17586 start_display (&it, w, pos);
17588 if ((w->cursor.y >= 0 /* not vscrolled */
17589 && w->cursor.y < this_scroll_margin
17590 && CHARPOS (pos) > BEGV
17591 && it_charpos < ZV)
17592 /* rms: considering make_cursor_line_fully_visible_p here
17593 seems to give wrong results. We don't want to recenter
17594 when the last line is partly visible, we want to allow
17595 that case to be handled in the usual way. */
17596 || w->cursor.y > (it.last_visible_y - partial_line_height (&it)
17597 - this_scroll_margin - 1))
17599 w->cursor.vpos = -1;
17600 clear_glyph_matrix (w->desired_matrix);
17601 return -1;
17605 /* If bottom moved off end of frame, change mode line percentage. */
17606 if (w->window_end_pos <= 0 && Z != it_charpos)
17607 w->update_mode_line = true;
17609 /* Set window_end_pos to the offset of the last character displayed
17610 on the window from the end of current_buffer. Set
17611 window_end_vpos to its row number. */
17612 if (last_text_row)
17614 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17615 adjust_window_ends (w, last_text_row, false);
17616 eassert
17617 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17618 w->window_end_vpos)));
17620 else
17622 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17623 w->window_end_pos = Z - ZV;
17624 w->window_end_vpos = 0;
17627 /* But that is not valid info until redisplay finishes. */
17628 w->window_end_valid = false;
17629 return 1;
17634 /************************************************************************
17635 Window redisplay reusing current matrix when buffer has not changed
17636 ************************************************************************/
17638 /* Try redisplay of window W showing an unchanged buffer with a
17639 different window start than the last time it was displayed by
17640 reusing its current matrix. Value is true if successful.
17641 W->start is the new window start. */
17643 static bool
17644 try_window_reusing_current_matrix (struct window *w)
17646 struct frame *f = XFRAME (w->frame);
17647 struct glyph_row *bottom_row;
17648 struct it it;
17649 struct run run;
17650 struct text_pos start, new_start;
17651 int nrows_scrolled, i;
17652 struct glyph_row *last_text_row;
17653 struct glyph_row *last_reused_text_row;
17654 struct glyph_row *start_row;
17655 int start_vpos, min_y, max_y;
17657 #ifdef GLYPH_DEBUG
17658 if (inhibit_try_window_reusing)
17659 return false;
17660 #endif
17662 if (/* This function doesn't handle terminal frames. */
17663 !FRAME_WINDOW_P (f)
17664 /* Don't try to reuse the display if windows have been split
17665 or such. */
17666 || windows_or_buffers_changed
17667 || f->cursor_type_changed)
17668 return false;
17670 /* Can't do this if showing trailing whitespace. */
17671 if (!NILP (Vshow_trailing_whitespace))
17672 return false;
17674 /* If top-line visibility has changed, give up. */
17675 if (window_wants_header_line (w)
17676 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17677 return false;
17679 /* Give up if old or new display is scrolled vertically. We could
17680 make this function handle this, but right now it doesn't. */
17681 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17682 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17683 return false;
17685 /* Clear the desired matrix for the display below. */
17686 clear_glyph_matrix (w->desired_matrix);
17688 /* Give up if line numbers are being displayed, because reusing the
17689 current matrix might use the wrong width for line-number
17690 display. */
17691 if (!NILP (Vdisplay_line_numbers))
17692 return false;
17694 /* The variable new_start now holds the new window start. The old
17695 start `start' can be determined from the current matrix. */
17696 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17697 start = start_row->minpos;
17698 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17700 if (CHARPOS (new_start) <= CHARPOS (start))
17702 /* Don't use this method if the display starts with an ellipsis
17703 displayed for invisible text. It's not easy to handle that case
17704 below, and it's certainly not worth the effort since this is
17705 not a frequent case. */
17706 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17707 return false;
17709 IF_DEBUG (debug_method_add (w, "twu1"));
17711 /* Display up to a row that can be reused. The variable
17712 last_text_row is set to the last row displayed that displays
17713 text. Note that it.vpos == 0 if or if not there is a
17714 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17715 start_display (&it, w, new_start);
17716 w->cursor.vpos = -1;
17717 last_text_row = last_reused_text_row = NULL;
17719 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17721 /* If we have reached into the characters in the START row,
17722 that means the line boundaries have changed. So we
17723 can't start copying with the row START. Maybe it will
17724 work to start copying with the following row. */
17725 while (IT_CHARPOS (it) > CHARPOS (start))
17727 /* Advance to the next row as the "start". */
17728 start_row++;
17729 start = start_row->minpos;
17730 /* If there are no more rows to try, or just one, give up. */
17731 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17732 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17733 || CHARPOS (start) == ZV)
17735 clear_glyph_matrix (w->desired_matrix);
17736 return false;
17739 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17741 /* If we have reached alignment, we can copy the rest of the
17742 rows. */
17743 if (IT_CHARPOS (it) == CHARPOS (start)
17744 /* Don't accept "alignment" inside a display vector,
17745 since start_row could have started in the middle of
17746 that same display vector (thus their character
17747 positions match), and we have no way of telling if
17748 that is the case. */
17749 && it.current.dpvec_index < 0)
17750 break;
17752 it.glyph_row->reversed_p = false;
17753 if (display_line (&it, -1))
17754 last_text_row = it.glyph_row - 1;
17758 /* A value of current_y < last_visible_y means that we stopped
17759 at the previous window start, which in turn means that we
17760 have at least one reusable row. */
17761 if (it.current_y < it.last_visible_y)
17763 struct glyph_row *row;
17765 /* IT.vpos always starts from 0; it counts text lines. */
17766 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17768 /* Find PT if not already found in the lines displayed. */
17769 if (w->cursor.vpos < 0)
17771 int dy = it.current_y - start_row->y;
17773 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17774 row = row_containing_pos (w, PT, row, NULL, dy);
17775 if (row)
17776 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17777 dy, nrows_scrolled);
17778 else
17780 clear_glyph_matrix (w->desired_matrix);
17781 return false;
17785 /* Scroll the display. Do it before the current matrix is
17786 changed. The problem here is that update has not yet
17787 run, i.e. part of the current matrix is not up to date.
17788 scroll_run_hook will clear the cursor, and use the
17789 current matrix to get the height of the row the cursor is
17790 in. */
17791 run.current_y = start_row->y;
17792 run.desired_y = it.current_y;
17793 run.height = it.last_visible_y - it.current_y;
17795 if (run.height > 0 && run.current_y != run.desired_y)
17797 update_begin (f);
17798 FRAME_RIF (f)->update_window_begin_hook (w);
17799 FRAME_RIF (f)->clear_window_mouse_face (w);
17800 FRAME_RIF (f)->scroll_run_hook (w, &run);
17801 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17802 update_end (f);
17805 /* Shift current matrix down by nrows_scrolled lines. */
17806 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17807 rotate_matrix (w->current_matrix,
17808 start_vpos,
17809 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17810 nrows_scrolled);
17812 /* Disable lines that must be updated. */
17813 for (i = 0; i < nrows_scrolled; ++i)
17814 (start_row + i)->enabled_p = false;
17816 /* Re-compute Y positions. */
17817 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17818 max_y = it.last_visible_y;
17819 for (row = start_row + nrows_scrolled;
17820 row < bottom_row;
17821 ++row)
17823 row->y = it.current_y;
17824 row->visible_height = row->height;
17826 if (row->y < min_y)
17827 row->visible_height -= min_y - row->y;
17828 if (row->y + row->height > max_y)
17829 row->visible_height -= row->y + row->height - max_y;
17830 if (row->fringe_bitmap_periodic_p)
17831 row->redraw_fringe_bitmaps_p = true;
17833 it.current_y += row->height;
17835 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17836 last_reused_text_row = row;
17837 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17838 break;
17841 /* Disable lines in the current matrix which are now
17842 below the window. */
17843 for (++row; row < bottom_row; ++row)
17844 row->enabled_p = row->mode_line_p = false;
17847 /* Update window_end_pos etc.; last_reused_text_row is the last
17848 reused row from the current matrix containing text, if any.
17849 The value of last_text_row is the last displayed line
17850 containing text. */
17851 if (last_reused_text_row)
17852 adjust_window_ends (w, last_reused_text_row, true);
17853 else if (last_text_row)
17854 adjust_window_ends (w, last_text_row, false);
17855 else
17857 /* This window must be completely empty. */
17858 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17859 w->window_end_pos = Z - ZV;
17860 w->window_end_vpos = 0;
17862 w->window_end_valid = false;
17864 /* Update hint: don't try scrolling again in update_window. */
17865 w->desired_matrix->no_scrolling_p = true;
17867 #ifdef GLYPH_DEBUG
17868 debug_method_add (w, "try_window_reusing_current_matrix 1");
17869 #endif
17870 return true;
17872 else if (CHARPOS (new_start) > CHARPOS (start))
17874 struct glyph_row *pt_row, *row;
17875 struct glyph_row *first_reusable_row;
17876 struct glyph_row *first_row_to_display;
17877 int dy;
17878 int yb = window_text_bottom_y (w);
17880 /* Find the row starting at new_start, if there is one. Don't
17881 reuse a partially visible line at the end. */
17882 first_reusable_row = start_row;
17883 while (first_reusable_row->enabled_p
17884 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17885 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17886 < CHARPOS (new_start)))
17887 ++first_reusable_row;
17889 /* Give up if there is no row to reuse. */
17890 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17891 || !first_reusable_row->enabled_p
17892 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17893 != CHARPOS (new_start)))
17894 return false;
17896 /* We can reuse fully visible rows beginning with
17897 first_reusable_row to the end of the window. Set
17898 first_row_to_display to the first row that cannot be reused.
17899 Set pt_row to the row containing point, if there is any. */
17900 pt_row = NULL;
17901 for (first_row_to_display = first_reusable_row;
17902 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17903 ++first_row_to_display)
17905 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17906 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17907 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17908 && first_row_to_display->ends_at_zv_p
17909 && pt_row == NULL)))
17910 pt_row = first_row_to_display;
17913 /* Start displaying at the start of first_row_to_display. */
17914 eassert (first_row_to_display->y < yb);
17915 init_to_row_start (&it, w, first_row_to_display);
17917 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17918 - start_vpos);
17919 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17920 - nrows_scrolled);
17921 it.current_y = (first_row_to_display->y - first_reusable_row->y
17922 + WINDOW_HEADER_LINE_HEIGHT (w));
17924 /* Display lines beginning with first_row_to_display in the
17925 desired matrix. Set last_text_row to the last row displayed
17926 that displays text. */
17927 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17928 if (pt_row == NULL)
17929 w->cursor.vpos = -1;
17930 last_text_row = NULL;
17931 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17932 if (display_line (&it, w->cursor.vpos))
17933 last_text_row = it.glyph_row - 1;
17935 /* If point is in a reused row, adjust y and vpos of the cursor
17936 position. */
17937 if (pt_row)
17939 w->cursor.vpos -= nrows_scrolled;
17940 w->cursor.y -= first_reusable_row->y - start_row->y;
17943 /* Give up if point isn't in a row displayed or reused. (This
17944 also handles the case where w->cursor.vpos < nrows_scrolled
17945 after the calls to display_line, which can happen with scroll
17946 margins. See bug#1295.) */
17947 if (w->cursor.vpos < 0)
17949 clear_glyph_matrix (w->desired_matrix);
17950 return false;
17953 /* Scroll the display. */
17954 run.current_y = first_reusable_row->y;
17955 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17956 run.height = it.last_visible_y - run.current_y;
17957 dy = run.current_y - run.desired_y;
17959 if (run.height)
17961 update_begin (f);
17962 FRAME_RIF (f)->update_window_begin_hook (w);
17963 FRAME_RIF (f)->clear_window_mouse_face (w);
17964 FRAME_RIF (f)->scroll_run_hook (w, &run);
17965 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17966 update_end (f);
17969 /* Adjust Y positions of reused rows. */
17970 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17971 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17972 max_y = it.last_visible_y;
17973 for (row = first_reusable_row; row < first_row_to_display; ++row)
17975 row->y -= dy;
17976 row->visible_height = row->height;
17977 if (row->y < min_y)
17978 row->visible_height -= min_y - row->y;
17979 if (row->y + row->height > max_y)
17980 row->visible_height -= row->y + row->height - max_y;
17981 if (row->fringe_bitmap_periodic_p)
17982 row->redraw_fringe_bitmaps_p = true;
17985 /* Scroll the current matrix. */
17986 eassert (nrows_scrolled > 0);
17987 rotate_matrix (w->current_matrix,
17988 start_vpos,
17989 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17990 -nrows_scrolled);
17992 /* Disable rows not reused. */
17993 for (row -= nrows_scrolled; row < bottom_row; ++row)
17994 row->enabled_p = false;
17996 /* Point may have moved to a different line, so we cannot assume that
17997 the previous cursor position is valid; locate the correct row. */
17998 if (pt_row)
18000 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
18001 row < bottom_row
18002 && PT >= MATRIX_ROW_END_CHARPOS (row)
18003 && !row->ends_at_zv_p;
18004 row++)
18006 w->cursor.vpos++;
18007 w->cursor.y = row->y;
18009 if (row < bottom_row)
18011 /* Can't simply scan the row for point with
18012 bidi-reordered glyph rows. Let set_cursor_from_row
18013 figure out where to put the cursor, and if it fails,
18014 give up. */
18015 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
18017 if (!set_cursor_from_row (w, row, w->current_matrix,
18018 0, 0, 0, 0))
18020 clear_glyph_matrix (w->desired_matrix);
18021 return false;
18024 else
18026 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
18027 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18029 for (; glyph < end
18030 && (!BUFFERP (glyph->object)
18031 || glyph->charpos < PT);
18032 glyph++)
18034 w->cursor.hpos++;
18035 w->cursor.x += glyph->pixel_width;
18041 /* Adjust window end. A null value of last_text_row means that
18042 the window end is in reused rows which in turn means that
18043 only its vpos can have changed. */
18044 if (last_text_row)
18045 adjust_window_ends (w, last_text_row, false);
18046 else
18047 w->window_end_vpos -= nrows_scrolled;
18049 w->window_end_valid = false;
18050 w->desired_matrix->no_scrolling_p = true;
18052 #ifdef GLYPH_DEBUG
18053 debug_method_add (w, "try_window_reusing_current_matrix 2");
18054 #endif
18055 return true;
18058 return false;
18063 /************************************************************************
18064 Window redisplay reusing current matrix when buffer has changed
18065 ************************************************************************/
18067 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
18068 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
18069 ptrdiff_t *, ptrdiff_t *);
18070 static struct glyph_row *
18071 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
18072 struct glyph_row *);
18075 /* Return the last row in MATRIX displaying text. If row START is
18076 non-null, start searching with that row. IT gives the dimensions
18077 of the display. Value is null if matrix is empty; otherwise it is
18078 a pointer to the row found. */
18080 static struct glyph_row *
18081 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
18082 struct glyph_row *start)
18084 struct glyph_row *row, *row_found;
18086 /* Set row_found to the last row in IT->w's current matrix
18087 displaying text. The loop looks funny but think of partially
18088 visible lines. */
18089 row_found = NULL;
18090 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
18091 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18093 eassert (row->enabled_p);
18094 row_found = row;
18095 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
18096 break;
18097 ++row;
18100 return row_found;
18104 /* Return the last row in the current matrix of W that is not affected
18105 by changes at the start of current_buffer that occurred since W's
18106 current matrix was built. Value is null if no such row exists.
18108 BEG_UNCHANGED us the number of characters unchanged at the start of
18109 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
18110 first changed character in current_buffer. Characters at positions <
18111 BEG + BEG_UNCHANGED are at the same buffer positions as they were
18112 when the current matrix was built. */
18114 static struct glyph_row *
18115 find_last_unchanged_at_beg_row (struct window *w)
18117 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
18118 struct glyph_row *row;
18119 struct glyph_row *row_found = NULL;
18120 int yb = window_text_bottom_y (w);
18122 /* Find the last row displaying unchanged text. */
18123 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18124 MATRIX_ROW_DISPLAYS_TEXT_P (row)
18125 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
18126 ++row)
18128 if (/* If row ends before first_changed_pos, it is unchanged,
18129 except in some case. */
18130 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
18131 /* When row ends in ZV and we write at ZV it is not
18132 unchanged. */
18133 && !row->ends_at_zv_p
18134 /* When first_changed_pos is the end of a continued line,
18135 row is not unchanged because it may be no longer
18136 continued. */
18137 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
18138 && (row->continued_p
18139 || row->exact_window_width_line_p))
18140 /* If ROW->end is beyond ZV, then ROW->end is outdated and
18141 needs to be recomputed, so don't consider this row as
18142 unchanged. This happens when the last line was
18143 bidi-reordered and was killed immediately before this
18144 redisplay cycle. In that case, ROW->end stores the
18145 buffer position of the first visual-order character of
18146 the killed text, which is now beyond ZV. */
18147 && CHARPOS (row->end.pos) <= ZV)
18148 row_found = row;
18150 /* Stop if last visible row. */
18151 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
18152 break;
18155 return row_found;
18159 /* Find the first glyph row in the current matrix of W that is not
18160 affected by changes at the end of current_buffer since the
18161 time W's current matrix was built.
18163 Return in *DELTA the number of chars by which buffer positions in
18164 unchanged text at the end of current_buffer must be adjusted.
18166 Return in *DELTA_BYTES the corresponding number of bytes.
18168 Value is null if no such row exists, i.e. all rows are affected by
18169 changes. */
18171 static struct glyph_row *
18172 find_first_unchanged_at_end_row (struct window *w,
18173 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
18175 struct glyph_row *row;
18176 struct glyph_row *row_found = NULL;
18178 *delta = *delta_bytes = 0;
18180 /* Display must not have been paused, otherwise the current matrix
18181 is not up to date. */
18182 eassert (w->window_end_valid);
18184 /* A value of window_end_pos >= END_UNCHANGED means that the window
18185 end is in the range of changed text. If so, there is no
18186 unchanged row at the end of W's current matrix. */
18187 if (w->window_end_pos >= END_UNCHANGED)
18188 return NULL;
18190 /* Set row to the last row in W's current matrix displaying text. */
18191 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18193 /* If matrix is entirely empty, no unchanged row exists. */
18194 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18196 /* The value of row is the last glyph row in the matrix having a
18197 meaningful buffer position in it. The end position of row
18198 corresponds to window_end_pos. This allows us to translate
18199 buffer positions in the current matrix to current buffer
18200 positions for characters not in changed text. */
18201 ptrdiff_t Z_old =
18202 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18203 ptrdiff_t Z_BYTE_old =
18204 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18205 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
18206 struct glyph_row *first_text_row
18207 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18209 *delta = Z - Z_old;
18210 *delta_bytes = Z_BYTE - Z_BYTE_old;
18212 /* Set last_unchanged_pos to the buffer position of the last
18213 character in the buffer that has not been changed. Z is the
18214 index + 1 of the last character in current_buffer, i.e. by
18215 subtracting END_UNCHANGED we get the index of the last
18216 unchanged character, and we have to add BEG to get its buffer
18217 position. */
18218 last_unchanged_pos = Z - END_UNCHANGED + BEG;
18219 last_unchanged_pos_old = last_unchanged_pos - *delta;
18221 /* Search backward from ROW for a row displaying a line that
18222 starts at a minimum position >= last_unchanged_pos_old. */
18223 for (; row > first_text_row; --row)
18225 /* This used to abort, but it can happen.
18226 It is ok to just stop the search instead here. KFS. */
18227 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
18228 break;
18230 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
18231 row_found = row;
18235 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
18237 return row_found;
18241 /* Make sure that glyph rows in the current matrix of window W
18242 reference the same glyph memory as corresponding rows in the
18243 frame's frame matrix. This function is called after scrolling W's
18244 current matrix on a terminal frame in try_window_id and
18245 try_window_reusing_current_matrix. */
18247 static void
18248 sync_frame_with_window_matrix_rows (struct window *w)
18250 struct frame *f = XFRAME (w->frame);
18251 struct glyph_row *window_row, *window_row_end, *frame_row;
18253 /* Preconditions: W must be a leaf window and full-width. Its frame
18254 must have a frame matrix. */
18255 eassert (BUFFERP (w->contents));
18256 eassert (WINDOW_FULL_WIDTH_P (w));
18257 eassert (!FRAME_WINDOW_P (f));
18259 /* If W is a full-width window, glyph pointers in W's current matrix
18260 have, by definition, to be the same as glyph pointers in the
18261 corresponding frame matrix. Note that frame matrices have no
18262 marginal areas (see build_frame_matrix). */
18263 window_row = w->current_matrix->rows;
18264 window_row_end = window_row + w->current_matrix->nrows;
18265 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
18266 while (window_row < window_row_end)
18268 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
18269 struct glyph *end = window_row->glyphs[LAST_AREA];
18271 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
18272 frame_row->glyphs[TEXT_AREA] = start;
18273 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
18274 frame_row->glyphs[LAST_AREA] = end;
18276 /* Disable frame rows whose corresponding window rows have
18277 been disabled in try_window_id. */
18278 if (!window_row->enabled_p)
18279 frame_row->enabled_p = false;
18281 ++window_row, ++frame_row;
18286 /* Find the glyph row in window W containing CHARPOS. Consider all
18287 rows between START and END (not inclusive). END null means search
18288 all rows to the end of the display area of W. Value is the row
18289 containing CHARPOS or null. */
18291 struct glyph_row *
18292 row_containing_pos (struct window *w, ptrdiff_t charpos,
18293 struct glyph_row *start, struct glyph_row *end, int dy)
18295 struct glyph_row *row = start;
18296 struct glyph_row *best_row = NULL;
18297 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18298 int last_y;
18300 /* If we happen to start on a header-line, skip that. */
18301 if (row->mode_line_p)
18302 ++row;
18304 if ((end && row >= end) || !row->enabled_p)
18305 return NULL;
18307 last_y = window_text_bottom_y (w) - dy;
18309 while (true)
18311 /* Give up if we have gone too far. */
18312 if ((end && row >= end) || !row->enabled_p)
18313 return NULL;
18314 /* This formerly returned if they were equal.
18315 I think that both quantities are of a "last plus one" type;
18316 if so, when they are equal, the row is within the screen. -- rms. */
18317 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18318 return NULL;
18320 /* If it is in this row, return this row. */
18321 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18322 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18323 /* The end position of a row equals the start
18324 position of the next row. If CHARPOS is there, we
18325 would rather consider it displayed in the next
18326 line, except when this line ends in ZV. */
18327 && !row_for_charpos_p (row, charpos)))
18328 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18330 struct glyph *g;
18332 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18333 || (!best_row && !row->continued_p))
18334 return row;
18335 /* In bidi-reordered rows, there could be several rows whose
18336 edges surround CHARPOS, all of these rows belonging to
18337 the same continued line. We need to find the row which
18338 fits CHARPOS the best. */
18339 for (g = row->glyphs[TEXT_AREA];
18340 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18341 g++)
18343 if (!STRINGP (g->object))
18345 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18347 mindif = eabs (g->charpos - charpos);
18348 best_row = row;
18349 /* Exact match always wins. */
18350 if (mindif == 0)
18351 return best_row;
18356 else if (best_row && !row->continued_p)
18357 return best_row;
18358 ++row;
18363 /* Try to redisplay window W by reusing its existing display. W's
18364 current matrix must be up to date when this function is called,
18365 i.e., window_end_valid must be true.
18367 Value is
18369 >= 1 if successful, i.e. display has been updated
18370 specifically:
18371 1 means the changes were in front of a newline that precedes
18372 the window start, and the whole current matrix was reused
18373 2 means the changes were after the last position displayed
18374 in the window, and the whole current matrix was reused
18375 3 means portions of the current matrix were reused, while
18376 some of the screen lines were redrawn
18377 -1 if redisplay with same window start is known not to succeed
18378 0 if otherwise unsuccessful
18380 The following steps are performed:
18382 1. Find the last row in the current matrix of W that is not
18383 affected by changes at the start of current_buffer. If no such row
18384 is found, give up.
18386 2. Find the first row in W's current matrix that is not affected by
18387 changes at the end of current_buffer. Maybe there is no such row.
18389 3. Display lines beginning with the row + 1 found in step 1 to the
18390 row found in step 2 or, if step 2 didn't find a row, to the end of
18391 the window.
18393 4. If cursor is not known to appear on the window, give up.
18395 5. If display stopped at the row found in step 2, scroll the
18396 display and current matrix as needed.
18398 6. Maybe display some lines at the end of W, if we must. This can
18399 happen under various circumstances, like a partially visible line
18400 becoming fully visible, or because newly displayed lines are displayed
18401 in smaller font sizes.
18403 7. Update W's window end information. */
18405 static int
18406 try_window_id (struct window *w)
18408 struct frame *f = XFRAME (w->frame);
18409 struct glyph_matrix *current_matrix = w->current_matrix;
18410 struct glyph_matrix *desired_matrix = w->desired_matrix;
18411 struct glyph_row *last_unchanged_at_beg_row;
18412 struct glyph_row *first_unchanged_at_end_row;
18413 struct glyph_row *row;
18414 struct glyph_row *bottom_row;
18415 int bottom_vpos;
18416 struct it it;
18417 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18418 int dvpos, dy;
18419 struct text_pos start_pos;
18420 struct run run;
18421 int first_unchanged_at_end_vpos = 0;
18422 struct glyph_row *last_text_row, *last_text_row_at_end;
18423 struct text_pos start;
18424 ptrdiff_t first_changed_charpos, last_changed_charpos;
18426 #ifdef GLYPH_DEBUG
18427 if (inhibit_try_window_id)
18428 return 0;
18429 #endif
18431 /* This is handy for debugging. */
18432 #if false
18433 #define GIVE_UP(X) \
18434 do { \
18435 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18436 return 0; \
18437 } while (false)
18438 #else
18439 #define GIVE_UP(X) return 0
18440 #endif
18442 SET_TEXT_POS_FROM_MARKER (start, w->start);
18444 /* Don't use this for mini-windows because these can show
18445 messages and mini-buffers, and we don't handle that here. */
18446 if (MINI_WINDOW_P (w))
18447 GIVE_UP (1);
18449 /* This flag is used to prevent redisplay optimizations. */
18450 if (windows_or_buffers_changed || f->cursor_type_changed)
18451 GIVE_UP (2);
18453 /* This function's optimizations cannot be used if overlays have
18454 changed in the buffer displayed by the window, so give up if they
18455 have. */
18456 if (w->last_overlay_modified != OVERLAY_MODIFF)
18457 GIVE_UP (200);
18459 /* Verify that narrowing has not changed.
18460 Also verify that we were not told to prevent redisplay optimizations.
18461 It would be nice to further
18462 reduce the number of cases where this prevents try_window_id. */
18463 if (current_buffer->clip_changed
18464 || current_buffer->prevent_redisplay_optimizations_p)
18465 GIVE_UP (3);
18467 /* Window must either use window-based redisplay or be full width. */
18468 if (!FRAME_WINDOW_P (f)
18469 && (!FRAME_LINE_INS_DEL_OK (f)
18470 || !WINDOW_FULL_WIDTH_P (w)))
18471 GIVE_UP (4);
18473 /* Give up if point is known NOT to appear in W. */
18474 if (PT < CHARPOS (start))
18475 GIVE_UP (5);
18477 /* Another way to prevent redisplay optimizations. */
18478 if (w->last_modified == 0)
18479 GIVE_UP (6);
18481 /* Verify that window is not hscrolled. */
18482 if (w->hscroll != 0)
18483 GIVE_UP (7);
18485 /* Verify that display wasn't paused. */
18486 if (!w->window_end_valid)
18487 GIVE_UP (8);
18489 /* Likewise if highlighting trailing whitespace. */
18490 if (!NILP (Vshow_trailing_whitespace))
18491 GIVE_UP (11);
18493 /* Can't use this if overlay arrow position and/or string have
18494 changed. */
18495 if (overlay_arrows_changed_p (false))
18496 GIVE_UP (12);
18498 /* When word-wrap is on, adding a space to the first word of a
18499 wrapped line can change the wrap position, altering the line
18500 above it. It might be worthwhile to handle this more
18501 intelligently, but for now just redisplay from scratch. */
18502 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18503 GIVE_UP (21);
18505 /* Under bidi reordering, adding or deleting a character in the
18506 beginning of a paragraph, before the first strong directional
18507 character, can change the base direction of the paragraph (unless
18508 the buffer specifies a fixed paragraph direction), which will
18509 require redisplaying the whole paragraph. It might be worthwhile
18510 to find the paragraph limits and widen the range of redisplayed
18511 lines to that, but for now just give up this optimization and
18512 redisplay from scratch. */
18513 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18514 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18515 GIVE_UP (22);
18517 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18518 to that variable require thorough redisplay. */
18519 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18520 GIVE_UP (23);
18522 /* Give up if display-line-numbers is in relative mode, or when the
18523 current line's number needs to be displayed in a distinct face. */
18524 if (EQ (Vdisplay_line_numbers, Qrelative)
18525 || EQ (Vdisplay_line_numbers, Qvisual)
18526 || (!NILP (Vdisplay_line_numbers)
18527 && NILP (Finternal_lisp_face_equal_p (Qline_number,
18528 Qline_number_current_line,
18529 w->frame))))
18530 GIVE_UP (24);
18532 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18533 only if buffer has really changed. The reason is that the gap is
18534 initially at Z for freshly visited files. The code below would
18535 set end_unchanged to 0 in that case. */
18536 if (MODIFF > SAVE_MODIFF
18537 /* This seems to happen sometimes after saving a buffer. */
18538 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18540 if (GPT - BEG < BEG_UNCHANGED)
18541 BEG_UNCHANGED = GPT - BEG;
18542 if (Z - GPT < END_UNCHANGED)
18543 END_UNCHANGED = Z - GPT;
18546 /* The position of the first and last character that has been changed. */
18547 first_changed_charpos = BEG + BEG_UNCHANGED;
18548 last_changed_charpos = Z - END_UNCHANGED;
18550 /* If window starts after a line end, and the last change is in
18551 front of that newline, then changes don't affect the display.
18552 This case happens with stealth-fontification. Note that although
18553 the display is unchanged, glyph positions in the matrix have to
18554 be adjusted, of course. */
18555 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18556 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18557 && ((last_changed_charpos < CHARPOS (start)
18558 && CHARPOS (start) == BEGV)
18559 || (last_changed_charpos < CHARPOS (start) - 1
18560 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18562 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18563 struct glyph_row *r0;
18565 /* Compute how many chars/bytes have been added to or removed
18566 from the buffer. */
18567 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18568 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18569 Z_delta = Z - Z_old;
18570 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18572 /* Give up if PT is not in the window. Note that it already has
18573 been checked at the start of try_window_id that PT is not in
18574 front of the window start. */
18575 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18576 GIVE_UP (13);
18578 /* If window start is unchanged, we can reuse the whole matrix
18579 as is, after adjusting glyph positions. No need to compute
18580 the window end again, since its offset from Z hasn't changed. */
18581 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18582 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18583 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18584 /* PT must not be in a partially visible line. */
18585 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18586 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18588 /* Adjust positions in the glyph matrix. */
18589 if (Z_delta || Z_delta_bytes)
18591 struct glyph_row *r1
18592 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18593 increment_matrix_positions (w->current_matrix,
18594 MATRIX_ROW_VPOS (r0, current_matrix),
18595 MATRIX_ROW_VPOS (r1, current_matrix),
18596 Z_delta, Z_delta_bytes);
18599 /* Set the cursor. */
18600 row = row_containing_pos (w, PT, r0, NULL, 0);
18601 if (row)
18602 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18603 return 1;
18607 /* Handle the case that changes are all below what is displayed in
18608 the window, and that PT is in the window. This shortcut cannot
18609 be taken if ZV is visible in the window, and text has been added
18610 there that is visible in the window. */
18611 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18612 /* ZV is not visible in the window, or there are no
18613 changes at ZV, actually. */
18614 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18615 || first_changed_charpos == last_changed_charpos))
18617 struct glyph_row *r0;
18619 /* Give up if PT is not in the window. Note that it already has
18620 been checked at the start of try_window_id that PT is not in
18621 front of the window start. */
18622 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18623 GIVE_UP (14);
18625 /* If window start is unchanged, we can reuse the whole matrix
18626 as is, without changing glyph positions since no text has
18627 been added/removed in front of the window end. */
18628 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18629 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18630 /* PT must not be in a partially visible line. */
18631 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18632 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18634 /* We have to compute the window end anew since text
18635 could have been added/removed after it. */
18636 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18637 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18639 /* Set the cursor. */
18640 row = row_containing_pos (w, PT, r0, NULL, 0);
18641 if (row)
18642 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18643 return 2;
18647 /* Give up if window start is in the changed area.
18649 The condition used to read
18651 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18653 but why that was tested escapes me at the moment. */
18654 if (CHARPOS (start) >= first_changed_charpos
18655 && CHARPOS (start) <= last_changed_charpos)
18656 GIVE_UP (15);
18658 /* Check that window start agrees with the start of the first glyph
18659 row in its current matrix. Check this after we know the window
18660 start is not in changed text, otherwise positions would not be
18661 comparable. */
18662 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18663 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18664 GIVE_UP (16);
18666 /* Give up if the window ends in strings. Overlay strings
18667 at the end are difficult to handle, so don't try. */
18668 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18669 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18670 GIVE_UP (20);
18672 /* Compute the position at which we have to start displaying new
18673 lines. Some of the lines at the top of the window might be
18674 reusable because they are not displaying changed text. Find the
18675 last row in W's current matrix not affected by changes at the
18676 start of current_buffer. Value is null if changes start in the
18677 first line of window. */
18678 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18679 if (last_unchanged_at_beg_row)
18681 /* Avoid starting to display in the middle of a character, a TAB
18682 for instance. This is easier than to set up the iterator
18683 exactly, and it's not a frequent case, so the additional
18684 effort wouldn't really pay off. */
18685 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18686 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18687 && last_unchanged_at_beg_row > w->current_matrix->rows)
18688 --last_unchanged_at_beg_row;
18690 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18691 GIVE_UP (17);
18693 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18694 GIVE_UP (18);
18695 start_pos = it.current.pos;
18697 /* Start displaying new lines in the desired matrix at the same
18698 vpos we would use in the current matrix, i.e. below
18699 last_unchanged_at_beg_row. */
18700 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18701 current_matrix);
18702 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18703 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18705 eassert (it.hpos == 0 && it.current_x == 0);
18707 else
18709 /* There are no reusable lines at the start of the window.
18710 Start displaying in the first text line. */
18711 start_display (&it, w, start);
18712 it.vpos = it.first_vpos;
18713 start_pos = it.current.pos;
18716 /* Find the first row that is not affected by changes at the end of
18717 the buffer. Value will be null if there is no unchanged row, in
18718 which case we must redisplay to the end of the window. delta
18719 will be set to the value by which buffer positions beginning with
18720 first_unchanged_at_end_row have to be adjusted due to text
18721 changes. */
18722 first_unchanged_at_end_row
18723 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18724 IF_DEBUG (debug_delta = delta);
18725 IF_DEBUG (debug_delta_bytes = delta_bytes);
18727 /* Set stop_pos to the buffer position up to which we will have to
18728 display new lines. If first_unchanged_at_end_row != NULL, this
18729 is the buffer position of the start of the line displayed in that
18730 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18731 that we don't stop at a buffer position. */
18732 stop_pos = 0;
18733 if (first_unchanged_at_end_row)
18735 eassert (last_unchanged_at_beg_row == NULL
18736 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18738 /* If this is a continuation line, move forward to the next one
18739 that isn't. Changes in lines above affect this line.
18740 Caution: this may move first_unchanged_at_end_row to a row
18741 not displaying text. */
18742 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18743 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18744 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18745 < it.last_visible_y))
18746 ++first_unchanged_at_end_row;
18748 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18749 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18750 >= it.last_visible_y))
18751 first_unchanged_at_end_row = NULL;
18752 else
18754 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18755 + delta);
18756 first_unchanged_at_end_vpos
18757 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18758 eassert (stop_pos >= Z - END_UNCHANGED);
18761 else if (last_unchanged_at_beg_row == NULL)
18762 GIVE_UP (19);
18765 #ifdef GLYPH_DEBUG
18767 /* Either there is no unchanged row at the end, or the one we have
18768 now displays text. This is a necessary condition for the window
18769 end pos calculation at the end of this function. */
18770 eassert (first_unchanged_at_end_row == NULL
18771 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18773 debug_last_unchanged_at_beg_vpos
18774 = (last_unchanged_at_beg_row
18775 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18776 : -1);
18777 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18779 #endif /* GLYPH_DEBUG */
18782 /* Display new lines. Set last_text_row to the last new line
18783 displayed which has text on it, i.e. might end up as being the
18784 line where the window_end_vpos is. */
18785 w->cursor.vpos = -1;
18786 last_text_row = NULL;
18787 overlay_arrow_seen = false;
18788 if (it.current_y < it.last_visible_y
18789 && !f->fonts_changed
18790 && (first_unchanged_at_end_row == NULL
18791 || IT_CHARPOS (it) < stop_pos))
18792 it.glyph_row->reversed_p = false;
18793 while (it.current_y < it.last_visible_y
18794 && !f->fonts_changed
18795 && (first_unchanged_at_end_row == NULL
18796 || IT_CHARPOS (it) < stop_pos))
18798 if (display_line (&it, -1))
18799 last_text_row = it.glyph_row - 1;
18802 if (f->fonts_changed)
18803 return -1;
18805 /* The redisplay iterations in display_line above could have
18806 triggered font-lock, which could have done something that
18807 invalidates IT->w window's end-point information, on which we
18808 rely below. E.g., one package, which will remain unnamed, used
18809 to install a font-lock-fontify-region-function that called
18810 bury-buffer, whose side effect is to switch the buffer displayed
18811 by IT->w, and that predictably resets IT->w's window_end_valid
18812 flag, which we already tested at the entry to this function.
18813 Amply punish such packages/modes by giving up on this
18814 optimization in those cases. */
18815 if (!w->window_end_valid)
18817 clear_glyph_matrix (w->desired_matrix);
18818 return -1;
18821 /* Compute differences in buffer positions, y-positions etc. for
18822 lines reused at the bottom of the window. Compute what we can
18823 scroll. */
18824 if (first_unchanged_at_end_row
18825 /* No lines reused because we displayed everything up to the
18826 bottom of the window. */
18827 && it.current_y < it.last_visible_y)
18829 dvpos = (it.vpos
18830 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18831 current_matrix));
18832 dy = it.current_y - first_unchanged_at_end_row->y;
18833 run.current_y = first_unchanged_at_end_row->y;
18834 run.desired_y = run.current_y + dy;
18835 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18837 else
18839 delta = delta_bytes = dvpos = dy
18840 = run.current_y = run.desired_y = run.height = 0;
18841 first_unchanged_at_end_row = NULL;
18843 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18846 /* Find the cursor if not already found. We have to decide whether
18847 PT will appear on this window (it sometimes doesn't, but this is
18848 not a very frequent case.) This decision has to be made before
18849 the current matrix is altered. A value of cursor.vpos < 0 means
18850 that PT is either in one of the lines beginning at
18851 first_unchanged_at_end_row or below the window. Don't care for
18852 lines that might be displayed later at the window end; as
18853 mentioned, this is not a frequent case. */
18854 if (w->cursor.vpos < 0)
18856 /* Cursor in unchanged rows at the top? */
18857 if (PT < CHARPOS (start_pos)
18858 && last_unchanged_at_beg_row)
18860 row = row_containing_pos (w, PT,
18861 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18862 last_unchanged_at_beg_row + 1, 0);
18863 if (row)
18864 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18867 /* Start from first_unchanged_at_end_row looking for PT. */
18868 else if (first_unchanged_at_end_row)
18870 row = row_containing_pos (w, PT - delta,
18871 first_unchanged_at_end_row, NULL, 0);
18872 if (row)
18873 set_cursor_from_row (w, row, w->current_matrix, delta,
18874 delta_bytes, dy, dvpos);
18877 /* Give up if cursor was not found. */
18878 if (w->cursor.vpos < 0)
18880 clear_glyph_matrix (w->desired_matrix);
18881 return -1;
18885 /* Don't let the cursor end in the scroll margins. */
18887 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
18888 int cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18890 if ((w->cursor.y < this_scroll_margin
18891 && CHARPOS (start) > BEGV)
18892 /* Old redisplay didn't take scroll margin into account at the bottom,
18893 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18894 || (w->cursor.y + (make_cursor_line_fully_visible_p
18895 ? cursor_height + this_scroll_margin
18896 : 1)) > it.last_visible_y)
18898 w->cursor.vpos = -1;
18899 clear_glyph_matrix (w->desired_matrix);
18900 return -1;
18904 /* Scroll the display. Do it before changing the current matrix so
18905 that xterm.c doesn't get confused about where the cursor glyph is
18906 found. */
18907 if (dy && run.height)
18909 update_begin (f);
18911 if (FRAME_WINDOW_P (f))
18913 FRAME_RIF (f)->update_window_begin_hook (w);
18914 FRAME_RIF (f)->clear_window_mouse_face (w);
18915 FRAME_RIF (f)->scroll_run_hook (w, &run);
18916 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18918 else
18920 /* Terminal frame. In this case, dvpos gives the number of
18921 lines to scroll by; dvpos < 0 means scroll up. */
18922 int from_vpos
18923 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18924 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18925 int end = (WINDOW_TOP_EDGE_LINE (w)
18926 + window_wants_header_line (w)
18927 + window_internal_height (w));
18929 #if defined (HAVE_GPM) || defined (MSDOS)
18930 x_clear_window_mouse_face (w);
18931 #endif
18932 /* Perform the operation on the screen. */
18933 if (dvpos > 0)
18935 /* Scroll last_unchanged_at_beg_row to the end of the
18936 window down dvpos lines. */
18937 set_terminal_window (f, end);
18939 /* On dumb terminals delete dvpos lines at the end
18940 before inserting dvpos empty lines. */
18941 if (!FRAME_SCROLL_REGION_OK (f))
18942 ins_del_lines (f, end - dvpos, -dvpos);
18944 /* Insert dvpos empty lines in front of
18945 last_unchanged_at_beg_row. */
18946 ins_del_lines (f, from, dvpos);
18948 else if (dvpos < 0)
18950 /* Scroll up last_unchanged_at_beg_vpos to the end of
18951 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18952 set_terminal_window (f, end);
18954 /* Delete dvpos lines in front of
18955 last_unchanged_at_beg_vpos. ins_del_lines will set
18956 the cursor to the given vpos and emit |dvpos| delete
18957 line sequences. */
18958 ins_del_lines (f, from + dvpos, dvpos);
18960 /* On a dumb terminal insert dvpos empty lines at the
18961 end. */
18962 if (!FRAME_SCROLL_REGION_OK (f))
18963 ins_del_lines (f, end + dvpos, -dvpos);
18966 set_terminal_window (f, 0);
18969 update_end (f);
18972 /* Shift reused rows of the current matrix to the right position.
18973 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18974 text. */
18975 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18976 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18977 if (dvpos < 0)
18979 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18980 bottom_vpos, dvpos);
18981 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18982 bottom_vpos);
18984 else if (dvpos > 0)
18986 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18987 bottom_vpos, dvpos);
18988 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18989 first_unchanged_at_end_vpos + dvpos);
18992 /* For frame-based redisplay, make sure that current frame and window
18993 matrix are in sync with respect to glyph memory. */
18994 if (!FRAME_WINDOW_P (f))
18995 sync_frame_with_window_matrix_rows (w);
18997 /* Adjust buffer positions in reused rows. */
18998 if (delta || delta_bytes)
18999 increment_matrix_positions (current_matrix,
19000 first_unchanged_at_end_vpos + dvpos,
19001 bottom_vpos, delta, delta_bytes);
19003 /* Adjust Y positions. */
19004 if (dy)
19005 shift_glyph_matrix (w, current_matrix,
19006 first_unchanged_at_end_vpos + dvpos,
19007 bottom_vpos, dy);
19009 if (first_unchanged_at_end_row)
19011 first_unchanged_at_end_row += dvpos;
19012 if (first_unchanged_at_end_row->y >= it.last_visible_y
19013 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
19014 first_unchanged_at_end_row = NULL;
19017 /* If scrolling up, there may be some lines to display at the end of
19018 the window. */
19019 last_text_row_at_end = NULL;
19020 if (dy < 0)
19022 /* Scrolling up can leave for example a partially visible line
19023 at the end of the window to be redisplayed. */
19024 /* Set last_row to the glyph row in the current matrix where the
19025 window end line is found. It has been moved up or down in
19026 the matrix by dvpos. */
19027 int last_vpos = w->window_end_vpos + dvpos;
19028 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
19030 /* If last_row is the window end line, it should display text. */
19031 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
19033 /* If window end line was partially visible before, begin
19034 displaying at that line. Otherwise begin displaying with the
19035 line following it. */
19036 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
19038 init_to_row_start (&it, w, last_row);
19039 it.vpos = last_vpos;
19040 it.current_y = last_row->y;
19042 else
19044 init_to_row_end (&it, w, last_row);
19045 it.vpos = 1 + last_vpos;
19046 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
19047 ++last_row;
19050 /* We may start in a continuation line. If so, we have to
19051 get the right continuation_lines_width and current_x. */
19052 it.continuation_lines_width = last_row->continuation_lines_width;
19053 it.hpos = it.current_x = 0;
19055 /* Display the rest of the lines at the window end. */
19056 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
19057 while (it.current_y < it.last_visible_y && !f->fonts_changed)
19059 /* Is it always sure that the display agrees with lines in
19060 the current matrix? I don't think so, so we mark rows
19061 displayed invalid in the current matrix by setting their
19062 enabled_p flag to false. */
19063 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
19064 if (display_line (&it, w->cursor.vpos))
19065 last_text_row_at_end = it.glyph_row - 1;
19069 /* Update window_end_pos and window_end_vpos. */
19070 if (first_unchanged_at_end_row && !last_text_row_at_end)
19072 /* Window end line if one of the preserved rows from the current
19073 matrix. Set row to the last row displaying text in current
19074 matrix starting at first_unchanged_at_end_row, after
19075 scrolling. */
19076 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
19077 row = find_last_row_displaying_text (w->current_matrix, &it,
19078 first_unchanged_at_end_row);
19079 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
19080 adjust_window_ends (w, row, true);
19081 eassert (w->window_end_bytepos >= 0);
19082 IF_DEBUG (debug_method_add (w, "A"));
19084 else if (last_text_row_at_end)
19086 adjust_window_ends (w, last_text_row_at_end, false);
19087 eassert (w->window_end_bytepos >= 0);
19088 IF_DEBUG (debug_method_add (w, "B"));
19090 else if (last_text_row)
19092 /* We have displayed either to the end of the window or at the
19093 end of the window, i.e. the last row with text is to be found
19094 in the desired matrix. */
19095 adjust_window_ends (w, last_text_row, false);
19096 eassert (w->window_end_bytepos >= 0);
19098 else if (first_unchanged_at_end_row == NULL
19099 && last_text_row == NULL
19100 && last_text_row_at_end == NULL)
19102 /* Displayed to end of window, but no line containing text was
19103 displayed. Lines were deleted at the end of the window. */
19104 bool first_vpos = window_wants_header_line (w);
19105 int vpos = w->window_end_vpos;
19106 struct glyph_row *current_row = current_matrix->rows + vpos;
19107 struct glyph_row *desired_row = desired_matrix->rows + vpos;
19109 for (row = NULL; !row; --vpos, --current_row, --desired_row)
19111 eassert (first_vpos <= vpos);
19112 if (desired_row->enabled_p)
19114 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
19115 row = desired_row;
19117 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
19118 row = current_row;
19121 w->window_end_vpos = vpos + 1;
19122 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
19123 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
19124 eassert (w->window_end_bytepos >= 0);
19125 IF_DEBUG (debug_method_add (w, "C"));
19127 else
19128 emacs_abort ();
19130 IF_DEBUG ((debug_end_pos = w->window_end_pos,
19131 debug_end_vpos = w->window_end_vpos));
19133 /* Record that display has not been completed. */
19134 w->window_end_valid = false;
19135 w->desired_matrix->no_scrolling_p = true;
19136 return 3;
19138 #undef GIVE_UP
19143 /***********************************************************************
19144 More debugging support
19145 ***********************************************************************/
19147 #ifdef GLYPH_DEBUG
19149 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
19150 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
19151 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
19154 /* Dump the contents of glyph matrix MATRIX on stderr.
19156 GLYPHS 0 means don't show glyph contents.
19157 GLYPHS 1 means show glyphs in short form
19158 GLYPHS > 1 means show glyphs in long form. */
19160 void
19161 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
19163 int i;
19164 for (i = 0; i < matrix->nrows; ++i)
19165 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
19169 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
19170 the glyph row and area where the glyph comes from. */
19172 void
19173 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
19175 if (glyph->type == CHAR_GLYPH
19176 || glyph->type == GLYPHLESS_GLYPH)
19178 fprintf (stderr,
19179 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19180 glyph - row->glyphs[TEXT_AREA],
19181 (glyph->type == CHAR_GLYPH
19182 ? 'C'
19183 : 'G'),
19184 glyph->charpos,
19185 (BUFFERP (glyph->object)
19186 ? 'B'
19187 : (STRINGP (glyph->object)
19188 ? 'S'
19189 : (NILP (glyph->object)
19190 ? '0'
19191 : '-'))),
19192 glyph->pixel_width,
19193 glyph->u.ch,
19194 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
19195 ? (int) glyph->u.ch
19196 : '.'),
19197 glyph->face_id,
19198 glyph->left_box_line_p,
19199 glyph->right_box_line_p);
19201 else if (glyph->type == STRETCH_GLYPH)
19203 fprintf (stderr,
19204 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19205 glyph - row->glyphs[TEXT_AREA],
19206 'S',
19207 glyph->charpos,
19208 (BUFFERP (glyph->object)
19209 ? 'B'
19210 : (STRINGP (glyph->object)
19211 ? 'S'
19212 : (NILP (glyph->object)
19213 ? '0'
19214 : '-'))),
19215 glyph->pixel_width,
19217 ' ',
19218 glyph->face_id,
19219 glyph->left_box_line_p,
19220 glyph->right_box_line_p);
19222 else if (glyph->type == IMAGE_GLYPH)
19224 fprintf (stderr,
19225 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19226 glyph - row->glyphs[TEXT_AREA],
19227 'I',
19228 glyph->charpos,
19229 (BUFFERP (glyph->object)
19230 ? 'B'
19231 : (STRINGP (glyph->object)
19232 ? 'S'
19233 : (NILP (glyph->object)
19234 ? '0'
19235 : '-'))),
19236 glyph->pixel_width,
19237 (unsigned int) glyph->u.img_id,
19238 '.',
19239 glyph->face_id,
19240 glyph->left_box_line_p,
19241 glyph->right_box_line_p);
19243 else if (glyph->type == COMPOSITE_GLYPH)
19245 fprintf (stderr,
19246 " %5"pD"d %c %9"pD"d %c %3d 0x%06x",
19247 glyph - row->glyphs[TEXT_AREA],
19248 '+',
19249 glyph->charpos,
19250 (BUFFERP (glyph->object)
19251 ? 'B'
19252 : (STRINGP (glyph->object)
19253 ? 'S'
19254 : (NILP (glyph->object)
19255 ? '0'
19256 : '-'))),
19257 glyph->pixel_width,
19258 (unsigned int) glyph->u.cmp.id);
19259 if (glyph->u.cmp.automatic)
19260 fprintf (stderr,
19261 "[%d-%d]",
19262 glyph->slice.cmp.from, glyph->slice.cmp.to);
19263 fprintf (stderr, " . %4d %1.1d%1.1d\n",
19264 glyph->face_id,
19265 glyph->left_box_line_p,
19266 glyph->right_box_line_p);
19268 else if (glyph->type == XWIDGET_GLYPH)
19270 #ifndef HAVE_XWIDGETS
19271 eassume (false);
19272 #else
19273 fprintf (stderr,
19274 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
19275 glyph - row->glyphs[TEXT_AREA],
19276 'X',
19277 glyph->charpos,
19278 (BUFFERP (glyph->object)
19279 ? 'B'
19280 : (STRINGP (glyph->object)
19281 ? 'S'
19282 : '-')),
19283 glyph->pixel_width,
19284 glyph->u.xwidget,
19285 '.',
19286 glyph->face_id,
19287 glyph->left_box_line_p,
19288 glyph->right_box_line_p);
19289 #endif
19294 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19295 GLYPHS 0 means don't show glyph contents.
19296 GLYPHS 1 means show glyphs in short form
19297 GLYPHS > 1 means show glyphs in long form. */
19299 void
19300 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19302 if (glyphs != 1)
19304 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19305 fprintf (stderr, "==============================================================================\n");
19307 fprintf (stderr, "%3d %9"pD"d %9"pD"d %4d %1.1d%1.1d%1.1d%1.1d\
19308 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19309 vpos,
19310 MATRIX_ROW_START_CHARPOS (row),
19311 MATRIX_ROW_END_CHARPOS (row),
19312 row->used[TEXT_AREA],
19313 row->contains_overlapping_glyphs_p,
19314 row->enabled_p,
19315 row->truncated_on_left_p,
19316 row->truncated_on_right_p,
19317 row->continued_p,
19318 MATRIX_ROW_CONTINUATION_LINE_P (row),
19319 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19320 row->ends_at_zv_p,
19321 row->fill_line_p,
19322 row->ends_in_middle_of_char_p,
19323 row->starts_in_middle_of_char_p,
19324 row->mouse_face_p,
19325 row->x,
19326 row->y,
19327 row->pixel_width,
19328 row->height,
19329 row->visible_height,
19330 row->ascent,
19331 row->phys_ascent);
19332 /* The next 3 lines should align to "Start" in the header. */
19333 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19334 row->end.overlay_string_index,
19335 row->continuation_lines_width);
19336 fprintf (stderr, " %9"pD"d %9"pD"d\n",
19337 CHARPOS (row->start.string_pos),
19338 CHARPOS (row->end.string_pos));
19339 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19340 row->end.dpvec_index);
19343 if (glyphs > 1)
19345 int area;
19347 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19349 struct glyph *glyph = row->glyphs[area];
19350 struct glyph *glyph_end = glyph + row->used[area];
19352 /* Glyph for a line end in text. */
19353 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19354 ++glyph_end;
19356 if (glyph < glyph_end)
19357 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19359 for (; glyph < glyph_end; ++glyph)
19360 dump_glyph (row, glyph, area);
19363 else if (glyphs == 1)
19365 int area;
19366 char s[SHRT_MAX + 4];
19368 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19370 int i;
19372 for (i = 0; i < row->used[area]; ++i)
19374 struct glyph *glyph = row->glyphs[area] + i;
19375 if (i == row->used[area] - 1
19376 && area == TEXT_AREA
19377 && NILP (glyph->object)
19378 && glyph->type == CHAR_GLYPH
19379 && glyph->u.ch == ' ')
19381 strcpy (&s[i], "[\\n]");
19382 i += 4;
19384 else if (glyph->type == CHAR_GLYPH
19385 && glyph->u.ch < 0x80
19386 && glyph->u.ch >= ' ')
19387 s[i] = glyph->u.ch;
19388 else
19389 s[i] = '.';
19392 s[i] = '\0';
19393 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19399 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19400 Sdump_glyph_matrix, 0, 1, "p",
19401 doc: /* Dump the current matrix of the selected window to stderr.
19402 Shows contents of glyph row structures. With non-nil
19403 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19404 glyphs in short form, otherwise show glyphs in long form.
19406 Interactively, no argument means show glyphs in short form;
19407 with numeric argument, its value is passed as the GLYPHS flag. */)
19408 (Lisp_Object glyphs)
19410 struct window *w = XWINDOW (selected_window);
19411 struct buffer *buffer = XBUFFER (w->contents);
19413 fprintf (stderr, "PT = %"pD"d, BEGV = %"pD"d. ZV = %"pD"d\n",
19414 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19415 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19416 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19417 fprintf (stderr, "=============================================\n");
19418 dump_glyph_matrix (w->current_matrix,
19419 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19420 return Qnil;
19424 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19425 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19426 Only text-mode frames have frame glyph matrices. */)
19427 (void)
19429 struct frame *f = XFRAME (selected_frame);
19431 if (f->current_matrix)
19432 dump_glyph_matrix (f->current_matrix, 1);
19433 else
19434 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19435 return Qnil;
19439 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
19440 doc: /* Dump glyph row ROW to stderr.
19441 GLYPH 0 means don't dump glyphs.
19442 GLYPH 1 means dump glyphs in short form.
19443 GLYPH > 1 or omitted means dump glyphs in long form. */)
19444 (Lisp_Object row, Lisp_Object glyphs)
19446 struct glyph_matrix *matrix;
19447 EMACS_INT vpos;
19449 CHECK_NUMBER (row);
19450 matrix = XWINDOW (selected_window)->current_matrix;
19451 vpos = XINT (row);
19452 if (vpos >= 0 && vpos < matrix->nrows)
19453 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19454 vpos,
19455 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19456 return Qnil;
19460 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
19461 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19462 GLYPH 0 means don't dump glyphs.
19463 GLYPH 1 means dump glyphs in short form.
19464 GLYPH > 1 or omitted means dump glyphs in long form.
19466 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19467 do nothing. */)
19468 (Lisp_Object row, Lisp_Object glyphs)
19470 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19471 struct frame *sf = SELECTED_FRAME ();
19472 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19473 EMACS_INT vpos;
19475 CHECK_NUMBER (row);
19476 vpos = XINT (row);
19477 if (vpos >= 0 && vpos < m->nrows)
19478 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19479 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19480 #endif
19481 return Qnil;
19485 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19486 doc: /* Toggle tracing of redisplay.
19487 With ARG, turn tracing on if and only if ARG is positive. */)
19488 (Lisp_Object arg)
19490 if (NILP (arg))
19491 trace_redisplay_p = !trace_redisplay_p;
19492 else
19494 arg = Fprefix_numeric_value (arg);
19495 trace_redisplay_p = XINT (arg) > 0;
19498 return Qnil;
19502 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19503 doc: /* Like `format', but print result to stderr.
19504 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19505 (ptrdiff_t nargs, Lisp_Object *args)
19507 Lisp_Object s = Fformat (nargs, args);
19508 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19509 return Qnil;
19512 #endif /* GLYPH_DEBUG */
19516 /***********************************************************************
19517 Building Desired Matrix Rows
19518 ***********************************************************************/
19520 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19521 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19523 static struct glyph_row *
19524 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19526 struct frame *f = XFRAME (WINDOW_FRAME (w));
19527 struct buffer *buffer = XBUFFER (w->contents);
19528 struct buffer *old = current_buffer;
19529 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19530 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19531 const unsigned char *arrow_end = arrow_string + arrow_len;
19532 const unsigned char *p;
19533 struct it it;
19534 bool multibyte_p;
19535 int n_glyphs_before;
19537 set_buffer_temp (buffer);
19538 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19539 scratch_glyph_row.reversed_p = false;
19540 it.glyph_row->used[TEXT_AREA] = 0;
19541 SET_TEXT_POS (it.position, 0, 0);
19543 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19544 p = arrow_string;
19545 while (p < arrow_end)
19547 Lisp_Object face, ilisp;
19549 /* Get the next character. */
19550 if (multibyte_p)
19551 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19552 else
19554 it.c = it.char_to_display = *p, it.len = 1;
19555 if (! ASCII_CHAR_P (it.c))
19556 it.char_to_display = BYTE8_TO_CHAR (it.c);
19558 p += it.len;
19560 /* Get its face. */
19561 ilisp = make_number (p - arrow_string);
19562 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19563 it.face_id = compute_char_face (f, it.char_to_display, face);
19565 /* Compute its width, get its glyphs. */
19566 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19567 SET_TEXT_POS (it.position, -1, -1);
19568 PRODUCE_GLYPHS (&it);
19570 /* If this character doesn't fit any more in the line, we have
19571 to remove some glyphs. */
19572 if (it.current_x > it.last_visible_x)
19574 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19575 break;
19579 set_buffer_temp (old);
19580 return it.glyph_row;
19584 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19585 glyphs to insert is determined by produce_special_glyphs. */
19587 static void
19588 insert_left_trunc_glyphs (struct it *it)
19590 struct it truncate_it;
19591 struct glyph *from, *end, *to, *toend;
19593 eassert (!FRAME_WINDOW_P (it->f)
19594 || (!it->glyph_row->reversed_p
19595 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19596 || (it->glyph_row->reversed_p
19597 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19599 /* Get the truncation glyphs. */
19600 truncate_it = *it;
19601 truncate_it.current_x = 0;
19602 truncate_it.face_id = DEFAULT_FACE_ID;
19603 truncate_it.glyph_row = &scratch_glyph_row;
19604 truncate_it.area = TEXT_AREA;
19605 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19606 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19607 truncate_it.object = Qnil;
19608 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19610 /* Overwrite glyphs from IT with truncation glyphs. */
19611 if (!it->glyph_row->reversed_p)
19613 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19615 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19616 end = from + tused;
19617 to = it->glyph_row->glyphs[TEXT_AREA];
19618 toend = to + it->glyph_row->used[TEXT_AREA];
19619 if (FRAME_WINDOW_P (it->f))
19621 /* On GUI frames, when variable-size fonts are displayed,
19622 the truncation glyphs may need more pixels than the row's
19623 glyphs they overwrite. We overwrite more glyphs to free
19624 enough screen real estate, and enlarge the stretch glyph
19625 on the right (see display_line), if there is one, to
19626 preserve the screen position of the truncation glyphs on
19627 the right. */
19628 int w = 0;
19629 struct glyph *g = to;
19630 short used;
19632 /* The first glyph could be partially visible, in which case
19633 it->glyph_row->x will be negative. But we want the left
19634 truncation glyphs to be aligned at the left margin of the
19635 window, so we override the x coordinate at which the row
19636 will begin. */
19637 it->glyph_row->x = 0;
19638 while (g < toend && w < it->truncation_pixel_width)
19640 w += g->pixel_width;
19641 ++g;
19643 if (g - to - tused > 0)
19645 memmove (to + tused, g, (toend - g) * sizeof(*g));
19646 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19648 used = it->glyph_row->used[TEXT_AREA];
19649 if (it->glyph_row->truncated_on_right_p
19650 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19651 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19652 == STRETCH_GLYPH)
19654 int extra = w - it->truncation_pixel_width;
19656 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19660 while (from < end)
19661 *to++ = *from++;
19663 /* There may be padding glyphs left over. Overwrite them too. */
19664 if (!FRAME_WINDOW_P (it->f))
19666 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19668 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19669 while (from < end)
19670 *to++ = *from++;
19674 if (to > toend)
19675 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19677 else
19679 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19681 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19682 that back to front. */
19683 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19684 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19685 toend = it->glyph_row->glyphs[TEXT_AREA];
19686 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19687 if (FRAME_WINDOW_P (it->f))
19689 int w = 0;
19690 struct glyph *g = to;
19692 while (g >= toend && w < it->truncation_pixel_width)
19694 w += g->pixel_width;
19695 --g;
19697 if (to - g - tused > 0)
19698 to = g + tused;
19699 if (it->glyph_row->truncated_on_right_p
19700 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19701 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19703 int extra = w - it->truncation_pixel_width;
19705 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19709 while (from >= end && to >= toend)
19710 *to-- = *from--;
19711 if (!FRAME_WINDOW_P (it->f))
19713 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19715 from =
19716 truncate_it.glyph_row->glyphs[TEXT_AREA]
19717 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19718 while (from >= end && to >= toend)
19719 *to-- = *from--;
19722 if (from >= end)
19724 /* Need to free some room before prepending additional
19725 glyphs. */
19726 int move_by = from - end + 1;
19727 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19728 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19730 for ( ; g >= g0; g--)
19731 g[move_by] = *g;
19732 while (from >= end)
19733 *to-- = *from--;
19734 it->glyph_row->used[TEXT_AREA] += move_by;
19739 /* Compute the hash code for ROW. */
19740 unsigned
19741 row_hash (struct glyph_row *row)
19743 int area, k;
19744 unsigned hashval = 0;
19746 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19747 for (k = 0; k < row->used[area]; ++k)
19748 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19749 + row->glyphs[area][k].u.val
19750 + row->glyphs[area][k].face_id
19751 + row->glyphs[area][k].padding_p
19752 + (row->glyphs[area][k].type << 2));
19754 return hashval;
19757 /* Compute the pixel height and width of IT->glyph_row.
19759 Most of the time, ascent and height of a display line will be equal
19760 to the max_ascent and max_height values of the display iterator
19761 structure. This is not the case if
19763 1. We hit ZV without displaying anything. In this case, max_ascent
19764 and max_height will be zero.
19766 2. We have some glyphs that don't contribute to the line height.
19767 (The glyph row flag contributes_to_line_height_p is for future
19768 pixmap extensions).
19770 The first case is easily covered by using default values because in
19771 these cases, the line height does not really matter, except that it
19772 must not be zero. */
19774 static void
19775 compute_line_metrics (struct it *it)
19777 struct glyph_row *row = it->glyph_row;
19779 if (FRAME_WINDOW_P (it->f))
19781 int i, min_y, max_y;
19783 /* The line may consist of one space only, that was added to
19784 place the cursor on it. If so, the row's height hasn't been
19785 computed yet. */
19786 if (row->height == 0)
19788 if (it->max_ascent + it->max_descent == 0)
19789 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19790 row->ascent = it->max_ascent;
19791 row->height = it->max_ascent + it->max_descent;
19792 row->phys_ascent = it->max_phys_ascent;
19793 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19794 row->extra_line_spacing = it->max_extra_line_spacing;
19797 /* Compute the width of this line. */
19798 row->pixel_width = row->x;
19799 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19800 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19802 eassert (row->pixel_width >= 0);
19803 eassert (row->ascent >= 0 && row->height > 0);
19805 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19806 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19808 /* If first line's physical ascent is larger than its logical
19809 ascent, use the physical ascent, and make the row taller.
19810 This makes accented characters fully visible. */
19811 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19812 && row->phys_ascent > row->ascent)
19814 row->height += row->phys_ascent - row->ascent;
19815 row->ascent = row->phys_ascent;
19818 /* Compute how much of the line is visible. */
19819 row->visible_height = row->height;
19821 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19822 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19824 if (row->y < min_y)
19825 row->visible_height -= min_y - row->y;
19826 if (row->y + row->height > max_y)
19827 row->visible_height -= row->y + row->height - max_y;
19829 else
19831 row->pixel_width = row->used[TEXT_AREA];
19832 if (row->continued_p)
19833 row->pixel_width -= it->continuation_pixel_width;
19834 else if (row->truncated_on_right_p)
19835 row->pixel_width -= it->truncation_pixel_width;
19836 row->ascent = row->phys_ascent = 0;
19837 row->height = row->phys_height = row->visible_height = 1;
19838 row->extra_line_spacing = 0;
19841 /* Compute a hash code for this row. */
19842 row->hash = row_hash (row);
19844 it->max_ascent = it->max_descent = 0;
19845 it->max_phys_ascent = it->max_phys_descent = 0;
19849 /* Append one space to the glyph row of iterator IT if doing a
19850 window-based redisplay. The space has the same face as
19851 IT->face_id. Value is true if a space was added.
19853 This function is called to make sure that there is always one glyph
19854 at the end of a glyph row that the cursor can be set on under
19855 window-systems. (If there weren't such a glyph we would not know
19856 how wide and tall a box cursor should be displayed).
19858 At the same time this space let's a nicely handle clearing to the
19859 end of the line if the row ends in italic text. */
19861 static bool
19862 append_space_for_newline (struct it *it, bool default_face_p)
19864 if (FRAME_WINDOW_P (it->f))
19866 int n = it->glyph_row->used[TEXT_AREA];
19868 if (it->glyph_row->glyphs[TEXT_AREA] + n
19869 < it->glyph_row->glyphs[1 + TEXT_AREA])
19871 /* Save some values that must not be changed.
19872 Must save IT->c and IT->len because otherwise
19873 ITERATOR_AT_END_P wouldn't work anymore after
19874 append_space_for_newline has been called. */
19875 enum display_element_type saved_what = it->what;
19876 int saved_c = it->c, saved_len = it->len;
19877 int saved_char_to_display = it->char_to_display;
19878 int saved_x = it->current_x;
19879 int saved_face_id = it->face_id;
19880 bool saved_box_end = it->end_of_box_run_p;
19881 struct text_pos saved_pos;
19882 Lisp_Object saved_object;
19883 struct face *face;
19885 saved_object = it->object;
19886 saved_pos = it->position;
19888 it->what = IT_CHARACTER;
19889 memset (&it->position, 0, sizeof it->position);
19890 it->object = Qnil;
19891 it->c = it->char_to_display = ' ';
19892 it->len = 1;
19894 /* If the default face was remapped, be sure to use the
19895 remapped face for the appended newline. */
19896 if (default_face_p)
19897 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19898 else if (it->face_before_selective_p)
19899 it->face_id = it->saved_face_id;
19900 face = FACE_FROM_ID (it->f, it->face_id);
19901 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19902 /* In R2L rows, we will prepend a stretch glyph that will
19903 have the end_of_box_run_p flag set for it, so there's no
19904 need for the appended newline glyph to have that flag
19905 set. */
19906 if (it->glyph_row->reversed_p
19907 /* But if the appended newline glyph goes all the way to
19908 the end of the row, there will be no stretch glyph,
19909 so leave the box flag set. */
19910 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19911 it->end_of_box_run_p = false;
19913 PRODUCE_GLYPHS (it);
19915 #ifdef HAVE_WINDOW_SYSTEM
19916 /* Make sure this space glyph has the right ascent and
19917 descent values, or else cursor at end of line will look
19918 funny, and height of empty lines will be incorrect. */
19919 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
19920 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19921 if (n == 0)
19923 Lisp_Object height, total_height;
19924 int extra_line_spacing = it->extra_line_spacing;
19925 int boff = font->baseline_offset;
19927 if (font->vertical_centering)
19928 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19930 it->object = saved_object; /* get_it_property needs this */
19931 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19932 /* Must do a subset of line height processing from
19933 x_produce_glyph for newline characters. */
19934 height = get_it_property (it, Qline_height);
19935 if (CONSP (height)
19936 && CONSP (XCDR (height))
19937 && NILP (XCDR (XCDR (height))))
19939 total_height = XCAR (XCDR (height));
19940 height = XCAR (height);
19942 else
19943 total_height = Qnil;
19944 height = calc_line_height_property (it, height, font, boff, true);
19946 if (it->override_ascent >= 0)
19948 it->ascent = it->override_ascent;
19949 it->descent = it->override_descent;
19950 boff = it->override_boff;
19952 if (EQ (height, Qt))
19953 extra_line_spacing = 0;
19954 else
19956 Lisp_Object spacing;
19958 it->phys_ascent = it->ascent;
19959 it->phys_descent = it->descent;
19960 if (!NILP (height)
19961 && XINT (height) > it->ascent + it->descent)
19962 it->ascent = XINT (height) - it->descent;
19964 if (!NILP (total_height))
19965 spacing = calc_line_height_property (it, total_height, font,
19966 boff, false);
19967 else
19969 spacing = get_it_property (it, Qline_spacing);
19970 spacing = calc_line_height_property (it, spacing, font,
19971 boff, false);
19973 if (INTEGERP (spacing))
19975 extra_line_spacing = XINT (spacing);
19976 if (!NILP (total_height))
19977 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19980 if (extra_line_spacing > 0)
19982 it->descent += extra_line_spacing;
19983 if (extra_line_spacing > it->max_extra_line_spacing)
19984 it->max_extra_line_spacing = extra_line_spacing;
19986 it->max_ascent = it->ascent;
19987 it->max_descent = it->descent;
19988 /* Make sure compute_line_metrics recomputes the row height. */
19989 it->glyph_row->height = 0;
19992 g->ascent = it->max_ascent;
19993 g->descent = it->max_descent;
19994 #endif
19996 it->override_ascent = -1;
19997 it->constrain_row_ascent_descent_p = false;
19998 it->current_x = saved_x;
19999 it->object = saved_object;
20000 it->position = saved_pos;
20001 it->what = saved_what;
20002 it->face_id = saved_face_id;
20003 it->len = saved_len;
20004 it->c = saved_c;
20005 it->char_to_display = saved_char_to_display;
20006 it->end_of_box_run_p = saved_box_end;
20007 return true;
20011 return false;
20015 /* Extend the face of the last glyph in the text area of IT->glyph_row
20016 to the end of the display line. Called from display_line. If the
20017 glyph row is empty, add a space glyph to it so that we know the
20018 face to draw. Set the glyph row flag fill_line_p. If the glyph
20019 row is R2L, prepend a stretch glyph to cover the empty space to the
20020 left of the leftmost glyph. */
20022 static void
20023 extend_face_to_end_of_line (struct it *it)
20025 struct face *face, *default_face;
20026 struct frame *f = it->f;
20028 /* If line is already filled, do nothing. Non window-system frames
20029 get a grace of one more ``pixel'' because their characters are
20030 1-``pixel'' wide, so they hit the equality too early. This grace
20031 is needed only for R2L rows that are not continued, to produce
20032 one extra blank where we could display the cursor. */
20033 if ((it->current_x >= it->last_visible_x
20034 + (!FRAME_WINDOW_P (f)
20035 && it->glyph_row->reversed_p
20036 && !it->glyph_row->continued_p))
20037 /* If the window has display margins, we will need to extend
20038 their face even if the text area is filled. */
20039 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20040 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
20041 return;
20043 /* The default face, possibly remapped. */
20044 default_face = FACE_FROM_ID_OR_NULL (f,
20045 lookup_basic_face (f, DEFAULT_FACE_ID));
20047 /* Face extension extends the background and box of IT->face_id
20048 to the end of the line. If the background equals the background
20049 of the frame, we don't have to do anything. */
20050 face = FACE_FROM_ID (f, (it->face_before_selective_p
20051 ? it->saved_face_id
20052 : it->face_id));
20054 if (FRAME_WINDOW_P (f)
20055 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
20056 && face->box == FACE_NO_BOX
20057 && face->background == FRAME_BACKGROUND_PIXEL (f)
20058 #ifdef HAVE_WINDOW_SYSTEM
20059 && !face->stipple
20060 #endif
20061 && !it->glyph_row->reversed_p)
20062 return;
20064 /* Set the glyph row flag indicating that the face of the last glyph
20065 in the text area has to be drawn to the end of the text area. */
20066 it->glyph_row->fill_line_p = true;
20068 /* If current character of IT is not ASCII, make sure we have the
20069 ASCII face. This will be automatically undone the next time
20070 get_next_display_element returns a multibyte character. Note
20071 that the character will always be single byte in unibyte
20072 text. */
20073 if (!ASCII_CHAR_P (it->c))
20075 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
20078 if (FRAME_WINDOW_P (f))
20080 /* If the row is empty, add a space with the current face of IT,
20081 so that we know which face to draw. */
20082 if (it->glyph_row->used[TEXT_AREA] == 0)
20084 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
20085 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
20086 it->glyph_row->used[TEXT_AREA] = 1;
20088 /* Mode line and the header line don't have margins, and
20089 likewise the frame's tool-bar window, if there is any. */
20090 if (!(it->glyph_row->mode_line_p
20091 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
20092 || (WINDOWP (f->tool_bar_window)
20093 && it->w == XWINDOW (f->tool_bar_window))
20094 #endif
20097 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20098 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
20100 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
20101 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
20102 default_face->id;
20103 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
20105 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20106 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
20108 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
20109 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
20110 default_face->id;
20111 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
20114 #ifdef HAVE_WINDOW_SYSTEM
20115 if (it->glyph_row->reversed_p)
20117 /* Prepend a stretch glyph to the row, such that the
20118 rightmost glyph will be drawn flushed all the way to the
20119 right margin of the window. The stretch glyph that will
20120 occupy the empty space, if any, to the left of the
20121 glyphs. */
20122 struct font *font = face->font ? face->font : FRAME_FONT (f);
20123 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
20124 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
20125 struct glyph *g;
20126 int row_width, stretch_ascent, stretch_width;
20127 struct text_pos saved_pos;
20128 int saved_face_id;
20129 bool saved_avoid_cursor, saved_box_start;
20131 for (row_width = 0, g = row_start; g < row_end; g++)
20132 row_width += g->pixel_width;
20134 /* FIXME: There are various minor display glitches in R2L
20135 rows when only one of the fringes is missing. The
20136 strange condition below produces the least bad effect. */
20137 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
20138 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
20139 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
20140 stretch_width = window_box_width (it->w, TEXT_AREA);
20141 else
20142 stretch_width = it->last_visible_x - it->first_visible_x;
20143 stretch_width -= row_width;
20145 if (stretch_width > 0)
20147 stretch_ascent =
20148 (((it->ascent + it->descent)
20149 * FONT_BASE (font)) / FONT_HEIGHT (font));
20150 saved_pos = it->position;
20151 memset (&it->position, 0, sizeof it->position);
20152 saved_avoid_cursor = it->avoid_cursor_p;
20153 it->avoid_cursor_p = true;
20154 saved_face_id = it->face_id;
20155 saved_box_start = it->start_of_box_run_p;
20156 /* The last row's stretch glyph should get the default
20157 face, to avoid painting the rest of the window with
20158 the region face, if the region ends at ZV. */
20159 if (it->glyph_row->ends_at_zv_p)
20160 it->face_id = default_face->id;
20161 else
20162 it->face_id = face->id;
20163 it->start_of_box_run_p = false;
20164 append_stretch_glyph (it, Qnil, stretch_width,
20165 it->ascent + it->descent, stretch_ascent);
20166 it->position = saved_pos;
20167 it->avoid_cursor_p = saved_avoid_cursor;
20168 it->face_id = saved_face_id;
20169 it->start_of_box_run_p = saved_box_start;
20171 /* If stretch_width comes out negative, it means that the
20172 last glyph is only partially visible. In R2L rows, we
20173 want the leftmost glyph to be partially visible, so we
20174 need to give the row the corresponding left offset. */
20175 if (stretch_width < 0)
20176 it->glyph_row->x = stretch_width;
20178 #endif /* HAVE_WINDOW_SYSTEM */
20180 else
20182 /* Save some values that must not be changed. */
20183 int saved_x = it->current_x;
20184 struct text_pos saved_pos;
20185 Lisp_Object saved_object;
20186 enum display_element_type saved_what = it->what;
20187 int saved_face_id = it->face_id;
20189 saved_object = it->object;
20190 saved_pos = it->position;
20192 it->what = IT_CHARACTER;
20193 memset (&it->position, 0, sizeof it->position);
20194 it->object = Qnil;
20195 it->c = it->char_to_display = ' ';
20196 it->len = 1;
20198 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20199 && (it->glyph_row->used[LEFT_MARGIN_AREA]
20200 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20201 && !it->glyph_row->mode_line_p
20202 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20204 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
20205 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
20207 for (it->current_x = 0; g < e; g++)
20208 it->current_x += g->pixel_width;
20210 it->area = LEFT_MARGIN_AREA;
20211 it->face_id = default_face->id;
20212 while (it->glyph_row->used[LEFT_MARGIN_AREA]
20213 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20215 PRODUCE_GLYPHS (it);
20216 /* term.c:produce_glyphs advances it->current_x only for
20217 TEXT_AREA. */
20218 it->current_x += it->pixel_width;
20221 it->current_x = saved_x;
20222 it->area = TEXT_AREA;
20225 /* The last row's blank glyphs should get the default face, to
20226 avoid painting the rest of the window with the region face,
20227 if the region ends at ZV. */
20228 if (it->glyph_row->ends_at_zv_p)
20229 it->face_id = default_face->id;
20230 else
20231 it->face_id = face->id;
20232 PRODUCE_GLYPHS (it);
20234 while (it->current_x <= it->last_visible_x)
20235 PRODUCE_GLYPHS (it);
20237 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20238 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
20239 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20240 && !it->glyph_row->mode_line_p
20241 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20243 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
20244 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
20246 for ( ; g < e; g++)
20247 it->current_x += g->pixel_width;
20249 it->area = RIGHT_MARGIN_AREA;
20250 it->face_id = default_face->id;
20251 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
20252 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20254 PRODUCE_GLYPHS (it);
20255 it->current_x += it->pixel_width;
20258 it->area = TEXT_AREA;
20261 /* Don't count these blanks really. It would let us insert a left
20262 truncation glyph below and make us set the cursor on them, maybe. */
20263 it->current_x = saved_x;
20264 it->object = saved_object;
20265 it->position = saved_pos;
20266 it->what = saved_what;
20267 it->face_id = saved_face_id;
20272 /* Value is true if text starting at CHARPOS in current_buffer is
20273 trailing whitespace. */
20275 static bool
20276 trailing_whitespace_p (ptrdiff_t charpos)
20278 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
20279 int c = 0;
20281 while (bytepos < ZV_BYTE
20282 && (c = FETCH_CHAR (bytepos),
20283 c == ' ' || c == '\t'))
20284 ++bytepos;
20286 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20288 if (bytepos != PT_BYTE)
20289 return true;
20291 return false;
20295 /* Highlight trailing whitespace, if any, in ROW. */
20297 static void
20298 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20300 int used = row->used[TEXT_AREA];
20302 if (used)
20304 struct glyph *start = row->glyphs[TEXT_AREA];
20305 struct glyph *glyph = start + used - 1;
20307 if (row->reversed_p)
20309 /* Right-to-left rows need to be processed in the opposite
20310 direction, so swap the edge pointers. */
20311 glyph = start;
20312 start = row->glyphs[TEXT_AREA] + used - 1;
20315 /* Skip over glyphs inserted to display the cursor at the
20316 end of a line, for extending the face of the last glyph
20317 to the end of the line on terminals, and for truncation
20318 and continuation glyphs. */
20319 if (!row->reversed_p)
20321 while (glyph >= start
20322 && glyph->type == CHAR_GLYPH
20323 && NILP (glyph->object))
20324 --glyph;
20326 else
20328 while (glyph <= start
20329 && glyph->type == CHAR_GLYPH
20330 && NILP (glyph->object))
20331 ++glyph;
20334 /* If last glyph is a space or stretch, and it's trailing
20335 whitespace, set the face of all trailing whitespace glyphs in
20336 IT->glyph_row to `trailing-whitespace'. */
20337 if ((row->reversed_p ? glyph <= start : glyph >= start)
20338 && BUFFERP (glyph->object)
20339 && (glyph->type == STRETCH_GLYPH
20340 || (glyph->type == CHAR_GLYPH
20341 && glyph->u.ch == ' '))
20342 && trailing_whitespace_p (glyph->charpos))
20344 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20345 if (face_id < 0)
20346 return;
20348 if (!row->reversed_p)
20350 while (glyph >= start
20351 && BUFFERP (glyph->object)
20352 && (glyph->type == STRETCH_GLYPH
20353 || (glyph->type == CHAR_GLYPH
20354 && glyph->u.ch == ' ')))
20355 (glyph--)->face_id = face_id;
20357 else
20359 while (glyph <= start
20360 && BUFFERP (glyph->object)
20361 && (glyph->type == STRETCH_GLYPH
20362 || (glyph->type == CHAR_GLYPH
20363 && glyph->u.ch == ' ')))
20364 (glyph++)->face_id = face_id;
20371 /* Value is true if glyph row ROW should be
20372 considered to hold the buffer position CHARPOS. */
20374 static bool
20375 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20377 bool result = true;
20379 if (charpos == CHARPOS (row->end.pos)
20380 || charpos == MATRIX_ROW_END_CHARPOS (row))
20382 /* Suppose the row ends on a string.
20383 Unless the row is continued, that means it ends on a newline
20384 in the string. If it's anything other than a display string
20385 (e.g., a before-string from an overlay), we don't want the
20386 cursor there. (This heuristic seems to give the optimal
20387 behavior for the various types of multi-line strings.)
20388 One exception: if the string has `cursor' property on one of
20389 its characters, we _do_ want the cursor there. */
20390 if (CHARPOS (row->end.string_pos) >= 0)
20392 if (row->continued_p)
20393 result = true;
20394 else
20396 /* Check for `display' property. */
20397 struct glyph *beg = row->glyphs[TEXT_AREA];
20398 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20399 struct glyph *glyph;
20401 result = false;
20402 for (glyph = end; glyph >= beg; --glyph)
20403 if (STRINGP (glyph->object))
20405 Lisp_Object prop
20406 = Fget_char_property (make_number (charpos),
20407 Qdisplay, Qnil);
20408 result =
20409 (!NILP (prop)
20410 && display_prop_string_p (prop, glyph->object));
20411 /* If there's a `cursor' property on one of the
20412 string's characters, this row is a cursor row,
20413 even though this is not a display string. */
20414 if (!result)
20416 Lisp_Object s = glyph->object;
20418 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20420 ptrdiff_t gpos = glyph->charpos;
20422 if (!NILP (Fget_char_property (make_number (gpos),
20423 Qcursor, s)))
20425 result = true;
20426 break;
20430 break;
20434 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20436 /* If the row ends in middle of a real character,
20437 and the line is continued, we want the cursor here.
20438 That's because CHARPOS (ROW->end.pos) would equal
20439 PT if PT is before the character. */
20440 if (!row->ends_in_ellipsis_p)
20441 result = row->continued_p;
20442 else
20443 /* If the row ends in an ellipsis, then
20444 CHARPOS (ROW->end.pos) will equal point after the
20445 invisible text. We want that position to be displayed
20446 after the ellipsis. */
20447 result = false;
20449 /* If the row ends at ZV, display the cursor at the end of that
20450 row instead of at the start of the row below. */
20451 else
20452 result = row->ends_at_zv_p;
20455 return result;
20458 /* Value is true if glyph row ROW should be
20459 used to hold the cursor. */
20461 static bool
20462 cursor_row_p (struct glyph_row *row)
20464 return row_for_charpos_p (row, PT);
20469 /* Push the property PROP so that it will be rendered at the current
20470 position in IT. Return true if PROP was successfully pushed, false
20471 otherwise. Called from handle_line_prefix to handle the
20472 `line-prefix' and `wrap-prefix' properties. */
20474 static bool
20475 push_prefix_prop (struct it *it, Lisp_Object prop)
20477 struct text_pos pos =
20478 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20480 eassert (it->method == GET_FROM_BUFFER
20481 || it->method == GET_FROM_DISPLAY_VECTOR
20482 || it->method == GET_FROM_STRING
20483 || it->method == GET_FROM_IMAGE);
20485 /* We need to save the current buffer/string position, so it will be
20486 restored by pop_it, because iterate_out_of_display_property
20487 depends on that being set correctly, but some situations leave
20488 it->position not yet set when this function is called. */
20489 push_it (it, &pos);
20491 if (STRINGP (prop))
20493 if (SCHARS (prop) == 0)
20495 pop_it (it);
20496 return false;
20499 it->string = prop;
20500 it->string_from_prefix_prop_p = true;
20501 it->multibyte_p = STRING_MULTIBYTE (it->string);
20502 it->current.overlay_string_index = -1;
20503 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20504 it->end_charpos = it->string_nchars = SCHARS (it->string);
20505 it->method = GET_FROM_STRING;
20506 it->stop_charpos = 0;
20507 it->prev_stop = 0;
20508 it->base_level_stop = 0;
20510 /* Force paragraph direction to be that of the parent
20511 buffer/string. */
20512 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20513 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20514 else
20515 it->paragraph_embedding = L2R;
20517 /* Set up the bidi iterator for this display string. */
20518 if (it->bidi_p)
20520 it->bidi_it.string.lstring = it->string;
20521 it->bidi_it.string.s = NULL;
20522 it->bidi_it.string.schars = it->end_charpos;
20523 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20524 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20525 it->bidi_it.string.unibyte = !it->multibyte_p;
20526 it->bidi_it.w = it->w;
20527 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20530 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20532 it->method = GET_FROM_STRETCH;
20533 it->object = prop;
20535 #ifdef HAVE_WINDOW_SYSTEM
20536 else if (IMAGEP (prop))
20538 it->what = IT_IMAGE;
20539 it->image_id = lookup_image (it->f, prop);
20540 it->method = GET_FROM_IMAGE;
20542 #endif /* HAVE_WINDOW_SYSTEM */
20543 else
20545 pop_it (it); /* bogus display property, give up */
20546 return false;
20549 return true;
20552 /* Return the character-property PROP at the current position in IT. */
20554 static Lisp_Object
20555 get_it_property (struct it *it, Lisp_Object prop)
20557 Lisp_Object position, object = it->object;
20559 if (STRINGP (object))
20560 position = make_number (IT_STRING_CHARPOS (*it));
20561 else if (BUFFERP (object))
20563 position = make_number (IT_CHARPOS (*it));
20564 object = it->window;
20566 else
20567 return Qnil;
20569 return Fget_char_property (position, prop, object);
20572 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20574 static void
20575 handle_line_prefix (struct it *it)
20577 Lisp_Object prefix;
20579 if (it->continuation_lines_width > 0)
20581 prefix = get_it_property (it, Qwrap_prefix);
20582 if (NILP (prefix))
20583 prefix = Vwrap_prefix;
20585 else
20587 prefix = get_it_property (it, Qline_prefix);
20588 if (NILP (prefix))
20589 prefix = Vline_prefix;
20591 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20593 /* If the prefix is wider than the window, and we try to wrap
20594 it, it would acquire its own wrap prefix, and so on till the
20595 iterator stack overflows. So, don't wrap the prefix. */
20596 it->line_wrap = TRUNCATE;
20597 it->avoid_cursor_p = true;
20603 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20604 only for R2L lines from display_line and display_string, when they
20605 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20606 the line/string needs to be continued on the next glyph row. */
20607 static void
20608 unproduce_glyphs (struct it *it, int n)
20610 struct glyph *glyph, *end;
20612 eassert (it->glyph_row);
20613 eassert (it->glyph_row->reversed_p);
20614 eassert (it->area == TEXT_AREA);
20615 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20617 if (n > it->glyph_row->used[TEXT_AREA])
20618 n = it->glyph_row->used[TEXT_AREA];
20619 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20620 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20621 for ( ; glyph < end; glyph++)
20622 glyph[-n] = *glyph;
20625 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20626 and ROW->maxpos. */
20627 static void
20628 find_row_edges (struct it *it, struct glyph_row *row,
20629 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20630 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20632 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20633 lines' rows is implemented for bidi-reordered rows. */
20635 /* ROW->minpos is the value of min_pos, the minimal buffer position
20636 we have in ROW, or ROW->start.pos if that is smaller. */
20637 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20638 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20639 else
20640 /* We didn't find buffer positions smaller than ROW->start, or
20641 didn't find _any_ valid buffer positions in any of the glyphs,
20642 so we must trust the iterator's computed positions. */
20643 row->minpos = row->start.pos;
20644 if (max_pos <= 0)
20646 max_pos = CHARPOS (it->current.pos);
20647 max_bpos = BYTEPOS (it->current.pos);
20650 /* Here are the various use-cases for ending the row, and the
20651 corresponding values for ROW->maxpos:
20653 Line ends in a newline from buffer eol_pos + 1
20654 Line is continued from buffer max_pos + 1
20655 Line is truncated on right it->current.pos
20656 Line ends in a newline from string max_pos + 1(*)
20657 (*) + 1 only when line ends in a forward scan
20658 Line is continued from string max_pos
20659 Line is continued from display vector max_pos
20660 Line is entirely from a string min_pos == max_pos
20661 Line is entirely from a display vector min_pos == max_pos
20662 Line that ends at ZV ZV
20664 If you discover other use-cases, please add them here as
20665 appropriate. */
20666 if (row->ends_at_zv_p)
20667 row->maxpos = it->current.pos;
20668 else if (row->used[TEXT_AREA])
20670 bool seen_this_string = false;
20671 struct glyph_row *r1 = row - 1;
20673 /* Did we see the same display string on the previous row? */
20674 if (STRINGP (it->object)
20675 /* this is not the first row */
20676 && row > it->w->desired_matrix->rows
20677 /* previous row is not the header line */
20678 && !r1->mode_line_p
20679 /* previous row also ends in a newline from a string */
20680 && r1->ends_in_newline_from_string_p)
20682 struct glyph *start, *end;
20684 /* Search for the last glyph of the previous row that came
20685 from buffer or string. Depending on whether the row is
20686 L2R or R2L, we need to process it front to back or the
20687 other way round. */
20688 if (!r1->reversed_p)
20690 start = r1->glyphs[TEXT_AREA];
20691 end = start + r1->used[TEXT_AREA];
20692 /* Glyphs inserted by redisplay have nil as their object. */
20693 while (end > start
20694 && NILP ((end - 1)->object)
20695 && (end - 1)->charpos <= 0)
20696 --end;
20697 if (end > start)
20699 if (EQ ((end - 1)->object, it->object))
20700 seen_this_string = true;
20702 else
20703 /* If all the glyphs of the previous row were inserted
20704 by redisplay, it means the previous row was
20705 produced from a single newline, which is only
20706 possible if that newline came from the same string
20707 as the one which produced this ROW. */
20708 seen_this_string = true;
20710 else
20712 end = r1->glyphs[TEXT_AREA] - 1;
20713 start = end + r1->used[TEXT_AREA];
20714 while (end < start
20715 && NILP ((end + 1)->object)
20716 && (end + 1)->charpos <= 0)
20717 ++end;
20718 if (end < start)
20720 if (EQ ((end + 1)->object, it->object))
20721 seen_this_string = true;
20723 else
20724 seen_this_string = true;
20727 /* Take note of each display string that covers a newline only
20728 once, the first time we see it. This is for when a display
20729 string includes more than one newline in it. */
20730 if (row->ends_in_newline_from_string_p && !seen_this_string)
20732 /* If we were scanning the buffer forward when we displayed
20733 the string, we want to account for at least one buffer
20734 position that belongs to this row (position covered by
20735 the display string), so that cursor positioning will
20736 consider this row as a candidate when point is at the end
20737 of the visual line represented by this row. This is not
20738 required when scanning back, because max_pos will already
20739 have a much larger value. */
20740 if (CHARPOS (row->end.pos) > max_pos)
20741 INC_BOTH (max_pos, max_bpos);
20742 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20744 else if (CHARPOS (it->eol_pos) > 0)
20745 SET_TEXT_POS (row->maxpos,
20746 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20747 else if (row->continued_p)
20749 /* If max_pos is different from IT's current position, it
20750 means IT->method does not belong to the display element
20751 at max_pos. However, it also means that the display
20752 element at max_pos was displayed in its entirety on this
20753 line, which is equivalent to saying that the next line
20754 starts at the next buffer position. */
20755 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20756 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20757 else
20759 INC_BOTH (max_pos, max_bpos);
20760 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20763 else if (row->truncated_on_right_p)
20764 /* display_line already called reseat_at_next_visible_line_start,
20765 which puts the iterator at the beginning of the next line, in
20766 the logical order. */
20767 row->maxpos = it->current.pos;
20768 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20769 /* A line that is entirely from a string/image/stretch... */
20770 row->maxpos = row->minpos;
20771 else
20772 emacs_abort ();
20774 else
20775 row->maxpos = it->current.pos;
20778 /* Like display_count_lines, but capable of counting outside of the
20779 current narrowed region. */
20780 static ptrdiff_t
20781 display_count_lines_logically (ptrdiff_t start_byte, ptrdiff_t limit_byte,
20782 ptrdiff_t count, ptrdiff_t *byte_pos_ptr)
20784 if (!display_line_numbers_widen || (BEGV == BEG && ZV == Z))
20785 return display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20787 ptrdiff_t val;
20788 ptrdiff_t pdl_count = SPECPDL_INDEX ();
20789 record_unwind_protect (save_restriction_restore, save_restriction_save ());
20790 Fwiden ();
20791 val = display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20792 unbind_to (pdl_count, Qnil);
20793 return val;
20796 /* Count the number of screen lines in window IT->w between character
20797 position IT_CHARPOS(*IT) and the line showing that window's point. */
20798 static ptrdiff_t
20799 display_count_lines_visually (struct it *it)
20801 struct it tem_it;
20802 ptrdiff_t to;
20803 struct text_pos from;
20805 /* If we already calculated a relative line number, use that. This
20806 trick relies on the fact that visual lines (a.k.a. "glyph rows")
20807 are laid out sequentially, one by one, for each sequence of calls
20808 to display_line or other similar function that follows a call to
20809 init_iterator. */
20810 if (it->lnum_bytepos > 0)
20811 return it->lnum + 1;
20812 else
20814 ptrdiff_t count = SPECPDL_INDEX ();
20816 if (IT_CHARPOS (*it) <= PT)
20818 from = it->current.pos;
20819 to = PT;
20821 else
20823 SET_TEXT_POS (from, PT, PT_BYTE);
20824 to = IT_CHARPOS (*it);
20826 start_display (&tem_it, it->w, from);
20827 /* Need to disable visual mode temporarily, since otherwise the
20828 call to move_it_to will cause infinite recursion. */
20829 specbind (Qdisplay_line_numbers, Qrelative);
20830 /* Some redisplay optimizations could invoke us very far from
20831 PT, which will make the caller painfully slow. There should
20832 be no need to go too far beyond the window's bottom, as any
20833 such optimization will fail to show point anyway. */
20834 move_it_to (&tem_it, to, -1,
20835 tem_it.last_visible_y
20836 + (SCROLL_LIMIT + 10) * FRAME_LINE_HEIGHT (tem_it.f),
20837 -1, MOVE_TO_POS | MOVE_TO_Y);
20838 unbind_to (count, Qnil);
20839 return IT_CHARPOS (*it) <= PT ? -tem_it.vpos : tem_it.vpos;
20843 /* Produce the line-number glyphs for the current glyph_row. If
20844 IT->glyph_row is non-NULL, populate the row with the produced
20845 glyphs. */
20846 static void
20847 maybe_produce_line_number (struct it *it)
20849 ptrdiff_t last_line = it->lnum;
20850 ptrdiff_t start_from, bytepos;
20851 ptrdiff_t this_line;
20852 bool first_time = false;
20853 ptrdiff_t beg_byte = display_line_numbers_widen ? BEG_BYTE : BEGV_BYTE;
20854 ptrdiff_t z_byte = display_line_numbers_widen ? Z_BYTE : ZV_BYTE;
20855 void *itdata = bidi_shelve_cache ();
20857 if (EQ (Vdisplay_line_numbers, Qvisual))
20858 this_line = display_count_lines_visually (it);
20859 else
20861 if (!last_line)
20863 /* If possible, reuse data cached by line-number-mode. */
20864 if (it->w->base_line_number > 0
20865 && it->w->base_line_pos > 0
20866 && it->w->base_line_pos <= IT_CHARPOS (*it)
20867 /* line-number-mode always displays narrowed line
20868 numbers, so we cannot use its data if the user wants
20869 line numbers that disregard narrowing. */
20870 && !(display_line_numbers_widen
20871 && (BEG_BYTE != BEGV_BYTE || Z_BYTE != ZV_BYTE)))
20873 start_from = CHAR_TO_BYTE (it->w->base_line_pos);
20874 last_line = it->w->base_line_number - 1;
20876 else
20877 start_from = beg_byte;
20878 if (!it->lnum_bytepos)
20879 first_time = true;
20881 else
20882 start_from = it->lnum_bytepos;
20884 /* Paranoia: what if someone changes the narrowing since the
20885 last time display_line was called? Shouldn't really happen,
20886 but who knows what some crazy Lisp invoked by :eval could do? */
20887 if (!(beg_byte <= start_from && start_from <= z_byte))
20889 last_line = 0;
20890 start_from = beg_byte;
20893 this_line =
20894 last_line + display_count_lines_logically (start_from,
20895 IT_BYTEPOS (*it),
20896 IT_CHARPOS (*it), &bytepos);
20897 eassert (this_line > 0 || (this_line == 0 && start_from == beg_byte));
20898 eassert (bytepos == IT_BYTEPOS (*it));
20901 /* Record the line number information. */
20902 if (this_line != last_line || !it->lnum_bytepos)
20904 it->lnum = this_line;
20905 it->lnum_bytepos = IT_BYTEPOS (*it);
20908 /* Produce the glyphs for the line number. */
20909 struct it tem_it;
20910 char lnum_buf[INT_STRLEN_BOUND (ptrdiff_t) + 1];
20911 bool beyond_zv = IT_BYTEPOS (*it) >= ZV_BYTE ? true : false;
20912 ptrdiff_t lnum_offset = -1; /* to produce 1-based line numbers */
20913 int lnum_face_id = merge_faces (it->f, Qline_number, 0, DEFAULT_FACE_ID);
20914 int current_lnum_face_id
20915 = merge_faces (it->f, Qline_number_current_line, 0, DEFAULT_FACE_ID);
20916 /* Compute point's line number if needed. */
20917 if ((EQ (Vdisplay_line_numbers, Qrelative)
20918 || EQ (Vdisplay_line_numbers, Qvisual)
20919 || lnum_face_id != current_lnum_face_id)
20920 && !it->pt_lnum)
20922 ptrdiff_t ignored;
20923 if (PT_BYTE > it->lnum_bytepos && !EQ (Vdisplay_line_numbers, Qvisual))
20924 it->pt_lnum =
20925 this_line + display_count_lines_logically (it->lnum_bytepos, PT_BYTE,
20926 PT, &ignored);
20927 else
20928 it->pt_lnum = display_count_lines_logically (beg_byte, PT_BYTE, PT,
20929 &ignored);
20931 /* Compute the required width if needed. */
20932 if (!it->lnum_width)
20934 if (NATNUMP (Vdisplay_line_numbers_width))
20935 it->lnum_width = XFASTINT (Vdisplay_line_numbers_width);
20937 /* Max line number to be displayed cannot be more than the one
20938 corresponding to the last row of the desired matrix. */
20939 ptrdiff_t max_lnum;
20941 if (NILP (Vdisplay_line_numbers_current_absolute)
20942 && (EQ (Vdisplay_line_numbers, Qrelative)
20943 || EQ (Vdisplay_line_numbers, Qvisual)))
20944 /* We subtract one more because the current line is always
20945 zero in this mode. */
20946 max_lnum = it->w->desired_matrix->nrows - 2;
20947 else if (EQ (Vdisplay_line_numbers, Qvisual))
20948 max_lnum = it->pt_lnum + it->w->desired_matrix->nrows - 1;
20949 else
20950 max_lnum = this_line + it->w->desired_matrix->nrows - 1 - it->vpos;
20951 max_lnum = max (1, max_lnum);
20952 it->lnum_width = max (it->lnum_width, log10 (max_lnum) + 1);
20953 eassert (it->lnum_width > 0);
20955 if (EQ (Vdisplay_line_numbers, Qrelative))
20956 lnum_offset = it->pt_lnum;
20957 else if (EQ (Vdisplay_line_numbers, Qvisual))
20958 lnum_offset = 0;
20960 /* Under 'relative', display the absolute line number for the
20961 current line, unless the user requests otherwise. */
20962 ptrdiff_t lnum_to_display = eabs (this_line - lnum_offset);
20963 if ((EQ (Vdisplay_line_numbers, Qrelative)
20964 || EQ (Vdisplay_line_numbers, Qvisual))
20965 && lnum_to_display == 0
20966 && !NILP (Vdisplay_line_numbers_current_absolute))
20967 lnum_to_display = it->pt_lnum + 1;
20968 /* In L2R rows we need to append the blank separator, in R2L
20969 rows we need to prepend it. But this function is usually
20970 called when no display elements were produced from the
20971 following line, so the paragraph direction might be unknown.
20972 Therefore we cheat and add 2 blanks, one on either side. */
20973 pint2str (lnum_buf, it->lnum_width + 1, lnum_to_display);
20974 strcat (lnum_buf, " ");
20976 /* Setup for producing the glyphs. */
20977 init_iterator (&tem_it, it->w, -1, -1, &scratch_glyph_row,
20978 /* FIXME: Use specialized face. */
20979 DEFAULT_FACE_ID);
20980 scratch_glyph_row.reversed_p = false;
20981 scratch_glyph_row.used[TEXT_AREA] = 0;
20982 SET_TEXT_POS (tem_it.position, 0, 0);
20983 tem_it.avoid_cursor_p = true;
20984 tem_it.bidi_p = true;
20985 tem_it.bidi_it.type = WEAK_EN;
20986 /* According to UAX#9, EN goes up 2 levels in L2R paragraph and
20987 1 level in R2L paragraphs. Emulate that, assuming we are in
20988 an L2R paragraph. */
20989 tem_it.bidi_it.resolved_level = 2;
20991 /* Produce glyphs for the line number in a scratch glyph_row. */
20992 int n_glyphs_before;
20993 for (const char *p = lnum_buf; *p; p++)
20995 /* For continuation lines and lines after ZV, instead of a line
20996 number, produce a blank prefix of the same width. Use the
20997 default face for the blank field beyond ZV. */
20998 if (beyond_zv)
20999 tem_it.face_id = it->base_face_id;
21000 else if (lnum_face_id != current_lnum_face_id
21001 && (EQ (Vdisplay_line_numbers, Qvisual)
21002 ? this_line == 0
21003 : this_line == it->pt_lnum))
21004 tem_it.face_id = current_lnum_face_id;
21005 else
21006 tem_it.face_id = lnum_face_id;
21007 if (beyond_zv
21008 /* Don't display the same line number more than once. */
21009 || (!EQ (Vdisplay_line_numbers, Qvisual)
21010 && (it->continuation_lines_width > 0
21011 || (this_line == last_line && !first_time))))
21012 tem_it.c = tem_it.char_to_display = ' ';
21013 else
21014 tem_it.c = tem_it.char_to_display = *p;
21015 tem_it.len = 1;
21016 n_glyphs_before = scratch_glyph_row.used[TEXT_AREA];
21017 /* Make sure these glyphs will have a "position" of -1. */
21018 SET_TEXT_POS (tem_it.position, -1, -1);
21019 PRODUCE_GLYPHS (&tem_it);
21021 /* Stop producing glyphs if we don't have enough space on
21022 this line. FIXME: should we refrain from producing the
21023 line number at all in that case? */
21024 if (tem_it.current_x > tem_it.last_visible_x)
21026 scratch_glyph_row.used[TEXT_AREA] = n_glyphs_before;
21027 break;
21031 /* Record the width in pixels we need for the line number display. */
21032 it->lnum_pixel_width = tem_it.current_x;
21033 /* Copy the produced glyphs into IT's glyph_row. */
21034 struct glyph *g = scratch_glyph_row.glyphs[TEXT_AREA];
21035 struct glyph *e = g + scratch_glyph_row.used[TEXT_AREA];
21036 struct glyph *p = it->glyph_row ? it->glyph_row->glyphs[TEXT_AREA] : NULL;
21037 short *u = it->glyph_row ? &it->glyph_row->used[TEXT_AREA] : NULL;
21039 eassert (it->glyph_row == NULL || it->glyph_row->used[TEXT_AREA] == 0);
21041 for ( ; g < e; g++)
21043 it->current_x += g->pixel_width;
21044 /* The following is important when this function is called
21045 from move_it_in_display_line_to: HPOS is incremented only
21046 when we are in the visible portion of the glyph row. */
21047 if (it->current_x > it->first_visible_x)
21048 it->hpos++;
21049 if (p)
21051 *p++ = *g;
21052 (*u)++;
21056 /* Update IT's metrics due to glyphs produced for line numbers. */
21057 if (it->glyph_row)
21059 struct glyph_row *row = it->glyph_row;
21061 it->max_ascent = max (row->ascent, tem_it.max_ascent);
21062 it->max_descent = max (row->height - row->ascent, tem_it.max_descent);
21063 it->max_phys_ascent = max (row->phys_ascent, tem_it.max_phys_ascent);
21064 it->max_phys_descent = max (row->phys_height - row->phys_ascent,
21065 tem_it.max_phys_descent);
21067 else
21069 it->max_ascent = max (it->max_ascent, tem_it.max_ascent);
21070 it->max_descent = max (it->max_descent, tem_it.max_descent);
21071 it->max_phys_ascent = max (it->max_phys_ascent, tem_it.max_phys_ascent);
21072 it->max_phys_descent = max (it->max_phys_descent, tem_it.max_phys_descent);
21075 bidi_unshelve_cache (itdata, false);
21078 /* Return true if this glyph row needs a line number to be produced
21079 for it. */
21080 static bool
21081 should_produce_line_number (struct it *it)
21083 if (NILP (Vdisplay_line_numbers))
21084 return false;
21086 /* Don't display line numbers in minibuffer windows. */
21087 if (MINI_WINDOW_P (it->w))
21088 return false;
21090 #ifdef HAVE_WINDOW_SYSTEM
21091 /* Don't display line number in tooltip frames. */
21092 if (FRAMEP (tip_frame) && EQ (WINDOW_FRAME (it->w), tip_frame))
21093 return false;
21094 #endif
21096 /* If the character at current position has a non-nil special
21097 property, disable line numbers for this row. This is for
21098 packages such as company-mode, which need this for their tricky
21099 layout, where line numbers get in the way. */
21100 Lisp_Object val = Fget_char_property (make_number (IT_CHARPOS (*it)),
21101 Qdisplay_line_numbers_disable,
21102 it->window);
21103 /* For ZV, we need to also look in empty overlays at that point,
21104 because get-char-property always returns nil for ZV, except if
21105 the property is in 'default-text-properties'. */
21106 if (NILP (val) && IT_CHARPOS (*it) >= ZV)
21107 val = disable_line_numbers_overlay_at_eob ();
21108 return NILP (val) ? true : false;
21111 /* Return true if ROW has no glyphs except those inserted by the
21112 display engine. This is needed for indicate-empty-lines and
21113 similar features when the glyph row starts with glyphs which didn't
21114 come from buffer or string. */
21115 static bool
21116 row_text_area_empty (struct glyph_row *row)
21118 if (!row->reversed_p)
21120 for (struct glyph *g = row->glyphs[TEXT_AREA];
21121 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21122 g++)
21123 if (!NILP (g->object) || g->charpos > 0)
21124 return false;
21126 else
21128 for (struct glyph *g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21129 g > row->glyphs[TEXT_AREA];
21130 g--)
21131 if (!NILP ((g - 1)->object) || (g - 1)->charpos > 0)
21132 return false;
21135 return true;
21138 /* Construct the glyph row IT->glyph_row in the desired matrix of
21139 IT->w from text at the current position of IT. See dispextern.h
21140 for an overview of struct it. Value is true if
21141 IT->glyph_row displays text, as opposed to a line displaying ZV
21142 only. CURSOR_VPOS is the window-relative vertical position of
21143 the glyph row displaying the cursor, or -1 if unknown. */
21145 static bool
21146 display_line (struct it *it, int cursor_vpos)
21148 struct glyph_row *row = it->glyph_row;
21149 Lisp_Object overlay_arrow_string;
21150 struct it wrap_it;
21151 void *wrap_data = NULL;
21152 bool may_wrap = false;
21153 int wrap_x UNINIT;
21154 int wrap_row_used = -1;
21155 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
21156 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
21157 int wrap_row_extra_line_spacing UNINIT;
21158 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
21159 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
21160 int cvpos;
21161 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
21162 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
21163 bool pending_handle_line_prefix = false;
21164 int header_line = window_wants_header_line (it->w);
21165 bool hscroll_this_line = (cursor_vpos >= 0
21166 && it->vpos == cursor_vpos - header_line
21167 && hscrolling_current_line_p (it->w));
21168 int first_visible_x = it->first_visible_x;
21169 int last_visible_x = it->last_visible_x;
21170 int x_incr = 0;
21172 /* We always start displaying at hpos zero even if hscrolled. */
21173 eassert (it->hpos == 0 && it->current_x == 0);
21175 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
21176 >= it->w->desired_matrix->nrows)
21178 it->w->nrows_scale_factor++;
21179 it->f->fonts_changed = true;
21180 return false;
21183 /* Clear the result glyph row and enable it. */
21184 prepare_desired_row (it->w, row, false);
21186 row->y = it->current_y;
21187 row->start = it->start;
21188 row->continuation_lines_width = it->continuation_lines_width;
21189 row->displays_text_p = true;
21190 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
21191 it->starts_in_middle_of_char_p = false;
21193 /* Arrange the overlays nicely for our purposes. Usually, we call
21194 display_line on only one line at a time, in which case this
21195 can't really hurt too much, or we call it on lines which appear
21196 one after another in the buffer, in which case all calls to
21197 recenter_overlay_lists but the first will be pretty cheap. */
21198 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
21200 /* If we are going to display the cursor's line, account for the
21201 hscroll of that line. We subtract the window's min_hscroll,
21202 because that was already accounted for in init_iterator. */
21203 if (hscroll_this_line)
21204 x_incr =
21205 (window_hscroll_limited (it->w, it->f) - it->w->min_hscroll)
21206 * FRAME_COLUMN_WIDTH (it->f);
21208 bool line_number_needed = should_produce_line_number (it);
21210 /* Move over display elements that are not visible because we are
21211 hscrolled. This may stop at an x-position < first_visible_x
21212 if the first glyph is partially visible or if we hit a line end. */
21213 if (it->current_x < it->first_visible_x + x_incr)
21215 enum move_it_result move_result;
21217 this_line_min_pos = row->start.pos;
21218 if (hscroll_this_line)
21220 it->first_visible_x += x_incr;
21221 it->last_visible_x += x_incr;
21223 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
21224 MOVE_TO_POS | MOVE_TO_X);
21225 /* If we are under a large hscroll, move_it_in_display_line_to
21226 could hit the end of the line without reaching
21227 first_visible_x. Pretend that we did reach it. This is
21228 especially important on a TTY, where we will call
21229 extend_face_to_end_of_line, which needs to know how many
21230 blank glyphs to produce. */
21231 if (it->current_x < it->first_visible_x
21232 && (move_result == MOVE_NEWLINE_OR_CR
21233 || move_result == MOVE_POS_MATCH_OR_ZV))
21234 it->current_x = it->first_visible_x;
21236 /* Record the smallest positions seen while we moved over
21237 display elements that are not visible. This is needed by
21238 redisplay_internal for optimizing the case where the cursor
21239 stays inside the same line. The rest of this function only
21240 considers positions that are actually displayed, so
21241 RECORD_MAX_MIN_POS will not otherwise record positions that
21242 are hscrolled to the left of the left edge of the window. */
21243 min_pos = CHARPOS (this_line_min_pos);
21244 min_bpos = BYTEPOS (this_line_min_pos);
21246 /* Produce line number, if needed. */
21247 if (line_number_needed)
21248 maybe_produce_line_number (it);
21250 else if (it->area == TEXT_AREA)
21252 /* Line numbers should precede the line-prefix or wrap-prefix. */
21253 if (line_number_needed)
21254 maybe_produce_line_number (it);
21256 /* We only do this when not calling move_it_in_display_line_to
21257 above, because that function calls itself handle_line_prefix. */
21258 handle_line_prefix (it);
21260 else
21262 /* Line-prefix and wrap-prefix are always displayed in the text
21263 area. But if this is the first call to display_line after
21264 init_iterator, the iterator might have been set up to write
21265 into a marginal area, e.g. if the line begins with some
21266 display property that writes to the margins. So we need to
21267 wait with the call to handle_line_prefix until whatever
21268 writes to the margin has done its job. */
21269 pending_handle_line_prefix = true;
21272 /* Get the initial row height. This is either the height of the
21273 text hscrolled, if there is any, or zero. */
21274 row->ascent = it->max_ascent;
21275 row->height = it->max_ascent + it->max_descent;
21276 row->phys_ascent = it->max_phys_ascent;
21277 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21278 row->extra_line_spacing = it->max_extra_line_spacing;
21280 /* Utility macro to record max and min buffer positions seen until now. */
21281 #define RECORD_MAX_MIN_POS(IT) \
21282 do \
21284 bool composition_p \
21285 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
21286 ptrdiff_t current_pos = \
21287 composition_p ? (IT)->cmp_it.charpos \
21288 : IT_CHARPOS (*(IT)); \
21289 ptrdiff_t current_bpos = \
21290 composition_p ? CHAR_TO_BYTE (current_pos) \
21291 : IT_BYTEPOS (*(IT)); \
21292 if (current_pos < min_pos) \
21294 min_pos = current_pos; \
21295 min_bpos = current_bpos; \
21297 if (IT_CHARPOS (*it) > max_pos) \
21299 max_pos = IT_CHARPOS (*it); \
21300 max_bpos = IT_BYTEPOS (*it); \
21303 while (false)
21305 /* Loop generating characters. The loop is left with IT on the next
21306 character to display. */
21307 while (true)
21309 int n_glyphs_before, hpos_before, x_before;
21310 int x, nglyphs;
21311 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
21313 /* Retrieve the next thing to display. Value is false if end of
21314 buffer reached. */
21315 if (!get_next_display_element (it))
21317 bool row_has_glyphs = false;
21318 /* Maybe add a space at the end of this line that is used to
21319 display the cursor there under X. Set the charpos of the
21320 first glyph of blank lines not corresponding to any text
21321 to -1. */
21322 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21323 row->exact_window_width_line_p = true;
21324 else if ((append_space_for_newline (it, true)
21325 && row->used[TEXT_AREA] == 1)
21326 || row->used[TEXT_AREA] == 0
21327 || (row_has_glyphs = row_text_area_empty (row)))
21329 row->glyphs[TEXT_AREA]->charpos = -1;
21330 /* Don't reset the displays_text_p flag if we are
21331 displaying line numbers or line-prefix. */
21332 if (!row_has_glyphs)
21333 row->displays_text_p = false;
21335 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
21336 && (!MINI_WINDOW_P (it->w)))
21337 row->indicate_empty_line_p = true;
21340 it->continuation_lines_width = 0;
21341 /* Reset those iterator values set from display property
21342 values. This is for the case when the display property
21343 ends at ZV, and is not a replacing property, so pop_it is
21344 not called. */
21345 it->font_height = Qnil;
21346 it->voffset = 0;
21347 row->ends_at_zv_p = true;
21348 /* A row that displays right-to-left text must always have
21349 its last face extended all the way to the end of line,
21350 even if this row ends in ZV, because we still write to
21351 the screen left to right. We also need to extend the
21352 last face if the default face is remapped to some
21353 different face, otherwise the functions that clear
21354 portions of the screen will clear with the default face's
21355 background color. */
21356 if (row->reversed_p
21357 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
21358 extend_face_to_end_of_line (it);
21359 break;
21362 /* Now, get the metrics of what we want to display. This also
21363 generates glyphs in `row' (which is IT->glyph_row). */
21364 n_glyphs_before = row->used[TEXT_AREA];
21365 x = it->current_x;
21367 /* Remember the line height so far in case the next element doesn't
21368 fit on the line. */
21369 if (it->line_wrap != TRUNCATE)
21371 ascent = it->max_ascent;
21372 descent = it->max_descent;
21373 phys_ascent = it->max_phys_ascent;
21374 phys_descent = it->max_phys_descent;
21376 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
21378 if (IT_DISPLAYING_WHITESPACE (it))
21379 may_wrap = true;
21380 else if (may_wrap)
21382 SAVE_IT (wrap_it, *it, wrap_data);
21383 wrap_x = x;
21384 wrap_row_used = row->used[TEXT_AREA];
21385 wrap_row_ascent = row->ascent;
21386 wrap_row_height = row->height;
21387 wrap_row_phys_ascent = row->phys_ascent;
21388 wrap_row_phys_height = row->phys_height;
21389 wrap_row_extra_line_spacing = row->extra_line_spacing;
21390 wrap_row_min_pos = min_pos;
21391 wrap_row_min_bpos = min_bpos;
21392 wrap_row_max_pos = max_pos;
21393 wrap_row_max_bpos = max_bpos;
21394 may_wrap = false;
21399 PRODUCE_GLYPHS (it);
21401 /* If this display element was in marginal areas, continue with
21402 the next one. */
21403 if (it->area != TEXT_AREA)
21405 row->ascent = max (row->ascent, it->max_ascent);
21406 row->height = max (row->height, it->max_ascent + it->max_descent);
21407 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21408 row->phys_height = max (row->phys_height,
21409 it->max_phys_ascent + it->max_phys_descent);
21410 row->extra_line_spacing = max (row->extra_line_spacing,
21411 it->max_extra_line_spacing);
21412 set_iterator_to_next (it, true);
21413 /* If we didn't handle the line/wrap prefix above, and the
21414 call to set_iterator_to_next just switched to TEXT_AREA,
21415 process the prefix now. */
21416 if (it->area == TEXT_AREA && pending_handle_line_prefix)
21418 /* Line numbers should precede the line-prefix or wrap-prefix. */
21419 if (line_number_needed)
21420 maybe_produce_line_number (it);
21422 pending_handle_line_prefix = false;
21423 handle_line_prefix (it);
21425 continue;
21428 /* Does the display element fit on the line? If we truncate
21429 lines, we should draw past the right edge of the window. If
21430 we don't truncate, we want to stop so that we can display the
21431 continuation glyph before the right margin. If lines are
21432 continued, there are two possible strategies for characters
21433 resulting in more than 1 glyph (e.g. tabs): Display as many
21434 glyphs as possible in this line and leave the rest for the
21435 continuation line, or display the whole element in the next
21436 line. Original redisplay did the former, so we do it also. */
21437 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21438 hpos_before = it->hpos;
21439 x_before = x;
21441 if (/* Not a newline. */
21442 nglyphs > 0
21443 /* Glyphs produced fit entirely in the line. */
21444 && it->current_x < it->last_visible_x)
21446 it->hpos += nglyphs;
21447 row->ascent = max (row->ascent, it->max_ascent);
21448 row->height = max (row->height, it->max_ascent + it->max_descent);
21449 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21450 row->phys_height = max (row->phys_height,
21451 it->max_phys_ascent + it->max_phys_descent);
21452 row->extra_line_spacing = max (row->extra_line_spacing,
21453 it->max_extra_line_spacing);
21454 if (it->current_x - it->pixel_width < it->first_visible_x
21455 /* In R2L rows, we arrange in extend_face_to_end_of_line
21456 to add a right offset to the line, by a suitable
21457 change to the stretch glyph that is the leftmost
21458 glyph of the line. */
21459 && !row->reversed_p)
21460 row->x = x - it->first_visible_x;
21461 /* Record the maximum and minimum buffer positions seen so
21462 far in glyphs that will be displayed by this row. */
21463 if (it->bidi_p)
21464 RECORD_MAX_MIN_POS (it);
21466 else
21468 int i, new_x;
21469 struct glyph *glyph;
21471 for (i = 0; i < nglyphs; ++i, x = new_x)
21473 /* Identify the glyphs added by the last call to
21474 PRODUCE_GLYPHS. In R2L rows, they are prepended to
21475 the previous glyphs. */
21476 if (!row->reversed_p)
21477 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21478 else
21479 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
21480 new_x = x + glyph->pixel_width;
21482 if (/* Lines are continued. */
21483 it->line_wrap != TRUNCATE
21484 && (/* Glyph doesn't fit on the line. */
21485 new_x > it->last_visible_x
21486 /* Or it fits exactly on a window system frame. */
21487 || (new_x == it->last_visible_x
21488 && FRAME_WINDOW_P (it->f)
21489 && (row->reversed_p
21490 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21491 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
21493 /* End of a continued line. */
21495 if (it->hpos == 0
21496 || (new_x == it->last_visible_x
21497 && FRAME_WINDOW_P (it->f)
21498 && (row->reversed_p
21499 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21500 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
21502 /* Current glyph is the only one on the line or
21503 fits exactly on the line. We must continue
21504 the line because we can't draw the cursor
21505 after the glyph. */
21506 row->continued_p = true;
21507 it->current_x = new_x;
21508 it->continuation_lines_width += new_x;
21509 ++it->hpos;
21510 if (i == nglyphs - 1)
21512 /* If line-wrap is on, check if a previous
21513 wrap point was found. */
21514 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
21515 && wrap_row_used > 0
21516 /* Even if there is a previous wrap
21517 point, continue the line here as
21518 usual, if (i) the previous character
21519 was a space or tab AND (ii) the
21520 current character is not. */
21521 && (!may_wrap
21522 || IT_DISPLAYING_WHITESPACE (it)))
21523 goto back_to_wrap;
21525 /* Record the maximum and minimum buffer
21526 positions seen so far in glyphs that will be
21527 displayed by this row. */
21528 if (it->bidi_p)
21529 RECORD_MAX_MIN_POS (it);
21530 set_iterator_to_next (it, true);
21531 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21533 if (!get_next_display_element (it))
21535 row->exact_window_width_line_p = true;
21536 it->continuation_lines_width = 0;
21537 it->font_height = Qnil;
21538 it->voffset = 0;
21539 row->continued_p = false;
21540 row->ends_at_zv_p = true;
21542 else if (ITERATOR_AT_END_OF_LINE_P (it))
21544 row->continued_p = false;
21545 row->exact_window_width_line_p = true;
21547 /* If line-wrap is on, check if a
21548 previous wrap point was found. */
21549 else if (wrap_row_used > 0
21550 /* Even if there is a previous wrap
21551 point, continue the line here as
21552 usual, if (i) the previous character
21553 was a space or tab AND (ii) the
21554 current character is not. */
21555 && (!may_wrap
21556 || IT_DISPLAYING_WHITESPACE (it)))
21557 goto back_to_wrap;
21561 else if (it->bidi_p)
21562 RECORD_MAX_MIN_POS (it);
21563 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21564 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21565 extend_face_to_end_of_line (it);
21567 else if (CHAR_GLYPH_PADDING_P (*glyph)
21568 && !FRAME_WINDOW_P (it->f))
21570 /* A padding glyph that doesn't fit on this line.
21571 This means the whole character doesn't fit
21572 on the line. */
21573 if (row->reversed_p)
21574 unproduce_glyphs (it, row->used[TEXT_AREA]
21575 - n_glyphs_before);
21576 row->used[TEXT_AREA] = n_glyphs_before;
21578 /* Fill the rest of the row with continuation
21579 glyphs like in 20.x. */
21580 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
21581 < row->glyphs[1 + TEXT_AREA])
21582 produce_special_glyphs (it, IT_CONTINUATION);
21584 row->continued_p = true;
21585 it->current_x = x_before;
21586 it->continuation_lines_width += x_before;
21588 /* Restore the height to what it was before the
21589 element not fitting on the line. */
21590 it->max_ascent = ascent;
21591 it->max_descent = descent;
21592 it->max_phys_ascent = phys_ascent;
21593 it->max_phys_descent = phys_descent;
21594 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21595 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21596 extend_face_to_end_of_line (it);
21598 else if (wrap_row_used > 0)
21600 back_to_wrap:
21601 if (row->reversed_p)
21602 unproduce_glyphs (it,
21603 row->used[TEXT_AREA] - wrap_row_used);
21604 RESTORE_IT (it, &wrap_it, wrap_data);
21605 it->continuation_lines_width += wrap_x;
21606 row->used[TEXT_AREA] = wrap_row_used;
21607 row->ascent = wrap_row_ascent;
21608 row->height = wrap_row_height;
21609 row->phys_ascent = wrap_row_phys_ascent;
21610 row->phys_height = wrap_row_phys_height;
21611 row->extra_line_spacing = wrap_row_extra_line_spacing;
21612 min_pos = wrap_row_min_pos;
21613 min_bpos = wrap_row_min_bpos;
21614 max_pos = wrap_row_max_pos;
21615 max_bpos = wrap_row_max_bpos;
21616 row->continued_p = true;
21617 row->ends_at_zv_p = false;
21618 row->exact_window_width_line_p = false;
21619 it->continuation_lines_width += x;
21621 /* Make sure that a non-default face is extended
21622 up to the right margin of the window. */
21623 extend_face_to_end_of_line (it);
21625 else if ((it->what == IT_CHARACTER
21626 || it->what == IT_STRETCH
21627 || it->what == IT_COMPOSITION)
21628 && it->c == '\t' && FRAME_WINDOW_P (it->f))
21630 /* A TAB that extends past the right edge of the
21631 window. This produces a single glyph on
21632 window system frames. We leave the glyph in
21633 this row and let it fill the row, but don't
21634 consume the TAB. */
21635 if ((row->reversed_p
21636 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21637 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21638 produce_special_glyphs (it, IT_CONTINUATION);
21639 it->continuation_lines_width += it->last_visible_x;
21640 row->ends_in_middle_of_char_p = true;
21641 row->continued_p = true;
21642 glyph->pixel_width = it->last_visible_x - x;
21643 it->starts_in_middle_of_char_p = true;
21644 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21645 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21646 extend_face_to_end_of_line (it);
21648 else
21650 /* Something other than a TAB that draws past
21651 the right edge of the window. Restore
21652 positions to values before the element. */
21653 if (row->reversed_p)
21654 unproduce_glyphs (it, row->used[TEXT_AREA]
21655 - (n_glyphs_before + i));
21656 row->used[TEXT_AREA] = n_glyphs_before + i;
21658 /* Display continuation glyphs. */
21659 it->current_x = x_before;
21660 it->continuation_lines_width += x;
21661 if (!FRAME_WINDOW_P (it->f)
21662 || (row->reversed_p
21663 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21664 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21665 produce_special_glyphs (it, IT_CONTINUATION);
21666 row->continued_p = true;
21668 extend_face_to_end_of_line (it);
21670 if (nglyphs > 1 && i > 0)
21672 row->ends_in_middle_of_char_p = true;
21673 it->starts_in_middle_of_char_p = true;
21676 /* Restore the height to what it was before the
21677 element not fitting on the line. */
21678 it->max_ascent = ascent;
21679 it->max_descent = descent;
21680 it->max_phys_ascent = phys_ascent;
21681 it->max_phys_descent = phys_descent;
21684 break;
21686 else if (new_x > it->first_visible_x)
21688 /* Increment number of glyphs actually displayed. */
21689 ++it->hpos;
21691 /* Record the maximum and minimum buffer positions
21692 seen so far in glyphs that will be displayed by
21693 this row. */
21694 if (it->bidi_p)
21695 RECORD_MAX_MIN_POS (it);
21697 if (x < it->first_visible_x && !row->reversed_p)
21698 /* Glyph is partially visible, i.e. row starts at
21699 negative X position. Don't do that in R2L
21700 rows, where we arrange to add a right offset to
21701 the line in extend_face_to_end_of_line, by a
21702 suitable change to the stretch glyph that is
21703 the leftmost glyph of the line. */
21704 row->x = x - it->first_visible_x;
21705 /* When the last glyph of an R2L row only fits
21706 partially on the line, we need to set row->x to a
21707 negative offset, so that the leftmost glyph is
21708 the one that is partially visible. But if we are
21709 going to produce the truncation glyph, this will
21710 be taken care of in produce_special_glyphs. */
21711 if (row->reversed_p
21712 && new_x > it->last_visible_x
21713 && !(it->line_wrap == TRUNCATE
21714 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21716 eassert (FRAME_WINDOW_P (it->f));
21717 row->x = it->last_visible_x - new_x;
21720 else
21722 /* Glyph is completely off the left margin of the
21723 window. This should not happen because of the
21724 move_it_in_display_line at the start of this
21725 function, unless the text display area of the
21726 window is empty. */
21727 eassert (it->first_visible_x <= it->last_visible_x);
21730 /* Even if this display element produced no glyphs at all,
21731 we want to record its position. */
21732 if (it->bidi_p && nglyphs == 0)
21733 RECORD_MAX_MIN_POS (it);
21735 row->ascent = max (row->ascent, it->max_ascent);
21736 row->height = max (row->height, it->max_ascent + it->max_descent);
21737 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21738 row->phys_height = max (row->phys_height,
21739 it->max_phys_ascent + it->max_phys_descent);
21740 row->extra_line_spacing = max (row->extra_line_spacing,
21741 it->max_extra_line_spacing);
21743 /* End of this display line if row is continued. */
21744 if (row->continued_p || row->ends_at_zv_p)
21745 break;
21748 at_end_of_line:
21749 /* Is this a line end? If yes, we're also done, after making
21750 sure that a non-default face is extended up to the right
21751 margin of the window. */
21752 if (ITERATOR_AT_END_OF_LINE_P (it))
21754 int used_before = row->used[TEXT_AREA];
21756 row->ends_in_newline_from_string_p = STRINGP (it->object);
21758 /* Add a space at the end of the line that is used to
21759 display the cursor there. */
21760 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21761 append_space_for_newline (it, false);
21763 /* Extend the face to the end of the line. */
21764 extend_face_to_end_of_line (it);
21766 /* Make sure we have the position. */
21767 if (used_before == 0)
21768 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21770 /* Record the position of the newline, for use in
21771 find_row_edges. */
21772 it->eol_pos = it->current.pos;
21774 /* Consume the line end. This skips over invisible lines. */
21775 set_iterator_to_next (it, true);
21776 it->continuation_lines_width = 0;
21777 break;
21780 /* Proceed with next display element. Note that this skips
21781 over lines invisible because of selective display. */
21782 set_iterator_to_next (it, true);
21784 /* If we truncate lines, we are done when the last displayed
21785 glyphs reach past the right margin of the window. */
21786 if (it->line_wrap == TRUNCATE
21787 && ((FRAME_WINDOW_P (it->f)
21788 /* Images are preprocessed in produce_image_glyph such
21789 that they are cropped at the right edge of the
21790 window, so an image glyph will always end exactly at
21791 last_visible_x, even if there's no right fringe. */
21792 && ((row->reversed_p
21793 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21794 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21795 || it->what == IT_IMAGE))
21796 ? (it->current_x >= it->last_visible_x)
21797 : (it->current_x > it->last_visible_x)))
21799 /* Maybe add truncation glyphs. */
21800 if (!FRAME_WINDOW_P (it->f)
21801 || (row->reversed_p
21802 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21803 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21805 int i, n;
21807 if (!row->reversed_p)
21809 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21810 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21811 break;
21813 else
21815 for (i = 0; i < row->used[TEXT_AREA]; i++)
21816 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21817 break;
21818 /* Remove any padding glyphs at the front of ROW, to
21819 make room for the truncation glyphs we will be
21820 adding below. The loop below always inserts at
21821 least one truncation glyph, so also remove the
21822 last glyph added to ROW. */
21823 unproduce_glyphs (it, i + 1);
21824 /* Adjust i for the loop below. */
21825 i = row->used[TEXT_AREA] - (i + 1);
21828 /* produce_special_glyphs overwrites the last glyph, so
21829 we don't want that if we want to keep that last
21830 glyph, which means it's an image. */
21831 if (it->current_x > it->last_visible_x)
21833 it->current_x = x_before;
21834 if (!FRAME_WINDOW_P (it->f))
21836 for (n = row->used[TEXT_AREA]; i < n; ++i)
21838 row->used[TEXT_AREA] = i;
21839 produce_special_glyphs (it, IT_TRUNCATION);
21842 else
21844 row->used[TEXT_AREA] = i;
21845 produce_special_glyphs (it, IT_TRUNCATION);
21847 it->hpos = hpos_before;
21850 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21852 /* Don't truncate if we can overflow newline into fringe. */
21853 if (!get_next_display_element (it))
21855 it->continuation_lines_width = 0;
21856 it->font_height = Qnil;
21857 it->voffset = 0;
21858 row->ends_at_zv_p = true;
21859 row->exact_window_width_line_p = true;
21860 break;
21862 if (ITERATOR_AT_END_OF_LINE_P (it))
21864 row->exact_window_width_line_p = true;
21865 goto at_end_of_line;
21867 it->current_x = x_before;
21868 it->hpos = hpos_before;
21871 row->truncated_on_right_p = true;
21872 it->continuation_lines_width = 0;
21873 reseat_at_next_visible_line_start (it, false);
21874 /* We insist below that IT's position be at ZV because in
21875 bidi-reordered lines the character at visible line start
21876 might not be the character that follows the newline in
21877 the logical order. */
21878 if (IT_BYTEPOS (*it) > BEG_BYTE)
21879 row->ends_at_zv_p =
21880 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21881 else
21882 row->ends_at_zv_p = false;
21883 break;
21887 if (wrap_data)
21888 bidi_unshelve_cache (wrap_data, true);
21890 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21891 at the left window margin. */
21892 if (it->first_visible_x
21893 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21895 if (!FRAME_WINDOW_P (it->f)
21896 || (((row->reversed_p
21897 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21898 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21899 /* Don't let insert_left_trunc_glyphs overwrite the
21900 first glyph of the row if it is an image. */
21901 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21902 insert_left_trunc_glyphs (it);
21903 row->truncated_on_left_p = true;
21906 /* Remember the position at which this line ends.
21908 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21909 cannot be before the call to find_row_edges below, since that is
21910 where these positions are determined. */
21911 row->end = it->current;
21912 if (!it->bidi_p)
21914 row->minpos = row->start.pos;
21915 row->maxpos = row->end.pos;
21917 else
21919 /* ROW->minpos and ROW->maxpos must be the smallest and
21920 `1 + the largest' buffer positions in ROW. But if ROW was
21921 bidi-reordered, these two positions can be anywhere in the
21922 row, so we must determine them now. */
21923 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21926 /* If the start of this line is the overlay arrow-position, then
21927 mark this glyph row as the one containing the overlay arrow.
21928 This is clearly a mess with variable size fonts. It would be
21929 better to let it be displayed like cursors under X. */
21930 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21931 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21932 !NILP (overlay_arrow_string)))
21934 /* Overlay arrow in window redisplay is a fringe bitmap. */
21935 if (STRINGP (overlay_arrow_string))
21937 struct glyph_row *arrow_row
21938 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21939 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21940 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21941 struct glyph *p = row->glyphs[TEXT_AREA];
21942 struct glyph *p2, *end;
21944 /* Copy the arrow glyphs. */
21945 while (glyph < arrow_end)
21946 *p++ = *glyph++;
21948 /* Throw away padding glyphs. */
21949 p2 = p;
21950 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21951 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21952 ++p2;
21953 if (p2 > p)
21955 while (p2 < end)
21956 *p++ = *p2++;
21957 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21960 else
21962 eassert (INTEGERP (overlay_arrow_string));
21963 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21965 overlay_arrow_seen = true;
21968 /* Highlight trailing whitespace. */
21969 if (!NILP (Vshow_trailing_whitespace))
21970 highlight_trailing_whitespace (it->f, it->glyph_row);
21972 /* Compute pixel dimensions of this line. */
21973 compute_line_metrics (it);
21975 /* Implementation note: No changes in the glyphs of ROW or in their
21976 faces can be done past this point, because compute_line_metrics
21977 computes ROW's hash value and stores it within the glyph_row
21978 structure. */
21980 /* Record whether this row ends inside an ellipsis. */
21981 row->ends_in_ellipsis_p
21982 = (it->method == GET_FROM_DISPLAY_VECTOR
21983 && it->ellipsis_p);
21985 /* Save fringe bitmaps in this row. */
21986 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
21987 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
21988 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
21989 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
21991 it->left_user_fringe_bitmap = 0;
21992 it->left_user_fringe_face_id = 0;
21993 it->right_user_fringe_bitmap = 0;
21994 it->right_user_fringe_face_id = 0;
21996 /* Maybe set the cursor. */
21997 cvpos = it->w->cursor.vpos;
21998 if ((cvpos < 0
21999 /* In bidi-reordered rows, keep checking for proper cursor
22000 position even if one has been found already, because buffer
22001 positions in such rows change non-linearly with ROW->VPOS,
22002 when a line is continued. One exception: when we are at ZV,
22003 display cursor on the first suitable glyph row, since all
22004 the empty rows after that also have their position set to ZV. */
22005 /* FIXME: Revisit this when glyph ``spilling'' in continuation
22006 lines' rows is implemented for bidi-reordered rows. */
22007 || (it->bidi_p
22008 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
22009 && PT >= MATRIX_ROW_START_CHARPOS (row)
22010 && PT <= MATRIX_ROW_END_CHARPOS (row)
22011 && cursor_row_p (row))
22012 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
22014 /* Prepare for the next line. This line starts horizontally at (X
22015 HPOS) = (0 0). Vertical positions are incremented. As a
22016 convenience for the caller, IT->glyph_row is set to the next
22017 row to be used. */
22018 it->current_x = it->hpos = 0;
22019 it->current_y += row->height;
22020 /* Restore the first and last visible X if we adjusted them for
22021 current-line hscrolling. */
22022 if (hscroll_this_line)
22024 it->first_visible_x = first_visible_x;
22025 it->last_visible_x = last_visible_x;
22027 SET_TEXT_POS (it->eol_pos, 0, 0);
22028 ++it->vpos;
22029 ++it->glyph_row;
22030 /* The next row should by default use the same value of the
22031 reversed_p flag as this one. set_iterator_to_next decides when
22032 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
22033 the flag accordingly. */
22034 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
22035 it->glyph_row->reversed_p = row->reversed_p;
22036 it->start = row->end;
22037 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
22039 #undef RECORD_MAX_MIN_POS
22042 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
22043 Scurrent_bidi_paragraph_direction, 0, 1, 0,
22044 doc: /* Return paragraph direction at point in BUFFER.
22045 Value is either `left-to-right' or `right-to-left'.
22046 If BUFFER is omitted or nil, it defaults to the current buffer.
22048 Paragraph direction determines how the text in the paragraph is displayed.
22049 In left-to-right paragraphs, text begins at the left margin of the window
22050 and the reading direction is generally left to right. In right-to-left
22051 paragraphs, text begins at the right margin and is read from right to left.
22053 See also `bidi-paragraph-direction'. */)
22054 (Lisp_Object buffer)
22056 struct buffer *buf = current_buffer;
22057 struct buffer *old = buf;
22059 if (! NILP (buffer))
22061 CHECK_BUFFER (buffer);
22062 buf = XBUFFER (buffer);
22065 if (NILP (BVAR (buf, bidi_display_reordering))
22066 || NILP (BVAR (buf, enable_multibyte_characters))
22067 /* When we are loading loadup.el, the character property tables
22068 needed for bidi iteration are not yet available. */
22069 || redisplay__inhibit_bidi)
22070 return Qleft_to_right;
22071 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
22072 return BVAR (buf, bidi_paragraph_direction);
22073 else
22075 /* Determine the direction from buffer text. We could try to
22076 use current_matrix if it is up to date, but this seems fast
22077 enough as it is. */
22078 struct bidi_it itb;
22079 ptrdiff_t pos = BUF_PT (buf);
22080 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
22081 int c;
22082 void *itb_data = bidi_shelve_cache ();
22084 set_buffer_temp (buf);
22085 /* bidi_paragraph_init finds the base direction of the paragraph
22086 by searching forward from paragraph start. We need the base
22087 direction of the current or _previous_ paragraph, so we need
22088 to make sure we are within that paragraph. To that end, find
22089 the previous non-empty line. */
22090 if (pos >= ZV && pos > BEGV)
22091 DEC_BOTH (pos, bytepos);
22092 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
22093 if (fast_looking_at (trailing_white_space,
22094 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
22096 while ((c = FETCH_BYTE (bytepos)) == '\n'
22097 || c == ' ' || c == '\t' || c == '\f')
22099 if (bytepos <= BEGV_BYTE)
22100 break;
22101 bytepos--;
22102 pos--;
22104 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
22105 bytepos--;
22107 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
22108 itb.paragraph_dir = NEUTRAL_DIR;
22109 itb.string.s = NULL;
22110 itb.string.lstring = Qnil;
22111 itb.string.bufpos = 0;
22112 itb.string.from_disp_str = false;
22113 itb.string.unibyte = false;
22114 /* We have no window to use here for ignoring window-specific
22115 overlays. Using NULL for window pointer will cause
22116 compute_display_string_pos to use the current buffer. */
22117 itb.w = NULL;
22118 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
22119 bidi_unshelve_cache (itb_data, false);
22120 set_buffer_temp (old);
22121 switch (itb.paragraph_dir)
22123 case L2R:
22124 return Qleft_to_right;
22125 break;
22126 case R2L:
22127 return Qright_to_left;
22128 break;
22129 default:
22130 emacs_abort ();
22135 DEFUN ("bidi-find-overridden-directionality",
22136 Fbidi_find_overridden_directionality,
22137 Sbidi_find_overridden_directionality, 2, 3, 0,
22138 doc: /* Return position between FROM and TO where directionality was overridden.
22140 This function returns the first character position in the specified
22141 region of OBJECT where there is a character whose `bidi-class' property
22142 is `L', but which was forced to display as `R' by a directional
22143 override, and likewise with characters whose `bidi-class' is `R'
22144 or `AL' that were forced to display as `L'.
22146 If no such character is found, the function returns nil.
22148 OBJECT is a Lisp string or buffer to search for overridden
22149 directionality, and defaults to the current buffer if nil or omitted.
22150 OBJECT can also be a window, in which case the function will search
22151 the buffer displayed in that window. Passing the window instead of
22152 a buffer is preferable when the buffer is displayed in some window,
22153 because this function will then be able to correctly account for
22154 window-specific overlays, which can affect the results.
22156 Strong directional characters `L', `R', and `AL' can have their
22157 intrinsic directionality overridden by directional override
22158 control characters RLO (u+202e) and LRO (u+202d). See the
22159 function `get-char-code-property' for a way to inquire about
22160 the `bidi-class' property of a character. */)
22161 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
22163 struct buffer *buf = current_buffer;
22164 struct buffer *old = buf;
22165 struct window *w = NULL;
22166 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
22167 struct bidi_it itb;
22168 ptrdiff_t from_pos, to_pos, from_bpos;
22169 void *itb_data;
22171 if (!NILP (object))
22173 if (BUFFERP (object))
22174 buf = XBUFFER (object);
22175 else if (WINDOWP (object))
22177 w = decode_live_window (object);
22178 buf = XBUFFER (w->contents);
22179 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
22181 else
22182 CHECK_STRING (object);
22185 if (STRINGP (object))
22187 /* Characters in unibyte strings are always treated by bidi.c as
22188 strong LTR. */
22189 if (!STRING_MULTIBYTE (object)
22190 /* When we are loading loadup.el, the character property
22191 tables needed for bidi iteration are not yet
22192 available. */
22193 || redisplay__inhibit_bidi)
22194 return Qnil;
22196 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
22197 if (from_pos >= SCHARS (object))
22198 return Qnil;
22200 /* Set up the bidi iterator. */
22201 itb_data = bidi_shelve_cache ();
22202 itb.paragraph_dir = NEUTRAL_DIR;
22203 itb.string.lstring = object;
22204 itb.string.s = NULL;
22205 itb.string.schars = SCHARS (object);
22206 itb.string.bufpos = 0;
22207 itb.string.from_disp_str = false;
22208 itb.string.unibyte = false;
22209 itb.w = w;
22210 bidi_init_it (0, 0, frame_window_p, &itb);
22212 else
22214 /* Nothing this fancy can happen in unibyte buffers, or in a
22215 buffer that disabled reordering, or if FROM is at EOB. */
22216 if (NILP (BVAR (buf, bidi_display_reordering))
22217 || NILP (BVAR (buf, enable_multibyte_characters))
22218 /* When we are loading loadup.el, the character property
22219 tables needed for bidi iteration are not yet
22220 available. */
22221 || redisplay__inhibit_bidi)
22222 return Qnil;
22224 set_buffer_temp (buf);
22225 validate_region (&from, &to);
22226 from_pos = XINT (from);
22227 to_pos = XINT (to);
22228 if (from_pos >= ZV)
22229 return Qnil;
22231 /* Set up the bidi iterator. */
22232 itb_data = bidi_shelve_cache ();
22233 from_bpos = CHAR_TO_BYTE (from_pos);
22234 if (from_pos == BEGV)
22236 itb.charpos = BEGV;
22237 itb.bytepos = BEGV_BYTE;
22239 else if (FETCH_CHAR (from_bpos - 1) == '\n')
22241 itb.charpos = from_pos;
22242 itb.bytepos = from_bpos;
22244 else
22245 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
22246 -1, &itb.bytepos);
22247 itb.paragraph_dir = NEUTRAL_DIR;
22248 itb.string.s = NULL;
22249 itb.string.lstring = Qnil;
22250 itb.string.bufpos = 0;
22251 itb.string.from_disp_str = false;
22252 itb.string.unibyte = false;
22253 itb.w = w;
22254 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
22257 ptrdiff_t found;
22258 do {
22259 /* For the purposes of this function, the actual base direction of
22260 the paragraph doesn't matter, so just set it to L2R. */
22261 bidi_paragraph_init (L2R, &itb, false);
22262 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
22264 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
22266 bidi_unshelve_cache (itb_data, false);
22267 set_buffer_temp (old);
22269 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
22272 DEFUN ("move-point-visually", Fmove_point_visually,
22273 Smove_point_visually, 1, 1, 0,
22274 doc: /* Move point in the visual order in the specified DIRECTION.
22275 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
22276 left.
22278 Value is the new character position of point. */)
22279 (Lisp_Object direction)
22281 struct window *w = XWINDOW (selected_window);
22282 struct buffer *b = XBUFFER (w->contents);
22283 struct glyph_row *row;
22284 int dir;
22285 Lisp_Object paragraph_dir;
22287 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
22288 (!(ROW)->continued_p \
22289 && NILP ((GLYPH)->object) \
22290 && (GLYPH)->type == CHAR_GLYPH \
22291 && (GLYPH)->u.ch == ' ' \
22292 && (GLYPH)->charpos >= 0 \
22293 && !(GLYPH)->avoid_cursor_p)
22295 CHECK_NUMBER (direction);
22296 dir = XINT (direction);
22297 if (dir > 0)
22298 dir = 1;
22299 else
22300 dir = -1;
22302 /* If current matrix is up-to-date, we can use the information
22303 recorded in the glyphs, at least as long as the goal is on the
22304 screen. */
22305 if (w->window_end_valid
22306 && !windows_or_buffers_changed
22307 && b
22308 && !b->clip_changed
22309 && !b->prevent_redisplay_optimizations_p
22310 && !window_outdated (w)
22311 /* We rely below on the cursor coordinates to be up to date, but
22312 we cannot trust them if some command moved point since the
22313 last complete redisplay. */
22314 && w->last_point == BUF_PT (b)
22315 && w->cursor.vpos >= 0
22316 && w->cursor.vpos < w->current_matrix->nrows
22317 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
22319 struct glyph *g = row->glyphs[TEXT_AREA];
22320 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
22321 struct glyph *gpt = g + w->cursor.hpos;
22323 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
22325 if (BUFFERP (g->object) && g->charpos != PT)
22327 SET_PT (g->charpos);
22328 w->cursor.vpos = -1;
22329 return make_number (PT);
22331 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
22333 ptrdiff_t new_pos;
22335 if (BUFFERP (gpt->object))
22337 new_pos = PT;
22338 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
22339 new_pos += (row->reversed_p ? -dir : dir);
22340 else
22341 new_pos -= (row->reversed_p ? -dir : dir);
22343 else if (BUFFERP (g->object))
22344 new_pos = g->charpos;
22345 else
22346 break;
22347 SET_PT (new_pos);
22348 w->cursor.vpos = -1;
22349 return make_number (PT);
22351 else if (ROW_GLYPH_NEWLINE_P (row, g))
22353 /* Glyphs inserted at the end of a non-empty line for
22354 positioning the cursor have zero charpos, so we must
22355 deduce the value of point by other means. */
22356 if (g->charpos > 0)
22357 SET_PT (g->charpos);
22358 else if (row->ends_at_zv_p && PT != ZV)
22359 SET_PT (ZV);
22360 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
22361 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22362 else
22363 break;
22364 w->cursor.vpos = -1;
22365 return make_number (PT);
22368 if (g == e || NILP (g->object))
22370 if (row->truncated_on_left_p || row->truncated_on_right_p)
22371 goto simulate_display;
22372 if (!row->reversed_p)
22373 row += dir;
22374 else
22375 row -= dir;
22376 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
22377 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
22378 goto simulate_display;
22380 if (dir > 0)
22382 if (row->reversed_p && !row->continued_p)
22384 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22385 w->cursor.vpos = -1;
22386 return make_number (PT);
22388 g = row->glyphs[TEXT_AREA];
22389 e = g + row->used[TEXT_AREA];
22390 for ( ; g < e; g++)
22392 if (BUFFERP (g->object)
22393 /* Empty lines have only one glyph, which stands
22394 for the newline, and whose charpos is the
22395 buffer position of the newline. */
22396 || ROW_GLYPH_NEWLINE_P (row, g)
22397 /* When the buffer ends in a newline, the line at
22398 EOB also has one glyph, but its charpos is -1. */
22399 || (row->ends_at_zv_p
22400 && !row->reversed_p
22401 && NILP (g->object)
22402 && g->type == CHAR_GLYPH
22403 && g->u.ch == ' '))
22405 if (g->charpos > 0)
22406 SET_PT (g->charpos);
22407 else if (!row->reversed_p
22408 && row->ends_at_zv_p
22409 && PT != ZV)
22410 SET_PT (ZV);
22411 else
22412 continue;
22413 w->cursor.vpos = -1;
22414 return make_number (PT);
22418 else
22420 if (!row->reversed_p && !row->continued_p)
22422 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22423 w->cursor.vpos = -1;
22424 return make_number (PT);
22426 e = row->glyphs[TEXT_AREA];
22427 g = e + row->used[TEXT_AREA] - 1;
22428 for ( ; g >= e; g--)
22430 if (BUFFERP (g->object)
22431 || (ROW_GLYPH_NEWLINE_P (row, g)
22432 && g->charpos > 0)
22433 /* Empty R2L lines on GUI frames have the buffer
22434 position of the newline stored in the stretch
22435 glyph. */
22436 || g->type == STRETCH_GLYPH
22437 || (row->ends_at_zv_p
22438 && row->reversed_p
22439 && NILP (g->object)
22440 && g->type == CHAR_GLYPH
22441 && g->u.ch == ' '))
22443 if (g->charpos > 0)
22444 SET_PT (g->charpos);
22445 else if (row->reversed_p
22446 && row->ends_at_zv_p
22447 && PT != ZV)
22448 SET_PT (ZV);
22449 else
22450 continue;
22451 w->cursor.vpos = -1;
22452 return make_number (PT);
22459 simulate_display:
22461 /* If we wind up here, we failed to move by using the glyphs, so we
22462 need to simulate display instead. */
22464 if (b)
22465 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
22466 else
22467 paragraph_dir = Qleft_to_right;
22468 if (EQ (paragraph_dir, Qright_to_left))
22469 dir = -dir;
22470 if (PT <= BEGV && dir < 0)
22471 xsignal0 (Qbeginning_of_buffer);
22472 else if (PT >= ZV && dir > 0)
22473 xsignal0 (Qend_of_buffer);
22474 else
22476 struct text_pos pt;
22477 struct it it;
22478 int pt_x, target_x, pixel_width, pt_vpos;
22479 bool at_eol_p;
22480 bool overshoot_expected = false;
22481 bool target_is_eol_p = false;
22483 /* Setup the arena. */
22484 SET_TEXT_POS (pt, PT, PT_BYTE);
22485 start_display (&it, w, pt);
22486 /* When lines are truncated, we could be called with point
22487 outside of the windows edges, in which case move_it_*
22488 functions either prematurely stop at window's edge or jump to
22489 the next screen line, whereas we rely below on our ability to
22490 reach point, in order to start from its X coordinate. So we
22491 need to disregard the window's horizontal extent in that case. */
22492 if (it.line_wrap == TRUNCATE)
22493 it.last_visible_x = DISP_INFINITY;
22495 if (it.cmp_it.id < 0
22496 && it.method == GET_FROM_STRING
22497 && it.area == TEXT_AREA
22498 && it.string_from_display_prop_p
22499 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
22500 overshoot_expected = true;
22502 /* Find the X coordinate of point. We start from the beginning
22503 of this or previous line to make sure we are before point in
22504 the logical order (since the move_it_* functions can only
22505 move forward). */
22506 reseat:
22507 reseat_at_previous_visible_line_start (&it);
22508 it.current_x = it.hpos = it.current_y = it.vpos = 0;
22509 if (IT_CHARPOS (it) != PT)
22511 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
22512 -1, -1, -1, MOVE_TO_POS);
22513 /* If we missed point because the character there is
22514 displayed out of a display vector that has more than one
22515 glyph, retry expecting overshoot. */
22516 if (it.method == GET_FROM_DISPLAY_VECTOR
22517 && it.current.dpvec_index > 0
22518 && !overshoot_expected)
22520 overshoot_expected = true;
22521 goto reseat;
22523 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
22524 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
22526 pt_x = it.current_x;
22527 pt_vpos = it.vpos;
22528 if (dir > 0 || overshoot_expected)
22530 struct glyph_row *row = it.glyph_row;
22532 /* When point is at beginning of line, we don't have
22533 information about the glyph there loaded into struct
22534 it. Calling get_next_display_element fixes that. */
22535 if (pt_x == 0)
22536 get_next_display_element (&it);
22537 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22538 it.glyph_row = NULL;
22539 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
22540 it.glyph_row = row;
22541 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
22542 it, lest it will become out of sync with it's buffer
22543 position. */
22544 it.current_x = pt_x;
22546 else
22547 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22548 pixel_width = it.pixel_width;
22549 if (overshoot_expected && at_eol_p)
22550 pixel_width = 0;
22551 else if (pixel_width <= 0)
22552 pixel_width = 1;
22554 /* If there's a display string (or something similar) at point,
22555 we are actually at the glyph to the left of point, so we need
22556 to correct the X coordinate. */
22557 if (overshoot_expected)
22559 if (it.bidi_p)
22560 pt_x += pixel_width * it.bidi_it.scan_dir;
22561 else
22562 pt_x += pixel_width;
22565 /* Compute target X coordinate, either to the left or to the
22566 right of point. On TTY frames, all characters have the same
22567 pixel width of 1, so we can use that. On GUI frames we don't
22568 have an easy way of getting at the pixel width of the
22569 character to the left of point, so we use a different method
22570 of getting to that place. */
22571 if (dir > 0)
22572 target_x = pt_x + pixel_width;
22573 else
22574 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
22576 /* Target X coordinate could be one line above or below the line
22577 of point, in which case we need to adjust the target X
22578 coordinate. Also, if moving to the left, we need to begin at
22579 the left edge of the point's screen line. */
22580 if (dir < 0)
22582 if (pt_x > 0)
22584 start_display (&it, w, pt);
22585 if (it.line_wrap == TRUNCATE)
22586 it.last_visible_x = DISP_INFINITY;
22587 reseat_at_previous_visible_line_start (&it);
22588 it.current_x = it.current_y = it.hpos = 0;
22589 if (pt_vpos != 0)
22590 move_it_by_lines (&it, pt_vpos);
22592 else
22594 move_it_by_lines (&it, -1);
22595 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
22596 target_is_eol_p = true;
22597 /* Under word-wrap, we don't know the x coordinate of
22598 the last character displayed on the previous line,
22599 which immediately precedes the wrap point. To find
22600 out its x coordinate, we try moving to the right
22601 margin of the window, which will stop at the wrap
22602 point, and then reset target_x to point at the
22603 character that precedes the wrap point. This is not
22604 needed on GUI frames, because (see below) there we
22605 move from the left margin one grapheme cluster at a
22606 time, and stop when we hit the wrap point. */
22607 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
22609 void *it_data = NULL;
22610 struct it it2;
22612 SAVE_IT (it2, it, it_data);
22613 move_it_in_display_line_to (&it, ZV, target_x,
22614 MOVE_TO_POS | MOVE_TO_X);
22615 /* If we arrived at target_x, that _is_ the last
22616 character on the previous line. */
22617 if (it.current_x != target_x)
22618 target_x = it.current_x - 1;
22619 RESTORE_IT (&it, &it2, it_data);
22623 else
22625 if (at_eol_p
22626 || (target_x >= it.last_visible_x
22627 && it.line_wrap != TRUNCATE))
22629 if (pt_x > 0)
22630 move_it_by_lines (&it, 0);
22631 move_it_by_lines (&it, 1);
22632 target_x = 0;
22636 /* Move to the target X coordinate. */
22637 /* On GUI frames, as we don't know the X coordinate of the
22638 character to the left of point, moving point to the left
22639 requires walking, one grapheme cluster at a time, until we
22640 find ourself at a place immediately to the left of the
22641 character at point. */
22642 if (FRAME_WINDOW_P (it.f) && dir < 0)
22644 struct text_pos new_pos;
22645 enum move_it_result rc = MOVE_X_REACHED;
22647 if (it.current_x == 0)
22648 get_next_display_element (&it);
22649 if (it.what == IT_COMPOSITION)
22651 new_pos.charpos = it.cmp_it.charpos;
22652 new_pos.bytepos = -1;
22654 else
22655 new_pos = it.current.pos;
22657 while (it.current_x + it.pixel_width <= target_x
22658 && (rc == MOVE_X_REACHED
22659 /* Under word-wrap, move_it_in_display_line_to
22660 stops at correct coordinates, but sometimes
22661 returns MOVE_POS_MATCH_OR_ZV. */
22662 || (it.line_wrap == WORD_WRAP
22663 && rc == MOVE_POS_MATCH_OR_ZV)))
22665 int new_x = it.current_x + it.pixel_width;
22667 /* For composed characters, we want the position of the
22668 first character in the grapheme cluster (usually, the
22669 composition's base character), whereas it.current
22670 might give us the position of the _last_ one, e.g. if
22671 the composition is rendered in reverse due to bidi
22672 reordering. */
22673 if (it.what == IT_COMPOSITION)
22675 new_pos.charpos = it.cmp_it.charpos;
22676 new_pos.bytepos = -1;
22678 else
22679 new_pos = it.current.pos;
22680 if (new_x == it.current_x)
22681 new_x++;
22682 rc = move_it_in_display_line_to (&it, ZV, new_x,
22683 MOVE_TO_POS | MOVE_TO_X);
22684 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
22685 break;
22687 /* The previous position we saw in the loop is the one we
22688 want. */
22689 if (new_pos.bytepos == -1)
22690 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
22691 it.current.pos = new_pos;
22693 else if (it.current_x != target_x)
22694 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
22696 /* If we ended up in a display string that covers point, move to
22697 buffer position to the right in the visual order. */
22698 if (dir > 0)
22700 while (IT_CHARPOS (it) == PT)
22702 set_iterator_to_next (&it, false);
22703 if (!get_next_display_element (&it))
22704 break;
22708 /* Move point to that position. */
22709 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22712 return make_number (PT);
22714 #undef ROW_GLYPH_NEWLINE_P
22717 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22718 Sbidi_resolved_levels, 0, 1, 0,
22719 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22721 The resolved levels are produced by the Emacs bidi reordering engine
22722 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22723 read the Unicode Standard Annex 9 (UAX#9) for background information
22724 about these levels.
22726 VPOS is the zero-based number of the current window's screen line
22727 for which to produce the resolved levels. If VPOS is nil or omitted,
22728 it defaults to the screen line of point. If the window displays a
22729 header line, VPOS of zero will report on the header line, and first
22730 line of text in the window will have VPOS of 1.
22732 Value is an array of resolved levels, indexed by glyph number.
22733 Glyphs are numbered from zero starting from the beginning of the
22734 screen line, i.e. the left edge of the window for left-to-right lines
22735 and from the right edge for right-to-left lines. The resolved levels
22736 are produced only for the window's text area; text in display margins
22737 is not included.
22739 If the selected window's display is not up-to-date, or if the specified
22740 screen line does not display text, this function returns nil. It is
22741 highly recommended to bind this function to some simple key, like F8,
22742 in order to avoid these problems.
22744 This function exists mainly for testing the correctness of the
22745 Emacs UBA implementation, in particular with the test suite. */)
22746 (Lisp_Object vpos)
22748 struct window *w = XWINDOW (selected_window);
22749 struct buffer *b = XBUFFER (w->contents);
22750 int nrow;
22751 struct glyph_row *row;
22753 if (NILP (vpos))
22755 int d1, d2, d3, d4, d5;
22757 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22759 else
22761 CHECK_NUMBER_COERCE_MARKER (vpos);
22762 nrow = XINT (vpos);
22765 /* We require up-to-date glyph matrix for this window. */
22766 if (w->window_end_valid
22767 && !windows_or_buffers_changed
22768 && b
22769 && !b->clip_changed
22770 && !b->prevent_redisplay_optimizations_p
22771 && !window_outdated (w)
22772 && nrow >= 0
22773 && nrow < w->current_matrix->nrows
22774 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22775 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22777 struct glyph *g, *e, *g1;
22778 int nglyphs, i;
22779 Lisp_Object levels;
22781 if (!row->reversed_p) /* Left-to-right glyph row. */
22783 g = g1 = row->glyphs[TEXT_AREA];
22784 e = g + row->used[TEXT_AREA];
22786 /* Skip over glyphs at the start of the row that was
22787 generated by redisplay for its own needs. */
22788 while (g < e
22789 && NILP (g->object)
22790 && g->charpos < 0)
22791 g++;
22792 g1 = g;
22794 /* Count the "interesting" glyphs in this row. */
22795 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22796 nglyphs++;
22798 /* Create and fill the array. */
22799 levels = make_uninit_vector (nglyphs);
22800 for (i = 0; g1 < g; i++, g1++)
22801 ASET (levels, i, make_number (g1->resolved_level));
22803 else /* Right-to-left glyph row. */
22805 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22806 e = row->glyphs[TEXT_AREA] - 1;
22807 while (g > e
22808 && NILP (g->object)
22809 && g->charpos < 0)
22810 g--;
22811 g1 = g;
22812 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22813 nglyphs++;
22814 levels = make_uninit_vector (nglyphs);
22815 for (i = 0; g1 > g; i++, g1--)
22816 ASET (levels, i, make_number (g1->resolved_level));
22818 return levels;
22820 else
22821 return Qnil;
22826 /***********************************************************************
22827 Menu Bar
22828 ***********************************************************************/
22830 /* Redisplay the menu bar in the frame for window W.
22832 The menu bar of X frames that don't have X toolkit support is
22833 displayed in a special window W->frame->menu_bar_window.
22835 The menu bar of terminal frames is treated specially as far as
22836 glyph matrices are concerned. Menu bar lines are not part of
22837 windows, so the update is done directly on the frame matrix rows
22838 for the menu bar. */
22840 static void
22841 display_menu_bar (struct window *w)
22843 struct frame *f = XFRAME (WINDOW_FRAME (w));
22844 struct it it;
22845 Lisp_Object items;
22846 int i;
22848 /* Don't do all this for graphical frames. */
22849 #ifdef HAVE_NTGUI
22850 if (FRAME_W32_P (f))
22851 return;
22852 #endif
22853 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22854 if (FRAME_X_P (f))
22855 return;
22856 #endif
22858 #ifdef HAVE_NS
22859 if (FRAME_NS_P (f))
22860 return;
22861 #endif /* HAVE_NS */
22863 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22864 eassert (!FRAME_WINDOW_P (f));
22865 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22866 it.first_visible_x = 0;
22867 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22868 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22869 if (FRAME_WINDOW_P (f))
22871 /* Menu bar lines are displayed in the desired matrix of the
22872 dummy window menu_bar_window. */
22873 struct window *menu_w;
22874 menu_w = XWINDOW (f->menu_bar_window);
22875 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22876 MENU_FACE_ID);
22877 it.first_visible_x = 0;
22878 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22880 else
22881 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22883 /* This is a TTY frame, i.e. character hpos/vpos are used as
22884 pixel x/y. */
22885 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22886 MENU_FACE_ID);
22887 it.first_visible_x = 0;
22888 it.last_visible_x = FRAME_COLS (f);
22891 /* FIXME: This should be controlled by a user option. See the
22892 comments in redisplay_tool_bar and display_mode_line about
22893 this. */
22894 it.paragraph_embedding = L2R;
22896 /* Clear all rows of the menu bar. */
22897 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22899 struct glyph_row *row = it.glyph_row + i;
22900 clear_glyph_row (row);
22901 row->enabled_p = true;
22902 row->full_width_p = true;
22903 row->reversed_p = false;
22906 /* Display all items of the menu bar. */
22907 items = FRAME_MENU_BAR_ITEMS (it.f);
22908 for (i = 0; i < ASIZE (items); i += 4)
22910 Lisp_Object string;
22912 /* Stop at nil string. */
22913 string = AREF (items, i + 1);
22914 if (NILP (string))
22915 break;
22917 /* Remember where item was displayed. */
22918 ASET (items, i + 3, make_number (it.hpos));
22920 /* Display the item, pad with one space. */
22921 if (it.current_x < it.last_visible_x)
22922 display_string (NULL, string, Qnil, 0, 0, &it,
22923 SCHARS (string) + 1, 0, 0, -1);
22926 /* Fill out the line with spaces. */
22927 if (it.current_x < it.last_visible_x)
22928 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22930 /* Compute the total height of the lines. */
22931 compute_line_metrics (&it);
22934 /* Deep copy of a glyph row, including the glyphs. */
22935 static void
22936 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22938 struct glyph *pointers[1 + LAST_AREA];
22939 int to_used = to->used[TEXT_AREA];
22941 /* Save glyph pointers of TO. */
22942 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22944 /* Do a structure assignment. */
22945 *to = *from;
22947 /* Restore original glyph pointers of TO. */
22948 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22950 /* Copy the glyphs. */
22951 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22952 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22954 /* If we filled only part of the TO row, fill the rest with
22955 space_glyph (which will display as empty space). */
22956 if (to_used > from->used[TEXT_AREA])
22957 fill_up_frame_row_with_spaces (to, to_used);
22960 /* Display one menu item on a TTY, by overwriting the glyphs in the
22961 frame F's desired glyph matrix with glyphs produced from the menu
22962 item text. Called from term.c to display TTY drop-down menus one
22963 item at a time.
22965 ITEM_TEXT is the menu item text as a C string.
22967 FACE_ID is the face ID to be used for this menu item. FACE_ID
22968 could specify one of 3 faces: a face for an enabled item, a face
22969 for a disabled item, or a face for a selected item.
22971 X and Y are coordinates of the first glyph in the frame's desired
22972 matrix to be overwritten by the menu item. Since this is a TTY, Y
22973 is the zero-based number of the glyph row and X is the zero-based
22974 glyph number in the row, starting from left, where to start
22975 displaying the item.
22977 SUBMENU means this menu item drops down a submenu, which
22978 should be indicated by displaying a proper visual cue after the
22979 item text. */
22981 void
22982 display_tty_menu_item (const char *item_text, int width, int face_id,
22983 int x, int y, bool submenu)
22985 struct it it;
22986 struct frame *f = SELECTED_FRAME ();
22987 struct window *w = XWINDOW (f->selected_window);
22988 struct glyph_row *row;
22989 size_t item_len = strlen (item_text);
22991 eassert (FRAME_TERMCAP_P (f));
22993 /* Don't write beyond the matrix's last row. This can happen for
22994 TTY screens that are not high enough to show the entire menu.
22995 (This is actually a bit of defensive programming, as
22996 tty_menu_display already limits the number of menu items to one
22997 less than the number of screen lines.) */
22998 if (y >= f->desired_matrix->nrows)
22999 return;
23001 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
23002 it.first_visible_x = 0;
23003 it.last_visible_x = FRAME_COLS (f) - 1;
23004 row = it.glyph_row;
23005 /* Start with the row contents from the current matrix. */
23006 deep_copy_glyph_row (row, f->current_matrix->rows + y);
23007 bool saved_width = row->full_width_p;
23008 row->full_width_p = true;
23009 bool saved_reversed = row->reversed_p;
23010 row->reversed_p = false;
23011 row->enabled_p = true;
23013 /* Arrange for the menu item glyphs to start at (X,Y) and have the
23014 desired face. */
23015 eassert (x < f->desired_matrix->matrix_w);
23016 it.current_x = it.hpos = x;
23017 it.current_y = it.vpos = y;
23018 int saved_used = row->used[TEXT_AREA];
23019 bool saved_truncated = row->truncated_on_right_p;
23020 row->used[TEXT_AREA] = x;
23021 it.face_id = face_id;
23022 it.line_wrap = TRUNCATE;
23024 /* FIXME: This should be controlled by a user option. See the
23025 comments in redisplay_tool_bar and display_mode_line about this.
23026 Also, if paragraph_embedding could ever be R2L, changes will be
23027 needed to avoid shifting to the right the row characters in
23028 term.c:append_glyph. */
23029 it.paragraph_embedding = L2R;
23031 /* Pad with a space on the left. */
23032 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
23033 width--;
23034 /* Display the menu item, pad with spaces to WIDTH. */
23035 if (submenu)
23037 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23038 item_len, 0, FRAME_COLS (f) - 1, -1);
23039 width -= item_len;
23040 /* Indicate with " >" that there's a submenu. */
23041 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
23042 FRAME_COLS (f) - 1, -1);
23044 else
23045 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23046 width, 0, FRAME_COLS (f) - 1, -1);
23048 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
23049 row->truncated_on_right_p = saved_truncated;
23050 row->hash = row_hash (row);
23051 row->full_width_p = saved_width;
23052 row->reversed_p = saved_reversed;
23055 /***********************************************************************
23056 Mode Line
23057 ***********************************************************************/
23059 /* Redisplay mode lines in the window tree whose root is WINDOW.
23060 If FORCE, redisplay mode lines unconditionally.
23061 Otherwise, redisplay only mode lines that are garbaged. Value is
23062 the number of windows whose mode lines were redisplayed. */
23064 static int
23065 redisplay_mode_lines (Lisp_Object window, bool force)
23067 int nwindows = 0;
23069 while (!NILP (window))
23071 struct window *w = XWINDOW (window);
23073 if (WINDOWP (w->contents))
23074 nwindows += redisplay_mode_lines (w->contents, force);
23075 else if (force
23076 || FRAME_GARBAGED_P (XFRAME (w->frame))
23077 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
23079 struct text_pos lpoint;
23080 struct buffer *old = current_buffer;
23082 /* Set the window's buffer for the mode line display. */
23083 SET_TEXT_POS (lpoint, PT, PT_BYTE);
23084 set_buffer_internal_1 (XBUFFER (w->contents));
23086 /* Point refers normally to the selected window. For any
23087 other window, set up appropriate value. */
23088 if (!EQ (window, selected_window))
23090 struct text_pos pt;
23092 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
23093 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
23096 /* Display mode lines. */
23097 clear_glyph_matrix (w->desired_matrix);
23098 if (display_mode_lines (w))
23099 ++nwindows;
23101 /* Restore old settings. */
23102 set_buffer_internal_1 (old);
23103 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
23106 window = w->next;
23109 return nwindows;
23113 /* Display the mode and/or header line of window W. Value is the
23114 sum number of mode lines and header lines displayed. */
23116 static int
23117 display_mode_lines (struct window *w)
23119 Lisp_Object old_selected_window = selected_window;
23120 Lisp_Object old_selected_frame = selected_frame;
23121 Lisp_Object new_frame = w->frame;
23122 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
23123 int n = 0;
23125 selected_frame = new_frame;
23126 /* FIXME: If we were to allow the mode-line's computation changing the buffer
23127 or window's point, then we'd need select_window_1 here as well. */
23128 XSETWINDOW (selected_window, w);
23129 XFRAME (new_frame)->selected_window = selected_window;
23131 /* These will be set while the mode line specs are processed. */
23132 line_number_displayed = false;
23133 w->column_number_displayed = -1;
23135 if (window_wants_mode_line (w))
23137 Lisp_Object window_mode_line_format
23138 = window_parameter (w, Qmode_line_format);
23140 struct window *sel_w = XWINDOW (old_selected_window);
23142 /* Select mode line face based on the real selected window. */
23143 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
23144 NILP (window_mode_line_format)
23145 ? BVAR (current_buffer, mode_line_format)
23146 : window_mode_line_format);
23147 ++n;
23150 if (window_wants_header_line (w))
23152 Lisp_Object window_header_line_format
23153 = window_parameter (w, Qheader_line_format);
23155 display_mode_line (w, HEADER_LINE_FACE_ID,
23156 NILP (window_header_line_format)
23157 ? BVAR (current_buffer, header_line_format)
23158 : window_header_line_format);
23159 ++n;
23162 XFRAME (new_frame)->selected_window = old_frame_selected_window;
23163 selected_frame = old_selected_frame;
23164 selected_window = old_selected_window;
23165 if (n > 0)
23166 w->must_be_updated_p = true;
23167 return n;
23171 /* Display mode or header line of window W. FACE_ID specifies which
23172 line to display; it is either MODE_LINE_FACE_ID or
23173 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
23174 display. Value is the pixel height of the mode/header line
23175 displayed. */
23177 static int
23178 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
23180 struct it it;
23181 struct face *face;
23182 ptrdiff_t count = SPECPDL_INDEX ();
23184 init_iterator (&it, w, -1, -1, NULL, face_id);
23185 /* Don't extend on a previously drawn mode-line.
23186 This may happen if called from pos_visible_p. */
23187 it.glyph_row->enabled_p = false;
23188 prepare_desired_row (w, it.glyph_row, true);
23190 it.glyph_row->mode_line_p = true;
23192 /* FIXME: This should be controlled by a user option. But
23193 supporting such an option is not trivial, since the mode line is
23194 made up of many separate strings. */
23195 it.paragraph_embedding = L2R;
23197 record_unwind_protect (unwind_format_mode_line,
23198 format_mode_line_unwind_data (NULL, NULL,
23199 Qnil, false));
23201 mode_line_target = MODE_LINE_DISPLAY;
23203 /* Temporarily make frame's keyboard the current kboard so that
23204 kboard-local variables in the mode_line_format will get the right
23205 values. */
23206 push_kboard (FRAME_KBOARD (it.f));
23207 record_unwind_save_match_data ();
23208 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23209 pop_kboard ();
23211 unbind_to (count, Qnil);
23213 /* Fill up with spaces. */
23214 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
23216 compute_line_metrics (&it);
23217 it.glyph_row->full_width_p = true;
23218 it.glyph_row->continued_p = false;
23219 it.glyph_row->truncated_on_left_p = false;
23220 it.glyph_row->truncated_on_right_p = false;
23222 /* Make a 3D mode-line have a shadow at its right end. */
23223 face = FACE_FROM_ID (it.f, face_id);
23224 extend_face_to_end_of_line (&it);
23225 if (face->box != FACE_NO_BOX)
23227 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
23228 + it.glyph_row->used[TEXT_AREA] - 1);
23229 last->right_box_line_p = true;
23232 return it.glyph_row->height;
23235 /* Move element ELT in LIST to the front of LIST.
23236 Return the updated list. */
23238 static Lisp_Object
23239 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
23241 register Lisp_Object tail, prev;
23242 register Lisp_Object tem;
23244 tail = list;
23245 prev = Qnil;
23246 while (CONSP (tail))
23248 tem = XCAR (tail);
23250 if (EQ (elt, tem))
23252 /* Splice out the link TAIL. */
23253 if (NILP (prev))
23254 list = XCDR (tail);
23255 else
23256 Fsetcdr (prev, XCDR (tail));
23258 /* Now make it the first. */
23259 Fsetcdr (tail, list);
23260 return tail;
23262 else
23263 prev = tail;
23264 tail = XCDR (tail);
23265 maybe_quit ();
23268 /* Not found--return unchanged LIST. */
23269 return list;
23272 /* Contribute ELT to the mode line for window IT->w. How it
23273 translates into text depends on its data type.
23275 IT describes the display environment in which we display, as usual.
23277 DEPTH is the depth in recursion. It is used to prevent
23278 infinite recursion here.
23280 FIELD_WIDTH is the number of characters the display of ELT should
23281 occupy in the mode line, and PRECISION is the maximum number of
23282 characters to display from ELT's representation. See
23283 display_string for details.
23285 Returns the hpos of the end of the text generated by ELT.
23287 PROPS is a property list to add to any string we encounter.
23289 If RISKY, remove (disregard) any properties in any string
23290 we encounter, and ignore :eval and :propertize.
23292 The global variable `mode_line_target' determines whether the
23293 output is passed to `store_mode_line_noprop',
23294 `store_mode_line_string', or `display_string'. */
23296 static int
23297 display_mode_element (struct it *it, int depth, int field_width, int precision,
23298 Lisp_Object elt, Lisp_Object props, bool risky)
23300 int n = 0, field, prec;
23301 bool literal = false;
23303 tail_recurse:
23304 if (depth > 100)
23305 elt = build_string ("*too-deep*");
23307 depth++;
23309 switch (XTYPE (elt))
23311 case Lisp_String:
23313 /* A string: output it and check for %-constructs within it. */
23314 unsigned char c;
23315 ptrdiff_t offset = 0;
23317 if (SCHARS (elt) > 0
23318 && (!NILP (props) || risky))
23320 Lisp_Object oprops, aelt;
23321 oprops = Ftext_properties_at (make_number (0), elt);
23323 /* If the starting string's properties are not what
23324 we want, translate the string. Also, if the string
23325 is risky, do that anyway. */
23327 if (NILP (Fequal (props, oprops)) || risky)
23329 /* If the starting string has properties,
23330 merge the specified ones onto the existing ones. */
23331 if (! NILP (oprops) && !risky)
23333 Lisp_Object tem;
23335 oprops = Fcopy_sequence (oprops);
23336 tem = props;
23337 while (CONSP (tem))
23339 oprops = Fplist_put (oprops, XCAR (tem),
23340 XCAR (XCDR (tem)));
23341 tem = XCDR (XCDR (tem));
23343 props = oprops;
23346 aelt = Fassoc (elt, mode_line_proptrans_alist, Qnil);
23347 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
23349 /* AELT is what we want. Move it to the front
23350 without consing. */
23351 elt = XCAR (aelt);
23352 mode_line_proptrans_alist
23353 = move_elt_to_front (aelt, mode_line_proptrans_alist);
23355 else
23357 Lisp_Object tem;
23359 /* If AELT has the wrong props, it is useless.
23360 so get rid of it. */
23361 if (! NILP (aelt))
23362 mode_line_proptrans_alist
23363 = Fdelq (aelt, mode_line_proptrans_alist);
23365 elt = Fcopy_sequence (elt);
23366 Fset_text_properties (make_number (0), Flength (elt),
23367 props, elt);
23368 /* Add this item to mode_line_proptrans_alist. */
23369 mode_line_proptrans_alist
23370 = Fcons (Fcons (elt, props),
23371 mode_line_proptrans_alist);
23372 /* Truncate mode_line_proptrans_alist
23373 to at most 50 elements. */
23374 tem = Fnthcdr (make_number (50),
23375 mode_line_proptrans_alist);
23376 if (! NILP (tem))
23377 XSETCDR (tem, Qnil);
23382 offset = 0;
23384 if (literal)
23386 prec = precision - n;
23387 switch (mode_line_target)
23389 case MODE_LINE_NOPROP:
23390 case MODE_LINE_TITLE:
23391 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
23392 break;
23393 case MODE_LINE_STRING:
23394 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
23395 break;
23396 case MODE_LINE_DISPLAY:
23397 n += display_string (NULL, elt, Qnil, 0, 0, it,
23398 0, prec, 0, STRING_MULTIBYTE (elt));
23399 break;
23402 break;
23405 /* Handle the non-literal case. */
23407 while ((precision <= 0 || n < precision)
23408 && SREF (elt, offset) != 0
23409 && (mode_line_target != MODE_LINE_DISPLAY
23410 || it->current_x < it->last_visible_x))
23412 ptrdiff_t last_offset = offset;
23414 /* Advance to end of string or next format specifier. */
23415 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
23418 if (offset - 1 != last_offset)
23420 ptrdiff_t nchars, nbytes;
23422 /* Output to end of string or up to '%'. Field width
23423 is length of string. Don't output more than
23424 PRECISION allows us. */
23425 offset--;
23427 prec = c_string_width (SDATA (elt) + last_offset,
23428 offset - last_offset, precision - n,
23429 &nchars, &nbytes);
23431 switch (mode_line_target)
23433 case MODE_LINE_NOPROP:
23434 case MODE_LINE_TITLE:
23435 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
23436 break;
23437 case MODE_LINE_STRING:
23439 ptrdiff_t bytepos = last_offset;
23440 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23441 ptrdiff_t endpos = (precision <= 0
23442 ? string_byte_to_char (elt, offset)
23443 : charpos + nchars);
23444 Lisp_Object mode_string
23445 = Fsubstring (elt, make_number (charpos),
23446 make_number (endpos));
23447 n += store_mode_line_string (NULL, mode_string, false,
23448 0, 0, Qnil);
23450 break;
23451 case MODE_LINE_DISPLAY:
23453 ptrdiff_t bytepos = last_offset;
23454 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23456 if (precision <= 0)
23457 nchars = string_byte_to_char (elt, offset) - charpos;
23458 n += display_string (NULL, elt, Qnil, 0, charpos,
23459 it, 0, nchars, 0,
23460 STRING_MULTIBYTE (elt));
23462 break;
23465 else /* c == '%' */
23467 ptrdiff_t percent_position = offset;
23469 /* Get the specified minimum width. Zero means
23470 don't pad. */
23471 field = 0;
23472 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
23473 field = field * 10 + c - '0';
23475 /* Don't pad beyond the total padding allowed. */
23476 if (field_width - n > 0 && field > field_width - n)
23477 field = field_width - n;
23479 /* Note that either PRECISION <= 0 or N < PRECISION. */
23480 prec = precision - n;
23482 if (c == 'M')
23483 n += display_mode_element (it, depth, field, prec,
23484 Vglobal_mode_string, props,
23485 risky);
23486 else if (c != 0)
23488 bool multibyte;
23489 ptrdiff_t bytepos, charpos;
23490 const char *spec;
23491 Lisp_Object string;
23493 bytepos = percent_position;
23494 charpos = (STRING_MULTIBYTE (elt)
23495 ? string_byte_to_char (elt, bytepos)
23496 : bytepos);
23497 spec = decode_mode_spec (it->w, c, field, &string);
23498 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
23500 switch (mode_line_target)
23502 case MODE_LINE_NOPROP:
23503 case MODE_LINE_TITLE:
23504 n += store_mode_line_noprop (spec, field, prec);
23505 break;
23506 case MODE_LINE_STRING:
23508 Lisp_Object tem = build_string (spec);
23509 props = Ftext_properties_at (make_number (charpos), elt);
23510 /* Should only keep face property in props */
23511 n += store_mode_line_string (NULL, tem, false,
23512 field, prec, props);
23514 break;
23515 case MODE_LINE_DISPLAY:
23517 int nglyphs_before, nwritten;
23519 nglyphs_before = it->glyph_row->used[TEXT_AREA];
23520 nwritten = display_string (spec, string, elt,
23521 charpos, 0, it,
23522 field, prec, 0,
23523 multibyte);
23525 /* Assign to the glyphs written above the
23526 string where the `%x' came from, position
23527 of the `%'. */
23528 if (nwritten > 0)
23530 struct glyph *glyph
23531 = (it->glyph_row->glyphs[TEXT_AREA]
23532 + nglyphs_before);
23533 int i;
23535 for (i = 0; i < nwritten; ++i)
23537 glyph[i].object = elt;
23538 glyph[i].charpos = charpos;
23541 n += nwritten;
23544 break;
23547 else /* c == 0 */
23548 break;
23552 break;
23554 case Lisp_Symbol:
23555 /* A symbol: process the value of the symbol recursively
23556 as if it appeared here directly. Avoid error if symbol void.
23557 Special case: if value of symbol is a string, output the string
23558 literally. */
23560 register Lisp_Object tem;
23562 /* If the variable is not marked as risky to set
23563 then its contents are risky to use. */
23564 if (NILP (Fget (elt, Qrisky_local_variable)))
23565 risky = true;
23567 tem = Fboundp (elt);
23568 if (!NILP (tem))
23570 tem = Fsymbol_value (elt);
23571 /* If value is a string, output that string literally:
23572 don't check for % within it. */
23573 if (STRINGP (tem))
23574 literal = true;
23576 if (!EQ (tem, elt))
23578 /* Give up right away for nil or t. */
23579 elt = tem;
23580 goto tail_recurse;
23584 break;
23586 case Lisp_Cons:
23588 register Lisp_Object car, tem;
23590 /* A cons cell: five distinct cases.
23591 If first element is :eval or :propertize, do something special.
23592 If first element is a string or a cons, process all the elements
23593 and effectively concatenate them.
23594 If first element is a negative number, truncate displaying cdr to
23595 at most that many characters. If positive, pad (with spaces)
23596 to at least that many characters.
23597 If first element is a symbol, process the cadr or caddr recursively
23598 according to whether the symbol's value is non-nil or nil. */
23599 car = XCAR (elt);
23600 if (EQ (car, QCeval))
23602 /* An element of the form (:eval FORM) means evaluate FORM
23603 and use the result as mode line elements. */
23605 if (risky)
23606 break;
23608 if (CONSP (XCDR (elt)))
23610 Lisp_Object spec;
23611 spec = safe__eval (true, XCAR (XCDR (elt)));
23612 n += display_mode_element (it, depth, field_width - n,
23613 precision - n, spec, props,
23614 risky);
23617 else if (EQ (car, QCpropertize))
23619 /* An element of the form (:propertize ELT PROPS...)
23620 means display ELT but applying properties PROPS. */
23622 if (risky)
23623 break;
23625 if (CONSP (XCDR (elt)))
23626 n += display_mode_element (it, depth, field_width - n,
23627 precision - n, XCAR (XCDR (elt)),
23628 XCDR (XCDR (elt)), risky);
23630 else if (SYMBOLP (car))
23632 tem = Fboundp (car);
23633 elt = XCDR (elt);
23634 if (!CONSP (elt))
23635 goto invalid;
23636 /* elt is now the cdr, and we know it is a cons cell.
23637 Use its car if CAR has a non-nil value. */
23638 if (!NILP (tem))
23640 tem = Fsymbol_value (car);
23641 if (!NILP (tem))
23643 elt = XCAR (elt);
23644 goto tail_recurse;
23647 /* Symbol's value is nil (or symbol is unbound)
23648 Get the cddr of the original list
23649 and if possible find the caddr and use that. */
23650 elt = XCDR (elt);
23651 if (NILP (elt))
23652 break;
23653 else if (!CONSP (elt))
23654 goto invalid;
23655 elt = XCAR (elt);
23656 goto tail_recurse;
23658 else if (INTEGERP (car))
23660 register int lim = XINT (car);
23661 elt = XCDR (elt);
23662 if (lim < 0)
23664 /* Negative int means reduce maximum width. */
23665 if (precision <= 0)
23666 precision = -lim;
23667 else
23668 precision = min (precision, -lim);
23670 else if (lim > 0)
23672 /* Padding specified. Don't let it be more than
23673 current maximum. */
23674 if (precision > 0)
23675 lim = min (precision, lim);
23677 /* If that's more padding than already wanted, queue it.
23678 But don't reduce padding already specified even if
23679 that is beyond the current truncation point. */
23680 field_width = max (lim, field_width);
23682 goto tail_recurse;
23684 else if (STRINGP (car) || CONSP (car))
23685 FOR_EACH_TAIL_SAFE (elt)
23687 if (0 < precision && precision <= n)
23688 break;
23689 n += display_mode_element (it, depth,
23690 /* Pad after only the last
23691 list element. */
23692 (! CONSP (XCDR (elt))
23693 ? field_width - n
23694 : 0),
23695 precision - n, XCAR (elt),
23696 props, risky);
23699 break;
23701 default:
23702 invalid:
23703 elt = build_string ("*invalid*");
23704 goto tail_recurse;
23707 /* Pad to FIELD_WIDTH. */
23708 if (field_width > 0 && n < field_width)
23710 switch (mode_line_target)
23712 case MODE_LINE_NOPROP:
23713 case MODE_LINE_TITLE:
23714 n += store_mode_line_noprop ("", field_width - n, 0);
23715 break;
23716 case MODE_LINE_STRING:
23717 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23718 Qnil);
23719 break;
23720 case MODE_LINE_DISPLAY:
23721 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23722 0, 0, 0);
23723 break;
23727 return n;
23730 /* Store a mode-line string element in mode_line_string_list.
23732 If STRING is non-null, display that C string. Otherwise, the Lisp
23733 string LISP_STRING is displayed.
23735 FIELD_WIDTH is the minimum number of output glyphs to produce.
23736 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23737 with spaces. FIELD_WIDTH <= 0 means don't pad.
23739 PRECISION is the maximum number of characters to output from
23740 STRING. PRECISION <= 0 means don't truncate the string.
23742 If COPY_STRING, make a copy of LISP_STRING before adding
23743 properties to the string.
23745 PROPS are the properties to add to the string.
23746 The mode_line_string_face face property is always added to the string.
23749 static int
23750 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23751 bool copy_string,
23752 int field_width, int precision, Lisp_Object props)
23754 ptrdiff_t len;
23755 int n = 0;
23757 if (string != NULL)
23759 len = strlen (string);
23760 if (precision > 0 && len > precision)
23761 len = precision;
23762 lisp_string = make_string (string, len);
23763 if (NILP (props))
23764 props = mode_line_string_face_prop;
23765 else if (!NILP (mode_line_string_face))
23767 Lisp_Object face = Fplist_get (props, Qface);
23768 props = Fcopy_sequence (props);
23769 if (NILP (face))
23770 face = mode_line_string_face;
23771 else
23772 face = list2 (face, mode_line_string_face);
23773 props = Fplist_put (props, Qface, face);
23775 Fadd_text_properties (make_number (0), make_number (len),
23776 props, lisp_string);
23778 else
23780 len = XFASTINT (Flength (lisp_string));
23781 if (precision > 0 && len > precision)
23783 len = precision;
23784 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23785 precision = -1;
23787 if (!NILP (mode_line_string_face))
23789 Lisp_Object face;
23790 if (NILP (props))
23791 props = Ftext_properties_at (make_number (0), lisp_string);
23792 face = Fplist_get (props, Qface);
23793 if (NILP (face))
23794 face = mode_line_string_face;
23795 else
23796 face = list2 (face, mode_line_string_face);
23797 props = list2 (Qface, face);
23798 if (copy_string)
23799 lisp_string = Fcopy_sequence (lisp_string);
23801 if (!NILP (props))
23802 Fadd_text_properties (make_number (0), make_number (len),
23803 props, lisp_string);
23806 if (len > 0)
23808 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23809 n += len;
23812 if (field_width > len)
23814 field_width -= len;
23815 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
23816 if (!NILP (props))
23817 Fadd_text_properties (make_number (0), make_number (field_width),
23818 props, lisp_string);
23819 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23820 n += field_width;
23823 return n;
23827 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23828 1, 4, 0,
23829 doc: /* Format a string out of a mode line format specification.
23830 First arg FORMAT specifies the mode line format (see `mode-line-format'
23831 for details) to use.
23833 By default, the format is evaluated for the currently selected window.
23835 Optional second arg FACE specifies the face property to put on all
23836 characters for which no face is specified. The value nil means the
23837 default face. The value t means whatever face the window's mode line
23838 currently uses (either `mode-line' or `mode-line-inactive',
23839 depending on whether the window is the selected window or not).
23840 An integer value means the value string has no text
23841 properties.
23843 Optional third and fourth args WINDOW and BUFFER specify the window
23844 and buffer to use as the context for the formatting (defaults
23845 are the selected window and the WINDOW's buffer). */)
23846 (Lisp_Object format, Lisp_Object face,
23847 Lisp_Object window, Lisp_Object buffer)
23849 struct it it;
23850 int len;
23851 struct window *w;
23852 struct buffer *old_buffer = NULL;
23853 int face_id;
23854 bool no_props = INTEGERP (face);
23855 ptrdiff_t count = SPECPDL_INDEX ();
23856 Lisp_Object str;
23857 int string_start = 0;
23859 w = decode_any_window (window);
23860 XSETWINDOW (window, w);
23862 if (NILP (buffer))
23863 buffer = w->contents;
23864 CHECK_BUFFER (buffer);
23866 /* Make formatting the modeline a non-op when noninteractive, otherwise
23867 there will be problems later caused by a partially initialized frame. */
23868 if (NILP (format) || noninteractive)
23869 return empty_unibyte_string;
23871 if (no_props)
23872 face = Qnil;
23874 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23875 : EQ (face, Qt) ? (EQ (window, selected_window)
23876 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23877 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23878 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23879 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23880 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23881 : DEFAULT_FACE_ID;
23883 old_buffer = current_buffer;
23885 /* Save things including mode_line_proptrans_alist,
23886 and set that to nil so that we don't alter the outer value. */
23887 record_unwind_protect (unwind_format_mode_line,
23888 format_mode_line_unwind_data
23889 (XFRAME (WINDOW_FRAME (w)),
23890 old_buffer, selected_window, true));
23891 mode_line_proptrans_alist = Qnil;
23893 Fselect_window (window, Qt);
23894 set_buffer_internal_1 (XBUFFER (buffer));
23896 init_iterator (&it, w, -1, -1, NULL, face_id);
23898 if (no_props)
23900 mode_line_target = MODE_LINE_NOPROP;
23901 mode_line_string_face_prop = Qnil;
23902 mode_line_string_list = Qnil;
23903 string_start = MODE_LINE_NOPROP_LEN (0);
23905 else
23907 mode_line_target = MODE_LINE_STRING;
23908 mode_line_string_list = Qnil;
23909 mode_line_string_face = face;
23910 mode_line_string_face_prop
23911 = NILP (face) ? Qnil : list2 (Qface, face);
23914 push_kboard (FRAME_KBOARD (it.f));
23915 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23916 pop_kboard ();
23918 if (no_props)
23920 len = MODE_LINE_NOPROP_LEN (string_start);
23921 str = make_string (mode_line_noprop_buf + string_start, len);
23923 else
23925 mode_line_string_list = Fnreverse (mode_line_string_list);
23926 str = Fmapconcat (Qidentity, mode_line_string_list,
23927 empty_unibyte_string);
23930 unbind_to (count, Qnil);
23931 return str;
23934 /* Write a null-terminated, right justified decimal representation of
23935 the positive integer D to BUF using a minimal field width WIDTH. */
23937 static void
23938 pint2str (register char *buf, register int width, register ptrdiff_t d)
23940 register char *p = buf;
23942 if (d <= 0)
23943 *p++ = '0';
23944 else
23946 while (d > 0)
23948 *p++ = d % 10 + '0';
23949 d /= 10;
23953 for (width -= (int) (p - buf); width > 0; --width)
23954 *p++ = ' ';
23955 *p-- = '\0';
23956 while (p > buf)
23958 d = *buf;
23959 *buf++ = *p;
23960 *p-- = d;
23964 /* Write a null-terminated, right justified decimal and "human
23965 readable" representation of the nonnegative integer D to BUF using
23966 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23968 static const char power_letter[] =
23970 0, /* no letter */
23971 'k', /* kilo */
23972 'M', /* mega */
23973 'G', /* giga */
23974 'T', /* tera */
23975 'P', /* peta */
23976 'E', /* exa */
23977 'Z', /* zetta */
23978 'Y' /* yotta */
23981 static void
23982 pint2hrstr (char *buf, int width, ptrdiff_t d)
23984 /* We aim to represent the nonnegative integer D as
23985 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
23986 ptrdiff_t quotient = d;
23987 int remainder = 0;
23988 /* -1 means: do not use TENTHS. */
23989 int tenths = -1;
23990 int exponent = 0;
23992 /* Length of QUOTIENT.TENTHS as a string. */
23993 int length;
23995 char * psuffix;
23996 char * p;
23998 if (quotient >= 1000)
24000 /* Scale to the appropriate EXPONENT. */
24003 remainder = quotient % 1000;
24004 quotient /= 1000;
24005 exponent++;
24007 while (quotient >= 1000);
24009 /* Round to nearest and decide whether to use TENTHS or not. */
24010 if (quotient <= 9)
24012 tenths = remainder / 100;
24013 if (remainder % 100 >= 50)
24015 if (tenths < 9)
24016 tenths++;
24017 else
24019 quotient++;
24020 if (quotient == 10)
24021 tenths = -1;
24022 else
24023 tenths = 0;
24027 else
24028 if (remainder >= 500)
24030 if (quotient < 999)
24031 quotient++;
24032 else
24034 quotient = 1;
24035 exponent++;
24036 tenths = 0;
24041 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
24042 if (tenths == -1 && quotient <= 99)
24043 if (quotient <= 9)
24044 length = 1;
24045 else
24046 length = 2;
24047 else
24048 length = 3;
24049 p = psuffix = buf + max (width, length);
24051 /* Print EXPONENT. */
24052 *psuffix++ = power_letter[exponent];
24053 *psuffix = '\0';
24055 /* Print TENTHS. */
24056 if (tenths >= 0)
24058 *--p = '0' + tenths;
24059 *--p = '.';
24062 /* Print QUOTIENT. */
24065 int digit = quotient % 10;
24066 *--p = '0' + digit;
24068 while ((quotient /= 10) != 0);
24070 /* Print leading spaces. */
24071 while (buf < p)
24072 *--p = ' ';
24075 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
24076 If EOL_FLAG, set also a mnemonic character for end-of-line
24077 type of CODING_SYSTEM. Return updated pointer into BUF. */
24079 static unsigned char invalid_eol_type[] = "(*invalid*)";
24081 static char *
24082 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
24084 Lisp_Object val;
24085 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
24086 const unsigned char *eol_str;
24087 int eol_str_len;
24088 /* The EOL conversion we are using. */
24089 Lisp_Object eoltype;
24091 val = CODING_SYSTEM_SPEC (coding_system);
24092 eoltype = Qnil;
24094 if (!VECTORP (val)) /* Not yet decided. */
24096 *buf++ = multibyte ? '-' : ' ';
24097 if (eol_flag)
24098 eoltype = eol_mnemonic_undecided;
24099 /* Don't mention EOL conversion if it isn't decided. */
24101 else
24103 Lisp_Object attrs;
24104 Lisp_Object eolvalue;
24106 attrs = AREF (val, 0);
24107 eolvalue = AREF (val, 2);
24109 *buf++ = multibyte
24110 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
24111 : ' ';
24113 if (eol_flag)
24115 /* The EOL conversion that is normal on this system. */
24117 if (NILP (eolvalue)) /* Not yet decided. */
24118 eoltype = eol_mnemonic_undecided;
24119 else if (VECTORP (eolvalue)) /* Not yet decided. */
24120 eoltype = eol_mnemonic_undecided;
24121 else /* eolvalue is Qunix, Qdos, or Qmac. */
24122 eoltype = (EQ (eolvalue, Qunix)
24123 ? eol_mnemonic_unix
24124 : EQ (eolvalue, Qdos)
24125 ? eol_mnemonic_dos : eol_mnemonic_mac);
24129 if (eol_flag)
24131 /* Mention the EOL conversion if it is not the usual one. */
24132 if (STRINGP (eoltype))
24134 eol_str = SDATA (eoltype);
24135 eol_str_len = SBYTES (eoltype);
24137 else if (CHARACTERP (eoltype))
24139 int c = XFASTINT (eoltype);
24140 return buf + CHAR_STRING (c, (unsigned char *) buf);
24142 else
24144 eol_str = invalid_eol_type;
24145 eol_str_len = sizeof (invalid_eol_type) - 1;
24147 memcpy (buf, eol_str, eol_str_len);
24148 buf += eol_str_len;
24151 return buf;
24154 /* Return the approximate percentage N is of D (rounding upward), or 99,
24155 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
24157 static int
24158 percent99 (ptrdiff_t n, ptrdiff_t d)
24160 int percent = (d - 1 + 100.0 * n) / d;
24161 return min (percent, 99);
24164 /* Return a string for the output of a mode line %-spec for window W,
24165 generated by character C. FIELD_WIDTH > 0 means pad the string
24166 returned with spaces to that value. Return a Lisp string in
24167 *STRING if the resulting string is taken from that Lisp string.
24169 Note we operate on the current buffer for most purposes. */
24171 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
24173 static const char *
24174 decode_mode_spec (struct window *w, register int c, int field_width,
24175 Lisp_Object *string)
24177 Lisp_Object obj;
24178 struct frame *f = XFRAME (WINDOW_FRAME (w));
24179 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
24180 /* We are going to use f->decode_mode_spec_buffer as the buffer to
24181 produce strings from numerical values, so limit preposterously
24182 large values of FIELD_WIDTH to avoid overrunning the buffer's
24183 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
24184 bytes plus the terminating null. */
24185 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
24186 struct buffer *b = current_buffer;
24188 obj = Qnil;
24189 *string = Qnil;
24191 switch (c)
24193 case '*':
24194 if (!NILP (BVAR (b, read_only)))
24195 return "%";
24196 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24197 return "*";
24198 return "-";
24200 case '+':
24201 /* This differs from %* only for a modified read-only buffer. */
24202 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24203 return "*";
24204 if (!NILP (BVAR (b, read_only)))
24205 return "%";
24206 return "-";
24208 case '&':
24209 /* This differs from %* in ignoring read-only-ness. */
24210 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24211 return "*";
24212 return "-";
24214 case '%':
24215 return "%";
24217 case '[':
24219 int i;
24220 char *p;
24222 if (command_loop_level > 5)
24223 return "[[[... ";
24224 p = decode_mode_spec_buf;
24225 for (i = 0; i < command_loop_level; i++)
24226 *p++ = '[';
24227 *p = 0;
24228 return decode_mode_spec_buf;
24231 case ']':
24233 int i;
24234 char *p;
24236 if (command_loop_level > 5)
24237 return " ...]]]";
24238 p = decode_mode_spec_buf;
24239 for (i = 0; i < command_loop_level; i++)
24240 *p++ = ']';
24241 *p = 0;
24242 return decode_mode_spec_buf;
24245 case '-':
24247 register int i;
24249 /* Let lots_of_dashes be a string of infinite length. */
24250 if (mode_line_target == MODE_LINE_NOPROP
24251 || mode_line_target == MODE_LINE_STRING)
24252 return "--";
24253 if (field_width <= 0
24254 || field_width > sizeof (lots_of_dashes))
24256 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
24257 decode_mode_spec_buf[i] = '-';
24258 decode_mode_spec_buf[i] = '\0';
24259 return decode_mode_spec_buf;
24261 else
24262 return lots_of_dashes;
24265 case 'b':
24266 obj = BVAR (b, name);
24267 break;
24269 case 'c':
24270 case 'C':
24271 /* %c, %C, and %l are ignored in `frame-title-format'.
24272 (In redisplay_internal, the frame title is drawn _before_ the
24273 windows are updated, so the stuff which depends on actual
24274 window contents (such as %l) may fail to render properly, or
24275 even crash emacs.) */
24276 if (mode_line_target == MODE_LINE_TITLE)
24277 return "";
24278 else
24280 ptrdiff_t col = current_column ();
24281 int disp_col = (c == 'C') ? col + 1 : col;
24282 w->column_number_displayed = col;
24283 pint2str (decode_mode_spec_buf, width, disp_col);
24284 return decode_mode_spec_buf;
24287 case 'e':
24288 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
24290 if (NILP (Vmemory_full))
24291 return "";
24292 else
24293 return "!MEM FULL! ";
24295 #else
24296 return "";
24297 #endif
24299 case 'F':
24300 /* %F displays the frame name. */
24301 if (!NILP (f->title))
24302 return SSDATA (f->title);
24303 if (f->explicit_name || ! FRAME_WINDOW_P (f))
24304 return SSDATA (f->name);
24305 return "Emacs";
24307 case 'f':
24308 obj = BVAR (b, filename);
24309 break;
24311 case 'i':
24313 ptrdiff_t size = ZV - BEGV;
24314 pint2str (decode_mode_spec_buf, width, size);
24315 return decode_mode_spec_buf;
24318 case 'I':
24320 ptrdiff_t size = ZV - BEGV;
24321 pint2hrstr (decode_mode_spec_buf, width, size);
24322 return decode_mode_spec_buf;
24325 case 'l':
24327 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
24328 ptrdiff_t topline, nlines, height;
24329 ptrdiff_t junk;
24331 /* %c, %C, and %l are ignored in `frame-title-format'. */
24332 if (mode_line_target == MODE_LINE_TITLE)
24333 return "";
24335 startpos = marker_position (w->start);
24336 startpos_byte = marker_byte_position (w->start);
24337 height = WINDOW_TOTAL_LINES (w);
24339 /* If we decided that this buffer isn't suitable for line numbers,
24340 don't forget that too fast. */
24341 if (w->base_line_pos == -1)
24342 goto no_value;
24344 /* If the buffer is very big, don't waste time. */
24345 if (INTEGERP (Vline_number_display_limit)
24346 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
24348 w->base_line_pos = 0;
24349 w->base_line_number = 0;
24350 goto no_value;
24353 if (w->base_line_number > 0
24354 && w->base_line_pos > 0
24355 && w->base_line_pos <= startpos)
24357 line = w->base_line_number;
24358 linepos = w->base_line_pos;
24359 linepos_byte = buf_charpos_to_bytepos (b, linepos);
24361 else
24363 line = 1;
24364 linepos = BUF_BEGV (b);
24365 linepos_byte = BUF_BEGV_BYTE (b);
24368 /* Count lines from base line to window start position. */
24369 nlines = display_count_lines (linepos_byte,
24370 startpos_byte,
24371 startpos, &junk);
24373 topline = nlines + line;
24375 /* Determine a new base line, if the old one is too close
24376 or too far away, or if we did not have one.
24377 "Too close" means it's plausible a scroll-down would
24378 go back past it. */
24379 if (startpos == BUF_BEGV (b))
24381 w->base_line_number = topline;
24382 w->base_line_pos = BUF_BEGV (b);
24384 else if (nlines < height + 25 || nlines > height * 3 + 50
24385 || linepos == BUF_BEGV (b))
24387 ptrdiff_t limit = BUF_BEGV (b);
24388 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
24389 ptrdiff_t position;
24390 ptrdiff_t distance =
24391 (height * 2 + 30) * line_number_display_limit_width;
24393 if (startpos - distance > limit)
24395 limit = startpos - distance;
24396 limit_byte = CHAR_TO_BYTE (limit);
24399 nlines = display_count_lines (startpos_byte,
24400 limit_byte,
24401 - (height * 2 + 30),
24402 &position);
24403 /* If we couldn't find the lines we wanted within
24404 line_number_display_limit_width chars per line,
24405 give up on line numbers for this window. */
24406 if (position == limit_byte && limit == startpos - distance)
24408 w->base_line_pos = -1;
24409 w->base_line_number = 0;
24410 goto no_value;
24413 w->base_line_number = topline - nlines;
24414 w->base_line_pos = BYTE_TO_CHAR (position);
24417 /* Now count lines from the start pos to point. */
24418 nlines = display_count_lines (startpos_byte,
24419 PT_BYTE, PT, &junk);
24421 /* Record that we did display the line number. */
24422 line_number_displayed = true;
24424 /* Make the string to show. */
24425 pint2str (decode_mode_spec_buf, width, topline + nlines);
24426 return decode_mode_spec_buf;
24427 no_value:
24429 char *p = decode_mode_spec_buf;
24430 int pad = width - 2;
24431 while (pad-- > 0)
24432 *p++ = ' ';
24433 *p++ = '?';
24434 *p++ = '?';
24435 *p = '\0';
24436 return decode_mode_spec_buf;
24439 break;
24441 case 'm':
24442 obj = BVAR (b, mode_name);
24443 break;
24445 case 'n':
24446 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
24447 return " Narrow";
24448 break;
24450 /* Display the "degree of travel" of the window through the buffer. */
24451 case 'o':
24453 ptrdiff_t toppos = marker_position (w->start);
24454 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24455 ptrdiff_t begv = BUF_BEGV (b);
24456 ptrdiff_t zv = BUF_ZV (b);
24458 if (zv <= botpos)
24459 return toppos <= begv ? "All" : "Bottom";
24460 else if (toppos <= begv)
24461 return "Top";
24462 else
24464 sprintf (decode_mode_spec_buf, "%2d%%",
24465 percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
24466 return decode_mode_spec_buf;
24470 /* Display percentage of buffer above the top of the screen. */
24471 case 'p':
24473 ptrdiff_t pos = marker_position (w->start);
24474 ptrdiff_t begv = BUF_BEGV (b);
24475 ptrdiff_t zv = BUF_ZV (b);
24477 if (w->window_end_pos <= BUF_Z (b) - zv)
24478 return pos <= begv ? "All" : "Bottom";
24479 else if (pos <= begv)
24480 return "Top";
24481 else
24483 sprintf (decode_mode_spec_buf, "%2d%%",
24484 percent99 (pos - begv, zv - begv));
24485 return decode_mode_spec_buf;
24489 /* Display percentage of size above the bottom of the screen. */
24490 case 'P':
24492 ptrdiff_t toppos = marker_position (w->start);
24493 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24494 ptrdiff_t begv = BUF_BEGV (b);
24495 ptrdiff_t zv = BUF_ZV (b);
24497 if (zv <= botpos)
24498 return toppos <= begv ? "All" : "Bottom";
24499 else
24501 sprintf (decode_mode_spec_buf,
24502 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
24503 percent99 (botpos - begv, zv - begv));
24504 return decode_mode_spec_buf;
24508 /* Display percentage offsets of top and bottom of the window,
24509 using "All" (but not "Top" or "Bottom") where appropriate. */
24510 case 'q':
24512 ptrdiff_t toppos = marker_position (w->start);
24513 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24514 ptrdiff_t begv = BUF_BEGV (b);
24515 ptrdiff_t zv = BUF_ZV (b);
24516 int top_perc, bot_perc;
24518 if ((toppos <= begv) && (zv <= botpos))
24519 return "All ";
24521 top_perc = toppos <= begv ? 0 : percent99 (toppos - begv, zv - begv);
24522 bot_perc = zv <= botpos ? 100 : percent99 (botpos - begv, zv - begv);
24524 if (top_perc == bot_perc)
24525 sprintf (decode_mode_spec_buf, "%d%%", top_perc);
24526 else
24527 sprintf (decode_mode_spec_buf, "%d-%d%%", top_perc, bot_perc);
24529 return decode_mode_spec_buf;
24532 case 's':
24533 /* status of process */
24534 obj = Fget_buffer_process (Fcurrent_buffer ());
24535 if (NILP (obj))
24536 return "no process";
24537 #ifndef MSDOS
24538 obj = Fsymbol_name (Fprocess_status (obj));
24539 #endif
24540 break;
24542 case '@':
24544 ptrdiff_t count = inhibit_garbage_collection ();
24545 Lisp_Object curdir = BVAR (current_buffer, directory);
24546 Lisp_Object val = Qnil;
24548 if (STRINGP (curdir))
24549 val = call1 (intern ("file-remote-p"), curdir);
24551 unbind_to (count, Qnil);
24553 if (NILP (val))
24554 return "-";
24555 else
24556 return "@";
24559 case 'z':
24560 /* coding-system (not including end-of-line format) */
24561 case 'Z':
24562 /* coding-system (including end-of-line type) */
24564 bool eol_flag = (c == 'Z');
24565 char *p = decode_mode_spec_buf;
24567 if (! FRAME_WINDOW_P (f))
24569 /* No need to mention EOL here--the terminal never needs
24570 to do EOL conversion. */
24571 p = decode_mode_spec_coding (CODING_ID_NAME
24572 (FRAME_KEYBOARD_CODING (f)->id),
24573 p, false);
24574 p = decode_mode_spec_coding (CODING_ID_NAME
24575 (FRAME_TERMINAL_CODING (f)->id),
24576 p, false);
24578 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
24579 p, eol_flag);
24581 #if false /* This proves to be annoying; I think we can do without. -- rms. */
24582 #ifdef subprocesses
24583 obj = Fget_buffer_process (Fcurrent_buffer ());
24584 if (PROCESSP (obj))
24586 p = decode_mode_spec_coding
24587 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
24588 p = decode_mode_spec_coding
24589 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
24591 #endif /* subprocesses */
24592 #endif /* false */
24593 *p = 0;
24594 return decode_mode_spec_buf;
24598 if (STRINGP (obj))
24600 *string = obj;
24601 return SSDATA (obj);
24603 else
24604 return "";
24608 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
24609 means count lines back from START_BYTE. But don't go beyond
24610 LIMIT_BYTE. Return the number of lines thus found (always
24611 nonnegative).
24613 Set *BYTE_POS_PTR to the byte position where we stopped. This is
24614 either the position COUNT lines after/before START_BYTE, if we
24615 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
24616 COUNT lines. */
24618 static ptrdiff_t
24619 display_count_lines (ptrdiff_t start_byte,
24620 ptrdiff_t limit_byte, ptrdiff_t count,
24621 ptrdiff_t *byte_pos_ptr)
24623 register unsigned char *cursor;
24624 unsigned char *base;
24626 register ptrdiff_t ceiling;
24627 register unsigned char *ceiling_addr;
24628 ptrdiff_t orig_count = count;
24630 /* If we are not in selective display mode,
24631 check only for newlines. */
24632 bool selective_display
24633 = (!NILP (BVAR (current_buffer, selective_display))
24634 && !INTEGERP (BVAR (current_buffer, selective_display)));
24636 if (count > 0)
24638 while (start_byte < limit_byte)
24640 ceiling = BUFFER_CEILING_OF (start_byte);
24641 ceiling = min (limit_byte - 1, ceiling);
24642 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
24643 base = (cursor = BYTE_POS_ADDR (start_byte));
24647 if (selective_display)
24649 while (*cursor != '\n' && *cursor != 015
24650 && ++cursor != ceiling_addr)
24651 continue;
24652 if (cursor == ceiling_addr)
24653 break;
24655 else
24657 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
24658 if (! cursor)
24659 break;
24662 cursor++;
24664 if (--count == 0)
24666 start_byte += cursor - base;
24667 *byte_pos_ptr = start_byte;
24668 return orig_count;
24671 while (cursor < ceiling_addr);
24673 start_byte += ceiling_addr - base;
24676 else
24678 while (start_byte > limit_byte)
24680 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
24681 ceiling = max (limit_byte, ceiling);
24682 ceiling_addr = BYTE_POS_ADDR (ceiling);
24683 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
24684 while (true)
24686 if (selective_display)
24688 while (--cursor >= ceiling_addr
24689 && *cursor != '\n' && *cursor != 015)
24690 continue;
24691 if (cursor < ceiling_addr)
24692 break;
24694 else
24696 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
24697 if (! cursor)
24698 break;
24701 if (++count == 0)
24703 start_byte += cursor - base + 1;
24704 *byte_pos_ptr = start_byte;
24705 /* When scanning backwards, we should
24706 not count the newline posterior to which we stop. */
24707 return - orig_count - 1;
24710 start_byte += ceiling_addr - base;
24714 *byte_pos_ptr = limit_byte;
24716 if (count < 0)
24717 return - orig_count + count;
24718 return orig_count - count;
24724 /***********************************************************************
24725 Displaying strings
24726 ***********************************************************************/
24728 /* Display a NUL-terminated string, starting with index START.
24730 If STRING is non-null, display that C string. Otherwise, the Lisp
24731 string LISP_STRING is displayed. There's a case that STRING is
24732 non-null and LISP_STRING is not nil. It means STRING is a string
24733 data of LISP_STRING. In that case, we display LISP_STRING while
24734 ignoring its text properties.
24736 If FACE_STRING is not nil, FACE_STRING_POS is a position in
24737 FACE_STRING. Display STRING or LISP_STRING with the face at
24738 FACE_STRING_POS in FACE_STRING:
24740 Display the string in the environment given by IT, but use the
24741 standard display table, temporarily.
24743 FIELD_WIDTH is the minimum number of output glyphs to produce.
24744 If STRING has fewer characters than FIELD_WIDTH, pad to the right
24745 with spaces. If STRING has more characters, more than FIELD_WIDTH
24746 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
24748 PRECISION is the maximum number of characters to output from
24749 STRING. PRECISION < 0 means don't truncate the string.
24751 This is roughly equivalent to printf format specifiers:
24753 FIELD_WIDTH PRECISION PRINTF
24754 ----------------------------------------
24755 -1 -1 %s
24756 -1 10 %.10s
24757 10 -1 %10s
24758 20 10 %20.10s
24760 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24761 display them, and < 0 means obey the current buffer's value of
24762 enable_multibyte_characters.
24764 Value is the number of columns displayed. */
24766 static int
24767 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24768 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24769 int field_width, int precision, int max_x, int multibyte)
24771 int hpos_at_start = it->hpos;
24772 int saved_face_id = it->face_id;
24773 struct glyph_row *row = it->glyph_row;
24774 ptrdiff_t it_charpos;
24776 /* Initialize the iterator IT for iteration over STRING beginning
24777 with index START. */
24778 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24779 precision, field_width, multibyte);
24780 if (string && STRINGP (lisp_string))
24781 /* LISP_STRING is the one returned by decode_mode_spec. We should
24782 ignore its text properties. */
24783 it->stop_charpos = it->end_charpos;
24785 /* If displaying STRING, set up the face of the iterator from
24786 FACE_STRING, if that's given. */
24787 if (STRINGP (face_string))
24789 ptrdiff_t endptr;
24790 struct face *face;
24792 it->face_id
24793 = face_at_string_position (it->w, face_string, face_string_pos,
24794 0, &endptr, it->base_face_id, false);
24795 face = FACE_FROM_ID (it->f, it->face_id);
24796 it->face_box_p = face->box != FACE_NO_BOX;
24799 /* Set max_x to the maximum allowed X position. Don't let it go
24800 beyond the right edge of the window. */
24801 if (max_x <= 0)
24802 max_x = it->last_visible_x;
24803 else
24804 max_x = min (max_x, it->last_visible_x);
24806 /* Skip over display elements that are not visible. because IT->w is
24807 hscrolled. */
24808 if (it->current_x < it->first_visible_x)
24809 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24810 MOVE_TO_POS | MOVE_TO_X);
24812 row->ascent = it->max_ascent;
24813 row->height = it->max_ascent + it->max_descent;
24814 row->phys_ascent = it->max_phys_ascent;
24815 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24816 row->extra_line_spacing = it->max_extra_line_spacing;
24818 if (STRINGP (it->string))
24819 it_charpos = IT_STRING_CHARPOS (*it);
24820 else
24821 it_charpos = IT_CHARPOS (*it);
24823 /* This condition is for the case that we are called with current_x
24824 past last_visible_x. */
24825 while (it->current_x < max_x)
24827 int x_before, x, n_glyphs_before, i, nglyphs;
24829 /* Get the next display element. */
24830 if (!get_next_display_element (it))
24831 break;
24833 /* Produce glyphs. */
24834 x_before = it->current_x;
24835 n_glyphs_before = row->used[TEXT_AREA];
24836 PRODUCE_GLYPHS (it);
24838 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24839 i = 0;
24840 x = x_before;
24841 while (i < nglyphs)
24843 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24845 if (it->line_wrap != TRUNCATE
24846 && x + glyph->pixel_width > max_x)
24848 /* End of continued line or max_x reached. */
24849 if (CHAR_GLYPH_PADDING_P (*glyph))
24851 /* A wide character is unbreakable. */
24852 if (row->reversed_p)
24853 unproduce_glyphs (it, row->used[TEXT_AREA]
24854 - n_glyphs_before);
24855 row->used[TEXT_AREA] = n_glyphs_before;
24856 it->current_x = x_before;
24858 else
24860 if (row->reversed_p)
24861 unproduce_glyphs (it, row->used[TEXT_AREA]
24862 - (n_glyphs_before + i));
24863 row->used[TEXT_AREA] = n_glyphs_before + i;
24864 it->current_x = x;
24866 break;
24868 else if (x + glyph->pixel_width >= it->first_visible_x)
24870 /* Glyph is at least partially visible. */
24871 ++it->hpos;
24872 if (x < it->first_visible_x)
24873 row->x = x - it->first_visible_x;
24875 else
24877 /* Glyph is off the left margin of the display area.
24878 Should not happen. */
24879 emacs_abort ();
24882 row->ascent = max (row->ascent, it->max_ascent);
24883 row->height = max (row->height, it->max_ascent + it->max_descent);
24884 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24885 row->phys_height = max (row->phys_height,
24886 it->max_phys_ascent + it->max_phys_descent);
24887 row->extra_line_spacing = max (row->extra_line_spacing,
24888 it->max_extra_line_spacing);
24889 x += glyph->pixel_width;
24890 ++i;
24893 /* Stop if max_x reached. */
24894 if (i < nglyphs)
24895 break;
24897 /* Stop at line ends. */
24898 if (ITERATOR_AT_END_OF_LINE_P (it))
24900 it->continuation_lines_width = 0;
24901 break;
24904 set_iterator_to_next (it, true);
24905 if (STRINGP (it->string))
24906 it_charpos = IT_STRING_CHARPOS (*it);
24907 else
24908 it_charpos = IT_CHARPOS (*it);
24910 /* Stop if truncating at the right edge. */
24911 if (it->line_wrap == TRUNCATE
24912 && it->current_x >= it->last_visible_x)
24914 /* Add truncation mark, but don't do it if the line is
24915 truncated at a padding space. */
24916 if (it_charpos < it->string_nchars)
24918 if (!FRAME_WINDOW_P (it->f))
24920 int ii, n;
24922 if (it->current_x > it->last_visible_x)
24924 if (!row->reversed_p)
24926 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24927 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24928 break;
24930 else
24932 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24933 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24934 break;
24935 unproduce_glyphs (it, ii + 1);
24936 ii = row->used[TEXT_AREA] - (ii + 1);
24938 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24940 row->used[TEXT_AREA] = ii;
24941 produce_special_glyphs (it, IT_TRUNCATION);
24944 produce_special_glyphs (it, IT_TRUNCATION);
24946 row->truncated_on_right_p = true;
24948 break;
24952 /* Maybe insert a truncation at the left. */
24953 if (it->first_visible_x
24954 && it_charpos > 0)
24956 if (!FRAME_WINDOW_P (it->f)
24957 || (row->reversed_p
24958 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24959 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24960 insert_left_trunc_glyphs (it);
24961 row->truncated_on_left_p = true;
24964 it->face_id = saved_face_id;
24966 /* Value is number of columns displayed. */
24967 return it->hpos - hpos_at_start;
24972 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24973 appears as an element of LIST or as the car of an element of LIST.
24974 If PROPVAL is a list, compare each element against LIST in that
24975 way, and return 1/2 if any element of PROPVAL is found in LIST.
24976 Otherwise return 0. This function cannot quit.
24977 The return value is 2 if the text is invisible but with an ellipsis
24978 and 1 if it's invisible and without an ellipsis. */
24981 invisible_prop (Lisp_Object propval, Lisp_Object list)
24983 Lisp_Object tail, proptail;
24985 for (tail = list; CONSP (tail); tail = XCDR (tail))
24987 register Lisp_Object tem;
24988 tem = XCAR (tail);
24989 if (EQ (propval, tem))
24990 return 1;
24991 if (CONSP (tem) && EQ (propval, XCAR (tem)))
24992 return NILP (XCDR (tem)) ? 1 : 2;
24995 if (CONSP (propval))
24997 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
24999 Lisp_Object propelt;
25000 propelt = XCAR (proptail);
25001 for (tail = list; CONSP (tail); tail = XCDR (tail))
25003 register Lisp_Object tem;
25004 tem = XCAR (tail);
25005 if (EQ (propelt, tem))
25006 return 1;
25007 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
25008 return NILP (XCDR (tem)) ? 1 : 2;
25013 return 0;
25016 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
25017 doc: /* Non-nil if the property makes the text invisible.
25018 POS-OR-PROP can be a marker or number, in which case it is taken to be
25019 a position in the current buffer and the value of the `invisible' property
25020 is checked; or it can be some other value, which is then presumed to be the
25021 value of the `invisible' property of the text of interest.
25022 The non-nil value returned can be t for truly invisible text or something
25023 else if the text is replaced by an ellipsis. */)
25024 (Lisp_Object pos_or_prop)
25026 Lisp_Object prop
25027 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
25028 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
25029 : pos_or_prop);
25030 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
25031 return (invis == 0 ? Qnil
25032 : invis == 1 ? Qt
25033 : make_number (invis));
25036 /* Calculate a width or height in pixels from a specification using
25037 the following elements:
25039 SPEC ::=
25040 NUM - a (fractional) multiple of the default font width/height
25041 (NUM) - specifies exactly NUM pixels
25042 UNIT - a fixed number of pixels, see below.
25043 ELEMENT - size of a display element in pixels, see below.
25044 (NUM . SPEC) - equals NUM * SPEC
25045 (+ SPEC SPEC ...) - add pixel values
25046 (- SPEC SPEC ...) - subtract pixel values
25047 (- SPEC) - negate pixel value
25049 NUM ::=
25050 INT or FLOAT - a number constant
25051 SYMBOL - use symbol's (buffer local) variable binding.
25053 UNIT ::=
25054 in - pixels per inch *)
25055 mm - pixels per 1/1000 meter *)
25056 cm - pixels per 1/100 meter *)
25057 width - width of current font in pixels.
25058 height - height of current font in pixels.
25060 *) using the ratio(s) defined in display-pixels-per-inch.
25062 ELEMENT ::=
25064 left-fringe - left fringe width in pixels
25065 right-fringe - right fringe width in pixels
25067 left-margin - left margin width in pixels
25068 right-margin - right margin width in pixels
25070 scroll-bar - scroll-bar area width in pixels
25072 Examples:
25074 Pixels corresponding to 5 inches:
25075 (5 . in)
25077 Total width of non-text areas on left side of window (if scroll-bar is on left):
25078 '(space :width (+ left-fringe left-margin scroll-bar))
25080 Align to first text column (in header line):
25081 '(space :align-to 0)
25083 Align to middle of text area minus half the width of variable `my-image'
25084 containing a loaded image:
25085 '(space :align-to (0.5 . (- text my-image)))
25087 Width of left margin minus width of 1 character in the default font:
25088 '(space :width (- left-margin 1))
25090 Width of left margin minus width of 2 characters in the current font:
25091 '(space :width (- left-margin (2 . width)))
25093 Center 1 character over left-margin (in header line):
25094 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
25096 Different ways to express width of left fringe plus left margin minus one pixel:
25097 '(space :width (- (+ left-fringe left-margin) (1)))
25098 '(space :width (+ left-fringe left-margin (- (1))))
25099 '(space :width (+ left-fringe left-margin (-1)))
25103 static bool
25104 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
25105 struct font *font, bool width_p, int *align_to)
25107 double pixels;
25109 # define OK_PIXELS(val) (*res = (val), true)
25110 # define OK_ALIGN_TO(val) (*align_to = (val), true)
25112 if (NILP (prop))
25113 return OK_PIXELS (0);
25115 eassert (FRAME_LIVE_P (it->f));
25117 if (SYMBOLP (prop))
25119 if (SCHARS (SYMBOL_NAME (prop)) == 2)
25121 char *unit = SSDATA (SYMBOL_NAME (prop));
25123 if (unit[0] == 'i' && unit[1] == 'n')
25124 pixels = 1.0;
25125 else if (unit[0] == 'm' && unit[1] == 'm')
25126 pixels = 25.4;
25127 else if (unit[0] == 'c' && unit[1] == 'm')
25128 pixels = 2.54;
25129 else
25130 pixels = 0;
25131 if (pixels > 0)
25133 double ppi = (width_p ? FRAME_RES_X (it->f)
25134 : FRAME_RES_Y (it->f));
25136 if (ppi > 0)
25137 return OK_PIXELS (ppi / pixels);
25138 return false;
25142 #ifdef HAVE_WINDOW_SYSTEM
25143 if (EQ (prop, Qheight))
25144 return OK_PIXELS (font
25145 ? normal_char_height (font, -1)
25146 : FRAME_LINE_HEIGHT (it->f));
25147 if (EQ (prop, Qwidth))
25148 return OK_PIXELS (font
25149 ? FONT_WIDTH (font)
25150 : FRAME_COLUMN_WIDTH (it->f));
25151 #else
25152 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
25153 return OK_PIXELS (1);
25154 #endif
25156 if (EQ (prop, Qtext))
25157 return OK_PIXELS (width_p
25158 ? window_box_width (it->w, TEXT_AREA)
25159 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
25161 if (align_to && *align_to < 0)
25163 *res = 0;
25164 if (EQ (prop, Qleft))
25165 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
25166 if (EQ (prop, Qright))
25167 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
25168 if (EQ (prop, Qcenter))
25169 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
25170 + window_box_width (it->w, TEXT_AREA) / 2);
25171 if (EQ (prop, Qleft_fringe))
25172 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25173 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
25174 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
25175 if (EQ (prop, Qright_fringe))
25176 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25177 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25178 : window_box_right_offset (it->w, TEXT_AREA));
25179 if (EQ (prop, Qleft_margin))
25180 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
25181 if (EQ (prop, Qright_margin))
25182 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
25183 if (EQ (prop, Qscroll_bar))
25184 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
25186 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25187 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25188 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
25189 : 0)));
25191 else
25193 if (EQ (prop, Qleft_fringe))
25194 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
25195 if (EQ (prop, Qright_fringe))
25196 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
25197 if (EQ (prop, Qleft_margin))
25198 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
25199 if (EQ (prop, Qright_margin))
25200 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
25201 if (EQ (prop, Qscroll_bar))
25202 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
25205 prop = buffer_local_value (prop, it->w->contents);
25206 if (EQ (prop, Qunbound))
25207 prop = Qnil;
25210 if (NUMBERP (prop))
25212 int base_unit = (width_p
25213 ? FRAME_COLUMN_WIDTH (it->f)
25214 : FRAME_LINE_HEIGHT (it->f));
25215 return OK_PIXELS (XFLOATINT (prop) * base_unit);
25218 if (CONSP (prop))
25220 Lisp_Object car = XCAR (prop);
25221 Lisp_Object cdr = XCDR (prop);
25223 if (SYMBOLP (car))
25225 #ifdef HAVE_WINDOW_SYSTEM
25226 if (FRAME_WINDOW_P (it->f)
25227 && valid_image_p (prop))
25229 ptrdiff_t id = lookup_image (it->f, prop);
25230 struct image *img = IMAGE_FROM_ID (it->f, id);
25232 return OK_PIXELS (width_p ? img->width : img->height);
25234 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
25236 /* TODO: Don't return dummy size. */
25237 return OK_PIXELS (100);
25239 #endif
25240 if (EQ (car, Qplus) || EQ (car, Qminus))
25242 bool first = true;
25243 double px;
25245 pixels = 0;
25246 while (CONSP (cdr))
25248 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
25249 font, width_p, align_to))
25250 return false;
25251 if (first)
25252 pixels = (EQ (car, Qplus) ? px : -px), first = false;
25253 else
25254 pixels += px;
25255 cdr = XCDR (cdr);
25257 if (EQ (car, Qminus))
25258 pixels = -pixels;
25259 return OK_PIXELS (pixels);
25262 car = buffer_local_value (car, it->w->contents);
25263 if (EQ (car, Qunbound))
25264 car = Qnil;
25267 if (NUMBERP (car))
25269 double fact;
25270 pixels = XFLOATINT (car);
25271 if (NILP (cdr))
25272 return OK_PIXELS (pixels);
25273 if (calc_pixel_width_or_height (&fact, it, cdr,
25274 font, width_p, align_to))
25275 return OK_PIXELS (pixels * fact);
25276 return false;
25279 return false;
25282 return false;
25285 void
25286 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
25288 #ifdef HAVE_WINDOW_SYSTEM
25289 normal_char_ascent_descent (font, -1, ascent, descent);
25290 #else
25291 *ascent = 1;
25292 *descent = 0;
25293 #endif
25297 /***********************************************************************
25298 Glyph Display
25299 ***********************************************************************/
25301 #ifdef HAVE_WINDOW_SYSTEM
25303 #ifdef GLYPH_DEBUG
25305 void
25306 dump_glyph_string (struct glyph_string *s)
25308 fprintf (stderr, "glyph string\n");
25309 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
25310 s->x, s->y, s->width, s->height);
25311 fprintf (stderr, " ybase = %d\n", s->ybase);
25312 fprintf (stderr, " hl = %u\n", s->hl);
25313 fprintf (stderr, " left overhang = %d, right = %d\n",
25314 s->left_overhang, s->right_overhang);
25315 fprintf (stderr, " nchars = %d\n", s->nchars);
25316 fprintf (stderr, " extends to end of line = %d\n",
25317 s->extends_to_end_of_line_p);
25318 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
25319 fprintf (stderr, " bg width = %d\n", s->background_width);
25322 #endif /* GLYPH_DEBUG */
25324 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
25325 of XChar2b structures for S; it can't be allocated in
25326 init_glyph_string because it must be allocated via `alloca'. W
25327 is the window on which S is drawn. ROW and AREA are the glyph row
25328 and area within the row from which S is constructed. START is the
25329 index of the first glyph structure covered by S. HL is a
25330 face-override for drawing S. */
25332 #ifdef HAVE_NTGUI
25333 #define OPTIONAL_HDC(hdc) HDC hdc,
25334 #define DECLARE_HDC(hdc) HDC hdc;
25335 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
25336 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
25337 #endif
25339 #ifndef OPTIONAL_HDC
25340 #define OPTIONAL_HDC(hdc)
25341 #define DECLARE_HDC(hdc)
25342 #define ALLOCATE_HDC(hdc, f)
25343 #define RELEASE_HDC(hdc, f)
25344 #endif
25346 static void
25347 init_glyph_string (struct glyph_string *s,
25348 OPTIONAL_HDC (hdc)
25349 XChar2b *char2b, struct window *w, struct glyph_row *row,
25350 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
25352 memset (s, 0, sizeof *s);
25353 s->w = w;
25354 s->f = XFRAME (w->frame);
25355 #ifdef HAVE_NTGUI
25356 s->hdc = hdc;
25357 #endif
25358 s->display = FRAME_X_DISPLAY (s->f);
25359 s->char2b = char2b;
25360 s->hl = hl;
25361 s->row = row;
25362 s->area = area;
25363 s->first_glyph = row->glyphs[area] + start;
25364 s->height = row->height;
25365 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
25366 s->ybase = s->y + row->ascent;
25370 /* Append the list of glyph strings with head H and tail T to the list
25371 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
25373 static void
25374 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25375 struct glyph_string *h, struct glyph_string *t)
25377 if (h)
25379 if (*head)
25380 (*tail)->next = h;
25381 else
25382 *head = h;
25383 h->prev = *tail;
25384 *tail = t;
25389 /* Prepend the list of glyph strings with head H and tail T to the
25390 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
25391 result. */
25393 static void
25394 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25395 struct glyph_string *h, struct glyph_string *t)
25397 if (h)
25399 if (*head)
25400 (*head)->prev = t;
25401 else
25402 *tail = t;
25403 t->next = *head;
25404 *head = h;
25409 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
25410 Set *HEAD and *TAIL to the resulting list. */
25412 static void
25413 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
25414 struct glyph_string *s)
25416 s->next = s->prev = NULL;
25417 append_glyph_string_lists (head, tail, s, s);
25421 /* Get face and two-byte form of character C in face FACE_ID on frame F.
25422 The encoding of C is returned in *CHAR2B. DISPLAY_P means
25423 make sure that X resources for the face returned are allocated.
25424 Value is a pointer to a realized face that is ready for display if
25425 DISPLAY_P. */
25427 static struct face *
25428 get_char_face_and_encoding (struct frame *f, int c, int face_id,
25429 XChar2b *char2b, bool display_p)
25431 struct face *face = FACE_FROM_ID (f, face_id);
25432 unsigned code = 0;
25434 if (face->font)
25436 code = face->font->driver->encode_char (face->font, c);
25438 if (code == FONT_INVALID_CODE)
25439 code = 0;
25441 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25443 /* Make sure X resources of the face are allocated. */
25444 #ifdef HAVE_X_WINDOWS
25445 if (display_p)
25446 #endif
25448 eassert (face != NULL);
25449 prepare_face_for_display (f, face);
25452 return face;
25456 /* Get face and two-byte form of character glyph GLYPH on frame F.
25457 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
25458 a pointer to a realized face that is ready for display. */
25460 static struct face *
25461 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
25462 XChar2b *char2b)
25464 struct face *face;
25465 unsigned code = 0;
25467 eassert (glyph->type == CHAR_GLYPH);
25468 face = FACE_FROM_ID (f, glyph->face_id);
25470 /* Make sure X resources of the face are allocated. */
25471 prepare_face_for_display (f, face);
25473 if (face->font)
25475 if (CHAR_BYTE8_P (glyph->u.ch))
25476 code = CHAR_TO_BYTE8 (glyph->u.ch);
25477 else
25478 code = face->font->driver->encode_char (face->font, glyph->u.ch);
25480 if (code == FONT_INVALID_CODE)
25481 code = 0;
25484 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25485 return face;
25489 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
25490 Return true iff FONT has a glyph for C. */
25492 static bool
25493 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
25495 unsigned code;
25497 if (CHAR_BYTE8_P (c))
25498 code = CHAR_TO_BYTE8 (c);
25499 else
25500 code = font->driver->encode_char (font, c);
25502 if (code == FONT_INVALID_CODE)
25503 return false;
25504 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25505 return true;
25509 /* Fill glyph string S with composition components specified by S->cmp.
25511 BASE_FACE is the base face of the composition.
25512 S->cmp_from is the index of the first component for S.
25514 OVERLAPS non-zero means S should draw the foreground only, and use
25515 its physical height for clipping. See also draw_glyphs.
25517 Value is the index of a component not in S. */
25519 static int
25520 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
25521 int overlaps)
25523 int i;
25524 /* For all glyphs of this composition, starting at the offset
25525 S->cmp_from, until we reach the end of the definition or encounter a
25526 glyph that requires the different face, add it to S. */
25527 struct face *face;
25529 eassert (s);
25531 s->for_overlaps = overlaps;
25532 s->face = NULL;
25533 s->font = NULL;
25534 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
25536 int c = COMPOSITION_GLYPH (s->cmp, i);
25538 /* TAB in a composition means display glyphs with padding space
25539 on the left or right. */
25540 if (c != '\t')
25542 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
25543 -1, Qnil);
25545 face = get_char_face_and_encoding (s->f, c, face_id,
25546 s->char2b + i, true);
25547 if (face)
25549 if (! s->face)
25551 s->face = face;
25552 s->font = s->face->font;
25554 else if (s->face != face)
25555 break;
25558 ++s->nchars;
25560 s->cmp_to = i;
25562 if (s->face == NULL)
25564 s->face = base_face->ascii_face;
25565 s->font = s->face->font;
25568 /* All glyph strings for the same composition has the same width,
25569 i.e. the width set for the first component of the composition. */
25570 s->width = s->first_glyph->pixel_width;
25572 /* If the specified font could not be loaded, use the frame's
25573 default font, but record the fact that we couldn't load it in
25574 the glyph string so that we can draw rectangles for the
25575 characters of the glyph string. */
25576 if (s->font == NULL)
25578 s->font_not_found_p = true;
25579 s->font = FRAME_FONT (s->f);
25582 /* Adjust base line for subscript/superscript text. */
25583 s->ybase += s->first_glyph->voffset;
25585 return s->cmp_to;
25588 static int
25589 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
25590 int start, int end, int overlaps)
25592 struct glyph *glyph, *last;
25593 Lisp_Object lgstring;
25594 int i;
25596 s->for_overlaps = overlaps;
25597 glyph = s->row->glyphs[s->area] + start;
25598 last = s->row->glyphs[s->area] + end;
25599 s->cmp_id = glyph->u.cmp.id;
25600 s->cmp_from = glyph->slice.cmp.from;
25601 s->cmp_to = glyph->slice.cmp.to + 1;
25602 s->face = FACE_FROM_ID (s->f, face_id);
25603 lgstring = composition_gstring_from_id (s->cmp_id);
25604 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
25605 glyph++;
25606 while (glyph < last
25607 && glyph->u.cmp.automatic
25608 && glyph->u.cmp.id == s->cmp_id
25609 && s->cmp_to == glyph->slice.cmp.from)
25610 s->cmp_to = (glyph++)->slice.cmp.to + 1;
25612 for (i = s->cmp_from; i < s->cmp_to; i++)
25614 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
25615 unsigned code = LGLYPH_CODE (lglyph);
25617 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
25619 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
25620 return glyph - s->row->glyphs[s->area];
25624 /* Fill glyph string S from a sequence glyphs for glyphless characters.
25625 See the comment of fill_glyph_string for arguments.
25626 Value is the index of the first glyph not in S. */
25629 static int
25630 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
25631 int start, int end, int overlaps)
25633 struct glyph *glyph, *last;
25634 int voffset;
25636 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
25637 s->for_overlaps = overlaps;
25638 glyph = s->row->glyphs[s->area] + start;
25639 last = s->row->glyphs[s->area] + end;
25640 voffset = glyph->voffset;
25641 s->face = FACE_FROM_ID (s->f, face_id);
25642 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
25643 s->nchars = 1;
25644 s->width = glyph->pixel_width;
25645 glyph++;
25646 while (glyph < last
25647 && glyph->type == GLYPHLESS_GLYPH
25648 && glyph->voffset == voffset
25649 && glyph->face_id == face_id)
25651 s->nchars++;
25652 s->width += glyph->pixel_width;
25653 glyph++;
25655 s->ybase += voffset;
25656 return glyph - s->row->glyphs[s->area];
25660 /* Fill glyph string S from a sequence of character glyphs.
25662 FACE_ID is the face id of the string. START is the index of the
25663 first glyph to consider, END is the index of the last + 1.
25664 OVERLAPS non-zero means S should draw the foreground only, and use
25665 its physical height for clipping. See also draw_glyphs.
25667 Value is the index of the first glyph not in S. */
25669 static int
25670 fill_glyph_string (struct glyph_string *s, int face_id,
25671 int start, int end, int overlaps)
25673 struct glyph *glyph, *last;
25674 int voffset;
25675 bool glyph_not_available_p;
25677 eassert (s->f == XFRAME (s->w->frame));
25678 eassert (s->nchars == 0);
25679 eassert (start >= 0 && end > start);
25681 s->for_overlaps = overlaps;
25682 glyph = s->row->glyphs[s->area] + start;
25683 last = s->row->glyphs[s->area] + end;
25684 voffset = glyph->voffset;
25685 s->padding_p = glyph->padding_p;
25686 glyph_not_available_p = glyph->glyph_not_available_p;
25688 while (glyph < last
25689 && glyph->type == CHAR_GLYPH
25690 && glyph->voffset == voffset
25691 /* Same face id implies same font, nowadays. */
25692 && glyph->face_id == face_id
25693 && glyph->glyph_not_available_p == glyph_not_available_p)
25695 s->face = get_glyph_face_and_encoding (s->f, glyph,
25696 s->char2b + s->nchars);
25697 ++s->nchars;
25698 eassert (s->nchars <= end - start);
25699 s->width += glyph->pixel_width;
25700 if (glyph++->padding_p != s->padding_p)
25701 break;
25704 s->font = s->face->font;
25706 /* If the specified font could not be loaded, use the frame's font,
25707 but record the fact that we couldn't load it in
25708 S->font_not_found_p so that we can draw rectangles for the
25709 characters of the glyph string. */
25710 if (s->font == NULL || glyph_not_available_p)
25712 s->font_not_found_p = true;
25713 s->font = FRAME_FONT (s->f);
25716 /* Adjust base line for subscript/superscript text. */
25717 s->ybase += voffset;
25719 eassert (s->face && s->face->gc);
25720 return glyph - s->row->glyphs[s->area];
25724 /* Fill glyph string S from image glyph S->first_glyph. */
25726 static void
25727 fill_image_glyph_string (struct glyph_string *s)
25729 eassert (s->first_glyph->type == IMAGE_GLYPH);
25730 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
25731 eassert (s->img);
25732 s->slice = s->first_glyph->slice.img;
25733 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25734 s->font = s->face->font;
25735 s->width = s->first_glyph->pixel_width;
25737 /* Adjust base line for subscript/superscript text. */
25738 s->ybase += s->first_glyph->voffset;
25742 #ifdef HAVE_XWIDGETS
25743 static void
25744 fill_xwidget_glyph_string (struct glyph_string *s)
25746 eassert (s->first_glyph->type == XWIDGET_GLYPH);
25747 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25748 s->font = s->face->font;
25749 s->width = s->first_glyph->pixel_width;
25750 s->ybase += s->first_glyph->voffset;
25751 s->xwidget = s->first_glyph->u.xwidget;
25753 #endif
25754 /* Fill glyph string S from a sequence of stretch glyphs.
25756 START is the index of the first glyph to consider,
25757 END is the index of the last + 1.
25759 Value is the index of the first glyph not in S. */
25761 static int
25762 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25764 struct glyph *glyph, *last;
25765 int voffset, face_id;
25767 eassert (s->first_glyph->type == STRETCH_GLYPH);
25769 glyph = s->row->glyphs[s->area] + start;
25770 last = s->row->glyphs[s->area] + end;
25771 face_id = glyph->face_id;
25772 s->face = FACE_FROM_ID (s->f, face_id);
25773 s->font = s->face->font;
25774 s->width = glyph->pixel_width;
25775 s->nchars = 1;
25776 voffset = glyph->voffset;
25778 for (++glyph;
25779 (glyph < last
25780 && glyph->type == STRETCH_GLYPH
25781 && glyph->voffset == voffset
25782 && glyph->face_id == face_id);
25783 ++glyph)
25784 s->width += glyph->pixel_width;
25786 /* Adjust base line for subscript/superscript text. */
25787 s->ybase += voffset;
25789 /* The case that face->gc == 0 is handled when drawing the glyph
25790 string by calling prepare_face_for_display. */
25791 eassert (s->face);
25792 return glyph - s->row->glyphs[s->area];
25795 static struct font_metrics *
25796 get_per_char_metric (struct font *font, XChar2b *char2b)
25798 static struct font_metrics metrics;
25799 unsigned code;
25801 if (! font)
25802 return NULL;
25803 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25804 if (code == FONT_INVALID_CODE)
25805 return NULL;
25806 font->driver->text_extents (font, &code, 1, &metrics);
25807 return &metrics;
25810 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25811 for FONT. Values are taken from font-global ones, except for fonts
25812 that claim preposterously large values, but whose glyphs actually
25813 have reasonable dimensions. C is the character to use for metrics
25814 if the font-global values are too large; if C is negative, the
25815 function selects a default character. */
25816 static void
25817 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25819 *ascent = FONT_BASE (font);
25820 *descent = FONT_DESCENT (font);
25822 if (FONT_TOO_HIGH (font))
25824 XChar2b char2b;
25826 /* Get metrics of C, defaulting to a reasonably sized ASCII
25827 character. */
25828 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25830 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25832 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25834 /* We add 1 pixel to character dimensions as heuristics
25835 that produces nicer display, e.g. when the face has
25836 the box attribute. */
25837 *ascent = pcm->ascent + 1;
25838 *descent = pcm->descent + 1;
25844 /* A subroutine that computes a reasonable "normal character height"
25845 for fonts that claim preposterously large vertical dimensions, but
25846 whose glyphs are actually reasonably sized. C is the character
25847 whose metrics to use for those fonts, or -1 for default
25848 character. */
25849 static int
25850 normal_char_height (struct font *font, int c)
25852 int ascent, descent;
25854 normal_char_ascent_descent (font, c, &ascent, &descent);
25856 return ascent + descent;
25859 /* EXPORT for RIF:
25860 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
25861 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
25862 assumed to be zero. */
25864 void
25865 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
25867 *left = *right = 0;
25869 if (glyph->type == CHAR_GLYPH)
25871 XChar2b char2b;
25872 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
25873 if (face->font)
25875 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
25876 if (pcm)
25878 if (pcm->rbearing > pcm->width)
25879 *right = pcm->rbearing - pcm->width;
25880 if (pcm->lbearing < 0)
25881 *left = -pcm->lbearing;
25885 else if (glyph->type == COMPOSITE_GLYPH)
25887 if (! glyph->u.cmp.automatic)
25889 struct composition *cmp = composition_table[glyph->u.cmp.id];
25891 if (cmp->rbearing > cmp->pixel_width)
25892 *right = cmp->rbearing - cmp->pixel_width;
25893 if (cmp->lbearing < 0)
25894 *left = - cmp->lbearing;
25896 else
25898 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25899 struct font_metrics metrics;
25901 composition_gstring_width (gstring, glyph->slice.cmp.from,
25902 glyph->slice.cmp.to + 1, &metrics);
25903 if (metrics.rbearing > metrics.width)
25904 *right = metrics.rbearing - metrics.width;
25905 if (metrics.lbearing < 0)
25906 *left = - metrics.lbearing;
25912 /* Return the index of the first glyph preceding glyph string S that
25913 is overwritten by S because of S's left overhang. Value is -1
25914 if no glyphs are overwritten. */
25916 static int
25917 left_overwritten (struct glyph_string *s)
25919 int k;
25921 if (s->left_overhang)
25923 int x = 0, i;
25924 struct glyph *glyphs = s->row->glyphs[s->area];
25925 int first = s->first_glyph - glyphs;
25927 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25928 x -= glyphs[i].pixel_width;
25930 k = i + 1;
25932 else
25933 k = -1;
25935 return k;
25939 /* Return the index of the first glyph preceding glyph string S that
25940 is overwriting S because of its right overhang. Value is -1 if no
25941 glyph in front of S overwrites S. */
25943 static int
25944 left_overwriting (struct glyph_string *s)
25946 int i, k, x;
25947 struct glyph *glyphs = s->row->glyphs[s->area];
25948 int first = s->first_glyph - glyphs;
25950 k = -1;
25951 x = 0;
25952 for (i = first - 1; i >= 0; --i)
25954 int left, right;
25955 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25956 if (x + right > 0)
25957 k = i;
25958 x -= glyphs[i].pixel_width;
25961 return k;
25965 /* Return the index of the last glyph following glyph string S that is
25966 overwritten by S because of S's right overhang. Value is -1 if
25967 no such glyph is found. */
25969 static int
25970 right_overwritten (struct glyph_string *s)
25972 int k = -1;
25974 if (s->right_overhang)
25976 int x = 0, i;
25977 struct glyph *glyphs = s->row->glyphs[s->area];
25978 int first = (s->first_glyph - glyphs
25979 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25980 int end = s->row->used[s->area];
25982 for (i = first; i < end && s->right_overhang > x; ++i)
25983 x += glyphs[i].pixel_width;
25985 k = i;
25988 return k;
25992 /* Return the index of the last glyph following glyph string S that
25993 overwrites S because of its left overhang. Value is negative
25994 if no such glyph is found. */
25996 static int
25997 right_overwriting (struct glyph_string *s)
25999 int i, k, x;
26000 int end = s->row->used[s->area];
26001 struct glyph *glyphs = s->row->glyphs[s->area];
26002 int first = (s->first_glyph - glyphs
26003 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
26005 k = -1;
26006 x = 0;
26007 for (i = first; i < end; ++i)
26009 int left, right;
26010 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
26011 if (x - left < 0)
26012 k = i;
26013 x += glyphs[i].pixel_width;
26016 return k;
26020 /* Set background width of glyph string S. START is the index of the
26021 first glyph following S. LAST_X is the right-most x-position + 1
26022 in the drawing area. */
26024 static void
26025 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
26027 /* If the face of this glyph string has to be drawn to the end of
26028 the drawing area, set S->extends_to_end_of_line_p. */
26030 if (start == s->row->used[s->area]
26031 && ((s->row->fill_line_p
26032 && (s->hl == DRAW_NORMAL_TEXT
26033 || s->hl == DRAW_IMAGE_RAISED
26034 || s->hl == DRAW_IMAGE_SUNKEN))
26035 || s->hl == DRAW_MOUSE_FACE))
26036 s->extends_to_end_of_line_p = true;
26038 /* If S extends its face to the end of the line, set its
26039 background_width to the distance to the right edge of the drawing
26040 area. */
26041 if (s->extends_to_end_of_line_p)
26042 s->background_width = last_x - s->x + 1;
26043 else
26044 s->background_width = s->width;
26048 /* Return glyph string that shares background with glyph string S and
26049 whose `background_width' member has been set. */
26051 static struct glyph_string *
26052 glyph_string_containing_background_width (struct glyph_string *s)
26054 if (s->cmp)
26055 while (s->cmp_from)
26056 s = s->prev;
26058 return s;
26062 /* Compute overhangs and x-positions for glyph string S and its
26063 predecessors, or successors. X is the starting x-position for S.
26064 BACKWARD_P means process predecessors. */
26066 static void
26067 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
26069 if (backward_p)
26071 while (s)
26073 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26074 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26075 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26076 x -= s->width;
26077 s->x = x;
26078 s = s->prev;
26081 else
26083 while (s)
26085 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26086 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26087 s->x = x;
26088 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26089 x += s->width;
26090 s = s->next;
26097 /* The following macros are only called from draw_glyphs below.
26098 They reference the following parameters of that function directly:
26099 `w', `row', `area', and `overlap_p'
26100 as well as the following local variables:
26101 `s', `f', and `hdc' (in W32) */
26103 #ifdef HAVE_NTGUI
26104 /* On W32, silently add local `hdc' variable to argument list of
26105 init_glyph_string. */
26106 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26107 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
26108 #else
26109 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26110 init_glyph_string (s, char2b, w, row, area, start, hl)
26111 #endif
26113 /* Add a glyph string for a stretch glyph to the list of strings
26114 between HEAD and TAIL. START is the index of the stretch glyph in
26115 row area AREA of glyph row ROW. END is the index of the last glyph
26116 in that glyph row area. X is the current output position assigned
26117 to the new glyph string constructed. HL overrides that face of the
26118 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26119 is the right-most x-position of the drawing area. */
26121 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
26122 and below -- keep them on one line. */
26123 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26124 do \
26126 s = alloca (sizeof *s); \
26127 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26128 START = fill_stretch_glyph_string (s, START, END); \
26129 append_glyph_string (&HEAD, &TAIL, s); \
26130 s->x = (X); \
26132 while (false)
26135 /* Add a glyph string for an image glyph to the list of strings
26136 between HEAD and TAIL. START is the index of the image glyph in
26137 row area AREA of glyph row ROW. END is the index of the last glyph
26138 in that glyph row area. X is the current output position assigned
26139 to the new glyph string constructed. HL overrides that face of the
26140 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26141 is the right-most x-position of the drawing area. */
26143 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26144 do \
26146 s = alloca (sizeof *s); \
26147 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26148 fill_image_glyph_string (s); \
26149 append_glyph_string (&HEAD, &TAIL, s); \
26150 ++START; \
26151 s->x = (X); \
26153 while (false)
26155 #ifndef HAVE_XWIDGETS
26156 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26157 eassume (false)
26158 #else
26159 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26160 do \
26162 s = alloca (sizeof *s); \
26163 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26164 fill_xwidget_glyph_string (s); \
26165 append_glyph_string (&(HEAD), &(TAIL), s); \
26166 ++(START); \
26167 s->x = (X); \
26169 while (false)
26170 #endif
26172 /* Add a glyph string for a sequence of character glyphs to the list
26173 of strings between HEAD and TAIL. START is the index of the first
26174 glyph in row area AREA of glyph row ROW that is part of the new
26175 glyph string. END is the index of the last glyph in that glyph row
26176 area. X is the current output position assigned to the new glyph
26177 string constructed. HL overrides that face of the glyph; e.g. it
26178 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
26179 right-most x-position of the drawing area. */
26181 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26182 do \
26184 int face_id; \
26185 XChar2b *char2b; \
26187 face_id = (row)->glyphs[area][START].face_id; \
26189 s = alloca (sizeof *s); \
26190 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
26191 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26192 append_glyph_string (&HEAD, &TAIL, s); \
26193 s->x = (X); \
26194 START = fill_glyph_string (s, face_id, START, END, overlaps); \
26196 while (false)
26199 /* Add a glyph string for a composite sequence to the list of strings
26200 between HEAD and TAIL. START is the index of the first glyph in
26201 row area AREA of glyph row ROW that is part of the new glyph
26202 string. END is the index of the last glyph in that glyph row area.
26203 X is the current output position assigned to the new glyph string
26204 constructed. HL overrides that face of the glyph; e.g. it is
26205 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
26206 x-position of the drawing area. */
26208 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26209 do { \
26210 int face_id = (row)->glyphs[area][START].face_id; \
26211 struct face *base_face = FACE_FROM_ID (f, face_id); \
26212 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
26213 struct composition *cmp = composition_table[cmp_id]; \
26214 XChar2b *char2b; \
26215 struct glyph_string *first_s = NULL; \
26216 int n; \
26218 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
26220 /* Make glyph_strings for each glyph sequence that is drawable by \
26221 the same face, and append them to HEAD/TAIL. */ \
26222 for (n = 0; n < cmp->glyph_len;) \
26224 s = alloca (sizeof *s); \
26225 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26226 append_glyph_string (&(HEAD), &(TAIL), s); \
26227 s->cmp = cmp; \
26228 s->cmp_from = n; \
26229 s->x = (X); \
26230 if (n == 0) \
26231 first_s = s; \
26232 n = fill_composite_glyph_string (s, base_face, overlaps); \
26235 ++START; \
26236 s = first_s; \
26237 } while (false)
26240 /* Add a glyph string for a glyph-string sequence to the list of strings
26241 between HEAD and TAIL. */
26243 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26244 do { \
26245 int face_id; \
26246 XChar2b *char2b; \
26247 Lisp_Object gstring; \
26249 face_id = (row)->glyphs[area][START].face_id; \
26250 gstring = (composition_gstring_from_id \
26251 ((row)->glyphs[area][START].u.cmp.id)); \
26252 s = alloca (sizeof *s); \
26253 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
26254 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26255 append_glyph_string (&(HEAD), &(TAIL), s); \
26256 s->x = (X); \
26257 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
26258 } while (false)
26261 /* Add a glyph string for a sequence of glyphless character's glyphs
26262 to the list of strings between HEAD and TAIL. The meanings of
26263 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
26265 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26266 do \
26268 int face_id; \
26270 face_id = (row)->glyphs[area][START].face_id; \
26272 s = alloca (sizeof *s); \
26273 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26274 append_glyph_string (&HEAD, &TAIL, s); \
26275 s->x = (X); \
26276 START = fill_glyphless_glyph_string (s, face_id, START, END, \
26277 overlaps); \
26279 while (false)
26282 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
26283 of AREA of glyph row ROW on window W between indices START and END.
26284 HL overrides the face for drawing glyph strings, e.g. it is
26285 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
26286 x-positions of the drawing area.
26288 This is an ugly monster macro construct because we must use alloca
26289 to allocate glyph strings (because draw_glyphs can be called
26290 asynchronously). */
26292 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26293 do \
26295 HEAD = TAIL = NULL; \
26296 while (START < END) \
26298 struct glyph *first_glyph = (row)->glyphs[area] + START; \
26299 switch (first_glyph->type) \
26301 case CHAR_GLYPH: \
26302 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
26303 HL, X, LAST_X); \
26304 break; \
26306 case COMPOSITE_GLYPH: \
26307 if (first_glyph->u.cmp.automatic) \
26308 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
26309 HL, X, LAST_X); \
26310 else \
26311 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
26312 HL, X, LAST_X); \
26313 break; \
26315 case STRETCH_GLYPH: \
26316 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
26317 HL, X, LAST_X); \
26318 break; \
26320 case IMAGE_GLYPH: \
26321 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
26322 HL, X, LAST_X); \
26323 break;
26325 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26326 case XWIDGET_GLYPH: \
26327 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
26328 HL, X, LAST_X); \
26329 break;
26331 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
26332 case GLYPHLESS_GLYPH: \
26333 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
26334 HL, X, LAST_X); \
26335 break; \
26337 default: \
26338 emacs_abort (); \
26341 if (s) \
26343 set_glyph_string_background_width (s, START, LAST_X); \
26344 (X) += s->width; \
26347 } while (false)
26350 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26351 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26352 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26353 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
26356 /* Draw glyphs between START and END in AREA of ROW on window W,
26357 starting at x-position X. X is relative to AREA in W. HL is a
26358 face-override with the following meaning:
26360 DRAW_NORMAL_TEXT draw normally
26361 DRAW_CURSOR draw in cursor face
26362 DRAW_MOUSE_FACE draw in mouse face.
26363 DRAW_INVERSE_VIDEO draw in mode line face
26364 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
26365 DRAW_IMAGE_RAISED draw an image with a raised relief around it
26367 If OVERLAPS is non-zero, draw only the foreground of characters and
26368 clip to the physical height of ROW. Non-zero value also defines
26369 the overlapping part to be drawn:
26371 OVERLAPS_PRED overlap with preceding rows
26372 OVERLAPS_SUCC overlap with succeeding rows
26373 OVERLAPS_BOTH overlap with both preceding/succeeding rows
26374 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
26376 Value is the x-position reached, relative to AREA of W. */
26378 static int
26379 draw_glyphs (struct window *w, int x, struct glyph_row *row,
26380 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
26381 enum draw_glyphs_face hl, int overlaps)
26383 struct glyph_string *head, *tail;
26384 struct glyph_string *s;
26385 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
26386 int i, j, x_reached, last_x, area_left = 0;
26387 struct frame *f = XFRAME (WINDOW_FRAME (w));
26388 DECLARE_HDC (hdc);
26390 ALLOCATE_HDC (hdc, f);
26392 /* Let's rather be paranoid than getting a SEGV. */
26393 end = min (end, row->used[area]);
26394 start = clip_to_bounds (0, start, end);
26396 /* Translate X to frame coordinates. Set last_x to the right
26397 end of the drawing area. */
26398 if (row->full_width_p)
26400 /* X is relative to the left edge of W, without scroll bars
26401 or fringes. */
26402 area_left = WINDOW_LEFT_EDGE_X (w);
26403 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
26404 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26406 else
26408 area_left = window_box_left (w, area);
26409 last_x = area_left + window_box_width (w, area);
26411 x += area_left;
26413 /* Build a doubly-linked list of glyph_string structures between
26414 head and tail from what we have to draw. Note that the macro
26415 BUILD_GLYPH_STRINGS will modify its start parameter. That's
26416 the reason we use a separate variable `i'. */
26417 i = start;
26418 USE_SAFE_ALLOCA;
26419 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
26420 if (tail)
26422 s = glyph_string_containing_background_width (tail);
26423 x_reached = s->x + s->background_width;
26425 else
26426 x_reached = x;
26428 /* If there are any glyphs with lbearing < 0 or rbearing > width in
26429 the row, redraw some glyphs in front or following the glyph
26430 strings built above. */
26431 if (head && !overlaps && row->contains_overlapping_glyphs_p)
26433 struct glyph_string *h, *t;
26434 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26435 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
26436 bool check_mouse_face = false;
26437 int dummy_x = 0;
26439 /* If mouse highlighting is on, we may need to draw adjacent
26440 glyphs using mouse-face highlighting. */
26441 if (area == TEXT_AREA && row->mouse_face_p
26442 && hlinfo->mouse_face_beg_row >= 0
26443 && hlinfo->mouse_face_end_row >= 0)
26445 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
26447 if (row_vpos >= hlinfo->mouse_face_beg_row
26448 && row_vpos <= hlinfo->mouse_face_end_row)
26450 check_mouse_face = true;
26451 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
26452 ? hlinfo->mouse_face_beg_col : 0;
26453 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
26454 ? hlinfo->mouse_face_end_col
26455 : row->used[TEXT_AREA];
26459 /* Compute overhangs for all glyph strings. */
26460 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
26461 for (s = head; s; s = s->next)
26462 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
26464 /* Prepend glyph strings for glyphs in front of the first glyph
26465 string that are overwritten because of the first glyph
26466 string's left overhang. The background of all strings
26467 prepended must be drawn because the first glyph string
26468 draws over it. */
26469 i = left_overwritten (head);
26470 if (i >= 0)
26472 enum draw_glyphs_face overlap_hl;
26474 /* If this row contains mouse highlighting, attempt to draw
26475 the overlapped glyphs with the correct highlight. This
26476 code fails if the overlap encompasses more than one glyph
26477 and mouse-highlight spans only some of these glyphs.
26478 However, making it work perfectly involves a lot more
26479 code, and I don't know if the pathological case occurs in
26480 practice, so we'll stick to this for now. --- cyd */
26481 if (check_mouse_face
26482 && mouse_beg_col < start && mouse_end_col > i)
26483 overlap_hl = DRAW_MOUSE_FACE;
26484 else
26485 overlap_hl = DRAW_NORMAL_TEXT;
26487 if (hl != overlap_hl)
26488 clip_head = head;
26489 j = i;
26490 BUILD_GLYPH_STRINGS (j, start, h, t,
26491 overlap_hl, dummy_x, last_x);
26492 start = i;
26493 compute_overhangs_and_x (t, head->x, true);
26494 prepend_glyph_string_lists (&head, &tail, h, t);
26495 if (clip_head == NULL)
26496 clip_head = head;
26499 /* Prepend glyph strings for glyphs in front of the first glyph
26500 string that overwrite that glyph string because of their
26501 right overhang. For these strings, only the foreground must
26502 be drawn, because it draws over the glyph string at `head'.
26503 The background must not be drawn because this would overwrite
26504 right overhangs of preceding glyphs for which no glyph
26505 strings exist. */
26506 i = left_overwriting (head);
26507 if (i >= 0)
26509 enum draw_glyphs_face overlap_hl;
26511 if (check_mouse_face
26512 && mouse_beg_col < start && mouse_end_col > i)
26513 overlap_hl = DRAW_MOUSE_FACE;
26514 else
26515 overlap_hl = DRAW_NORMAL_TEXT;
26517 if (hl == overlap_hl || clip_head == NULL)
26518 clip_head = head;
26519 BUILD_GLYPH_STRINGS (i, start, h, t,
26520 overlap_hl, dummy_x, last_x);
26521 for (s = h; s; s = s->next)
26522 s->background_filled_p = true;
26523 compute_overhangs_and_x (t, head->x, true);
26524 prepend_glyph_string_lists (&head, &tail, h, t);
26527 /* Append glyphs strings for glyphs following the last glyph
26528 string tail that are overwritten by tail. The background of
26529 these strings has to be drawn because tail's foreground draws
26530 over it. */
26531 i = right_overwritten (tail);
26532 if (i >= 0)
26534 enum draw_glyphs_face overlap_hl;
26536 if (check_mouse_face
26537 && mouse_beg_col < i && mouse_end_col > end)
26538 overlap_hl = DRAW_MOUSE_FACE;
26539 else
26540 overlap_hl = DRAW_NORMAL_TEXT;
26542 if (hl != overlap_hl)
26543 clip_tail = tail;
26544 BUILD_GLYPH_STRINGS (end, i, h, t,
26545 overlap_hl, x, last_x);
26546 /* Because BUILD_GLYPH_STRINGS updates the first argument,
26547 we don't have `end = i;' here. */
26548 compute_overhangs_and_x (h, tail->x + tail->width, false);
26549 append_glyph_string_lists (&head, &tail, h, t);
26550 if (clip_tail == NULL)
26551 clip_tail = tail;
26554 /* Append glyph strings for glyphs following the last glyph
26555 string tail that overwrite tail. The foreground of such
26556 glyphs has to be drawn because it writes into the background
26557 of tail. The background must not be drawn because it could
26558 paint over the foreground of following glyphs. */
26559 i = right_overwriting (tail);
26560 if (i >= 0)
26562 enum draw_glyphs_face overlap_hl;
26563 if (check_mouse_face
26564 && mouse_beg_col < i && mouse_end_col > end)
26565 overlap_hl = DRAW_MOUSE_FACE;
26566 else
26567 overlap_hl = DRAW_NORMAL_TEXT;
26569 if (hl == overlap_hl || clip_tail == NULL)
26570 clip_tail = tail;
26571 i++; /* We must include the Ith glyph. */
26572 BUILD_GLYPH_STRINGS (end, i, h, t,
26573 overlap_hl, x, last_x);
26574 for (s = h; s; s = s->next)
26575 s->background_filled_p = true;
26576 compute_overhangs_and_x (h, tail->x + tail->width, false);
26577 append_glyph_string_lists (&head, &tail, h, t);
26579 tail = glyph_string_containing_background_width (tail);
26580 if (clip_tail)
26581 clip_tail = glyph_string_containing_background_width (clip_tail);
26582 if (clip_head || clip_tail)
26583 for (s = head; s; s = s->next)
26585 s->clip_head = clip_head;
26586 s->clip_tail = clip_tail;
26590 /* Draw all strings. */
26591 for (s = head; s; s = s->next)
26592 FRAME_RIF (f)->draw_glyph_string (s);
26594 #ifndef HAVE_NS
26595 /* When focus a sole frame and move horizontally, this clears on_p
26596 causing a failure to erase prev cursor position. */
26597 if (area == TEXT_AREA
26598 && !row->full_width_p
26599 /* When drawing overlapping rows, only the glyph strings'
26600 foreground is drawn, which doesn't erase a cursor
26601 completely. */
26602 && !overlaps)
26604 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
26605 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
26606 : (tail ? tail->x + tail->background_width : x));
26607 x0 -= area_left;
26608 x1 -= area_left;
26610 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
26611 row->y, MATRIX_ROW_BOTTOM_Y (row));
26613 #endif
26615 /* Value is the x-position up to which drawn, relative to AREA of W.
26616 This doesn't include parts drawn because of overhangs. */
26617 if (row->full_width_p)
26618 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
26619 else
26620 x_reached -= area_left;
26622 RELEASE_HDC (hdc, f);
26624 SAFE_FREE ();
26625 return x_reached;
26628 /* Find the first glyph in the run of underlined glyphs preceding the
26629 beginning of glyph string S, and return its font (which could be
26630 NULL). This is needed because that font determines the underline
26631 position and thickness for the entire run of the underlined glyphs.
26632 This function is called from the draw_glyph_string method of GUI
26633 frame's redisplay interface (RIF) when it needs to draw in an
26634 underlined face. */
26635 struct font *
26636 font_for_underline_metrics (struct glyph_string *s)
26638 struct glyph *g0 = s->row->glyphs[s->area], *g;
26640 for (g = s->first_glyph - 1; g >= g0; g--)
26642 struct face *prev_face = FACE_FROM_ID (s->f, g->face_id);
26643 if (!(prev_face && prev_face->underline_p))
26644 break;
26647 /* If preceding glyphs are not underlined, use the font of S. */
26648 if (g == s->first_glyph - 1)
26649 return s->font;
26650 else
26652 /* Otherwise use the font of the last glyph we saw in the above
26653 loop whose face had the underline_p flag set. */
26654 return FACE_FROM_ID (s->f, g[1].face_id)->font;
26658 /* Expand row matrix if too narrow. Don't expand if area
26659 is not present. */
26661 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
26663 if (!it->f->fonts_changed \
26664 && (it->glyph_row->glyphs[area] \
26665 < it->glyph_row->glyphs[area + 1])) \
26667 it->w->ncols_scale_factor++; \
26668 it->f->fonts_changed = true; \
26672 /* Store one glyph for IT->char_to_display in IT->glyph_row.
26673 Called from x_produce_glyphs when IT->glyph_row is non-null. */
26675 static void
26676 append_glyph (struct it *it)
26678 struct glyph *glyph;
26679 enum glyph_row_area area = it->area;
26681 eassert (it->glyph_row);
26682 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
26684 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26685 if (glyph < it->glyph_row->glyphs[area + 1])
26687 /* If the glyph row is reversed, we need to prepend the glyph
26688 rather than append it. */
26689 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26691 struct glyph *g;
26693 /* Make room for the additional glyph. */
26694 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26695 g[1] = *g;
26696 glyph = it->glyph_row->glyphs[area];
26698 glyph->charpos = CHARPOS (it->position);
26699 glyph->object = it->object;
26700 if (it->pixel_width > 0)
26702 eassert (it->pixel_width <= SHRT_MAX);
26703 glyph->pixel_width = it->pixel_width;
26704 glyph->padding_p = false;
26706 else
26708 /* Assure at least 1-pixel width. Otherwise, cursor can't
26709 be displayed correctly. */
26710 glyph->pixel_width = 1;
26711 glyph->padding_p = true;
26713 glyph->ascent = it->ascent;
26714 glyph->descent = it->descent;
26715 glyph->voffset = it->voffset;
26716 glyph->type = CHAR_GLYPH;
26717 glyph->avoid_cursor_p = it->avoid_cursor_p;
26718 glyph->multibyte_p = it->multibyte_p;
26719 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26721 /* In R2L rows, the left and the right box edges need to be
26722 drawn in reverse direction. */
26723 glyph->right_box_line_p = it->start_of_box_run_p;
26724 glyph->left_box_line_p = it->end_of_box_run_p;
26726 else
26728 glyph->left_box_line_p = it->start_of_box_run_p;
26729 glyph->right_box_line_p = it->end_of_box_run_p;
26731 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26732 || it->phys_descent > it->descent);
26733 glyph->glyph_not_available_p = it->glyph_not_available_p;
26734 glyph->face_id = it->face_id;
26735 glyph->u.ch = it->char_to_display;
26736 glyph->slice.img = null_glyph_slice;
26737 glyph->font_type = FONT_TYPE_UNKNOWN;
26738 if (it->bidi_p)
26740 glyph->resolved_level = it->bidi_it.resolved_level;
26741 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26742 glyph->bidi_type = it->bidi_it.type;
26744 else
26746 glyph->resolved_level = 0;
26747 glyph->bidi_type = UNKNOWN_BT;
26749 ++it->glyph_row->used[area];
26751 else
26752 IT_EXPAND_MATRIX_WIDTH (it, area);
26755 /* Store one glyph for the composition IT->cmp_it.id in
26756 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
26757 non-null. */
26759 static void
26760 append_composite_glyph (struct it *it)
26762 struct glyph *glyph;
26763 enum glyph_row_area area = it->area;
26765 eassert (it->glyph_row);
26767 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26768 if (glyph < it->glyph_row->glyphs[area + 1])
26770 /* If the glyph row is reversed, we need to prepend the glyph
26771 rather than append it. */
26772 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
26774 struct glyph *g;
26776 /* Make room for the new glyph. */
26777 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26778 g[1] = *g;
26779 glyph = it->glyph_row->glyphs[it->area];
26781 glyph->charpos = it->cmp_it.charpos;
26782 glyph->object = it->object;
26783 eassert (it->pixel_width <= SHRT_MAX);
26784 glyph->pixel_width = it->pixel_width;
26785 glyph->ascent = it->ascent;
26786 glyph->descent = it->descent;
26787 glyph->voffset = it->voffset;
26788 glyph->type = COMPOSITE_GLYPH;
26789 if (it->cmp_it.ch < 0)
26791 glyph->u.cmp.automatic = false;
26792 glyph->u.cmp.id = it->cmp_it.id;
26793 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
26795 else
26797 glyph->u.cmp.automatic = true;
26798 glyph->u.cmp.id = it->cmp_it.id;
26799 glyph->slice.cmp.from = it->cmp_it.from;
26800 glyph->slice.cmp.to = it->cmp_it.to - 1;
26802 glyph->avoid_cursor_p = it->avoid_cursor_p;
26803 glyph->multibyte_p = it->multibyte_p;
26804 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26806 /* In R2L rows, the left and the right box edges need to be
26807 drawn in reverse direction. */
26808 glyph->right_box_line_p = it->start_of_box_run_p;
26809 glyph->left_box_line_p = it->end_of_box_run_p;
26811 else
26813 glyph->left_box_line_p = it->start_of_box_run_p;
26814 glyph->right_box_line_p = it->end_of_box_run_p;
26816 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26817 || it->phys_descent > it->descent);
26818 glyph->padding_p = false;
26819 glyph->glyph_not_available_p = false;
26820 glyph->face_id = it->face_id;
26821 glyph->font_type = FONT_TYPE_UNKNOWN;
26822 if (it->bidi_p)
26824 glyph->resolved_level = it->bidi_it.resolved_level;
26825 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26826 glyph->bidi_type = it->bidi_it.type;
26828 ++it->glyph_row->used[area];
26830 else
26831 IT_EXPAND_MATRIX_WIDTH (it, area);
26835 /* Change IT->ascent and IT->height according to the setting of
26836 IT->voffset. */
26838 static void
26839 take_vertical_position_into_account (struct it *it)
26841 if (it->voffset)
26843 if (it->voffset < 0)
26844 /* Increase the ascent so that we can display the text higher
26845 in the line. */
26846 it->ascent -= it->voffset;
26847 else
26848 /* Increase the descent so that we can display the text lower
26849 in the line. */
26850 it->descent += it->voffset;
26855 /* Produce glyphs/get display metrics for the image IT is loaded with.
26856 See the description of struct display_iterator in dispextern.h for
26857 an overview of struct display_iterator. */
26859 static void
26860 produce_image_glyph (struct it *it)
26862 struct image *img;
26863 struct face *face;
26864 int glyph_ascent, crop;
26865 struct glyph_slice slice;
26867 eassert (it->what == IT_IMAGE);
26869 face = FACE_FROM_ID (it->f, it->face_id);
26870 /* Make sure X resources of the face is loaded. */
26871 prepare_face_for_display (it->f, face);
26873 if (it->image_id < 0)
26875 /* Fringe bitmap. */
26876 it->ascent = it->phys_ascent = 0;
26877 it->descent = it->phys_descent = 0;
26878 it->pixel_width = 0;
26879 it->nglyphs = 0;
26880 return;
26883 img = IMAGE_FROM_ID (it->f, it->image_id);
26884 /* Make sure X resources of the image is loaded. */
26885 prepare_image_for_display (it->f, img);
26887 slice.x = slice.y = 0;
26888 slice.width = img->width;
26889 slice.height = img->height;
26891 if (INTEGERP (it->slice.x))
26892 slice.x = XINT (it->slice.x);
26893 else if (FLOATP (it->slice.x))
26894 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
26896 if (INTEGERP (it->slice.y))
26897 slice.y = XINT (it->slice.y);
26898 else if (FLOATP (it->slice.y))
26899 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
26901 if (INTEGERP (it->slice.width))
26902 slice.width = XINT (it->slice.width);
26903 else if (FLOATP (it->slice.width))
26904 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
26906 if (INTEGERP (it->slice.height))
26907 slice.height = XINT (it->slice.height);
26908 else if (FLOATP (it->slice.height))
26909 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
26911 if (slice.x >= img->width)
26912 slice.x = img->width;
26913 if (slice.y >= img->height)
26914 slice.y = img->height;
26915 if (slice.x + slice.width >= img->width)
26916 slice.width = img->width - slice.x;
26917 if (slice.y + slice.height > img->height)
26918 slice.height = img->height - slice.y;
26920 if (slice.width == 0 || slice.height == 0)
26921 return;
26923 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
26925 it->descent = slice.height - glyph_ascent;
26926 if (slice.y == 0)
26927 it->descent += img->vmargin;
26928 if (slice.y + slice.height == img->height)
26929 it->descent += img->vmargin;
26930 it->phys_descent = it->descent;
26932 it->pixel_width = slice.width;
26933 if (slice.x == 0)
26934 it->pixel_width += img->hmargin;
26935 if (slice.x + slice.width == img->width)
26936 it->pixel_width += img->hmargin;
26938 /* It's quite possible for images to have an ascent greater than
26939 their height, so don't get confused in that case. */
26940 if (it->descent < 0)
26941 it->descent = 0;
26943 it->nglyphs = 1;
26945 if (face->box != FACE_NO_BOX)
26947 if (face->box_line_width > 0)
26949 if (slice.y == 0)
26950 it->ascent += face->box_line_width;
26951 if (slice.y + slice.height == img->height)
26952 it->descent += face->box_line_width;
26955 if (it->start_of_box_run_p && slice.x == 0)
26956 it->pixel_width += eabs (face->box_line_width);
26957 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
26958 it->pixel_width += eabs (face->box_line_width);
26961 take_vertical_position_into_account (it);
26963 /* Automatically crop wide image glyphs at right edge so we can
26964 draw the cursor on same display row. */
26965 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
26966 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26968 it->pixel_width -= crop;
26969 slice.width -= crop;
26972 if (it->glyph_row)
26974 struct glyph *glyph;
26975 enum glyph_row_area area = it->area;
26977 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26978 if (it->glyph_row->reversed_p)
26980 struct glyph *g;
26982 /* Make room for the new glyph. */
26983 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26984 g[1] = *g;
26985 glyph = it->glyph_row->glyphs[it->area];
26987 if (glyph < it->glyph_row->glyphs[area + 1])
26989 glyph->charpos = CHARPOS (it->position);
26990 glyph->object = it->object;
26991 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26992 glyph->ascent = glyph_ascent;
26993 glyph->descent = it->descent;
26994 glyph->voffset = it->voffset;
26995 glyph->type = IMAGE_GLYPH;
26996 glyph->avoid_cursor_p = it->avoid_cursor_p;
26997 glyph->multibyte_p = it->multibyte_p;
26998 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27000 /* In R2L rows, the left and the right box edges need to be
27001 drawn in reverse direction. */
27002 glyph->right_box_line_p = it->start_of_box_run_p;
27003 glyph->left_box_line_p = it->end_of_box_run_p;
27005 else
27007 glyph->left_box_line_p = it->start_of_box_run_p;
27008 glyph->right_box_line_p = it->end_of_box_run_p;
27010 glyph->overlaps_vertically_p = false;
27011 glyph->padding_p = false;
27012 glyph->glyph_not_available_p = false;
27013 glyph->face_id = it->face_id;
27014 glyph->u.img_id = img->id;
27015 glyph->slice.img = slice;
27016 glyph->font_type = FONT_TYPE_UNKNOWN;
27017 if (it->bidi_p)
27019 glyph->resolved_level = it->bidi_it.resolved_level;
27020 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27021 glyph->bidi_type = it->bidi_it.type;
27023 ++it->glyph_row->used[area];
27025 else
27026 IT_EXPAND_MATRIX_WIDTH (it, area);
27030 static void
27031 produce_xwidget_glyph (struct it *it)
27033 #ifdef HAVE_XWIDGETS
27034 struct xwidget *xw;
27035 int glyph_ascent, crop;
27036 eassert (it->what == IT_XWIDGET);
27038 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27039 /* Make sure X resources of the face is loaded. */
27040 prepare_face_for_display (it->f, face);
27042 xw = it->xwidget;
27043 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
27044 it->descent = xw->height/2;
27045 it->phys_descent = it->descent;
27046 it->pixel_width = xw->width;
27047 /* It's quite possible for images to have an ascent greater than
27048 their height, so don't get confused in that case. */
27049 if (it->descent < 0)
27050 it->descent = 0;
27052 it->nglyphs = 1;
27054 if (face->box != FACE_NO_BOX)
27056 if (face->box_line_width > 0)
27058 it->ascent += face->box_line_width;
27059 it->descent += face->box_line_width;
27062 if (it->start_of_box_run_p)
27063 it->pixel_width += eabs (face->box_line_width);
27064 it->pixel_width += eabs (face->box_line_width);
27067 take_vertical_position_into_account (it);
27069 /* Automatically crop wide image glyphs at right edge so we can
27070 draw the cursor on same display row. */
27071 crop = it->pixel_width - (it->last_visible_x - it->current_x);
27072 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
27073 it->pixel_width -= crop;
27075 if (it->glyph_row)
27077 enum glyph_row_area area = it->area;
27078 struct glyph *glyph
27079 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27081 if (it->glyph_row->reversed_p)
27083 struct glyph *g;
27085 /* Make room for the new glyph. */
27086 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27087 g[1] = *g;
27088 glyph = it->glyph_row->glyphs[it->area];
27090 if (glyph < it->glyph_row->glyphs[area + 1])
27092 glyph->charpos = CHARPOS (it->position);
27093 glyph->object = it->object;
27094 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
27095 glyph->ascent = glyph_ascent;
27096 glyph->descent = it->descent;
27097 glyph->voffset = it->voffset;
27098 glyph->type = XWIDGET_GLYPH;
27099 glyph->avoid_cursor_p = it->avoid_cursor_p;
27100 glyph->multibyte_p = it->multibyte_p;
27101 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27103 /* In R2L rows, the left and the right box edges need to be
27104 drawn in reverse direction. */
27105 glyph->right_box_line_p = it->start_of_box_run_p;
27106 glyph->left_box_line_p = it->end_of_box_run_p;
27108 else
27110 glyph->left_box_line_p = it->start_of_box_run_p;
27111 glyph->right_box_line_p = it->end_of_box_run_p;
27113 glyph->overlaps_vertically_p = 0;
27114 glyph->padding_p = 0;
27115 glyph->glyph_not_available_p = 0;
27116 glyph->face_id = it->face_id;
27117 glyph->u.xwidget = it->xwidget;
27118 glyph->font_type = FONT_TYPE_UNKNOWN;
27119 if (it->bidi_p)
27121 glyph->resolved_level = it->bidi_it.resolved_level;
27122 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27123 glyph->bidi_type = it->bidi_it.type;
27125 ++it->glyph_row->used[area];
27127 else
27128 IT_EXPAND_MATRIX_WIDTH (it, area);
27130 #endif
27133 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
27134 of the glyph, WIDTH and HEIGHT are the width and height of the
27135 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
27137 static void
27138 append_stretch_glyph (struct it *it, Lisp_Object object,
27139 int width, int height, int ascent)
27141 struct glyph *glyph;
27142 enum glyph_row_area area = it->area;
27144 eassert (ascent >= 0 && ascent <= height);
27146 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27147 if (glyph < it->glyph_row->glyphs[area + 1])
27149 /* If the glyph row is reversed, we need to prepend the glyph
27150 rather than append it. */
27151 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27153 struct glyph *g;
27155 /* Make room for the additional glyph. */
27156 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27157 g[1] = *g;
27158 glyph = it->glyph_row->glyphs[area];
27160 /* Decrease the width of the first glyph of the row that
27161 begins before first_visible_x (e.g., due to hscroll).
27162 This is so the overall width of the row becomes smaller
27163 by the scroll amount, and the stretch glyph appended by
27164 extend_face_to_end_of_line will be wider, to shift the
27165 row glyphs to the right. (In L2R rows, the corresponding
27166 left-shift effect is accomplished by setting row->x to a
27167 negative value, which won't work with R2L rows.)
27169 This must leave us with a positive value of WIDTH, since
27170 otherwise the call to move_it_in_display_line_to at the
27171 beginning of display_line would have got past the entire
27172 first glyph, and then it->current_x would have been
27173 greater or equal to it->first_visible_x. */
27174 if (it->current_x < it->first_visible_x)
27175 width -= it->first_visible_x - it->current_x;
27176 eassert (width > 0);
27178 glyph->charpos = CHARPOS (it->position);
27179 glyph->object = object;
27180 /* FIXME: It would be better to use TYPE_MAX here, but
27181 __typeof__ is not portable enough... */
27182 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
27183 glyph->ascent = ascent;
27184 glyph->descent = height - ascent;
27185 glyph->voffset = it->voffset;
27186 glyph->type = STRETCH_GLYPH;
27187 glyph->avoid_cursor_p = it->avoid_cursor_p;
27188 glyph->multibyte_p = it->multibyte_p;
27189 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27191 /* In R2L rows, the left and the right box edges need to be
27192 drawn in reverse direction. */
27193 glyph->right_box_line_p = it->start_of_box_run_p;
27194 glyph->left_box_line_p = it->end_of_box_run_p;
27196 else
27198 glyph->left_box_line_p = it->start_of_box_run_p;
27199 glyph->right_box_line_p = it->end_of_box_run_p;
27201 glyph->overlaps_vertically_p = false;
27202 glyph->padding_p = false;
27203 glyph->glyph_not_available_p = false;
27204 glyph->face_id = it->face_id;
27205 glyph->u.stretch.ascent = ascent;
27206 glyph->u.stretch.height = height;
27207 glyph->slice.img = null_glyph_slice;
27208 glyph->font_type = FONT_TYPE_UNKNOWN;
27209 if (it->bidi_p)
27211 glyph->resolved_level = it->bidi_it.resolved_level;
27212 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27213 glyph->bidi_type = it->bidi_it.type;
27215 else
27217 glyph->resolved_level = 0;
27218 glyph->bidi_type = UNKNOWN_BT;
27220 ++it->glyph_row->used[area];
27222 else
27223 IT_EXPAND_MATRIX_WIDTH (it, area);
27226 #endif /* HAVE_WINDOW_SYSTEM */
27228 /* Produce a stretch glyph for iterator IT. IT->object is the value
27229 of the glyph property displayed. The value must be a list
27230 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
27231 being recognized:
27233 1. `:width WIDTH' specifies that the space should be WIDTH *
27234 canonical char width wide. WIDTH may be an integer or floating
27235 point number.
27237 2. `:relative-width FACTOR' specifies that the width of the stretch
27238 should be computed from the width of the first character having the
27239 `glyph' property, and should be FACTOR times that width.
27241 3. `:align-to HPOS' specifies that the space should be wide enough
27242 to reach HPOS, a value in canonical character units.
27244 Exactly one of the above pairs must be present.
27246 4. `:height HEIGHT' specifies that the height of the stretch produced
27247 should be HEIGHT, measured in canonical character units.
27249 5. `:relative-height FACTOR' specifies that the height of the
27250 stretch should be FACTOR times the height of the characters having
27251 the glyph property.
27253 Either none or exactly one of 4 or 5 must be present.
27255 6. `:ascent ASCENT' specifies that ASCENT percent of the height
27256 of the stretch should be used for the ascent of the stretch.
27257 ASCENT must be in the range 0 <= ASCENT <= 100. */
27259 void
27260 produce_stretch_glyph (struct it *it)
27262 /* (space :width WIDTH :height HEIGHT ...) */
27263 Lisp_Object prop, plist;
27264 int width = 0, height = 0, align_to = -1;
27265 bool zero_width_ok_p = false;
27266 double tem;
27267 struct font *font = NULL;
27269 #ifdef HAVE_WINDOW_SYSTEM
27270 int ascent = 0;
27271 bool zero_height_ok_p = false;
27273 if (FRAME_WINDOW_P (it->f))
27275 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27276 font = face->font ? face->font : FRAME_FONT (it->f);
27277 prepare_face_for_display (it->f, face);
27279 #endif
27281 /* List should start with `space'. */
27282 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
27283 plist = XCDR (it->object);
27285 /* Compute the width of the stretch. */
27286 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
27287 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
27289 /* Absolute width `:width WIDTH' specified and valid. */
27290 zero_width_ok_p = true;
27291 width = (int)tem;
27293 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
27295 /* Relative width `:relative-width FACTOR' specified and valid.
27296 Compute the width of the characters having the `glyph'
27297 property. */
27298 struct it it2;
27299 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
27301 it2 = *it;
27302 if (it->multibyte_p)
27303 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
27304 else
27306 it2.c = it2.char_to_display = *p, it2.len = 1;
27307 if (! ASCII_CHAR_P (it2.c))
27308 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
27311 it2.glyph_row = NULL;
27312 it2.what = IT_CHARACTER;
27313 PRODUCE_GLYPHS (&it2);
27314 width = NUMVAL (prop) * it2.pixel_width;
27316 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
27317 && calc_pixel_width_or_height (&tem, it, prop, font, true,
27318 &align_to))
27320 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
27321 align_to = (align_to < 0
27323 : align_to - window_box_left_offset (it->w, TEXT_AREA));
27324 else if (align_to < 0)
27325 align_to = window_box_left_offset (it->w, TEXT_AREA);
27326 width = max (0, (int)tem + align_to - it->current_x);
27327 zero_width_ok_p = true;
27329 else
27330 /* Nothing specified -> width defaults to canonical char width. */
27331 width = FRAME_COLUMN_WIDTH (it->f);
27333 if (width <= 0 && (width < 0 || !zero_width_ok_p))
27334 width = 1;
27336 #ifdef HAVE_WINDOW_SYSTEM
27337 /* Compute height. */
27338 if (FRAME_WINDOW_P (it->f))
27340 int default_height = normal_char_height (font, ' ');
27342 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
27343 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27345 height = (int)tem;
27346 zero_height_ok_p = true;
27348 else if (prop = Fplist_get (plist, QCrelative_height),
27349 NUMVAL (prop) > 0)
27350 height = default_height * NUMVAL (prop);
27351 else
27352 height = default_height;
27354 if (height <= 0 && (height < 0 || !zero_height_ok_p))
27355 height = 1;
27357 /* Compute percentage of height used for ascent. If
27358 `:ascent ASCENT' is present and valid, use that. Otherwise,
27359 derive the ascent from the font in use. */
27360 if (prop = Fplist_get (plist, QCascent),
27361 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
27362 ascent = height * NUMVAL (prop) / 100.0;
27363 else if (!NILP (prop)
27364 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27365 ascent = min (max (0, (int)tem), height);
27366 else
27367 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
27369 else
27370 #endif /* HAVE_WINDOW_SYSTEM */
27371 height = 1;
27373 if (width > 0 && it->line_wrap != TRUNCATE
27374 && it->current_x + width > it->last_visible_x)
27376 width = it->last_visible_x - it->current_x;
27377 #ifdef HAVE_WINDOW_SYSTEM
27378 /* Subtract one more pixel from the stretch width, but only on
27379 GUI frames, since on a TTY each glyph is one "pixel" wide. */
27380 width -= FRAME_WINDOW_P (it->f);
27381 #endif
27384 if (width > 0 && height > 0 && it->glyph_row)
27386 Lisp_Object o_object = it->object;
27387 Lisp_Object object = it->stack[it->sp - 1].string;
27388 int n = width;
27390 if (!STRINGP (object))
27391 object = it->w->contents;
27392 #ifdef HAVE_WINDOW_SYSTEM
27393 if (FRAME_WINDOW_P (it->f))
27394 append_stretch_glyph (it, object, width, height, ascent);
27395 else
27396 #endif
27398 it->object = object;
27399 it->char_to_display = ' ';
27400 it->pixel_width = it->len = 1;
27401 while (n--)
27402 tty_append_glyph (it);
27403 it->object = o_object;
27407 it->pixel_width = width;
27408 #ifdef HAVE_WINDOW_SYSTEM
27409 if (FRAME_WINDOW_P (it->f))
27411 it->ascent = it->phys_ascent = ascent;
27412 it->descent = it->phys_descent = height - it->ascent;
27413 it->nglyphs = width > 0 && height > 0;
27414 take_vertical_position_into_account (it);
27416 else
27417 #endif
27418 it->nglyphs = width;
27421 /* Get information about special display element WHAT in an
27422 environment described by IT. WHAT is one of IT_TRUNCATION or
27423 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
27424 non-null glyph_row member. This function ensures that fields like
27425 face_id, c, len of IT are left untouched. */
27427 static void
27428 produce_special_glyphs (struct it *it, enum display_element_type what)
27430 struct it temp_it;
27431 Lisp_Object gc;
27432 GLYPH glyph;
27434 temp_it = *it;
27435 temp_it.object = Qnil;
27436 memset (&temp_it.current, 0, sizeof temp_it.current);
27438 if (what == IT_CONTINUATION)
27440 /* Continuation glyph. For R2L lines, we mirror it by hand. */
27441 if (it->bidi_it.paragraph_dir == R2L)
27442 SET_GLYPH_FROM_CHAR (glyph, '/');
27443 else
27444 SET_GLYPH_FROM_CHAR (glyph, '\\');
27445 if (it->dp
27446 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27448 /* FIXME: Should we mirror GC for R2L lines? */
27449 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27450 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27453 else if (what == IT_TRUNCATION)
27455 /* Truncation glyph. */
27456 SET_GLYPH_FROM_CHAR (glyph, '$');
27457 if (it->dp
27458 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27460 /* FIXME: Should we mirror GC for R2L lines? */
27461 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27462 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27465 else
27466 emacs_abort ();
27468 #ifdef HAVE_WINDOW_SYSTEM
27469 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
27470 is turned off, we precede the truncation/continuation glyphs by a
27471 stretch glyph whose width is computed such that these special
27472 glyphs are aligned at the window margin, even when very different
27473 fonts are used in different glyph rows. */
27474 if (FRAME_WINDOW_P (temp_it.f)
27475 /* init_iterator calls this with it->glyph_row == NULL, and it
27476 wants only the pixel width of the truncation/continuation
27477 glyphs. */
27478 && temp_it.glyph_row
27479 /* insert_left_trunc_glyphs calls us at the beginning of the
27480 row, and it has its own calculation of the stretch glyph
27481 width. */
27482 && temp_it.glyph_row->used[TEXT_AREA] > 0
27483 && (temp_it.glyph_row->reversed_p
27484 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
27485 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
27487 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
27489 if (stretch_width > 0)
27491 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
27492 struct font *font =
27493 face->font ? face->font : FRAME_FONT (temp_it.f);
27494 int stretch_ascent =
27495 (((temp_it.ascent + temp_it.descent)
27496 * FONT_BASE (font)) / FONT_HEIGHT (font));
27498 append_stretch_glyph (&temp_it, Qnil, stretch_width,
27499 temp_it.ascent + temp_it.descent,
27500 stretch_ascent);
27503 #endif
27505 temp_it.dp = NULL;
27506 temp_it.what = IT_CHARACTER;
27507 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
27508 temp_it.face_id = GLYPH_FACE (glyph);
27509 temp_it.len = CHAR_BYTES (temp_it.c);
27511 PRODUCE_GLYPHS (&temp_it);
27512 it->pixel_width = temp_it.pixel_width;
27513 it->nglyphs = temp_it.nglyphs;
27516 #ifdef HAVE_WINDOW_SYSTEM
27518 /* Calculate line-height and line-spacing properties.
27519 An integer value specifies explicit pixel value.
27520 A float value specifies relative value to current face height.
27521 A cons (float . face-name) specifies relative value to
27522 height of specified face font.
27524 Returns height in pixels, or nil. */
27526 static Lisp_Object
27527 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
27528 int boff, bool override)
27530 Lisp_Object face_name = Qnil;
27531 int ascent, descent, height;
27533 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
27534 return val;
27536 if (CONSP (val))
27538 face_name = XCAR (val);
27539 val = XCDR (val);
27540 if (!NUMBERP (val))
27541 val = make_number (1);
27542 if (NILP (face_name))
27544 height = it->ascent + it->descent;
27545 goto scale;
27549 if (NILP (face_name))
27551 font = FRAME_FONT (it->f);
27552 boff = FRAME_BASELINE_OFFSET (it->f);
27554 else if (EQ (face_name, Qt))
27556 override = false;
27558 else
27560 int face_id;
27561 struct face *face;
27563 face_id = lookup_named_face (it->f, face_name, false);
27564 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
27565 if (face == NULL || ((font = face->font) == NULL))
27566 return make_number (-1);
27567 boff = font->baseline_offset;
27568 if (font->vertical_centering)
27569 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27572 normal_char_ascent_descent (font, -1, &ascent, &descent);
27574 if (override)
27576 it->override_ascent = ascent;
27577 it->override_descent = descent;
27578 it->override_boff = boff;
27581 height = ascent + descent;
27583 scale:
27584 if (FLOATP (val))
27585 height = (int)(XFLOAT_DATA (val) * height);
27586 else if (INTEGERP (val))
27587 height *= XINT (val);
27589 return make_number (height);
27593 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
27594 is a face ID to be used for the glyph. FOR_NO_FONT is true if
27595 and only if this is for a character for which no font was found.
27597 If the display method (it->glyphless_method) is
27598 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
27599 length of the acronym or the hexadecimal string, UPPER_XOFF and
27600 UPPER_YOFF are pixel offsets for the upper part of the string,
27601 LOWER_XOFF and LOWER_YOFF are for the lower part.
27603 For the other display methods, LEN through LOWER_YOFF are zero. */
27605 static void
27606 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
27607 short upper_xoff, short upper_yoff,
27608 short lower_xoff, short lower_yoff)
27610 struct glyph *glyph;
27611 enum glyph_row_area area = it->area;
27613 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27614 if (glyph < it->glyph_row->glyphs[area + 1])
27616 /* If the glyph row is reversed, we need to prepend the glyph
27617 rather than append it. */
27618 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27620 struct glyph *g;
27622 /* Make room for the additional glyph. */
27623 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27624 g[1] = *g;
27625 glyph = it->glyph_row->glyphs[area];
27627 glyph->charpos = CHARPOS (it->position);
27628 glyph->object = it->object;
27629 eassert (it->pixel_width <= SHRT_MAX);
27630 glyph->pixel_width = it->pixel_width;
27631 glyph->ascent = it->ascent;
27632 glyph->descent = it->descent;
27633 glyph->voffset = it->voffset;
27634 glyph->type = GLYPHLESS_GLYPH;
27635 glyph->u.glyphless.method = it->glyphless_method;
27636 glyph->u.glyphless.for_no_font = for_no_font;
27637 glyph->u.glyphless.len = len;
27638 glyph->u.glyphless.ch = it->c;
27639 glyph->slice.glyphless.upper_xoff = upper_xoff;
27640 glyph->slice.glyphless.upper_yoff = upper_yoff;
27641 glyph->slice.glyphless.lower_xoff = lower_xoff;
27642 glyph->slice.glyphless.lower_yoff = lower_yoff;
27643 glyph->avoid_cursor_p = it->avoid_cursor_p;
27644 glyph->multibyte_p = it->multibyte_p;
27645 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27647 /* In R2L rows, the left and the right box edges need to be
27648 drawn in reverse direction. */
27649 glyph->right_box_line_p = it->start_of_box_run_p;
27650 glyph->left_box_line_p = it->end_of_box_run_p;
27652 else
27654 glyph->left_box_line_p = it->start_of_box_run_p;
27655 glyph->right_box_line_p = it->end_of_box_run_p;
27657 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
27658 || it->phys_descent > it->descent);
27659 glyph->padding_p = false;
27660 glyph->glyph_not_available_p = false;
27661 glyph->face_id = face_id;
27662 glyph->font_type = FONT_TYPE_UNKNOWN;
27663 if (it->bidi_p)
27665 glyph->resolved_level = it->bidi_it.resolved_level;
27666 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27667 glyph->bidi_type = it->bidi_it.type;
27669 ++it->glyph_row->used[area];
27671 else
27672 IT_EXPAND_MATRIX_WIDTH (it, area);
27676 /* Produce a glyph for a glyphless character for iterator IT.
27677 IT->glyphless_method specifies which method to use for displaying
27678 the character. See the description of enum
27679 glyphless_display_method in dispextern.h for the detail.
27681 FOR_NO_FONT is true if and only if this is for a character for
27682 which no font was found. ACRONYM, if non-nil, is an acronym string
27683 for the character. */
27685 static void
27686 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
27688 int face_id;
27689 struct face *face;
27690 struct font *font;
27691 int base_width, base_height, width, height;
27692 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
27693 int len;
27695 /* Get the metrics of the base font. We always refer to the current
27696 ASCII face. */
27697 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
27698 font = face->font ? face->font : FRAME_FONT (it->f);
27699 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
27700 it->ascent += font->baseline_offset;
27701 it->descent -= font->baseline_offset;
27702 base_height = it->ascent + it->descent;
27703 base_width = font->average_width;
27705 face_id = merge_glyphless_glyph_face (it);
27707 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
27709 it->pixel_width = THIN_SPACE_WIDTH;
27710 len = 0;
27711 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27713 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
27715 width = CHARACTER_WIDTH (it->c);
27716 if (width == 0)
27717 width = 1;
27718 else if (width > 4)
27719 width = 4;
27720 it->pixel_width = base_width * width;
27721 len = 0;
27722 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27724 else
27726 char buf[7];
27727 const char *str;
27728 unsigned int code[6];
27729 int upper_len;
27730 int ascent, descent;
27731 struct font_metrics metrics_upper, metrics_lower;
27733 face = FACE_FROM_ID (it->f, face_id);
27734 font = face->font ? face->font : FRAME_FONT (it->f);
27735 prepare_face_for_display (it->f, face);
27737 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
27739 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
27740 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
27741 if (CONSP (acronym))
27742 acronym = XCAR (acronym);
27743 str = STRINGP (acronym) ? SSDATA (acronym) : "";
27745 else
27747 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
27748 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
27749 str = buf;
27751 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
27752 code[len] = font->driver->encode_char (font, str[len]);
27753 upper_len = (len + 1) / 2;
27754 font->driver->text_extents (font, code, upper_len,
27755 &metrics_upper);
27756 font->driver->text_extents (font, code + upper_len, len - upper_len,
27757 &metrics_lower);
27761 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
27762 width = max (metrics_upper.width, metrics_lower.width) + 4;
27763 upper_xoff = upper_yoff = 2; /* the typical case */
27764 if (base_width >= width)
27766 /* Align the upper to the left, the lower to the right. */
27767 it->pixel_width = base_width;
27768 lower_xoff = base_width - 2 - metrics_lower.width;
27770 else
27772 /* Center the shorter one. */
27773 it->pixel_width = width;
27774 if (metrics_upper.width >= metrics_lower.width)
27775 lower_xoff = (width - metrics_lower.width) / 2;
27776 else
27778 /* FIXME: This code doesn't look right. It formerly was
27779 missing the "lower_xoff = 0;", which couldn't have
27780 been right since it left lower_xoff uninitialized. */
27781 lower_xoff = 0;
27782 upper_xoff = (width - metrics_upper.width) / 2;
27786 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
27787 top, bottom, and between upper and lower strings. */
27788 height = (metrics_upper.ascent + metrics_upper.descent
27789 + metrics_lower.ascent + metrics_lower.descent) + 5;
27790 /* Center vertically.
27791 H:base_height, D:base_descent
27792 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
27794 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
27795 descent = D - H/2 + h/2;
27796 lower_yoff = descent - 2 - ld;
27797 upper_yoff = lower_yoff - la - 1 - ud; */
27798 ascent = - (it->descent - (base_height + height + 1) / 2);
27799 descent = it->descent - (base_height - height) / 2;
27800 lower_yoff = descent - 2 - metrics_lower.descent;
27801 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
27802 - metrics_upper.descent);
27803 /* Don't make the height shorter than the base height. */
27804 if (height > base_height)
27806 it->ascent = ascent;
27807 it->descent = descent;
27811 it->phys_ascent = it->ascent;
27812 it->phys_descent = it->descent;
27813 if (it->glyph_row)
27814 append_glyphless_glyph (it, face_id, for_no_font, len,
27815 upper_xoff, upper_yoff,
27816 lower_xoff, lower_yoff);
27817 it->nglyphs = 1;
27818 take_vertical_position_into_account (it);
27822 /* RIF:
27823 Produce glyphs/get display metrics for the display element IT is
27824 loaded with. See the description of struct it in dispextern.h
27825 for an overview of struct it. */
27827 void
27828 x_produce_glyphs (struct it *it)
27830 int extra_line_spacing = it->extra_line_spacing;
27832 it->glyph_not_available_p = false;
27834 if (it->what == IT_CHARACTER)
27836 XChar2b char2b;
27837 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27838 struct font *font = face->font;
27839 struct font_metrics *pcm = NULL;
27840 int boff; /* Baseline offset. */
27842 if (font == NULL)
27844 /* When no suitable font is found, display this character by
27845 the method specified in the first extra slot of
27846 Vglyphless_char_display. */
27847 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
27849 eassert (it->what == IT_GLYPHLESS);
27850 produce_glyphless_glyph (it, true,
27851 STRINGP (acronym) ? acronym : Qnil);
27852 goto done;
27855 boff = font->baseline_offset;
27856 if (font->vertical_centering)
27857 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27859 if (it->char_to_display != '\n' && it->char_to_display != '\t')
27861 it->nglyphs = 1;
27863 if (it->override_ascent >= 0)
27865 it->ascent = it->override_ascent;
27866 it->descent = it->override_descent;
27867 boff = it->override_boff;
27869 else
27871 it->ascent = FONT_BASE (font) + boff;
27872 it->descent = FONT_DESCENT (font) - boff;
27875 if (get_char_glyph_code (it->char_to_display, font, &char2b))
27877 pcm = get_per_char_metric (font, &char2b);
27878 if (pcm->width == 0
27879 && pcm->rbearing == 0 && pcm->lbearing == 0)
27880 pcm = NULL;
27883 if (pcm)
27885 it->phys_ascent = pcm->ascent + boff;
27886 it->phys_descent = pcm->descent - boff;
27887 it->pixel_width = pcm->width;
27888 /* Don't use font-global values for ascent and descent
27889 if they result in an exceedingly large line height. */
27890 if (it->override_ascent < 0)
27892 if (FONT_TOO_HIGH (font))
27894 it->ascent = it->phys_ascent;
27895 it->descent = it->phys_descent;
27896 /* These limitations are enforced by an
27897 assertion near the end of this function. */
27898 if (it->ascent < 0)
27899 it->ascent = 0;
27900 if (it->descent < 0)
27901 it->descent = 0;
27905 else
27907 it->glyph_not_available_p = true;
27908 it->phys_ascent = it->ascent;
27909 it->phys_descent = it->descent;
27910 it->pixel_width = font->space_width;
27913 if (it->constrain_row_ascent_descent_p)
27915 if (it->descent > it->max_descent)
27917 it->ascent += it->descent - it->max_descent;
27918 it->descent = it->max_descent;
27920 if (it->ascent > it->max_ascent)
27922 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27923 it->ascent = it->max_ascent;
27925 it->phys_ascent = min (it->phys_ascent, it->ascent);
27926 it->phys_descent = min (it->phys_descent, it->descent);
27927 extra_line_spacing = 0;
27930 /* If this is a space inside a region of text with
27931 `space-width' property, change its width. */
27932 bool stretched_p
27933 = it->char_to_display == ' ' && !NILP (it->space_width);
27934 if (stretched_p)
27935 it->pixel_width *= XFLOATINT (it->space_width);
27937 /* If face has a box, add the box thickness to the character
27938 height. If character has a box line to the left and/or
27939 right, add the box line width to the character's width. */
27940 if (face->box != FACE_NO_BOX)
27942 int thick = face->box_line_width;
27944 if (thick > 0)
27946 it->ascent += thick;
27947 it->descent += thick;
27949 else
27950 thick = -thick;
27952 if (it->start_of_box_run_p)
27953 it->pixel_width += thick;
27954 if (it->end_of_box_run_p)
27955 it->pixel_width += thick;
27958 /* If face has an overline, add the height of the overline
27959 (1 pixel) and a 1 pixel margin to the character height. */
27960 if (face->overline_p)
27961 it->ascent += overline_margin;
27963 if (it->constrain_row_ascent_descent_p)
27965 if (it->ascent > it->max_ascent)
27966 it->ascent = it->max_ascent;
27967 if (it->descent > it->max_descent)
27968 it->descent = it->max_descent;
27971 take_vertical_position_into_account (it);
27973 /* If we have to actually produce glyphs, do it. */
27974 if (it->glyph_row)
27976 if (stretched_p)
27978 /* Translate a space with a `space-width' property
27979 into a stretch glyph. */
27980 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
27981 / FONT_HEIGHT (font));
27982 append_stretch_glyph (it, it->object, it->pixel_width,
27983 it->ascent + it->descent, ascent);
27985 else
27986 append_glyph (it);
27988 /* If characters with lbearing or rbearing are displayed
27989 in this line, record that fact in a flag of the
27990 glyph row. This is used to optimize X output code. */
27991 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
27992 it->glyph_row->contains_overlapping_glyphs_p = true;
27994 if (! stretched_p && it->pixel_width == 0)
27995 /* We assure that all visible glyphs have at least 1-pixel
27996 width. */
27997 it->pixel_width = 1;
27999 else if (it->char_to_display == '\n')
28001 /* A newline has no width, but we need the height of the
28002 line. But if previous part of the line sets a height,
28003 don't increase that height. */
28005 Lisp_Object height;
28006 Lisp_Object total_height = Qnil;
28008 it->override_ascent = -1;
28009 it->pixel_width = 0;
28010 it->nglyphs = 0;
28012 height = get_it_property (it, Qline_height);
28013 /* Split (line-height total-height) list. */
28014 if (CONSP (height)
28015 && CONSP (XCDR (height))
28016 && NILP (XCDR (XCDR (height))))
28018 total_height = XCAR (XCDR (height));
28019 height = XCAR (height);
28021 height = calc_line_height_property (it, height, font, boff, true);
28023 if (it->override_ascent >= 0)
28025 it->ascent = it->override_ascent;
28026 it->descent = it->override_descent;
28027 boff = it->override_boff;
28029 else
28031 if (FONT_TOO_HIGH (font))
28033 it->ascent = font->pixel_size + boff - 1;
28034 it->descent = -boff + 1;
28035 if (it->descent < 0)
28036 it->descent = 0;
28038 else
28040 it->ascent = FONT_BASE (font) + boff;
28041 it->descent = FONT_DESCENT (font) - boff;
28045 if (EQ (height, Qt))
28047 if (it->descent > it->max_descent)
28049 it->ascent += it->descent - it->max_descent;
28050 it->descent = it->max_descent;
28052 if (it->ascent > it->max_ascent)
28054 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
28055 it->ascent = it->max_ascent;
28057 it->phys_ascent = min (it->phys_ascent, it->ascent);
28058 it->phys_descent = min (it->phys_descent, it->descent);
28059 it->constrain_row_ascent_descent_p = true;
28060 extra_line_spacing = 0;
28062 else
28064 Lisp_Object spacing;
28066 it->phys_ascent = it->ascent;
28067 it->phys_descent = it->descent;
28069 if ((it->max_ascent > 0 || it->max_descent > 0)
28070 && face->box != FACE_NO_BOX
28071 && face->box_line_width > 0)
28073 it->ascent += face->box_line_width;
28074 it->descent += face->box_line_width;
28076 if (!NILP (height)
28077 && XINT (height) > it->ascent + it->descent)
28078 it->ascent = XINT (height) - it->descent;
28080 if (!NILP (total_height))
28081 spacing = calc_line_height_property (it, total_height, font,
28082 boff, false);
28083 else
28085 spacing = get_it_property (it, Qline_spacing);
28086 spacing = calc_line_height_property (it, spacing, font,
28087 boff, false);
28089 if (INTEGERP (spacing))
28091 extra_line_spacing = XINT (spacing);
28092 if (!NILP (total_height))
28093 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
28097 else /* i.e. (it->char_to_display == '\t') */
28099 if (font->space_width > 0)
28101 int tab_width = it->tab_width * font->space_width;
28102 int x = it->current_x + it->continuation_lines_width;
28103 int x0 = x;
28104 /* Adjust for line numbers, if needed. */
28105 if (!NILP (Vdisplay_line_numbers) && x0 >= it->lnum_pixel_width)
28106 x -= it->lnum_pixel_width;
28107 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
28109 /* If the distance from the current position to the next tab
28110 stop is less than a space character width, use the
28111 tab stop after that. */
28112 if (next_tab_x - x < font->space_width)
28113 next_tab_x += tab_width;
28114 if (!NILP (Vdisplay_line_numbers) && x0 >= it->lnum_pixel_width)
28115 next_tab_x += (it->lnum_pixel_width
28116 - ((it->w->hscroll * font->space_width)
28117 % tab_width));
28119 it->pixel_width = next_tab_x - x0;
28120 it->nglyphs = 1;
28121 if (FONT_TOO_HIGH (font))
28123 if (get_char_glyph_code (' ', font, &char2b))
28125 pcm = get_per_char_metric (font, &char2b);
28126 if (pcm->width == 0
28127 && pcm->rbearing == 0 && pcm->lbearing == 0)
28128 pcm = NULL;
28131 if (pcm)
28133 it->ascent = pcm->ascent + boff;
28134 it->descent = pcm->descent - boff;
28136 else
28138 it->ascent = font->pixel_size + boff - 1;
28139 it->descent = -boff + 1;
28141 if (it->ascent < 0)
28142 it->ascent = 0;
28143 if (it->descent < 0)
28144 it->descent = 0;
28146 else
28148 it->ascent = FONT_BASE (font) + boff;
28149 it->descent = FONT_DESCENT (font) - boff;
28151 it->phys_ascent = it->ascent;
28152 it->phys_descent = it->descent;
28154 if (it->glyph_row)
28156 append_stretch_glyph (it, it->object, it->pixel_width,
28157 it->ascent + it->descent, it->ascent);
28160 else
28162 it->pixel_width = 0;
28163 it->nglyphs = 1;
28167 if (FONT_TOO_HIGH (font))
28169 int font_ascent, font_descent;
28171 /* For very large fonts, where we ignore the declared font
28172 dimensions, and go by per-character metrics instead,
28173 don't let the row ascent and descent values (and the row
28174 height computed from them) be smaller than the "normal"
28175 character metrics. This avoids unpleasant effects
28176 whereby lines on display would change their height
28177 depending on which characters are shown. */
28178 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28179 it->max_ascent = max (it->max_ascent, font_ascent);
28180 it->max_descent = max (it->max_descent, font_descent);
28183 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
28185 /* A static composition.
28187 Note: A composition is represented as one glyph in the
28188 glyph matrix. There are no padding glyphs.
28190 Important note: pixel_width, ascent, and descent are the
28191 values of what is drawn by draw_glyphs (i.e. the values of
28192 the overall glyphs composed). */
28193 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28194 int boff; /* baseline offset */
28195 struct composition *cmp = composition_table[it->cmp_it.id];
28196 int glyph_len = cmp->glyph_len;
28197 struct font *font = face->font;
28199 it->nglyphs = 1;
28201 /* If we have not yet calculated pixel size data of glyphs of
28202 the composition for the current face font, calculate them
28203 now. Theoretically, we have to check all fonts for the
28204 glyphs, but that requires much time and memory space. So,
28205 here we check only the font of the first glyph. This may
28206 lead to incorrect display, but it's very rare, and C-l
28207 (recenter-top-bottom) can correct the display anyway. */
28208 if (! cmp->font || cmp->font != font)
28210 /* Ascent and descent of the font of the first character
28211 of this composition (adjusted by baseline offset).
28212 Ascent and descent of overall glyphs should not be less
28213 than these, respectively. */
28214 int font_ascent, font_descent, font_height;
28215 /* Bounding box of the overall glyphs. */
28216 int leftmost, rightmost, lowest, highest;
28217 int lbearing, rbearing;
28218 int i, width, ascent, descent;
28219 int c;
28220 XChar2b char2b;
28221 struct font_metrics *pcm;
28222 ptrdiff_t pos;
28224 eassume (0 < glyph_len); /* See Bug#8512. */
28226 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
28227 while (c == '\t' && 0 < --glyph_len);
28229 bool right_padded = glyph_len < cmp->glyph_len;
28230 for (i = 0; i < glyph_len; i++)
28232 c = COMPOSITION_GLYPH (cmp, i);
28233 if (c != '\t')
28234 break;
28235 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28237 bool left_padded = i > 0;
28239 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
28240 : IT_CHARPOS (*it));
28241 /* If no suitable font is found, use the default font. */
28242 bool font_not_found_p = font == NULL;
28243 if (font_not_found_p)
28245 face = face->ascii_face;
28246 font = face->font;
28248 boff = font->baseline_offset;
28249 if (font->vertical_centering)
28250 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
28251 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28252 font_ascent += boff;
28253 font_descent -= boff;
28254 font_height = font_ascent + font_descent;
28256 cmp->font = font;
28258 pcm = NULL;
28259 if (! font_not_found_p)
28261 get_char_face_and_encoding (it->f, c, it->face_id,
28262 &char2b, false);
28263 pcm = get_per_char_metric (font, &char2b);
28266 /* Initialize the bounding box. */
28267 if (pcm)
28269 width = cmp->glyph_len > 0 ? pcm->width : 0;
28270 ascent = pcm->ascent;
28271 descent = pcm->descent;
28272 lbearing = pcm->lbearing;
28273 rbearing = pcm->rbearing;
28275 else
28277 width = cmp->glyph_len > 0 ? font->space_width : 0;
28278 ascent = FONT_BASE (font);
28279 descent = FONT_DESCENT (font);
28280 lbearing = 0;
28281 rbearing = width;
28284 rightmost = width;
28285 leftmost = 0;
28286 lowest = - descent + boff;
28287 highest = ascent + boff;
28289 if (! font_not_found_p
28290 && font->default_ascent
28291 && CHAR_TABLE_P (Vuse_default_ascent)
28292 && !NILP (Faref (Vuse_default_ascent,
28293 make_number (it->char_to_display))))
28294 highest = font->default_ascent + boff;
28296 /* Draw the first glyph at the normal position. It may be
28297 shifted to right later if some other glyphs are drawn
28298 at the left. */
28299 cmp->offsets[i * 2] = 0;
28300 cmp->offsets[i * 2 + 1] = boff;
28301 cmp->lbearing = lbearing;
28302 cmp->rbearing = rbearing;
28304 /* Set cmp->offsets for the remaining glyphs. */
28305 for (i++; i < glyph_len; i++)
28307 int left, right, btm, top;
28308 int ch = COMPOSITION_GLYPH (cmp, i);
28309 int face_id;
28310 struct face *this_face;
28312 if (ch == '\t')
28313 ch = ' ';
28314 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
28315 this_face = FACE_FROM_ID (it->f, face_id);
28316 font = this_face->font;
28318 if (font == NULL)
28319 pcm = NULL;
28320 else
28322 get_char_face_and_encoding (it->f, ch, face_id,
28323 &char2b, false);
28324 pcm = get_per_char_metric (font, &char2b);
28326 if (! pcm)
28327 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28328 else
28330 width = pcm->width;
28331 ascent = pcm->ascent;
28332 descent = pcm->descent;
28333 lbearing = pcm->lbearing;
28334 rbearing = pcm->rbearing;
28335 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
28337 /* Relative composition with or without
28338 alternate chars. */
28339 left = (leftmost + rightmost - width) / 2;
28340 btm = - descent + boff;
28341 if (font->relative_compose
28342 && (! CHAR_TABLE_P (Vignore_relative_composition)
28343 || NILP (Faref (Vignore_relative_composition,
28344 make_number (ch)))))
28347 if (- descent >= font->relative_compose)
28348 /* One extra pixel between two glyphs. */
28349 btm = highest + 1;
28350 else if (ascent <= 0)
28351 /* One extra pixel between two glyphs. */
28352 btm = lowest - 1 - ascent - descent;
28355 else
28357 /* A composition rule is specified by an integer
28358 value that encodes global and new reference
28359 points (GREF and NREF). GREF and NREF are
28360 specified by numbers as below:
28362 0---1---2 -- ascent
28366 9--10--11 -- center
28368 ---3---4---5--- baseline
28370 6---7---8 -- descent
28372 int rule = COMPOSITION_RULE (cmp, i);
28373 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
28375 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
28376 grefx = gref % 3, nrefx = nref % 3;
28377 grefy = gref / 3, nrefy = nref / 3;
28378 if (xoff)
28379 xoff = font_height * (xoff - 128) / 256;
28380 if (yoff)
28381 yoff = font_height * (yoff - 128) / 256;
28383 left = (leftmost
28384 + grefx * (rightmost - leftmost) / 2
28385 - nrefx * width / 2
28386 + xoff);
28388 btm = ((grefy == 0 ? highest
28389 : grefy == 1 ? 0
28390 : grefy == 2 ? lowest
28391 : (highest + lowest) / 2)
28392 - (nrefy == 0 ? ascent + descent
28393 : nrefy == 1 ? descent - boff
28394 : nrefy == 2 ? 0
28395 : (ascent + descent) / 2)
28396 + yoff);
28399 cmp->offsets[i * 2] = left;
28400 cmp->offsets[i * 2 + 1] = btm + descent;
28402 /* Update the bounding box of the overall glyphs. */
28403 if (width > 0)
28405 right = left + width;
28406 if (left < leftmost)
28407 leftmost = left;
28408 if (right > rightmost)
28409 rightmost = right;
28411 top = btm + descent + ascent;
28412 if (top > highest)
28413 highest = top;
28414 if (btm < lowest)
28415 lowest = btm;
28417 if (cmp->lbearing > left + lbearing)
28418 cmp->lbearing = left + lbearing;
28419 if (cmp->rbearing < left + rbearing)
28420 cmp->rbearing = left + rbearing;
28424 /* If there are glyphs whose x-offsets are negative,
28425 shift all glyphs to the right and make all x-offsets
28426 non-negative. */
28427 if (leftmost < 0)
28429 for (i = 0; i < cmp->glyph_len; i++)
28430 cmp->offsets[i * 2] -= leftmost;
28431 rightmost -= leftmost;
28432 cmp->lbearing -= leftmost;
28433 cmp->rbearing -= leftmost;
28436 if (left_padded && cmp->lbearing < 0)
28438 for (i = 0; i < cmp->glyph_len; i++)
28439 cmp->offsets[i * 2] -= cmp->lbearing;
28440 rightmost -= cmp->lbearing;
28441 cmp->rbearing -= cmp->lbearing;
28442 cmp->lbearing = 0;
28444 if (right_padded && rightmost < cmp->rbearing)
28446 rightmost = cmp->rbearing;
28449 cmp->pixel_width = rightmost;
28450 cmp->ascent = highest;
28451 cmp->descent = - lowest;
28452 if (cmp->ascent < font_ascent)
28453 cmp->ascent = font_ascent;
28454 if (cmp->descent < font_descent)
28455 cmp->descent = font_descent;
28458 if (it->glyph_row
28459 && (cmp->lbearing < 0
28460 || cmp->rbearing > cmp->pixel_width))
28461 it->glyph_row->contains_overlapping_glyphs_p = true;
28463 it->pixel_width = cmp->pixel_width;
28464 it->ascent = it->phys_ascent = cmp->ascent;
28465 it->descent = it->phys_descent = cmp->descent;
28466 if (face->box != FACE_NO_BOX)
28468 int thick = face->box_line_width;
28470 if (thick > 0)
28472 it->ascent += thick;
28473 it->descent += thick;
28475 else
28476 thick = - thick;
28478 if (it->start_of_box_run_p)
28479 it->pixel_width += thick;
28480 if (it->end_of_box_run_p)
28481 it->pixel_width += thick;
28484 /* If face has an overline, add the height of the overline
28485 (1 pixel) and a 1 pixel margin to the character height. */
28486 if (face->overline_p)
28487 it->ascent += overline_margin;
28489 take_vertical_position_into_account (it);
28490 if (it->ascent < 0)
28491 it->ascent = 0;
28492 if (it->descent < 0)
28493 it->descent = 0;
28495 if (it->glyph_row && cmp->glyph_len > 0)
28496 append_composite_glyph (it);
28498 else if (it->what == IT_COMPOSITION)
28500 /* A dynamic (automatic) composition. */
28501 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28502 Lisp_Object gstring;
28503 struct font_metrics metrics;
28505 it->nglyphs = 1;
28507 gstring = composition_gstring_from_id (it->cmp_it.id);
28508 it->pixel_width
28509 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
28510 &metrics);
28511 if (it->glyph_row
28512 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
28513 it->glyph_row->contains_overlapping_glyphs_p = true;
28514 it->ascent = it->phys_ascent = metrics.ascent;
28515 it->descent = it->phys_descent = metrics.descent;
28516 if (face->box != FACE_NO_BOX)
28518 int thick = face->box_line_width;
28520 if (thick > 0)
28522 it->ascent += thick;
28523 it->descent += thick;
28525 else
28526 thick = - thick;
28528 if (it->start_of_box_run_p)
28529 it->pixel_width += thick;
28530 if (it->end_of_box_run_p)
28531 it->pixel_width += thick;
28533 /* If face has an overline, add the height of the overline
28534 (1 pixel) and a 1 pixel margin to the character height. */
28535 if (face->overline_p)
28536 it->ascent += overline_margin;
28537 take_vertical_position_into_account (it);
28538 if (it->ascent < 0)
28539 it->ascent = 0;
28540 if (it->descent < 0)
28541 it->descent = 0;
28543 if (it->glyph_row)
28544 append_composite_glyph (it);
28546 else if (it->what == IT_GLYPHLESS)
28547 produce_glyphless_glyph (it, false, Qnil);
28548 else if (it->what == IT_IMAGE)
28549 produce_image_glyph (it);
28550 else if (it->what == IT_STRETCH)
28551 produce_stretch_glyph (it);
28552 else if (it->what == IT_XWIDGET)
28553 produce_xwidget_glyph (it);
28555 done:
28556 /* Accumulate dimensions. Note: can't assume that it->descent > 0
28557 because this isn't true for images with `:ascent 100'. */
28558 eassert (it->ascent >= 0 && it->descent >= 0);
28559 if (it->area == TEXT_AREA)
28560 it->current_x += it->pixel_width;
28562 if (extra_line_spacing > 0)
28564 it->descent += extra_line_spacing;
28565 if (extra_line_spacing > it->max_extra_line_spacing)
28566 it->max_extra_line_spacing = extra_line_spacing;
28569 it->max_ascent = max (it->max_ascent, it->ascent);
28570 it->max_descent = max (it->max_descent, it->descent);
28571 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
28572 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
28575 /* EXPORT for RIF:
28576 Output LEN glyphs starting at START at the nominal cursor position.
28577 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
28578 being updated, and UPDATED_AREA is the area of that row being updated. */
28580 void
28581 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
28582 struct glyph *start, enum glyph_row_area updated_area, int len)
28584 int x, hpos, chpos = w->phys_cursor.hpos;
28586 eassert (updated_row);
28587 /* When the window is hscrolled, cursor hpos can legitimately be out
28588 of bounds, but we draw the cursor at the corresponding window
28589 margin in that case. */
28590 if (!updated_row->reversed_p && chpos < 0)
28591 chpos = 0;
28592 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
28593 chpos = updated_row->used[TEXT_AREA] - 1;
28595 block_input ();
28597 /* Write glyphs. */
28599 hpos = start - updated_row->glyphs[updated_area];
28600 x = draw_glyphs (w, w->output_cursor.x,
28601 updated_row, updated_area,
28602 hpos, hpos + len,
28603 DRAW_NORMAL_TEXT, 0);
28605 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
28606 if (updated_area == TEXT_AREA
28607 && w->phys_cursor_on_p
28608 && w->phys_cursor.vpos == w->output_cursor.vpos
28609 && chpos >= hpos
28610 && chpos < hpos + len)
28611 w->phys_cursor_on_p = false;
28613 unblock_input ();
28615 /* Advance the output cursor. */
28616 w->output_cursor.hpos += len;
28617 w->output_cursor.x = x;
28621 /* EXPORT for RIF:
28622 Insert LEN glyphs from START at the nominal cursor position. */
28624 void
28625 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
28626 struct glyph *start, enum glyph_row_area updated_area, int len)
28628 struct frame *f;
28629 int line_height, shift_by_width, shifted_region_width;
28630 struct glyph_row *row;
28631 struct glyph *glyph;
28632 int frame_x, frame_y;
28633 ptrdiff_t hpos;
28635 eassert (updated_row);
28636 block_input ();
28637 f = XFRAME (WINDOW_FRAME (w));
28639 /* Get the height of the line we are in. */
28640 row = updated_row;
28641 line_height = row->height;
28643 /* Get the width of the glyphs to insert. */
28644 shift_by_width = 0;
28645 for (glyph = start; glyph < start + len; ++glyph)
28646 shift_by_width += glyph->pixel_width;
28648 /* Get the width of the region to shift right. */
28649 shifted_region_width = (window_box_width (w, updated_area)
28650 - w->output_cursor.x
28651 - shift_by_width);
28653 /* Shift right. */
28654 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
28655 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
28657 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
28658 line_height, shift_by_width);
28660 /* Write the glyphs. */
28661 hpos = start - row->glyphs[updated_area];
28662 draw_glyphs (w, w->output_cursor.x, row, updated_area,
28663 hpos, hpos + len,
28664 DRAW_NORMAL_TEXT, 0);
28666 /* Advance the output cursor. */
28667 w->output_cursor.hpos += len;
28668 w->output_cursor.x += shift_by_width;
28669 unblock_input ();
28673 /* EXPORT for RIF:
28674 Erase the current text line from the nominal cursor position
28675 (inclusive) to pixel column TO_X (exclusive). The idea is that
28676 everything from TO_X onward is already erased.
28678 TO_X is a pixel position relative to UPDATED_AREA of currently
28679 updated window W. TO_X == -1 means clear to the end of this area. */
28681 void
28682 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
28683 enum glyph_row_area updated_area, int to_x)
28685 struct frame *f;
28686 int max_x, min_y, max_y;
28687 int from_x, from_y, to_y;
28689 eassert (updated_row);
28690 f = XFRAME (w->frame);
28692 if (updated_row->full_width_p)
28693 max_x = (WINDOW_PIXEL_WIDTH (w)
28694 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
28695 else
28696 max_x = window_box_width (w, updated_area);
28697 max_y = window_text_bottom_y (w);
28699 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
28700 of window. For TO_X > 0, truncate to end of drawing area. */
28701 if (to_x == 0)
28702 return;
28703 else if (to_x < 0)
28704 to_x = max_x;
28705 else
28706 to_x = min (to_x, max_x);
28708 to_y = min (max_y, w->output_cursor.y + updated_row->height);
28710 /* Notice if the cursor will be cleared by this operation. */
28711 if (!updated_row->full_width_p)
28712 notice_overwritten_cursor (w, updated_area,
28713 w->output_cursor.x, -1,
28714 updated_row->y,
28715 MATRIX_ROW_BOTTOM_Y (updated_row));
28717 from_x = w->output_cursor.x;
28719 /* Translate to frame coordinates. */
28720 if (updated_row->full_width_p)
28722 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
28723 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
28725 else
28727 int area_left = window_box_left (w, updated_area);
28728 from_x += area_left;
28729 to_x += area_left;
28732 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
28733 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
28734 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
28736 /* Prevent inadvertently clearing to end of the X window. */
28737 if (to_x > from_x && to_y > from_y)
28739 block_input ();
28740 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
28741 to_x - from_x, to_y - from_y);
28742 unblock_input ();
28746 #endif /* HAVE_WINDOW_SYSTEM */
28750 /***********************************************************************
28751 Cursor types
28752 ***********************************************************************/
28754 /* Value is the internal representation of the specified cursor type
28755 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
28756 of the bar cursor. */
28758 static enum text_cursor_kinds
28759 get_specified_cursor_type (Lisp_Object arg, int *width)
28761 enum text_cursor_kinds type;
28763 if (NILP (arg))
28764 return NO_CURSOR;
28766 if (EQ (arg, Qbox))
28767 return FILLED_BOX_CURSOR;
28769 if (EQ (arg, Qhollow))
28770 return HOLLOW_BOX_CURSOR;
28772 if (EQ (arg, Qbar))
28774 *width = 2;
28775 return BAR_CURSOR;
28778 if (CONSP (arg)
28779 && EQ (XCAR (arg), Qbar)
28780 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28782 *width = XINT (XCDR (arg));
28783 return BAR_CURSOR;
28786 if (EQ (arg, Qhbar))
28788 *width = 2;
28789 return HBAR_CURSOR;
28792 if (CONSP (arg)
28793 && EQ (XCAR (arg), Qhbar)
28794 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28796 *width = XINT (XCDR (arg));
28797 return HBAR_CURSOR;
28800 /* Treat anything unknown as "hollow box cursor".
28801 It was bad to signal an error; people have trouble fixing
28802 .Xdefaults with Emacs, when it has something bad in it. */
28803 type = HOLLOW_BOX_CURSOR;
28805 return type;
28808 /* Set the default cursor types for specified frame. */
28809 void
28810 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
28812 int width = 1;
28813 Lisp_Object tem;
28815 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
28816 FRAME_CURSOR_WIDTH (f) = width;
28818 /* By default, set up the blink-off state depending on the on-state. */
28820 tem = Fassoc (arg, Vblink_cursor_alist, Qnil);
28821 if (!NILP (tem))
28823 FRAME_BLINK_OFF_CURSOR (f)
28824 = get_specified_cursor_type (XCDR (tem), &width);
28825 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
28827 else
28828 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
28830 /* Make sure the cursor gets redrawn. */
28831 f->cursor_type_changed = true;
28835 #ifdef HAVE_WINDOW_SYSTEM
28837 /* Return the cursor we want to be displayed in window W. Return
28838 width of bar/hbar cursor through WIDTH arg. Return with
28839 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
28840 (i.e. if the `system caret' should track this cursor).
28842 In a mini-buffer window, we want the cursor only to appear if we
28843 are reading input from this window. For the selected window, we
28844 want the cursor type given by the frame parameter or buffer local
28845 setting of cursor-type. If explicitly marked off, draw no cursor.
28846 In all other cases, we want a hollow box cursor. */
28848 static enum text_cursor_kinds
28849 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
28850 bool *active_cursor)
28852 struct frame *f = XFRAME (w->frame);
28853 struct buffer *b = XBUFFER (w->contents);
28854 int cursor_type = DEFAULT_CURSOR;
28855 Lisp_Object alt_cursor;
28856 bool non_selected = false;
28858 *active_cursor = true;
28860 /* Echo area */
28861 if (cursor_in_echo_area
28862 && FRAME_HAS_MINIBUF_P (f)
28863 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
28865 if (w == XWINDOW (echo_area_window))
28867 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
28869 *width = FRAME_CURSOR_WIDTH (f);
28870 return FRAME_DESIRED_CURSOR (f);
28872 else
28873 return get_specified_cursor_type (BVAR (b, cursor_type), width);
28876 *active_cursor = false;
28877 non_selected = true;
28880 /* Detect a nonselected window or nonselected frame. */
28881 else if (w != XWINDOW (f->selected_window)
28882 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
28884 *active_cursor = false;
28886 if (MINI_WINDOW_P (w) && minibuf_level == 0)
28887 return NO_CURSOR;
28889 non_selected = true;
28892 /* Never display a cursor in a window in which cursor-type is nil. */
28893 if (NILP (BVAR (b, cursor_type)))
28894 return NO_CURSOR;
28896 /* Get the normal cursor type for this window. */
28897 if (EQ (BVAR (b, cursor_type), Qt))
28899 cursor_type = FRAME_DESIRED_CURSOR (f);
28900 *width = FRAME_CURSOR_WIDTH (f);
28902 else
28903 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
28905 /* Use cursor-in-non-selected-windows instead
28906 for non-selected window or frame. */
28907 if (non_selected)
28909 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
28910 if (!EQ (Qt, alt_cursor))
28911 return get_specified_cursor_type (alt_cursor, width);
28912 /* t means modify the normal cursor type. */
28913 if (cursor_type == FILLED_BOX_CURSOR)
28914 cursor_type = HOLLOW_BOX_CURSOR;
28915 else if (cursor_type == BAR_CURSOR && *width > 1)
28916 --*width;
28917 return cursor_type;
28920 /* Use normal cursor if not blinked off. */
28921 if (!w->cursor_off_p)
28923 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
28924 return NO_CURSOR;
28925 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28927 if (cursor_type == FILLED_BOX_CURSOR)
28929 /* Using a block cursor on large images can be very annoying.
28930 So use a hollow cursor for "large" images.
28931 If image is not transparent (no mask), also use hollow cursor. */
28932 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
28933 if (img != NULL && IMAGEP (img->spec))
28935 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
28936 where N = size of default frame font size.
28937 This should cover most of the "tiny" icons people may use. */
28938 if (!img->mask
28939 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
28940 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
28941 cursor_type = HOLLOW_BOX_CURSOR;
28944 else if (cursor_type != NO_CURSOR)
28946 /* Display current only supports BOX and HOLLOW cursors for images.
28947 So for now, unconditionally use a HOLLOW cursor when cursor is
28948 not a solid box cursor. */
28949 cursor_type = HOLLOW_BOX_CURSOR;
28952 return cursor_type;
28955 /* Cursor is blinked off, so determine how to "toggle" it. */
28957 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
28958 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist, Qnil), !NILP (alt_cursor)))
28959 return get_specified_cursor_type (XCDR (alt_cursor), width);
28961 /* Then see if frame has specified a specific blink off cursor type. */
28962 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
28964 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
28965 return FRAME_BLINK_OFF_CURSOR (f);
28968 #if false
28969 /* Some people liked having a permanently visible blinking cursor,
28970 while others had very strong opinions against it. So it was
28971 decided to remove it. KFS 2003-09-03 */
28973 /* Finally perform built-in cursor blinking:
28974 filled box <-> hollow box
28975 wide [h]bar <-> narrow [h]bar
28976 narrow [h]bar <-> no cursor
28977 other type <-> no cursor */
28979 if (cursor_type == FILLED_BOX_CURSOR)
28980 return HOLLOW_BOX_CURSOR;
28982 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
28984 *width = 1;
28985 return cursor_type;
28987 #endif
28989 return NO_CURSOR;
28993 /* Notice when the text cursor of window W has been completely
28994 overwritten by a drawing operation that outputs glyphs in AREA
28995 starting at X0 and ending at X1 in the line starting at Y0 and
28996 ending at Y1. X coordinates are area-relative. X1 < 0 means all
28997 the rest of the line after X0 has been written. Y coordinates
28998 are window-relative. */
29000 static void
29001 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
29002 int x0, int x1, int y0, int y1)
29004 int cx0, cx1, cy0, cy1;
29005 struct glyph_row *row;
29007 if (!w->phys_cursor_on_p)
29008 return;
29009 if (area != TEXT_AREA)
29010 return;
29012 if (w->phys_cursor.vpos < 0
29013 || w->phys_cursor.vpos >= w->current_matrix->nrows
29014 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
29015 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
29016 return;
29018 if (row->cursor_in_fringe_p)
29020 row->cursor_in_fringe_p = false;
29021 draw_fringe_bitmap (w, row, row->reversed_p);
29022 w->phys_cursor_on_p = false;
29023 return;
29026 cx0 = w->phys_cursor.x;
29027 cx1 = cx0 + w->phys_cursor_width;
29028 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
29029 return;
29031 /* The cursor image will be completely removed from the
29032 screen if the output area intersects the cursor area in
29033 y-direction. When we draw in [y0 y1[, and some part of
29034 the cursor is at y < y0, that part must have been drawn
29035 before. When scrolling, the cursor is erased before
29036 actually scrolling, so we don't come here. When not
29037 scrolling, the rows above the old cursor row must have
29038 changed, and in this case these rows must have written
29039 over the cursor image.
29041 Likewise if part of the cursor is below y1, with the
29042 exception of the cursor being in the first blank row at
29043 the buffer and window end because update_text_area
29044 doesn't draw that row. (Except when it does, but
29045 that's handled in update_text_area.) */
29047 cy0 = w->phys_cursor.y;
29048 cy1 = cy0 + w->phys_cursor_height;
29049 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
29050 return;
29052 w->phys_cursor_on_p = false;
29055 #endif /* HAVE_WINDOW_SYSTEM */
29058 /************************************************************************
29059 Mouse Face
29060 ************************************************************************/
29062 #ifdef HAVE_WINDOW_SYSTEM
29064 /* EXPORT for RIF:
29065 Fix the display of area AREA of overlapping row ROW in window W
29066 with respect to the overlapping part OVERLAPS. */
29068 void
29069 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
29070 enum glyph_row_area area, int overlaps)
29072 int i, x;
29074 block_input ();
29076 x = 0;
29077 for (i = 0; i < row->used[area];)
29079 if (row->glyphs[area][i].overlaps_vertically_p)
29081 int start = i, start_x = x;
29085 x += row->glyphs[area][i].pixel_width;
29086 ++i;
29088 while (i < row->used[area]
29089 && row->glyphs[area][i].overlaps_vertically_p);
29091 draw_glyphs (w, start_x, row, area,
29092 start, i,
29093 DRAW_NORMAL_TEXT, overlaps);
29095 else
29097 x += row->glyphs[area][i].pixel_width;
29098 ++i;
29102 unblock_input ();
29106 /* EXPORT:
29107 Draw the cursor glyph of window W in glyph row ROW. See the
29108 comment of draw_glyphs for the meaning of HL. */
29110 void
29111 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
29112 enum draw_glyphs_face hl)
29114 /* If cursor hpos is out of bounds, don't draw garbage. This can
29115 happen in mini-buffer windows when switching between echo area
29116 glyphs and mini-buffer. */
29117 if ((row->reversed_p
29118 ? (w->phys_cursor.hpos >= 0)
29119 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
29121 bool on_p = w->phys_cursor_on_p;
29122 int x1;
29123 int hpos = w->phys_cursor.hpos;
29125 /* When the window is hscrolled, cursor hpos can legitimately be
29126 out of bounds, but we draw the cursor at the corresponding
29127 window margin in that case. */
29128 if (!row->reversed_p && hpos < 0)
29129 hpos = 0;
29130 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29131 hpos = row->used[TEXT_AREA] - 1;
29133 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
29134 hl, 0);
29135 w->phys_cursor_on_p = on_p;
29137 if (hl == DRAW_CURSOR)
29138 w->phys_cursor_width = x1 - w->phys_cursor.x;
29139 /* When we erase the cursor, and ROW is overlapped by other
29140 rows, make sure that these overlapping parts of other rows
29141 are redrawn. */
29142 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
29144 w->phys_cursor_width = x1 - w->phys_cursor.x;
29146 if (row > w->current_matrix->rows
29147 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
29148 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
29149 OVERLAPS_ERASED_CURSOR);
29151 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
29152 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
29153 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
29154 OVERLAPS_ERASED_CURSOR);
29160 /* Erase the image of a cursor of window W from the screen. */
29162 void
29163 erase_phys_cursor (struct window *w)
29165 struct frame *f = XFRAME (w->frame);
29166 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29167 int hpos = w->phys_cursor.hpos;
29168 int vpos = w->phys_cursor.vpos;
29169 bool mouse_face_here_p = false;
29170 struct glyph_matrix *active_glyphs = w->current_matrix;
29171 struct glyph_row *cursor_row;
29172 struct glyph *cursor_glyph;
29173 enum draw_glyphs_face hl;
29175 /* No cursor displayed or row invalidated => nothing to do on the
29176 screen. */
29177 if (w->phys_cursor_type == NO_CURSOR)
29178 goto mark_cursor_off;
29180 /* VPOS >= active_glyphs->nrows means that window has been resized.
29181 Don't bother to erase the cursor. */
29182 if (vpos >= active_glyphs->nrows)
29183 goto mark_cursor_off;
29185 /* If row containing cursor is marked invalid, there is nothing we
29186 can do. */
29187 cursor_row = MATRIX_ROW (active_glyphs, vpos);
29188 if (!cursor_row->enabled_p)
29189 goto mark_cursor_off;
29191 /* If line spacing is > 0, old cursor may only be partially visible in
29192 window after split-window. So adjust visible height. */
29193 cursor_row->visible_height = min (cursor_row->visible_height,
29194 window_text_bottom_y (w) - cursor_row->y);
29196 /* If row is completely invisible, don't attempt to delete a cursor which
29197 isn't there. This can happen if cursor is at top of a window, and
29198 we switch to a buffer with a header line in that window. */
29199 if (cursor_row->visible_height <= 0)
29200 goto mark_cursor_off;
29202 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
29203 if (cursor_row->cursor_in_fringe_p)
29205 cursor_row->cursor_in_fringe_p = false;
29206 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
29207 goto mark_cursor_off;
29210 /* This can happen when the new row is shorter than the old one.
29211 In this case, either draw_glyphs or clear_end_of_line
29212 should have cleared the cursor. Note that we wouldn't be
29213 able to erase the cursor in this case because we don't have a
29214 cursor glyph at hand. */
29215 if ((cursor_row->reversed_p
29216 ? (w->phys_cursor.hpos < 0)
29217 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
29218 goto mark_cursor_off;
29220 /* When the window is hscrolled, cursor hpos can legitimately be out
29221 of bounds, but we draw the cursor at the corresponding window
29222 margin in that case. */
29223 if (!cursor_row->reversed_p && hpos < 0)
29224 hpos = 0;
29225 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
29226 hpos = cursor_row->used[TEXT_AREA] - 1;
29228 /* If the cursor is in the mouse face area, redisplay that when
29229 we clear the cursor. */
29230 if (! NILP (hlinfo->mouse_face_window)
29231 && coords_in_mouse_face_p (w, hpos, vpos)
29232 /* Don't redraw the cursor's spot in mouse face if it is at the
29233 end of a line (on a newline). The cursor appears there, but
29234 mouse highlighting does not. */
29235 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
29236 mouse_face_here_p = true;
29238 /* Maybe clear the display under the cursor. */
29239 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
29241 int x, y;
29242 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
29243 int width;
29245 cursor_glyph = get_phys_cursor_glyph (w);
29246 if (cursor_glyph == NULL)
29247 goto mark_cursor_off;
29249 width = cursor_glyph->pixel_width;
29250 x = w->phys_cursor.x;
29251 if (x < 0)
29253 width += x;
29254 x = 0;
29256 width = min (width, window_box_width (w, TEXT_AREA) - x);
29257 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
29258 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
29260 if (width > 0)
29261 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
29264 /* Erase the cursor by redrawing the character underneath it. */
29265 if (mouse_face_here_p)
29266 hl = DRAW_MOUSE_FACE;
29267 else
29268 hl = DRAW_NORMAL_TEXT;
29269 draw_phys_cursor_glyph (w, cursor_row, hl);
29271 mark_cursor_off:
29272 w->phys_cursor_on_p = false;
29273 w->phys_cursor_type = NO_CURSOR;
29277 /* Display or clear cursor of window W. If !ON, clear the cursor.
29278 If ON, display the cursor; where to put the cursor is specified by
29279 HPOS, VPOS, X and Y. */
29281 void
29282 display_and_set_cursor (struct window *w, bool on,
29283 int hpos, int vpos, int x, int y)
29285 struct frame *f = XFRAME (w->frame);
29286 int new_cursor_type;
29287 int new_cursor_width;
29288 bool active_cursor;
29289 struct glyph_row *glyph_row;
29290 struct glyph *glyph;
29292 /* This is pointless on invisible frames, and dangerous on garbaged
29293 windows and frames; in the latter case, the frame or window may
29294 be in the midst of changing its size, and x and y may be off the
29295 window. */
29296 if (! FRAME_VISIBLE_P (f)
29297 || vpos >= w->current_matrix->nrows
29298 || hpos >= w->current_matrix->matrix_w)
29299 return;
29301 /* If cursor is off and we want it off, return quickly. */
29302 if (!on && !w->phys_cursor_on_p)
29303 return;
29305 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
29306 /* If cursor row is not enabled, we don't really know where to
29307 display the cursor. */
29308 if (!glyph_row->enabled_p)
29310 w->phys_cursor_on_p = false;
29311 return;
29314 /* A frame might be marked garbaged even though its cursor position
29315 is correct, and will not change upon subsequent redisplay. This
29316 happens in some rare situations, like toggling the sort order in
29317 Dired windows. We've already established that VPOS is valid, so
29318 it shouldn't do any harm to record the cursor position, as we are
29319 going to return without acting on it anyway. Otherwise, expose
29320 events might come in and call update_window_cursor, which will
29321 blindly use outdated values in w->phys_cursor. */
29322 if (FRAME_GARBAGED_P (f))
29324 if (on)
29326 w->phys_cursor.x = x;
29327 w->phys_cursor.y = glyph_row->y;
29328 w->phys_cursor.hpos = hpos;
29329 w->phys_cursor.vpos = vpos;
29331 return;
29334 glyph = NULL;
29335 if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
29336 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
29338 eassert (input_blocked_p ());
29340 /* Set new_cursor_type to the cursor we want to be displayed. */
29341 new_cursor_type = get_window_cursor_type (w, glyph,
29342 &new_cursor_width, &active_cursor);
29344 /* If cursor is currently being shown and we don't want it to be or
29345 it is in the wrong place, or the cursor type is not what we want,
29346 erase it. */
29347 if (w->phys_cursor_on_p
29348 && (!on
29349 || w->phys_cursor.x != x
29350 || w->phys_cursor.y != y
29351 /* HPOS can be negative in R2L rows whose
29352 exact_window_width_line_p flag is set (i.e. their newline
29353 would "overflow into the fringe"). */
29354 || hpos < 0
29355 || new_cursor_type != w->phys_cursor_type
29356 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
29357 && new_cursor_width != w->phys_cursor_width)))
29358 erase_phys_cursor (w);
29360 /* Don't check phys_cursor_on_p here because that flag is only set
29361 to false in some cases where we know that the cursor has been
29362 completely erased, to avoid the extra work of erasing the cursor
29363 twice. In other words, phys_cursor_on_p can be true and the cursor
29364 still not be visible, or it has only been partly erased. */
29365 if (on)
29367 w->phys_cursor_ascent = glyph_row->ascent;
29368 w->phys_cursor_height = glyph_row->height;
29370 /* Set phys_cursor_.* before x_draw_.* is called because some
29371 of them may need the information. */
29372 w->phys_cursor.x = x;
29373 w->phys_cursor.y = glyph_row->y;
29374 w->phys_cursor.hpos = hpos;
29375 w->phys_cursor.vpos = vpos;
29378 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
29379 new_cursor_type, new_cursor_width,
29380 on, active_cursor);
29384 /* Switch the display of W's cursor on or off, according to the value
29385 of ON. */
29387 static void
29388 update_window_cursor (struct window *w, bool on)
29390 /* Don't update cursor in windows whose frame is in the process
29391 of being deleted. */
29392 if (w->current_matrix)
29394 int hpos = w->phys_cursor.hpos;
29395 int vpos = w->phys_cursor.vpos;
29396 struct glyph_row *row;
29398 if (vpos >= w->current_matrix->nrows
29399 || hpos >= w->current_matrix->matrix_w)
29400 return;
29402 row = MATRIX_ROW (w->current_matrix, vpos);
29404 /* When the window is hscrolled, cursor hpos can legitimately be
29405 out of bounds, but we draw the cursor at the corresponding
29406 window margin in that case. */
29407 if (!row->reversed_p && hpos < 0)
29408 hpos = 0;
29409 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29410 hpos = row->used[TEXT_AREA] - 1;
29412 block_input ();
29413 display_and_set_cursor (w, on, hpos, vpos,
29414 w->phys_cursor.x, w->phys_cursor.y);
29415 unblock_input ();
29420 /* Call update_window_cursor with parameter ON_P on all leaf windows
29421 in the window tree rooted at W. */
29423 static void
29424 update_cursor_in_window_tree (struct window *w, bool on_p)
29426 while (w)
29428 if (WINDOWP (w->contents))
29429 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
29430 else
29431 update_window_cursor (w, on_p);
29433 w = NILP (w->next) ? 0 : XWINDOW (w->next);
29438 /* EXPORT:
29439 Display the cursor on window W, or clear it, according to ON_P.
29440 Don't change the cursor's position. */
29442 void
29443 x_update_cursor (struct frame *f, bool on_p)
29445 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
29449 /* EXPORT:
29450 Clear the cursor of window W to background color, and mark the
29451 cursor as not shown. This is used when the text where the cursor
29452 is about to be rewritten. */
29454 void
29455 x_clear_cursor (struct window *w)
29457 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
29458 update_window_cursor (w, false);
29461 #endif /* HAVE_WINDOW_SYSTEM */
29463 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
29464 and MSDOS. */
29465 static void
29466 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
29467 int start_hpos, int end_hpos,
29468 enum draw_glyphs_face draw)
29470 #ifdef HAVE_WINDOW_SYSTEM
29471 if (FRAME_WINDOW_P (XFRAME (w->frame)))
29473 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
29474 return;
29476 #endif
29477 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
29478 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
29479 #endif
29482 /* Display the active region described by mouse_face_* according to DRAW. */
29484 static void
29485 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
29487 struct window *w = XWINDOW (hlinfo->mouse_face_window);
29488 struct frame *f = XFRAME (WINDOW_FRAME (w));
29490 if (/* If window is in the process of being destroyed, don't bother
29491 to do anything. */
29492 w->current_matrix != NULL
29493 /* Don't update mouse highlight if hidden. */
29494 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
29495 /* Recognize when we are called to operate on rows that don't exist
29496 anymore. This can happen when a window is split. */
29497 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
29499 bool phys_cursor_on_p = w->phys_cursor_on_p;
29500 struct glyph_row *row, *first, *last;
29502 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
29503 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
29505 for (row = first; row <= last && row->enabled_p; ++row)
29507 int start_hpos, end_hpos, start_x;
29509 /* For all but the first row, the highlight starts at column 0. */
29510 if (row == first)
29512 /* R2L rows have BEG and END in reversed order, but the
29513 screen drawing geometry is always left to right. So
29514 we need to mirror the beginning and end of the
29515 highlighted area in R2L rows. */
29516 if (!row->reversed_p)
29518 start_hpos = hlinfo->mouse_face_beg_col;
29519 start_x = hlinfo->mouse_face_beg_x;
29521 else if (row == last)
29523 start_hpos = hlinfo->mouse_face_end_col;
29524 start_x = hlinfo->mouse_face_end_x;
29526 else
29528 start_hpos = 0;
29529 start_x = 0;
29532 else if (row->reversed_p && row == last)
29534 start_hpos = hlinfo->mouse_face_end_col;
29535 start_x = hlinfo->mouse_face_end_x;
29537 else
29539 start_hpos = 0;
29540 start_x = 0;
29543 if (row == last)
29545 if (!row->reversed_p)
29546 end_hpos = hlinfo->mouse_face_end_col;
29547 else if (row == first)
29548 end_hpos = hlinfo->mouse_face_beg_col;
29549 else
29551 end_hpos = row->used[TEXT_AREA];
29552 if (draw == DRAW_NORMAL_TEXT)
29553 row->fill_line_p = true; /* Clear to end of line. */
29556 else if (row->reversed_p && row == first)
29557 end_hpos = hlinfo->mouse_face_beg_col;
29558 else
29560 end_hpos = row->used[TEXT_AREA];
29561 if (draw == DRAW_NORMAL_TEXT)
29562 row->fill_line_p = true; /* Clear to end of line. */
29565 if (end_hpos > start_hpos)
29567 draw_row_with_mouse_face (w, start_x, row,
29568 start_hpos, end_hpos, draw);
29570 row->mouse_face_p
29571 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
29575 /* When we've written over the cursor, arrange for it to
29576 be displayed again. */
29577 if (FRAME_WINDOW_P (f)
29578 && phys_cursor_on_p && !w->phys_cursor_on_p)
29580 #ifdef HAVE_WINDOW_SYSTEM
29581 int hpos = w->phys_cursor.hpos;
29583 /* When the window is hscrolled, cursor hpos can legitimately be
29584 out of bounds, but we draw the cursor at the corresponding
29585 window margin in that case. */
29586 if (!row->reversed_p && hpos < 0)
29587 hpos = 0;
29588 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29589 hpos = row->used[TEXT_AREA] - 1;
29591 block_input ();
29592 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
29593 w->phys_cursor.x, w->phys_cursor.y);
29594 unblock_input ();
29595 #endif /* HAVE_WINDOW_SYSTEM */
29599 #ifdef HAVE_WINDOW_SYSTEM
29600 /* Change the mouse cursor. */
29601 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
29603 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29604 if (draw == DRAW_NORMAL_TEXT
29605 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
29606 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
29607 else
29608 #endif
29609 if (draw == DRAW_MOUSE_FACE)
29610 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
29611 else
29612 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
29614 #endif /* HAVE_WINDOW_SYSTEM */
29617 /* EXPORT:
29618 Clear out the mouse-highlighted active region.
29619 Redraw it un-highlighted first. Value is true if mouse
29620 face was actually drawn unhighlighted. */
29622 bool
29623 clear_mouse_face (Mouse_HLInfo *hlinfo)
29625 bool cleared
29626 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
29627 if (cleared)
29628 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
29629 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
29630 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
29631 hlinfo->mouse_face_window = Qnil;
29632 hlinfo->mouse_face_overlay = Qnil;
29633 return cleared;
29636 /* Return true if the coordinates HPOS and VPOS on windows W are
29637 within the mouse face on that window. */
29638 static bool
29639 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
29641 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29643 /* Quickly resolve the easy cases. */
29644 if (!(WINDOWP (hlinfo->mouse_face_window)
29645 && XWINDOW (hlinfo->mouse_face_window) == w))
29646 return false;
29647 if (vpos < hlinfo->mouse_face_beg_row
29648 || vpos > hlinfo->mouse_face_end_row)
29649 return false;
29650 if (vpos > hlinfo->mouse_face_beg_row
29651 && vpos < hlinfo->mouse_face_end_row)
29652 return true;
29654 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
29656 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29658 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
29659 return true;
29661 else if ((vpos == hlinfo->mouse_face_beg_row
29662 && hpos >= hlinfo->mouse_face_beg_col)
29663 || (vpos == hlinfo->mouse_face_end_row
29664 && hpos < hlinfo->mouse_face_end_col))
29665 return true;
29667 else
29669 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29671 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
29672 return true;
29674 else if ((vpos == hlinfo->mouse_face_beg_row
29675 && hpos <= hlinfo->mouse_face_beg_col)
29676 || (vpos == hlinfo->mouse_face_end_row
29677 && hpos > hlinfo->mouse_face_end_col))
29678 return true;
29680 return false;
29684 /* EXPORT:
29685 True if physical cursor of window W is within mouse face. */
29687 bool
29688 cursor_in_mouse_face_p (struct window *w)
29690 int hpos = w->phys_cursor.hpos;
29691 int vpos = w->phys_cursor.vpos;
29692 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
29694 /* When the window is hscrolled, cursor hpos can legitimately be out
29695 of bounds, but we draw the cursor at the corresponding window
29696 margin in that case. */
29697 if (!row->reversed_p && hpos < 0)
29698 hpos = 0;
29699 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29700 hpos = row->used[TEXT_AREA] - 1;
29702 return coords_in_mouse_face_p (w, hpos, vpos);
29707 /* Find the glyph rows START_ROW and END_ROW of window W that display
29708 characters between buffer positions START_CHARPOS and END_CHARPOS
29709 (excluding END_CHARPOS). DISP_STRING is a display string that
29710 covers these buffer positions. This is similar to
29711 row_containing_pos, but is more accurate when bidi reordering makes
29712 buffer positions change non-linearly with glyph rows. */
29713 static void
29714 rows_from_pos_range (struct window *w,
29715 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
29716 Lisp_Object disp_string,
29717 struct glyph_row **start, struct glyph_row **end)
29719 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29720 int last_y = window_text_bottom_y (w);
29721 struct glyph_row *row;
29723 *start = NULL;
29724 *end = NULL;
29726 while (!first->enabled_p
29727 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
29728 first++;
29730 /* Find the START row. */
29731 for (row = first;
29732 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
29733 row++)
29735 /* A row can potentially be the START row if the range of the
29736 characters it displays intersects the range
29737 [START_CHARPOS..END_CHARPOS). */
29738 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
29739 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
29740 /* See the commentary in row_containing_pos, for the
29741 explanation of the complicated way to check whether
29742 some position is beyond the end of the characters
29743 displayed by a row. */
29744 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
29745 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
29746 && !row->ends_at_zv_p
29747 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
29748 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
29749 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
29750 && !row->ends_at_zv_p
29751 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
29753 /* Found a candidate row. Now make sure at least one of the
29754 glyphs it displays has a charpos from the range
29755 [START_CHARPOS..END_CHARPOS).
29757 This is not obvious because bidi reordering could make
29758 buffer positions of a row be 1,2,3,102,101,100, and if we
29759 want to highlight characters in [50..60), we don't want
29760 this row, even though [50..60) does intersect [1..103),
29761 the range of character positions given by the row's start
29762 and end positions. */
29763 struct glyph *g = row->glyphs[TEXT_AREA];
29764 struct glyph *e = g + row->used[TEXT_AREA];
29766 while (g < e)
29768 if (((BUFFERP (g->object) || NILP (g->object))
29769 && start_charpos <= g->charpos && g->charpos < end_charpos)
29770 /* A glyph that comes from DISP_STRING is by
29771 definition to be highlighted. */
29772 || EQ (g->object, disp_string))
29773 *start = row;
29774 g++;
29776 if (*start)
29777 break;
29781 /* Find the END row. */
29782 if (!*start
29783 /* If the last row is partially visible, start looking for END
29784 from that row, instead of starting from FIRST. */
29785 && !(row->enabled_p
29786 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
29787 row = first;
29788 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
29790 struct glyph_row *next = row + 1;
29791 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
29793 if (!next->enabled_p
29794 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
29795 /* The first row >= START whose range of displayed characters
29796 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
29797 is the row END + 1. */
29798 || (start_charpos < next_start
29799 && end_charpos < next_start)
29800 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
29801 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
29802 && !next->ends_at_zv_p
29803 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
29804 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
29805 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
29806 && !next->ends_at_zv_p
29807 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
29809 *end = row;
29810 break;
29812 else
29814 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
29815 but none of the characters it displays are in the range, it is
29816 also END + 1. */
29817 struct glyph *g = next->glyphs[TEXT_AREA];
29818 struct glyph *s = g;
29819 struct glyph *e = g + next->used[TEXT_AREA];
29821 while (g < e)
29823 if (((BUFFERP (g->object) || NILP (g->object))
29824 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
29825 /* If the buffer position of the first glyph in
29826 the row is equal to END_CHARPOS, it means
29827 the last character to be highlighted is the
29828 newline of ROW, and we must consider NEXT as
29829 END, not END+1. */
29830 || (((!next->reversed_p && g == s)
29831 || (next->reversed_p && g == e - 1))
29832 && (g->charpos == end_charpos
29833 /* Special case for when NEXT is an
29834 empty line at ZV. */
29835 || (g->charpos == -1
29836 && !row->ends_at_zv_p
29837 && next_start == end_charpos)))))
29838 /* A glyph that comes from DISP_STRING is by
29839 definition to be highlighted. */
29840 || EQ (g->object, disp_string))
29841 break;
29842 g++;
29844 if (g == e)
29846 *end = row;
29847 break;
29849 /* The first row that ends at ZV must be the last to be
29850 highlighted. */
29851 else if (next->ends_at_zv_p)
29853 *end = next;
29854 break;
29860 /* This function sets the mouse_face_* elements of HLINFO, assuming
29861 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
29862 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
29863 for the overlay or run of text properties specifying the mouse
29864 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
29865 before-string and after-string that must also be highlighted.
29866 DISP_STRING, if non-nil, is a display string that may cover some
29867 or all of the highlighted text. */
29869 static void
29870 mouse_face_from_buffer_pos (Lisp_Object window,
29871 Mouse_HLInfo *hlinfo,
29872 ptrdiff_t mouse_charpos,
29873 ptrdiff_t start_charpos,
29874 ptrdiff_t end_charpos,
29875 Lisp_Object before_string,
29876 Lisp_Object after_string,
29877 Lisp_Object disp_string)
29879 struct window *w = XWINDOW (window);
29880 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29881 struct glyph_row *r1, *r2;
29882 struct glyph *glyph, *end;
29883 ptrdiff_t ignore, pos;
29884 int x;
29886 eassert (NILP (disp_string) || STRINGP (disp_string));
29887 eassert (NILP (before_string) || STRINGP (before_string));
29888 eassert (NILP (after_string) || STRINGP (after_string));
29890 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
29891 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
29892 if (r1 == NULL)
29893 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29894 /* If the before-string or display-string contains newlines,
29895 rows_from_pos_range skips to its last row. Move back. */
29896 if (!NILP (before_string) || !NILP (disp_string))
29898 struct glyph_row *prev;
29899 while ((prev = r1 - 1, prev >= first)
29900 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
29901 && prev->used[TEXT_AREA] > 0)
29903 struct glyph *beg = prev->glyphs[TEXT_AREA];
29904 glyph = beg + prev->used[TEXT_AREA];
29905 while (--glyph >= beg && NILP (glyph->object));
29906 if (glyph < beg
29907 || !(EQ (glyph->object, before_string)
29908 || EQ (glyph->object, disp_string)))
29909 break;
29910 r1 = prev;
29913 if (r2 == NULL)
29915 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29916 hlinfo->mouse_face_past_end = true;
29918 else if (!NILP (after_string))
29920 /* If the after-string has newlines, advance to its last row. */
29921 struct glyph_row *next;
29922 struct glyph_row *last
29923 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29925 for (next = r2 + 1;
29926 next <= last
29927 && next->used[TEXT_AREA] > 0
29928 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
29929 ++next)
29930 r2 = next;
29932 /* The rest of the display engine assumes that mouse_face_beg_row is
29933 either above mouse_face_end_row or identical to it. But with
29934 bidi-reordered continued lines, the row for START_CHARPOS could
29935 be below the row for END_CHARPOS. If so, swap the rows and store
29936 them in correct order. */
29937 if (r1->y > r2->y)
29939 struct glyph_row *tem = r2;
29941 r2 = r1;
29942 r1 = tem;
29945 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
29946 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
29948 /* For a bidi-reordered row, the positions of BEFORE_STRING,
29949 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
29950 could be anywhere in the row and in any order. The strategy
29951 below is to find the leftmost and the rightmost glyph that
29952 belongs to either of these 3 strings, or whose position is
29953 between START_CHARPOS and END_CHARPOS, and highlight all the
29954 glyphs between those two. This may cover more than just the text
29955 between START_CHARPOS and END_CHARPOS if the range of characters
29956 strides the bidi level boundary, e.g. if the beginning is in R2L
29957 text while the end is in L2R text or vice versa. */
29958 if (!r1->reversed_p)
29960 /* This row is in a left to right paragraph. Scan it left to
29961 right. */
29962 glyph = r1->glyphs[TEXT_AREA];
29963 end = glyph + r1->used[TEXT_AREA];
29964 x = r1->x;
29966 /* Skip truncation glyphs at the start of the glyph row. */
29967 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29968 for (; glyph < end
29969 && NILP (glyph->object)
29970 && glyph->charpos < 0;
29971 ++glyph)
29972 x += glyph->pixel_width;
29974 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29975 or DISP_STRING, and the first glyph from buffer whose
29976 position is between START_CHARPOS and END_CHARPOS. */
29977 for (; glyph < end
29978 && !NILP (glyph->object)
29979 && !EQ (glyph->object, disp_string)
29980 && !(BUFFERP (glyph->object)
29981 && (glyph->charpos >= start_charpos
29982 && glyph->charpos < end_charpos));
29983 ++glyph)
29985 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29986 are present at buffer positions between START_CHARPOS and
29987 END_CHARPOS, or if they come from an overlay. */
29988 if (EQ (glyph->object, before_string))
29990 pos = string_buffer_position (before_string,
29991 start_charpos);
29992 /* If pos == 0, it means before_string came from an
29993 overlay, not from a buffer position. */
29994 if (!pos || (pos >= start_charpos && pos < end_charpos))
29995 break;
29997 else if (EQ (glyph->object, after_string))
29999 pos = string_buffer_position (after_string, end_charpos);
30000 if (!pos || (pos >= start_charpos && pos < end_charpos))
30001 break;
30003 x += glyph->pixel_width;
30005 hlinfo->mouse_face_beg_x = x;
30006 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30008 else
30010 /* This row is in a right to left paragraph. Scan it right to
30011 left. */
30012 struct glyph *g;
30014 end = r1->glyphs[TEXT_AREA] - 1;
30015 glyph = end + r1->used[TEXT_AREA];
30017 /* Skip truncation glyphs at the start of the glyph row. */
30018 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
30019 for (; glyph > end
30020 && NILP (glyph->object)
30021 && glyph->charpos < 0;
30022 --glyph)
30025 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
30026 or DISP_STRING, and the first glyph from buffer whose
30027 position is between START_CHARPOS and END_CHARPOS. */
30028 for (; glyph > end
30029 && !NILP (glyph->object)
30030 && !EQ (glyph->object, disp_string)
30031 && !(BUFFERP (glyph->object)
30032 && (glyph->charpos >= start_charpos
30033 && glyph->charpos < end_charpos));
30034 --glyph)
30036 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30037 are present at buffer positions between START_CHARPOS and
30038 END_CHARPOS, or if they come from an overlay. */
30039 if (EQ (glyph->object, before_string))
30041 pos = string_buffer_position (before_string, start_charpos);
30042 /* If pos == 0, it means before_string came from an
30043 overlay, not from a buffer position. */
30044 if (!pos || (pos >= start_charpos && pos < end_charpos))
30045 break;
30047 else if (EQ (glyph->object, after_string))
30049 pos = string_buffer_position (after_string, end_charpos);
30050 if (!pos || (pos >= start_charpos && pos < end_charpos))
30051 break;
30055 glyph++; /* first glyph to the right of the highlighted area */
30056 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
30057 x += g->pixel_width;
30058 hlinfo->mouse_face_beg_x = x;
30059 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30062 /* If the highlight ends in a different row, compute GLYPH and END
30063 for the end row. Otherwise, reuse the values computed above for
30064 the row where the highlight begins. */
30065 if (r2 != r1)
30067 if (!r2->reversed_p)
30069 glyph = r2->glyphs[TEXT_AREA];
30070 end = glyph + r2->used[TEXT_AREA];
30071 x = r2->x;
30073 else
30075 end = r2->glyphs[TEXT_AREA] - 1;
30076 glyph = end + r2->used[TEXT_AREA];
30080 if (!r2->reversed_p)
30082 /* Skip truncation and continuation glyphs near the end of the
30083 row, and also blanks and stretch glyphs inserted by
30084 extend_face_to_end_of_line. */
30085 while (end > glyph
30086 && NILP ((end - 1)->object))
30087 --end;
30088 /* Scan the rest of the glyph row from the end, looking for the
30089 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30090 DISP_STRING, or whose position is between START_CHARPOS
30091 and END_CHARPOS */
30092 for (--end;
30093 end > glyph
30094 && !NILP (end->object)
30095 && !EQ (end->object, disp_string)
30096 && !(BUFFERP (end->object)
30097 && (end->charpos >= start_charpos
30098 && end->charpos < end_charpos));
30099 --end)
30101 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30102 are present at buffer positions between START_CHARPOS and
30103 END_CHARPOS, or if they come from an overlay. */
30104 if (EQ (end->object, before_string))
30106 pos = string_buffer_position (before_string, start_charpos);
30107 if (!pos || (pos >= start_charpos && pos < end_charpos))
30108 break;
30110 else if (EQ (end->object, after_string))
30112 pos = string_buffer_position (after_string, end_charpos);
30113 if (!pos || (pos >= start_charpos && pos < end_charpos))
30114 break;
30117 /* Find the X coordinate of the last glyph to be highlighted. */
30118 for (; glyph <= end; ++glyph)
30119 x += glyph->pixel_width;
30121 hlinfo->mouse_face_end_x = x;
30122 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
30124 else
30126 /* Skip truncation and continuation glyphs near the end of the
30127 row, and also blanks and stretch glyphs inserted by
30128 extend_face_to_end_of_line. */
30129 x = r2->x;
30130 end++;
30131 while (end < glyph
30132 && NILP (end->object))
30134 x += end->pixel_width;
30135 ++end;
30137 /* Scan the rest of the glyph row from the end, looking for the
30138 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30139 DISP_STRING, or whose position is between START_CHARPOS
30140 and END_CHARPOS */
30141 for ( ;
30142 end < glyph
30143 && !NILP (end->object)
30144 && !EQ (end->object, disp_string)
30145 && !(BUFFERP (end->object)
30146 && (end->charpos >= start_charpos
30147 && end->charpos < end_charpos));
30148 ++end)
30150 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30151 are present at buffer positions between START_CHARPOS and
30152 END_CHARPOS, or if they come from an overlay. */
30153 if (EQ (end->object, before_string))
30155 pos = string_buffer_position (before_string, start_charpos);
30156 if (!pos || (pos >= start_charpos && pos < end_charpos))
30157 break;
30159 else if (EQ (end->object, after_string))
30161 pos = string_buffer_position (after_string, end_charpos);
30162 if (!pos || (pos >= start_charpos && pos < end_charpos))
30163 break;
30165 x += end->pixel_width;
30167 /* If we exited the above loop because we arrived at the last
30168 glyph of the row, and its buffer position is still not in
30169 range, it means the last character in range is the preceding
30170 newline. Bump the end column and x values to get past the
30171 last glyph. */
30172 if (end == glyph
30173 && BUFFERP (end->object)
30174 && (end->charpos < start_charpos
30175 || end->charpos >= end_charpos))
30177 x += end->pixel_width;
30178 ++end;
30180 hlinfo->mouse_face_end_x = x;
30181 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
30184 hlinfo->mouse_face_window = window;
30185 hlinfo->mouse_face_face_id
30186 = face_at_buffer_position (w, mouse_charpos, &ignore,
30187 mouse_charpos + 1,
30188 !hlinfo->mouse_face_hidden, -1);
30189 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30192 /* The following function is not used anymore (replaced with
30193 mouse_face_from_string_pos), but I leave it here for the time
30194 being, in case someone would. */
30196 #if false /* not used */
30198 /* Find the position of the glyph for position POS in OBJECT in
30199 window W's current matrix, and return in *X, *Y the pixel
30200 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
30202 RIGHT_P means return the position of the right edge of the glyph.
30203 !RIGHT_P means return the left edge position.
30205 If no glyph for POS exists in the matrix, return the position of
30206 the glyph with the next smaller position that is in the matrix, if
30207 RIGHT_P is false. If RIGHT_P, and no glyph for POS
30208 exists in the matrix, return the position of the glyph with the
30209 next larger position in OBJECT.
30211 Value is true if a glyph was found. */
30213 static bool
30214 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
30215 int *hpos, int *vpos, int *x, int *y, bool right_p)
30217 int yb = window_text_bottom_y (w);
30218 struct glyph_row *r;
30219 struct glyph *best_glyph = NULL;
30220 struct glyph_row *best_row = NULL;
30221 int best_x = 0;
30223 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30224 r->enabled_p && r->y < yb;
30225 ++r)
30227 struct glyph *g = r->glyphs[TEXT_AREA];
30228 struct glyph *e = g + r->used[TEXT_AREA];
30229 int gx;
30231 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30232 if (EQ (g->object, object))
30234 if (g->charpos == pos)
30236 best_glyph = g;
30237 best_x = gx;
30238 best_row = r;
30239 goto found;
30241 else if (best_glyph == NULL
30242 || ((eabs (g->charpos - pos)
30243 < eabs (best_glyph->charpos - pos))
30244 && (right_p
30245 ? g->charpos < pos
30246 : g->charpos > pos)))
30248 best_glyph = g;
30249 best_x = gx;
30250 best_row = r;
30255 found:
30257 if (best_glyph)
30259 *x = best_x;
30260 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
30262 if (right_p)
30264 *x += best_glyph->pixel_width;
30265 ++*hpos;
30268 *y = best_row->y;
30269 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
30272 return best_glyph != NULL;
30274 #endif /* not used */
30276 /* Find the positions of the first and the last glyphs in window W's
30277 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
30278 (assumed to be a string), and return in HLINFO's mouse_face_*
30279 members the pixel and column/row coordinates of those glyphs. */
30281 static void
30282 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
30283 Lisp_Object object,
30284 ptrdiff_t startpos, ptrdiff_t endpos)
30286 int yb = window_text_bottom_y (w);
30287 struct glyph_row *r;
30288 struct glyph *g, *e;
30289 int gx;
30290 bool found = false;
30292 /* Find the glyph row with at least one position in the range
30293 [STARTPOS..ENDPOS), and the first glyph in that row whose
30294 position belongs to that range. */
30295 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30296 r->enabled_p && r->y < yb;
30297 ++r)
30299 if (!r->reversed_p)
30301 g = r->glyphs[TEXT_AREA];
30302 e = g + r->used[TEXT_AREA];
30303 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30304 if (EQ (g->object, object)
30305 && startpos <= g->charpos && g->charpos < endpos)
30307 hlinfo->mouse_face_beg_row
30308 = MATRIX_ROW_VPOS (r, w->current_matrix);
30309 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30310 hlinfo->mouse_face_beg_x = gx;
30311 found = true;
30312 break;
30315 else
30317 struct glyph *g1;
30319 e = r->glyphs[TEXT_AREA];
30320 g = e + r->used[TEXT_AREA];
30321 for ( ; g > e; --g)
30322 if (EQ ((g-1)->object, object)
30323 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
30325 hlinfo->mouse_face_beg_row
30326 = MATRIX_ROW_VPOS (r, w->current_matrix);
30327 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30328 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
30329 gx += g1->pixel_width;
30330 hlinfo->mouse_face_beg_x = gx;
30331 found = true;
30332 break;
30335 if (found)
30336 break;
30339 if (!found)
30340 return;
30342 /* Starting with the next row, look for the first row which does NOT
30343 include any glyphs whose positions are in the range. */
30344 for (++r; r->enabled_p && r->y < yb; ++r)
30346 g = r->glyphs[TEXT_AREA];
30347 e = g + r->used[TEXT_AREA];
30348 found = false;
30349 for ( ; g < e; ++g)
30350 if (EQ (g->object, object)
30351 && startpos <= g->charpos && g->charpos < endpos)
30353 found = true;
30354 break;
30356 if (!found)
30357 break;
30360 /* The highlighted region ends on the previous row. */
30361 r--;
30363 /* Set the end row. */
30364 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
30366 /* Compute and set the end column and the end column's horizontal
30367 pixel coordinate. */
30368 if (!r->reversed_p)
30370 g = r->glyphs[TEXT_AREA];
30371 e = g + r->used[TEXT_AREA];
30372 for ( ; e > g; --e)
30373 if (EQ ((e-1)->object, object)
30374 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
30375 break;
30376 hlinfo->mouse_face_end_col = e - g;
30378 for (gx = r->x; g < e; ++g)
30379 gx += g->pixel_width;
30380 hlinfo->mouse_face_end_x = gx;
30382 else
30384 e = r->glyphs[TEXT_AREA];
30385 g = e + r->used[TEXT_AREA];
30386 for (gx = r->x ; e < g; ++e)
30388 if (EQ (e->object, object)
30389 && startpos <= e->charpos && e->charpos < endpos)
30390 break;
30391 gx += e->pixel_width;
30393 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
30394 hlinfo->mouse_face_end_x = gx;
30398 #ifdef HAVE_WINDOW_SYSTEM
30400 /* See if position X, Y is within a hot-spot of an image. */
30402 static bool
30403 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
30405 if (!CONSP (hot_spot))
30406 return false;
30408 if (EQ (XCAR (hot_spot), Qrect))
30410 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
30411 Lisp_Object rect = XCDR (hot_spot);
30412 Lisp_Object tem;
30413 if (!CONSP (rect))
30414 return false;
30415 if (!CONSP (XCAR (rect)))
30416 return false;
30417 if (!CONSP (XCDR (rect)))
30418 return false;
30419 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
30420 return false;
30421 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
30422 return false;
30423 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
30424 return false;
30425 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
30426 return false;
30427 return true;
30429 else if (EQ (XCAR (hot_spot), Qcircle))
30431 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
30432 Lisp_Object circ = XCDR (hot_spot);
30433 Lisp_Object lr, lx0, ly0;
30434 if (CONSP (circ)
30435 && CONSP (XCAR (circ))
30436 && (lr = XCDR (circ), NUMBERP (lr))
30437 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
30438 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
30440 double r = XFLOATINT (lr);
30441 double dx = XINT (lx0) - x;
30442 double dy = XINT (ly0) - y;
30443 return (dx * dx + dy * dy <= r * r);
30446 else if (EQ (XCAR (hot_spot), Qpoly))
30448 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
30449 if (VECTORP (XCDR (hot_spot)))
30451 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
30452 Lisp_Object *poly = v->contents;
30453 ptrdiff_t n = v->header.size;
30454 ptrdiff_t i;
30455 bool inside = false;
30456 Lisp_Object lx, ly;
30457 int x0, y0;
30459 /* Need an even number of coordinates, and at least 3 edges. */
30460 if (n < 6 || n & 1)
30461 return false;
30463 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
30464 If count is odd, we are inside polygon. Pixels on edges
30465 may or may not be included depending on actual geometry of the
30466 polygon. */
30467 if ((lx = poly[n-2], !INTEGERP (lx))
30468 || (ly = poly[n-1], !INTEGERP (lx)))
30469 return false;
30470 x0 = XINT (lx), y0 = XINT (ly);
30471 for (i = 0; i < n; i += 2)
30473 int x1 = x0, y1 = y0;
30474 if ((lx = poly[i], !INTEGERP (lx))
30475 || (ly = poly[i+1], !INTEGERP (ly)))
30476 return false;
30477 x0 = XINT (lx), y0 = XINT (ly);
30479 /* Does this segment cross the X line? */
30480 if (x0 >= x)
30482 if (x1 >= x)
30483 continue;
30485 else if (x1 < x)
30486 continue;
30487 if (y > y0 && y > y1)
30488 continue;
30489 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
30490 inside = !inside;
30492 return inside;
30495 return false;
30498 Lisp_Object
30499 find_hot_spot (Lisp_Object map, int x, int y)
30501 while (CONSP (map))
30503 if (CONSP (XCAR (map))
30504 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
30505 return XCAR (map);
30506 map = XCDR (map);
30509 return Qnil;
30512 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
30513 3, 3, 0,
30514 doc: /* Lookup in image map MAP coordinates X and Y.
30515 An image map is an alist where each element has the format (AREA ID PLIST).
30516 An AREA is specified as either a rectangle, a circle, or a polygon:
30517 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
30518 pixel coordinates of the upper left and bottom right corners.
30519 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
30520 and the radius of the circle; r may be a float or integer.
30521 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
30522 vector describes one corner in the polygon.
30523 Returns the alist element for the first matching AREA in MAP. */)
30524 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
30526 if (NILP (map))
30527 return Qnil;
30529 CHECK_NUMBER (x);
30530 CHECK_NUMBER (y);
30532 return find_hot_spot (map,
30533 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
30534 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
30536 #endif /* HAVE_WINDOW_SYSTEM */
30539 /* Display frame CURSOR, optionally using shape defined by POINTER. */
30540 static void
30541 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
30543 #ifdef HAVE_WINDOW_SYSTEM
30544 if (!FRAME_WINDOW_P (f))
30545 return;
30547 /* Do not change cursor shape while dragging mouse. */
30548 if (EQ (do_mouse_tracking, Qdragging))
30549 return;
30551 if (!NILP (pointer))
30553 if (EQ (pointer, Qarrow))
30554 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30555 else if (EQ (pointer, Qhand))
30556 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
30557 else if (EQ (pointer, Qtext))
30558 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30559 else if (EQ (pointer, intern ("hdrag")))
30560 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30561 else if (EQ (pointer, intern ("nhdrag")))
30562 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30563 # ifdef HAVE_X_WINDOWS
30564 else if (EQ (pointer, intern ("vdrag")))
30565 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30566 # endif
30567 else if (EQ (pointer, intern ("hourglass")))
30568 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
30569 else if (EQ (pointer, Qmodeline))
30570 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
30571 else
30572 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30575 if (cursor != No_Cursor)
30576 FRAME_RIF (f)->define_frame_cursor (f, cursor);
30577 #endif
30580 /* Take proper action when mouse has moved to the mode or header line
30581 or marginal area AREA of window W, x-position X and y-position Y.
30582 X is relative to the start of the text display area of W, so the
30583 width of bitmap areas and scroll bars must be subtracted to get a
30584 position relative to the start of the mode line. */
30586 static void
30587 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
30588 enum window_part area)
30590 struct window *w = XWINDOW (window);
30591 struct frame *f = XFRAME (w->frame);
30592 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30593 #ifdef HAVE_WINDOW_SYSTEM
30594 Display_Info *dpyinfo;
30595 #endif
30596 Cursor cursor = No_Cursor;
30597 Lisp_Object pointer = Qnil;
30598 int dx, dy, width, height;
30599 ptrdiff_t charpos;
30600 Lisp_Object string, object = Qnil;
30601 Lisp_Object pos UNINIT;
30602 Lisp_Object mouse_face;
30603 int original_x_pixel = x;
30604 struct glyph * glyph = NULL, * row_start_glyph = NULL;
30605 struct glyph_row *row UNINIT;
30607 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
30609 int x0;
30610 struct glyph *end;
30612 /* Kludge alert: mode_line_string takes X/Y in pixels, but
30613 returns them in row/column units! */
30614 string = mode_line_string (w, area, &x, &y, &charpos,
30615 &object, &dx, &dy, &width, &height);
30617 row = (area == ON_MODE_LINE
30618 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
30619 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
30621 /* Find the glyph under the mouse pointer. */
30622 if (row->mode_line_p && row->enabled_p)
30624 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
30625 end = glyph + row->used[TEXT_AREA];
30627 for (x0 = original_x_pixel;
30628 glyph < end && x0 >= glyph->pixel_width;
30629 ++glyph)
30630 x0 -= glyph->pixel_width;
30632 if (glyph >= end)
30633 glyph = NULL;
30636 else
30638 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
30639 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
30640 returns them in row/column units! */
30641 string = marginal_area_string (w, area, &x, &y, &charpos,
30642 &object, &dx, &dy, &width, &height);
30645 Lisp_Object help = Qnil;
30647 #ifdef HAVE_WINDOW_SYSTEM
30648 if (IMAGEP (object))
30650 Lisp_Object image_map, hotspot;
30651 if ((image_map = Fplist_get (XCDR (object), QCmap),
30652 !NILP (image_map))
30653 && (hotspot = find_hot_spot (image_map, dx, dy),
30654 CONSP (hotspot))
30655 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30657 Lisp_Object plist;
30659 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
30660 If so, we could look for mouse-enter, mouse-leave
30661 properties in PLIST (and do something...). */
30662 hotspot = XCDR (hotspot);
30663 if (CONSP (hotspot)
30664 && (plist = XCAR (hotspot), CONSP (plist)))
30666 pointer = Fplist_get (plist, Qpointer);
30667 if (NILP (pointer))
30668 pointer = Qhand;
30669 help = Fplist_get (plist, Qhelp_echo);
30670 if (!NILP (help))
30672 help_echo_string = help;
30673 XSETWINDOW (help_echo_window, w);
30674 help_echo_object = w->contents;
30675 help_echo_pos = charpos;
30679 if (NILP (pointer))
30680 pointer = Fplist_get (XCDR (object), QCpointer);
30682 #endif /* HAVE_WINDOW_SYSTEM */
30684 if (STRINGP (string))
30685 pos = make_number (charpos);
30687 /* Set the help text and mouse pointer. If the mouse is on a part
30688 of the mode line without any text (e.g. past the right edge of
30689 the mode line text), use the default help text and pointer. */
30690 if (STRINGP (string) || area == ON_MODE_LINE)
30692 /* Arrange to display the help by setting the global variables
30693 help_echo_string, help_echo_object, and help_echo_pos. */
30694 if (NILP (help))
30696 if (STRINGP (string))
30697 help = Fget_text_property (pos, Qhelp_echo, string);
30699 if (!NILP (help))
30701 help_echo_string = help;
30702 XSETWINDOW (help_echo_window, w);
30703 help_echo_object = string;
30704 help_echo_pos = charpos;
30706 else if (area == ON_MODE_LINE)
30708 Lisp_Object default_help
30709 = buffer_local_value (Qmode_line_default_help_echo,
30710 w->contents);
30712 if (STRINGP (default_help))
30714 help_echo_string = default_help;
30715 XSETWINDOW (help_echo_window, w);
30716 help_echo_object = Qnil;
30717 help_echo_pos = -1;
30722 #ifdef HAVE_WINDOW_SYSTEM
30723 /* Change the mouse pointer according to what is under it. */
30724 if (FRAME_WINDOW_P (f))
30726 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
30727 || minibuf_level
30728 || NILP (Vresize_mini_windows));
30730 dpyinfo = FRAME_DISPLAY_INFO (f);
30731 if (STRINGP (string))
30733 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30735 if (NILP (pointer))
30736 pointer = Fget_text_property (pos, Qpointer, string);
30738 /* Change the mouse pointer according to what is under X/Y. */
30739 if (NILP (pointer)
30740 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
30742 Lisp_Object map;
30743 map = Fget_text_property (pos, Qlocal_map, string);
30744 if (!KEYMAPP (map))
30745 map = Fget_text_property (pos, Qkeymap, string);
30746 if (!KEYMAPP (map) && draggable)
30747 cursor = dpyinfo->vertical_scroll_bar_cursor;
30750 else if (draggable)
30751 /* Default mode-line pointer. */
30752 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30754 #endif
30757 /* Change the mouse face according to what is under X/Y. */
30758 bool mouse_face_shown = false;
30759 if (STRINGP (string))
30761 mouse_face = Fget_text_property (pos, Qmouse_face, string);
30762 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
30763 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
30764 && glyph)
30766 Lisp_Object b, e;
30768 struct glyph * tmp_glyph;
30770 int gpos;
30771 int gseq_length;
30772 int total_pixel_width;
30773 ptrdiff_t begpos, endpos, ignore;
30775 int vpos, hpos;
30777 b = Fprevious_single_property_change (make_number (charpos + 1),
30778 Qmouse_face, string, Qnil);
30779 if (NILP (b))
30780 begpos = 0;
30781 else
30782 begpos = XINT (b);
30784 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
30785 if (NILP (e))
30786 endpos = SCHARS (string);
30787 else
30788 endpos = XINT (e);
30790 /* Calculate the glyph position GPOS of GLYPH in the
30791 displayed string, relative to the beginning of the
30792 highlighted part of the string.
30794 Note: GPOS is different from CHARPOS. CHARPOS is the
30795 position of GLYPH in the internal string object. A mode
30796 line string format has structures which are converted to
30797 a flattened string by the Emacs Lisp interpreter. The
30798 internal string is an element of those structures. The
30799 displayed string is the flattened string. */
30800 tmp_glyph = row_start_glyph;
30801 while (tmp_glyph < glyph
30802 && (!(EQ (tmp_glyph->object, glyph->object)
30803 && begpos <= tmp_glyph->charpos
30804 && tmp_glyph->charpos < endpos)))
30805 tmp_glyph++;
30806 gpos = glyph - tmp_glyph;
30808 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
30809 the highlighted part of the displayed string to which
30810 GLYPH belongs. Note: GSEQ_LENGTH is different from
30811 SCHARS (STRING), because the latter returns the length of
30812 the internal string. */
30813 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
30814 tmp_glyph > glyph
30815 && (!(EQ (tmp_glyph->object, glyph->object)
30816 && begpos <= tmp_glyph->charpos
30817 && tmp_glyph->charpos < endpos));
30818 tmp_glyph--)
30820 gseq_length = gpos + (tmp_glyph - glyph) + 1;
30822 /* Calculate the total pixel width of all the glyphs between
30823 the beginning of the highlighted area and GLYPH. */
30824 total_pixel_width = 0;
30825 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
30826 total_pixel_width += tmp_glyph->pixel_width;
30828 /* Pre calculation of re-rendering position. Note: X is in
30829 column units here, after the call to mode_line_string or
30830 marginal_area_string. */
30831 hpos = x - gpos;
30832 vpos = (area == ON_MODE_LINE
30833 ? (w->current_matrix)->nrows - 1
30834 : 0);
30836 /* If GLYPH's position is included in the region that is
30837 already drawn in mouse face, we have nothing to do. */
30838 if ( EQ (window, hlinfo->mouse_face_window)
30839 && (!row->reversed_p
30840 ? (hlinfo->mouse_face_beg_col <= hpos
30841 && hpos < hlinfo->mouse_face_end_col)
30842 /* In R2L rows we swap BEG and END, see below. */
30843 : (hlinfo->mouse_face_end_col <= hpos
30844 && hpos < hlinfo->mouse_face_beg_col))
30845 && hlinfo->mouse_face_beg_row == vpos )
30846 return;
30848 if (clear_mouse_face (hlinfo))
30849 cursor = No_Cursor;
30851 if (!row->reversed_p)
30853 hlinfo->mouse_face_beg_col = hpos;
30854 hlinfo->mouse_face_beg_x = original_x_pixel
30855 - (total_pixel_width + dx);
30856 hlinfo->mouse_face_end_col = hpos + gseq_length;
30857 hlinfo->mouse_face_end_x = 0;
30859 else
30861 /* In R2L rows, show_mouse_face expects BEG and END
30862 coordinates to be swapped. */
30863 hlinfo->mouse_face_end_col = hpos;
30864 hlinfo->mouse_face_end_x = original_x_pixel
30865 - (total_pixel_width + dx);
30866 hlinfo->mouse_face_beg_col = hpos + gseq_length;
30867 hlinfo->mouse_face_beg_x = 0;
30870 hlinfo->mouse_face_beg_row = vpos;
30871 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
30872 hlinfo->mouse_face_past_end = false;
30873 hlinfo->mouse_face_window = window;
30875 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
30876 charpos,
30877 0, &ignore,
30878 glyph->face_id,
30879 true);
30880 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30881 mouse_face_shown = true;
30883 if (NILP (pointer))
30884 pointer = Qhand;
30888 /* If mouse-face doesn't need to be shown, clear any existing
30889 mouse-face. */
30890 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
30891 clear_mouse_face (hlinfo);
30893 define_frame_cursor1 (f, cursor, pointer);
30897 /* EXPORT:
30898 Take proper action when the mouse has moved to position X, Y on
30899 frame F with regards to highlighting portions of display that have
30900 mouse-face properties. Also de-highlight portions of display where
30901 the mouse was before, set the mouse pointer shape as appropriate
30902 for the mouse coordinates, and activate help echo (tooltips).
30903 X and Y can be negative or out of range. */
30905 void
30906 note_mouse_highlight (struct frame *f, int x, int y)
30908 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30909 enum window_part part = ON_NOTHING;
30910 Lisp_Object window;
30911 struct window *w;
30912 Cursor cursor = No_Cursor;
30913 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
30914 struct buffer *b;
30916 /* When a menu is active, don't highlight because this looks odd. */
30917 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
30918 if (popup_activated ())
30919 return;
30920 #endif
30922 if (!f->glyphs_initialized_p
30923 || f->pointer_invisible)
30924 return;
30926 hlinfo->mouse_face_mouse_x = x;
30927 hlinfo->mouse_face_mouse_y = y;
30928 hlinfo->mouse_face_mouse_frame = f;
30930 if (hlinfo->mouse_face_defer)
30931 return;
30933 /* Which window is that in? */
30934 window = window_from_coordinates (f, x, y, &part, true);
30936 /* If displaying active text in another window, clear that. */
30937 if (! EQ (window, hlinfo->mouse_face_window)
30938 /* Also clear if we move out of text area in same window. */
30939 || (!NILP (hlinfo->mouse_face_window)
30940 && !NILP (window)
30941 && part != ON_TEXT
30942 && part != ON_MODE_LINE
30943 && part != ON_HEADER_LINE))
30944 clear_mouse_face (hlinfo);
30946 /* Reset help_echo_string. It will get recomputed below. */
30947 help_echo_string = Qnil;
30949 #ifdef HAVE_WINDOW_SYSTEM
30950 /* If the cursor is on the internal border of FRAME and FRAME's
30951 internal border is draggable, provide some visual feedback. */
30952 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0
30953 && !NILP (get_frame_param (f, Qdrag_internal_border)))
30955 enum internal_border_part part = frame_internal_border_part (f, x, y);
30957 switch (part)
30959 case INTERNAL_BORDER_NONE:
30960 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
30961 /* Reset cursor. */
30962 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30963 break;
30964 case INTERNAL_BORDER_LEFT_EDGE:
30965 cursor = FRAME_X_OUTPUT (f)->left_edge_cursor;
30966 break;
30967 case INTERNAL_BORDER_TOP_LEFT_CORNER:
30968 cursor = FRAME_X_OUTPUT (f)->top_left_corner_cursor;
30969 break;
30970 case INTERNAL_BORDER_TOP_EDGE:
30971 cursor = FRAME_X_OUTPUT (f)->top_edge_cursor;
30972 break;
30973 case INTERNAL_BORDER_TOP_RIGHT_CORNER:
30974 cursor = FRAME_X_OUTPUT (f)->top_right_corner_cursor;
30975 break;
30976 case INTERNAL_BORDER_RIGHT_EDGE:
30977 cursor = FRAME_X_OUTPUT (f)->right_edge_cursor;
30978 break;
30979 case INTERNAL_BORDER_BOTTOM_RIGHT_CORNER:
30980 cursor = FRAME_X_OUTPUT (f)->bottom_right_corner_cursor;
30981 break;
30982 case INTERNAL_BORDER_BOTTOM_EDGE:
30983 cursor = FRAME_X_OUTPUT (f)->bottom_edge_cursor;
30984 break;
30985 case INTERNAL_BORDER_BOTTOM_LEFT_CORNER:
30986 cursor = FRAME_X_OUTPUT (f)->bottom_left_corner_cursor;
30987 break;
30988 default:
30989 /* This should not happen. */
30990 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
30991 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30994 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
30996 /* Do we really want a help echo here? */
30997 help_echo_string = build_string ("drag-mouse-1: resize frame");
30998 goto set_cursor;
31001 #endif /* HAVE_WINDOW_SYSTEM */
31003 /* Not on a window -> return. */
31004 if (!WINDOWP (window))
31005 return;
31007 /* Convert to window-relative pixel coordinates. */
31008 w = XWINDOW (window);
31009 frame_to_window_pixel_xy (w, &x, &y);
31011 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
31012 /* Handle tool-bar window differently since it doesn't display a
31013 buffer. */
31014 if (EQ (window, f->tool_bar_window))
31016 note_tool_bar_highlight (f, x, y);
31017 return;
31019 #endif
31021 /* Mouse is on the mode, header line or margin? */
31022 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
31023 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31025 note_mode_line_or_margin_highlight (window, x, y, part);
31027 #ifdef HAVE_WINDOW_SYSTEM
31028 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31030 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31031 /* Show non-text cursor (Bug#16647). */
31032 goto set_cursor;
31034 else
31035 #endif
31036 return;
31039 #ifdef HAVE_WINDOW_SYSTEM
31040 if (part == ON_VERTICAL_BORDER)
31042 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31043 help_echo_string = build_string ("drag-mouse-1: resize");
31044 goto set_cursor;
31046 else if (part == ON_RIGHT_DIVIDER)
31048 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31049 help_echo_string = build_string ("drag-mouse-1: resize");
31050 goto set_cursor;
31052 else if (part == ON_BOTTOM_DIVIDER)
31053 if (! WINDOW_BOTTOMMOST_P (w)
31054 || minibuf_level
31055 || NILP (Vresize_mini_windows))
31057 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
31058 help_echo_string = build_string ("drag-mouse-1: resize");
31059 goto set_cursor;
31061 else
31062 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31063 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
31064 || part == ON_VERTICAL_SCROLL_BAR
31065 || part == ON_HORIZONTAL_SCROLL_BAR)
31066 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31067 else
31068 cursor = FRAME_X_OUTPUT (f)->text_cursor;
31069 #endif
31071 /* Are we in a window whose display is up to date?
31072 And verify the buffer's text has not changed. */
31073 b = XBUFFER (w->contents);
31074 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
31076 int hpos, vpos, dx, dy, area = LAST_AREA;
31077 ptrdiff_t pos;
31078 struct glyph *glyph;
31079 Lisp_Object object;
31080 Lisp_Object mouse_face = Qnil, position;
31081 Lisp_Object *overlay_vec = NULL;
31082 ptrdiff_t i, noverlays;
31083 struct buffer *obuf;
31084 ptrdiff_t obegv, ozv;
31085 bool same_region;
31087 /* Find the glyph under X/Y. */
31088 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
31090 #ifdef HAVE_WINDOW_SYSTEM
31091 /* Look for :pointer property on image. */
31092 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
31094 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
31095 if (img != NULL && IMAGEP (img->spec))
31097 Lisp_Object image_map, hotspot;
31098 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
31099 !NILP (image_map))
31100 && (hotspot = find_hot_spot (image_map,
31101 glyph->slice.img.x + dx,
31102 glyph->slice.img.y + dy),
31103 CONSP (hotspot))
31104 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
31106 Lisp_Object plist;
31108 /* Could check XCAR (hotspot) to see if we enter/leave
31109 this hot-spot.
31110 If so, we could look for mouse-enter, mouse-leave
31111 properties in PLIST (and do something...). */
31112 hotspot = XCDR (hotspot);
31113 if (CONSP (hotspot)
31114 && (plist = XCAR (hotspot), CONSP (plist)))
31116 pointer = Fplist_get (plist, Qpointer);
31117 if (NILP (pointer))
31118 pointer = Qhand;
31119 help_echo_string = Fplist_get (plist, Qhelp_echo);
31120 if (!NILP (help_echo_string))
31122 help_echo_window = window;
31123 help_echo_object = glyph->object;
31124 help_echo_pos = glyph->charpos;
31128 if (NILP (pointer))
31129 pointer = Fplist_get (XCDR (img->spec), QCpointer);
31132 #endif /* HAVE_WINDOW_SYSTEM */
31134 /* Clear mouse face if X/Y not over text. */
31135 if (glyph == NULL
31136 || area != TEXT_AREA
31137 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
31138 /* Glyph's OBJECT is nil for glyphs inserted by the
31139 display engine for its internal purposes, like truncation
31140 and continuation glyphs and blanks beyond the end of
31141 line's text on text terminals. If we are over such a
31142 glyph, we are not over any text. */
31143 || NILP (glyph->object)
31144 /* R2L rows have a stretch glyph at their front, which
31145 stands for no text, whereas L2R rows have no glyphs at
31146 all beyond the end of text. Treat such stretch glyphs
31147 like we do with NULL glyphs in L2R rows. */
31148 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
31149 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
31150 && glyph->type == STRETCH_GLYPH
31151 && glyph->avoid_cursor_p))
31153 if (clear_mouse_face (hlinfo))
31154 cursor = No_Cursor;
31155 if (FRAME_WINDOW_P (f) && NILP (pointer))
31157 #ifdef HAVE_WINDOW_SYSTEM
31158 if (area != TEXT_AREA)
31159 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31160 else
31161 pointer = Vvoid_text_area_pointer;
31162 #endif
31164 goto set_cursor;
31167 pos = glyph->charpos;
31168 object = glyph->object;
31169 if (!STRINGP (object) && !BUFFERP (object))
31170 goto set_cursor;
31172 /* If we get an out-of-range value, return now; avoid an error. */
31173 if (BUFFERP (object) && pos > BUF_Z (b))
31174 goto set_cursor;
31176 /* Make the window's buffer temporarily current for
31177 overlays_at and compute_char_face. */
31178 obuf = current_buffer;
31179 current_buffer = b;
31180 obegv = BEGV;
31181 ozv = ZV;
31182 BEGV = BEG;
31183 ZV = Z;
31185 /* Is this char mouse-active or does it have help-echo? */
31186 position = make_number (pos);
31188 USE_SAFE_ALLOCA;
31190 if (BUFFERP (object))
31192 /* Put all the overlays we want in a vector in overlay_vec. */
31193 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
31194 /* Sort overlays into increasing priority order. */
31195 noverlays = sort_overlays (overlay_vec, noverlays, w);
31197 else
31198 noverlays = 0;
31200 if (NILP (Vmouse_highlight))
31202 clear_mouse_face (hlinfo);
31203 goto check_help_echo;
31206 same_region = coords_in_mouse_face_p (w, hpos, vpos);
31208 if (same_region)
31209 cursor = No_Cursor;
31211 /* Check mouse-face highlighting. */
31212 if (! same_region
31213 /* If there exists an overlay with mouse-face overlapping
31214 the one we are currently highlighting, we have to
31215 check if we enter the overlapping overlay, and then
31216 highlight only that. */
31217 || (OVERLAYP (hlinfo->mouse_face_overlay)
31218 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
31220 /* Find the highest priority overlay with a mouse-face. */
31221 Lisp_Object overlay = Qnil;
31222 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
31224 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
31225 if (!NILP (mouse_face))
31226 overlay = overlay_vec[i];
31229 /* If we're highlighting the same overlay as before, there's
31230 no need to do that again. */
31231 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
31232 goto check_help_echo;
31234 /* Clear the display of the old active region, if any. */
31235 if (clear_mouse_face (hlinfo))
31236 cursor = No_Cursor;
31238 /* Record the overlay, if any, to be highlighted. */
31239 hlinfo->mouse_face_overlay = overlay;
31241 /* If no overlay applies, get a text property. */
31242 if (NILP (overlay))
31243 mouse_face = Fget_text_property (position, Qmouse_face, object);
31245 /* Next, compute the bounds of the mouse highlighting and
31246 display it. */
31247 if (!NILP (mouse_face) && STRINGP (object))
31249 /* The mouse-highlighting comes from a display string
31250 with a mouse-face. */
31251 Lisp_Object s, e;
31252 ptrdiff_t ignore;
31254 s = Fprevious_single_property_change
31255 (make_number (pos + 1), Qmouse_face, object, Qnil);
31256 e = Fnext_single_property_change
31257 (position, Qmouse_face, object, Qnil);
31258 if (NILP (s))
31259 s = make_number (0);
31260 if (NILP (e))
31261 e = make_number (SCHARS (object));
31262 mouse_face_from_string_pos (w, hlinfo, object,
31263 XINT (s), XINT (e));
31264 hlinfo->mouse_face_past_end = false;
31265 hlinfo->mouse_face_window = window;
31266 hlinfo->mouse_face_face_id
31267 = face_at_string_position (w, object, pos, 0, &ignore,
31268 glyph->face_id, true);
31269 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
31270 cursor = No_Cursor;
31272 else
31274 /* The mouse-highlighting, if any, comes from an overlay
31275 or text property in the buffer. */
31276 Lisp_Object buffer UNINIT;
31277 Lisp_Object disp_string UNINIT;
31279 if (STRINGP (object))
31281 /* If we are on a display string with no mouse-face,
31282 check if the text under it has one. */
31283 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
31284 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31285 pos = string_buffer_position (object, start);
31286 if (pos > 0)
31288 mouse_face = get_char_property_and_overlay
31289 (make_number (pos), Qmouse_face, w->contents, &overlay);
31290 buffer = w->contents;
31291 disp_string = object;
31294 else
31296 buffer = object;
31297 disp_string = Qnil;
31300 if (!NILP (mouse_face))
31302 Lisp_Object before, after;
31303 Lisp_Object before_string, after_string;
31304 /* To correctly find the limits of mouse highlight
31305 in a bidi-reordered buffer, we must not use the
31306 optimization of limiting the search in
31307 previous-single-property-change and
31308 next-single-property-change, because
31309 rows_from_pos_range needs the real start and end
31310 positions to DTRT in this case. That's because
31311 the first row visible in a window does not
31312 necessarily display the character whose position
31313 is the smallest. */
31314 Lisp_Object lim1
31315 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31316 ? Fmarker_position (w->start)
31317 : Qnil;
31318 Lisp_Object lim2
31319 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31320 ? make_number (BUF_Z (XBUFFER (buffer))
31321 - w->window_end_pos)
31322 : Qnil;
31324 if (NILP (overlay))
31326 /* Handle the text property case. */
31327 before = Fprevious_single_property_change
31328 (make_number (pos + 1), Qmouse_face, buffer, lim1);
31329 after = Fnext_single_property_change
31330 (make_number (pos), Qmouse_face, buffer, lim2);
31331 before_string = after_string = Qnil;
31333 else
31335 /* Handle the overlay case. */
31336 before = Foverlay_start (overlay);
31337 after = Foverlay_end (overlay);
31338 before_string = Foverlay_get (overlay, Qbefore_string);
31339 after_string = Foverlay_get (overlay, Qafter_string);
31341 if (!STRINGP (before_string)) before_string = Qnil;
31342 if (!STRINGP (after_string)) after_string = Qnil;
31345 mouse_face_from_buffer_pos (window, hlinfo, pos,
31346 NILP (before)
31348 : XFASTINT (before),
31349 NILP (after)
31350 ? BUF_Z (XBUFFER (buffer))
31351 : XFASTINT (after),
31352 before_string, after_string,
31353 disp_string);
31354 cursor = No_Cursor;
31359 check_help_echo:
31361 /* Look for a `help-echo' property. */
31362 if (NILP (help_echo_string)) {
31363 Lisp_Object help, overlay;
31365 /* Check overlays first. */
31366 help = overlay = Qnil;
31367 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
31369 overlay = overlay_vec[i];
31370 help = Foverlay_get (overlay, Qhelp_echo);
31373 if (!NILP (help))
31375 help_echo_string = help;
31376 help_echo_window = window;
31377 help_echo_object = overlay;
31378 help_echo_pos = pos;
31380 else
31382 Lisp_Object obj = glyph->object;
31383 ptrdiff_t charpos = glyph->charpos;
31385 /* Try text properties. */
31386 if (STRINGP (obj)
31387 && charpos >= 0
31388 && charpos < SCHARS (obj))
31390 help = Fget_text_property (make_number (charpos),
31391 Qhelp_echo, obj);
31392 if (NILP (help))
31394 /* If the string itself doesn't specify a help-echo,
31395 see if the buffer text ``under'' it does. */
31396 struct glyph_row *r
31397 = MATRIX_ROW (w->current_matrix, vpos);
31398 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31399 ptrdiff_t p = string_buffer_position (obj, start);
31400 if (p > 0)
31402 help = Fget_char_property (make_number (p),
31403 Qhelp_echo, w->contents);
31404 if (!NILP (help))
31406 charpos = p;
31407 obj = w->contents;
31412 else if (BUFFERP (obj)
31413 && charpos >= BEGV
31414 && charpos < ZV)
31415 help = Fget_text_property (make_number (charpos), Qhelp_echo,
31416 obj);
31418 if (!NILP (help))
31420 help_echo_string = help;
31421 help_echo_window = window;
31422 help_echo_object = obj;
31423 help_echo_pos = charpos;
31428 #ifdef HAVE_WINDOW_SYSTEM
31429 /* Look for a `pointer' property. */
31430 if (FRAME_WINDOW_P (f) && NILP (pointer))
31432 /* Check overlays first. */
31433 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
31434 pointer = Foverlay_get (overlay_vec[i], Qpointer);
31436 if (NILP (pointer))
31438 Lisp_Object obj = glyph->object;
31439 ptrdiff_t charpos = glyph->charpos;
31441 /* Try text properties. */
31442 if (STRINGP (obj)
31443 && charpos >= 0
31444 && charpos < SCHARS (obj))
31446 pointer = Fget_text_property (make_number (charpos),
31447 Qpointer, obj);
31448 if (NILP (pointer))
31450 /* If the string itself doesn't specify a pointer,
31451 see if the buffer text ``under'' it does. */
31452 struct glyph_row *r
31453 = MATRIX_ROW (w->current_matrix, vpos);
31454 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31455 ptrdiff_t p = string_buffer_position (obj, start);
31456 if (p > 0)
31457 pointer = Fget_char_property (make_number (p),
31458 Qpointer, w->contents);
31461 else if (BUFFERP (obj)
31462 && charpos >= BEGV
31463 && charpos < ZV)
31464 pointer = Fget_text_property (make_number (charpos),
31465 Qpointer, obj);
31468 #endif /* HAVE_WINDOW_SYSTEM */
31470 BEGV = obegv;
31471 ZV = ozv;
31472 current_buffer = obuf;
31473 SAFE_FREE ();
31476 set_cursor:
31477 define_frame_cursor1 (f, cursor, pointer);
31481 /* EXPORT for RIF:
31482 Clear any mouse-face on window W. This function is part of the
31483 redisplay interface, and is called from try_window_id and similar
31484 functions to ensure the mouse-highlight is off. */
31486 void
31487 x_clear_window_mouse_face (struct window *w)
31489 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
31490 Lisp_Object window;
31492 block_input ();
31493 XSETWINDOW (window, w);
31494 if (EQ (window, hlinfo->mouse_face_window))
31495 clear_mouse_face (hlinfo);
31496 unblock_input ();
31500 /* EXPORT:
31501 Just discard the mouse face information for frame F, if any.
31502 This is used when the size of F is changed. */
31504 void
31505 cancel_mouse_face (struct frame *f)
31507 Lisp_Object window;
31508 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31510 window = hlinfo->mouse_face_window;
31511 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
31512 reset_mouse_highlight (hlinfo);
31517 /***********************************************************************
31518 Exposure Events
31519 ***********************************************************************/
31521 #ifdef HAVE_WINDOW_SYSTEM
31523 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
31524 which intersects rectangle R. R is in window-relative coordinates. */
31526 static void
31527 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
31528 enum glyph_row_area area)
31530 struct glyph *first = row->glyphs[area];
31531 struct glyph *end = row->glyphs[area] + row->used[area];
31532 struct glyph *last;
31533 int first_x, start_x, x;
31535 if (area == TEXT_AREA && row->fill_line_p)
31536 /* If row extends face to end of line write the whole line. */
31537 draw_glyphs (w, 0, row, area,
31538 0, row->used[area],
31539 DRAW_NORMAL_TEXT, 0);
31540 else
31542 /* Set START_X to the window-relative start position for drawing glyphs of
31543 AREA. The first glyph of the text area can be partially visible.
31544 The first glyphs of other areas cannot. */
31545 start_x = window_box_left_offset (w, area);
31546 x = start_x;
31547 if (area == TEXT_AREA)
31548 x += row->x;
31550 /* Find the first glyph that must be redrawn. */
31551 while (first < end
31552 && x + first->pixel_width < r->x)
31554 x += first->pixel_width;
31555 ++first;
31558 /* Find the last one. */
31559 last = first;
31560 first_x = x;
31561 /* Use a signed int intermediate value to avoid catastrophic
31562 failures due to comparison between signed and unsigned, when
31563 x is negative (can happen for wide images that are hscrolled). */
31564 int r_end = r->x + r->width;
31565 while (last < end && x < r_end)
31567 x += last->pixel_width;
31568 ++last;
31571 /* Repaint. */
31572 if (last > first)
31573 draw_glyphs (w, first_x - start_x, row, area,
31574 first - row->glyphs[area], last - row->glyphs[area],
31575 DRAW_NORMAL_TEXT, 0);
31580 /* Redraw the parts of the glyph row ROW on window W intersecting
31581 rectangle R. R is in window-relative coordinates. Value is
31582 true if mouse-face was overwritten. */
31584 static bool
31585 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
31587 eassert (row->enabled_p);
31589 if (row->mode_line_p || w->pseudo_window_p)
31590 draw_glyphs (w, 0, row, TEXT_AREA,
31591 0, row->used[TEXT_AREA],
31592 DRAW_NORMAL_TEXT, 0);
31593 else
31595 if (row->used[LEFT_MARGIN_AREA])
31596 expose_area (w, row, r, LEFT_MARGIN_AREA);
31597 if (row->used[TEXT_AREA])
31598 expose_area (w, row, r, TEXT_AREA);
31599 if (row->used[RIGHT_MARGIN_AREA])
31600 expose_area (w, row, r, RIGHT_MARGIN_AREA);
31601 draw_row_fringe_bitmaps (w, row);
31604 return row->mouse_face_p;
31608 /* Redraw those parts of glyphs rows during expose event handling that
31609 overlap other rows. Redrawing of an exposed line writes over parts
31610 of lines overlapping that exposed line; this function fixes that.
31612 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
31613 row in W's current matrix that is exposed and overlaps other rows.
31614 LAST_OVERLAPPING_ROW is the last such row. */
31616 static void
31617 expose_overlaps (struct window *w,
31618 struct glyph_row *first_overlapping_row,
31619 struct glyph_row *last_overlapping_row,
31620 XRectangle *r)
31622 struct glyph_row *row;
31624 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
31625 if (row->overlapping_p)
31627 eassert (row->enabled_p && !row->mode_line_p);
31629 row->clip = r;
31630 if (row->used[LEFT_MARGIN_AREA])
31631 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
31633 if (row->used[TEXT_AREA])
31634 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
31636 if (row->used[RIGHT_MARGIN_AREA])
31637 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
31638 row->clip = NULL;
31643 /* Return true if W's cursor intersects rectangle R. */
31645 static bool
31646 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
31648 XRectangle cr, result;
31649 struct glyph *cursor_glyph;
31650 struct glyph_row *row;
31652 if (w->phys_cursor.vpos >= 0
31653 && w->phys_cursor.vpos < w->current_matrix->nrows
31654 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
31655 row->enabled_p)
31656 && row->cursor_in_fringe_p)
31658 /* Cursor is in the fringe. */
31659 cr.x = window_box_right_offset (w,
31660 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
31661 ? RIGHT_MARGIN_AREA
31662 : TEXT_AREA));
31663 cr.y = row->y;
31664 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
31665 cr.height = row->height;
31666 return x_intersect_rectangles (&cr, r, &result);
31669 cursor_glyph = get_phys_cursor_glyph (w);
31670 if (cursor_glyph)
31672 /* r is relative to W's box, but w->phys_cursor.x is relative
31673 to left edge of W's TEXT area. Adjust it. */
31674 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
31675 cr.y = w->phys_cursor.y;
31676 cr.width = cursor_glyph->pixel_width;
31677 cr.height = w->phys_cursor_height;
31678 /* ++KFS: W32 version used W32-specific IntersectRect here, but
31679 I assume the effect is the same -- and this is portable. */
31680 return x_intersect_rectangles (&cr, r, &result);
31682 /* If we don't understand the format, pretend we're not in the hot-spot. */
31683 return false;
31687 /* EXPORT:
31688 Draw a vertical window border to the right of window W if W doesn't
31689 have vertical scroll bars. */
31691 void
31692 x_draw_vertical_border (struct window *w)
31694 struct frame *f = XFRAME (WINDOW_FRAME (w));
31696 /* We could do better, if we knew what type of scroll-bar the adjacent
31697 windows (on either side) have... But we don't :-(
31698 However, I think this works ok. ++KFS 2003-04-25 */
31700 /* Redraw borders between horizontally adjacent windows. Don't
31701 do it for frames with vertical scroll bars because either the
31702 right scroll bar of a window, or the left scroll bar of its
31703 neighbor will suffice as a border. */
31704 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
31705 return;
31707 /* Note: It is necessary to redraw both the left and the right
31708 borders, for when only this single window W is being
31709 redisplayed. */
31710 if (!WINDOW_RIGHTMOST_P (w)
31711 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
31713 int x0, x1, y0, y1;
31715 window_box_edges (w, &x0, &y0, &x1, &y1);
31716 y1 -= 1;
31718 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31719 x1 -= 1;
31721 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
31724 if (!WINDOW_LEFTMOST_P (w)
31725 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
31727 int x0, x1, y0, y1;
31729 window_box_edges (w, &x0, &y0, &x1, &y1);
31730 y1 -= 1;
31732 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31733 x0 -= 1;
31735 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
31740 /* Draw window dividers for window W. */
31742 void
31743 x_draw_right_divider (struct window *w)
31745 struct frame *f = WINDOW_XFRAME (w);
31747 if (w->mini || w->pseudo_window_p)
31748 return;
31749 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31751 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
31752 int x1 = WINDOW_RIGHT_EDGE_X (w);
31753 int y0 = WINDOW_TOP_EDGE_Y (w);
31754 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31756 /* If W is horizontally combined and has a right sibling, don't
31757 draw over any bottom divider. */
31758 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w)
31759 && !NILP (w->parent)
31760 && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (w->parent))
31761 && !NILP (w->next))
31762 y1 -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31764 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31768 static void
31769 x_draw_bottom_divider (struct window *w)
31771 struct frame *f = XFRAME (WINDOW_FRAME (w));
31773 if (w->mini || w->pseudo_window_p)
31774 return;
31775 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31777 int x0 = WINDOW_LEFT_EDGE_X (w);
31778 int x1 = WINDOW_RIGHT_EDGE_X (w);
31779 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31780 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31781 struct window *p = !NILP (w->parent) ? XWINDOW (w->parent) : false;
31783 /* If W is vertically combined and has a sibling below, don't draw
31784 over any right divider. */
31785 if (WINDOW_RIGHT_DIVIDER_WIDTH (w)
31786 && p
31787 && ((WINDOW_VERTICAL_COMBINATION_P (p)
31788 && !NILP (w->next))
31789 || (WINDOW_HORIZONTAL_COMBINATION_P (p)
31790 && NILP (w->next)
31791 && !NILP (p->parent)
31792 && WINDOW_VERTICAL_COMBINATION_P (XWINDOW (p->parent))
31793 && !NILP (XWINDOW (p->parent)->next))))
31794 x1 -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
31796 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31800 /* Redraw the part of window W intersection rectangle FR. Pixel
31801 coordinates in FR are frame-relative. Call this function with
31802 input blocked. Value is true if the exposure overwrites
31803 mouse-face. */
31805 static bool
31806 expose_window (struct window *w, XRectangle *fr)
31808 struct frame *f = XFRAME (w->frame);
31809 XRectangle wr, r;
31810 bool mouse_face_overwritten_p = false;
31812 /* If window is not yet fully initialized, do nothing. This can
31813 happen when toolkit scroll bars are used and a window is split.
31814 Reconfiguring the scroll bar will generate an expose for a newly
31815 created window. */
31816 if (w->current_matrix == NULL)
31817 return false;
31819 /* When we're currently updating the window, display and current
31820 matrix usually don't agree. Arrange for a thorough display
31821 later. */
31822 if (w->must_be_updated_p)
31824 SET_FRAME_GARBAGED (f);
31825 return false;
31828 /* Frame-relative pixel rectangle of W. */
31829 wr.x = WINDOW_LEFT_EDGE_X (w);
31830 wr.y = WINDOW_TOP_EDGE_Y (w);
31831 wr.width = WINDOW_PIXEL_WIDTH (w);
31832 wr.height = WINDOW_PIXEL_HEIGHT (w);
31834 if (x_intersect_rectangles (fr, &wr, &r))
31836 int yb = window_text_bottom_y (w);
31837 struct glyph_row *row;
31838 struct glyph_row *first_overlapping_row, *last_overlapping_row;
31840 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
31841 r.x, r.y, r.width, r.height));
31843 /* Convert to window coordinates. */
31844 r.x -= WINDOW_LEFT_EDGE_X (w);
31845 r.y -= WINDOW_TOP_EDGE_Y (w);
31847 /* Turn off the cursor. */
31848 bool cursor_cleared_p = (!w->pseudo_window_p
31849 && phys_cursor_in_rect_p (w, &r));
31850 if (cursor_cleared_p)
31851 x_clear_cursor (w);
31853 /* If the row containing the cursor extends face to end of line,
31854 then expose_area might overwrite the cursor outside the
31855 rectangle and thus notice_overwritten_cursor might clear
31856 w->phys_cursor_on_p. We remember the original value and
31857 check later if it is changed. */
31858 bool phys_cursor_on_p = w->phys_cursor_on_p;
31860 /* Use a signed int intermediate value to avoid catastrophic
31861 failures due to comparison between signed and unsigned, when
31862 y0 or y1 is negative (can happen for tall images). */
31863 int r_bottom = r.y + r.height;
31865 /* Update lines intersecting rectangle R. */
31866 first_overlapping_row = last_overlapping_row = NULL;
31867 for (row = w->current_matrix->rows;
31868 row->enabled_p;
31869 ++row)
31871 int y0 = row->y;
31872 int y1 = MATRIX_ROW_BOTTOM_Y (row);
31874 if ((y0 >= r.y && y0 < r_bottom)
31875 || (y1 > r.y && y1 < r_bottom)
31876 || (r.y >= y0 && r.y < y1)
31877 || (r_bottom > y0 && r_bottom < y1))
31879 /* A header line may be overlapping, but there is no need
31880 to fix overlapping areas for them. KFS 2005-02-12 */
31881 if (row->overlapping_p && !row->mode_line_p)
31883 if (first_overlapping_row == NULL)
31884 first_overlapping_row = row;
31885 last_overlapping_row = row;
31888 row->clip = fr;
31889 if (expose_line (w, row, &r))
31890 mouse_face_overwritten_p = true;
31891 row->clip = NULL;
31893 else if (row->overlapping_p)
31895 /* We must redraw a row overlapping the exposed area. */
31896 if (y0 < r.y
31897 ? y0 + row->phys_height > r.y
31898 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
31900 if (first_overlapping_row == NULL)
31901 first_overlapping_row = row;
31902 last_overlapping_row = row;
31906 if (y1 >= yb)
31907 break;
31910 /* Display the mode line if there is one. */
31911 if (window_wants_mode_line (w)
31912 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
31913 row->enabled_p)
31914 && row->y < r_bottom)
31916 if (expose_line (w, row, &r))
31917 mouse_face_overwritten_p = true;
31920 if (!w->pseudo_window_p)
31922 /* Fix the display of overlapping rows. */
31923 if (first_overlapping_row)
31924 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
31925 fr);
31927 /* Draw border between windows. */
31928 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31929 x_draw_right_divider (w);
31930 else
31931 x_draw_vertical_border (w);
31933 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31934 x_draw_bottom_divider (w);
31936 /* Turn the cursor on again. */
31937 if (cursor_cleared_p
31938 || (phys_cursor_on_p && !w->phys_cursor_on_p))
31939 update_window_cursor (w, true);
31943 return mouse_face_overwritten_p;
31948 /* Redraw (parts) of all windows in the window tree rooted at W that
31949 intersect R. R contains frame pixel coordinates. Value is
31950 true if the exposure overwrites mouse-face. */
31952 static bool
31953 expose_window_tree (struct window *w, XRectangle *r)
31955 struct frame *f = XFRAME (w->frame);
31956 bool mouse_face_overwritten_p = false;
31958 while (w && !FRAME_GARBAGED_P (f))
31960 mouse_face_overwritten_p
31961 |= (WINDOWP (w->contents)
31962 ? expose_window_tree (XWINDOW (w->contents), r)
31963 : expose_window (w, r));
31965 w = NILP (w->next) ? NULL : XWINDOW (w->next);
31968 return mouse_face_overwritten_p;
31972 /* EXPORT:
31973 Redisplay an exposed area of frame F. X and Y are the upper-left
31974 corner of the exposed rectangle. W and H are width and height of
31975 the exposed area. All are pixel values. W or H zero means redraw
31976 the entire frame. */
31978 void
31979 expose_frame (struct frame *f, int x, int y, int w, int h)
31981 XRectangle r;
31982 bool mouse_face_overwritten_p = false;
31984 TRACE ((stderr, "expose_frame "));
31986 /* No need to redraw if frame will be redrawn soon. */
31987 if (FRAME_GARBAGED_P (f))
31989 TRACE ((stderr, " garbaged\n"));
31990 return;
31993 /* If basic faces haven't been realized yet, there is no point in
31994 trying to redraw anything. This can happen when we get an expose
31995 event while Emacs is starting, e.g. by moving another window. */
31996 if (FRAME_FACE_CACHE (f) == NULL
31997 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
31999 TRACE ((stderr, " no faces\n"));
32000 return;
32003 if (w == 0 || h == 0)
32005 r.x = r.y = 0;
32006 r.width = FRAME_TEXT_WIDTH (f);
32007 r.height = FRAME_TEXT_HEIGHT (f);
32009 else
32011 r.x = x;
32012 r.y = y;
32013 r.width = w;
32014 r.height = h;
32017 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
32018 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
32020 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
32021 if (WINDOWP (f->tool_bar_window))
32022 mouse_face_overwritten_p
32023 |= expose_window (XWINDOW (f->tool_bar_window), &r);
32024 #endif
32026 #ifdef HAVE_X_WINDOWS
32027 #ifndef MSDOS
32028 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
32029 if (WINDOWP (f->menu_bar_window))
32030 mouse_face_overwritten_p
32031 |= expose_window (XWINDOW (f->menu_bar_window), &r);
32032 #endif /* not USE_X_TOOLKIT and not USE_GTK */
32033 #endif
32034 #endif
32036 /* Some window managers support a focus-follows-mouse style with
32037 delayed raising of frames. Imagine a partially obscured frame,
32038 and moving the mouse into partially obscured mouse-face on that
32039 frame. The visible part of the mouse-face will be highlighted,
32040 then the WM raises the obscured frame. With at least one WM, KDE
32041 2.1, Emacs is not getting any event for the raising of the frame
32042 (even tried with SubstructureRedirectMask), only Expose events.
32043 These expose events will draw text normally, i.e. not
32044 highlighted. Which means we must redo the highlight here.
32045 Subsume it under ``we love X''. --gerd 2001-08-15 */
32046 /* Included in Windows version because Windows most likely does not
32047 do the right thing if any third party tool offers
32048 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
32049 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
32051 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
32052 if (f == hlinfo->mouse_face_mouse_frame)
32054 int mouse_x = hlinfo->mouse_face_mouse_x;
32055 int mouse_y = hlinfo->mouse_face_mouse_y;
32056 clear_mouse_face (hlinfo);
32057 note_mouse_highlight (f, mouse_x, mouse_y);
32063 /* EXPORT:
32064 Determine the intersection of two rectangles R1 and R2. Return
32065 the intersection in *RESULT. Value is true if RESULT is not
32066 empty. */
32068 bool
32069 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
32071 XRectangle *left, *right;
32072 XRectangle *upper, *lower;
32073 bool intersection_p = false;
32075 /* Rearrange so that R1 is the left-most rectangle. */
32076 if (r1->x < r2->x)
32077 left = r1, right = r2;
32078 else
32079 left = r2, right = r1;
32081 /* X0 of the intersection is right.x0, if this is inside R1,
32082 otherwise there is no intersection. */
32083 if (right->x <= left->x + left->width)
32085 result->x = right->x;
32087 /* The right end of the intersection is the minimum of
32088 the right ends of left and right. */
32089 result->width = (min (left->x + left->width, right->x + right->width)
32090 - result->x);
32092 /* Same game for Y. */
32093 if (r1->y < r2->y)
32094 upper = r1, lower = r2;
32095 else
32096 upper = r2, lower = r1;
32098 /* The upper end of the intersection is lower.y0, if this is inside
32099 of upper. Otherwise, there is no intersection. */
32100 if (lower->y <= upper->y + upper->height)
32102 result->y = lower->y;
32104 /* The lower end of the intersection is the minimum of the lower
32105 ends of upper and lower. */
32106 result->height = (min (lower->y + lower->height,
32107 upper->y + upper->height)
32108 - result->y);
32109 intersection_p = true;
32113 return intersection_p;
32116 #endif /* HAVE_WINDOW_SYSTEM */
32119 /***********************************************************************
32120 Initialization
32121 ***********************************************************************/
32123 void
32124 syms_of_xdisp (void)
32126 Vwith_echo_area_save_vector = Qnil;
32127 staticpro (&Vwith_echo_area_save_vector);
32129 Vmessage_stack = Qnil;
32130 staticpro (&Vmessage_stack);
32132 /* Non-nil means don't actually do any redisplay. */
32133 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
32135 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
32137 DEFVAR_BOOL("inhibit-message", inhibit_message,
32138 doc: /* Non-nil means calls to `message' are not displayed.
32139 They are still logged to the *Messages* buffer. */);
32140 inhibit_message = 0;
32142 message_dolog_marker1 = Fmake_marker ();
32143 staticpro (&message_dolog_marker1);
32144 message_dolog_marker2 = Fmake_marker ();
32145 staticpro (&message_dolog_marker2);
32146 message_dolog_marker3 = Fmake_marker ();
32147 staticpro (&message_dolog_marker3);
32149 defsubr (&Sset_buffer_redisplay);
32150 #ifdef GLYPH_DEBUG
32151 defsubr (&Sdump_frame_glyph_matrix);
32152 defsubr (&Sdump_glyph_matrix);
32153 defsubr (&Sdump_glyph_row);
32154 defsubr (&Sdump_tool_bar_row);
32155 defsubr (&Strace_redisplay);
32156 defsubr (&Strace_to_stderr);
32157 #endif
32158 #ifdef HAVE_WINDOW_SYSTEM
32159 defsubr (&Stool_bar_height);
32160 defsubr (&Slookup_image_map);
32161 #endif
32162 defsubr (&Sline_pixel_height);
32163 defsubr (&Sformat_mode_line);
32164 defsubr (&Sinvisible_p);
32165 defsubr (&Scurrent_bidi_paragraph_direction);
32166 defsubr (&Swindow_text_pixel_size);
32167 defsubr (&Smove_point_visually);
32168 defsubr (&Sbidi_find_overridden_directionality);
32170 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
32171 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
32172 DEFSYM (Qoverriding_local_map, "overriding-local-map");
32173 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
32174 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
32175 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
32176 DEFSYM (Qeval, "eval");
32177 DEFSYM (QCdata, ":data");
32179 /* Names of text properties relevant for redisplay. */
32180 DEFSYM (Qdisplay, "display");
32181 DEFSYM (Qspace_width, "space-width");
32182 DEFSYM (Qraise, "raise");
32183 DEFSYM (Qslice, "slice");
32184 DEFSYM (Qspace, "space");
32185 DEFSYM (Qmargin, "margin");
32186 DEFSYM (Qpointer, "pointer");
32187 DEFSYM (Qleft_margin, "left-margin");
32188 DEFSYM (Qright_margin, "right-margin");
32189 DEFSYM (Qcenter, "center");
32190 DEFSYM (Qline_height, "line-height");
32191 DEFSYM (QCalign_to, ":align-to");
32192 DEFSYM (QCrelative_width, ":relative-width");
32193 DEFSYM (QCrelative_height, ":relative-height");
32194 DEFSYM (QCeval, ":eval");
32195 DEFSYM (QCpropertize, ":propertize");
32196 DEFSYM (QCfile, ":file");
32197 DEFSYM (Qfontified, "fontified");
32198 DEFSYM (Qfontification_functions, "fontification-functions");
32200 /* Name of the face used to highlight trailing whitespace. */
32201 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
32203 /* Names of the faces used to display line numbers. */
32204 DEFSYM (Qline_number, "line-number");
32205 DEFSYM (Qline_number_current_line, "line-number-current-line");
32206 /* Name of a text property which disables line-number display. */
32207 DEFSYM (Qdisplay_line_numbers_disable, "display-line-numbers-disable");
32209 /* Name and number of the face used to highlight escape glyphs. */
32210 DEFSYM (Qescape_glyph, "escape-glyph");
32212 /* Name and number of the face used to highlight non-breaking
32213 spaces/hyphens. */
32214 DEFSYM (Qnobreak_space, "nobreak-space");
32215 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
32217 /* The symbol 'image' which is the car of the lists used to represent
32218 images in Lisp. Also a tool bar style. */
32219 DEFSYM (Qimage, "image");
32221 /* Tool bar styles. */
32222 DEFSYM (Qtext, "text");
32223 DEFSYM (Qboth, "both");
32224 DEFSYM (Qboth_horiz, "both-horiz");
32225 DEFSYM (Qtext_image_horiz, "text-image-horiz");
32227 /* The image map types. */
32228 DEFSYM (QCmap, ":map");
32229 DEFSYM (QCpointer, ":pointer");
32230 DEFSYM (Qrect, "rect");
32231 DEFSYM (Qcircle, "circle");
32232 DEFSYM (Qpoly, "poly");
32234 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
32236 DEFSYM (Qgrow_only, "grow-only");
32237 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
32238 DEFSYM (Qposition, "position");
32239 DEFSYM (Qbuffer_position, "buffer-position");
32240 DEFSYM (Qobject, "object");
32242 /* Cursor shapes. */
32243 DEFSYM (Qbar, "bar");
32244 DEFSYM (Qhbar, "hbar");
32245 DEFSYM (Qbox, "box");
32246 DEFSYM (Qhollow, "hollow");
32248 /* Pointer shapes. */
32249 DEFSYM (Qhand, "hand");
32250 DEFSYM (Qarrow, "arrow");
32251 /* also Qtext */
32253 DEFSYM (Qdragging, "dragging");
32255 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
32257 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
32258 staticpro (&list_of_error);
32260 /* Values of those variables at last redisplay are stored as
32261 properties on 'overlay-arrow-position' symbol. However, if
32262 Voverlay_arrow_position is a marker, last-arrow-position is its
32263 numerical position. */
32264 DEFSYM (Qlast_arrow_position, "last-arrow-position");
32265 DEFSYM (Qlast_arrow_string, "last-arrow-string");
32267 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
32268 properties on a symbol in overlay-arrow-variable-list. */
32269 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
32270 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
32272 echo_buffer[0] = echo_buffer[1] = Qnil;
32273 staticpro (&echo_buffer[0]);
32274 staticpro (&echo_buffer[1]);
32276 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
32277 staticpro (&echo_area_buffer[0]);
32278 staticpro (&echo_area_buffer[1]);
32280 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
32281 staticpro (&Vmessages_buffer_name);
32283 mode_line_proptrans_alist = Qnil;
32284 staticpro (&mode_line_proptrans_alist);
32285 mode_line_string_list = Qnil;
32286 staticpro (&mode_line_string_list);
32287 mode_line_string_face = Qnil;
32288 staticpro (&mode_line_string_face);
32289 mode_line_string_face_prop = Qnil;
32290 staticpro (&mode_line_string_face_prop);
32291 Vmode_line_unwind_vector = Qnil;
32292 staticpro (&Vmode_line_unwind_vector);
32294 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
32296 help_echo_string = Qnil;
32297 staticpro (&help_echo_string);
32298 help_echo_object = Qnil;
32299 staticpro (&help_echo_object);
32300 help_echo_window = Qnil;
32301 staticpro (&help_echo_window);
32302 previous_help_echo_string = Qnil;
32303 staticpro (&previous_help_echo_string);
32304 help_echo_pos = -1;
32306 DEFSYM (Qright_to_left, "right-to-left");
32307 DEFSYM (Qleft_to_right, "left-to-right");
32308 defsubr (&Sbidi_resolved_levels);
32310 #ifdef HAVE_WINDOW_SYSTEM
32311 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
32312 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
32313 For example, if a block cursor is over a tab, it will be drawn as
32314 wide as that tab on the display. */);
32315 x_stretch_cursor_p = 0;
32316 #endif
32318 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
32319 doc: /* Non-nil means highlight trailing whitespace.
32320 The face used for trailing whitespace is `trailing-whitespace'. */);
32321 Vshow_trailing_whitespace = Qnil;
32323 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
32324 doc: /* Control highlighting of non-ASCII space and hyphen chars.
32325 If the value is t, Emacs highlights non-ASCII chars which have the
32326 same appearance as an ASCII space or hyphen, using the `nobreak-space'
32327 or `nobreak-hyphen' face respectively.
32329 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
32330 U+2011 (non-breaking hyphen) are affected.
32332 Any other non-nil value means to display these characters as a escape
32333 glyph followed by an ordinary space or hyphen.
32335 A value of nil means no special handling of these characters. */);
32336 Vnobreak_char_display = Qt;
32338 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
32339 doc: /* The pointer shape to show in void text areas.
32340 A value of nil means to show the text pointer. Other options are
32341 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
32342 `hourglass'. */);
32343 Vvoid_text_area_pointer = Qarrow;
32345 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
32346 doc: /* Non-nil means don't actually do any redisplay.
32347 This is used for internal purposes. */);
32348 Vinhibit_redisplay = Qnil;
32350 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
32351 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
32352 Vglobal_mode_string = Qnil;
32354 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
32355 doc: /* Marker for where to display an arrow on top of the buffer text.
32356 This must be the beginning of a line in order to work.
32357 See also `overlay-arrow-string'. */);
32358 Voverlay_arrow_position = Qnil;
32360 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
32361 doc: /* String to display as an arrow in non-window frames.
32362 See also `overlay-arrow-position'. */);
32363 Voverlay_arrow_string = build_pure_c_string ("=>");
32365 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
32366 doc: /* List of variables (symbols) which hold markers for overlay arrows.
32367 The symbols on this list are examined during redisplay to determine
32368 where to display overlay arrows. */);
32369 Voverlay_arrow_variable_list
32370 = list1 (intern_c_string ("overlay-arrow-position"));
32372 DEFVAR_INT ("scroll-step", emacs_scroll_step,
32373 doc: /* The number of lines to try scrolling a window by when point moves out.
32374 If that fails to bring point back on frame, point is centered instead.
32375 If this is zero, point is always centered after it moves off frame.
32376 If you want scrolling to always be a line at a time, you should set
32377 `scroll-conservatively' to a large value rather than set this to 1. */);
32379 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
32380 doc: /* Scroll up to this many lines, to bring point back on screen.
32381 If point moves off-screen, redisplay will scroll by up to
32382 `scroll-conservatively' lines in order to bring point just barely
32383 onto the screen again. If that cannot be done, then redisplay
32384 recenters point as usual.
32386 If the value is greater than 100, redisplay will never recenter point,
32387 but will always scroll just enough text to bring point into view, even
32388 if you move far away.
32390 A value of zero means always recenter point if it moves off screen. */);
32391 scroll_conservatively = 0;
32393 DEFVAR_INT ("scroll-margin", scroll_margin,
32394 doc: /* Number of lines of margin at the top and bottom of a window.
32395 Recenter the window whenever point gets within this many lines
32396 of the top or bottom of the window. */);
32397 scroll_margin = 0;
32399 DEFVAR_LISP ("maximum-scroll-margin", Vmaximum_scroll_margin,
32400 doc: /* Maximum effective value of `scroll-margin'.
32401 Given as a fraction of the current window's lines. The value should
32402 be a floating point number between 0.0 and 0.5. The effective maximum
32403 is limited to (/ (1- window-lines) 2). Non-float values for this
32404 variable are ignored and the default 0.25 is used instead. */);
32405 Vmaximum_scroll_margin = make_float (0.25);
32407 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
32408 doc: /* Pixels per inch value for non-window system displays.
32409 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
32410 Vdisplay_pixels_per_inch = make_float (72.0);
32412 #ifdef GLYPH_DEBUG
32413 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
32414 #endif
32416 DEFVAR_LISP ("truncate-partial-width-windows",
32417 Vtruncate_partial_width_windows,
32418 doc: /* Non-nil means truncate lines in windows narrower than the frame.
32419 For an integer value, truncate lines in each window narrower than the
32420 full frame width, provided the total window width in column units is less
32421 than that integer; otherwise, respect the value of `truncate-lines'.
32422 The total width of the window is as returned by `window-total-width', it
32423 includes the fringes, the continuation and truncation glyphs, the
32424 display margins (if any), and the scroll bar
32426 For any other non-nil value, truncate lines in all windows that do
32427 not span the full frame width.
32429 A value of nil means to respect the value of `truncate-lines'.
32431 If `word-wrap' is enabled, you might want to reduce this. */);
32432 Vtruncate_partial_width_windows = make_number (50);
32434 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
32435 doc: /* Maximum buffer size for which line number should be displayed.
32436 If the buffer is bigger than this, the line number does not appear
32437 in the mode line. A value of nil means no limit. */);
32438 Vline_number_display_limit = Qnil;
32440 DEFVAR_INT ("line-number-display-limit-width",
32441 line_number_display_limit_width,
32442 doc: /* Maximum line width (in characters) for line number display.
32443 If the average length of the lines near point is bigger than this, then the
32444 line number may be omitted from the mode line. */);
32445 line_number_display_limit_width = 200;
32447 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
32448 doc: /* Non-nil means highlight region even in nonselected windows. */);
32449 highlight_nonselected_windows = false;
32451 DEFVAR_BOOL ("multiple-frames", multiple_frames,
32452 doc: /* Non-nil if more than one frame is visible on this display.
32453 Minibuffer-only frames don't count, but iconified frames do.
32454 This variable is not guaranteed to be accurate except while processing
32455 `frame-title-format' and `icon-title-format'. */);
32457 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
32458 doc: /* Template for displaying the title bar of visible frames.
32459 \(Assuming the window manager supports this feature.)
32461 This variable has the same structure as `mode-line-format', except that
32462 the %c, %C, and %l constructs are ignored. It is used only on frames for
32463 which no explicit name has been set (see `modify-frame-parameters'). */);
32465 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
32466 doc: /* Template for displaying the title bar of an iconified frame.
32467 \(Assuming the window manager supports this feature.)
32468 This variable has the same structure as `mode-line-format' (which see),
32469 and is used only on frames for which no explicit name has been set
32470 \(see `modify-frame-parameters'). */);
32471 Vicon_title_format
32472 = Vframe_title_format
32473 = listn (CONSTYPE_PURE, 3,
32474 intern_c_string ("multiple-frames"),
32475 build_pure_c_string ("%b"),
32476 listn (CONSTYPE_PURE, 4,
32477 empty_unibyte_string,
32478 intern_c_string ("invocation-name"),
32479 build_pure_c_string ("@"),
32480 intern_c_string ("system-name")));
32482 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
32483 doc: /* Maximum number of lines to keep in the message log buffer.
32484 If nil, disable message logging. If t, log messages but don't truncate
32485 the buffer when it becomes large. */);
32486 Vmessage_log_max = make_number (1000);
32488 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
32489 doc: /* List of functions to call before redisplaying a window with scrolling.
32490 Each function is called with two arguments, the window and its new
32491 display-start position.
32492 These functions are called whenever the `window-start' marker is modified,
32493 either to point into another buffer (e.g. via `set-window-buffer') or another
32494 place in the same buffer.
32495 Note that the value of `window-end' is not valid when these functions are
32496 called.
32498 Warning: Do not use this feature to alter the way the window
32499 is scrolled. It is not designed for that, and such use probably won't
32500 work. */);
32501 Vwindow_scroll_functions = Qnil;
32503 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
32504 doc: /* Functions called when redisplay of a window reaches the end trigger.
32505 Each function is called with two arguments, the window and the end trigger value.
32506 See `set-window-redisplay-end-trigger'. */);
32507 Vredisplay_end_trigger_functions = Qnil;
32509 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
32510 doc: /* Non-nil means autoselect window with mouse pointer.
32511 If nil, do not autoselect windows.
32512 A positive number means delay autoselection by that many seconds: a
32513 window is autoselected only after the mouse has remained in that
32514 window for the duration of the delay.
32515 A negative number has a similar effect, but causes windows to be
32516 autoselected only after the mouse has stopped moving. (Because of
32517 the way Emacs compares mouse events, you will occasionally wait twice
32518 that time before the window gets selected.)
32519 Any other value means to autoselect window instantaneously when the
32520 mouse pointer enters it.
32522 Autoselection selects the minibuffer only if it is active, and never
32523 unselects the minibuffer if it is active.
32525 When customizing this variable make sure that the actual value of
32526 `focus-follows-mouse' matches the behavior of your window manager. */);
32527 Vmouse_autoselect_window = Qnil;
32529 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
32530 doc: /* Non-nil means automatically resize tool-bars.
32531 This dynamically changes the tool-bar's height to the minimum height
32532 that is needed to make all tool-bar items visible.
32533 If value is `grow-only', the tool-bar's height is only increased
32534 automatically; to decrease the tool-bar height, use \\[recenter]. */);
32535 Vauto_resize_tool_bars = Qt;
32537 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
32538 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
32539 auto_raise_tool_bar_buttons_p = true;
32541 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
32542 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
32543 make_cursor_line_fully_visible_p = true;
32545 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
32546 doc: /* Border below tool-bar in pixels.
32547 If an integer, use it as the height of the border.
32548 If it is one of `internal-border-width' or `border-width', use the
32549 value of the corresponding frame parameter.
32550 Otherwise, no border is added below the tool-bar. */);
32551 Vtool_bar_border = Qinternal_border_width;
32553 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
32554 doc: /* Margin around tool-bar buttons in pixels.
32555 If an integer, use that for both horizontal and vertical margins.
32556 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
32557 HORZ specifying the horizontal margin, and VERT specifying the
32558 vertical margin. */);
32559 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
32561 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
32562 doc: /* Relief thickness of tool-bar buttons. */);
32563 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
32565 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
32566 doc: /* Tool bar style to use.
32567 It can be one of
32568 image - show images only
32569 text - show text only
32570 both - show both, text below image
32571 both-horiz - show text to the right of the image
32572 text-image-horiz - show text to the left of the image
32573 any other - use system default or image if no system default.
32575 This variable only affects the GTK+ toolkit version of Emacs. */);
32576 Vtool_bar_style = Qnil;
32578 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
32579 doc: /* Maximum number of characters a label can have to be shown.
32580 The tool bar style must also show labels for this to have any effect, see
32581 `tool-bar-style'. */);
32582 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
32584 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
32585 doc: /* List of functions to call to fontify regions of text.
32586 Each function is called with one argument POS. Functions must
32587 fontify a region starting at POS in the current buffer, and give
32588 fontified regions the property `fontified'. */);
32589 Vfontification_functions = Qnil;
32590 Fmake_variable_buffer_local (Qfontification_functions);
32592 DEFVAR_BOOL ("unibyte-display-via-language-environment",
32593 unibyte_display_via_language_environment,
32594 doc: /* Non-nil means display unibyte text according to language environment.
32595 Specifically, this means that raw bytes in the range 160-255 decimal
32596 are displayed by converting them to the equivalent multibyte characters
32597 according to the current language environment. As a result, they are
32598 displayed according to the current fontset.
32600 Note that this variable affects only how these bytes are displayed,
32601 but does not change the fact they are interpreted as raw bytes. */);
32602 unibyte_display_via_language_environment = false;
32604 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
32605 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
32606 If a float, it specifies a fraction of the mini-window frame's height.
32607 If an integer, it specifies a number of lines. */);
32608 Vmax_mini_window_height = make_float (0.25);
32610 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
32611 doc: /* How to resize mini-windows (the minibuffer and the echo area).
32612 A value of nil means don't automatically resize mini-windows.
32613 A value of t means resize them to fit the text displayed in them.
32614 A value of `grow-only', the default, means let mini-windows grow only;
32615 they return to their normal size when the minibuffer is closed, or the
32616 echo area becomes empty. */);
32617 /* Contrary to the doc string, we initialize this to nil, so that
32618 loading loadup.el won't try to resize windows before loading
32619 window.el, where some functions we need to call for this live.
32620 We assign the 'grow-only' value right after loading window.el
32621 during loadup. */
32622 Vresize_mini_windows = Qnil;
32624 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
32625 doc: /* Alist specifying how to blink the cursor off.
32626 Each element has the form (ON-STATE . OFF-STATE). Whenever the
32627 `cursor-type' frame-parameter or variable equals ON-STATE,
32628 comparing using `equal', Emacs uses OFF-STATE to specify
32629 how to blink it off. ON-STATE and OFF-STATE are values for
32630 the `cursor-type' frame parameter.
32632 If a frame's ON-STATE has no entry in this list,
32633 the frame's other specifications determine how to blink the cursor off. */);
32634 Vblink_cursor_alist = Qnil;
32636 DEFVAR_LISP ("auto-hscroll-mode", automatic_hscrolling,
32637 doc: /* Allow or disallow automatic horizontal scrolling of windows.
32638 The value `current-line' means the line displaying point in each window
32639 is automatically scrolled horizontally to make point visible.
32640 Any other non-nil value means all the lines in a window are automatically
32641 scrolled horizontally to make point visible. */);
32642 automatic_hscrolling = Qt;
32643 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
32644 DEFSYM (Qcurrent_line, "current-line");
32646 DEFVAR_INT ("hscroll-margin", hscroll_margin,
32647 doc: /* How many columns away from the window edge point is allowed to get
32648 before automatic hscrolling will horizontally scroll the window. */);
32649 hscroll_margin = 5;
32651 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
32652 doc: /* How many columns to scroll the window when point gets too close to the edge.
32653 When point is less than `hscroll-margin' columns from the window
32654 edge, automatic hscrolling will scroll the window by the amount of columns
32655 determined by this variable. If its value is a positive integer, scroll that
32656 many columns. If it's a positive floating-point number, it specifies the
32657 fraction of the window's width to scroll. If it's nil or zero, point will be
32658 centered horizontally after the scroll. Any other value, including negative
32659 numbers, are treated as if the value were zero.
32661 Automatic hscrolling always moves point outside the scroll margin, so if
32662 point was more than scroll step columns inside the margin, the window will
32663 scroll more than the value given by the scroll step.
32665 Note that the lower bound for automatic hscrolling specified by `scroll-left'
32666 and `scroll-right' overrides this variable's effect. */);
32667 Vhscroll_step = make_number (0);
32669 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
32670 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
32671 Bind this around calls to `message' to let it take effect. */);
32672 message_truncate_lines = false;
32674 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
32675 doc: /* Normal hook run to update the menu bar definitions.
32676 Redisplay runs this hook before it redisplays the menu bar.
32677 This is used to update menus such as Buffers, whose contents depend on
32678 various data. */);
32679 Vmenu_bar_update_hook = Qnil;
32681 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
32682 doc: /* Frame for which we are updating a menu.
32683 The enable predicate for a menu binding should check this variable. */);
32684 Vmenu_updating_frame = Qnil;
32686 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
32687 doc: /* Non-nil means don't update menu bars. Internal use only. */);
32688 inhibit_menubar_update = false;
32690 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
32691 doc: /* Prefix prepended to all continuation lines at display time.
32692 The value may be a string, an image, or a stretch-glyph; it is
32693 interpreted in the same way as the value of a `display' text property.
32695 This variable is overridden by any `wrap-prefix' text or overlay
32696 property.
32698 To add a prefix to non-continuation lines, use `line-prefix'. */);
32699 Vwrap_prefix = Qnil;
32700 DEFSYM (Qwrap_prefix, "wrap-prefix");
32701 Fmake_variable_buffer_local (Qwrap_prefix);
32703 DEFVAR_LISP ("line-prefix", Vline_prefix,
32704 doc: /* Prefix prepended to all non-continuation lines at display time.
32705 The value may be a string, an image, or a stretch-glyph; it is
32706 interpreted in the same way as the value of a `display' text property.
32708 This variable is overridden by any `line-prefix' text or overlay
32709 property.
32711 To add a prefix to continuation lines, use `wrap-prefix'. */);
32712 Vline_prefix = Qnil;
32713 DEFSYM (Qline_prefix, "line-prefix");
32714 Fmake_variable_buffer_local (Qline_prefix);
32716 DEFVAR_LISP ("display-line-numbers", Vdisplay_line_numbers,
32717 doc: /* Non-nil means display line numbers.
32718 If the value is t, display the absolute number of each line of a buffer
32719 shown in a window. Absolute line numbers count from the beginning of
32720 the current narrowing, or from buffer beginning. If the value is
32721 `relative', display for each line not containing the window's point its
32722 relative number instead, i.e. the number of the line relative to the
32723 line showing the window's point.
32725 In either case, line numbers are displayed at the beginning of each
32726 non-continuation line that displays buffer text, i.e. after each newline
32727 character that comes from the buffer. The value `visual' is like
32728 `relative' but counts screen lines instead of buffer lines. In practice
32729 this means that continuation lines count as well when calculating the
32730 relative number of a line.
32732 Lisp programs can disable display of a line number of a particular
32733 buffer line by putting the `display-line-numbers-disable' text property
32734 or overlay property on the first visible character of that line. */);
32735 Vdisplay_line_numbers = Qnil;
32736 DEFSYM (Qdisplay_line_numbers, "display-line-numbers");
32737 Fmake_variable_buffer_local (Qdisplay_line_numbers);
32738 DEFSYM (Qrelative, "relative");
32739 DEFSYM (Qvisual, "visual");
32741 DEFVAR_LISP ("display-line-numbers-width", Vdisplay_line_numbers_width,
32742 doc: /* Minimum width of space reserved for line number display.
32743 A positive number means reserve that many columns for line numbers,
32744 even if the actual number needs less space.
32745 The default value of nil means compute the space dynamically.
32746 Any other value is treated as nil. */);
32747 Vdisplay_line_numbers_width = Qnil;
32748 DEFSYM (Qdisplay_line_numbers_width, "display-line-numbers-width");
32749 Fmake_variable_buffer_local (Qdisplay_line_numbers_width);
32751 DEFVAR_LISP ("display-line-numbers-current-absolute",
32752 Vdisplay_line_numbers_current_absolute,
32753 doc: /* Non-nil means display absolute number of current line.
32754 This variable has effect only when `display-line-numbers' is
32755 either `relative' or `visual'. */);
32756 Vdisplay_line_numbers_current_absolute = Qt;
32758 DEFVAR_BOOL ("display-line-numbers-widen", display_line_numbers_widen,
32759 doc: /* Non-nil means display line numbers disregarding any narrowing. */);
32760 display_line_numbers_widen = false;
32761 DEFSYM (Qdisplay_line_numbers_widen, "display-line-numbers-widen");
32762 Fmake_variable_buffer_local (Qdisplay_line_numbers_widen);
32764 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
32765 doc: /* Non-nil means don't eval Lisp during redisplay. */);
32766 inhibit_eval_during_redisplay = false;
32768 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
32769 doc: /* Non-nil means don't free realized faces. Internal use only. */);
32770 inhibit_free_realized_faces = false;
32772 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
32773 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
32774 Intended for use during debugging and for testing bidi display;
32775 see biditest.el in the test suite. */);
32776 inhibit_bidi_mirroring = false;
32778 #ifdef GLYPH_DEBUG
32779 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
32780 doc: /* Inhibit try_window_id display optimization. */);
32781 inhibit_try_window_id = false;
32783 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
32784 doc: /* Inhibit try_window_reusing display optimization. */);
32785 inhibit_try_window_reusing = false;
32787 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
32788 doc: /* Inhibit try_cursor_movement display optimization. */);
32789 inhibit_try_cursor_movement = false;
32790 #endif /* GLYPH_DEBUG */
32792 DEFVAR_INT ("overline-margin", overline_margin,
32793 doc: /* Space between overline and text, in pixels.
32794 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
32795 margin to the character height. */);
32796 overline_margin = 2;
32798 DEFVAR_INT ("underline-minimum-offset",
32799 underline_minimum_offset,
32800 doc: /* Minimum distance between baseline and underline.
32801 This can improve legibility of underlined text at small font sizes,
32802 particularly when using variable `x-use-underline-position-properties'
32803 with fonts that specify an UNDERLINE_POSITION relatively close to the
32804 baseline. The default value is 1. */);
32805 underline_minimum_offset = 1;
32807 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
32808 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
32809 This feature only works when on a window system that can change
32810 cursor shapes. */);
32811 display_hourglass_p = true;
32813 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
32814 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
32815 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
32817 #ifdef HAVE_WINDOW_SYSTEM
32818 hourglass_atimer = NULL;
32819 hourglass_shown_p = false;
32820 #endif /* HAVE_WINDOW_SYSTEM */
32822 /* Name of the face used to display glyphless characters. */
32823 DEFSYM (Qglyphless_char, "glyphless-char");
32825 /* Method symbols for Vglyphless_char_display. */
32826 DEFSYM (Qhex_code, "hex-code");
32827 DEFSYM (Qempty_box, "empty-box");
32828 DEFSYM (Qthin_space, "thin-space");
32829 DEFSYM (Qzero_width, "zero-width");
32831 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
32832 doc: /* Function run just before redisplay.
32833 It is called with one argument, which is the set of windows that are to
32834 be redisplayed. This set can be nil (meaning, only the selected window),
32835 or t (meaning all windows). */);
32836 Vpre_redisplay_function = intern ("ignore");
32838 /* Symbol for the purpose of Vglyphless_char_display. */
32839 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
32840 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
32842 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
32843 doc: /* Char-table defining glyphless characters.
32844 Each element, if non-nil, should be one of the following:
32845 an ASCII acronym string: display this string in a box
32846 `hex-code': display the hexadecimal code of a character in a box
32847 `empty-box': display as an empty box
32848 `thin-space': display as 1-pixel width space
32849 `zero-width': don't display
32850 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
32851 display method for graphical terminals and text terminals respectively.
32852 GRAPHICAL and TEXT should each have one of the values listed above.
32854 The char-table has one extra slot to control the display of a character for
32855 which no font is found. This slot only takes effect on graphical terminals.
32856 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
32857 `thin-space'. The default is `empty-box'.
32859 If a character has a non-nil entry in an active display table, the
32860 display table takes effect; in this case, Emacs does not consult
32861 `glyphless-char-display' at all. */);
32862 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
32863 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
32864 Qempty_box);
32866 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
32867 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
32868 Vdebug_on_message = Qnil;
32870 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
32871 doc: /* */);
32872 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
32874 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
32875 doc: /* */);
32876 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
32878 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
32879 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
32880 /* Initialize to t, since we need to disable reordering until
32881 loadup.el successfully loads charprop.el. */
32882 redisplay__inhibit_bidi = true;
32884 DEFVAR_BOOL ("display-raw-bytes-as-hex", display_raw_bytes_as_hex,
32885 doc: /* Non-nil means display raw bytes in hexadecimal format.
32886 The default is to use octal format (\200) whereas hexadecimal (\x80)
32887 may be more familiar to users. */);
32888 display_raw_bytes_as_hex = false;
32893 /* Initialize this module when Emacs starts. */
32895 void
32896 init_xdisp (void)
32898 CHARPOS (this_line_start_pos) = 0;
32900 if (!noninteractive)
32902 struct window *m = XWINDOW (minibuf_window);
32903 Lisp_Object frame = m->frame;
32904 struct frame *f = XFRAME (frame);
32905 Lisp_Object root = FRAME_ROOT_WINDOW (f);
32906 struct window *r = XWINDOW (root);
32907 int i;
32909 echo_area_window = minibuf_window;
32911 r->top_line = FRAME_TOP_MARGIN (f);
32912 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
32913 r->total_cols = FRAME_COLS (f);
32914 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
32915 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
32916 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
32918 m->top_line = FRAME_TOTAL_LINES (f) - 1;
32919 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
32920 m->total_cols = FRAME_COLS (f);
32921 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
32922 m->total_lines = 1;
32923 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
32925 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
32926 scratch_glyph_row.glyphs[TEXT_AREA + 1]
32927 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
32929 /* The default ellipsis glyphs `...'. */
32930 for (i = 0; i < 3; ++i)
32931 default_invis_vector[i] = make_number ('.');
32935 /* Allocate the buffer for frame titles.
32936 Also used for `format-mode-line'. */
32937 int size = 100;
32938 mode_line_noprop_buf = xmalloc (size);
32939 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
32940 mode_line_noprop_ptr = mode_line_noprop_buf;
32941 mode_line_target = MODE_LINE_DISPLAY;
32944 help_echo_showing_p = false;
32947 #ifdef HAVE_WINDOW_SYSTEM
32949 /* Platform-independent portion of hourglass implementation. */
32951 /* Timer function of hourglass_atimer. */
32953 static void
32954 show_hourglass (struct atimer *timer)
32956 /* The timer implementation will cancel this timer automatically
32957 after this function has run. Set hourglass_atimer to null
32958 so that we know the timer doesn't have to be canceled. */
32959 hourglass_atimer = NULL;
32961 if (!hourglass_shown_p)
32963 Lisp_Object tail, frame;
32965 block_input ();
32967 FOR_EACH_FRAME (tail, frame)
32969 struct frame *f = XFRAME (frame);
32971 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32972 && FRAME_RIF (f)->show_hourglass)
32973 FRAME_RIF (f)->show_hourglass (f);
32976 hourglass_shown_p = true;
32977 unblock_input ();
32981 /* Cancel a currently active hourglass timer, and start a new one. */
32983 void
32984 start_hourglass (void)
32986 struct timespec delay;
32988 cancel_hourglass ();
32990 if (INTEGERP (Vhourglass_delay)
32991 && XINT (Vhourglass_delay) > 0)
32992 delay = make_timespec (min (XINT (Vhourglass_delay),
32993 TYPE_MAXIMUM (time_t)),
32995 else if (FLOATP (Vhourglass_delay)
32996 && XFLOAT_DATA (Vhourglass_delay) > 0)
32997 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
32998 else
32999 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
33001 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
33002 show_hourglass, NULL);
33005 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
33006 shown. */
33008 void
33009 cancel_hourglass (void)
33011 if (hourglass_atimer)
33013 cancel_atimer (hourglass_atimer);
33014 hourglass_atimer = NULL;
33017 if (hourglass_shown_p)
33019 Lisp_Object tail, frame;
33021 block_input ();
33023 FOR_EACH_FRAME (tail, frame)
33025 struct frame *f = XFRAME (frame);
33027 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
33028 && FRAME_RIF (f)->hide_hourglass)
33029 FRAME_RIF (f)->hide_hourglass (f);
33030 #ifdef HAVE_NTGUI
33031 /* No cursors on non GUI frames - restore to stock arrow cursor. */
33032 else if (!FRAME_W32_P (f))
33033 w32_arrow_cursor ();
33034 #endif
33037 hourglass_shown_p = false;
33038 unblock_input ();
33042 #endif /* HAVE_WINDOW_SYSTEM */